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 <crypto/public_key.h>
15 #include <crypto/hash.h>
16 #include <keys/asymmetric-type.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 [enum pkey_algo] */
31 u8 hash; /* Digest algorithm [enum pkey_hash_algo] */
32 u8 id_type; /* Key identifier type [enum pkey_id_type] */
33 u8 signer_len; /* Length of signer's name */
34 u8 key_id_len; /* Length of key identifier */
36 __be32 sig_len; /* Length of signature data */
40 * Digest the module contents.
42 static struct public_key_signature *mod_make_digest(enum pkey_hash_algo hash,
46 struct public_key_signature *pks;
47 struct crypto_shash *tfm;
48 struct shash_desc *desc;
49 size_t digest_size, desc_size;
52 pr_devel("==>%s()\n", __func__);
54 /* Allocate the hashing algorithm we're going to need and find out how
55 * big the hash operational data will be.
57 tfm = crypto_alloc_shash(pkey_hash_algo[hash], 0, 0);
59 return (PTR_ERR(tfm) == -ENOENT) ? ERR_PTR(-ENOPKG) : ERR_CAST(tfm);
61 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
62 digest_size = crypto_shash_digestsize(tfm);
64 /* We allocate the hash operational data storage on the end of our
65 * context data and the digest output buffer on the end of that.
68 pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
72 pks->pkey_hash_algo = hash;
73 pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
74 pks->digest_size = digest_size;
76 desc = (void *)pks + sizeof(*pks);
78 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
80 ret = crypto_shash_init(desc);
84 ret = crypto_shash_finup(desc, mod, modlen, pks->digest);
88 crypto_free_shash(tfm);
89 pr_devel("<==%s() = ok\n", __func__);
95 crypto_free_shash(tfm);
96 pr_devel("<==%s() = %d\n", __func__, ret);
101 * Extract an MPI array from the signature data. This represents the actual
102 * signature. Each raw MPI is prefaced by a BE 2-byte value indicating the
103 * size of the MPI in bytes.
105 * RSA signatures only have one MPI, so currently we only read one.
107 static int mod_extract_mpi_array(struct public_key_signature *pks,
108 const void *data, size_t len)
115 nbytes = ((const u8 *)data)[0] << 8 | ((const u8 *)data)[1];
121 mpi = mpi_read_raw_data(data, nbytes);
130 * Request an asymmetric key.
132 static struct key *request_asymmetric_key(const char *signer, size_t signer_len,
133 const u8 *key_id, size_t key_id_len)
139 pr_devel("==>%s(,%zu,,%zu)\n", __func__, signer_len, key_id_len);
141 /* Construct an identifier. */
142 id = kmalloc(signer_len + 2 + key_id_len * 2 + 1, GFP_KERNEL);
144 return ERR_PTR(-ENOKEY);
146 memcpy(id, signer, signer_len);
151 for (i = 0; i < key_id_len; i++) {
152 *q++ = hex_asc[*key_id >> 4];
153 *q++ = hex_asc[*key_id++ & 0x0f];
158 pr_debug("Look up: \"%s\"\n", id);
160 key = keyring_search(make_key_ref(modsign_keyring, 1),
161 &key_type_asymmetric, id);
163 pr_warn("Request for unknown module key '%s' err %ld\n",
168 switch (PTR_ERR(key)) {
169 /* Hide some search errors */
173 return ERR_PTR(-ENOKEY);
175 return ERR_CAST(key);
179 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key)));
180 return key_ref_to_ptr(key);
184 * Verify the signature on a module.
186 int mod_verify_sig(const void *mod, unsigned long *_modlen)
188 struct public_key_signature *pks;
189 struct module_signature ms;
192 size_t modlen = *_modlen, sig_len;
195 pr_devel("==>%s(,%zu)\n", __func__, modlen);
197 if (modlen <= sizeof(ms))
200 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
201 modlen -= sizeof(ms);
203 sig_len = be32_to_cpu(ms.sig_len);
204 if (sig_len >= modlen)
207 if ((size_t)ms.signer_len + ms.key_id_len >= modlen)
209 modlen -= (size_t)ms.signer_len + ms.key_id_len;
214 /* For the moment, only support RSA and X.509 identifiers */
215 if (ms.algo != PKEY_ALGO_RSA ||
216 ms.id_type != PKEY_ID_X509)
219 if (ms.hash >= PKEY_HASH__LAST ||
220 !pkey_hash_algo[ms.hash])
223 key = request_asymmetric_key(sig, ms.signer_len,
224 sig + ms.signer_len, ms.key_id_len);
228 pks = mod_make_digest(ms.hash, mod, modlen);
234 ret = mod_extract_mpi_array(pks, sig + ms.signer_len + ms.key_id_len,
239 ret = verify_signature(key, pks);
240 pr_devel("verify_signature() = %d\n", ret);
243 mpi_free(pks->rsa.s);
247 pr_devel("<==%s() = %d\n", __func__, ret);