1 /* Asymmetric public-key cryptography key type
3 * See Documentation/security/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 "asymmetric_keys.h"
23 MODULE_LICENSE("GPL");
25 const char *const key_being_used_for
[NR__KEY_BEING_USED_FOR
] = {
26 [VERIFYING_MODULE_SIGNATURE
] = "mod sig",
27 [VERIFYING_FIRMWARE_SIGNATURE
] = "firmware sig",
28 [VERIFYING_KEXEC_PE_SIGNATURE
] = "kexec PE sig",
29 [VERIFYING_KEY_SIGNATURE
] = "key sig",
30 [VERIFYING_KEY_SELF_SIGNATURE
] = "key self sig",
31 [VERIFYING_UNSPECIFIED_SIGNATURE
] = "unspec sig",
33 EXPORT_SYMBOL_GPL(key_being_used_for
);
35 static LIST_HEAD(asymmetric_key_parsers
);
36 static DECLARE_RWSEM(asymmetric_key_parsers_sem
);
39 * find_asymmetric_key - Find a key by ID.
40 * @keyring: The keys to search.
41 * @id_0: The first ID to look for or NULL.
42 * @id_1: The second ID to look for or NULL.
43 * @partial: Use partial match 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 * lookup is by the former, but the latter must also match.
49 struct key
*find_asymmetric_key(struct key
*keyring
,
50 const struct asymmetric_key_id
*id_0
,
51 const struct asymmetric_key_id
*id_1
,
68 /* Construct an identifier "id:<keyid>". */
69 p
= req
= kmalloc(2 + 1 + len
* 2 + 1, GFP_KERNEL
);
71 return ERR_PTR(-ENOMEM
);
81 p
= bin2hex(p
, lookup
, len
);
84 pr_debug("Look up: \"%s\"\n", req
);
86 ref
= keyring_search(make_key_ref(keyring
, 1),
87 &key_type_asymmetric
, req
);
89 pr_debug("Request for key '%s' err %ld\n", req
, PTR_ERR(ref
));
93 switch (PTR_ERR(ref
)) {
94 /* Hide some search errors */
98 return ERR_PTR(-ENOKEY
);
100 return ERR_CAST(ref
);
104 key
= key_ref_to_ptr(ref
);
106 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
109 pr_debug("First ID matches, but second is missing\n");
112 if (!asymmetric_key_id_same(id_1
, kids
->id
[1])) {
113 pr_debug("First ID matches, but second does not\n");
118 pr_devel("<==%s() = 0 [%x]\n", __func__
, key_serial(key
));
123 return ERR_PTR(-EKEYREJECTED
);
125 EXPORT_SYMBOL_GPL(find_asymmetric_key
);
128 * asymmetric_key_generate_id: Construct an asymmetric key ID
129 * @val_1: First binary blob
130 * @len_1: Length of first binary blob
131 * @val_2: Second binary blob
132 * @len_2: Length of second binary blob
134 * Construct an asymmetric key ID from a pair of binary blobs.
136 struct asymmetric_key_id
*asymmetric_key_generate_id(const void *val_1
,
141 struct asymmetric_key_id
*kid
;
143 kid
= kmalloc(sizeof(struct asymmetric_key_id
) + len_1
+ len_2
,
146 return ERR_PTR(-ENOMEM
);
147 kid
->len
= len_1
+ len_2
;
148 memcpy(kid
->data
, val_1
, len_1
);
149 memcpy(kid
->data
+ len_1
, val_2
, len_2
);
152 EXPORT_SYMBOL_GPL(asymmetric_key_generate_id
);
155 * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
156 * @kid_1, @kid_2: The key IDs to compare
158 bool asymmetric_key_id_same(const struct asymmetric_key_id
*kid1
,
159 const struct asymmetric_key_id
*kid2
)
163 if (kid1
->len
!= kid2
->len
)
165 return memcmp(kid1
->data
, kid2
->data
, kid1
->len
) == 0;
167 EXPORT_SYMBOL_GPL(asymmetric_key_id_same
);
170 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
172 * @kid_1, @kid_2: The key IDs to compare
174 bool asymmetric_key_id_partial(const struct asymmetric_key_id
*kid1
,
175 const struct asymmetric_key_id
*kid2
)
179 if (kid1
->len
< kid2
->len
)
181 return memcmp(kid1
->data
+ (kid1
->len
- kid2
->len
),
182 kid2
->data
, kid2
->len
) == 0;
184 EXPORT_SYMBOL_GPL(asymmetric_key_id_partial
);
187 * asymmetric_match_key_ids - Search asymmetric key IDs
188 * @kids: The list of key IDs to check
189 * @match_id: The key ID we're looking for
190 * @match: The match function to use
192 static bool asymmetric_match_key_ids(
193 const struct asymmetric_key_ids
*kids
,
194 const struct asymmetric_key_id
*match_id
,
195 bool (*match
)(const struct asymmetric_key_id
*kid1
,
196 const struct asymmetric_key_id
*kid2
))
200 if (!kids
|| !match_id
)
202 for (i
= 0; i
< ARRAY_SIZE(kids
->id
); i
++)
203 if (match(kids
->id
[i
], match_id
))
208 /* helper function can be called directly with pre-allocated memory */
209 inline int __asymmetric_key_hex_to_key_id(const char *id
,
210 struct asymmetric_key_id
*match_id
,
213 match_id
->len
= hexlen
;
214 return hex2bin(match_id
->data
, id
, hexlen
);
218 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
219 * @id: The ID as a hex string.
221 struct asymmetric_key_id
*asymmetric_key_hex_to_key_id(const char *id
)
223 struct asymmetric_key_id
*match_id
;
228 return ERR_PTR(-EINVAL
);
229 asciihexlen
= strlen(id
);
231 return ERR_PTR(-EINVAL
);
233 match_id
= kmalloc(sizeof(struct asymmetric_key_id
) + asciihexlen
/ 2,
236 return ERR_PTR(-ENOMEM
);
237 ret
= __asymmetric_key_hex_to_key_id(id
, match_id
, asciihexlen
/ 2);
240 return ERR_PTR(-EINVAL
);
246 * Match asymmetric keys by an exact match on an ID.
248 static bool asymmetric_key_cmp(const struct key
*key
,
249 const struct key_match_data
*match_data
)
251 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
252 const struct asymmetric_key_id
*match_id
= match_data
->preparsed
;
254 return asymmetric_match_key_ids(kids
, match_id
,
255 asymmetric_key_id_same
);
259 * Match asymmetric keys by a partial match on an IDs.
261 static bool asymmetric_key_cmp_partial(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_partial
);
272 * Preparse the match criterion. If we don't set lookup_type and cmp,
273 * the default will be an exact match on the key description.
275 * There are some specifiers for matching key IDs rather than by the key
278 * "id:<id>" - find a key by partial match on any available ID
279 * "ex:<id>" - find a key by exact match on any available ID
281 * These have to be searched by iteration rather than by direct lookup because
282 * the key is hashed according to its description.
284 static int asymmetric_key_match_preparse(struct key_match_data
*match_data
)
286 struct asymmetric_key_id
*match_id
;
287 const char *spec
= match_data
->raw_data
;
289 bool (*cmp
)(const struct key
*, const struct key_match_data
*) =
294 if (spec
[0] == 'i' &&
298 cmp
= asymmetric_key_cmp_partial
;
299 } else if (spec
[0] == 'e' &&
307 match_id
= asymmetric_key_hex_to_key_id(id
);
308 if (IS_ERR(match_id
))
309 return PTR_ERR(match_id
);
311 match_data
->preparsed
= match_id
;
312 match_data
->cmp
= cmp
;
313 match_data
->lookup_type
= KEYRING_SEARCH_LOOKUP_ITERATE
;
321 * Free the preparsed the match criterion.
323 static void asymmetric_key_match_free(struct key_match_data
*match_data
)
325 kfree(match_data
->preparsed
);
329 * Describe the asymmetric key
331 static void asymmetric_key_describe(const struct key
*key
, struct seq_file
*m
)
333 const struct asymmetric_key_subtype
*subtype
= asymmetric_key_subtype(key
);
334 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
335 const struct asymmetric_key_id
*kid
;
336 const unsigned char *p
;
339 seq_puts(m
, key
->description
);
343 subtype
->describe(key
, m
);
345 if (kids
&& kids
->id
[1]) {
354 seq_printf(m
, "%*phN", n
, p
);
358 /* put something here to indicate the key's capabilities */
364 * Preparse a asymmetric payload to get format the contents appropriately for the
365 * internal payload to cut down on the number of scans of the data performed.
367 * We also generate a proposed description from the contents of the key that
368 * can be used to name the key if the user doesn't want to provide one.
370 static int asymmetric_key_preparse(struct key_preparsed_payload
*prep
)
372 struct asymmetric_key_parser
*parser
;
375 pr_devel("==>%s()\n", __func__
);
377 if (prep
->datalen
== 0)
380 down_read(&asymmetric_key_parsers_sem
);
383 list_for_each_entry(parser
, &asymmetric_key_parsers
, link
) {
384 pr_debug("Trying parser '%s'\n", parser
->name
);
386 ret
= parser
->parse(prep
);
387 if (ret
!= -EBADMSG
) {
388 pr_debug("Parser recognised the format (ret %d)\n",
394 up_read(&asymmetric_key_parsers_sem
);
395 pr_devel("<==%s() = %d\n", __func__
, ret
);
400 * Clean up the key ID list
402 static void asymmetric_key_free_kids(struct asymmetric_key_ids
*kids
)
407 for (i
= 0; i
< ARRAY_SIZE(kids
->id
); i
++)
414 * Clean up the preparse data
416 static void asymmetric_key_free_preparse(struct key_preparsed_payload
*prep
)
418 struct asymmetric_key_subtype
*subtype
= prep
->payload
.data
[asym_subtype
];
419 struct asymmetric_key_ids
*kids
= prep
->payload
.data
[asym_key_ids
];
421 pr_devel("==>%s()\n", __func__
);
424 subtype
->destroy(prep
->payload
.data
[asym_crypto
],
425 prep
->payload
.data
[asym_auth
]);
426 module_put(subtype
->owner
);
428 asymmetric_key_free_kids(kids
);
429 kfree(prep
->description
);
433 * dispose of the data dangling from the corpse of a asymmetric key
435 static void asymmetric_key_destroy(struct key
*key
)
437 struct asymmetric_key_subtype
*subtype
= asymmetric_key_subtype(key
);
438 struct asymmetric_key_ids
*kids
= key
->payload
.data
[asym_key_ids
];
439 void *data
= key
->payload
.data
[asym_crypto
];
440 void *auth
= key
->payload
.data
[asym_auth
];
442 key
->payload
.data
[asym_crypto
] = NULL
;
443 key
->payload
.data
[asym_subtype
] = NULL
;
444 key
->payload
.data
[asym_key_ids
] = NULL
;
445 key
->payload
.data
[asym_auth
] = NULL
;
448 subtype
->destroy(data
, auth
);
449 module_put(subtype
->owner
);
452 asymmetric_key_free_kids(kids
);
455 static struct key_restriction
*asymmetric_restriction_alloc(
456 key_restrict_link_func_t check
,
459 struct key_restriction
*keyres
=
460 kzalloc(sizeof(struct key_restriction
), GFP_KERNEL
);
463 return ERR_PTR(-ENOMEM
);
465 keyres
->check
= check
;
467 keyres
->keytype
= &key_type_asymmetric
;
473 * look up keyring restrict functions for asymmetric keys
475 static struct key_restriction
*asymmetric_lookup_restriction(
476 const char *restriction
)
478 char *restrict_method
;
481 struct key_restriction
*ret
= ERR_PTR(-EINVAL
);
483 if (strcmp("builtin_trusted", restriction
) == 0)
484 return asymmetric_restriction_alloc(
485 restrict_link_by_builtin_trusted
, NULL
);
487 if (strcmp("builtin_and_secondary_trusted", restriction
) == 0)
488 return asymmetric_restriction_alloc(
489 restrict_link_by_builtin_and_secondary_trusted
, NULL
);
491 parse_buf
= kstrndup(restriction
, PAGE_SIZE
, GFP_KERNEL
);
493 return ERR_PTR(-ENOMEM
);
496 restrict_method
= strsep(&next
, ":");
498 if ((strcmp(restrict_method
, "key_or_keyring") == 0) && next
) {
502 key_restrict_link_func_t link_fn
=
503 restrict_link_by_key_or_keyring
;
504 bool allow_null_key
= false;
506 key_text
= strsep(&next
, ":");
509 if (strcmp(next
, "chain") != 0)
512 link_fn
= restrict_link_by_key_or_keyring_chain
;
513 allow_null_key
= true;
516 if (kstrtos32(key_text
, 0, &serial
) < 0)
519 if ((serial
== 0) && allow_null_key
) {
522 key
= key_lookup(serial
);
529 ret
= asymmetric_restriction_alloc(link_fn
, key
);
539 struct key_type key_type_asymmetric
= {
540 .name
= "asymmetric",
541 .preparse
= asymmetric_key_preparse
,
542 .free_preparse
= asymmetric_key_free_preparse
,
543 .instantiate
= generic_key_instantiate
,
544 .match_preparse
= asymmetric_key_match_preparse
,
545 .match_free
= asymmetric_key_match_free
,
546 .destroy
= asymmetric_key_destroy
,
547 .describe
= asymmetric_key_describe
,
548 .lookup_restriction
= asymmetric_lookup_restriction
,
550 EXPORT_SYMBOL_GPL(key_type_asymmetric
);
553 * register_asymmetric_key_parser - Register a asymmetric key blob parser
554 * @parser: The parser to register
556 int register_asymmetric_key_parser(struct asymmetric_key_parser
*parser
)
558 struct asymmetric_key_parser
*cursor
;
561 down_write(&asymmetric_key_parsers_sem
);
563 list_for_each_entry(cursor
, &asymmetric_key_parsers
, link
) {
564 if (strcmp(cursor
->name
, parser
->name
) == 0) {
565 pr_err("Asymmetric key parser '%s' already registered\n",
572 list_add_tail(&parser
->link
, &asymmetric_key_parsers
);
574 pr_notice("Asymmetric key parser '%s' registered\n", parser
->name
);
578 up_write(&asymmetric_key_parsers_sem
);
581 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser
);
584 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
585 * @parser: The parser to unregister
587 void unregister_asymmetric_key_parser(struct asymmetric_key_parser
*parser
)
589 down_write(&asymmetric_key_parsers_sem
);
590 list_del(&parser
->link
);
591 up_write(&asymmetric_key_parsers_sem
);
593 pr_notice("Asymmetric key parser '%s' unregistered\n", parser
->name
);
595 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser
);
600 static int __init
asymmetric_key_init(void)
602 return register_key_type(&key_type_asymmetric
);
605 static void __exit
asymmetric_key_cleanup(void)
607 unregister_key_type(&key_type_asymmetric
);
610 module_init(asymmetric_key_init
);
611 module_exit(asymmetric_key_cleanup
);