x509_public_key.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <crypto/hash.h>
  21. #include "asymmetric_keys.h"
  22. #include "public_key.h"
  23. #include "x509_parser.h"
  24. /*
  25. * Set up the signature parameters in an X.509 certificate. This involves
  26. * digesting the signed data and extracting the signature.
  27. */
  28. int x509_get_sig_params(struct x509_certificate *cert)
  29. {
  30. struct crypto_shash *tfm;
  31. struct shash_desc *desc;
  32. size_t digest_size, desc_size;
  33. void *digest;
  34. int ret;
  35. pr_devel("==>%s()\n", __func__);
  36. if (cert->sig.rsa.s)
  37. return 0;
  38. cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
  39. if (!cert->sig.rsa.s)
  40. return -ENOMEM;
  41. cert->sig.nr_mpi = 1;
  42. /* Allocate the hashing algorithm we're going to need and find out how
  43. * big the hash operational data will be.
  44. */
  45. tfm = crypto_alloc_shash(pkey_hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
  46. if (IS_ERR(tfm))
  47. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  48. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  49. digest_size = crypto_shash_digestsize(tfm);
  50. /* We allocate the hash operational data storage on the end of the
  51. * digest storage space.
  52. */
  53. ret = -ENOMEM;
  54. digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
  55. if (!digest)
  56. goto error;
  57. cert->sig.digest = digest;
  58. cert->sig.digest_size = digest_size;
  59. desc = digest + digest_size;
  60. desc->tfm = tfm;
  61. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  62. ret = crypto_shash_init(desc);
  63. if (ret < 0)
  64. goto error;
  65. might_sleep();
  66. ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
  67. error:
  68. crypto_free_shash(tfm);
  69. pr_devel("<==%s() = %d\n", __func__, ret);
  70. return ret;
  71. }
  72. EXPORT_SYMBOL_GPL(x509_get_sig_params);
  73. /*
  74. * Check the signature on a certificate using the provided public key
  75. */
  76. int x509_check_signature(const struct public_key *pub,
  77. struct x509_certificate *cert)
  78. {
  79. int ret;
  80. pr_devel("==>%s()\n", __func__);
  81. ret = x509_get_sig_params(cert);
  82. if (ret < 0)
  83. return ret;
  84. ret = public_key_verify_signature(pub, &cert->sig);
  85. pr_debug("Cert Verification: %d\n", ret);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL_GPL(x509_check_signature);
  89. /*
  90. * Attempt to parse a data blob for a key as an X509 certificate.
  91. */
  92. static int x509_key_preparse(struct key_preparsed_payload *prep)
  93. {
  94. struct x509_certificate *cert;
  95. struct tm now;
  96. size_t srlen, sulen;
  97. char *desc = NULL;
  98. int ret;
  99. cert = x509_cert_parse(prep->data, prep->datalen);
  100. if (IS_ERR(cert))
  101. return PTR_ERR(cert);
  102. pr_devel("Cert Issuer: %s\n", cert->issuer);
  103. pr_devel("Cert Subject: %s\n", cert->subject);
  104. pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
  105. pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
  106. cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
  107. cert->valid_from.tm_mday, cert->valid_from.tm_hour,
  108. cert->valid_from.tm_min, cert->valid_from.tm_sec);
  109. pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
  110. cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
  111. cert->valid_to.tm_mday, cert->valid_to.tm_hour,
  112. cert->valid_to.tm_min, cert->valid_to.tm_sec);
  113. pr_devel("Cert Signature: %s + %s\n",
  114. pkey_algo_name[cert->sig.pkey_algo],
  115. pkey_hash_algo_name[cert->sig.pkey_hash_algo]);
  116. if (!cert->fingerprint || !cert->authority) {
  117. pr_warn("Cert for '%s' must have SubjKeyId and AuthKeyId extensions\n",
  118. cert->subject);
  119. ret = -EKEYREJECTED;
  120. goto error_free_cert;
  121. }
  122. time_to_tm(CURRENT_TIME.tv_sec, 0, &now);
  123. pr_devel("Now: %04ld-%02d-%02d %02d:%02d:%02d\n",
  124. now.tm_year + 1900, now.tm_mon + 1, now.tm_mday,
  125. now.tm_hour, now.tm_min, now.tm_sec);
  126. if (now.tm_year < cert->valid_from.tm_year ||
  127. (now.tm_year == cert->valid_from.tm_year &&
  128. (now.tm_mon < cert->valid_from.tm_mon ||
  129. (now.tm_mon == cert->valid_from.tm_mon &&
  130. (now.tm_mday < cert->valid_from.tm_mday ||
  131. (now.tm_mday == cert->valid_from.tm_mday &&
  132. (now.tm_hour < cert->valid_from.tm_hour ||
  133. (now.tm_hour == cert->valid_from.tm_hour &&
  134. (now.tm_min < cert->valid_from.tm_min ||
  135. (now.tm_min == cert->valid_from.tm_min &&
  136. (now.tm_sec < cert->valid_from.tm_sec
  137. ))))))))))) {
  138. pr_warn("Cert %s is not yet valid\n", cert->fingerprint);
  139. ret = -EKEYREJECTED;
  140. goto error_free_cert;
  141. }
  142. if (now.tm_year > cert->valid_to.tm_year ||
  143. (now.tm_year == cert->valid_to.tm_year &&
  144. (now.tm_mon > cert->valid_to.tm_mon ||
  145. (now.tm_mon == cert->valid_to.tm_mon &&
  146. (now.tm_mday > cert->valid_to.tm_mday ||
  147. (now.tm_mday == cert->valid_to.tm_mday &&
  148. (now.tm_hour > cert->valid_to.tm_hour ||
  149. (now.tm_hour == cert->valid_to.tm_hour &&
  150. (now.tm_min > cert->valid_to.tm_min ||
  151. (now.tm_min == cert->valid_to.tm_min &&
  152. (now.tm_sec > cert->valid_to.tm_sec
  153. ))))))))))) {
  154. pr_warn("Cert %s has expired\n", cert->fingerprint);
  155. ret = -EKEYEXPIRED;
  156. goto error_free_cert;
  157. }
  158. cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
  159. cert->pub->id_type = PKEY_ID_X509;
  160. /* Check the signature on the key */
  161. if (strcmp(cert->fingerprint, cert->authority) == 0) {
  162. ret = x509_check_signature(cert->pub, cert);
  163. if (ret < 0)
  164. goto error_free_cert;
  165. }
  166. /* Propose a description */
  167. sulen = strlen(cert->subject);
  168. srlen = strlen(cert->fingerprint);
  169. ret = -ENOMEM;
  170. desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
  171. if (!desc)
  172. goto error_free_cert;
  173. memcpy(desc, cert->subject, sulen);
  174. desc[sulen] = ':';
  175. desc[sulen + 1] = ' ';
  176. memcpy(desc + sulen + 2, cert->fingerprint, srlen);
  177. desc[sulen + 2 + srlen] = 0;
  178. /* We're pinning the module by being linked against it */
  179. __module_get(public_key_subtype.owner);
  180. prep->type_data[0] = &public_key_subtype;
  181. prep->type_data[1] = cert->fingerprint;
  182. prep->payload = cert->pub;
  183. prep->description = desc;
  184. prep->quotalen = 100;
  185. /* We've finished with the certificate */
  186. cert->pub = NULL;
  187. cert->fingerprint = NULL;
  188. desc = NULL;
  189. ret = 0;
  190. error_free_cert:
  191. x509_free_certificate(cert);
  192. return ret;
  193. }
  194. static struct asymmetric_key_parser x509_key_parser = {
  195. .owner = THIS_MODULE,
  196. .name = "x509",
  197. .parse = x509_key_preparse,
  198. };
  199. /*
  200. * Module stuff
  201. */
  202. static int __init x509_key_init(void)
  203. {
  204. return register_asymmetric_key_parser(&x509_key_parser);
  205. }
  206. static void __exit x509_key_exit(void)
  207. {
  208. unregister_asymmetric_key_parser(&x509_key_parser);
  209. }
  210. module_init(x509_key_init);
  211. module_exit(x509_key_exit);