KEYS: handle error code encoded in pointer
[firefly-linux-kernel-4.4.55.git] / crypto / asymmetric_keys / asymmetric_type.c
1 /* 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>
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"
20
21 MODULE_LICENSE("GPL");
22
23 static LIST_HEAD(asymmetric_key_parsers);
24 static DECLARE_RWSEM(asymmetric_key_parsers_sem);
25
26 /**
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  */
35 struct 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 }
51 EXPORT_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  */
57 bool 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 }
66 EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
67
68 /**
69  * asymmetric_match_key_ids - Search asymmetric key IDs
70  * @kids: The list of key IDs to check
71  * @match_id: The key ID we're looking for
72  */
73 bool asymmetric_match_key_ids(const struct asymmetric_key_ids *kids,
74                               const struct asymmetric_key_id *match_id)
75 {
76         if (!kids || !match_id)
77                 return false;
78         if (asymmetric_key_id_same(kids->id[0], match_id))
79                 return true;
80         if (asymmetric_key_id_same(kids->id[1], match_id))
81                 return true;
82         return false;
83 }
84 EXPORT_SYMBOL_GPL(asymmetric_match_key_ids);
85
86 /**
87  * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
88  * @id: The ID as a hex string.
89  */
90 struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
91 {
92         struct asymmetric_key_id *match_id;
93         size_t hexlen;
94         int ret;
95
96         if (!*id)
97                 return ERR_PTR(-EINVAL);
98         hexlen = strlen(id);
99         if (hexlen & 1)
100                 return ERR_PTR(-EINVAL);
101
102         match_id = kmalloc(sizeof(struct asymmetric_key_id) + hexlen / 2,
103                            GFP_KERNEL);
104         if (!match_id)
105                 return ERR_PTR(-ENOMEM);
106         match_id->len = hexlen / 2;
107         ret = hex2bin(match_id->data, id, hexlen / 2);
108         if (ret < 0) {
109                 kfree(match_id);
110                 return ERR_PTR(-EINVAL);
111         }
112         return match_id;
113 }
114
115 /*
116  * Match asymmetric keys by ID.
117  */
118 static bool asymmetric_key_cmp(const struct key *key,
119                                const struct key_match_data *match_data)
120 {
121         const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
122         const struct asymmetric_key_id *match_id = match_data->preparsed;
123
124         return asymmetric_match_key_ids(kids, match_id);
125 }
126
127 /*
128  * Preparse the match criterion.  If we don't set lookup_type and cmp,
129  * the default will be an exact match on the key description.
130  *
131  * There are some specifiers for matching key IDs rather than by the key
132  * description:
133  *
134  *      "id:<id>" - request a key by any available ID
135  *
136  * These have to be searched by iteration rather than by direct lookup because
137  * the key is hashed according to its description.
138  */
139 static int asymmetric_key_match_preparse(struct key_match_data *match_data)
140 {
141         struct asymmetric_key_id *match_id;
142         const char *spec = match_data->raw_data;
143         const char *id;
144
145         if (!spec || !*spec)
146                 return -EINVAL;
147         if (spec[0] == 'i' &&
148             spec[1] == 'd' &&
149             spec[2] == ':') {
150                 id = spec + 3;
151         } else {
152                 goto default_match;
153         }
154
155         match_id = asymmetric_key_hex_to_key_id(id);
156         if (IS_ERR(match_id))
157                 return PTR_ERR(match_id);
158
159         match_data->preparsed = match_id;
160         match_data->cmp = asymmetric_key_cmp;
161         match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
162         return 0;
163
164 default_match:
165         return 0;
166 }
167
168 /*
169  * Free the preparsed the match criterion.
170  */
171 static void asymmetric_key_match_free(struct key_match_data *match_data)
172 {
173         kfree(match_data->preparsed);
174 }
175
176 /*
177  * Describe the asymmetric key
178  */
179 static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
180 {
181         const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
182         const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
183         const struct asymmetric_key_id *kid;
184         const unsigned char *p;
185         int n;
186
187         seq_puts(m, key->description);
188
189         if (subtype) {
190                 seq_puts(m, ": ");
191                 subtype->describe(key, m);
192
193                 if (kids && kids->id[0]) {
194                         kid = kids->id[0];
195                         seq_putc(m, ' ');
196                         n = kid->len;
197                         p = kid->data;
198                         if (n > 8) {
199                                 p += n - 8;
200                                 n = 8;
201                         }
202                         seq_printf(m, "%*phN", n, p);
203                 }
204
205                 seq_puts(m, " [");
206                 /* put something here to indicate the key's capabilities */
207                 seq_putc(m, ']');
208         }
209 }
210
211 /*
212  * Preparse a asymmetric payload to get format the contents appropriately for the
213  * internal payload to cut down on the number of scans of the data performed.
214  *
215  * We also generate a proposed description from the contents of the key that
216  * can be used to name the key if the user doesn't want to provide one.
217  */
218 static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
219 {
220         struct asymmetric_key_parser *parser;
221         int ret;
222
223         pr_devel("==>%s()\n", __func__);
224
225         if (prep->datalen == 0)
226                 return -EINVAL;
227
228         down_read(&asymmetric_key_parsers_sem);
229
230         ret = -EBADMSG;
231         list_for_each_entry(parser, &asymmetric_key_parsers, link) {
232                 pr_debug("Trying parser '%s'\n", parser->name);
233
234                 ret = parser->parse(prep);
235                 if (ret != -EBADMSG) {
236                         pr_debug("Parser recognised the format (ret %d)\n",
237                                  ret);
238                         break;
239                 }
240         }
241
242         up_read(&asymmetric_key_parsers_sem);
243         pr_devel("<==%s() = %d\n", __func__, ret);
244         return ret;
245 }
246
247 /*
248  * Clean up the preparse data
249  */
250 static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
251 {
252         struct asymmetric_key_subtype *subtype = prep->type_data[0];
253         struct asymmetric_key_ids *kids = prep->type_data[1];
254
255         pr_devel("==>%s()\n", __func__);
256
257         if (subtype) {
258                 subtype->destroy(prep->payload[0]);
259                 module_put(subtype->owner);
260         }
261         if (kids) {
262                 kfree(kids->id[0]);
263                 kfree(kids->id[1]);
264                 kfree(kids);
265         }
266         kfree(prep->description);
267 }
268
269 /*
270  * dispose of the data dangling from the corpse of a asymmetric key
271  */
272 static void asymmetric_key_destroy(struct key *key)
273 {
274         struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
275         struct asymmetric_key_ids *kids = key->type_data.p[1];
276
277         if (subtype) {
278                 subtype->destroy(key->payload.data);
279                 module_put(subtype->owner);
280                 key->type_data.p[0] = NULL;
281         }
282
283         if (kids) {
284                 kfree(kids->id[0]);
285                 kfree(kids->id[1]);
286                 kfree(kids);
287                 key->type_data.p[1] = NULL;
288         }
289 }
290
291 struct key_type key_type_asymmetric = {
292         .name           = "asymmetric",
293         .preparse       = asymmetric_key_preparse,
294         .free_preparse  = asymmetric_key_free_preparse,
295         .instantiate    = generic_key_instantiate,
296         .match_preparse = asymmetric_key_match_preparse,
297         .match_free     = asymmetric_key_match_free,
298         .destroy        = asymmetric_key_destroy,
299         .describe       = asymmetric_key_describe,
300 };
301 EXPORT_SYMBOL_GPL(key_type_asymmetric);
302
303 /**
304  * register_asymmetric_key_parser - Register a asymmetric key blob parser
305  * @parser: The parser to register
306  */
307 int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
308 {
309         struct asymmetric_key_parser *cursor;
310         int ret;
311
312         down_write(&asymmetric_key_parsers_sem);
313
314         list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
315                 if (strcmp(cursor->name, parser->name) == 0) {
316                         pr_err("Asymmetric key parser '%s' already registered\n",
317                                parser->name);
318                         ret = -EEXIST;
319                         goto out;
320                 }
321         }
322
323         list_add_tail(&parser->link, &asymmetric_key_parsers);
324
325         pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
326         ret = 0;
327
328 out:
329         up_write(&asymmetric_key_parsers_sem);
330         return ret;
331 }
332 EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
333
334 /**
335  * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
336  * @parser: The parser to unregister
337  */
338 void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
339 {
340         down_write(&asymmetric_key_parsers_sem);
341         list_del(&parser->link);
342         up_write(&asymmetric_key_parsers_sem);
343
344         pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
345 }
346 EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
347
348 /*
349  * Module stuff
350  */
351 static int __init asymmetric_key_init(void)
352 {
353         return register_key_type(&key_type_asymmetric);
354 }
355
356 static void __exit asymmetric_key_cleanup(void)
357 {
358         unregister_key_type(&key_type_asymmetric);
359 }
360
361 module_init(asymmetric_key_init);
362 module_exit(asymmetric_key_cleanup);