x509_public_key.c 6.4 KB

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