1 /*
2  * Copyright 2019-2021 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/crypto.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include "internal/provider.h"
15 #include "internal/refcount.h"
16 #include "internal/core.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
19 
keymgmt_new(void)20 static void *keymgmt_new(void)
21 {
22     EVP_KEYMGMT *keymgmt = NULL;
23 
24     if ((keymgmt = OPENSSL_zalloc(sizeof(*keymgmt))) == NULL
25         || (keymgmt->lock = CRYPTO_THREAD_lock_new()) == NULL) {
26         EVP_KEYMGMT_free(keymgmt);
27         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
28         return NULL;
29     }
30 
31     keymgmt->refcnt = 1;
32 
33     return keymgmt;
34 }
35 
keymgmt_from_algorithm(int name_id,const OSSL_ALGORITHM * algodef,OSSL_PROVIDER * prov)36 static void *keymgmt_from_algorithm(int name_id,
37                                     const OSSL_ALGORITHM *algodef,
38                                     OSSL_PROVIDER *prov)
39 {
40     const OSSL_DISPATCH *fns = algodef->implementation;
41     EVP_KEYMGMT *keymgmt = NULL;
42     int setparamfncnt = 0, getparamfncnt = 0;
43     int setgenparamfncnt = 0;
44     int importfncnt = 0, exportfncnt = 0;
45 
46     if ((keymgmt = keymgmt_new()) == NULL)
47         return NULL;
48 
49     keymgmt->name_id = name_id;
50     if ((keymgmt->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
51         EVP_KEYMGMT_free(keymgmt);
52         return NULL;
53     }
54     keymgmt->description = algodef->algorithm_description;
55 
56     for (; fns->function_id != 0; fns++) {
57         switch (fns->function_id) {
58         case OSSL_FUNC_KEYMGMT_NEW:
59             if (keymgmt->new == NULL)
60                 keymgmt->new = OSSL_FUNC_keymgmt_new(fns);
61             break;
62         case OSSL_FUNC_KEYMGMT_GEN_INIT:
63             if (keymgmt->gen_init == NULL)
64                 keymgmt->gen_init = OSSL_FUNC_keymgmt_gen_init(fns);
65             break;
66         case OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE:
67             if (keymgmt->gen_set_template == NULL)
68                 keymgmt->gen_set_template =
69                     OSSL_FUNC_keymgmt_gen_set_template(fns);
70             break;
71         case OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS:
72             if (keymgmt->gen_set_params == NULL) {
73                 setgenparamfncnt++;
74                 keymgmt->gen_set_params =
75                     OSSL_FUNC_keymgmt_gen_set_params(fns);
76             }
77             break;
78         case OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS:
79             if (keymgmt->gen_settable_params == NULL) {
80                 setgenparamfncnt++;
81                 keymgmt->gen_settable_params =
82                     OSSL_FUNC_keymgmt_gen_settable_params(fns);
83             }
84             break;
85         case OSSL_FUNC_KEYMGMT_GEN:
86             if (keymgmt->gen == NULL)
87                 keymgmt->gen = OSSL_FUNC_keymgmt_gen(fns);
88             break;
89         case OSSL_FUNC_KEYMGMT_GEN_CLEANUP:
90             if (keymgmt->gen_cleanup == NULL)
91                 keymgmt->gen_cleanup = OSSL_FUNC_keymgmt_gen_cleanup(fns);
92             break;
93         case OSSL_FUNC_KEYMGMT_FREE:
94             if (keymgmt->free == NULL)
95                 keymgmt->free = OSSL_FUNC_keymgmt_free(fns);
96             break;
97         case OSSL_FUNC_KEYMGMT_LOAD:
98             if (keymgmt->load == NULL)
99                 keymgmt->load = OSSL_FUNC_keymgmt_load(fns);
100             break;
101         case OSSL_FUNC_KEYMGMT_GET_PARAMS:
102             if (keymgmt->get_params == NULL) {
103                 getparamfncnt++;
104                 keymgmt->get_params = OSSL_FUNC_keymgmt_get_params(fns);
105             }
106             break;
107         case OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS:
108             if (keymgmt->gettable_params == NULL) {
109                 getparamfncnt++;
110                 keymgmt->gettable_params =
111                     OSSL_FUNC_keymgmt_gettable_params(fns);
112             }
113             break;
114          case OSSL_FUNC_KEYMGMT_SET_PARAMS:
115             if (keymgmt->set_params == NULL) {
116                 setparamfncnt++;
117                 keymgmt->set_params = OSSL_FUNC_keymgmt_set_params(fns);
118             }
119             break;
120         case OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS:
121             if (keymgmt->settable_params == NULL) {
122                 setparamfncnt++;
123                 keymgmt->settable_params =
124                     OSSL_FUNC_keymgmt_settable_params(fns);
125             }
126             break;
127         case OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME:
128             if (keymgmt->query_operation_name == NULL)
129                 keymgmt->query_operation_name =
130                     OSSL_FUNC_keymgmt_query_operation_name(fns);
131             break;
132         case OSSL_FUNC_KEYMGMT_HAS:
133             if (keymgmt->has == NULL)
134                 keymgmt->has = OSSL_FUNC_keymgmt_has(fns);
135             break;
136         case OSSL_FUNC_KEYMGMT_DUP:
137             if (keymgmt->dup == NULL)
138                 keymgmt->dup = OSSL_FUNC_keymgmt_dup(fns);
139             break;
140         case OSSL_FUNC_KEYMGMT_VALIDATE:
141             if (keymgmt->validate == NULL)
142                 keymgmt->validate = OSSL_FUNC_keymgmt_validate(fns);
143             break;
144         case OSSL_FUNC_KEYMGMT_MATCH:
145             if (keymgmt->match == NULL)
146                 keymgmt->match = OSSL_FUNC_keymgmt_match(fns);
147             break;
148         case OSSL_FUNC_KEYMGMT_IMPORT:
149             if (keymgmt->import == NULL) {
150                 importfncnt++;
151                 keymgmt->import = OSSL_FUNC_keymgmt_import(fns);
152             }
153             break;
154         case OSSL_FUNC_KEYMGMT_IMPORT_TYPES:
155             if (keymgmt->import_types == NULL) {
156                 importfncnt++;
157                 keymgmt->import_types = OSSL_FUNC_keymgmt_import_types(fns);
158             }
159             break;
160         case OSSL_FUNC_KEYMGMT_EXPORT:
161             if (keymgmt->export == NULL) {
162                 exportfncnt++;
163                 keymgmt->export = OSSL_FUNC_keymgmt_export(fns);
164             }
165             break;
166         case OSSL_FUNC_KEYMGMT_EXPORT_TYPES:
167             if (keymgmt->export_types == NULL) {
168                 exportfncnt++;
169                 keymgmt->export_types = OSSL_FUNC_keymgmt_export_types(fns);
170             }
171             break;
172         }
173     }
174     /*
175      * Try to check that the method is sensible.
176      * At least one constructor and the destructor are MANDATORY
177      * The functions 'has' is MANDATORY
178      * It makes no sense being able to free stuff if you can't create it.
179      * It makes no sense providing OSSL_PARAM descriptors for import and
180      * export if you can't import or export.
181      */
182     if (keymgmt->free == NULL
183         || (keymgmt->new == NULL
184             && keymgmt->gen == NULL
185             && keymgmt->load == NULL)
186         || keymgmt->has == NULL
187         || (getparamfncnt != 0 && getparamfncnt != 2)
188         || (setparamfncnt != 0 && setparamfncnt != 2)
189         || (setgenparamfncnt != 0 && setgenparamfncnt != 2)
190         || (importfncnt != 0 && importfncnt != 2)
191         || (exportfncnt != 0 && exportfncnt != 2)
192         || (keymgmt->gen != NULL
193             && (keymgmt->gen_init == NULL
194                 || keymgmt->gen_cleanup == NULL))) {
195         EVP_KEYMGMT_free(keymgmt);
196         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
197         return NULL;
198     }
199     keymgmt->prov = prov;
200     if (prov != NULL)
201         ossl_provider_up_ref(prov);
202 
203     return keymgmt;
204 }
205 
evp_keymgmt_fetch_by_number(OSSL_LIB_CTX * ctx,int name_id,const char * properties)206 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
207                                          const char *properties)
208 {
209     return evp_generic_fetch_by_number(ctx,
210                                        OSSL_OP_KEYMGMT, name_id, properties,
211                                        keymgmt_from_algorithm,
212                                        (int (*)(void *))EVP_KEYMGMT_up_ref,
213                                        (void (*)(void *))EVP_KEYMGMT_free);
214 }
215 
EVP_KEYMGMT_fetch(OSSL_LIB_CTX * ctx,const char * algorithm,const char * properties)216 EVP_KEYMGMT *EVP_KEYMGMT_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
217                                const char *properties)
218 {
219     return evp_generic_fetch(ctx, OSSL_OP_KEYMGMT, algorithm, properties,
220                              keymgmt_from_algorithm,
221                              (int (*)(void *))EVP_KEYMGMT_up_ref,
222                              (void (*)(void *))EVP_KEYMGMT_free);
223 }
224 
EVP_KEYMGMT_up_ref(EVP_KEYMGMT * keymgmt)225 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
226 {
227     int ref = 0;
228 
229     CRYPTO_UP_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
230     return 1;
231 }
232 
EVP_KEYMGMT_free(EVP_KEYMGMT * keymgmt)233 void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
234 {
235     int ref = 0;
236 
237     if (keymgmt == NULL)
238         return;
239 
240     CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
241     if (ref > 0)
242         return;
243     OPENSSL_free(keymgmt->type_name);
244     ossl_provider_free(keymgmt->prov);
245     CRYPTO_THREAD_lock_free(keymgmt->lock);
246     OPENSSL_free(keymgmt);
247 }
248 
EVP_KEYMGMT_get0_provider(const EVP_KEYMGMT * keymgmt)249 const OSSL_PROVIDER *EVP_KEYMGMT_get0_provider(const EVP_KEYMGMT *keymgmt)
250 {
251     return keymgmt->prov;
252 }
253 
evp_keymgmt_get_number(const EVP_KEYMGMT * keymgmt)254 int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt)
255 {
256     return keymgmt->name_id;
257 }
258 
EVP_KEYMGMT_get0_description(const EVP_KEYMGMT * keymgmt)259 const char *EVP_KEYMGMT_get0_description(const EVP_KEYMGMT *keymgmt)
260 {
261     return keymgmt->description;
262 }
263 
EVP_KEYMGMT_get0_name(const EVP_KEYMGMT * keymgmt)264 const char *EVP_KEYMGMT_get0_name(const EVP_KEYMGMT *keymgmt)
265 {
266     return keymgmt->type_name;
267 }
268 
EVP_KEYMGMT_is_a(const EVP_KEYMGMT * keymgmt,const char * name)269 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
270 {
271     return evp_is_a(keymgmt->prov, keymgmt->name_id, NULL, name);
272 }
273 
EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX * libctx,void (* fn)(EVP_KEYMGMT * keymgmt,void * arg),void * arg)274 void EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX *libctx,
275                                  void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
276                                  void *arg)
277 {
278     evp_generic_do_all(libctx, OSSL_OP_KEYMGMT,
279                        (void (*)(void *, void *))fn, arg,
280                        keymgmt_from_algorithm,
281                        (int (*)(void *))EVP_KEYMGMT_up_ref,
282                        (void (*)(void *))EVP_KEYMGMT_free);
283 }
284 
EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT * keymgmt,void (* fn)(const char * name,void * data),void * data)285 int EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
286                              void (*fn)(const char *name, void *data),
287                              void *data)
288 {
289     if (keymgmt->prov != NULL)
290         return evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
291 
292     return 1;
293 }
294 
295 /*
296  * Internal API that interfaces with the method function pointers
297  */
evp_keymgmt_newdata(const EVP_KEYMGMT * keymgmt)298 void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt)
299 {
300     void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
301 
302     /*
303      * 'new' is currently mandatory on its own, but when new
304      * constructors appear, it won't be quite as mandatory,
305      * so we have a check for future cases.
306      */
307     if (keymgmt->new == NULL)
308         return NULL;
309     return keymgmt->new(provctx);
310 }
311 
evp_keymgmt_freedata(const EVP_KEYMGMT * keymgmt,void * keydata)312 void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keydata)
313 {
314     /* This is mandatory, no need to check for its presence */
315     keymgmt->free(keydata);
316 }
317 
evp_keymgmt_gen_init(const EVP_KEYMGMT * keymgmt,int selection,const OSSL_PARAM params[])318 void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
319                            const OSSL_PARAM params[])
320 {
321     void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
322 
323     if (keymgmt->gen_init == NULL)
324         return NULL;
325     return keymgmt->gen_init(provctx, selection, params);
326 }
327 
evp_keymgmt_gen_set_template(const EVP_KEYMGMT * keymgmt,void * genctx,void * template)328 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
329                                  void *template)
330 {
331     /*
332      * It's arguable if we actually should return success in this case, as
333      * it allows the caller to set a template key, which is then ignored.
334      * However, this is how the legacy methods (EVP_PKEY_METHOD) operate,
335      * so we do this in the interest of backward compatibility.
336      */
337     if (keymgmt->gen_set_template == NULL)
338         return 1;
339     return keymgmt->gen_set_template(genctx, template);
340 }
341 
evp_keymgmt_gen_set_params(const EVP_KEYMGMT * keymgmt,void * genctx,const OSSL_PARAM params[])342 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
343                                const OSSL_PARAM params[])
344 {
345     if (keymgmt->gen_set_params == NULL)
346         return 0;
347     return keymgmt->gen_set_params(genctx, params);
348 }
349 
EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT * keymgmt)350 const OSSL_PARAM *EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT *keymgmt)
351 {
352     void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
353 
354     if (keymgmt->gen_settable_params == NULL)
355         return NULL;
356     return keymgmt->gen_settable_params(NULL, provctx);
357 }
358 
evp_keymgmt_gen(const EVP_KEYMGMT * keymgmt,void * genctx,OSSL_CALLBACK * cb,void * cbarg)359 void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
360                       OSSL_CALLBACK *cb, void *cbarg)
361 {
362     if (keymgmt->gen == NULL)
363         return NULL;
364     return keymgmt->gen(genctx, cb, cbarg);
365 }
366 
evp_keymgmt_gen_cleanup(const EVP_KEYMGMT * keymgmt,void * genctx)367 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx)
368 {
369     if (keymgmt->gen != NULL)
370         keymgmt->gen_cleanup(genctx);
371 }
372 
evp_keymgmt_has_load(const EVP_KEYMGMT * keymgmt)373 int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt)
374 {
375     return keymgmt != NULL && keymgmt->load != NULL;
376 }
377 
evp_keymgmt_load(const EVP_KEYMGMT * keymgmt,const void * objref,size_t objref_sz)378 void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
379                        const void *objref, size_t objref_sz)
380 {
381     if (evp_keymgmt_has_load(keymgmt))
382         return keymgmt->load(objref, objref_sz);
383     return NULL;
384 }
385 
evp_keymgmt_get_params(const EVP_KEYMGMT * keymgmt,void * keydata,OSSL_PARAM params[])386 int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata,
387                            OSSL_PARAM params[])
388 {
389     if (keymgmt->get_params == NULL)
390         return 1;
391     return keymgmt->get_params(keydata, params);
392 }
393 
EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT * keymgmt)394 const OSSL_PARAM *EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT *keymgmt)
395 {
396     void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
397 
398     if (keymgmt->gettable_params == NULL)
399         return NULL;
400     return keymgmt->gettable_params(provctx);
401 }
402 
evp_keymgmt_set_params(const EVP_KEYMGMT * keymgmt,void * keydata,const OSSL_PARAM params[])403 int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, void *keydata,
404                            const OSSL_PARAM params[])
405 {
406     if (keymgmt->set_params == NULL)
407         return 1;
408     return keymgmt->set_params(keydata, params);
409 }
410 
EVP_KEYMGMT_settable_params(const EVP_KEYMGMT * keymgmt)411 const OSSL_PARAM *EVP_KEYMGMT_settable_params(const EVP_KEYMGMT *keymgmt)
412 {
413     void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
414 
415     if (keymgmt->settable_params == NULL)
416         return NULL;
417     return keymgmt->settable_params(provctx);
418 }
419 
evp_keymgmt_has(const EVP_KEYMGMT * keymgmt,void * keydata,int selection)420 int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keydata, int selection)
421 {
422     /* This is mandatory, no need to check for its presence */
423     return keymgmt->has(keydata, selection);
424 }
425 
evp_keymgmt_validate(const EVP_KEYMGMT * keymgmt,void * keydata,int selection,int checktype)426 int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
427                          int selection, int checktype)
428 {
429     /* We assume valid if the implementation doesn't have a function */
430     if (keymgmt->validate == NULL)
431         return 1;
432     return keymgmt->validate(keydata, selection, checktype);
433 }
434 
evp_keymgmt_match(const EVP_KEYMGMT * keymgmt,const void * keydata1,const void * keydata2,int selection)435 int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
436                       const void *keydata1, const void *keydata2,
437                       int selection)
438 {
439     /* We assume no match if the implementation doesn't have a function */
440     if (keymgmt->match == NULL)
441         return 0;
442     return keymgmt->match(keydata1, keydata2, selection);
443 }
444 
evp_keymgmt_import(const EVP_KEYMGMT * keymgmt,void * keydata,int selection,const OSSL_PARAM params[])445 int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
446                        int selection, const OSSL_PARAM params[])
447 {
448     if (keymgmt->import == NULL)
449         return 0;
450     return keymgmt->import(keydata, selection, params);
451 }
452 
evp_keymgmt_import_types(const EVP_KEYMGMT * keymgmt,int selection)453 const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
454                                            int selection)
455 {
456     if (keymgmt->import_types == NULL)
457         return NULL;
458     return keymgmt->import_types(selection);
459 }
460 
evp_keymgmt_export(const EVP_KEYMGMT * keymgmt,void * keydata,int selection,OSSL_CALLBACK * param_cb,void * cbarg)461 int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
462                        int selection, OSSL_CALLBACK *param_cb, void *cbarg)
463 {
464     if (keymgmt->export == NULL)
465         return 0;
466     return keymgmt->export(keydata, selection, param_cb, cbarg);
467 }
468 
evp_keymgmt_export_types(const EVP_KEYMGMT * keymgmt,int selection)469 const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
470                                            int selection)
471 {
472     if (keymgmt->export_types == NULL)
473         return NULL;
474     return keymgmt->export_types(selection);
475 }
476 
evp_keymgmt_dup(const EVP_KEYMGMT * keymgmt,const void * keydata_from,int selection)477 void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, const void *keydata_from,
478                       int selection)
479 {
480     /* We assume no dup if the implementation doesn't have a function */
481     if (keymgmt->dup == NULL)
482         return NULL;
483     return keymgmt->dup(keydata_from, selection);
484 }
485