x509_public_key.c 9.9 KB

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