EVP: Add internal functions to fetch type specific EVP methods from provider
[openssl.git] / crypto / evp / exchange.c
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/evp.h>
12 #include <openssl/err.h>
13 #include "internal/cryptlib.h"
14 #include "internal/refcount.h"
15 #include "internal/provider.h"
16 #include "internal/core.h"
17 #include "internal/numbers.h"   /* includes SIZE_MAX */
18 #include "crypto/evp.h"
19 #include "evp_local.h"
20
21 static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
22 {
23     EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
24
25     if (exchange == NULL) {
26         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
27         return NULL;
28     }
29
30     exchange->lock = CRYPTO_THREAD_lock_new();
31     if (exchange->lock == NULL) {
32         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
33         OPENSSL_free(exchange);
34         return NULL;
35     }
36     exchange->prov = prov;
37     ossl_provider_up_ref(prov);
38     exchange->refcnt = 1;
39
40     return exchange;
41 }
42
43 static void *evp_keyexch_from_algorithm(int name_id,
44                                         const OSSL_ALGORITHM *algodef,
45                                         OSSL_PROVIDER *prov)
46 {
47     const OSSL_DISPATCH *fns = algodef->implementation;
48     EVP_KEYEXCH *exchange = NULL;
49     int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
50
51     if ((exchange = evp_keyexch_new(prov)) == NULL) {
52         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
53         goto err;
54     }
55
56     exchange->name_id = name_id;
57     if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
58         goto err;
59     exchange->description = algodef->algorithm_description;
60
61     for (; fns->function_id != 0; fns++) {
62         switch (fns->function_id) {
63         case OSSL_FUNC_KEYEXCH_NEWCTX:
64             if (exchange->newctx != NULL)
65                 break;
66             exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
67             fncnt++;
68             break;
69         case OSSL_FUNC_KEYEXCH_INIT:
70             if (exchange->init != NULL)
71                 break;
72             exchange->init = OSSL_FUNC_keyexch_init(fns);
73             fncnt++;
74             break;
75         case OSSL_FUNC_KEYEXCH_SET_PEER:
76             if (exchange->set_peer != NULL)
77                 break;
78             exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
79             break;
80         case OSSL_FUNC_KEYEXCH_DERIVE:
81             if (exchange->derive != NULL)
82                 break;
83             exchange->derive = OSSL_FUNC_keyexch_derive(fns);
84             fncnt++;
85             break;
86         case OSSL_FUNC_KEYEXCH_FREECTX:
87             if (exchange->freectx != NULL)
88                 break;
89             exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
90             fncnt++;
91             break;
92         case OSSL_FUNC_KEYEXCH_DUPCTX:
93             if (exchange->dupctx != NULL)
94                 break;
95             exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
96             break;
97         case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
98             if (exchange->get_ctx_params != NULL)
99                 break;
100             exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
101             gparamfncnt++;
102             break;
103         case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
104             if (exchange->gettable_ctx_params != NULL)
105                 break;
106             exchange->gettable_ctx_params
107                 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
108             gparamfncnt++;
109             break;
110         case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
111             if (exchange->set_ctx_params != NULL)
112                 break;
113             exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
114             sparamfncnt++;
115             break;
116         case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
117             if (exchange->settable_ctx_params != NULL)
118                 break;
119             exchange->settable_ctx_params
120                 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
121             sparamfncnt++;
122             break;
123         }
124     }
125     if (fncnt != 4
126             || (gparamfncnt != 0 && gparamfncnt != 2)
127             || (sparamfncnt != 0 && sparamfncnt != 2)) {
128         /*
129          * In order to be a consistent set of functions we must have at least
130          * a complete set of "exchange" functions: init, derive, newctx,
131          * and freectx. The set_ctx_params and settable_ctx_params functions are
132          * optional, but if one of them is present then the other one must also
133          * be present. Same goes for get_ctx_params and gettable_ctx_params.
134          * The dupctx and set_peer functions are optional.
135          */
136         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
137         goto err;
138     }
139
140     return exchange;
141
142  err:
143     EVP_KEYEXCH_free(exchange);
144     return NULL;
145 }
146
147 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
148 {
149     int i;
150
151     if (exchange == NULL)
152         return;
153     CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
154     if (i > 0)
155         return;
156     OPENSSL_free(exchange->type_name);
157     ossl_provider_free(exchange->prov);
158     CRYPTO_THREAD_lock_free(exchange->lock);
159     OPENSSL_free(exchange);
160 }
161
162 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
163 {
164     int ref = 0;
165
166     CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
167     return 1;
168 }
169
170 OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
171 {
172     return exchange->prov;
173 }
174
175 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
176                                const char *properties)
177 {
178     return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
179                              evp_keyexch_from_algorithm,
180                              (int (*)(void *))EVP_KEYEXCH_up_ref,
181                              (void (*)(void *))EVP_KEYEXCH_free);
182 }
183
184 EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
185                                          const char *algorithm,
186                                          const char *properties)
187 {
188     return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
189                                        algorithm, properties,
190                                        evp_keyexch_from_algorithm,
191                                        (int (*)(void *))EVP_KEYEXCH_up_ref,
192                                        (void (*)(void *))EVP_KEYEXCH_free);
193 }
194
195 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
196 {
197     return EVP_PKEY_derive_init_ex(ctx, NULL);
198 }
199
200 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
201 {
202     int ret;
203     void *provkey = NULL;
204     EVP_KEYEXCH *exchange = NULL;
205     EVP_KEYMGMT *tmp_keymgmt = NULL;
206     const char *supported_exch = NULL;
207
208     if (ctx == NULL) {
209         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
210         return -2;
211     }
212
213     evp_pkey_ctx_free_old_ops(ctx);
214     ctx->operation = EVP_PKEY_OP_DERIVE;
215
216     ERR_set_mark();
217
218     if (evp_pkey_ctx_is_legacy(ctx))
219         goto legacy;
220
221     /*
222      * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
223      * a blank one.
224      */
225     if (ctx->pkey == NULL) {
226         EVP_PKEY *pkey = EVP_PKEY_new();
227
228         if (pkey == NULL
229             || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
230             || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) {
231             ERR_clear_last_mark();
232             EVP_PKEY_free(pkey);
233             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
234             goto err;
235         }
236         ctx->pkey = pkey;
237     }
238
239     /*
240      * Try to derive the supported exch from |ctx->keymgmt|.
241      */
242     if (!ossl_assert(ctx->pkey->keymgmt == NULL
243                      || ctx->pkey->keymgmt == ctx->keymgmt)) {
244         ERR_clear_last_mark();
245         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
246         goto err;
247     }
248     supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
249                                                            OSSL_OP_KEYEXCH);
250     if (supported_exch == NULL) {
251         ERR_clear_last_mark();
252         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
253         goto err;
254     }
255
256
257     /*
258      * Because we cleared out old ops, we shouldn't need to worry about
259      * checking if exchange is already there.
260      */
261     exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
262     if (exchange == NULL)
263         goto legacy;
264
265     /*
266      * Ensure that the key is provided, either natively, or as a cached export.
267      * We start by fetching the keymgmt with the same name as |ctx->pkey|,
268      * but from the provider of the exch method, using the same property
269      * query as when fetching the exch method.
270      * With the keymgmt we found (if we did), we try to export |ctx->pkey|
271      * to it (evp_pkey_export_to_provider() is smart enough to only actually
272
273      * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
274      */
275     tmp_keymgmt
276         = evp_keymgmt_fetch_from_prov(EVP_KEYEXCH_get0_provider(exchange),
277                                       EVP_KEYMGMT_get0_name(ctx->keymgmt),
278                                       ctx->propquery);
279     if (tmp_keymgmt != NULL)
280         provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
281                                               &tmp_keymgmt, ctx->propquery);
282     if (provkey == NULL)
283         goto legacy;
284
285     ERR_pop_to_mark();
286
287     /* No more legacy from here down to legacy: */
288
289     ctx->op.kex.exchange = exchange;
290     ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
291     if (ctx->op.kex.algctx == NULL) {
292         /* The provider key can stay in the cache */
293         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
294         goto err;
295     }
296     ret = exchange->init(ctx->op.kex.algctx, provkey, params);
297
298     EVP_KEYMGMT_free(tmp_keymgmt);
299     return ret ? 1 : 0;
300  err:
301     evp_pkey_ctx_free_old_ops(ctx);
302     ctx->operation = EVP_PKEY_OP_UNDEFINED;
303     EVP_KEYMGMT_free(tmp_keymgmt);
304     return 0;
305
306  legacy:
307     /*
308      * If we don't have the full support we need with provided methods,
309      * let's go see if legacy does.
310      */
311     ERR_pop_to_mark();
312
313 #ifdef FIPS_MODULE
314     return 0;
315 #else
316     if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
317         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
318         return -2;
319     }
320
321     if (ctx->pmeth->derive_init == NULL)
322         return 1;
323     ret = ctx->pmeth->derive_init(ctx);
324     if (ret <= 0)
325         ctx->operation = EVP_PKEY_OP_UNDEFINED;
326     EVP_KEYMGMT_free(tmp_keymgmt);
327     return ret;
328 #endif
329 }
330
331 int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
332                                 int validate_peer)
333 {
334     int ret = 0, check;
335     void *provkey = NULL;
336     EVP_PKEY_CTX *check_ctx = NULL;
337
338     if (ctx == NULL) {
339         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
340         return -1;
341     }
342
343     if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
344         goto legacy;
345
346     if (ctx->op.kex.exchange->set_peer == NULL) {
347         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
348         return -2;
349     }
350
351     if (validate_peer) {
352         check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
353         if (check_ctx == NULL)
354             return -1;
355         check = EVP_PKEY_public_check(check_ctx);
356         EVP_PKEY_CTX_free(check_ctx);
357         if (check <= 0)
358             return -1;
359     }
360
361     provkey = evp_pkey_export_to_provider(peer, ctx->libctx, &ctx->keymgmt,
362                                           ctx->propquery);
363     /*
364      * If making the key provided wasn't possible, legacy may be able to pick
365      * it up
366      */
367     if (provkey == NULL)
368         goto legacy;
369     return ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
370
371  legacy:
372 #ifdef FIPS_MODULE
373     return ret;
374 #else
375     if (ctx->pmeth == NULL
376         || !(ctx->pmeth->derive != NULL
377              || ctx->pmeth->encrypt != NULL
378              || ctx->pmeth->decrypt != NULL)
379         || ctx->pmeth->ctrl == NULL) {
380         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
381         return -2;
382     }
383     if (ctx->operation != EVP_PKEY_OP_DERIVE
384         && ctx->operation != EVP_PKEY_OP_ENCRYPT
385         && ctx->operation != EVP_PKEY_OP_DECRYPT) {
386         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
387         return -1;
388     }
389
390     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
391
392     if (ret <= 0)
393         return ret;
394
395     if (ret == 2)
396         return 1;
397
398     if (ctx->pkey == NULL) {
399         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
400         return -1;
401     }
402
403     if (ctx->pkey->type != peer->type) {
404         ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
405         return -1;
406     }
407
408     /*
409      * For clarity.  The error is if parameters in peer are
410      * present (!missing) but don't match.  EVP_PKEY_parameters_eq may return
411      * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
412      * (different key types) is impossible here because it is checked earlier.
413      * -2 is OK for us here, as well as 1, so we can check for 0 only.
414      */
415     if (!EVP_PKEY_missing_parameters(peer) &&
416         !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
417         ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
418         return -1;
419     }
420
421     EVP_PKEY_free(ctx->peerkey);
422     ctx->peerkey = peer;
423
424     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
425
426     if (ret <= 0) {
427         ctx->peerkey = NULL;
428         return ret;
429     }
430
431     EVP_PKEY_up_ref(peer);
432     return 1;
433 #endif
434 }
435
436 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
437 {
438     return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
439 }
440
441 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
442 {
443     int ret;
444
445     if (ctx == NULL || pkeylen == NULL) {
446         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
447         return -1;
448     }
449
450     if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
451         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
452         return -1;
453     }
454
455     if (ctx->op.kex.algctx == NULL)
456         goto legacy;
457
458     ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
459                                        key != NULL ? *pkeylen : 0);
460
461     return ret;
462  legacy:
463     if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
464         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
465         return -2;
466     }
467
468     M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
469         return ctx->pmeth->derive(ctx, key, pkeylen);
470 }
471
472 int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
473 {
474     return keyexch->name_id;
475 }
476
477 const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
478 {
479     return keyexch->type_name;
480 }
481
482 const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
483 {
484     return keyexch->description;
485 }
486
487 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
488 {
489     return evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
490 }
491
492 void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
493                                  void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
494                                  void *arg)
495 {
496     evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
497                        (void (*)(void *, void *))fn, arg,
498                        evp_keyexch_from_algorithm,
499                        (int (*)(void *))EVP_KEYEXCH_up_ref,
500                        (void (*)(void *))EVP_KEYEXCH_free);
501 }
502
503 int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
504                              void (*fn)(const char *name, void *data),
505                              void *data)
506 {
507     if (keyexch->prov != NULL)
508         return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
509
510     return 1;
511 }
512
513 const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
514 {
515     void *provctx;
516
517     if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
518         return NULL;
519
520     provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
521     return keyexch->gettable_ctx_params(NULL, provctx);
522 }
523
524 const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
525 {
526     void *provctx;
527
528     if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
529         return NULL;
530     provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
531     return keyexch->settable_ctx_params(NULL, provctx);
532 }