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 "asymmetric_keys.h"
22 MODULE_LICENSE("GPL");
24 const char *const key_being_used_for
[NR__KEY_BEING_USED_FOR
] = {
25 [VERIFYING_MODULE_SIGNATURE
] = "mod sig",
26 [VERIFYING_FIRMWARE_SIGNATURE
] = "firmware sig",
27 [VERIFYING_KEXEC_PE_SIGNATURE
] = "kexec PE sig",
28 [VERIFYING_KEY_SIGNATURE
] = "key sig",
29 [VERIFYING_KEY_SELF_SIGNATURE
] = "key self sig",
30 [VERIFYING_UNSPECIFIED_SIGNATURE
] = "unspec sig",
32 EXPORT_SYMBOL_GPL(key_being_used_for
);
34 static LIST_HEAD(asymmetric_key_parsers
);
35 static DECLARE_RWSEM(asymmetric_key_parsers_sem
);
38 * asymmetric_key_generate_id: Construct an asymmetric key ID
39 * @val_1: First binary blob
40 * @len_1: Length of first binary blob
41 * @val_2: Second binary blob
42 * @len_2: Length of second binary blob
44 * Construct an asymmetric key ID from a pair of binary blobs.
46 struct asymmetric_key_id
*asymmetric_key_generate_id(const void *val_1
,
51 struct asymmetric_key_id
*kid
;
53 kid
= kmalloc(sizeof(struct asymmetric_key_id
) + len_1
+ len_2
,
56 return ERR_PTR(-ENOMEM
);
57 kid
->len
= len_1
+ len_2
;
58 memcpy(kid
->data
, val_1
, len_1
);
59 memcpy(kid
->data
+ len_1
, val_2
, len_2
);
62 EXPORT_SYMBOL_GPL(asymmetric_key_generate_id
);
65 * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
66 * @kid_1, @kid_2: The key IDs to compare
68 bool asymmetric_key_id_same(const struct asymmetric_key_id
*kid1
,
69 const struct asymmetric_key_id
*kid2
)
73 if (kid1
->len
!= kid2
->len
)
75 return memcmp(kid1
->data
, kid2
->data
, kid1
->len
) == 0;
77 EXPORT_SYMBOL_GPL(asymmetric_key_id_same
);
80 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
82 * @kid_1, @kid_2: The key IDs to compare
84 bool asymmetric_key_id_partial(const struct asymmetric_key_id
*kid1
,
85 const struct asymmetric_key_id
*kid2
)
89 if (kid1
->len
< kid2
->len
)
91 return memcmp(kid1
->data
+ (kid1
->len
- kid2
->len
),
92 kid2
->data
, kid2
->len
) == 0;
94 EXPORT_SYMBOL_GPL(asymmetric_key_id_partial
);
97 * asymmetric_match_key_ids - Search asymmetric key IDs
98 * @kids: The list of key IDs to check
99 * @match_id: The key ID we're looking for
100 * @match: The match function to use
102 static bool asymmetric_match_key_ids(
103 const struct asymmetric_key_ids
*kids
,
104 const struct asymmetric_key_id
*match_id
,
105 bool (*match
)(const struct asymmetric_key_id
*kid1
,
106 const struct asymmetric_key_id
*kid2
))
110 if (!kids
|| !match_id
)
112 for (i
= 0; i
< ARRAY_SIZE(kids
->id
); i
++)
113 if (match(kids
->id
[i
], match_id
))
118 /* helper function can be called directly with pre-allocated memory */
119 inline int __asymmetric_key_hex_to_key_id(const char *id
,
120 struct asymmetric_key_id
*match_id
,
123 match_id
->len
= hexlen
;
124 return hex2bin(match_id
->data
, id
, hexlen
);
128 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
129 * @id: The ID as a hex string.
131 struct asymmetric_key_id
*asymmetric_key_hex_to_key_id(const char *id
)
133 struct asymmetric_key_id
*match_id
;
138 return ERR_PTR(-EINVAL
);
139 asciihexlen
= strlen(id
);
141 return ERR_PTR(-EINVAL
);
143 match_id
= kmalloc(sizeof(struct asymmetric_key_id
) + asciihexlen
/ 2,
146 return ERR_PTR(-ENOMEM
);
147 ret
= __asymmetric_key_hex_to_key_id(id
, match_id
, asciihexlen
/ 2);
150 return ERR_PTR(-EINVAL
);
156 * Match asymmetric keys by an exact match on an ID.
158 static bool asymmetric_key_cmp(const struct key
*key
,
159 const struct key_match_data
*match_data
)
161 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
162 const struct asymmetric_key_id
*match_id
= match_data
->preparsed
;
164 return asymmetric_match_key_ids(kids
, match_id
,
165 asymmetric_key_id_same
);
169 * Match asymmetric keys by a partial match on an IDs.
171 static bool asymmetric_key_cmp_partial(const struct key
*key
,
172 const struct key_match_data
*match_data
)
174 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
175 const struct asymmetric_key_id
*match_id
= match_data
->preparsed
;
177 return asymmetric_match_key_ids(kids
, match_id
,
178 asymmetric_key_id_partial
);
182 * Preparse the match criterion. If we don't set lookup_type and cmp,
183 * the default will be an exact match on the key description.
185 * There are some specifiers for matching key IDs rather than by the key
188 * "id:<id>" - find a key by partial match on any available ID
189 * "ex:<id>" - find a key by exact match on any available ID
191 * These have to be searched by iteration rather than by direct lookup because
192 * the key is hashed according to its description.
194 static int asymmetric_key_match_preparse(struct key_match_data
*match_data
)
196 struct asymmetric_key_id
*match_id
;
197 const char *spec
= match_data
->raw_data
;
199 bool (*cmp
)(const struct key
*, const struct key_match_data
*) =
204 if (spec
[0] == 'i' &&
208 cmp
= asymmetric_key_cmp_partial
;
209 } else if (spec
[0] == 'e' &&
217 match_id
= asymmetric_key_hex_to_key_id(id
);
218 if (IS_ERR(match_id
))
219 return PTR_ERR(match_id
);
221 match_data
->preparsed
= match_id
;
222 match_data
->cmp
= cmp
;
223 match_data
->lookup_type
= KEYRING_SEARCH_LOOKUP_ITERATE
;
231 * Free the preparsed the match criterion.
233 static void asymmetric_key_match_free(struct key_match_data
*match_data
)
235 kfree(match_data
->preparsed
);
239 * Describe the asymmetric key
241 static void asymmetric_key_describe(const struct key
*key
, struct seq_file
*m
)
243 const struct asymmetric_key_subtype
*subtype
= asymmetric_key_subtype(key
);
244 const struct asymmetric_key_ids
*kids
= asymmetric_key_ids(key
);
245 const struct asymmetric_key_id
*kid
;
246 const unsigned char *p
;
249 seq_puts(m
, key
->description
);
253 subtype
->describe(key
, m
);
255 if (kids
&& kids
->id
[1]) {
264 seq_printf(m
, "%*phN", n
, p
);
268 /* put something here to indicate the key's capabilities */
274 * Preparse a asymmetric payload to get format the contents appropriately for the
275 * internal payload to cut down on the number of scans of the data performed.
277 * We also generate a proposed description from the contents of the key that
278 * can be used to name the key if the user doesn't want to provide one.
280 static int asymmetric_key_preparse(struct key_preparsed_payload
*prep
)
282 struct asymmetric_key_parser
*parser
;
285 pr_devel("==>%s()\n", __func__
);
287 if (prep
->datalen
== 0)
290 down_read(&asymmetric_key_parsers_sem
);
293 list_for_each_entry(parser
, &asymmetric_key_parsers
, link
) {
294 pr_debug("Trying parser '%s'\n", parser
->name
);
296 ret
= parser
->parse(prep
);
297 if (ret
!= -EBADMSG
) {
298 pr_debug("Parser recognised the format (ret %d)\n",
304 up_read(&asymmetric_key_parsers_sem
);
305 pr_devel("<==%s() = %d\n", __func__
, ret
);
310 * Clean up the key ID list
312 static void asymmetric_key_free_kids(struct asymmetric_key_ids
*kids
)
317 for (i
= 0; i
< ARRAY_SIZE(kids
->id
); i
++)
324 * Clean up the preparse data
326 static void asymmetric_key_free_preparse(struct key_preparsed_payload
*prep
)
328 struct asymmetric_key_subtype
*subtype
= prep
->payload
.data
[asym_subtype
];
329 struct asymmetric_key_ids
*kids
= prep
->payload
.data
[asym_key_ids
];
331 pr_devel("==>%s()\n", __func__
);
334 subtype
->destroy(prep
->payload
.data
[asym_crypto
]);
335 module_put(subtype
->owner
);
337 asymmetric_key_free_kids(kids
);
338 kfree(prep
->description
);
342 * dispose of the data dangling from the corpse of a asymmetric key
344 static void asymmetric_key_destroy(struct key
*key
)
346 struct asymmetric_key_subtype
*subtype
= asymmetric_key_subtype(key
);
347 struct asymmetric_key_ids
*kids
= key
->payload
.data
[asym_key_ids
];
348 void *data
= key
->payload
.data
[asym_crypto
];
350 key
->payload
.data
[asym_crypto
] = NULL
;
351 key
->payload
.data
[asym_subtype
] = NULL
;
352 key
->payload
.data
[asym_key_ids
] = NULL
;
355 subtype
->destroy(data
);
356 module_put(subtype
->owner
);
359 asymmetric_key_free_kids(kids
);
362 struct key_type key_type_asymmetric
= {
363 .name
= "asymmetric",
364 .preparse
= asymmetric_key_preparse
,
365 .free_preparse
= asymmetric_key_free_preparse
,
366 .instantiate
= generic_key_instantiate
,
367 .match_preparse
= asymmetric_key_match_preparse
,
368 .match_free
= asymmetric_key_match_free
,
369 .destroy
= asymmetric_key_destroy
,
370 .describe
= asymmetric_key_describe
,
372 EXPORT_SYMBOL_GPL(key_type_asymmetric
);
375 * register_asymmetric_key_parser - Register a asymmetric key blob parser
376 * @parser: The parser to register
378 int register_asymmetric_key_parser(struct asymmetric_key_parser
*parser
)
380 struct asymmetric_key_parser
*cursor
;
383 down_write(&asymmetric_key_parsers_sem
);
385 list_for_each_entry(cursor
, &asymmetric_key_parsers
, link
) {
386 if (strcmp(cursor
->name
, parser
->name
) == 0) {
387 pr_err("Asymmetric key parser '%s' already registered\n",
394 list_add_tail(&parser
->link
, &asymmetric_key_parsers
);
396 pr_notice("Asymmetric key parser '%s' registered\n", parser
->name
);
400 up_write(&asymmetric_key_parsers_sem
);
403 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser
);
406 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
407 * @parser: The parser to unregister
409 void unregister_asymmetric_key_parser(struct asymmetric_key_parser
*parser
)
411 down_write(&asymmetric_key_parsers_sem
);
412 list_del(&parser
->link
);
413 up_write(&asymmetric_key_parsers_sem
);
415 pr_notice("Asymmetric key parser '%s' unregistered\n", parser
->name
);
417 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser
);
422 static int __init
asymmetric_key_init(void)
424 return register_key_type(&key_type_asymmetric
);
427 static void __exit
asymmetric_key_cleanup(void)
429 unregister_key_type(&key_type_asymmetric
);
432 module_init(asymmetric_key_init
);
433 module_exit(asymmetric_key_cleanup
);