x509_public_key.c 9.2 KB

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