x509_public_key.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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(digest_size + desc_size, GFP_KERNEL);
  173. if (!digest)
  174. goto error;
  175. cert->sig.digest = digest;
  176. cert->sig.digest_size = digest_size;
  177. desc = digest + digest_size;
  178. desc->tfm = tfm;
  179. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  180. ret = crypto_shash_init(desc);
  181. if (ret < 0)
  182. goto error;
  183. might_sleep();
  184. ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
  185. error:
  186. crypto_free_shash(tfm);
  187. pr_devel("<==%s() = %d\n", __func__, ret);
  188. return ret;
  189. }
  190. EXPORT_SYMBOL_GPL(x509_get_sig_params);
  191. /*
  192. * Check the signature on a certificate using the provided public key
  193. */
  194. int x509_check_signature(const struct public_key *pub,
  195. struct x509_certificate *cert)
  196. {
  197. int ret;
  198. pr_devel("==>%s()\n", __func__);
  199. ret = x509_get_sig_params(cert);
  200. if (ret < 0)
  201. return ret;
  202. ret = public_key_verify_signature(pub, &cert->sig);
  203. if (ret == -ENOPKG)
  204. cert->unsupported_crypto = true;
  205. pr_debug("Cert Verification: %d\n", ret);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(x509_check_signature);
  209. /*
  210. * Check the new certificate against the ones in the trust keyring. If one of
  211. * those is the signing key and validates the new certificate, then mark the
  212. * new certificate as being trusted.
  213. *
  214. * Return 0 if the new certificate was successfully validated, 1 if we couldn't
  215. * find a matching parent certificate in the trusted list and an error if there
  216. * is a matching certificate but the signature check fails.
  217. */
  218. static int x509_validate_trust(struct x509_certificate *cert,
  219. struct key *trust_keyring)
  220. {
  221. struct key *key;
  222. int ret = 1;
  223. if (!trust_keyring)
  224. return -EOPNOTSUPP;
  225. if (ca_keyid && !asymmetric_key_id_partial(cert->akid_skid, ca_keyid))
  226. return -EPERM;
  227. key = x509_request_asymmetric_key(trust_keyring,
  228. cert->akid_id, cert->akid_skid,
  229. false);
  230. if (!IS_ERR(key)) {
  231. if (!use_builtin_keys
  232. || test_bit(KEY_FLAG_BUILTIN, &key->flags))
  233. ret = x509_check_signature(key->payload.data, cert);
  234. key_put(key);
  235. }
  236. return ret;
  237. }
  238. /*
  239. * Attempt to parse a data blob for a key as an X509 certificate.
  240. */
  241. static int x509_key_preparse(struct key_preparsed_payload *prep)
  242. {
  243. struct asymmetric_key_ids *kids;
  244. struct x509_certificate *cert;
  245. const char *q;
  246. size_t srlen, sulen;
  247. char *desc = NULL, *p;
  248. int ret;
  249. cert = x509_cert_parse(prep->data, prep->datalen);
  250. if (IS_ERR(cert))
  251. return PTR_ERR(cert);
  252. pr_devel("Cert Issuer: %s\n", cert->issuer);
  253. pr_devel("Cert Subject: %s\n", cert->subject);
  254. if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
  255. cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
  256. cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
  257. !pkey_algo[cert->pub->pkey_algo] ||
  258. !pkey_algo[cert->sig.pkey_algo] ||
  259. !hash_algo_name[cert->sig.pkey_hash_algo]) {
  260. ret = -ENOPKG;
  261. goto error_free_cert;
  262. }
  263. pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
  264. pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
  265. pr_devel("Cert Signature: %s + %s\n",
  266. pkey_algo_name[cert->sig.pkey_algo],
  267. hash_algo_name[cert->sig.pkey_hash_algo]);
  268. cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
  269. cert->pub->id_type = PKEY_ID_X509;
  270. /* Check the signature on the key if it appears to be self-signed */
  271. if ((!cert->akid_skid && !cert->akid_id) ||
  272. asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
  273. asymmetric_key_id_same(cert->id, cert->akid_id)) {
  274. ret = x509_check_signature(cert->pub, cert); /* self-signed */
  275. if (ret < 0)
  276. goto error_free_cert;
  277. } else if (!prep->trusted) {
  278. ret = x509_validate_trust(cert, get_system_trusted_keyring());
  279. if (!ret)
  280. prep->trusted = 1;
  281. }
  282. /* Propose a description */
  283. sulen = strlen(cert->subject);
  284. if (cert->raw_skid) {
  285. srlen = cert->raw_skid_size;
  286. q = cert->raw_skid;
  287. } else {
  288. srlen = cert->raw_serial_size;
  289. q = cert->raw_serial;
  290. }
  291. if (srlen > 1 && *q == 0) {
  292. srlen--;
  293. q++;
  294. }
  295. ret = -ENOMEM;
  296. desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
  297. if (!desc)
  298. goto error_free_cert;
  299. p = memcpy(desc, cert->subject, sulen);
  300. p += sulen;
  301. *p++ = ':';
  302. *p++ = ' ';
  303. p = bin2hex(p, q, srlen);
  304. *p = 0;
  305. kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
  306. if (!kids)
  307. goto error_free_desc;
  308. kids->id[0] = cert->id;
  309. kids->id[1] = cert->skid;
  310. /* We're pinning the module by being linked against it */
  311. __module_get(public_key_subtype.owner);
  312. prep->type_data[0] = &public_key_subtype;
  313. prep->type_data[1] = kids;
  314. prep->payload[0] = cert->pub;
  315. prep->description = desc;
  316. prep->quotalen = 100;
  317. /* We've finished with the certificate */
  318. cert->pub = NULL;
  319. cert->id = NULL;
  320. cert->skid = NULL;
  321. desc = NULL;
  322. ret = 0;
  323. error_free_desc:
  324. kfree(desc);
  325. error_free_cert:
  326. x509_free_certificate(cert);
  327. return ret;
  328. }
  329. static struct asymmetric_key_parser x509_key_parser = {
  330. .owner = THIS_MODULE,
  331. .name = "x509",
  332. .parse = x509_key_preparse,
  333. };
  334. /*
  335. * Module stuff
  336. */
  337. static int __init x509_key_init(void)
  338. {
  339. return register_asymmetric_key_parser(&x509_key_parser);
  340. }
  341. static void __exit x509_key_exit(void)
  342. {
  343. unregister_asymmetric_key_parser(&x509_key_parser);
  344. }
  345. module_init(x509_key_init);
  346. module_exit(x509_key_exit);
  347. MODULE_DESCRIPTION("X.509 certificate parser");
  348. MODULE_LICENSE("GPL");