1 /* Asymmetric public-key cryptography key type
3 * See Documentation/crypto/asymmetric-keys.txt
5 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public Licence
10 * as published by the Free Software Foundation; either version
11 * 2 of the Licence, or (at your option) any later version.
13 #include <keys/asymmetric-subtype.h>
14 #include <keys/asymmetric-parser.h>
15 #include <crypto/public_key.h>
16 #include <linux/seq_file.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/ctype.h>
20 #include <keys/system_keyring.h>
21 #include <keys/user-type.h>
22 #include "asymmetric_keys.h"
24 MODULE_LICENSE("GPL");
26 const char *const key_being_used_for
[NR__KEY_BEING_USED_FOR
] = {
27 [VERIFYING_MODULE_SIGNATURE
] = "mod sig",
28 [VERIFYING_FIRMWARE_SIGNATURE
] = "firmware sig",
29 [VERIFYING_KEXEC_PE_SIGNATURE
] = "kexec PE sig",
30 [VERIFYING_KEY_SIGNATURE
] = "key sig",
31 [VERIFYING_KEY_SELF_SIGNATURE
] = "key self sig",
32 [VERIFYING_UNSPECIFIED_SIGNATURE
] = "unspec sig",
34 EXPORT_SYMBOL_GPL(key_being_used_for
);
36 static LIST_HEAD(asymmetric_key_parsers
);
37 static DECLARE_RWSEM(asymmetric_key_parsers_sem
);
40 * find_asymmetric_key - Find a key by ID.
41 * @keyring: The keys to search.
42 * @id_0: The first ID to look for or NULL.
43 * @id_1: The second ID to look for or NULL.
44 * @partial: Use partial match if true, exact if false.
46 * Find a key in the given keyring by identifier. The preferred identifier is
47 * the id_0 and the fallback identifier is the id_1. If both are given, the
48 * lookup is by the former, but the latter must also match.
50 struct key
*find_asymmetric_key(struct key
*keyring
,
51 const struct asymmetric_key_id
*id_0
,
52 const struct asymmetric_key_id
*id_1
,
61 BUG_ON(!id_0
&& !id_1
);
71 /* Construct an identifier "id:<keyid>". */
72 p
= req
= kmalloc(2 + 1 + len
* 2 + 1, GFP_KERNEL
);
74 return ERR_PTR(-ENOMEM
);
84 p
= bin2hex(p
, lookup
, len
);
87 pr_debug("Look up: \"%s\"\n", req
);
89 ref
= keyring_search(make_key_ref(keyring
, 1),
90 &key_type_asymmetric
, req
);
92 pr_debug("Request for key '%s' err %ld\n", req
, PTR_ERR(ref
));
96 switch (PTR_ERR(ref
)) {
97 /* Hide some search errors */
101 return ERR_PTR(-ENOKEY
);
103 return ERR_CAST(ref
);
107 key
= key_ref_to_ptr(ref
);
109 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
112 pr_debug("First ID matches, but second is missing\n");
115 if (!asymmetric_key_id_same(id_1
, kids
->id
[1])) {
116 pr_debug("First ID matches, but second does not\n");
121 pr_devel("<==%s() = 0 [%x]\n", __func__
, key_serial(key
));
126 return ERR_PTR(-EKEYREJECTED
);
128 EXPORT_SYMBOL_GPL(find_asymmetric_key
);
131 * asymmetric_key_generate_id: Construct an asymmetric key ID
132 * @val_1: First binary blob
133 * @len_1: Length of first binary blob
134 * @val_2: Second binary blob
135 * @len_2: Length of second binary blob
137 * Construct an asymmetric key ID from a pair of binary blobs.
139 struct asymmetric_key_id
*asymmetric_key_generate_id(const void *val_1
,
144 struct asymmetric_key_id
*kid
;
146 kid
= kmalloc(sizeof(struct asymmetric_key_id
) + len_1
+ len_2
,
149 return ERR_PTR(-ENOMEM
);
150 kid
->len
= len_1
+ len_2
;
151 memcpy(kid
->data
, val_1
, len_1
);
152 memcpy(kid
->data
+ len_1
, val_2
, len_2
);
155 EXPORT_SYMBOL_GPL(asymmetric_key_generate_id
);
158 * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
159 * @kid_1, @kid_2: The key IDs to compare
161 bool asymmetric_key_id_same(const struct asymmetric_key_id
*kid1
,
162 const struct asymmetric_key_id
*kid2
)
166 if (kid1
->len
!= kid2
->len
)
168 return memcmp(kid1
->data
, kid2
->data
, kid1
->len
) == 0;
170 EXPORT_SYMBOL_GPL(asymmetric_key_id_same
);
173 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
175 * @kid_1, @kid_2: The key IDs to compare
177 bool asymmetric_key_id_partial(const struct asymmetric_key_id
*kid1
,
178 const struct asymmetric_key_id
*kid2
)
182 if (kid1
->len
< kid2
->len
)
184 return memcmp(kid1
->data
+ (kid1
->len
- kid2
->len
),
185 kid2
->data
, kid2
->len
) == 0;
187 EXPORT_SYMBOL_GPL(asymmetric_key_id_partial
);
190 * asymmetric_match_key_ids - Search asymmetric key IDs
191 * @kids: The list of key IDs to check
192 * @match_id: The key ID we're looking for
193 * @match: The match function to use
195 static bool asymmetric_match_key_ids(
196 const struct asymmetric_key_ids
*kids
,
197 const struct asymmetric_key_id
*match_id
,
198 bool (*match
)(const struct asymmetric_key_id
*kid1
,
199 const struct asymmetric_key_id
*kid2
))
203 if (!kids
|| !match_id
)
205 for (i
= 0; i
< ARRAY_SIZE(kids
->id
); i
++)
206 if (match(kids
->id
[i
], match_id
))
211 /* helper function can be called directly with pre-allocated memory */
212 inline int __asymmetric_key_hex_to_key_id(const char *id
,
213 struct asymmetric_key_id
*match_id
,
216 match_id
->len
= hexlen
;
217 return hex2bin(match_id
->data
, id
, hexlen
);
221 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
222 * @id: The ID as a hex string.
224 struct asymmetric_key_id
*asymmetric_key_hex_to_key_id(const char *id
)
226 struct asymmetric_key_id
*match_id
;
231 return ERR_PTR(-EINVAL
);
232 asciihexlen
= strlen(id
);
234 return ERR_PTR(-EINVAL
);
236 match_id
= kmalloc(sizeof(struct asymmetric_key_id
) + asciihexlen
/ 2,
239 return ERR_PTR(-ENOMEM
);
240 ret
= __asymmetric_key_hex_to_key_id(id
, match_id
, asciihexlen
/ 2);
243 return ERR_PTR(-EINVAL
);
249 * Match asymmetric keys by an exact match on an ID.
251 static bool asymmetric_key_cmp(const struct key
*key
,
252 const struct key_match_data
*match_data
)
254 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
255 const struct asymmetric_key_id
*match_id
= match_data
->preparsed
;
257 return asymmetric_match_key_ids(kids
, match_id
,
258 asymmetric_key_id_same
);
262 * Match asymmetric keys by a partial match on an IDs.
264 static bool asymmetric_key_cmp_partial(const struct key
*key
,
265 const struct key_match_data
*match_data
)
267 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
268 const struct asymmetric_key_id
*match_id
= match_data
->preparsed
;
270 return asymmetric_match_key_ids(kids
, match_id
,
271 asymmetric_key_id_partial
);
275 * Preparse the match criterion. If we don't set lookup_type and cmp,
276 * the default will be an exact match on the key description.
278 * There are some specifiers for matching key IDs rather than by the key
281 * "id:<id>" - find a key by partial match on any available ID
282 * "ex:<id>" - find a key by exact match on any available ID
284 * These have to be searched by iteration rather than by direct lookup because
285 * the key is hashed according to its description.
287 static int asymmetric_key_match_preparse(struct key_match_data
*match_data
)
289 struct asymmetric_key_id
*match_id
;
290 const char *spec
= match_data
->raw_data
;
292 bool (*cmp
)(const struct key
*, const struct key_match_data
*) =
297 if (spec
[0] == 'i' &&
301 cmp
= asymmetric_key_cmp_partial
;
302 } else if (spec
[0] == 'e' &&
310 match_id
= asymmetric_key_hex_to_key_id(id
);
311 if (IS_ERR(match_id
))
312 return PTR_ERR(match_id
);
314 match_data
->preparsed
= match_id
;
315 match_data
->cmp
= cmp
;
316 match_data
->lookup_type
= KEYRING_SEARCH_LOOKUP_ITERATE
;
324 * Free the preparsed the match criterion.
326 static void asymmetric_key_match_free(struct key_match_data
*match_data
)
328 kfree(match_data
->preparsed
);
332 * Describe the asymmetric key
334 static void asymmetric_key_describe(const struct key
*key
, struct seq_file
*m
)
336 const struct asymmetric_key_subtype
*subtype
= asymmetric_key_subtype(key
);
337 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
338 const struct asymmetric_key_id
*kid
;
339 const unsigned char *p
;
342 seq_puts(m
, key
->description
);
346 subtype
->describe(key
, m
);
348 if (kids
&& kids
->id
[1]) {
357 seq_printf(m
, "%*phN", n
, p
);
361 /* put something here to indicate the key's capabilities */
367 * Preparse a asymmetric payload to get format the contents appropriately for the
368 * internal payload to cut down on the number of scans of the data performed.
370 * We also generate a proposed description from the contents of the key that
371 * can be used to name the key if the user doesn't want to provide one.
373 static int asymmetric_key_preparse(struct key_preparsed_payload
*prep
)
375 struct asymmetric_key_parser
*parser
;
378 pr_devel("==>%s()\n", __func__
);
380 if (prep
->datalen
== 0)
383 down_read(&asymmetric_key_parsers_sem
);
386 list_for_each_entry(parser
, &asymmetric_key_parsers
, link
) {
387 pr_debug("Trying parser '%s'\n", parser
->name
);
389 ret
= parser
->parse(prep
);
390 if (ret
!= -EBADMSG
) {
391 pr_debug("Parser recognised the format (ret %d)\n",
397 up_read(&asymmetric_key_parsers_sem
);
398 pr_devel("<==%s() = %d\n", __func__
, ret
);
403 * Clean up the key ID list
405 static void asymmetric_key_free_kids(struct asymmetric_key_ids
*kids
)
410 for (i
= 0; i
< ARRAY_SIZE(kids
->id
); i
++)
417 * Clean up the preparse data
419 static void asymmetric_key_free_preparse(struct key_preparsed_payload
*prep
)
421 struct asymmetric_key_subtype
*subtype
= prep
->payload
.data
[asym_subtype
];
422 struct asymmetric_key_ids
*kids
= prep
->payload
.data
[asym_key_ids
];
424 pr_devel("==>%s()\n", __func__
);
427 subtype
->destroy(prep
->payload
.data
[asym_crypto
],
428 prep
->payload
.data
[asym_auth
]);
429 module_put(subtype
->owner
);
431 asymmetric_key_free_kids(kids
);
432 kfree(prep
->description
);
436 * dispose of the data dangling from the corpse of a asymmetric key
438 static void asymmetric_key_destroy(struct key
*key
)
440 struct asymmetric_key_subtype
*subtype
= asymmetric_key_subtype(key
);
441 struct asymmetric_key_ids
*kids
= key
->payload
.data
[asym_key_ids
];
442 void *data
= key
->payload
.data
[asym_crypto
];
443 void *auth
= key
->payload
.data
[asym_auth
];
445 key
->payload
.data
[asym_crypto
] = NULL
;
446 key
->payload
.data
[asym_subtype
] = NULL
;
447 key
->payload
.data
[asym_key_ids
] = NULL
;
448 key
->payload
.data
[asym_auth
] = NULL
;
451 subtype
->destroy(data
, auth
);
452 module_put(subtype
->owner
);
455 asymmetric_key_free_kids(kids
);
458 static struct key_restriction
*asymmetric_restriction_alloc(
459 key_restrict_link_func_t check
,
462 struct key_restriction
*keyres
=
463 kzalloc(sizeof(struct key_restriction
), GFP_KERNEL
);
466 return ERR_PTR(-ENOMEM
);
468 keyres
->check
= check
;
470 keyres
->keytype
= &key_type_asymmetric
;
476 * look up keyring restrict functions for asymmetric keys
478 static struct key_restriction
*asymmetric_lookup_restriction(
479 const char *restriction
)
481 char *restrict_method
;
484 struct key_restriction
*ret
= ERR_PTR(-EINVAL
);
486 if (strcmp("builtin_trusted", restriction
) == 0)
487 return asymmetric_restriction_alloc(
488 restrict_link_by_builtin_trusted
, NULL
);
490 if (strcmp("builtin_and_secondary_trusted", restriction
) == 0)
491 return asymmetric_restriction_alloc(
492 restrict_link_by_builtin_and_secondary_trusted
, NULL
);
494 parse_buf
= kstrndup(restriction
, PAGE_SIZE
, GFP_KERNEL
);
496 return ERR_PTR(-ENOMEM
);
499 restrict_method
= strsep(&next
, ":");
501 if ((strcmp(restrict_method
, "key_or_keyring") == 0) && next
) {
505 key_restrict_link_func_t link_fn
=
506 restrict_link_by_key_or_keyring
;
507 bool allow_null_key
= false;
509 key_text
= strsep(&next
, ":");
512 if (strcmp(next
, "chain") != 0)
515 link_fn
= restrict_link_by_key_or_keyring_chain
;
516 allow_null_key
= true;
519 if (kstrtos32(key_text
, 0, &serial
) < 0)
522 if ((serial
== 0) && allow_null_key
) {
525 key
= key_lookup(serial
);
532 ret
= asymmetric_restriction_alloc(link_fn
, key
);
542 int asymmetric_key_eds_op(struct kernel_pkey_params
*params
,
543 const void *in
, void *out
)
545 const struct asymmetric_key_subtype
*subtype
;
546 struct key
*key
= params
->key
;
549 pr_devel("==>%s()\n", __func__
);
551 if (key
->type
!= &key_type_asymmetric
)
553 subtype
= asymmetric_key_subtype(key
);
555 !key
->payload
.data
[0])
557 if (!subtype
->eds_op
)
560 ret
= subtype
->eds_op(params
, in
, out
);
562 pr_devel("<==%s() = %d\n", __func__
, ret
);
566 static int asymmetric_key_verify_signature(struct kernel_pkey_params
*params
,
567 const void *in
, const void *in2
)
569 struct public_key_signature sig
= {
570 .s_size
= params
->in2_len
,
571 .digest_size
= params
->in_len
,
572 .encoding
= params
->encoding
,
573 .hash_algo
= params
->hash_algo
,
574 .digest
= (void *)in
,
578 return verify_signature(params
->key
, &sig
);
581 struct key_type key_type_asymmetric
= {
582 .name
= "asymmetric",
583 .preparse
= asymmetric_key_preparse
,
584 .free_preparse
= asymmetric_key_free_preparse
,
585 .instantiate
= generic_key_instantiate
,
586 .match_preparse
= asymmetric_key_match_preparse
,
587 .match_free
= asymmetric_key_match_free
,
588 .destroy
= asymmetric_key_destroy
,
589 .describe
= asymmetric_key_describe
,
590 .lookup_restriction
= asymmetric_lookup_restriction
,
591 .asym_query
= query_asymmetric_key
,
592 .asym_eds_op
= asymmetric_key_eds_op
,
593 .asym_verify_signature
= asymmetric_key_verify_signature
,
595 EXPORT_SYMBOL_GPL(key_type_asymmetric
);
598 * register_asymmetric_key_parser - Register a asymmetric key blob parser
599 * @parser: The parser to register
601 int register_asymmetric_key_parser(struct asymmetric_key_parser
*parser
)
603 struct asymmetric_key_parser
*cursor
;
606 down_write(&asymmetric_key_parsers_sem
);
608 list_for_each_entry(cursor
, &asymmetric_key_parsers
, link
) {
609 if (strcmp(cursor
->name
, parser
->name
) == 0) {
610 pr_err("Asymmetric key parser '%s' already registered\n",
617 list_add_tail(&parser
->link
, &asymmetric_key_parsers
);
619 pr_notice("Asymmetric key parser '%s' registered\n", parser
->name
);
623 up_write(&asymmetric_key_parsers_sem
);
626 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser
);
629 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
630 * @parser: The parser to unregister
632 void unregister_asymmetric_key_parser(struct asymmetric_key_parser
*parser
)
634 down_write(&asymmetric_key_parsers_sem
);
635 list_del(&parser
->link
);
636 up_write(&asymmetric_key_parsers_sem
);
638 pr_notice("Asymmetric key parser '%s' unregistered\n", parser
->name
);
640 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser
);
645 static int __init
asymmetric_key_init(void)
647 return register_key_type(&key_type_asymmetric
);
650 static void __exit
asymmetric_key_cleanup(void)
652 unregister_key_type(&key_type_asymmetric
);
655 module_init(asymmetric_key_init
);
656 module_exit(asymmetric_key_cleanup
);