system_keyring.c 6.8 KB

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