x509_public_key.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <keys/system_keyring.h>
  21. #include <crypto/hash.h>
  22. #include "asymmetric_keys.h"
  23. #include "public_key.h"
  24. #include "x509_parser.h"
  25. static bool use_builtin_keys;
  26. static char *ca_keyid;
  27. #ifndef MODULE
  28. static int __init ca_keys_setup(char *str)
  29. {
  30. if (!str) /* default system keyring */
  31. return 1;
  32. if (strncmp(str, "id:", 3) == 0)
  33. ca_keyid = str; /* owner key 'id:xxxxxx' */
  34. else if (strcmp(str, "builtin") == 0)
  35. use_builtin_keys = true;
  36. return 1;
  37. }
  38. __setup("ca_keys=", ca_keys_setup);
  39. #endif
  40. /**
  41. * x509_request_asymmetric_key - Request a key by X.509 certificate params.
  42. * @keyring: The keys to search.
  43. * @subject: The name of the subject to whom the key belongs.
  44. * @key_id: The subject key ID as a hex string.
  45. *
  46. * Find a key in the given keyring by subject name and key ID. These might,
  47. * for instance, be the issuer name and the authority key ID of an X.509
  48. * certificate that needs to be verified.
  49. */
  50. struct key *x509_request_asymmetric_key(struct key *keyring,
  51. const char *subject,
  52. const char *key_id)
  53. {
  54. key_ref_t key;
  55. size_t subject_len = strlen(subject), key_id_len = strlen(key_id);
  56. char *id;
  57. /* Construct an identifier "<subjname>:<keyid>". */
  58. id = kmalloc(subject_len + 2 + key_id_len + 1, GFP_KERNEL);
  59. if (!id)
  60. return ERR_PTR(-ENOMEM);
  61. memcpy(id, subject, subject_len);
  62. id[subject_len + 0] = ':';
  63. id[subject_len + 1] = ' ';
  64. memcpy(id + subject_len + 2, key_id, key_id_len);
  65. id[subject_len + 2 + key_id_len] = 0;
  66. pr_debug("Look up: \"%s\"\n", id);
  67. key = keyring_search(make_key_ref(keyring, 1),
  68. &key_type_asymmetric, id);
  69. if (IS_ERR(key))
  70. pr_debug("Request for key '%s' err %ld\n", id, PTR_ERR(key));
  71. kfree(id);
  72. if (IS_ERR(key)) {
  73. switch (PTR_ERR(key)) {
  74. /* Hide some search errors */
  75. case -EACCES:
  76. case -ENOTDIR:
  77. case -EAGAIN:
  78. return ERR_PTR(-ENOKEY);
  79. default:
  80. return ERR_CAST(key);
  81. }
  82. }
  83. pr_devel("<==%s() = 0 [%x]\n", __func__,
  84. key_serial(key_ref_to_ptr(key)));
  85. return key_ref_to_ptr(key);
  86. }
  87. EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
  88. /*
  89. * Set up the signature parameters in an X.509 certificate. This involves
  90. * digesting the signed data and extracting the signature.
  91. */
  92. int x509_get_sig_params(struct x509_certificate *cert)
  93. {
  94. struct crypto_shash *tfm;
  95. struct shash_desc *desc;
  96. size_t digest_size, desc_size;
  97. void *digest;
  98. int ret;
  99. pr_devel("==>%s()\n", __func__);
  100. if (cert->sig.rsa.s)
  101. return 0;
  102. cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
  103. if (!cert->sig.rsa.s)
  104. return -ENOMEM;
  105. cert->sig.nr_mpi = 1;
  106. /* Allocate the hashing algorithm we're going to need and find out how
  107. * big the hash operational data will be.
  108. */
  109. tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
  110. if (IS_ERR(tfm))
  111. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  112. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  113. digest_size = crypto_shash_digestsize(tfm);
  114. /* We allocate the hash operational data storage on the end of the
  115. * digest storage space.
  116. */
  117. ret = -ENOMEM;
  118. digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
  119. if (!digest)
  120. goto error;
  121. cert->sig.digest = digest;
  122. cert->sig.digest_size = digest_size;
  123. desc = digest + digest_size;
  124. desc->tfm = tfm;
  125. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  126. ret = crypto_shash_init(desc);
  127. if (ret < 0)
  128. goto error;
  129. might_sleep();
  130. ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
  131. error:
  132. crypto_free_shash(tfm);
  133. pr_devel("<==%s() = %d\n", __func__, ret);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(x509_get_sig_params);
  137. /*
  138. * Check the signature on a certificate using the provided public key
  139. */
  140. int x509_check_signature(const struct public_key *pub,
  141. struct x509_certificate *cert)
  142. {
  143. int ret;
  144. pr_devel("==>%s()\n", __func__);
  145. ret = x509_get_sig_params(cert);
  146. if (ret < 0)
  147. return ret;
  148. ret = public_key_verify_signature(pub, &cert->sig);
  149. pr_debug("Cert Verification: %d\n", ret);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL_GPL(x509_check_signature);
  153. /*
  154. * Check the new certificate against the ones in the trust keyring. If one of
  155. * those is the signing key and validates the new certificate, then mark the
  156. * new certificate as being trusted.
  157. *
  158. * Return 0 if the new certificate was successfully validated, 1 if we couldn't
  159. * find a matching parent certificate in the trusted list and an error if there
  160. * is a matching certificate but the signature check fails.
  161. */
  162. static int x509_validate_trust(struct x509_certificate *cert,
  163. struct key *trust_keyring)
  164. {
  165. struct key *key;
  166. int ret = 1;
  167. if (!trust_keyring)
  168. return -EOPNOTSUPP;
  169. if (ca_keyid && !asymmetric_keyid_match(cert->authority, ca_keyid))
  170. return -EPERM;
  171. key = x509_request_asymmetric_key(trust_keyring,
  172. cert->issuer, cert->authority);
  173. if (!IS_ERR(key)) {
  174. if (!use_builtin_keys
  175. || test_bit(KEY_FLAG_BUILTIN, &key->flags))
  176. ret = x509_check_signature(key->payload.data, cert);
  177. key_put(key);
  178. }
  179. return ret;
  180. }
  181. /*
  182. * Attempt to parse a data blob for a key as an X509 certificate.
  183. */
  184. static int x509_key_preparse(struct key_preparsed_payload *prep)
  185. {
  186. struct x509_certificate *cert;
  187. size_t srlen, sulen;
  188. char *desc = NULL;
  189. int ret;
  190. cert = x509_cert_parse(prep->data, prep->datalen);
  191. if (IS_ERR(cert))
  192. return PTR_ERR(cert);
  193. pr_devel("Cert Issuer: %s\n", cert->issuer);
  194. pr_devel("Cert Subject: %s\n", cert->subject);
  195. if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
  196. cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
  197. cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
  198. !pkey_algo[cert->pub->pkey_algo] ||
  199. !pkey_algo[cert->sig.pkey_algo] ||
  200. !hash_algo_name[cert->sig.pkey_hash_algo]) {
  201. ret = -ENOPKG;
  202. goto error_free_cert;
  203. }
  204. pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
  205. pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
  206. cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
  207. cert->valid_from.tm_mday, cert->valid_from.tm_hour,
  208. cert->valid_from.tm_min, cert->valid_from.tm_sec);
  209. pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
  210. cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
  211. cert->valid_to.tm_mday, cert->valid_to.tm_hour,
  212. cert->valid_to.tm_min, cert->valid_to.tm_sec);
  213. pr_devel("Cert Signature: %s + %s\n",
  214. pkey_algo_name[cert->sig.pkey_algo],
  215. hash_algo_name[cert->sig.pkey_hash_algo]);
  216. if (!cert->fingerprint) {
  217. pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
  218. cert->subject);
  219. ret = -EKEYREJECTED;
  220. goto error_free_cert;
  221. }
  222. cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
  223. cert->pub->id_type = PKEY_ID_X509;
  224. /* Check the signature on the key if it appears to be self-signed */
  225. if (!cert->authority ||
  226. strcmp(cert->fingerprint, cert->authority) == 0) {
  227. ret = x509_check_signature(cert->pub, cert); /* self-signed */
  228. if (ret < 0)
  229. goto error_free_cert;
  230. } else if (!prep->trusted) {
  231. ret = x509_validate_trust(cert, get_system_trusted_keyring());
  232. if (!ret)
  233. prep->trusted = 1;
  234. }
  235. /* Propose a description */
  236. sulen = strlen(cert->subject);
  237. srlen = strlen(cert->fingerprint);
  238. ret = -ENOMEM;
  239. desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
  240. if (!desc)
  241. goto error_free_cert;
  242. memcpy(desc, cert->subject, sulen);
  243. desc[sulen] = ':';
  244. desc[sulen + 1] = ' ';
  245. memcpy(desc + sulen + 2, cert->fingerprint, srlen);
  246. desc[sulen + 2 + srlen] = 0;
  247. /* We're pinning the module by being linked against it */
  248. __module_get(public_key_subtype.owner);
  249. prep->type_data[0] = &public_key_subtype;
  250. prep->type_data[1] = cert->fingerprint;
  251. prep->payload[0] = cert->pub;
  252. prep->description = desc;
  253. prep->quotalen = 100;
  254. /* We've finished with the certificate */
  255. cert->pub = NULL;
  256. cert->fingerprint = NULL;
  257. desc = NULL;
  258. ret = 0;
  259. error_free_cert:
  260. x509_free_certificate(cert);
  261. return ret;
  262. }
  263. static struct asymmetric_key_parser x509_key_parser = {
  264. .owner = THIS_MODULE,
  265. .name = "x509",
  266. .parse = x509_key_preparse,
  267. };
  268. /*
  269. * Module stuff
  270. */
  271. static int __init x509_key_init(void)
  272. {
  273. return register_asymmetric_key_parser(&x509_key_parser);
  274. }
  275. static void __exit x509_key_exit(void)
  276. {
  277. unregister_asymmetric_key_parser(&x509_key_parser);
  278. }
  279. module_init(x509_key_init);
  280. module_exit(x509_key_exit);
  281. MODULE_DESCRIPTION("X.509 certificate parser");
  282. MODULE_LICENSE("GPL");