system_keyring.c 8.5 KB

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