blob: b0e4ed23d6683c54b47d633b1f6e4f01f1de8025 [file] [log] [blame]
David Howells964f3b32012-09-13 15:17:21 +01001/* Asymmetric public-key cryptography key type
2 *
3 * See Documentation/security/asymmetric-keys.txt
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
7 *
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.
12 */
13#include <keys/asymmetric-subtype.h>
David Howells46c6f172012-09-13 15:17:32 +010014#include <keys/asymmetric-parser.h>
David Howells964f3b32012-09-13 15:17:21 +010015#include <linux/seq_file.h>
16#include <linux/module.h>
17#include <linux/slab.h>
David Howells7901c1a2014-09-16 17:36:11 +010018#include <linux/ctype.h>
David Howells964f3b32012-09-13 15:17:21 +010019#include "asymmetric_keys.h"
20
21MODULE_LICENSE("GPL");
22
David Howells46c6f172012-09-13 15:17:32 +010023static LIST_HEAD(asymmetric_key_parsers);
24static DECLARE_RWSEM(asymmetric_key_parsers_sem);
25
David Howells7901c1a2014-09-16 17:36:11 +010026/**
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
32 *
33 * Construct an asymmetric key ID from a pair of binary blobs.
34 */
35struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
36 size_t len_1,
37 const void *val_2,
38 size_t len_2)
39{
40 struct asymmetric_key_id *kid;
41
42 kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
43 GFP_KERNEL);
44 if (!kid)
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);
49 return kid;
50}
51EXPORT_SYMBOL_GPL(asymmetric_key_generate_id);
52
53/**
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
56 */
57bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
58 const struct asymmetric_key_id *kid2)
59{
60 if (!kid1 || !kid2)
61 return false;
62 if (kid1->len != kid2->len)
63 return false;
64 return memcmp(kid1->data, kid2->data, kid1->len) == 0;
65}
66EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
67
68/**
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010069 * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
70 * partially match
71 * @kid_1, @kid_2: The key IDs to compare
72 */
73bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
74 const struct asymmetric_key_id *kid2)
75{
76 if (!kid1 || !kid2)
77 return false;
78 if (kid1->len < kid2->len)
79 return false;
80 return memcmp(kid1->data + (kid1->len - kid2->len),
81 kid2->data, kid2->len) == 0;
82}
83EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
84
85/**
David Howells7901c1a2014-09-16 17:36:11 +010086 * 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
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010089 * @match: The match function to use
David Howells7901c1a2014-09-16 17:36:11 +010090 */
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010091static 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))
David Howells7901c1a2014-09-16 17:36:11 +010096{
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010097 int i;
98
David Howells7901c1a2014-09-16 17:36:11 +010099 if (!kids || !match_id)
100 return false;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100101 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
102 if (match(kids->id[i], match_id))
103 return true;
David Howells7901c1a2014-09-16 17:36:11 +0100104 return false;
105}
David Howells7901c1a2014-09-16 17:36:11 +0100106
Mimi Zoharf2b3dee2015-02-11 07:33:34 -0500107/* helper function can be called directly with pre-allocated memory */
108inline int __asymmetric_key_hex_to_key_id(const char *id,
109 struct asymmetric_key_id *match_id,
110 size_t hexlen)
111{
112 match_id->len = hexlen;
113 return hex2bin(match_id->data, id, hexlen);
114}
115
David Howells7901c1a2014-09-16 17:36:11 +0100116/**
117 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
118 * @id: The ID as a hex string.
119 */
120struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
121{
122 struct asymmetric_key_id *match_id;
Mimi Zoharf2b3dee2015-02-11 07:33:34 -0500123 size_t asciihexlen;
David Howellsd1ac5542014-09-22 00:02:01 +0100124 int ret;
David Howells7901c1a2014-09-16 17:36:11 +0100125
126 if (!*id)
127 return ERR_PTR(-EINVAL);
Mimi Zoharf2b3dee2015-02-11 07:33:34 -0500128 asciihexlen = strlen(id);
129 if (asciihexlen & 1)
David Howells7901c1a2014-09-16 17:36:11 +0100130 return ERR_PTR(-EINVAL);
131
Mimi Zoharf2b3dee2015-02-11 07:33:34 -0500132 match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
David Howells7901c1a2014-09-16 17:36:11 +0100133 GFP_KERNEL);
134 if (!match_id)
135 return ERR_PTR(-ENOMEM);
Mimi Zoharf2b3dee2015-02-11 07:33:34 -0500136 ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
David Howellsd1ac5542014-09-22 00:02:01 +0100137 if (ret < 0) {
138 kfree(match_id);
139 return ERR_PTR(-EINVAL);
140 }
David Howells7901c1a2014-09-16 17:36:11 +0100141 return match_id;
142}
143
David Howells964f3b32012-09-13 15:17:21 +0100144/*
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100145 * Match asymmetric keys by an exact match on an ID.
David Howells964f3b32012-09-13 15:17:21 +0100146 */
David Howells0c903ab2014-09-16 17:36:08 +0100147static bool asymmetric_key_cmp(const struct key *key,
148 const struct key_match_data *match_data)
David Howells964f3b32012-09-13 15:17:21 +0100149{
David Howells46963b72014-09-16 17:36:13 +0100150 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
151 const struct asymmetric_key_id *match_id = match_data->preparsed;
David Howells964f3b32012-09-13 15:17:21 +0100152
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100153 return asymmetric_match_key_ids(kids, match_id,
154 asymmetric_key_id_same);
155}
156
157/*
158 * Match asymmetric keys by a partial match on an IDs.
159 */
160static bool asymmetric_key_cmp_partial(const struct key *key,
161 const struct key_match_data *match_data)
162{
163 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
164 const struct asymmetric_key_id *match_id = match_data->preparsed;
165
166 return asymmetric_match_key_ids(kids, match_id,
167 asymmetric_key_id_partial);
David Howells964f3b32012-09-13 15:17:21 +0100168}
169
170/*
David Howells46291952014-09-16 17:36:02 +0100171 * 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.
173 *
174 * There are some specifiers for matching key IDs rather than by the key
175 * description:
176 *
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100177 * "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
David Howells46291952014-09-16 17:36:02 +0100179 *
180 * These have to be searched by iteration rather than by direct lookup because
181 * the key is hashed according to its description.
182 */
183static int asymmetric_key_match_preparse(struct key_match_data *match_data)
184{
David Howells46963b72014-09-16 17:36:13 +0100185 struct asymmetric_key_id *match_id;
186 const char *spec = match_data->raw_data;
187 const char *id;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100188 bool (*cmp)(const struct key *, const struct key_match_data *) =
189 asymmetric_key_cmp;
David Howells46963b72014-09-16 17:36:13 +0100190
191 if (!spec || !*spec)
192 return -EINVAL;
193 if (spec[0] == 'i' &&
194 spec[1] == 'd' &&
195 spec[2] == ':') {
196 id = spec + 3;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100197 cmp = asymmetric_key_cmp_partial;
198 } else if (spec[0] == 'e' &&
199 spec[1] == 'x' &&
200 spec[2] == ':') {
201 id = spec + 3;
David Howells46963b72014-09-16 17:36:13 +0100202 } else {
203 goto default_match;
204 }
205
206 match_id = asymmetric_key_hex_to_key_id(id);
Dmitry Kasatkin40b50e82014-10-03 11:53:28 +0300207 if (IS_ERR(match_id))
208 return PTR_ERR(match_id);
David Howells46963b72014-09-16 17:36:13 +0100209
210 match_data->preparsed = match_id;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100211 match_data->cmp = cmp;
David Howells46963b72014-09-16 17:36:13 +0100212 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
213 return 0;
214
215default_match:
David Howells46291952014-09-16 17:36:02 +0100216 return 0;
217}
218
219/*
220 * Free the preparsed the match criterion.
221 */
222static void asymmetric_key_match_free(struct key_match_data *match_data)
223{
David Howells46963b72014-09-16 17:36:13 +0100224 kfree(match_data->preparsed);
David Howells46291952014-09-16 17:36:02 +0100225}
226
227/*
David Howells964f3b32012-09-13 15:17:21 +0100228 * Describe the asymmetric key
229 */
230static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
231{
232 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
David Howells46963b72014-09-16 17:36:13 +0100233 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
234 const struct asymmetric_key_id *kid;
235 const unsigned char *p;
236 int n;
David Howells964f3b32012-09-13 15:17:21 +0100237
238 seq_puts(m, key->description);
239
240 if (subtype) {
241 seq_puts(m, ": ");
242 subtype->describe(key, m);
243
Dmitry Kasatkind4016582014-10-06 17:25:45 +0100244 if (kids && kids->id[1]) {
245 kid = kids->id[1];
David Howells964f3b32012-09-13 15:17:21 +0100246 seq_putc(m, ' ');
David Howells46963b72014-09-16 17:36:13 +0100247 n = kid->len;
248 p = kid->data;
Dmitry Kasatkind4016582014-10-06 17:25:45 +0100249 if (n > 4) {
250 p += n - 4;
251 n = 4;
David Howells46963b72014-09-16 17:36:13 +0100252 }
253 seq_printf(m, "%*phN", n, p);
David Howells964f3b32012-09-13 15:17:21 +0100254 }
255
256 seq_puts(m, " [");
257 /* put something here to indicate the key's capabilities */
258 seq_putc(m, ']');
259 }
260}
261
262/*
David Howells46c6f172012-09-13 15:17:32 +0100263 * 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.
265 *
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.
268 */
269static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
270{
271 struct asymmetric_key_parser *parser;
272 int ret;
273
274 pr_devel("==>%s()\n", __func__);
275
276 if (prep->datalen == 0)
277 return -EINVAL;
278
279 down_read(&asymmetric_key_parsers_sem);
280
281 ret = -EBADMSG;
282 list_for_each_entry(parser, &asymmetric_key_parsers, link) {
283 pr_debug("Trying parser '%s'\n", parser->name);
284
285 ret = parser->parse(prep);
286 if (ret != -EBADMSG) {
287 pr_debug("Parser recognised the format (ret %d)\n",
288 ret);
289 break;
290 }
291 }
292
293 up_read(&asymmetric_key_parsers_sem);
294 pr_devel("<==%s() = %d\n", __func__, ret);
295 return ret;
296}
297
298/*
299 * Clean up the preparse data
300 */
301static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
302{
303 struct asymmetric_key_subtype *subtype = prep->type_data[0];
David Howells46963b72014-09-16 17:36:13 +0100304 struct asymmetric_key_ids *kids = prep->type_data[1];
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100305 int i;
David Howells46c6f172012-09-13 15:17:32 +0100306
307 pr_devel("==>%s()\n", __func__);
308
309 if (subtype) {
David Howellsfc7c70e2014-07-18 18:56:34 +0100310 subtype->destroy(prep->payload[0]);
David Howells46c6f172012-09-13 15:17:32 +0100311 module_put(subtype->owner);
312 }
David Howells46963b72014-09-16 17:36:13 +0100313 if (kids) {
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100314 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
315 kfree(kids->id[i]);
David Howells46963b72014-09-16 17:36:13 +0100316 kfree(kids);
317 }
David Howells46c6f172012-09-13 15:17:32 +0100318 kfree(prep->description);
319}
320
321/*
David Howells964f3b32012-09-13 15:17:21 +0100322 * dispose of the data dangling from the corpse of a asymmetric key
323 */
324static void asymmetric_key_destroy(struct key *key)
325{
326 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
David Howells46963b72014-09-16 17:36:13 +0100327 struct asymmetric_key_ids *kids = key->type_data.p[1];
328
David Howells964f3b32012-09-13 15:17:21 +0100329 if (subtype) {
330 subtype->destroy(key->payload.data);
331 module_put(subtype->owner);
332 key->type_data.p[0] = NULL;
333 }
David Howells46963b72014-09-16 17:36:13 +0100334
335 if (kids) {
336 kfree(kids->id[0]);
337 kfree(kids->id[1]);
338 kfree(kids);
339 key->type_data.p[1] = NULL;
340 }
David Howells964f3b32012-09-13 15:17:21 +0100341}
342
343struct key_type key_type_asymmetric = {
344 .name = "asymmetric",
David Howells46c6f172012-09-13 15:17:32 +0100345 .preparse = asymmetric_key_preparse,
346 .free_preparse = asymmetric_key_free_preparse,
David Howells6a09d172014-07-18 18:56:34 +0100347 .instantiate = generic_key_instantiate,
David Howells46291952014-09-16 17:36:02 +0100348 .match_preparse = asymmetric_key_match_preparse,
David Howells46291952014-09-16 17:36:02 +0100349 .match_free = asymmetric_key_match_free,
David Howells964f3b32012-09-13 15:17:21 +0100350 .destroy = asymmetric_key_destroy,
351 .describe = asymmetric_key_describe,
352};
353EXPORT_SYMBOL_GPL(key_type_asymmetric);
354
David Howells46c6f172012-09-13 15:17:32 +0100355/**
356 * register_asymmetric_key_parser - Register a asymmetric key blob parser
357 * @parser: The parser to register
358 */
359int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
360{
361 struct asymmetric_key_parser *cursor;
362 int ret;
363
364 down_write(&asymmetric_key_parsers_sem);
365
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",
369 parser->name);
370 ret = -EEXIST;
371 goto out;
372 }
373 }
374
375 list_add_tail(&parser->link, &asymmetric_key_parsers);
376
377 pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
378 ret = 0;
379
380out:
381 up_write(&asymmetric_key_parsers_sem);
382 return ret;
383}
384EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
385
386/**
387 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
388 * @parser: The parser to unregister
389 */
390void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
391{
392 down_write(&asymmetric_key_parsers_sem);
393 list_del(&parser->link);
394 up_write(&asymmetric_key_parsers_sem);
395
396 pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
397}
398EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
399
David Howells964f3b32012-09-13 15:17:21 +0100400/*
401 * Module stuff
402 */
403static int __init asymmetric_key_init(void)
404{
405 return register_key_type(&key_type_asymmetric);
406}
407
408static void __exit asymmetric_key_cleanup(void)
409{
410 unregister_key_type(&key_type_asymmetric);
411}
412
413module_init(asymmetric_key_init);
414module_exit(asymmetric_key_cleanup);