6e63c5ab2d48b2645045d237235a6652832d3e17
[openssl.git] / crypto / evp / keymgmt_lib.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/core_names.h>
11 #include "internal/cryptlib.h"
12 #include "internal/nelem.h"
13 #include "crypto/evp.h"
14 #include "crypto/asn1.h"
15 #include "internal/core.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
18
19 /*
20  * match_type() checks if two EVP_KEYMGMT are matching key types.  This
21  * function assumes that the caller has made all the necessary NULL checks.
22  */
23 static int match_type(const EVP_KEYMGMT *keymgmt1, const EVP_KEYMGMT *keymgmt2)
24 {
25     const OSSL_PROVIDER *prov2 = EVP_KEYMGMT_provider(keymgmt2);
26     const char *name2 = evp_first_name(prov2, EVP_KEYMGMT_number(keymgmt2));
27
28     return EVP_KEYMGMT_is_a(keymgmt1, name2);
29 }
30
31 struct import_data_st {
32     EVP_KEYMGMT *keymgmt;
33     void *keydata;
34
35     int selection;
36 };
37
38 static int try_import(const OSSL_PARAM params[], void *arg)
39 {
40     struct import_data_st *data = arg;
41
42     /*
43      * It's fine if there was no data to transfer, we just end up with an
44      * empty destination key.
45      */
46     if (params[0].key == NULL)
47         return 1;
48
49     /* Just in time creation of keydata, if needed */
50     if (data->keydata == NULL
51         && (data->keydata = evp_keymgmt_newdata(data->keymgmt)) == NULL) {
52         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
53         return 0;
54     }
55
56     return evp_keymgmt_import(data->keymgmt, data->keydata, data->selection,
57                               params);
58 }
59
60 void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
61 {
62     struct import_data_st import_data;
63     size_t i = 0;
64
65     /* Export to where? */
66     if (keymgmt == NULL)
67         return NULL;
68
69     /* If we have an unassigned key, give up */
70     if (pk->keydata == NULL)
71         return NULL;
72
73     /* If |keymgmt| matches the "origin" |keymgmt|, no more to do */
74     if (pk->keymgmt == keymgmt)
75         return pk->keydata;
76
77     /* If this key is already exported to |keymgmt|, no more to do */
78     i = evp_keymgmt_util_find_operation_cache_index(pk, keymgmt);
79     if (i < OSSL_NELEM(pk->operation_cache)
80         && pk->operation_cache[i].keymgmt != NULL)
81         return pk->operation_cache[i].keydata;
82
83     /* If the "origin" |keymgmt| doesn't support exporting, give up */
84     /*
85      * TODO(3.0) consider an evp_keymgmt_export() return value that indicates
86      * that the method is unsupported.
87      */
88     if (pk->keymgmt->export == NULL)
89         return NULL;
90
91     /* Check that we have found an empty slot in the export cache */
92     /*
93      * TODO(3.0) Right now, we assume we have ample space.  We will have to
94      * think about a cache aging scheme, though, if |i| indexes outside the
95      * array.
96      */
97     if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
98         return NULL;
99
100     /*
101      * Make sure that the type of the keymgmt to export to matches the type
102      * of the "origin"
103      */
104     if (!ossl_assert(match_type(pk->keymgmt, keymgmt)))
105         return NULL;
106
107     /*
108      * We look at the already cached provider keys, and import from the
109      * first that supports it (i.e. use its export function), and export
110      * the imported data to the new provider.
111      */
112
113     /* Setup for the export callback */
114     import_data.keydata = NULL;  /* try_import will create it */
115     import_data.keymgmt = keymgmt;
116     import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
117
118     /*
119      * The export function calls the callback (try_import), which does the
120      * import for us.  If successful, we're done.
121      */
122     if (!evp_keymgmt_export(pk->keymgmt, pk->keydata, OSSL_KEYMGMT_SELECT_ALL,
123                             &try_import, &import_data)) {
124         /* If there was an error, bail out */
125         evp_keymgmt_freedata(keymgmt, import_data.keydata);
126         return NULL;
127     }
128
129     /* Add the new export to the operation cache */
130     if (!evp_keymgmt_util_cache_keydata(pk, i, keymgmt, import_data.keydata)) {
131         evp_keymgmt_freedata(keymgmt, import_data.keydata);
132         return NULL;
133     }
134
135     return import_data.keydata;
136 }
137
138 void evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk)
139 {
140     size_t i, end = OSSL_NELEM(pk->operation_cache);
141
142     if (pk != NULL) {
143         for (i = 0; i < end && pk->operation_cache[i].keymgmt != NULL; i++) {
144             EVP_KEYMGMT *keymgmt = pk->operation_cache[i].keymgmt;
145             void *keydata = pk->operation_cache[i].keydata;
146
147             pk->operation_cache[i].keymgmt = NULL;
148             pk->operation_cache[i].keydata = NULL;
149             evp_keymgmt_freedata(keymgmt, keydata);
150             EVP_KEYMGMT_free(keymgmt);
151         }
152     }
153 }
154
155 size_t evp_keymgmt_util_find_operation_cache_index(EVP_PKEY *pk,
156                                                    EVP_KEYMGMT *keymgmt)
157 {
158     size_t i, end = OSSL_NELEM(pk->operation_cache);
159
160     for (i = 0; i < end && pk->operation_cache[i].keymgmt != NULL; i++) {
161         if (keymgmt == pk->operation_cache[i].keymgmt)
162             break;
163     }
164
165     return i;
166 }
167
168 int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, size_t index,
169                                    EVP_KEYMGMT *keymgmt, void *keydata)
170 {
171     if (keydata != NULL) {
172         if (!EVP_KEYMGMT_up_ref(keymgmt))
173             return 0;
174         pk->operation_cache[index].keydata = keydata;
175         pk->operation_cache[index].keymgmt = keymgmt;
176     }
177     return 1;
178 }
179
180 void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk)
181 {
182     /*
183      * Cache information about the provider "origin" key.
184      *
185      * This services functions like EVP_PKEY_size, EVP_PKEY_bits, etc
186      */
187     if (pk->keydata != NULL) {
188         int bits = 0;
189         int security_bits = 0;
190         int size = 0;
191         OSSL_PARAM params[4];
192
193         params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_BITS, &bits);
194         params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_SECURITY_BITS,
195                                              &security_bits);
196         params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_MAX_SIZE, &size);
197         params[3] = OSSL_PARAM_construct_end();
198         if (evp_keymgmt_get_params(pk->keymgmt, pk->keydata, params)) {
199             pk->cache.size = size;
200             pk->cache.bits = bits;
201             pk->cache.security_bits = security_bits;
202         }
203     }
204 }
205
206 void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
207                                 int selection, const OSSL_PARAM params[])
208 {
209     void *keydata = evp_keymgmt_newdata(keymgmt);
210
211     if (keydata != NULL) {
212         if (!evp_keymgmt_import(keymgmt, keydata, selection, params)
213             || !EVP_KEYMGMT_up_ref(keymgmt)) {
214             evp_keymgmt_freedata(keymgmt, keydata);
215             return NULL;
216         }
217
218         evp_keymgmt_util_clear_operation_cache(target);
219         target->keymgmt = keymgmt;
220         target->keydata = keydata;
221         evp_keymgmt_util_cache_keyinfo(target);
222     }
223
224     return keydata;
225 }
226
227 int evp_keymgmt_util_has(EVP_PKEY *pk, int selection)
228 {
229     /* Check if key is even assigned */
230     if (pk->keymgmt == NULL)
231         return 0;
232
233     return evp_keymgmt_has(pk->keymgmt, pk->keydata, selection);
234 }
235
236 /*
237  * evp_keymgmt_util_match() doesn't just look at the provider side "origin",
238  * but also in the operation cache to see if there's any common keymgmt that
239  * supplies OP_keymgmt_match.
240  *
241  * evp_keymgmt_util_match() adheres to the return values that EVP_PKEY_cmp()
242  * and EVP_PKEY_cmp_parameters() return, i.e.:
243  *
244  *  1   same key
245  *  0   not same key
246  * -1   not same key type
247  * -2   unsupported operation
248  */
249 int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection)
250 {
251     EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
252     void *keydata1 = NULL, *keydata2 = NULL;
253
254     if (pk1 == NULL || pk2 == NULL) {
255         if (pk1 == NULL && pk2 == NULL)
256             return 1;
257         return 0;
258     }
259
260     keymgmt1 = pk1->keymgmt;
261     keydata1 = pk1->keydata;
262     keymgmt2 = pk2->keymgmt;
263     keydata2 = pk2->keydata;
264
265     if (keymgmt1 != keymgmt2) {
266         /*
267          * The condition for a successful cross export is that the
268          * keydata to be exported is NULL (typed, but otherwise empty
269          * EVP_PKEY), or that it was possible to export it with
270          * evp_keymgmt_util_export_to_provider().
271          *
272          * We use |ok| to determine if it's ok to cross export one way,
273          * but also to determine if we should attempt a cross export
274          * the other way.  There's no point doing it both ways.
275          */
276         int ok = 1;
277
278         /* Complex case, where the keymgmt differ */
279         if (keymgmt1 != NULL
280             && keymgmt2 != NULL
281             && !match_type(keymgmt1, keymgmt2)) {
282             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
283             return -1;           /* Not the same type */
284         }
285
286         /*
287          * The key types are determined to match, so we try cross export,
288          * but only to keymgmt's that supply a matching function.
289          */
290         if (keymgmt2 != NULL
291             && keymgmt2->match != NULL) {
292             void *tmp_keydata = NULL;
293
294             ok = 1;
295             if (keydata1 != NULL) {
296                 tmp_keydata =
297                     evp_keymgmt_util_export_to_provider(pk1, keymgmt2);
298                 ok = (tmp_keydata != NULL);
299             }
300             if (ok) {
301                 keymgmt1 = keymgmt2;
302                 keydata1 = tmp_keydata;
303             }
304         }
305         /*
306          * If we've successfully cross exported one way, there's not point
307          * doing it the other way, hence the |!ok| check.
308          */
309         if (!ok
310             && keymgmt1 != NULL
311             && keymgmt1->match != NULL) {
312             void *tmp_keydata = NULL;
313
314             ok = 1;
315             if (keydata2 != NULL) {
316                 tmp_keydata =
317                     evp_keymgmt_util_export_to_provider(pk2, keymgmt1);
318                 ok = (tmp_keydata != NULL);
319             }
320             if (ok) {
321                 keymgmt2 = keymgmt1;
322                 keydata2 = tmp_keydata;
323             }
324         }
325     }
326
327     /* If we still don't have matching keymgmt implementations, we give up */
328     if (keymgmt1 != keymgmt2)
329         return -2;
330
331     /* If both keydata are NULL, then they're the same key */
332     if (keydata1 == NULL && keydata2 == NULL)
333         return 1;
334     /* If only one of the keydata is NULL, then they're different keys */
335     if (keydata1 == NULL || keydata2 == NULL)
336         return 0;
337     /* If both keydata are non-NULL, we let the backend decide */
338     return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
339 }
340
341 int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection)
342 {
343     /* Save copies of pointers we want to play with without affecting |to| */
344     EVP_KEYMGMT *to_keymgmt = to->keymgmt;
345     void *to_keydata = to->keydata, *alloc_keydata = NULL;
346
347     /* An unassigned key can't be copied */
348     if (from == NULL || from->keydata == NULL)
349         return 0;
350
351     if (to_keymgmt == from->keymgmt && to_keymgmt->copy != NULL) {
352         /* Make sure there's somewhere to copy to */
353         if (to_keydata == NULL
354             && (to_keydata = evp_keymgmt_newdata(to_keymgmt)) == NULL) {
355             ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
356             return 0;
357         }
358
359         /*
360          * |to| and |from| have the same keymgmt, and the copy function is
361          * implemented, so just copy and be done
362          */
363         if (!evp_keymgmt_copy(to_keymgmt, to_keydata, from->keydata,
364                               selection))
365             return 0;
366     } else if (match_type(to_keymgmt, from->keymgmt)) {
367         struct import_data_st import_data;
368
369         import_data.keymgmt = to_keymgmt;
370         import_data.keydata = to_keydata;
371         import_data.selection = selection;
372
373         if (!evp_keymgmt_export(from->keymgmt, from->keydata, selection,
374                                 &try_import, &import_data)) {
375             evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
376             return 0;
377         }
378
379         /*
380          * In this case to_keydata was previously unallocated, try_import()
381          * may have created it for us.
382          */
383         to_keydata = import_data.keydata;
384     } else {
385         ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
386         return 0;
387     }
388
389     if (to->keymgmt == NULL
390         && !EVP_KEYMGMT_up_ref(to_keymgmt)) {
391         evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
392         return 0;
393     }
394     evp_keymgmt_util_clear_operation_cache(to);
395     to->keymgmt = to_keymgmt;
396     to->keydata = to_keydata;
397     evp_keymgmt_util_cache_keyinfo(to);
398
399     return 1;
400 }
401
402 void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
403                            void *genctx, OSSL_CALLBACK *cb, void *cbarg)
404 {
405     void *keydata = evp_keymgmt_gen(keymgmt, genctx, cb, cbarg);
406
407     if (keydata != NULL) {
408         if (!EVP_KEYMGMT_up_ref(keymgmt)) {
409             evp_keymgmt_freedata(keymgmt, keydata);
410             return NULL;
411         }
412
413         evp_keymgmt_util_clear_operation_cache(target);
414         target->keymgmt = keymgmt;
415         target->keydata = keydata;
416         evp_keymgmt_util_cache_keyinfo(target);
417     }
418
419     return keydata;
420 }