2012-09-26 11:09:40 +02:00
|
|
|
/* Module signature checker
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public Licence
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the Licence, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2015-10-21 15:04:48 +02:00
|
|
|
#include <linux/errno.h>
|
2016-03-01 11:36:07 +01:00
|
|
|
#include <linux/string.h>
|
2013-08-30 17:07:30 +02:00
|
|
|
#include <keys/system_keyring.h>
|
2015-07-20 22:16:27 +02:00
|
|
|
#include <crypto/public_key.h>
|
2012-09-26 11:09:40 +02:00
|
|
|
#include "module-internal.h"
|
|
|
|
|
2016-03-03 22:49:27 +01:00
|
|
|
enum pkey_id_type {
|
|
|
|
PKEY_ID_PGP, /* OpenPGP generated key ID */
|
|
|
|
PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */
|
|
|
|
PKEY_ID_PKCS7, /* Signature in PKCS#7 message */
|
|
|
|
};
|
|
|
|
|
2012-09-26 11:11:03 +02:00
|
|
|
/*
|
|
|
|
* Module signature information block.
|
|
|
|
*
|
|
|
|
* The constituents of the signature section are, in order:
|
|
|
|
*
|
|
|
|
* - Signer's name
|
|
|
|
* - Key identifier
|
|
|
|
* - Signature data
|
|
|
|
* - Information block
|
|
|
|
*/
|
|
|
|
struct module_signature {
|
2015-07-20 22:16:27 +02:00
|
|
|
u8 algo; /* Public-key crypto algorithm [0] */
|
|
|
|
u8 hash; /* Digest algorithm [0] */
|
|
|
|
u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */
|
|
|
|
u8 signer_len; /* Length of signer's name [0] */
|
|
|
|
u8 key_id_len; /* Length of key identifier [0] */
|
2012-10-22 16:05:48 +02:00
|
|
|
u8 __pad[3];
|
|
|
|
__be32 sig_len; /* Length of signature data */
|
2012-09-26 11:11:03 +02:00
|
|
|
};
|
|
|
|
|
2012-09-26 11:09:40 +02:00
|
|
|
/*
|
|
|
|
* Verify the signature on a module.
|
|
|
|
*/
|
2012-10-20 02:19:29 +02:00
|
|
|
int mod_verify_sig(const void *mod, unsigned long *_modlen)
|
2012-09-26 11:09:40 +02:00
|
|
|
{
|
2012-09-26 11:11:03 +02:00
|
|
|
struct module_signature ms;
|
2012-10-20 02:19:29 +02:00
|
|
|
size_t modlen = *_modlen, sig_len;
|
2012-09-26 11:11:03 +02:00
|
|
|
|
2012-10-21 03:59:31 +02:00
|
|
|
pr_devel("==>%s(,%zu)\n", __func__, modlen);
|
2012-09-26 11:11:03 +02:00
|
|
|
|
2012-10-20 02:19:29 +02:00
|
|
|
if (modlen <= sizeof(ms))
|
2012-09-26 11:11:03 +02:00
|
|
|
return -EBADMSG;
|
|
|
|
|
2012-10-20 02:19:29 +02:00
|
|
|
memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
|
|
|
|
modlen -= sizeof(ms);
|
2012-09-26 11:11:03 +02:00
|
|
|
|
|
|
|
sig_len = be32_to_cpu(ms.sig_len);
|
2012-10-20 02:19:29 +02:00
|
|
|
if (sig_len >= modlen)
|
2012-09-26 11:11:03 +02:00
|
|
|
return -EBADMSG;
|
2012-10-20 02:19:29 +02:00
|
|
|
modlen -= sig_len;
|
|
|
|
*_modlen = modlen;
|
2012-09-26 11:11:03 +02:00
|
|
|
|
2015-07-20 22:16:27 +02:00
|
|
|
if (ms.id_type != PKEY_ID_PKCS7) {
|
|
|
|
pr_err("Module is not signed with expected PKCS#7 message\n");
|
2012-09-26 11:11:03 +02:00
|
|
|
return -ENOPKG;
|
|
|
|
}
|
|
|
|
|
2015-07-20 22:16:27 +02:00
|
|
|
if (ms.algo != 0 ||
|
|
|
|
ms.hash != 0 ||
|
|
|
|
ms.signer_len != 0 ||
|
|
|
|
ms.key_id_len != 0 ||
|
|
|
|
ms.__pad[0] != 0 ||
|
|
|
|
ms.__pad[1] != 0 ||
|
|
|
|
ms.__pad[2] != 0) {
|
|
|
|
pr_err("PKCS#7 signature info has unexpected non-zero params\n");
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
2012-09-26 11:11:03 +02:00
|
|
|
|
2016-04-06 17:14:24 +02:00
|
|
|
return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
|
|
|
|
NULL, -ENOKEY, VERIFYING_MODULE_SIGNATURE,
|
|
|
|
NULL, NULL);
|
2012-09-26 11:09:40 +02:00
|
|
|
}
|