blob: bcbbbd794e1da209d519baca7de8b0ddf1115716 [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
107/**
108 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
109 * @id: The ID as a hex string.
110 */
111struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
112{
113 struct asymmetric_key_id *match_id;
David Howellsd1ac5542014-09-22 00:02:01 +0100114 size_t hexlen;
115 int ret;
David Howells7901c1a2014-09-16 17:36:11 +0100116
117 if (!*id)
118 return ERR_PTR(-EINVAL);
David Howellsd1ac5542014-09-22 00:02:01 +0100119 hexlen = strlen(id);
David Howells7901c1a2014-09-16 17:36:11 +0100120 if (hexlen & 1)
121 return ERR_PTR(-EINVAL);
122
123 match_id = kmalloc(sizeof(struct asymmetric_key_id) + hexlen / 2,
124 GFP_KERNEL);
125 if (!match_id)
126 return ERR_PTR(-ENOMEM);
127 match_id->len = hexlen / 2;
David Howellsd1ac5542014-09-22 00:02:01 +0100128 ret = hex2bin(match_id->data, id, hexlen / 2);
129 if (ret < 0) {
130 kfree(match_id);
131 return ERR_PTR(-EINVAL);
132 }
David Howells7901c1a2014-09-16 17:36:11 +0100133 return match_id;
134}
135
David Howells964f3b32012-09-13 15:17:21 +0100136/*
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100137 * Match asymmetric keys by an exact match on an ID.
David Howells964f3b32012-09-13 15:17:21 +0100138 */
David Howells0c903ab2014-09-16 17:36:08 +0100139static bool asymmetric_key_cmp(const struct key *key,
140 const struct key_match_data *match_data)
David Howells964f3b32012-09-13 15:17:21 +0100141{
David Howells46963b72014-09-16 17:36:13 +0100142 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
143 const struct asymmetric_key_id *match_id = match_data->preparsed;
David Howells964f3b32012-09-13 15:17:21 +0100144
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100145 return asymmetric_match_key_ids(kids, match_id,
146 asymmetric_key_id_same);
147}
148
149/*
150 * Match asymmetric keys by a partial match on an IDs.
151 */
152static bool asymmetric_key_cmp_partial(const struct key *key,
153 const struct key_match_data *match_data)
154{
155 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
156 const struct asymmetric_key_id *match_id = match_data->preparsed;
157
158 return asymmetric_match_key_ids(kids, match_id,
159 asymmetric_key_id_partial);
David Howells964f3b32012-09-13 15:17:21 +0100160}
161
162/*
David Howells46291952014-09-16 17:36:02 +0100163 * Preparse the match criterion. If we don't set lookup_type and cmp,
164 * the default will be an exact match on the key description.
165 *
166 * There are some specifiers for matching key IDs rather than by the key
167 * description:
168 *
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100169 * "id:<id>" - find a key by partial match on any available ID
170 * "ex:<id>" - find a key by exact match on any available ID
David Howells46291952014-09-16 17:36:02 +0100171 *
172 * These have to be searched by iteration rather than by direct lookup because
173 * the key is hashed according to its description.
174 */
175static int asymmetric_key_match_preparse(struct key_match_data *match_data)
176{
David Howells46963b72014-09-16 17:36:13 +0100177 struct asymmetric_key_id *match_id;
178 const char *spec = match_data->raw_data;
179 const char *id;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100180 bool (*cmp)(const struct key *, const struct key_match_data *) =
181 asymmetric_key_cmp;
David Howells46963b72014-09-16 17:36:13 +0100182
183 if (!spec || !*spec)
184 return -EINVAL;
185 if (spec[0] == 'i' &&
186 spec[1] == 'd' &&
187 spec[2] == ':') {
188 id = spec + 3;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100189 cmp = asymmetric_key_cmp_partial;
190 } else if (spec[0] == 'e' &&
191 spec[1] == 'x' &&
192 spec[2] == ':') {
193 id = spec + 3;
David Howells46963b72014-09-16 17:36:13 +0100194 } else {
195 goto default_match;
196 }
197
198 match_id = asymmetric_key_hex_to_key_id(id);
Dmitry Kasatkin40b50e82014-10-03 11:53:28 +0300199 if (IS_ERR(match_id))
200 return PTR_ERR(match_id);
David Howells46963b72014-09-16 17:36:13 +0100201
202 match_data->preparsed = match_id;
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100203 match_data->cmp = cmp;
David Howells46963b72014-09-16 17:36:13 +0100204 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
205 return 0;
206
207default_match:
David Howells46291952014-09-16 17:36:02 +0100208 return 0;
209}
210
211/*
212 * Free the preparsed the match criterion.
213 */
214static void asymmetric_key_match_free(struct key_match_data *match_data)
215{
David Howells46963b72014-09-16 17:36:13 +0100216 kfree(match_data->preparsed);
David Howells46291952014-09-16 17:36:02 +0100217}
218
219/*
David Howells964f3b32012-09-13 15:17:21 +0100220 * Describe the asymmetric key
221 */
222static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
223{
224 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
David Howells46963b72014-09-16 17:36:13 +0100225 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
226 const struct asymmetric_key_id *kid;
227 const unsigned char *p;
228 int n;
David Howells964f3b32012-09-13 15:17:21 +0100229
230 seq_puts(m, key->description);
231
232 if (subtype) {
233 seq_puts(m, ": ");
234 subtype->describe(key, m);
235
Dmitry Kasatkind4016582014-10-06 17:25:45 +0100236 if (kids && kids->id[1]) {
237 kid = kids->id[1];
David Howells964f3b32012-09-13 15:17:21 +0100238 seq_putc(m, ' ');
David Howells46963b72014-09-16 17:36:13 +0100239 n = kid->len;
240 p = kid->data;
Dmitry Kasatkind4016582014-10-06 17:25:45 +0100241 if (n > 4) {
242 p += n - 4;
243 n = 4;
David Howells46963b72014-09-16 17:36:13 +0100244 }
245 seq_printf(m, "%*phN", n, p);
David Howells964f3b32012-09-13 15:17:21 +0100246 }
247
248 seq_puts(m, " [");
249 /* put something here to indicate the key's capabilities */
250 seq_putc(m, ']');
251 }
252}
253
254/*
David Howells46c6f172012-09-13 15:17:32 +0100255 * Preparse a asymmetric payload to get format the contents appropriately for the
256 * internal payload to cut down on the number of scans of the data performed.
257 *
258 * We also generate a proposed description from the contents of the key that
259 * can be used to name the key if the user doesn't want to provide one.
260 */
261static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
262{
263 struct asymmetric_key_parser *parser;
264 int ret;
265
266 pr_devel("==>%s()\n", __func__);
267
268 if (prep->datalen == 0)
269 return -EINVAL;
270
271 down_read(&asymmetric_key_parsers_sem);
272
273 ret = -EBADMSG;
274 list_for_each_entry(parser, &asymmetric_key_parsers, link) {
275 pr_debug("Trying parser '%s'\n", parser->name);
276
277 ret = parser->parse(prep);
278 if (ret != -EBADMSG) {
279 pr_debug("Parser recognised the format (ret %d)\n",
280 ret);
281 break;
282 }
283 }
284
285 up_read(&asymmetric_key_parsers_sem);
286 pr_devel("<==%s() = %d\n", __func__, ret);
287 return ret;
288}
289
290/*
291 * Clean up the preparse data
292 */
293static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
294{
295 struct asymmetric_key_subtype *subtype = prep->type_data[0];
David Howells46963b72014-09-16 17:36:13 +0100296 struct asymmetric_key_ids *kids = prep->type_data[1];
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100297 int i;
David Howells46c6f172012-09-13 15:17:32 +0100298
299 pr_devel("==>%s()\n", __func__);
300
301 if (subtype) {
David Howellsfc7c70e2014-07-18 18:56:34 +0100302 subtype->destroy(prep->payload[0]);
David Howells46c6f172012-09-13 15:17:32 +0100303 module_put(subtype->owner);
304 }
David Howells46963b72014-09-16 17:36:13 +0100305 if (kids) {
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100306 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
307 kfree(kids->id[i]);
David Howells46963b72014-09-16 17:36:13 +0100308 kfree(kids);
309 }
David Howells46c6f172012-09-13 15:17:32 +0100310 kfree(prep->description);
311}
312
313/*
David Howells964f3b32012-09-13 15:17:21 +0100314 * dispose of the data dangling from the corpse of a asymmetric key
315 */
316static void asymmetric_key_destroy(struct key *key)
317{
318 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
David Howells46963b72014-09-16 17:36:13 +0100319 struct asymmetric_key_ids *kids = key->type_data.p[1];
320
David Howells964f3b32012-09-13 15:17:21 +0100321 if (subtype) {
322 subtype->destroy(key->payload.data);
323 module_put(subtype->owner);
324 key->type_data.p[0] = NULL;
325 }
David Howells46963b72014-09-16 17:36:13 +0100326
327 if (kids) {
328 kfree(kids->id[0]);
329 kfree(kids->id[1]);
330 kfree(kids);
331 key->type_data.p[1] = NULL;
332 }
David Howells964f3b32012-09-13 15:17:21 +0100333}
334
335struct key_type key_type_asymmetric = {
336 .name = "asymmetric",
David Howells46c6f172012-09-13 15:17:32 +0100337 .preparse = asymmetric_key_preparse,
338 .free_preparse = asymmetric_key_free_preparse,
David Howells6a09d172014-07-18 18:56:34 +0100339 .instantiate = generic_key_instantiate,
David Howells46291952014-09-16 17:36:02 +0100340 .match_preparse = asymmetric_key_match_preparse,
David Howells46291952014-09-16 17:36:02 +0100341 .match_free = asymmetric_key_match_free,
David Howells964f3b32012-09-13 15:17:21 +0100342 .destroy = asymmetric_key_destroy,
343 .describe = asymmetric_key_describe,
344};
345EXPORT_SYMBOL_GPL(key_type_asymmetric);
346
David Howells46c6f172012-09-13 15:17:32 +0100347/**
348 * register_asymmetric_key_parser - Register a asymmetric key blob parser
349 * @parser: The parser to register
350 */
351int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
352{
353 struct asymmetric_key_parser *cursor;
354 int ret;
355
356 down_write(&asymmetric_key_parsers_sem);
357
358 list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
359 if (strcmp(cursor->name, parser->name) == 0) {
360 pr_err("Asymmetric key parser '%s' already registered\n",
361 parser->name);
362 ret = -EEXIST;
363 goto out;
364 }
365 }
366
367 list_add_tail(&parser->link, &asymmetric_key_parsers);
368
369 pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
370 ret = 0;
371
372out:
373 up_write(&asymmetric_key_parsers_sem);
374 return ret;
375}
376EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
377
378/**
379 * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
380 * @parser: The parser to unregister
381 */
382void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
383{
384 down_write(&asymmetric_key_parsers_sem);
385 list_del(&parser->link);
386 up_write(&asymmetric_key_parsers_sem);
387
388 pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
389}
390EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
391
David Howells964f3b32012-09-13 15:17:21 +0100392/*
393 * Module stuff
394 */
395static int __init asymmetric_key_init(void)
396{
397 return register_key_type(&key_type_asymmetric);
398}
399
400static void __exit asymmetric_key_cleanup(void)
401{
402 unregister_key_type(&key_type_asymmetric);
403}
404
405module_init(asymmetric_key_init);
406module_exit(asymmetric_key_cleanup);