restrict.c 7.8 KB

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