x509_public_key.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Instantiate a public key crypto key from an X.509 Certificate
  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. #define pr_fmt(fmt) "X.509: "fmt
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/mpi.h>
  17. #include <linux/asn1_decoder.h>
  18. #include <keys/asymmetric-subtype.h>
  19. #include <keys/asymmetric-parser.h>
  20. #include <keys/system_keyring.h>
  21. #include <crypto/hash.h>
  22. #include "asymmetric_keys.h"
  23. #include "public_key.h"
  24. #include "x509_parser.h"
  25. /*
  26. * Find a key in the given keyring by issuer and authority.
  27. */
  28. static struct key *x509_request_asymmetric_key(
  29. struct key *keyring,
  30. const char *signer, size_t signer_len,
  31. const char *authority, size_t auth_len)
  32. {
  33. key_ref_t key;
  34. char *id;
  35. /* Construct an identifier. */
  36. id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL);
  37. if (!id)
  38. return ERR_PTR(-ENOMEM);
  39. memcpy(id, signer, signer_len);
  40. id[signer_len + 0] = ':';
  41. id[signer_len + 1] = ' ';
  42. memcpy(id + signer_len + 2, authority, auth_len);
  43. id[signer_len + 2 + auth_len] = 0;
  44. pr_debug("Look up: \"%s\"\n", id);
  45. key = keyring_search(make_key_ref(keyring, 1),
  46. &key_type_asymmetric, id);
  47. if (IS_ERR(key))
  48. pr_debug("Request for module key '%s' err %ld\n",
  49. id, PTR_ERR(key));
  50. kfree(id);
  51. if (IS_ERR(key)) {
  52. switch (PTR_ERR(key)) {
  53. /* Hide some search errors */
  54. case -EACCES:
  55. case -ENOTDIR:
  56. case -EAGAIN:
  57. return ERR_PTR(-ENOKEY);
  58. default:
  59. return ERR_CAST(key);
  60. }
  61. }
  62. pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key)));
  63. return key_ref_to_ptr(key);
  64. }
  65. /*
  66. * Set up the signature parameters in an X.509 certificate. This involves
  67. * digesting the signed data and extracting the signature.
  68. */
  69. int x509_get_sig_params(struct x509_certificate *cert)
  70. {
  71. struct crypto_shash *tfm;
  72. struct shash_desc *desc;
  73. size_t digest_size, desc_size;
  74. void *digest;
  75. int ret;
  76. pr_devel("==>%s()\n", __func__);
  77. if (cert->sig.rsa.s)
  78. return 0;
  79. cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
  80. if (!cert->sig.rsa.s)
  81. return -ENOMEM;
  82. cert->sig.nr_mpi = 1;
  83. /* Allocate the hashing algorithm we're going to need and find out how
  84. * big the hash operational data will be.
  85. */
  86. tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
  87. if (IS_ERR(tfm))
  88. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  89. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  90. digest_size = crypto_shash_digestsize(tfm);
  91. /* We allocate the hash operational data storage on the end of the
  92. * digest storage space.
  93. */
  94. ret = -ENOMEM;
  95. digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
  96. if (!digest)
  97. goto error;
  98. cert->sig.digest = digest;
  99. cert->sig.digest_size = digest_size;
  100. desc = digest + digest_size;
  101. desc->tfm = tfm;
  102. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  103. ret = crypto_shash_init(desc);
  104. if (ret < 0)
  105. goto error;
  106. might_sleep();
  107. ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
  108. error:
  109. crypto_free_shash(tfm);
  110. pr_devel("<==%s() = %d\n", __func__, ret);
  111. return ret;
  112. }
  113. EXPORT_SYMBOL_GPL(x509_get_sig_params);
  114. /*
  115. * Check the signature on a certificate using the provided public key
  116. */
  117. int x509_check_signature(const struct public_key *pub,
  118. struct x509_certificate *cert)
  119. {
  120. int ret;
  121. pr_devel("==>%s()\n", __func__);
  122. ret = x509_get_sig_params(cert);
  123. if (ret < 0)
  124. return ret;
  125. ret = public_key_verify_signature(pub, &cert->sig);
  126. pr_debug("Cert Verification: %d\n", ret);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL_GPL(x509_check_signature);
  130. /*
  131. * Check the new certificate against the ones in the trust keyring. If one of
  132. * those is the signing key and validates the new certificate, then mark the
  133. * new certificate as being trusted.
  134. *
  135. * Return 0 if the new certificate was successfully validated, 1 if we couldn't
  136. * find a matching parent certificate in the trusted list and an error if there
  137. * is a matching certificate but the signature check fails.
  138. */
  139. static int x509_validate_trust(struct x509_certificate *cert,
  140. struct key *trust_keyring)
  141. {
  142. const struct public_key *pk;
  143. struct key *key;
  144. int ret = 1;
  145. key = x509_request_asymmetric_key(trust_keyring,
  146. cert->issuer, strlen(cert->issuer),
  147. cert->authority,
  148. strlen(cert->authority));
  149. if (!IS_ERR(key)) {
  150. pk = key->payload.data;
  151. ret = x509_check_signature(pk, cert);
  152. }
  153. return ret;
  154. }
  155. /*
  156. * Attempt to parse a data blob for a key as an X509 certificate.
  157. */
  158. static int x509_key_preparse(struct key_preparsed_payload *prep)
  159. {
  160. struct x509_certificate *cert;
  161. size_t srlen, sulen;
  162. char *desc = NULL;
  163. int ret;
  164. cert = x509_cert_parse(prep->data, prep->datalen);
  165. if (IS_ERR(cert))
  166. return PTR_ERR(cert);
  167. pr_devel("Cert Issuer: %s\n", cert->issuer);
  168. pr_devel("Cert Subject: %s\n", cert->subject);
  169. if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
  170. cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
  171. cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
  172. !pkey_algo[cert->pub->pkey_algo] ||
  173. !pkey_algo[cert->sig.pkey_algo] ||
  174. !hash_algo_name[cert->sig.pkey_hash_algo]) {
  175. ret = -ENOPKG;
  176. goto error_free_cert;
  177. }
  178. pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
  179. pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
  180. cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
  181. cert->valid_from.tm_mday, cert->valid_from.tm_hour,
  182. cert->valid_from.tm_min, cert->valid_from.tm_sec);
  183. pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
  184. cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
  185. cert->valid_to.tm_mday, cert->valid_to.tm_hour,
  186. cert->valid_to.tm_min, cert->valid_to.tm_sec);
  187. pr_devel("Cert Signature: %s + %s\n",
  188. pkey_algo_name[cert->sig.pkey_algo],
  189. hash_algo_name[cert->sig.pkey_hash_algo]);
  190. if (!cert->fingerprint) {
  191. pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
  192. cert->subject);
  193. ret = -EKEYREJECTED;
  194. goto error_free_cert;
  195. }
  196. cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
  197. cert->pub->id_type = PKEY_ID_X509;
  198. /* Check the signature on the key if it appears to be self-signed */
  199. if (!cert->authority ||
  200. strcmp(cert->fingerprint, cert->authority) == 0) {
  201. ret = x509_check_signature(cert->pub, cert); /* self-signed */
  202. if (ret < 0)
  203. goto error_free_cert;
  204. } else {
  205. ret = x509_validate_trust(cert, system_trusted_keyring);
  206. if (!ret)
  207. prep->trusted = 1;
  208. }
  209. /* Propose a description */
  210. sulen = strlen(cert->subject);
  211. srlen = strlen(cert->fingerprint);
  212. ret = -ENOMEM;
  213. desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
  214. if (!desc)
  215. goto error_free_cert;
  216. memcpy(desc, cert->subject, sulen);
  217. desc[sulen] = ':';
  218. desc[sulen + 1] = ' ';
  219. memcpy(desc + sulen + 2, cert->fingerprint, srlen);
  220. desc[sulen + 2 + srlen] = 0;
  221. /* We're pinning the module by being linked against it */
  222. __module_get(public_key_subtype.owner);
  223. prep->type_data[0] = &public_key_subtype;
  224. prep->type_data[1] = cert->fingerprint;
  225. prep->payload = cert->pub;
  226. prep->description = desc;
  227. prep->quotalen = 100;
  228. /* We've finished with the certificate */
  229. cert->pub = NULL;
  230. cert->fingerprint = NULL;
  231. desc = NULL;
  232. ret = 0;
  233. error_free_cert:
  234. x509_free_certificate(cert);
  235. return ret;
  236. }
  237. static struct asymmetric_key_parser x509_key_parser = {
  238. .owner = THIS_MODULE,
  239. .name = "x509",
  240. .parse = x509_key_preparse,
  241. };
  242. /*
  243. * Module stuff
  244. */
  245. static int __init x509_key_init(void)
  246. {
  247. return register_asymmetric_key_parser(&x509_key_parser);
  248. }
  249. static void __exit x509_key_exit(void)
  250. {
  251. unregister_asymmetric_key_parser(&x509_key_parser);
  252. }
  253. module_init(x509_key_init);
  254. module_exit(x509_key_exit);
  255. MODULE_DESCRIPTION("X.509 certificate parser");
  256. MODULE_LICENSE("GPL");