module_signing.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Module signature checker
  2. *
  3. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  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.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/err.h>
  13. #include <crypto/public_key.h>
  14. #include <crypto/hash.h>
  15. #include <keys/asymmetric-type.h>
  16. #include "module-internal.h"
  17. /*
  18. * Module signature information block.
  19. *
  20. * The constituents of the signature section are, in order:
  21. *
  22. * - Signer's name
  23. * - Key identifier
  24. * - Signature data
  25. * - Information block
  26. */
  27. struct module_signature {
  28. enum pkey_algo algo : 8; /* Public-key crypto algorithm */
  29. enum pkey_hash_algo hash : 8; /* Digest algorithm */
  30. enum pkey_id_type id_type : 8; /* Key identifier type */
  31. u8 signer_len; /* Length of signer's name */
  32. u8 key_id_len; /* Length of key identifier */
  33. u8 __pad[3];
  34. __be32 sig_len; /* Length of signature data */
  35. };
  36. /*
  37. * Digest the module contents.
  38. */
  39. static struct public_key_signature *mod_make_digest(enum pkey_hash_algo hash,
  40. const void *mod,
  41. unsigned long modlen)
  42. {
  43. struct public_key_signature *pks;
  44. struct crypto_shash *tfm;
  45. struct shash_desc *desc;
  46. size_t digest_size, desc_size;
  47. int ret;
  48. pr_devel("==>%s()\n", __func__);
  49. /* Allocate the hashing algorithm we're going to need and find out how
  50. * big the hash operational data will be.
  51. */
  52. tfm = crypto_alloc_shash(pkey_hash_algo[hash], 0, 0);
  53. if (IS_ERR(tfm))
  54. return (PTR_ERR(tfm) == -ENOENT) ? ERR_PTR(-ENOPKG) : ERR_CAST(tfm);
  55. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  56. digest_size = crypto_shash_digestsize(tfm);
  57. /* We allocate the hash operational data storage on the end of our
  58. * context data and the digest output buffer on the end of that.
  59. */
  60. ret = -ENOMEM;
  61. pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
  62. if (!pks)
  63. goto error_no_pks;
  64. pks->pkey_hash_algo = hash;
  65. pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
  66. pks->digest_size = digest_size;
  67. desc = (void *)pks + sizeof(*pks);
  68. desc->tfm = tfm;
  69. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  70. ret = crypto_shash_init(desc);
  71. if (ret < 0)
  72. goto error;
  73. ret = crypto_shash_finup(desc, mod, modlen, pks->digest);
  74. if (ret < 0)
  75. goto error;
  76. crypto_free_shash(tfm);
  77. pr_devel("<==%s() = ok\n", __func__);
  78. return pks;
  79. error:
  80. kfree(pks);
  81. error_no_pks:
  82. crypto_free_shash(tfm);
  83. pr_devel("<==%s() = %d\n", __func__, ret);
  84. return ERR_PTR(ret);
  85. }
  86. /*
  87. * Extract an MPI array from the signature data. This represents the actual
  88. * signature. Each raw MPI is prefaced by a BE 2-byte value indicating the
  89. * size of the MPI in bytes.
  90. *
  91. * RSA signatures only have one MPI, so currently we only read one.
  92. */
  93. static int mod_extract_mpi_array(struct public_key_signature *pks,
  94. const void *data, size_t len)
  95. {
  96. size_t nbytes;
  97. MPI mpi;
  98. if (len < 3)
  99. return -EBADMSG;
  100. nbytes = ((const u8 *)data)[0] << 8 | ((const u8 *)data)[1];
  101. data += 2;
  102. len -= 2;
  103. if (len != nbytes)
  104. return -EBADMSG;
  105. mpi = mpi_read_raw_data(data, nbytes);
  106. if (!mpi)
  107. return -ENOMEM;
  108. pks->mpi[0] = mpi;
  109. pks->nr_mpi = 1;
  110. return 0;
  111. }
  112. /*
  113. * Request an asymmetric key.
  114. */
  115. static struct key *request_asymmetric_key(const char *signer, size_t signer_len,
  116. const u8 *key_id, size_t key_id_len)
  117. {
  118. key_ref_t key;
  119. size_t i;
  120. char *id, *q;
  121. pr_devel("==>%s(,%zu,,%zu)\n", __func__, signer_len, key_id_len);
  122. /* Construct an identifier. */
  123. id = kmalloc(signer_len + 2 + key_id_len * 2 + 1, GFP_KERNEL);
  124. if (!id)
  125. return ERR_PTR(-ENOKEY);
  126. memcpy(id, signer, signer_len);
  127. q = id + signer_len;
  128. *q++ = ':';
  129. *q++ = ' ';
  130. for (i = 0; i < key_id_len; i++) {
  131. *q++ = hex_asc[*key_id >> 4];
  132. *q++ = hex_asc[*key_id++ & 0x0f];
  133. }
  134. *q = 0;
  135. pr_debug("Look up: \"%s\"\n", id);
  136. key = keyring_search(make_key_ref(modsign_keyring, 1),
  137. &key_type_asymmetric, id);
  138. if (IS_ERR(key))
  139. pr_warn("Request for unknown module key '%s' err %ld\n",
  140. id, PTR_ERR(key));
  141. kfree(id);
  142. if (IS_ERR(key)) {
  143. switch (PTR_ERR(key)) {
  144. /* Hide some search errors */
  145. case -EACCES:
  146. case -ENOTDIR:
  147. case -EAGAIN:
  148. return ERR_PTR(-ENOKEY);
  149. default:
  150. return ERR_CAST(key);
  151. }
  152. }
  153. pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key)));
  154. return key_ref_to_ptr(key);
  155. }
  156. /*
  157. * Verify the signature on a module.
  158. */
  159. int mod_verify_sig(const void *mod, unsigned long modlen,
  160. const void *sig, unsigned long siglen)
  161. {
  162. struct public_key_signature *pks;
  163. struct module_signature ms;
  164. struct key *key;
  165. size_t sig_len;
  166. int ret;
  167. pr_devel("==>%s(,%lu,,%lu,)\n", __func__, modlen, siglen);
  168. if (siglen <= sizeof(ms))
  169. return -EBADMSG;
  170. memcpy(&ms, sig + (siglen - sizeof(ms)), sizeof(ms));
  171. siglen -= sizeof(ms);
  172. sig_len = be32_to_cpu(ms.sig_len);
  173. if (sig_len >= siglen ||
  174. siglen - sig_len != (size_t)ms.signer_len + ms.key_id_len)
  175. return -EBADMSG;
  176. /* For the moment, only support RSA and X.509 identifiers */
  177. if (ms.algo != PKEY_ALGO_RSA ||
  178. ms.id_type != PKEY_ID_X509)
  179. return -ENOPKG;
  180. if (ms.hash >= PKEY_HASH__LAST ||
  181. !pkey_hash_algo[ms.hash])
  182. return -ENOPKG;
  183. key = request_asymmetric_key(sig, ms.signer_len,
  184. sig + ms.signer_len, ms.key_id_len);
  185. if (IS_ERR(key))
  186. return PTR_ERR(key);
  187. pks = mod_make_digest(ms.hash, mod, modlen);
  188. if (IS_ERR(pks)) {
  189. ret = PTR_ERR(pks);
  190. goto error_put_key;
  191. }
  192. ret = mod_extract_mpi_array(pks, sig + ms.signer_len + ms.key_id_len,
  193. sig_len);
  194. if (ret < 0)
  195. goto error_free_pks;
  196. ret = verify_signature(key, pks);
  197. pr_devel("verify_signature() = %d\n", ret);
  198. error_free_pks:
  199. mpi_free(pks->rsa.s);
  200. kfree(pks);
  201. error_put_key:
  202. key_put(key);
  203. pr_devel("<==%s() = %d\n", __func__, ret);
  204. return ret;
  205. }