x509_public_key.c 8.4 KB

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