1
2
3
4
5
6
7
8
9
10
11
12
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"
21
22MODULE_LICENSE("GPL");
23
24const 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",
31};
32EXPORT_SYMBOL_GPL(key_being_used_for);
33
34static LIST_HEAD(asymmetric_key_parsers);
35static DECLARE_RWSEM(asymmetric_key_parsers_sem);
36
37
38
39
40
41
42
43
44
45
46struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
47 size_t len_1,
48 const void *val_2,
49 size_t len_2)
50{
51 struct asymmetric_key_id *kid;
52
53 kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
54 GFP_KERNEL);
55 if (!kid)
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);
60 return kid;
61}
62EXPORT_SYMBOL_GPL(asymmetric_key_generate_id);
63
64
65
66
67
68bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
69 const struct asymmetric_key_id *kid2)
70{
71 if (!kid1 || !kid2)
72 return false;
73 if (kid1->len != kid2->len)
74 return false;
75 return memcmp(kid1->data, kid2->data, kid1->len) == 0;
76}
77EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
78
79
80
81
82
83
84bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
85 const struct asymmetric_key_id *kid2)
86{
87 if (!kid1 || !kid2)
88 return false;
89 if (kid1->len < kid2->len)
90 return false;
91 return memcmp(kid1->data + (kid1->len - kid2->len),
92 kid2->data, kid2->len) == 0;
93}
94EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
95
96
97
98
99
100
101
102static 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))
107{
108 int i;
109
110 if (!kids || !match_id)
111 return false;
112 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
113 if (match(kids->id[i], match_id))
114 return true;
115 return false;
116}
117
118
119inline int __asymmetric_key_hex_to_key_id(const char *id,
120 struct asymmetric_key_id *match_id,
121 size_t hexlen)
122{
123 match_id->len = hexlen;
124 return hex2bin(match_id->data, id, hexlen);
125}
126
127
128
129
130
131struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
132{
133 struct asymmetric_key_id *match_id;
134 size_t asciihexlen;
135 int ret;
136
137 if (!*id)
138 return ERR_PTR(-EINVAL);
139 asciihexlen = strlen(id);
140 if (asciihexlen & 1)
141 return ERR_PTR(-EINVAL);
142
143 match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
144 GFP_KERNEL);
145 if (!match_id)
146 return ERR_PTR(-ENOMEM);
147 ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
148 if (ret < 0) {
149 kfree(match_id);
150 return ERR_PTR(-EINVAL);
151 }
152 return match_id;
153}
154
155
156
157
158static bool asymmetric_key_cmp(const struct key *key,
159 const struct key_match_data *match_data)
160{
161 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
162 const struct asymmetric_key_id *match_id = match_data->preparsed;
163
164 return asymmetric_match_key_ids(kids, match_id,
165 asymmetric_key_id_same);
166}
167
168
169
170
171static bool asymmetric_key_cmp_partial(const struct key *key,
172 const struct key_match_data *match_data)
173{
174 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
175 const struct asymmetric_key_id *match_id = match_data->preparsed;
176
177 return asymmetric_match_key_ids(kids, match_id,
178 asymmetric_key_id_partial);
179}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194static int asymmetric_key_match_preparse(struct key_match_data *match_data)
195{
196 struct asymmetric_key_id *match_id;
197 const char *spec = match_data->raw_data;
198 const char *id;
199 bool (*cmp)(const struct key *, const struct key_match_data *) =
200 asymmetric_key_cmp;
201
202 if (!spec || !*spec)
203 return -EINVAL;
204 if (spec[0] == 'i' &&
205 spec[1] == 'd' &&
206 spec[2] == ':') {
207 id = spec + 3;
208 cmp = asymmetric_key_cmp_partial;
209 } else if (spec[0] == 'e' &&
210 spec[1] == 'x' &&
211 spec[2] == ':') {
212 id = spec + 3;
213 } else {
214 goto default_match;
215 }
216
217 match_id = asymmetric_key_hex_to_key_id(id);
218 if (IS_ERR(match_id))
219 return PTR_ERR(match_id);
220
221 match_data->preparsed = match_id;
222 match_data->cmp = cmp;
223 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
224 return 0;
225
226default_match:
227 return 0;
228}
229
230
231
232
233static void asymmetric_key_match_free(struct key_match_data *match_data)
234{
235 kfree(match_data->preparsed);
236}
237
238
239
240
241static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
242{
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;
247 int n;
248
249 seq_puts(m, key->description);
250
251 if (subtype) {
252 seq_puts(m, ": ");
253 subtype->describe(key, m);
254
255 if (kids && kids->id[1]) {
256 kid = kids->id[1];
257 seq_putc(m, ' ');
258 n = kid->len;
259 p = kid->data;
260 if (n > 4) {
261 p += n - 4;
262 n = 4;
263 }
264 seq_printf(m, "%*phN", n, p);
265 }
266
267 seq_puts(m, " [");
268
269 seq_putc(m, ']');
270 }
271}
272
273
274
275
276
277
278
279
280static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
281{
282 struct asymmetric_key_parser *parser;
283 int ret;
284
285 pr_devel("==>%s()\n", __func__);
286
287 if (prep->datalen == 0)
288 return -EINVAL;
289
290 down_read(&asymmetric_key_parsers_sem);
291
292 ret = -EBADMSG;
293 list_for_each_entry(parser, &asymmetric_key_parsers, link) {
294 pr_debug("Trying parser '%s'\n", parser->name);
295
296 ret = parser->parse(prep);
297 if (ret != -EBADMSG) {
298 pr_debug("Parser recognised the format (ret %d)\n",
299 ret);
300 break;
301 }
302 }
303
304 up_read(&asymmetric_key_parsers_sem);
305 pr_devel("<==%s() = %d\n", __func__, ret);
306 return ret;
307}
308
309
310
311
312static void asymmetric_key_free_kids(struct asymmetric_key_ids *kids)
313{
314 int i;
315
316 if (kids) {
317 for (i = 0; i < ARRAY_SIZE(kids->id); i++)
318 kfree(kids->id[i]);
319 kfree(kids);
320 }
321}
322
323
324
325
326static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
327{
328 struct asymmetric_key_subtype *subtype = prep->payload.data[asym_subtype];
329 struct asymmetric_key_ids *kids = prep->payload.data[asym_key_ids];
330
331 pr_devel("==>%s()\n", __func__);
332
333 if (subtype) {
334 subtype->destroy(prep->payload.data[asym_crypto]);
335 module_put(subtype->owner);
336 }
337 asymmetric_key_free_kids(kids);
338 kfree(prep->description);
339}
340
341
342
343
344static void asymmetric_key_destroy(struct key *key)
345{
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];
349
350 key->payload.data[asym_crypto] = NULL;
351 key->payload.data[asym_subtype] = NULL;
352 key->payload.data[asym_key_ids] = NULL;
353
354 if (subtype) {
355 subtype->destroy(data);
356 module_put(subtype->owner);
357 }
358
359 asymmetric_key_free_kids(kids);
360}
361
362struct 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,
371};
372EXPORT_SYMBOL_GPL(key_type_asymmetric);
373
374
375
376
377
378int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
379{
380 struct asymmetric_key_parser *cursor;
381 int ret;
382
383 down_write(&asymmetric_key_parsers_sem);
384
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",
388 parser->name);
389 ret = -EEXIST;
390 goto out;
391 }
392 }
393
394 list_add_tail(&parser->link, &asymmetric_key_parsers);
395
396 pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
397 ret = 0;
398
399out:
400 up_write(&asymmetric_key_parsers_sem);
401 return ret;
402}
403EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
404
405
406
407
408
409void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
410{
411 down_write(&asymmetric_key_parsers_sem);
412 list_del(&parser->link);
413 up_write(&asymmetric_key_parsers_sem);
414
415 pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
416}
417EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
418
419
420
421
422static int __init asymmetric_key_init(void)
423{
424 return register_key_type(&key_type_asymmetric);
425}
426
427static void __exit asymmetric_key_cleanup(void)
428{
429 unregister_key_type(&key_type_asymmetric);
430}
431
432module_init(asymmetric_key_init);
433module_exit(asymmetric_key_cleanup);
434