x509_public_key.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Instantiate a public key crypto key from an X.509 Certificate
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) "X.509: "fmt
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <keys/asymmetric-subtype.h>
  12. #include <keys/asymmetric-parser.h>
  13. #include <keys/system_keyring.h>
  14. #include <crypto/hash.h>
  15. #include "asymmetric_keys.h"
  16. #include "x509_parser.h"
  17. /*
  18. * Set up the signature parameters in an X.509 certificate. This involves
  19. * digesting the signed data and extracting the signature.
  20. */
  21. int x509_get_sig_params(struct x509_certificate *cert)
  22. {
  23. struct public_key_signature *sig = cert->sig;
  24. struct crypto_shash *tfm;
  25. struct shash_desc *desc;
  26. size_t desc_size;
  27. int ret;
  28. pr_devel("==>%s()\n", __func__);
  29. sig->data = cert->tbs;
  30. sig->data_size = cert->tbs_size;
  31. if (!cert->pub->pkey_algo)
  32. cert->unsupported_key = true;
  33. if (!sig->pkey_algo)
  34. cert->unsupported_sig = true;
  35. /* We check the hash if we can - even if we can't then verify it */
  36. if (!sig->hash_algo) {
  37. cert->unsupported_sig = true;
  38. return 0;
  39. }
  40. sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
  41. if (!sig->s)
  42. return -ENOMEM;
  43. sig->s_size = cert->raw_sig_size;
  44. /* Allocate the hashing algorithm we're going to need and find out how
  45. * big the hash operational data will be.
  46. */
  47. tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
  48. if (IS_ERR(tfm)) {
  49. if (PTR_ERR(tfm) == -ENOENT) {
  50. cert->unsupported_sig = true;
  51. return 0;
  52. }
  53. return PTR_ERR(tfm);
  54. }
  55. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  56. sig->digest_size = crypto_shash_digestsize(tfm);
  57. ret = -ENOMEM;
  58. sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
  59. if (!sig->digest)
  60. goto error;
  61. desc = kzalloc(desc_size, GFP_KERNEL);
  62. if (!desc)
  63. goto error;
  64. desc->tfm = tfm;
  65. ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
  66. if (ret < 0)
  67. goto error_2;
  68. ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
  69. if (ret == -EKEYREJECTED) {
  70. pr_err("Cert %*phN is blacklisted\n",
  71. sig->digest_size, sig->digest);
  72. cert->blacklisted = true;
  73. ret = 0;
  74. }
  75. error_2:
  76. kfree(desc);
  77. error:
  78. crypto_free_shash(tfm);
  79. pr_devel("<==%s() = %d\n", __func__, ret);
  80. return ret;
  81. }
  82. /*
  83. * Check for self-signedness in an X.509 cert and if found, check the signature
  84. * immediately if we can.
  85. */
  86. int x509_check_for_self_signed(struct x509_certificate *cert)
  87. {
  88. int ret = 0;
  89. pr_devel("==>%s()\n", __func__);
  90. if (cert->raw_subject_size != cert->raw_issuer_size ||
  91. memcmp(cert->raw_subject, cert->raw_issuer,
  92. cert->raw_issuer_size) != 0)
  93. goto not_self_signed;
  94. if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
  95. /* If the AKID is present it may have one or two parts. If
  96. * both are supplied, both must match.
  97. */
  98. bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
  99. bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
  100. if (!a && !b)
  101. goto not_self_signed;
  102. ret = -EKEYREJECTED;
  103. if (((a && !b) || (b && !a)) &&
  104. cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
  105. goto out;
  106. }
  107. ret = -EKEYREJECTED;
  108. if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0)
  109. goto out;
  110. ret = public_key_verify_signature(cert->pub, cert->sig);
  111. if (ret < 0) {
  112. if (ret == -ENOPKG) {
  113. cert->unsupported_sig = true;
  114. ret = 0;
  115. }
  116. goto out;
  117. }
  118. pr_devel("Cert Self-signature verified");
  119. cert->self_signed = true;
  120. out:
  121. pr_devel("<==%s() = %d\n", __func__, ret);
  122. return ret;
  123. not_self_signed:
  124. pr_devel("<==%s() = 0 [not]\n", __func__);
  125. return 0;
  126. }
  127. /*
  128. * Attempt to parse a data blob for a key as an X509 certificate.
  129. */
  130. static int x509_key_preparse(struct key_preparsed_payload *prep)
  131. {
  132. struct asymmetric_key_ids *kids;
  133. struct x509_certificate *cert;
  134. const char *q;
  135. size_t srlen, sulen;
  136. char *desc = NULL, *p;
  137. int ret;
  138. cert = x509_cert_parse(prep->data, prep->datalen);
  139. if (IS_ERR(cert))
  140. return PTR_ERR(cert);
  141. pr_devel("Cert Issuer: %s\n", cert->issuer);
  142. pr_devel("Cert Subject: %s\n", cert->subject);
  143. if (cert->unsupported_key) {
  144. ret = -ENOPKG;
  145. goto error_free_cert;
  146. }
  147. pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
  148. pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
  149. cert->pub->id_type = "X509";
  150. if (cert->unsupported_sig) {
  151. public_key_signature_free(cert->sig);
  152. cert->sig = NULL;
  153. } else {
  154. pr_devel("Cert Signature: %s + %s\n",
  155. cert->sig->pkey_algo, cert->sig->hash_algo);
  156. }
  157. /* Don't permit addition of blacklisted keys */
  158. ret = -EKEYREJECTED;
  159. if (cert->blacklisted)
  160. goto error_free_cert;
  161. /* Propose a description */
  162. sulen = strlen(cert->subject);
  163. if (cert->raw_skid) {
  164. srlen = cert->raw_skid_size;
  165. q = cert->raw_skid;
  166. } else {
  167. srlen = cert->raw_serial_size;
  168. q = cert->raw_serial;
  169. }
  170. ret = -ENOMEM;
  171. desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
  172. if (!desc)
  173. goto error_free_cert;
  174. p = memcpy(desc, cert->subject, sulen);
  175. p += sulen;
  176. *p++ = ':';
  177. *p++ = ' ';
  178. p = bin2hex(p, q, srlen);
  179. *p = 0;
  180. kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
  181. if (!kids)
  182. goto error_free_desc;
  183. kids->id[0] = cert->id;
  184. kids->id[1] = cert->skid;
  185. /* We're pinning the module by being linked against it */
  186. __module_get(public_key_subtype.owner);
  187. prep->payload.data[asym_subtype] = &public_key_subtype;
  188. prep->payload.data[asym_key_ids] = kids;
  189. prep->payload.data[asym_crypto] = cert->pub;
  190. prep->payload.data[asym_auth] = cert->sig;
  191. prep->description = desc;
  192. prep->quotalen = 100;
  193. /* We've finished with the certificate */
  194. cert->pub = NULL;
  195. cert->id = NULL;
  196. cert->skid = NULL;
  197. cert->sig = NULL;
  198. desc = NULL;
  199. ret = 0;
  200. error_free_desc:
  201. kfree(desc);
  202. error_free_cert:
  203. x509_free_certificate(cert);
  204. return ret;
  205. }
  206. static struct asymmetric_key_parser x509_key_parser = {
  207. .owner = THIS_MODULE,
  208. .name = "x509",
  209. .parse = x509_key_preparse,
  210. };
  211. /*
  212. * Module stuff
  213. */
  214. static int __init x509_key_init(void)
  215. {
  216. return register_asymmetric_key_parser(&x509_key_parser);
  217. }
  218. static void __exit x509_key_exit(void)
  219. {
  220. unregister_asymmetric_key_parser(&x509_key_parser);
  221. }
  222. module_init(x509_key_init);
  223. module_exit(x509_key_exit);
  224. MODULE_DESCRIPTION("X.509 certificate parser");
  225. MODULE_AUTHOR("Red Hat, Inc.");
  226. MODULE_LICENSE("GPL");