x509_public_key.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_init(desc);
  69. if (ret < 0)
  70. goto error_2;
  71. might_sleep();
  72. ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);
  73. error_2:
  74. kfree(desc);
  75. error:
  76. crypto_free_shash(tfm);
  77. pr_devel("<==%s() = %d\n", __func__, ret);
  78. return ret;
  79. }
  80. /*
  81. * Check for self-signedness in an X.509 cert and if found, check the signature
  82. * immediately if we can.
  83. */
  84. int x509_check_for_self_signed(struct x509_certificate *cert)
  85. {
  86. int ret = 0;
  87. pr_devel("==>%s()\n", __func__);
  88. if (cert->raw_subject_size != cert->raw_issuer_size ||
  89. memcmp(cert->raw_subject, cert->raw_issuer,
  90. cert->raw_issuer_size) != 0)
  91. goto not_self_signed;
  92. if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
  93. /* If the AKID is present it may have one or two parts. If
  94. * both are supplied, both must match.
  95. */
  96. bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
  97. bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
  98. if (!a && !b)
  99. goto not_self_signed;
  100. ret = -EKEYREJECTED;
  101. if (((a && !b) || (b && !a)) &&
  102. cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
  103. goto out;
  104. }
  105. ret = -EKEYREJECTED;
  106. if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0)
  107. goto out;
  108. ret = public_key_verify_signature(cert->pub, cert->sig);
  109. if (ret < 0) {
  110. if (ret == -ENOPKG) {
  111. cert->unsupported_sig = true;
  112. ret = 0;
  113. }
  114. goto out;
  115. }
  116. pr_devel("Cert Self-signature verified");
  117. cert->self_signed = true;
  118. out:
  119. pr_devel("<==%s() = %d\n", __func__, ret);
  120. return ret;
  121. not_self_signed:
  122. pr_devel("<==%s() = 0 [not]\n", __func__);
  123. return 0;
  124. }
  125. /*
  126. * Attempt to parse a data blob for a key as an X509 certificate.
  127. */
  128. static int x509_key_preparse(struct key_preparsed_payload *prep)
  129. {
  130. struct asymmetric_key_ids *kids;
  131. struct x509_certificate *cert;
  132. const char *q;
  133. size_t srlen, sulen;
  134. char *desc = NULL, *p;
  135. int ret;
  136. cert = x509_cert_parse(prep->data, prep->datalen);
  137. if (IS_ERR(cert))
  138. return PTR_ERR(cert);
  139. pr_devel("Cert Issuer: %s\n", cert->issuer);
  140. pr_devel("Cert Subject: %s\n", cert->subject);
  141. if (cert->unsupported_key) {
  142. ret = -ENOPKG;
  143. goto error_free_cert;
  144. }
  145. pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
  146. pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
  147. cert->pub->id_type = "X509";
  148. if (cert->unsupported_sig) {
  149. public_key_signature_free(cert->sig);
  150. cert->sig = NULL;
  151. } else {
  152. pr_devel("Cert Signature: %s + %s\n",
  153. cert->sig->pkey_algo, cert->sig->hash_algo);
  154. }
  155. /* Propose a description */
  156. sulen = strlen(cert->subject);
  157. if (cert->raw_skid) {
  158. srlen = cert->raw_skid_size;
  159. q = cert->raw_skid;
  160. } else {
  161. srlen = cert->raw_serial_size;
  162. q = cert->raw_serial;
  163. }
  164. ret = -ENOMEM;
  165. desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
  166. if (!desc)
  167. goto error_free_cert;
  168. p = memcpy(desc, cert->subject, sulen);
  169. p += sulen;
  170. *p++ = ':';
  171. *p++ = ' ';
  172. p = bin2hex(p, q, srlen);
  173. *p = 0;
  174. kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
  175. if (!kids)
  176. goto error_free_desc;
  177. kids->id[0] = cert->id;
  178. kids->id[1] = cert->skid;
  179. /* We're pinning the module by being linked against it */
  180. __module_get(public_key_subtype.owner);
  181. prep->payload.data[asym_subtype] = &public_key_subtype;
  182. prep->payload.data[asym_key_ids] = kids;
  183. prep->payload.data[asym_crypto] = cert->pub;
  184. prep->payload.data[asym_auth] = cert->sig;
  185. prep->description = desc;
  186. prep->quotalen = 100;
  187. /* We've finished with the certificate */
  188. cert->pub = NULL;
  189. cert->id = NULL;
  190. cert->skid = NULL;
  191. cert->sig = NULL;
  192. desc = NULL;
  193. ret = 0;
  194. error_free_desc:
  195. kfree(desc);
  196. error_free_cert:
  197. x509_free_certificate(cert);
  198. return ret;
  199. }
  200. static struct asymmetric_key_parser x509_key_parser = {
  201. .owner = THIS_MODULE,
  202. .name = "x509",
  203. .parse = x509_key_preparse,
  204. };
  205. /*
  206. * Module stuff
  207. */
  208. static int __init x509_key_init(void)
  209. {
  210. return register_asymmetric_key_parser(&x509_key_parser);
  211. }
  212. static void __exit x509_key_exit(void)
  213. {
  214. unregister_asymmetric_key_parser(&x509_key_parser);
  215. }
  216. module_init(x509_key_init);
  217. module_exit(x509_key_exit);
  218. MODULE_DESCRIPTION("X.509 certificate parser");
  219. MODULE_LICENSE("GPL");