x509_public_key.c 6.6 KB

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