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"
20 MODULE_LICENSE("GPL");
22 const char *const key_being_used_for[NR__KEY_BEING_USED_FOR] = {
23 [VERIFYING_MODULE_SIGNATURE] = "mod sig",
24 [VERIFYING_FIRMWARE_SIGNATURE] = "firmware sig",
25 [VERIFYING_KEXEC_PE_SIGNATURE] = "kexec PE sig",
26 [VERIFYING_KEY_SIGNATURE] = "key sig",
27 [VERIFYING_KEY_SELF_SIGNATURE] = "key self sig",
28 [VERIFYING_UNSPECIFIED_SIGNATURE] = "unspec sig",
30 EXPORT_SYMBOL_GPL(key_being_used_for);
32 static LIST_HEAD(asymmetric_key_parsers);
33 static DECLARE_RWSEM(asymmetric_key_parsers_sem);
36 * find_asymmetric_key - Find a key by ID.
37 * @keyring: The keys to search.
38 * @id_0: The first ID to look for or NULL.
39 * @id_1: The second ID to look for or NULL, matched together with @id_0
40 * against @keyring keys' id[0] and id[1].
41 * @id_2: The fallback ID to match against @keyring keys' id[2] if both of the
43 * @partial: Use partial match for @id_0 and @id_1 if true, exact if false.
45 * Find a key in the given keyring by identifier. The preferred identifier is
46 * the id_0 and the fallback identifier is the id_1. If both are given, the
47 * former is matched (exactly or partially) against either of the sought key's
48 * identifiers and the latter must match the found key's second identifier
49 * exactly. If both are missing, id_2 must match the sought key's third
52 struct key *find_asymmetric_key(struct key *keyring,
53 const struct asymmetric_key_id *id_0,
54 const struct asymmetric_key_id *id_1,
55 const struct asymmetric_key_id *id_2,
64 WARN_ON(!id_0 && !id_1 && !id_2);
77 /* Construct an identifier "id:<keyid>". */
78 p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
80 return ERR_PTR(-ENOMEM);
93 p = bin2hex(p, lookup, len);
96 pr_debug("Look up: \"%s\"\n", req);
98 ref = keyring_search(make_key_ref(keyring, 1),
99 &key_type_asymmetric, req, true);
101 pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
105 switch (PTR_ERR(ref)) {
106 /* Hide some search errors */
110 return ERR_PTR(-ENOKEY);
112 return ERR_CAST(ref);
116 key = key_ref_to_ptr(ref);
118 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
121 pr_debug("First ID matches, but second is missing\n");
124 if (!asymmetric_key_id_same(id_1, kids->id[1])) {
125 pr_debug("First ID matches, but second does not\n");
130 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
135 return ERR_PTR(-EKEYREJECTED);
137 EXPORT_SYMBOL_GPL(find_asymmetric_key);
140 * asymmetric_key_generate_id: Construct an asymmetric key ID
141 * @val_1: First binary blob
142 * @len_1: Length of first binary blob
143 * @val_2: Second binary blob
144 * @len_2: Length of second binary blob
146 * Construct an asymmetric key ID from a pair of binary blobs.
148 struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
153 struct asymmetric_key_id *kid;
155 kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
158 return ERR_PTR(-ENOMEM);
159 kid->len = len_1 + len_2;
160 memcpy(kid->data, val_1, len_1);
161 memcpy(kid->data + len_1, val_2, len_2);
164 EXPORT_SYMBOL_GPL(asymmetric_key_generate_id);
167 * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
168 * @kid1: The key ID to compare
169 * @kid2: The key ID to compare
171 bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
172 const struct asymmetric_key_id *kid2)
176 if (kid1->len != kid2->len)
178 return memcmp(kid1->data, kid2->data, kid1->len) == 0;
180 EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
183 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
185 * @kid1: The key ID to compare
186 * @kid2: The key ID to compare
188 bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
189 const struct asymmetric_key_id *kid2)
193 if (kid1->len < kid2->len)
195 return memcmp(kid1->data + (kid1->len - kid2->len),
196 kid2->data, kid2->len) == 0;
198 EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
201 * asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2
202 * @kids: The pair of key IDs to check
203 * @match_id: The key ID we're looking for
204 * @match: The match function to use
206 static bool asymmetric_match_key_ids(
207 const struct asymmetric_key_ids *kids,
208 const struct asymmetric_key_id *match_id,
209 bool (*match)(const struct asymmetric_key_id *kid1,
210 const struct asymmetric_key_id *kid2))
214 if (!kids || !match_id)
216 for (i = 0; i < 2; i++)
217 if (match(kids->id[i], match_id))
222 /* helper function can be called directly with pre-allocated memory */
223 inline int __asymmetric_key_hex_to_key_id(const char *id,
224 struct asymmetric_key_id *match_id,
227 match_id->len = hexlen;
228 return hex2bin(match_id->data, id, hexlen);
232 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
233 * @id: The ID as a hex string.
235 struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
237 struct asymmetric_key_id *match_id;
242 return ERR_PTR(-EINVAL);
243 asciihexlen = strlen(id);
245 return ERR_PTR(-EINVAL);
247 match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
250 return ERR_PTR(-ENOMEM);
251 ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
254 return ERR_PTR(-EINVAL);
260 * Match asymmetric keys by an exact match on one of the first two IDs.
262 static bool asymmetric_key_cmp(const struct key *key,
263 const struct key_match_data *match_data)
265 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
266 const struct asymmetric_key_id *match_id = match_data->preparsed;
268 return asymmetric_match_key_ids(kids, match_id,
269 asymmetric_key_id_same);
273 * Match asymmetric keys by a partial match on one of the first two IDs.
275 static bool asymmetric_key_cmp_partial(const struct key *key,
276 const struct key_match_data *match_data)
278 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
279 const struct asymmetric_key_id *match_id = match_data->preparsed;
281 return asymmetric_match_key_ids(kids, match_id,
282 asymmetric_key_id_partial);
286 * Match asymmetric keys by an exact match on the third IDs.
288 static bool asymmetric_key_cmp_name(const struct key *key,
289 const struct key_match_data *match_data)
291 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
292 const struct asymmetric_key_id *match_id = match_data->preparsed;
294 return kids && asymmetric_key_id_same(kids->id[2], match_id);
298 * Preparse the match criterion. If we don't set lookup_type and cmp,
299 * the default will be an exact match on the key description.
301 * There are some specifiers for matching key IDs rather than by the key
304 * "id:<id>" - find a key by partial match on one of the first two IDs
305 * "ex:<id>" - find a key by exact match on one of the first two IDs
306 * "dn:<id>" - find a key by exact match on the third ID
308 * These have to be searched by iteration rather than by direct lookup because
309 * the key is hashed according to its description.
311 static int asymmetric_key_match_preparse(struct key_match_data *match_data)
313 struct asymmetric_key_id *match_id;
314 const char *spec = match_data->raw_data;
316 bool (*cmp)(const struct key *, const struct key_match_data *) =
321 if (spec[0] == 'i' &&
325 cmp = asymmetric_key_cmp_partial;
326 } else if (spec[0] == 'e' &&
330 } else if (spec[0] == 'd' &&
334 cmp = asymmetric_key_cmp_name;
339 match_id = asymmetric_key_hex_to_key_id(id);
340 if (IS_ERR(match_id))
341 return PTR_ERR(match_id);
343 match_data->preparsed = match_id;
344 match_data->cmp = cmp;
345 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
353 * Free the preparsed the match criterion.
355 static void asymmetric_key_match_free(struct key_match_data *match_data)
357 kfree(match_data->preparsed);
361 * Describe the asymmetric key
363 static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
365 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
366 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
367 const struct asymmetric_key_id *kid;
368 const unsigned char *p;
371 seq_puts(m, key->description);
375 subtype->describe(key, m);
377 if (kids && kids->id[1]) {
386 seq_printf(m, "%*phN", n, p);
390 /* put something here to indicate the key's capabilities */
396 * Preparse a asymmetric payload to get format the contents appropriately for the
397 * internal payload to cut down on the number of scans of the data performed.
399 * We also generate a proposed description from the contents of the key that
400 * can be used to name the key if the user doesn't want to provide one.
402 static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
404 struct asymmetric_key_parser *parser;
407 pr_devel("==>%s()\n", __func__);
409 if (prep->datalen == 0)
412 down_read(&asymmetric_key_parsers_sem);
415 list_for_each_entry(parser, &asymmetric_key_parsers, link) {
416 pr_debug("Trying parser '%s'\n", parser->name);
418 ret = parser->parse(prep);
419 if (ret != -EBADMSG) {
420 pr_debug("Parser recognised the format (ret %d)\n",
426 up_read(&asymmetric_key_parsers_sem);
427 pr_devel("<==%s() = %d\n", __func__, ret);
432 * Clean up the key ID list
434 static void asymmetric_key_free_kids(struct asymmetric_key_ids *kids)
439 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
446 * Clean up the preparse data
448 static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
450 struct asymmetric_key_subtype *subtype = prep->payload.data[asym_subtype];
451 struct asymmetric_key_ids *kids = prep->payload.data[asym_key_ids];
453 pr_devel("==>%s()\n", __func__);
456 subtype->destroy(prep->payload.data[asym_crypto],
457 prep->payload.data[asym_auth]);
458 module_put(subtype->owner);
460 asymmetric_key_free_kids(kids);
461 kfree(prep->description);
465 * dispose of the data dangling from the corpse of a asymmetric key
467 static void asymmetric_key_destroy(struct key *key)
469 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
470 struct asymmetric_key_ids *kids = key->payload.data[asym_key_ids];
471 void *data = key->payload.data[asym_crypto];
472 void *auth = key->payload.data[asym_auth];
474 key->payload.data[asym_crypto] = NULL;
475 key->payload.data[asym_subtype] = NULL;
476 key->payload.data[asym_key_ids] = NULL;
477 key->payload.data[asym_auth] = NULL;
480 subtype->destroy(data, auth);
481 module_put(subtype->owner);
484 asymmetric_key_free_kids(kids);
487 static struct key_restriction *asymmetric_restriction_alloc(
488 key_restrict_link_func_t check,
491 struct key_restriction *keyres =
492 kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
495 return ERR_PTR(-ENOMEM);
497 keyres->check = check;
499 keyres->keytype = &key_type_asymmetric;
505 * look up keyring restrict functions for asymmetric keys
507 static struct key_restriction *asymmetric_lookup_restriction(
508 const char *restriction)
510 char *restrict_method;
513 struct key_restriction *ret = ERR_PTR(-EINVAL);
515 if (strcmp("builtin_trusted", restriction) == 0)
516 return asymmetric_restriction_alloc(
517 restrict_link_by_builtin_trusted, NULL);
519 if (strcmp("builtin_and_secondary_trusted", restriction) == 0)
520 return asymmetric_restriction_alloc(
521 restrict_link_by_builtin_and_secondary_trusted, NULL);
523 parse_buf = kstrndup(restriction, PAGE_SIZE, GFP_KERNEL);
525 return ERR_PTR(-ENOMEM);
528 restrict_method = strsep(&next, ":");
530 if ((strcmp(restrict_method, "key_or_keyring") == 0) && next) {
534 key_restrict_link_func_t link_fn =
535 restrict_link_by_key_or_keyring;
536 bool allow_null_key = false;
538 key_text = strsep(&next, ":");
541 if (strcmp(next, "chain") != 0)
544 link_fn = restrict_link_by_key_or_keyring_chain;
545 allow_null_key = true;
548 if (kstrtos32(key_text, 0, &serial) < 0)
551 if ((serial == 0) && allow_null_key) {
554 key = key_lookup(serial);
561 ret = asymmetric_restriction_alloc(link_fn, key);
571 int asymmetric_key_eds_op(struct kernel_pkey_params *params,
572 const void *in, void *out)
574 const struct asymmetric_key_subtype *subtype;
575 struct key *key = params->key;
578 pr_devel("==>%s()\n", __func__);
580 if (key->type != &key_type_asymmetric)
582 subtype = asymmetric_key_subtype(key);
584 !key->payload.data[0])
586 if (!subtype->eds_op)
589 ret = subtype->eds_op(params, in, out);
591 pr_devel("<==%s() = %d\n", __func__, ret);
595 static int asymmetric_key_verify_signature(struct kernel_pkey_params *params,
596 const void *in, const void *in2)
598 struct public_key_signature sig = {
599 .s_size = params->in2_len,
600 .digest_size = params->in_len,
601 .encoding = params->encoding,
602 .hash_algo = params->hash_algo,
603 .digest = (void *)in,
607 return verify_signature(params->key, &sig);
610 struct key_type key_type_asymmetric = {
611 .name = "asymmetric",
612 .preparse = asymmetric_key_preparse,
613 .free_preparse = asymmetric_key_free_preparse,
614 .instantiate = generic_key_instantiate,
615 .match_preparse = asymmetric_key_match_preparse,
616 .match_free = asymmetric_key_match_free,
617 .destroy = asymmetric_key_destroy,
618 .describe = asymmetric_key_describe,
619 .lookup_restriction = asymmetric_lookup_restriction,
620 .asym_query = query_asymmetric_key,
621 .asym_eds_op = asymmetric_key_eds_op,
622 .asym_verify_signature = asymmetric_key_verify_signature,
624 EXPORT_SYMBOL_GPL(key_type_asymmetric);
627 * register_asymmetric_key_parser - Register a asymmetric key blob parser
628 * @parser: The parser to register
630 int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
632 struct asymmetric_key_parser *cursor;
635 down_write(&asymmetric_key_parsers_sem);
637 list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
638 if (strcmp(cursor->name, parser->name) == 0) {
639 pr_err("Asymmetric key parser '%s' already registered\n",
646 list_add_tail(&parser->link, &asymmetric_key_parsers);
648 pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
652 up_write(&asymmetric_key_parsers_sem);
655 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
658 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
659 * @parser: The parser to unregister
661 void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
663 down_write(&asymmetric_key_parsers_sem);
664 list_del(&parser->link);
665 up_write(&asymmetric_key_parsers_sem);
667 pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
669 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
674 static int __init asymmetric_key_init(void)
676 return register_key_type(&key_type_asymmetric);
679 static void __exit asymmetric_key_cleanup(void)
681 unregister_key_type(&key_type_asymmetric);
684 module_init(asymmetric_key_init);
685 module_exit(asymmetric_key_cleanup);