system_keyring.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* System trusted keyring for trusted public keys
  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. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/cred.h>
  15. #include <linux/err.h>
  16. #include <linux/verification.h>
  17. #include <keys/asymmetric-type.h>
  18. #include <keys/system_keyring.h>
  19. #include <crypto/pkcs7.h>
  20. static struct key *builtin_trusted_keys;
  21. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  22. static struct key *secondary_trusted_keys;
  23. #endif
  24. extern __initconst const u8 system_certificate_list[];
  25. extern __initconst const unsigned long system_certificate_list_size;
  26. /**
  27. * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
  28. *
  29. * Restrict the addition of keys into a keyring based on the key-to-be-added
  30. * being vouched for by a key in the built in system keyring.
  31. */
  32. int restrict_link_by_builtin_trusted(struct key *keyring,
  33. const struct key_type *type,
  34. const union key_payload *payload)
  35. {
  36. return restrict_link_by_signature(builtin_trusted_keys, type, payload);
  37. }
  38. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  39. /**
  40. * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
  41. * addition by both builtin and secondary keyrings
  42. *
  43. * Restrict the addition of keys into a keyring based on the key-to-be-added
  44. * being vouched for by a key in either the built-in or the secondary system
  45. * keyrings.
  46. */
  47. int restrict_link_by_builtin_and_secondary_trusted(
  48. struct key *keyring,
  49. const struct key_type *type,
  50. const union key_payload *payload)
  51. {
  52. /* If we have a secondary trusted keyring, then that contains a link
  53. * through to the builtin keyring and the search will follow that link.
  54. */
  55. if (type == &key_type_keyring &&
  56. keyring == secondary_trusted_keys &&
  57. payload == &builtin_trusted_keys->payload)
  58. /* Allow the builtin keyring to be added to the secondary */
  59. return 0;
  60. return restrict_link_by_signature(secondary_trusted_keys, type, payload);
  61. }
  62. #endif
  63. /*
  64. * Create the trusted keyrings
  65. */
  66. static __init int system_trusted_keyring_init(void)
  67. {
  68. pr_notice("Initialise system trusted keyrings\n");
  69. builtin_trusted_keys =
  70. keyring_alloc(".builtin_trusted_keys",
  71. KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
  72. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  73. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
  74. KEY_ALLOC_NOT_IN_QUOTA,
  75. NULL, NULL);
  76. if (IS_ERR(builtin_trusted_keys))
  77. panic("Can't allocate builtin trusted keyring\n");
  78. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  79. secondary_trusted_keys =
  80. keyring_alloc(".secondary_trusted_keys",
  81. KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
  82. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  83. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
  84. KEY_USR_WRITE),
  85. KEY_ALLOC_NOT_IN_QUOTA,
  86. restrict_link_by_builtin_and_secondary_trusted,
  87. NULL);
  88. if (IS_ERR(secondary_trusted_keys))
  89. panic("Can't allocate secondary trusted keyring\n");
  90. if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
  91. panic("Can't link trusted keyrings\n");
  92. #endif
  93. return 0;
  94. }
  95. /*
  96. * Must be initialised before we try and load the keys into the keyring.
  97. */
  98. device_initcall(system_trusted_keyring_init);
  99. /*
  100. * Load the compiled-in list of X.509 certificates.
  101. */
  102. static __init int load_system_certificate_list(void)
  103. {
  104. key_ref_t key;
  105. const u8 *p, *end;
  106. size_t plen;
  107. pr_notice("Loading compiled-in X.509 certificates\n");
  108. p = system_certificate_list;
  109. end = p + system_certificate_list_size;
  110. while (p < end) {
  111. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  112. * than 256 bytes in size.
  113. */
  114. if (end - p < 4)
  115. goto dodgy_cert;
  116. if (p[0] != 0x30 &&
  117. p[1] != 0x82)
  118. goto dodgy_cert;
  119. plen = (p[2] << 8) | p[3];
  120. plen += 4;
  121. if (plen > end - p)
  122. goto dodgy_cert;
  123. key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
  124. "asymmetric",
  125. NULL,
  126. p,
  127. plen,
  128. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  129. KEY_USR_VIEW | KEY_USR_READ),
  130. KEY_ALLOC_NOT_IN_QUOTA |
  131. KEY_ALLOC_BUILT_IN |
  132. KEY_ALLOC_BYPASS_RESTRICTION);
  133. if (IS_ERR(key)) {
  134. pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
  135. PTR_ERR(key));
  136. } else {
  137. pr_notice("Loaded X.509 cert '%s'\n",
  138. key_ref_to_ptr(key)->description);
  139. key_ref_put(key);
  140. }
  141. p += plen;
  142. }
  143. return 0;
  144. dodgy_cert:
  145. pr_err("Problem parsing in-kernel X.509 certificate list\n");
  146. return 0;
  147. }
  148. late_initcall(load_system_certificate_list);
  149. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  150. /**
  151. * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
  152. * @data: The data to be verified (NULL if expecting internal data).
  153. * @len: Size of @data.
  154. * @raw_pkcs7: The PKCS#7 message that is the signature.
  155. * @pkcs7_len: The size of @raw_pkcs7.
  156. * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
  157. * (void *)1UL for all trusted keys).
  158. * @usage: The use to which the key is being put.
  159. * @view_content: Callback to gain access to content.
  160. * @ctx: Context for callback.
  161. */
  162. int verify_pkcs7_signature(const void *data, size_t len,
  163. const void *raw_pkcs7, size_t pkcs7_len,
  164. struct key *trusted_keys,
  165. enum key_being_used_for usage,
  166. int (*view_content)(void *ctx,
  167. const void *data, size_t len,
  168. size_t asn1hdrlen),
  169. void *ctx)
  170. {
  171. struct pkcs7_message *pkcs7;
  172. int ret;
  173. pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
  174. if (IS_ERR(pkcs7))
  175. return PTR_ERR(pkcs7);
  176. /* The data should be detached - so we need to supply it. */
  177. if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
  178. pr_err("PKCS#7 signature with non-detached data\n");
  179. ret = -EBADMSG;
  180. goto error;
  181. }
  182. ret = pkcs7_verify(pkcs7, usage);
  183. if (ret < 0)
  184. goto error;
  185. if (!trusted_keys) {
  186. trusted_keys = builtin_trusted_keys;
  187. } else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
  188. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  189. trusted_keys = secondary_trusted_keys;
  190. #else
  191. trusted_keys = builtin_trusted_keys;
  192. #endif
  193. }
  194. ret = pkcs7_validate_trust(pkcs7, trusted_keys);
  195. if (ret < 0) {
  196. if (ret == -ENOKEY)
  197. pr_err("PKCS#7 signature not signed with a trusted key\n");
  198. goto error;
  199. }
  200. if (view_content) {
  201. size_t asn1hdrlen;
  202. ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
  203. if (ret < 0) {
  204. if (ret == -ENODATA)
  205. pr_devel("PKCS#7 message does not contain data\n");
  206. goto error;
  207. }
  208. ret = view_content(ctx, data, len, asn1hdrlen);
  209. }
  210. error:
  211. pkcs7_free_message(pkcs7);
  212. pr_devel("<==%s() = %d\n", __func__, ret);
  213. return ret;
  214. }
  215. EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
  216. #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
  217. /**
  218. * verify_signature_one - Verify a signature with keys from given keyring
  219. * @sig: The signature to be verified
  220. * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
  221. * (void *)1UL for all trusted keys).
  222. * @keyid: key description (not partial)
  223. */
  224. int verify_signature_one(const struct public_key_signature *sig,
  225. struct key *trusted_keys, const char *keyid)
  226. {
  227. key_ref_t ref;
  228. struct key *key;
  229. int ret;
  230. if (!sig)
  231. return -EBADMSG;
  232. if (!trusted_keys) {
  233. trusted_keys = builtin_trusted_keys;
  234. } else if (trusted_keys == (void *)1UL) {
  235. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  236. trusted_keys = secondary_trusted_keys;
  237. #else
  238. trusted_keys = builtin_trusted_keys;
  239. #endif
  240. }
  241. ref = keyring_search(make_key_ref(trusted_keys, 1),
  242. &key_type_asymmetric, keyid);
  243. if (IS_ERR(ref)) {
  244. pr_err("Asymmetric key (%s) not found in keyring(%s)\n",
  245. keyid, trusted_keys->description);
  246. return -ENOKEY;
  247. }
  248. key = key_ref_to_ptr(ref);
  249. ret = verify_signature(key, sig);
  250. key_put(key);
  251. return ret;
  252. }
  253. EXPORT_SYMBOL_GPL(verify_signature_one);