restrict.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Instantiate a public key crypto key from an X.509 Certificate
  3. *
  4. * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #define pr_fmt(fmt) "ASYM: "fmt
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/err.h>
  11. #include <crypto/public_key.h>
  12. #include "asymmetric_keys.h"
  13. static bool use_builtin_keys;
  14. static struct asymmetric_key_id *ca_keyid;
  15. #ifndef MODULE
  16. static struct {
  17. struct asymmetric_key_id id;
  18. unsigned char data[10];
  19. } cakey;
  20. static int __init ca_keys_setup(char *str)
  21. {
  22. if (!str) /* default system keyring */
  23. return 1;
  24. if (strncmp(str, "id:", 3) == 0) {
  25. struct asymmetric_key_id *p = &cakey.id;
  26. size_t hexlen = (strlen(str) - 3) / 2;
  27. int ret;
  28. if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
  29. pr_err("Missing or invalid ca_keys id\n");
  30. return 1;
  31. }
  32. ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
  33. if (ret < 0)
  34. pr_err("Unparsable ca_keys id hex string\n");
  35. else
  36. ca_keyid = p; /* owner key 'id:xxxxxx' */
  37. } else if (strcmp(str, "builtin") == 0) {
  38. use_builtin_keys = true;
  39. }
  40. return 1;
  41. }
  42. __setup("ca_keys=", ca_keys_setup);
  43. #endif
  44. /**
  45. * restrict_link_by_signature - Restrict additions to a ring of public keys
  46. * @dest_keyring: Keyring being linked to.
  47. * @type: The type of key being added.
  48. * @payload: The payload of the new key.
  49. * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
  50. *
  51. * Check the new certificate against the ones in the trust keyring. If one of
  52. * those is the signing key and validates the new certificate, then mark the
  53. * new certificate as being trusted.
  54. *
  55. * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
  56. * matching parent certificate in the trusted list, -EKEYREJECTED if the
  57. * signature check fails or the key is blacklisted, -ENOPKG if the signature
  58. * uses unsupported crypto, or some other error if there is a matching
  59. * certificate but the signature check cannot be performed.
  60. */
  61. int restrict_link_by_signature(struct key *dest_keyring,
  62. const struct key_type *type,
  63. const union key_payload *payload,
  64. struct key *trust_keyring)
  65. {
  66. const struct public_key_signature *sig;
  67. struct key *key;
  68. int ret;
  69. pr_devel("==>%s()\n", __func__);
  70. if (!trust_keyring)
  71. return -ENOKEY;
  72. if (type != &key_type_asymmetric)
  73. return -EOPNOTSUPP;
  74. sig = payload->data[asym_auth];
  75. if (!sig)
  76. return -ENOPKG;
  77. if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
  78. return -ENOKEY;
  79. if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
  80. return -EPERM;
  81. /* See if we have a key that signed this one. */
  82. key = find_asymmetric_key(trust_keyring,
  83. sig->auth_ids[0], sig->auth_ids[1],
  84. sig->auth_ids[2], false);
  85. if (IS_ERR(key))
  86. return -ENOKEY;
  87. if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
  88. ret = -ENOKEY;
  89. else
  90. ret = verify_signature(key, sig);
  91. key_put(key);
  92. return ret;
  93. }
  94. static bool match_either_id(const struct asymmetric_key_id **pair,
  95. const struct asymmetric_key_id *single)
  96. {
  97. return (asymmetric_key_id_same(pair[0], single) ||
  98. asymmetric_key_id_same(pair[1], single));
  99. }
  100. static int key_or_keyring_common(struct key *dest_keyring,
  101. const struct key_type *type,
  102. const union key_payload *payload,
  103. struct key *trusted, bool check_dest)
  104. {
  105. const struct public_key_signature *sig;
  106. struct key *key = NULL;
  107. int ret;
  108. pr_devel("==>%s()\n", __func__);
  109. if (!dest_keyring)
  110. return -ENOKEY;
  111. else if (dest_keyring->type != &key_type_keyring)
  112. return -EOPNOTSUPP;
  113. if (!trusted && !check_dest)
  114. return -ENOKEY;
  115. if (type != &key_type_asymmetric)
  116. return -EOPNOTSUPP;
  117. sig = payload->data[asym_auth];
  118. if (!sig)
  119. return -ENOPKG;
  120. if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
  121. return -ENOKEY;
  122. if (trusted) {
  123. if (trusted->type == &key_type_keyring) {
  124. /* See if we have a key that signed this one. */
  125. key = find_asymmetric_key(trusted, sig->auth_ids[0],
  126. sig->auth_ids[1],
  127. sig->auth_ids[2], false);
  128. if (IS_ERR(key))
  129. key = NULL;
  130. } else if (trusted->type == &key_type_asymmetric) {
  131. const struct asymmetric_key_id **signer_ids;
  132. signer_ids = (const struct asymmetric_key_id **)
  133. asymmetric_key_ids(trusted)->id;
  134. /*
  135. * The auth_ids come from the candidate key (the
  136. * one that is being considered for addition to
  137. * dest_keyring) and identify the key that was
  138. * used to sign.
  139. *
  140. * The signer_ids are identifiers for the
  141. * signing key specified for dest_keyring.
  142. *
  143. * The first auth_id is the preferred id, 2nd and
  144. * 3rd are the fallbacks. If exactly one of
  145. * auth_ids[0] and auth_ids[1] is present, it may
  146. * match either signer_ids[0] or signed_ids[1].
  147. * If both are present the first one may match
  148. * either signed_id but the second one must match
  149. * the second signer_id. If neither of them is
  150. * available, auth_ids[2] is matched against
  151. * signer_ids[2] as a fallback.
  152. */
  153. if (!sig->auth_ids[0] && !sig->auth_ids[1]) {
  154. if (asymmetric_key_id_same(signer_ids[2],
  155. sig->auth_ids[2]))
  156. key = __key_get(trusted);
  157. } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
  158. const struct asymmetric_key_id *auth_id;
  159. auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
  160. if (match_either_id(signer_ids, auth_id))
  161. key = __key_get(trusted);
  162. } else if (asymmetric_key_id_same(signer_ids[1],
  163. sig->auth_ids[1]) &&
  164. match_either_id(signer_ids,
  165. sig->auth_ids[0])) {
  166. key = __key_get(trusted);
  167. }
  168. } else {
  169. return -EOPNOTSUPP;
  170. }
  171. }
  172. if (check_dest && !key) {
  173. /* See if the destination has a key that signed this one. */
  174. key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
  175. sig->auth_ids[1], sig->auth_ids[2],
  176. false);
  177. if (IS_ERR(key))
  178. key = NULL;
  179. }
  180. if (!key)
  181. return -ENOKEY;
  182. ret = key_validate(key);
  183. if (ret == 0)
  184. ret = verify_signature(key, sig);
  185. key_put(key);
  186. return ret;
  187. }
  188. /**
  189. * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
  190. * keys using the restrict_key information stored in the ring.
  191. * @dest_keyring: Keyring being linked to.
  192. * @type: The type of key being added.
  193. * @payload: The payload of the new key.
  194. * @trusted: A key or ring of keys that can be used to vouch for the new cert.
  195. *
  196. * Check the new certificate only against the key or keys passed in the data
  197. * parameter. If one of those is the signing key and validates the new
  198. * certificate, then mark the new certificate as being ok to link.
  199. *
  200. * Returns 0 if the new certificate was accepted, -ENOKEY if we
  201. * couldn't find a matching parent certificate in the trusted list,
  202. * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
  203. * unsupported crypto, or some other error if there is a matching certificate
  204. * but the signature check cannot be performed.
  205. */
  206. int restrict_link_by_key_or_keyring(struct key *dest_keyring,
  207. const struct key_type *type,
  208. const union key_payload *payload,
  209. struct key *trusted)
  210. {
  211. return key_or_keyring_common(dest_keyring, type, payload, trusted,
  212. false);
  213. }
  214. /**
  215. * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
  216. * public keys using the restrict_key information stored in the ring.
  217. * @dest_keyring: Keyring being linked to.
  218. * @type: The type of key being added.
  219. * @payload: The payload of the new key.
  220. * @trusted: A key or ring of keys that can be used to vouch for the new cert.
  221. *
  222. * Check the new certificate against the key or keys passed in the data
  223. * parameter and against the keys already linked to the destination keyring. If
  224. * one of those is the signing key and validates the new certificate, then mark
  225. * the new certificate as being ok to link.
  226. *
  227. * Returns 0 if the new certificate was accepted, -ENOKEY if we
  228. * couldn't find a matching parent certificate in the trusted list,
  229. * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
  230. * unsupported crypto, or some other error if there is a matching certificate
  231. * but the signature check cannot be performed.
  232. */
  233. int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
  234. const struct key_type *type,
  235. const union key_payload *payload,
  236. struct key *trusted)
  237. {
  238. return key_or_keyring_common(dest_keyring, type, payload, trusted,
  239. true);
  240. }