x509_public_key.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. size_t srlen, sulen;
  96. char *desc = NULL;
  97. int ret;
  98. cert = x509_cert_parse(prep->data, prep->datalen);
  99. if (IS_ERR(cert))
  100. return PTR_ERR(cert);
  101. pr_devel("Cert Issuer: %s\n", cert->issuer);
  102. pr_devel("Cert Subject: %s\n", cert->subject);
  103. if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
  104. cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
  105. cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
  106. !pkey_algo[cert->pub->pkey_algo] ||
  107. !pkey_algo[cert->sig.pkey_algo] ||
  108. !pkey_hash_algo_name[cert->sig.pkey_hash_algo]) {
  109. ret = -ENOPKG;
  110. goto error_free_cert;
  111. }
  112. pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
  113. pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
  114. cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
  115. cert->valid_from.tm_mday, cert->valid_from.tm_hour,
  116. cert->valid_from.tm_min, cert->valid_from.tm_sec);
  117. pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
  118. cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
  119. cert->valid_to.tm_mday, cert->valid_to.tm_hour,
  120. cert->valid_to.tm_min, cert->valid_to.tm_sec);
  121. pr_devel("Cert Signature: %s + %s\n",
  122. pkey_algo_name[cert->sig.pkey_algo],
  123. pkey_hash_algo_name[cert->sig.pkey_hash_algo]);
  124. if (!cert->fingerprint) {
  125. pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
  126. cert->subject);
  127. ret = -EKEYREJECTED;
  128. goto error_free_cert;
  129. }
  130. cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
  131. cert->pub->id_type = PKEY_ID_X509;
  132. /* Check the signature on the key if it appears to be self-signed */
  133. if (!cert->authority ||
  134. strcmp(cert->fingerprint, cert->authority) == 0) {
  135. ret = x509_check_signature(cert->pub, cert);
  136. if (ret < 0)
  137. goto error_free_cert;
  138. }
  139. /* Propose a description */
  140. sulen = strlen(cert->subject);
  141. srlen = strlen(cert->fingerprint);
  142. ret = -ENOMEM;
  143. desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
  144. if (!desc)
  145. goto error_free_cert;
  146. memcpy(desc, cert->subject, sulen);
  147. desc[sulen] = ':';
  148. desc[sulen + 1] = ' ';
  149. memcpy(desc + sulen + 2, cert->fingerprint, srlen);
  150. desc[sulen + 2 + srlen] = 0;
  151. /* We're pinning the module by being linked against it */
  152. __module_get(public_key_subtype.owner);
  153. prep->type_data[0] = &public_key_subtype;
  154. prep->type_data[1] = cert->fingerprint;
  155. prep->payload = cert->pub;
  156. prep->description = desc;
  157. prep->quotalen = 100;
  158. /* We've finished with the certificate */
  159. cert->pub = NULL;
  160. cert->fingerprint = NULL;
  161. desc = NULL;
  162. ret = 0;
  163. error_free_cert:
  164. x509_free_certificate(cert);
  165. return ret;
  166. }
  167. static struct asymmetric_key_parser x509_key_parser = {
  168. .owner = THIS_MODULE,
  169. .name = "x509",
  170. .parse = x509_key_preparse,
  171. };
  172. /*
  173. * Module stuff
  174. */
  175. static int __init x509_key_init(void)
  176. {
  177. return register_asymmetric_key_parser(&x509_key_parser);
  178. }
  179. static void __exit x509_key_exit(void)
  180. {
  181. unregister_asymmetric_key_parser(&x509_key_parser);
  182. }
  183. module_init(x509_key_init);
  184. module_exit(x509_key_exit);