restrict.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 (dhowells@redhat.com)
  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 if (IS_BUILTIN(CONFIG_SECONDARY_TRUSTED_KEYRING_SIGNED_BY_BUILTIN) &&
  90. !strcmp(dest_keyring->description, ".secondary_trusted_keys") &&
  91. !test_bit(KEY_FLAG_BUILTIN, &key->flags))
  92. ret = -ENOKEY;
  93. else
  94. ret = verify_signature(key, sig);
  95. key_put(key);
  96. return ret;
  97. }
  98. /**
  99. * restrict_link_by_ca - Restrict additions to a ring of CA keys
  100. * @dest_keyring: Keyring being linked to.
  101. * @type: The type of key being added.
  102. * @payload: The payload of the new key.
  103. * @trust_keyring: Unused.
  104. *
  105. * Check if the new certificate is a CA. If it is a CA, then mark the new
  106. * certificate as being ok to link.
  107. *
  108. * Returns 0 if the new certificate was accepted, -ENOKEY if the
  109. * certificate is not a CA. -ENOPKG if the signature uses unsupported
  110. * crypto, or some other error if there is a matching certificate but
  111. * the signature check cannot be performed.
  112. */
  113. int restrict_link_by_ca(struct key *dest_keyring,
  114. const struct key_type *type,
  115. const union key_payload *payload,
  116. struct key *trust_keyring)
  117. {
  118. const struct public_key *pkey;
  119. if (type != &key_type_asymmetric)
  120. return -EOPNOTSUPP;
  121. pkey = payload->data[asym_crypto];
  122. if (!pkey)
  123. return -ENOPKG;
  124. if (!test_bit(KEY_EFLAG_CA, &pkey->key_eflags))
  125. return -ENOKEY;
  126. if (!test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags))
  127. return -ENOKEY;
  128. if (!IS_ENABLED(CONFIG_INTEGRITY_CA_MACHINE_KEYRING_MAX))
  129. return 0;
  130. if (test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags))
  131. return -ENOKEY;
  132. return 0;
  133. }
  134. /**
  135. * restrict_link_by_digsig - Restrict additions to a ring of digsig keys
  136. * @dest_keyring: Keyring being linked to.
  137. * @type: The type of key being added.
  138. * @payload: The payload of the new key.
  139. * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
  140. *
  141. * Check if the new certificate has digitalSignature usage set. If it is,
  142. * then mark the new certificate as being ok to link. Afterwards verify
  143. * the new certificate against the ones in the trust_keyring.
  144. *
  145. * Returns 0 if the new certificate was accepted, -ENOKEY if the
  146. * certificate is not a digsig. -ENOPKG if the signature uses unsupported
  147. * crypto, or some other error if there is a matching certificate but
  148. * the signature check cannot be performed.
  149. */
  150. int restrict_link_by_digsig(struct key *dest_keyring,
  151. const struct key_type *type,
  152. const union key_payload *payload,
  153. struct key *trust_keyring)
  154. {
  155. const struct public_key *pkey;
  156. if (type != &key_type_asymmetric)
  157. return -EOPNOTSUPP;
  158. pkey = payload->data[asym_crypto];
  159. if (!pkey)
  160. return -ENOPKG;
  161. if (!test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags))
  162. return -ENOKEY;
  163. if (test_bit(KEY_EFLAG_CA, &pkey->key_eflags))
  164. return -ENOKEY;
  165. if (test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags))
  166. return -ENOKEY;
  167. return restrict_link_by_signature(dest_keyring, type, payload,
  168. trust_keyring);
  169. }
  170. static bool match_either_id(const struct asymmetric_key_id **pair,
  171. const struct asymmetric_key_id *single)
  172. {
  173. return (asymmetric_key_id_same(pair[0], single) ||
  174. asymmetric_key_id_same(pair[1], single));
  175. }
  176. static int key_or_keyring_common(struct key *dest_keyring,
  177. const struct key_type *type,
  178. const union key_payload *payload,
  179. struct key *trusted, bool check_dest)
  180. {
  181. const struct public_key_signature *sig;
  182. struct key *key = NULL;
  183. int ret;
  184. pr_devel("==>%s()\n", __func__);
  185. if (!dest_keyring)
  186. return -ENOKEY;
  187. else if (dest_keyring->type != &key_type_keyring)
  188. return -EOPNOTSUPP;
  189. if (!trusted && !check_dest)
  190. return -ENOKEY;
  191. if (type != &key_type_asymmetric)
  192. return -EOPNOTSUPP;
  193. sig = payload->data[asym_auth];
  194. if (!sig)
  195. return -ENOPKG;
  196. if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
  197. return -ENOKEY;
  198. if (trusted) {
  199. if (trusted->type == &key_type_keyring) {
  200. /* See if we have a key that signed this one. */
  201. key = find_asymmetric_key(trusted, sig->auth_ids[0],
  202. sig->auth_ids[1],
  203. sig->auth_ids[2], false);
  204. if (IS_ERR(key))
  205. key = NULL;
  206. } else if (trusted->type == &key_type_asymmetric) {
  207. const struct asymmetric_key_id **signer_ids;
  208. signer_ids = (const struct asymmetric_key_id **)
  209. asymmetric_key_ids(trusted)->id;
  210. /*
  211. * The auth_ids come from the candidate key (the
  212. * one that is being considered for addition to
  213. * dest_keyring) and identify the key that was
  214. * used to sign.
  215. *
  216. * The signer_ids are identifiers for the
  217. * signing key specified for dest_keyring.
  218. *
  219. * The first auth_id is the preferred id, 2nd and
  220. * 3rd are the fallbacks. If exactly one of
  221. * auth_ids[0] and auth_ids[1] is present, it may
  222. * match either signer_ids[0] or signed_ids[1].
  223. * If both are present the first one may match
  224. * either signed_id but the second one must match
  225. * the second signer_id. If neither of them is
  226. * available, auth_ids[2] is matched against
  227. * signer_ids[2] as a fallback.
  228. */
  229. if (!sig->auth_ids[0] && !sig->auth_ids[1]) {
  230. if (asymmetric_key_id_same(signer_ids[2],
  231. sig->auth_ids[2]))
  232. key = __key_get(trusted);
  233. } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
  234. const struct asymmetric_key_id *auth_id;
  235. auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
  236. if (match_either_id(signer_ids, auth_id))
  237. key = __key_get(trusted);
  238. } else if (asymmetric_key_id_same(signer_ids[1],
  239. sig->auth_ids[1]) &&
  240. match_either_id(signer_ids,
  241. sig->auth_ids[0])) {
  242. key = __key_get(trusted);
  243. }
  244. } else {
  245. return -EOPNOTSUPP;
  246. }
  247. }
  248. if (check_dest && !key) {
  249. /* See if the destination has a key that signed this one. */
  250. key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
  251. sig->auth_ids[1], sig->auth_ids[2],
  252. false);
  253. if (IS_ERR(key))
  254. key = NULL;
  255. }
  256. if (!key)
  257. return -ENOKEY;
  258. ret = key_validate(key);
  259. if (ret == 0)
  260. ret = verify_signature(key, sig);
  261. key_put(key);
  262. return ret;
  263. }
  264. /**
  265. * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
  266. * keys using the restrict_key information stored in the ring.
  267. * @dest_keyring: Keyring being linked to.
  268. * @type: The type of key being added.
  269. * @payload: The payload of the new key.
  270. * @trusted: A key or ring of keys that can be used to vouch for the new cert.
  271. *
  272. * Check the new certificate only against the key or keys passed in the data
  273. * parameter. If one of those is the signing key and validates the new
  274. * certificate, then mark the new certificate as being ok to link.
  275. *
  276. * Returns 0 if the new certificate was accepted, -ENOKEY if we
  277. * couldn't find a matching parent certificate in the trusted list,
  278. * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
  279. * unsupported crypto, or some other error if there is a matching certificate
  280. * but the signature check cannot be performed.
  281. */
  282. int restrict_link_by_key_or_keyring(struct key *dest_keyring,
  283. const struct key_type *type,
  284. const union key_payload *payload,
  285. struct key *trusted)
  286. {
  287. return key_or_keyring_common(dest_keyring, type, payload, trusted,
  288. false);
  289. }
  290. /**
  291. * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
  292. * public keys using the restrict_key information stored in the ring.
  293. * @dest_keyring: Keyring being linked to.
  294. * @type: The type of key being added.
  295. * @payload: The payload of the new key.
  296. * @trusted: A key or ring of keys that can be used to vouch for the new cert.
  297. *
  298. * Check the new certificate against the key or keys passed in the data
  299. * parameter and against the keys already linked to the destination keyring. If
  300. * one of those is the signing key and validates the new certificate, then mark
  301. * the new certificate as being ok to link.
  302. *
  303. * Returns 0 if the new certificate was accepted, -ENOKEY if we
  304. * couldn't find a matching parent certificate in the trusted list,
  305. * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
  306. * unsupported crypto, or some other error if there is a matching certificate
  307. * but the signature check cannot be performed.
  308. */
  309. int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
  310. const struct key_type *type,
  311. const union key_payload *payload,
  312. struct key *trusted)
  313. {
  314. return key_or_keyring_common(dest_keyring, type, payload, trusted,
  315. true);
  316. }