1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Asymmetric public-key cryptography key type
4 * See Documentation/crypto/asymmetric-keys.rst
6 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
9 #include <keys/asymmetric-subtype.h>
10 #include <keys/asymmetric-parser.h>
11 #include <crypto/public_key.h>
12 #include <linux/seq_file.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/ctype.h>
16 #include <keys/system_keyring.h>
17 #include <keys/user-type.h>
18 #include "asymmetric_keys.h"
21 const char *const key_being_used_for[NR__KEY_BEING_USED_FOR] = {
22 [VERIFYING_MODULE_SIGNATURE] = "mod sig",
23 [VERIFYING_FIRMWARE_SIGNATURE] = "firmware sig",
24 [VERIFYING_KEXEC_PE_SIGNATURE] = "kexec PE sig",
25 [VERIFYING_KEY_SIGNATURE] = "key sig",
26 [VERIFYING_KEY_SELF_SIGNATURE] = "key self sig",
27 [VERIFYING_UNSPECIFIED_SIGNATURE] = "unspec sig",
29 EXPORT_SYMBOL_GPL(key_being_used_for);
31 static LIST_HEAD(asymmetric_key_parsers);
32 static DECLARE_RWSEM(asymmetric_key_parsers_sem);
35 * find_asymmetric_key - Find a key by ID.
36 * @keyring: The keys to search.
37 * @id_0: The first ID to look for or NULL.
38 * @id_1: The second ID to look for or NULL, matched together with @id_0
39 * against @keyring keys' id[0] and id[1].
40 * @id_2: The fallback ID to match against @keyring keys' id[2] if both of the
42 * @partial: Use partial match for @id_0 and @id_1 if true, exact if false.
44 * Find a key in the given keyring by identifier. The preferred identifier is
45 * the id_0 and the fallback identifier is the id_1. If both are given, the
46 * former is matched (exactly or partially) against either of the sought key's
47 * identifiers and the latter must match the found key's second identifier
48 * exactly. If both are missing, id_2 must match the sought key's third
51 struct key *find_asymmetric_key(struct key *keyring,
52 const struct asymmetric_key_id *id_0,
53 const struct asymmetric_key_id *id_1,
54 const struct asymmetric_key_id *id_2,
63 WARN_ON(!id_0 && !id_1 && !id_2);
76 /* Construct an identifier "id:<keyid>". */
77 p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
79 return ERR_PTR(-ENOMEM);
92 p = bin2hex(p, lookup, len);
95 pr_debug("Look up: \"%s\"\n", req);
97 ref = keyring_search(make_key_ref(keyring, 1),
98 &key_type_asymmetric, req, true);
100 pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
104 switch (PTR_ERR(ref)) {
105 /* Hide some search errors */
109 return ERR_PTR(-ENOKEY);
111 return ERR_CAST(ref);
115 key = key_ref_to_ptr(ref);
117 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
120 pr_debug("First ID matches, but second is missing\n");
123 if (!asymmetric_key_id_same(id_1, kids->id[1])) {
124 pr_debug("First ID matches, but second does not\n");
129 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
134 return ERR_PTR(-EKEYREJECTED);
136 EXPORT_SYMBOL_GPL(find_asymmetric_key);
139 * asymmetric_key_generate_id: Construct an asymmetric key ID
140 * @val_1: First binary blob
141 * @len_1: Length of first binary blob
142 * @val_2: Second binary blob
143 * @len_2: Length of second binary blob
145 * Construct an asymmetric key ID from a pair of binary blobs.
147 struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
152 struct asymmetric_key_id *kid;
154 kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
157 return ERR_PTR(-ENOMEM);
158 kid->len = len_1 + len_2;
159 memcpy(kid->data, val_1, len_1);
160 memcpy(kid->data + len_1, val_2, len_2);
163 EXPORT_SYMBOL_GPL(asymmetric_key_generate_id);
166 * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
167 * @kid1: The key ID to compare
168 * @kid2: The key ID to compare
170 bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
171 const struct asymmetric_key_id *kid2)
175 if (kid1->len != kid2->len)
177 return memcmp(kid1->data, kid2->data, kid1->len) == 0;
179 EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
182 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
184 * @kid1: The key ID to compare
185 * @kid2: The key ID to compare
187 bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
188 const struct asymmetric_key_id *kid2)
192 if (kid1->len < kid2->len)
194 return memcmp(kid1->data + (kid1->len - kid2->len),
195 kid2->data, kid2->len) == 0;
197 EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
200 * asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2
201 * @kids: The pair of key IDs to check
202 * @match_id: The key ID we're looking for
203 * @match: The match function to use
205 static bool asymmetric_match_key_ids(
206 const struct asymmetric_key_ids *kids,
207 const struct asymmetric_key_id *match_id,
208 bool (*match)(const struct asymmetric_key_id *kid1,
209 const struct asymmetric_key_id *kid2))
213 if (!kids || !match_id)
215 for (i = 0; i < 2; i++)
216 if (match(kids->id[i], match_id))
221 /* helper function can be called directly with pre-allocated memory */
222 inline int __asymmetric_key_hex_to_key_id(const char *id,
223 struct asymmetric_key_id *match_id,
226 match_id->len = hexlen;
227 return hex2bin(match_id->data, id, hexlen);
231 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
232 * @id: The ID as a hex string.
234 struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
236 struct asymmetric_key_id *match_id;
241 return ERR_PTR(-EINVAL);
242 asciihexlen = strlen(id);
244 return ERR_PTR(-EINVAL);
246 match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
249 return ERR_PTR(-ENOMEM);
250 ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
253 return ERR_PTR(-EINVAL);
259 * Match asymmetric keys by an exact match on one of the first two IDs.
261 static bool asymmetric_key_cmp(const struct key *key,
262 const struct key_match_data *match_data)
264 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
265 const struct asymmetric_key_id *match_id = match_data->preparsed;
267 return asymmetric_match_key_ids(kids, match_id,
268 asymmetric_key_id_same);
272 * Match asymmetric keys by a partial match on one of the first two IDs.
274 static bool asymmetric_key_cmp_partial(const struct key *key,
275 const struct key_match_data *match_data)
277 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
278 const struct asymmetric_key_id *match_id = match_data->preparsed;
280 return asymmetric_match_key_ids(kids, match_id,
281 asymmetric_key_id_partial);
285 * Match asymmetric keys by an exact match on the third IDs.
287 static bool asymmetric_key_cmp_name(const struct key *key,
288 const struct key_match_data *match_data)
290 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
291 const struct asymmetric_key_id *match_id = match_data->preparsed;
293 return kids && asymmetric_key_id_same(kids->id[2], match_id);
297 * Preparse the match criterion. If we don't set lookup_type and cmp,
298 * the default will be an exact match on the key description.
300 * There are some specifiers for matching key IDs rather than by the key
303 * "id:<id>" - find a key by partial match on one of the first two IDs
304 * "ex:<id>" - find a key by exact match on one of the first two IDs
305 * "dn:<id>" - find a key by exact match on the third ID
307 * These have to be searched by iteration rather than by direct lookup because
308 * the key is hashed according to its description.
310 static int asymmetric_key_match_preparse(struct key_match_data *match_data)
312 struct asymmetric_key_id *match_id;
313 const char *spec = match_data->raw_data;
315 bool (*cmp)(const struct key *, const struct key_match_data *) =
320 if (spec[0] == 'i' &&
324 cmp = asymmetric_key_cmp_partial;
325 } else if (spec[0] == 'e' &&
329 } else if (spec[0] == 'd' &&
333 cmp = asymmetric_key_cmp_name;
338 match_id = asymmetric_key_hex_to_key_id(id);
339 if (IS_ERR(match_id))
340 return PTR_ERR(match_id);
342 match_data->preparsed = match_id;
343 match_data->cmp = cmp;
344 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
352 * Free the preparsed the match criterion.
354 static void asymmetric_key_match_free(struct key_match_data *match_data)
356 kfree(match_data->preparsed);
360 * Describe the asymmetric key
362 static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
364 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
365 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
366 const struct asymmetric_key_id *kid;
367 const unsigned char *p;
370 seq_puts(m, key->description);
374 subtype->describe(key, m);
376 if (kids && kids->id[1]) {
385 seq_printf(m, "%*phN", n, p);
389 /* put something here to indicate the key's capabilities */
395 * Preparse a asymmetric payload to get format the contents appropriately for the
396 * internal payload to cut down on the number of scans of the data performed.
398 * We also generate a proposed description from the contents of the key that
399 * can be used to name the key if the user doesn't want to provide one.
401 static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
403 struct asymmetric_key_parser *parser;
406 pr_devel("==>%s()\n", __func__);
408 if (prep->datalen == 0)
411 down_read(&asymmetric_key_parsers_sem);
414 list_for_each_entry(parser, &asymmetric_key_parsers, link) {
415 pr_debug("Trying parser '%s'\n", parser->name);
417 ret = parser->parse(prep);
418 if (ret != -EBADMSG) {
419 pr_debug("Parser recognised the format (ret %d)\n",
425 up_read(&asymmetric_key_parsers_sem);
426 pr_devel("<==%s() = %d\n", __func__, ret);
431 * Clean up the key ID list
433 static void asymmetric_key_free_kids(struct asymmetric_key_ids *kids)
438 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
445 * Clean up the preparse data
447 static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
449 struct asymmetric_key_subtype *subtype = prep->payload.data[asym_subtype];
450 struct asymmetric_key_ids *kids = prep->payload.data[asym_key_ids];
452 pr_devel("==>%s()\n", __func__);
455 subtype->destroy(prep->payload.data[asym_crypto],
456 prep->payload.data[asym_auth]);
457 module_put(subtype->owner);
459 asymmetric_key_free_kids(kids);
460 kfree(prep->description);
464 * dispose of the data dangling from the corpse of a asymmetric key
466 static void asymmetric_key_destroy(struct key *key)
468 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
469 struct asymmetric_key_ids *kids = key->payload.data[asym_key_ids];
470 void *data = key->payload.data[asym_crypto];
471 void *auth = key->payload.data[asym_auth];
473 key->payload.data[asym_crypto] = NULL;
474 key->payload.data[asym_subtype] = NULL;
475 key->payload.data[asym_key_ids] = NULL;
476 key->payload.data[asym_auth] = NULL;
479 subtype->destroy(data, auth);
480 module_put(subtype->owner);
483 asymmetric_key_free_kids(kids);
486 static struct key_restriction *asymmetric_restriction_alloc(
487 key_restrict_link_func_t check,
490 struct key_restriction *keyres =
491 kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
494 return ERR_PTR(-ENOMEM);
496 keyres->check = check;
498 keyres->keytype = &key_type_asymmetric;
504 * look up keyring restrict functions for asymmetric keys
506 static struct key_restriction *asymmetric_lookup_restriction(
507 const char *restriction)
509 char *restrict_method;
512 struct key_restriction *ret = ERR_PTR(-EINVAL);
514 if (strcmp("builtin_trusted", restriction) == 0)
515 return asymmetric_restriction_alloc(
516 restrict_link_by_builtin_trusted, NULL);
518 if (strcmp("builtin_and_secondary_trusted", restriction) == 0)
519 return asymmetric_restriction_alloc(
520 restrict_link_by_builtin_and_secondary_trusted, NULL);
522 parse_buf = kstrndup(restriction, PAGE_SIZE, GFP_KERNEL);
524 return ERR_PTR(-ENOMEM);
527 restrict_method = strsep(&next, ":");
529 if ((strcmp(restrict_method, "key_or_keyring") == 0) && next) {
533 key_restrict_link_func_t link_fn =
534 restrict_link_by_key_or_keyring;
535 bool allow_null_key = false;
537 key_text = strsep(&next, ":");
540 if (strcmp(next, "chain") != 0)
543 link_fn = restrict_link_by_key_or_keyring_chain;
544 allow_null_key = true;
547 if (kstrtos32(key_text, 0, &serial) < 0)
550 if ((serial == 0) && allow_null_key) {
553 key = key_lookup(serial);
560 ret = asymmetric_restriction_alloc(link_fn, key);
570 int asymmetric_key_eds_op(struct kernel_pkey_params *params,
571 const void *in, void *out)
573 const struct asymmetric_key_subtype *subtype;
574 struct key *key = params->key;
577 pr_devel("==>%s()\n", __func__);
579 if (key->type != &key_type_asymmetric)
581 subtype = asymmetric_key_subtype(key);
583 !key->payload.data[0])
585 if (!subtype->eds_op)
588 ret = subtype->eds_op(params, in, out);
590 pr_devel("<==%s() = %d\n", __func__, ret);
594 static int asymmetric_key_verify_signature(struct kernel_pkey_params *params,
595 const void *in, const void *in2)
597 struct public_key_signature sig = {
598 .s_size = params->in2_len,
599 .digest_size = params->in_len,
600 .encoding = params->encoding,
601 .hash_algo = params->hash_algo,
602 .digest = (void *)in,
606 return verify_signature(params->key, &sig);
609 struct key_type key_type_asymmetric = {
610 .name = "asymmetric",
611 .preparse = asymmetric_key_preparse,
612 .free_preparse = asymmetric_key_free_preparse,
613 .instantiate = generic_key_instantiate,
614 .match_preparse = asymmetric_key_match_preparse,
615 .match_free = asymmetric_key_match_free,
616 .destroy = asymmetric_key_destroy,
617 .describe = asymmetric_key_describe,
618 .lookup_restriction = asymmetric_lookup_restriction,
619 .asym_query = query_asymmetric_key,
620 .asym_eds_op = asymmetric_key_eds_op,
621 .asym_verify_signature = asymmetric_key_verify_signature,
623 EXPORT_SYMBOL_GPL(key_type_asymmetric);
626 * register_asymmetric_key_parser - Register a asymmetric key blob parser
627 * @parser: The parser to register
629 int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
631 struct asymmetric_key_parser *cursor;
634 down_write(&asymmetric_key_parsers_sem);
636 list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
637 if (strcmp(cursor->name, parser->name) == 0) {
638 pr_err("Asymmetric key parser '%s' already registered\n",
645 list_add_tail(&parser->link, &asymmetric_key_parsers);
647 pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
651 up_write(&asymmetric_key_parsers_sem);
654 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
657 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
658 * @parser: The parser to unregister
660 void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
662 down_write(&asymmetric_key_parsers_sem);
663 list_del(&parser->link);
664 up_write(&asymmetric_key_parsers_sem);
666 pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
668 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
673 static int __init asymmetric_key_init(void)
675 return register_key_type(&key_type_asymmetric);
678 static void __exit asymmetric_key_cleanup(void)
680 unregister_key_type(&key_type_asymmetric);
683 module_init(asymmetric_key_init);
684 module_exit(asymmetric_key_cleanup);