x509_public_key.c 9.4 KB

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