1 /* Module signature checker
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/err.h>
14 #include <keys/system_keyring.h>
15 #include <crypto/public_key.h>
16 #include <crypto/pkcs7.h>
17 #include "module-internal.h"
20 * Module signature information block.
22 * The constituents of the signature section are, in order:
29 struct module_signature {
30 u8 algo; /* Public-key crypto algorithm [0] */
31 u8 hash; /* Digest algorithm [0] */
32 u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */
33 u8 signer_len; /* Length of signer's name [0] */
34 u8 key_id_len; /* Length of key identifier [0] */
36 __be32 sig_len; /* Length of signature data */
40 * Verify a PKCS#7-based signature on a module.
42 static int mod_verify_pkcs7(const void *mod, unsigned long modlen,
43 const void *raw_pkcs7, size_t pkcs7_len)
45 struct pkcs7_message *pkcs7;
49 pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
51 return PTR_ERR(pkcs7);
53 /* The data should be detached - so we need to supply it. */
54 if (pkcs7_supply_detached_data(pkcs7, mod, modlen) < 0) {
55 pr_err("PKCS#7 signature with non-detached data\n");
60 ret = pkcs7_verify(pkcs7);
64 ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
69 pr_err("PKCS#7 signature not signed with a trusted key\n");
74 pkcs7_free_message(pkcs7);
75 pr_devel("<==%s() = %d\n", __func__, ret);
80 * Verify the signature on a module.
82 int mod_verify_sig(const void *mod, unsigned long *_modlen)
84 struct module_signature ms;
85 size_t modlen = *_modlen, sig_len;
87 pr_devel("==>%s(,%zu)\n", __func__, modlen);
89 if (modlen <= sizeof(ms))
92 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
95 sig_len = be32_to_cpu(ms.sig_len);
96 if (sig_len >= modlen)
101 if (ms.id_type != PKEY_ID_PKCS7) {
102 pr_err("Module is not signed with expected PKCS#7 message\n");
108 ms.signer_len != 0 ||
109 ms.key_id_len != 0 ||
113 pr_err("PKCS#7 signature info has unexpected non-zero params\n");
117 return mod_verify_pkcs7(mod, modlen, mod + modlen, sig_len);