Commit | Line | Data |
---|---|---|
b4d0d230 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
cfb664ff DH |
2 | /* Instantiate a public key crypto key from an X.509 Certificate |
3 | * | |
a511e1af | 4 | * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved. |
cfb664ff | 5 | * Written by David Howells (dhowells@redhat.com) |
cfb664ff DH |
6 | */ |
7 | ||
a511e1af | 8 | #define pr_fmt(fmt) "ASYM: "fmt |
cfb664ff DH |
9 | #include <linux/module.h> |
10 | #include <linux/kernel.h> | |
cfb664ff | 11 | #include <linux/err.h> |
cfb664ff DH |
12 | #include <crypto/public_key.h> |
13 | #include "asymmetric_keys.h" | |
cfb664ff DH |
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 | ||
a511e1af DH |
53 | /** |
54 | * restrict_link_by_signature - Restrict additions to a ring of public keys | |
aaf66c88 | 55 | * @dest_keyring: Keyring being linked to. |
a511e1af DH |
56 | * @type: The type of key being added. |
57 | * @payload: The payload of the new key. | |
aaf66c88 | 58 | * @trust_keyring: A ring of keys that can be used to vouch for the new cert. |
a511e1af | 59 | * |
cfb664ff DH |
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 | * | |
a511e1af DH |
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 | |
4b34968e EB |
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. | |
cfb664ff | 69 | */ |
aaf66c88 | 70 | int restrict_link_by_signature(struct key *dest_keyring, |
a511e1af | 71 | const struct key_type *type, |
aaf66c88 MM |
72 | const union key_payload *payload, |
73 | struct key *trust_keyring) | |
cfb664ff | 74 | { |
a511e1af | 75 | const struct public_key_signature *sig; |
cfb664ff | 76 | struct key *key; |
a511e1af | 77 | int ret; |
cfb664ff | 78 | |
a511e1af | 79 | pr_devel("==>%s()\n", __func__); |
cfb664ff DH |
80 | |
81 | if (!trust_keyring) | |
a511e1af DH |
82 | return -ENOKEY; |
83 | ||
84 | if (type != &key_type_asymmetric) | |
cfb664ff | 85 | return -EOPNOTSUPP; |
a511e1af DH |
86 | |
87 | sig = payload->data[asym_auth]; | |
4b34968e EB |
88 | if (!sig) |
89 | return -ENOPKG; | |
7d30198e | 90 | if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2]) |
acddc720 | 91 | return -ENOKEY; |
a511e1af | 92 | |
cfb664ff DH |
93 | if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid)) |
94 | return -EPERM; | |
cfb664ff | 95 | |
a511e1af | 96 | /* See if we have a key that signed this one. */ |
cfb664ff DH |
97 | key = find_asymmetric_key(trust_keyring, |
98 | sig->auth_ids[0], sig->auth_ids[1], | |
7d30198e | 99 | sig->auth_ids[2], false); |
cfb664ff | 100 | if (IS_ERR(key)) |
a511e1af | 101 | return -ENOKEY; |
cfb664ff | 102 | |
a511e1af DH |
103 | if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags)) |
104 | ret = -ENOKEY; | |
105 | else | |
106 | ret = verify_signature(key, sig); | |
cfb664ff DH |
107 | key_put(key); |
108 | return ret; | |
109 | } | |
7e3c4d22 | 110 | |
76adb2fb ES |
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; | |
099f26f2 ES |
143 | if (!IS_ENABLED(CONFIG_INTEGRITY_CA_MACHINE_KEYRING_MAX)) |
144 | return 0; | |
76adb2fb ES |
145 | if (test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags)) |
146 | return -ENOKEY; | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
4cfb9080 ES |
151 | /** |
152 | * restrict_link_by_digsig - Restrict additions to a ring of digsig keys | |
153 | * @dest_keyring: Keyring being linked to. | |
154 | * @type: The type of key being added. | |
155 | * @payload: The payload of the new key. | |
156 | * @trust_keyring: A ring of keys that can be used to vouch for the new cert. | |
157 | * | |
158 | * Check if the new certificate has digitalSignature usage set. If it is, | |
159 | * then mark the new certificate as being ok to link. Afterwards verify | |
160 | * the new certificate against the ones in the trust_keyring. | |
161 | * | |
162 | * Returns 0 if the new certificate was accepted, -ENOKEY if the | |
163 | * certificate is not a digsig. -ENOPKG if the signature uses unsupported | |
164 | * crypto, or some other error if there is a matching certificate but | |
165 | * the signature check cannot be performed. | |
166 | */ | |
167 | int restrict_link_by_digsig(struct key *dest_keyring, | |
168 | const struct key_type *type, | |
169 | const union key_payload *payload, | |
170 | struct key *trust_keyring) | |
171 | { | |
172 | const struct public_key *pkey; | |
173 | ||
174 | if (type != &key_type_asymmetric) | |
175 | return -EOPNOTSUPP; | |
176 | ||
177 | pkey = payload->data[asym_crypto]; | |
178 | ||
179 | if (!pkey) | |
180 | return -ENOPKG; | |
181 | ||
182 | if (!test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags)) | |
183 | return -ENOKEY; | |
184 | ||
185 | if (test_bit(KEY_EFLAG_CA, &pkey->key_eflags)) | |
186 | return -ENOKEY; | |
187 | ||
188 | if (test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags)) | |
189 | return -ENOKEY; | |
190 | ||
191 | return restrict_link_by_signature(dest_keyring, type, payload, | |
192 | trust_keyring); | |
193 | } | |
194 | ||
7d30198e | 195 | static bool match_either_id(const struct asymmetric_key_id **pair, |
8e323a02 MM |
196 | const struct asymmetric_key_id *single) |
197 | { | |
7d30198e AZ |
198 | return (asymmetric_key_id_same(pair[0], single) || |
199 | asymmetric_key_id_same(pair[1], single)); | |
8e323a02 MM |
200 | } |
201 | ||
202 | static int key_or_keyring_common(struct key *dest_keyring, | |
203 | const struct key_type *type, | |
204 | const union key_payload *payload, | |
205 | struct key *trusted, bool check_dest) | |
7e3c4d22 MM |
206 | { |
207 | const struct public_key_signature *sig; | |
8e323a02 | 208 | struct key *key = NULL; |
7e3c4d22 MM |
209 | int ret; |
210 | ||
211 | pr_devel("==>%s()\n", __func__); | |
212 | ||
213 | if (!dest_keyring) | |
214 | return -ENOKEY; | |
215 | else if (dest_keyring->type != &key_type_keyring) | |
216 | return -EOPNOTSUPP; | |
217 | ||
8e323a02 | 218 | if (!trusted && !check_dest) |
7e3c4d22 MM |
219 | return -ENOKEY; |
220 | ||
221 | if (type != &key_type_asymmetric) | |
222 | return -EOPNOTSUPP; | |
223 | ||
224 | sig = payload->data[asym_auth]; | |
4b34968e EB |
225 | if (!sig) |
226 | return -ENOPKG; | |
7d30198e | 227 | if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2]) |
7e3c4d22 MM |
228 | return -ENOKEY; |
229 | ||
8e323a02 MM |
230 | if (trusted) { |
231 | if (trusted->type == &key_type_keyring) { | |
232 | /* See if we have a key that signed this one. */ | |
233 | key = find_asymmetric_key(trusted, sig->auth_ids[0], | |
7d30198e AZ |
234 | sig->auth_ids[1], |
235 | sig->auth_ids[2], false); | |
8e323a02 MM |
236 | if (IS_ERR(key)) |
237 | key = NULL; | |
238 | } else if (trusted->type == &key_type_asymmetric) { | |
7d30198e | 239 | const struct asymmetric_key_id **signer_ids; |
7e3c4d22 | 240 | |
7d30198e AZ |
241 | signer_ids = (const struct asymmetric_key_id **) |
242 | asymmetric_key_ids(trusted)->id; | |
7e3c4d22 | 243 | |
8e323a02 MM |
244 | /* |
245 | * The auth_ids come from the candidate key (the | |
246 | * one that is being considered for addition to | |
247 | * dest_keyring) and identify the key that was | |
248 | * used to sign. | |
249 | * | |
250 | * The signer_ids are identifiers for the | |
251 | * signing key specified for dest_keyring. | |
252 | * | |
7d30198e AZ |
253 | * The first auth_id is the preferred id, 2nd and |
254 | * 3rd are the fallbacks. If exactly one of | |
255 | * auth_ids[0] and auth_ids[1] is present, it may | |
256 | * match either signer_ids[0] or signed_ids[1]. | |
257 | * If both are present the first one may match | |
258 | * either signed_id but the second one must match | |
259 | * the second signer_id. If neither of them is | |
260 | * available, auth_ids[2] is matched against | |
261 | * signer_ids[2] as a fallback. | |
8e323a02 | 262 | */ |
7d30198e AZ |
263 | if (!sig->auth_ids[0] && !sig->auth_ids[1]) { |
264 | if (asymmetric_key_id_same(signer_ids[2], | |
265 | sig->auth_ids[2])) | |
266 | key = __key_get(trusted); | |
267 | ||
268 | } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) { | |
8e323a02 | 269 | const struct asymmetric_key_id *auth_id; |
7e3c4d22 | 270 | |
8e323a02 MM |
271 | auth_id = sig->auth_ids[0] ?: sig->auth_ids[1]; |
272 | if (match_either_id(signer_ids, auth_id)) | |
273 | key = __key_get(trusted); | |
274 | ||
7d30198e | 275 | } else if (asymmetric_key_id_same(signer_ids[1], |
8e323a02 MM |
276 | sig->auth_ids[1]) && |
277 | match_either_id(signer_ids, | |
278 | sig->auth_ids[0])) { | |
279 | key = __key_get(trusted); | |
280 | } | |
281 | } else { | |
282 | return -EOPNOTSUPP; | |
283 | } | |
7e3c4d22 MM |
284 | } |
285 | ||
8e323a02 MM |
286 | if (check_dest && !key) { |
287 | /* See if the destination has a key that signed this one. */ | |
288 | key = find_asymmetric_key(dest_keyring, sig->auth_ids[0], | |
7d30198e AZ |
289 | sig->auth_ids[1], sig->auth_ids[2], |
290 | false); | |
8e323a02 MM |
291 | if (IS_ERR(key)) |
292 | key = NULL; | |
293 | } | |
294 | ||
295 | if (!key) | |
296 | return -ENOKEY; | |
297 | ||
7e3c4d22 MM |
298 | ret = key_validate(key); |
299 | if (ret == 0) | |
300 | ret = verify_signature(key, sig); | |
301 | ||
302 | key_put(key); | |
303 | return ret; | |
304 | } | |
8e323a02 MM |
305 | |
306 | /** | |
307 | * restrict_link_by_key_or_keyring - Restrict additions to a ring of public | |
308 | * keys using the restrict_key information stored in the ring. | |
309 | * @dest_keyring: Keyring being linked to. | |
310 | * @type: The type of key being added. | |
311 | * @payload: The payload of the new key. | |
312 | * @trusted: A key or ring of keys that can be used to vouch for the new cert. | |
313 | * | |
314 | * Check the new certificate only against the key or keys passed in the data | |
315 | * parameter. If one of those is the signing key and validates the new | |
316 | * certificate, then mark the new certificate as being ok to link. | |
317 | * | |
318 | * Returns 0 if the new certificate was accepted, -ENOKEY if we | |
319 | * couldn't find a matching parent certificate in the trusted list, | |
4b34968e EB |
320 | * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses |
321 | * unsupported crypto, or some other error if there is a matching certificate | |
322 | * but the signature check cannot be performed. | |
8e323a02 MM |
323 | */ |
324 | int restrict_link_by_key_or_keyring(struct key *dest_keyring, | |
325 | const struct key_type *type, | |
326 | const union key_payload *payload, | |
327 | struct key *trusted) | |
328 | { | |
329 | return key_or_keyring_common(dest_keyring, type, payload, trusted, | |
330 | false); | |
331 | } | |
332 | ||
333 | /** | |
334 | * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of | |
335 | * public keys using the restrict_key information stored in the ring. | |
336 | * @dest_keyring: Keyring being linked to. | |
337 | * @type: The type of key being added. | |
338 | * @payload: The payload of the new key. | |
339 | * @trusted: A key or ring of keys that can be used to vouch for the new cert. | |
340 | * | |
40d32b59 AZ |
341 | * Check the new certificate against the key or keys passed in the data |
342 | * parameter and against the keys already linked to the destination keyring. If | |
343 | * one of those is the signing key and validates the new certificate, then mark | |
344 | * the new certificate as being ok to link. | |
8e323a02 MM |
345 | * |
346 | * Returns 0 if the new certificate was accepted, -ENOKEY if we | |
347 | * couldn't find a matching parent certificate in the trusted list, | |
4b34968e EB |
348 | * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses |
349 | * unsupported crypto, or some other error if there is a matching certificate | |
350 | * but the signature check cannot be performed. | |
8e323a02 MM |
351 | */ |
352 | int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring, | |
353 | const struct key_type *type, | |
354 | const union key_payload *payload, | |
355 | struct key *trusted) | |
356 | { | |
357 | return key_or_keyring_common(dest_keyring, type, payload, trusted, | |
358 | true); | |
359 | } |