xref: /openbmc/linux/crypto/asymmetric_keys/restrict.c (revision 76adb2fbc69a13c80b39042aab4d34e99309c8d4)
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 
8 #define pr_fmt(fmt) "ASYM: "fmt
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/err.h>
12 #include <crypto/public_key.h>
13 #include "asymmetric_keys.h"
14 
15 static bool use_builtin_keys;
16 static struct asymmetric_key_id *ca_keyid;
17 
18 #ifndef MODULE
19 static struct {
20 	struct asymmetric_key_id id;
21 	unsigned char data[10];
22 } cakey;
23 
24 static int __init ca_keys_setup(char *str)
25 {
26 	if (!str)		/* default system keyring */
27 		return 1;
28 
29 	if (strncmp(str, "id:", 3) == 0) {
30 		struct asymmetric_key_id *p = &cakey.id;
31 		size_t hexlen = (strlen(str) - 3) / 2;
32 		int ret;
33 
34 		if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
35 			pr_err("Missing or invalid ca_keys id\n");
36 			return 1;
37 		}
38 
39 		ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
40 		if (ret < 0)
41 			pr_err("Unparsable ca_keys id hex string\n");
42 		else
43 			ca_keyid = p;	/* owner key 'id:xxxxxx' */
44 	} else if (strcmp(str, "builtin") == 0) {
45 		use_builtin_keys = true;
46 	}
47 
48 	return 1;
49 }
50 __setup("ca_keys=", ca_keys_setup);
51 #endif
52 
53 /**
54  * restrict_link_by_signature - Restrict additions to a ring of public keys
55  * @dest_keyring: Keyring being linked to.
56  * @type: The type of key being added.
57  * @payload: The payload of the new key.
58  * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
59  *
60  * Check the new certificate against the ones in the trust keyring.  If one of
61  * those is the signing key and validates the new certificate, then mark the
62  * new certificate as being trusted.
63  *
64  * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
65  * matching parent certificate in the trusted list, -EKEYREJECTED if the
66  * signature check fails or the key is blacklisted, -ENOPKG if the signature
67  * uses unsupported crypto, or some other error if there is a matching
68  * certificate but the signature check cannot be performed.
69  */
70 int restrict_link_by_signature(struct key *dest_keyring,
71 			       const struct key_type *type,
72 			       const union key_payload *payload,
73 			       struct key *trust_keyring)
74 {
75 	const struct public_key_signature *sig;
76 	struct key *key;
77 	int ret;
78 
79 	pr_devel("==>%s()\n", __func__);
80 
81 	if (!trust_keyring)
82 		return -ENOKEY;
83 
84 	if (type != &key_type_asymmetric)
85 		return -EOPNOTSUPP;
86 
87 	sig = payload->data[asym_auth];
88 	if (!sig)
89 		return -ENOPKG;
90 	if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
91 		return -ENOKEY;
92 
93 	if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
94 		return -EPERM;
95 
96 	/* See if we have a key that signed this one. */
97 	key = find_asymmetric_key(trust_keyring,
98 				  sig->auth_ids[0], sig->auth_ids[1],
99 				  sig->auth_ids[2], false);
100 	if (IS_ERR(key))
101 		return -ENOKEY;
102 
103 	if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
104 		ret = -ENOKEY;
105 	else
106 		ret = verify_signature(key, sig);
107 	key_put(key);
108 	return ret;
109 }
110 
111 /**
112  * restrict_link_by_ca - Restrict additions to a ring of CA keys
113  * @dest_keyring: Keyring being linked to.
114  * @type: The type of key being added.
115  * @payload: The payload of the new key.
116  * @trust_keyring: Unused.
117  *
118  * Check if the new certificate is a CA. If it is a CA, then mark the new
119  * certificate as being ok to link.
120  *
121  * Returns 0 if the new certificate was accepted, -ENOKEY if the
122  * certificate is not a CA. -ENOPKG if the signature uses unsupported
123  * crypto, or some other error if there is a matching certificate but
124  * the signature check cannot be performed.
125  */
126 int restrict_link_by_ca(struct key *dest_keyring,
127 			const struct key_type *type,
128 			const union key_payload *payload,
129 			struct key *trust_keyring)
130 {
131 	const struct public_key *pkey;
132 
133 	if (type != &key_type_asymmetric)
134 		return -EOPNOTSUPP;
135 
136 	pkey = payload->data[asym_crypto];
137 	if (!pkey)
138 		return -ENOPKG;
139 	if (!test_bit(KEY_EFLAG_CA, &pkey->key_eflags))
140 		return -ENOKEY;
141 	if (!test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags))
142 		return -ENOKEY;
143 	if (test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags))
144 		return -ENOKEY;
145 
146 	return 0;
147 }
148 
149 static bool match_either_id(const struct asymmetric_key_id **pair,
150 			    const struct asymmetric_key_id *single)
151 {
152 	return (asymmetric_key_id_same(pair[0], single) ||
153 		asymmetric_key_id_same(pair[1], single));
154 }
155 
156 static int key_or_keyring_common(struct key *dest_keyring,
157 				 const struct key_type *type,
158 				 const union key_payload *payload,
159 				 struct key *trusted, bool check_dest)
160 {
161 	const struct public_key_signature *sig;
162 	struct key *key = NULL;
163 	int ret;
164 
165 	pr_devel("==>%s()\n", __func__);
166 
167 	if (!dest_keyring)
168 		return -ENOKEY;
169 	else if (dest_keyring->type != &key_type_keyring)
170 		return -EOPNOTSUPP;
171 
172 	if (!trusted && !check_dest)
173 		return -ENOKEY;
174 
175 	if (type != &key_type_asymmetric)
176 		return -EOPNOTSUPP;
177 
178 	sig = payload->data[asym_auth];
179 	if (!sig)
180 		return -ENOPKG;
181 	if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
182 		return -ENOKEY;
183 
184 	if (trusted) {
185 		if (trusted->type == &key_type_keyring) {
186 			/* See if we have a key that signed this one. */
187 			key = find_asymmetric_key(trusted, sig->auth_ids[0],
188 						  sig->auth_ids[1],
189 						  sig->auth_ids[2], false);
190 			if (IS_ERR(key))
191 				key = NULL;
192 		} else if (trusted->type == &key_type_asymmetric) {
193 			const struct asymmetric_key_id **signer_ids;
194 
195 			signer_ids = (const struct asymmetric_key_id **)
196 				asymmetric_key_ids(trusted)->id;
197 
198 			/*
199 			 * The auth_ids come from the candidate key (the
200 			 * one that is being considered for addition to
201 			 * dest_keyring) and identify the key that was
202 			 * used to sign.
203 			 *
204 			 * The signer_ids are identifiers for the
205 			 * signing key specified for dest_keyring.
206 			 *
207 			 * The first auth_id is the preferred id, 2nd and
208 			 * 3rd are the fallbacks. If exactly one of
209 			 * auth_ids[0] and auth_ids[1] is present, it may
210 			 * match either signer_ids[0] or signed_ids[1].
211 			 * If both are present the first one may match
212 			 * either signed_id but the second one must match
213 			 * the second signer_id. If neither of them is
214 			 * available, auth_ids[2] is matched against
215 			 * signer_ids[2] as a fallback.
216 			 */
217 			if (!sig->auth_ids[0] && !sig->auth_ids[1]) {
218 				if (asymmetric_key_id_same(signer_ids[2],
219 							   sig->auth_ids[2]))
220 					key = __key_get(trusted);
221 
222 			} else if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
223 				const struct asymmetric_key_id *auth_id;
224 
225 				auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
226 				if (match_either_id(signer_ids, auth_id))
227 					key = __key_get(trusted);
228 
229 			} else if (asymmetric_key_id_same(signer_ids[1],
230 							  sig->auth_ids[1]) &&
231 				   match_either_id(signer_ids,
232 						   sig->auth_ids[0])) {
233 				key = __key_get(trusted);
234 			}
235 		} else {
236 			return -EOPNOTSUPP;
237 		}
238 	}
239 
240 	if (check_dest && !key) {
241 		/* See if the destination has a key that signed this one. */
242 		key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
243 					  sig->auth_ids[1], sig->auth_ids[2],
244 					  false);
245 		if (IS_ERR(key))
246 			key = NULL;
247 	}
248 
249 	if (!key)
250 		return -ENOKEY;
251 
252 	ret = key_validate(key);
253 	if (ret == 0)
254 		ret = verify_signature(key, sig);
255 
256 	key_put(key);
257 	return ret;
258 }
259 
260 /**
261  * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
262  * keys using the restrict_key information stored in the ring.
263  * @dest_keyring: Keyring being linked to.
264  * @type: The type of key being added.
265  * @payload: The payload of the new key.
266  * @trusted: A key or ring of keys that can be used to vouch for the new cert.
267  *
268  * Check the new certificate only against the key or keys passed in the data
269  * parameter. If one of those is the signing key and validates the new
270  * certificate, then mark the new certificate as being ok to link.
271  *
272  * Returns 0 if the new certificate was accepted, -ENOKEY if we
273  * couldn't find a matching parent certificate in the trusted list,
274  * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
275  * unsupported crypto, or some other error if there is a matching certificate
276  * but the signature check cannot be performed.
277  */
278 int restrict_link_by_key_or_keyring(struct key *dest_keyring,
279 				    const struct key_type *type,
280 				    const union key_payload *payload,
281 				    struct key *trusted)
282 {
283 	return key_or_keyring_common(dest_keyring, type, payload, trusted,
284 				     false);
285 }
286 
287 /**
288  * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
289  * public keys using the restrict_key information stored in the ring.
290  * @dest_keyring: Keyring being linked to.
291  * @type: The type of key being added.
292  * @payload: The payload of the new key.
293  * @trusted: A key or ring of keys that can be used to vouch for the new cert.
294  *
295  * Check the new certificate against the key or keys passed in the data
296  * parameter and against the keys already linked to the destination keyring. If
297  * one of those is the signing key and validates the new certificate, then mark
298  * the new certificate as being ok to link.
299  *
300  * Returns 0 if the new certificate was accepted, -ENOKEY if we
301  * couldn't find a matching parent certificate in the trusted list,
302  * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
303  * unsupported crypto, or some other error if there is a matching certificate
304  * but the signature check cannot be performed.
305  */
306 int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
307 					  const struct key_type *type,
308 					  const union key_payload *payload,
309 					  struct key *trusted)
310 {
311 	return key_or_keyring_common(dest_keyring, type, payload, trusted,
312 				     true);
313 }
314