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 <linux/seq_file.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/ctype.h>
19 #include "asymmetric_keys.h"
21 MODULE_LICENSE("GPL");
23 static LIST_HEAD(asymmetric_key_parsers);
24 static DECLARE_RWSEM(asymmetric_key_parsers_sem);
27 * asymmetric_key_generate_id: Construct an asymmetric key ID
28 * @val_1: First binary blob
29 * @len_1: Length of first binary blob
30 * @val_2: Second binary blob
31 * @len_2: Length of second binary blob
33 * Construct an asymmetric key ID from a pair of binary blobs.
35 struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
40 struct asymmetric_key_id *kid;
42 kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
45 return ERR_PTR(-ENOMEM);
46 kid->len = len_1 + len_2;
47 memcpy(kid->data, val_1, len_1);
48 memcpy(kid->data + len_1, val_2, len_2);
51 EXPORT_SYMBOL_GPL(asymmetric_key_generate_id);
54 * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
55 * @kid_1, @kid_2: The key IDs to compare
57 bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
58 const struct asymmetric_key_id *kid2)
62 if (kid1->len != kid2->len)
64 return memcmp(kid1->data, kid2->data, kid1->len) == 0;
66 EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
69 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
71 * @kid_1, @kid_2: The key IDs to compare
73 bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
74 const struct asymmetric_key_id *kid2)
78 if (kid1->len < kid2->len)
80 return memcmp(kid1->data + (kid1->len - kid2->len),
81 kid2->data, kid2->len) == 0;
83 EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
86 * asymmetric_match_key_ids - Search asymmetric key IDs
87 * @kids: The list of key IDs to check
88 * @match_id: The key ID we're looking for
89 * @match: The match function to use
91 static bool asymmetric_match_key_ids(
92 const struct asymmetric_key_ids *kids,
93 const struct asymmetric_key_id *match_id,
94 bool (*match)(const struct asymmetric_key_id *kid1,
95 const struct asymmetric_key_id *kid2))
99 if (!kids || !match_id)
101 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
102 if (match(kids->id[i], match_id))
107 /* helper function can be called directly with pre-allocated memory */
108 inline int __asymmetric_key_hex_to_key_id(const char *id,
109 struct asymmetric_key_id *match_id,
112 match_id->len = hexlen;
113 return hex2bin(match_id->data, id, hexlen);
117 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
118 * @id: The ID as a hex string.
120 struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
122 struct asymmetric_key_id *match_id;
127 return ERR_PTR(-EINVAL);
128 asciihexlen = strlen(id);
130 return ERR_PTR(-EINVAL);
132 match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
135 return ERR_PTR(-ENOMEM);
136 ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
139 return ERR_PTR(-EINVAL);
145 * Match asymmetric keys by an exact match on an ID.
147 static bool asymmetric_key_cmp(const struct key *key,
148 const struct key_match_data *match_data)
150 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
151 const struct asymmetric_key_id *match_id = match_data->preparsed;
153 return asymmetric_match_key_ids(kids, match_id,
154 asymmetric_key_id_same);
158 * Match asymmetric keys by a partial match on an IDs.
160 static bool asymmetric_key_cmp_partial(const struct key *key,
161 const struct key_match_data *match_data)
163 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
164 const struct asymmetric_key_id *match_id = match_data->preparsed;
166 return asymmetric_match_key_ids(kids, match_id,
167 asymmetric_key_id_partial);
171 * Preparse the match criterion. If we don't set lookup_type and cmp,
172 * the default will be an exact match on the key description.
174 * There are some specifiers for matching key IDs rather than by the key
177 * "id:<id>" - find a key by partial match on any available ID
178 * "ex:<id>" - find a key by exact match on any available ID
180 * These have to be searched by iteration rather than by direct lookup because
181 * the key is hashed according to its description.
183 static int asymmetric_key_match_preparse(struct key_match_data *match_data)
185 struct asymmetric_key_id *match_id;
186 const char *spec = match_data->raw_data;
188 bool (*cmp)(const struct key *, const struct key_match_data *) =
193 if (spec[0] == 'i' &&
197 cmp = asymmetric_key_cmp_partial;
198 } else if (spec[0] == 'e' &&
206 match_id = asymmetric_key_hex_to_key_id(id);
207 if (IS_ERR(match_id))
208 return PTR_ERR(match_id);
210 match_data->preparsed = match_id;
211 match_data->cmp = cmp;
212 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
220 * Free the preparsed the match criterion.
222 static void asymmetric_key_match_free(struct key_match_data *match_data)
224 kfree(match_data->preparsed);
228 * Describe the asymmetric key
230 static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
232 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
233 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
234 const struct asymmetric_key_id *kid;
235 const unsigned char *p;
238 seq_puts(m, key->description);
242 subtype->describe(key, m);
244 if (kids && kids->id[1]) {
253 seq_printf(m, "%*phN", n, p);
257 /* put something here to indicate the key's capabilities */
263 * Preparse a asymmetric payload to get format the contents appropriately for the
264 * internal payload to cut down on the number of scans of the data performed.
266 * We also generate a proposed description from the contents of the key that
267 * can be used to name the key if the user doesn't want to provide one.
269 static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
271 struct asymmetric_key_parser *parser;
274 pr_devel("==>%s()\n", __func__);
276 if (prep->datalen == 0)
279 down_read(&asymmetric_key_parsers_sem);
282 list_for_each_entry(parser, &asymmetric_key_parsers, link) {
283 pr_debug("Trying parser '%s'\n", parser->name);
285 ret = parser->parse(prep);
286 if (ret != -EBADMSG) {
287 pr_debug("Parser recognised the format (ret %d)\n",
293 up_read(&asymmetric_key_parsers_sem);
294 pr_devel("<==%s() = %d\n", __func__, ret);
299 * Clean up the preparse data
301 static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
303 struct asymmetric_key_subtype *subtype = prep->type_data[0];
304 struct asymmetric_key_ids *kids = prep->type_data[1];
307 pr_devel("==>%s()\n", __func__);
310 subtype->destroy(prep->payload[0]);
311 module_put(subtype->owner);
314 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
318 kfree(prep->description);
322 * dispose of the data dangling from the corpse of a asymmetric key
324 static void asymmetric_key_destroy(struct key *key)
326 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
327 struct asymmetric_key_ids *kids = key->type_data.p[1];
330 subtype->destroy(key->payload.data);
331 module_put(subtype->owner);
332 key->type_data.p[0] = NULL;
339 key->type_data.p[1] = NULL;
343 struct key_type key_type_asymmetric = {
344 .name = "asymmetric",
345 .preparse = asymmetric_key_preparse,
346 .free_preparse = asymmetric_key_free_preparse,
347 .instantiate = generic_key_instantiate,
348 .match_preparse = asymmetric_key_match_preparse,
349 .match_free = asymmetric_key_match_free,
350 .destroy = asymmetric_key_destroy,
351 .describe = asymmetric_key_describe,
353 EXPORT_SYMBOL_GPL(key_type_asymmetric);
356 * register_asymmetric_key_parser - Register a asymmetric key blob parser
357 * @parser: The parser to register
359 int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
361 struct asymmetric_key_parser *cursor;
364 down_write(&asymmetric_key_parsers_sem);
366 list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
367 if (strcmp(cursor->name, parser->name) == 0) {
368 pr_err("Asymmetric key parser '%s' already registered\n",
375 list_add_tail(&parser->link, &asymmetric_key_parsers);
377 pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
381 up_write(&asymmetric_key_parsers_sem);
384 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
387 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
388 * @parser: The parser to unregister
390 void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
392 down_write(&asymmetric_key_parsers_sem);
393 list_del(&parser->link);
394 up_write(&asymmetric_key_parsers_sem);
396 pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
398 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
403 static int __init asymmetric_key_init(void)
405 return register_key_type(&key_type_asymmetric);
408 static void __exit asymmetric_key_cleanup(void)
410 unregister_key_type(&key_type_asymmetric);
413 module_init(asymmetric_key_init);
414 module_exit(asymmetric_key_cleanup);