system_keyring.c 7.5 KB

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