restrict.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, -ENOPKG if the signature
  62. * uses unsupported crypto, or some other error if there is a matching
  63. * certificate but the signature check cannot be performed.
  64. */
  65. int restrict_link_by_signature(struct key *dest_keyring,
  66. const struct key_type *type,
  67. const union key_payload *payload,
  68. struct key *trust_keyring)
  69. {
  70. const struct public_key_signature *sig;
  71. struct key *key;
  72. int ret;
  73. pr_devel("==>%s()\n", __func__);
  74. if (!trust_keyring)
  75. return -ENOKEY;
  76. if (type != &key_type_asymmetric)
  77. return -EOPNOTSUPP;
  78. sig = payload->data[asym_auth];
  79. if (!sig)
  80. return -ENOPKG;
  81. if (!sig->auth_ids[0] && !sig->auth_ids[1])
  82. return -ENOKEY;
  83. if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
  84. return -EPERM;
  85. /* See if we have a key that signed this one. */
  86. key = find_asymmetric_key(trust_keyring,
  87. sig->auth_ids[0], sig->auth_ids[1],
  88. false);
  89. if (IS_ERR(key))
  90. return -ENOKEY;
  91. if (use_builtin_keys && !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. static bool match_either_id(const struct asymmetric_key_ids *pair,
  99. const struct asymmetric_key_id *single)
  100. {
  101. return (asymmetric_key_id_same(pair->id[0], single) ||
  102. asymmetric_key_id_same(pair->id[1], single));
  103. }
  104. static int key_or_keyring_common(struct key *dest_keyring,
  105. const struct key_type *type,
  106. const union key_payload *payload,
  107. struct key *trusted, bool check_dest)
  108. {
  109. const struct public_key_signature *sig;
  110. struct key *key = NULL;
  111. int ret;
  112. pr_devel("==>%s()\n", __func__);
  113. if (!dest_keyring)
  114. return -ENOKEY;
  115. else if (dest_keyring->type != &key_type_keyring)
  116. return -EOPNOTSUPP;
  117. if (!trusted && !check_dest)
  118. return -ENOKEY;
  119. if (type != &key_type_asymmetric)
  120. return -EOPNOTSUPP;
  121. sig = payload->data[asym_auth];
  122. if (!sig)
  123. return -ENOPKG;
  124. if (!sig->auth_ids[0] && !sig->auth_ids[1])
  125. return -ENOKEY;
  126. if (trusted) {
  127. if (trusted->type == &key_type_keyring) {
  128. /* See if we have a key that signed this one. */
  129. key = find_asymmetric_key(trusted, sig->auth_ids[0],
  130. sig->auth_ids[1], false);
  131. if (IS_ERR(key))
  132. key = NULL;
  133. } else if (trusted->type == &key_type_asymmetric) {
  134. const struct asymmetric_key_ids *signer_ids;
  135. signer_ids = asymmetric_key_ids(trusted);
  136. /*
  137. * The auth_ids come from the candidate key (the
  138. * one that is being considered for addition to
  139. * dest_keyring) and identify the key that was
  140. * used to sign.
  141. *
  142. * The signer_ids are identifiers for the
  143. * signing key specified for dest_keyring.
  144. *
  145. * The first auth_id is the preferred id, and
  146. * the second is the fallback. If only one
  147. * auth_id is present, it may match against
  148. * either signer_id. If two auth_ids are
  149. * present, the first auth_id must match one
  150. * signer_id and the second auth_id must match
  151. * the second signer_id.
  152. */
  153. if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
  154. const struct asymmetric_key_id *auth_id;
  155. auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
  156. if (match_either_id(signer_ids, auth_id))
  157. key = __key_get(trusted);
  158. } else if (asymmetric_key_id_same(signer_ids->id[1],
  159. sig->auth_ids[1]) &&
  160. match_either_id(signer_ids,
  161. sig->auth_ids[0])) {
  162. key = __key_get(trusted);
  163. }
  164. } else {
  165. return -EOPNOTSUPP;
  166. }
  167. }
  168. if (check_dest && !key) {
  169. /* See if the destination has a key that signed this one. */
  170. key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
  171. sig->auth_ids[1], false);
  172. if (IS_ERR(key))
  173. key = NULL;
  174. }
  175. if (!key)
  176. return -ENOKEY;
  177. ret = key_validate(key);
  178. if (ret == 0)
  179. ret = verify_signature(key, sig);
  180. key_put(key);
  181. return ret;
  182. }
  183. /**
  184. * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
  185. * keys using the restrict_key information stored in the ring.
  186. * @dest_keyring: Keyring being linked to.
  187. * @type: The type of key being added.
  188. * @payload: The payload of the new key.
  189. * @trusted: A key or ring of keys that can be used to vouch for the new cert.
  190. *
  191. * Check the new certificate only against the key or keys passed in the data
  192. * parameter. If one of those is the signing key and validates the new
  193. * certificate, then mark the new certificate as being ok to link.
  194. *
  195. * Returns 0 if the new certificate was accepted, -ENOKEY if we
  196. * couldn't find a matching parent certificate in the trusted list,
  197. * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
  198. * unsupported crypto, or some other error if there is a matching certificate
  199. * but the signature check cannot be performed.
  200. */
  201. int restrict_link_by_key_or_keyring(struct key *dest_keyring,
  202. const struct key_type *type,
  203. const union key_payload *payload,
  204. struct key *trusted)
  205. {
  206. return key_or_keyring_common(dest_keyring, type, payload, trusted,
  207. false);
  208. }
  209. /**
  210. * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
  211. * public keys using the restrict_key information stored in the ring.
  212. * @dest_keyring: Keyring being linked to.
  213. * @type: The type of key being added.
  214. * @payload: The payload of the new key.
  215. * @trusted: A key or ring of keys that can be used to vouch for the new cert.
  216. *
  217. * Check the new certificate only against the key or keys passed in the data
  218. * parameter. If one of those is the signing key and validates the new
  219. * certificate, then mark the new certificate as being ok to link.
  220. *
  221. * Returns 0 if the new certificate was accepted, -ENOKEY if we
  222. * couldn't find a matching parent certificate in the trusted list,
  223. * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
  224. * unsupported crypto, or some other error if there is a matching certificate
  225. * but the signature check cannot be performed.
  226. */
  227. int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
  228. const struct key_type *type,
  229. const union key_payload *payload,
  230. struct key *trusted)
  231. {
  232. return key_or_keyring_common(dest_keyring, type, payload, trusted,
  233. true);
  234. }