Clean away unnecessary length related OSSL_PARAM key names
[openssl.git] / crypto / evp / pmeth_lib.c
1 /*
2  * Copyright 2006-2020 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 /*
11  * Low level key APIs (DH etc) are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <openssl/engine.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/core_names.h>
22 #include <openssl/dh.h>
23 #include <openssl/rsa.h>
24 #include <openssl/kdf.h>
25 #include "internal/cryptlib.h"
26 #include "crypto/asn1.h"
27 #include "crypto/evp.h"
28 #include "crypto/dh.h"
29 #include "crypto/ec.h"
30 #include "internal/ffc.h"
31 #include "internal/numbers.h"
32 #include "internal/provider.h"
33 #include "evp_local.h"
34
35 #ifndef FIPS_MODULE
36
37 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
38                                           int keytype, int optype,
39                                           int cmd, const char *name,
40                                           const void *data, size_t data_len);
41 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
42                                           int cmd, const char *name);
43 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
44
45 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
46 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
47
48 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
49
50 /* This array needs to be in order of NIDs */
51 static pmeth_fn standard_methods[] = {
52     ossl_rsa_pkey_method,
53 # ifndef OPENSSL_NO_DH
54     dh_pkey_method,
55 # endif
56 # ifndef OPENSSL_NO_DSA
57     dsa_pkey_method,
58 # endif
59 # ifndef OPENSSL_NO_EC
60     ec_pkey_method,
61 # endif
62     ossl_rsa_pss_pkey_method,
63 # ifndef OPENSSL_NO_DH
64     dhx_pkey_method,
65 # endif
66 # ifndef OPENSSL_NO_EC
67     ecx25519_pkey_method,
68     ecx448_pkey_method,
69 # endif
70 # ifndef OPENSSL_NO_EC
71     ed25519_pkey_method,
72     ed448_pkey_method,
73 # endif
74 };
75
76 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
77
78 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
79 {
80     return ((*a)->pkey_id - ((**b)())->pkey_id);
81 }
82
83 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
84
85 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
86                      const EVP_PKEY_METHOD *const *b)
87 {
88     return ((*a)->pkey_id - (*b)->pkey_id);
89 }
90
91 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
92 {
93     pmeth_fn *ret;
94     EVP_PKEY_METHOD tmp;
95     const EVP_PKEY_METHOD *t = &tmp;
96
97     tmp.pkey_id = type;
98     if (app_pkey_methods) {
99         int idx;
100         idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
101         if (idx >= 0)
102             return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
103     }
104     ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
105                                  sizeof(standard_methods) /
106                                  sizeof(pmeth_fn));
107     if (ret == NULL || *ret == NULL)
108         return NULL;
109     return (**ret)();
110 }
111
112 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
113 {
114     EVP_PKEY_METHOD *pmeth;
115
116     pmeth = OPENSSL_zalloc(sizeof(*pmeth));
117     if (pmeth == NULL) {
118         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
119         return NULL;
120     }
121
122     pmeth->pkey_id = id;
123     pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
124     return pmeth;
125 }
126
127 /* Three possible states: */
128 # define EVP_PKEY_STATE_UNKNOWN         0
129 # define EVP_PKEY_STATE_LEGACY          1
130 # define EVP_PKEY_STATE_PROVIDER        2
131
132 static int evp_pkey_ctx_state(EVP_PKEY_CTX *ctx)
133 {
134     if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
135         return EVP_PKEY_STATE_UNKNOWN;
136
137     if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
138          && ctx->op.kex.exchprovctx != NULL)
139         || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
140             && ctx->op.sig.sigprovctx != NULL)
141         || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
142             && ctx->op.ciph.ciphprovctx != NULL)
143         || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
144             && ctx->op.keymgmt.genctx != NULL)
145         || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
146             && ctx->op.encap.kemprovctx != NULL))
147         return EVP_PKEY_STATE_PROVIDER;
148
149     return EVP_PKEY_STATE_LEGACY;
150 }
151
152 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
153                                                   void *arg)
154 {
155     int *type = arg;
156
157     if (*type == NID_undef)
158         *type = evp_pkey_name2type(keytype);
159 }
160
161 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
162 {
163     int type = NID_undef;
164
165     EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
166                              &type);
167     return type;
168 }
169 #endif /* FIPS_MODULE */
170
171 static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
172                                  EVP_PKEY *pkey, ENGINE *e,
173                                  const char *keytype, const char *propquery,
174                                  int id)
175
176 {
177     EVP_PKEY_CTX *ret = NULL;
178     const EVP_PKEY_METHOD *pmeth = NULL;
179     EVP_KEYMGMT *keymgmt = NULL;
180
181     /*
182      * If the given |pkey| is provided, we extract the keytype from its
183      * keymgmt and skip over the legacy code.
184      */
185     if (pkey != NULL && evp_pkey_is_provided(pkey)) {
186         /* If we have an engine, something went wrong somewhere... */
187         if (!ossl_assert(e == NULL))
188             return NULL;
189         keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
190         goto common;
191     }
192
193 #ifndef FIPS_MODULE
194     /*
195      * TODO(3.0) This legacy code section should be removed when we stop
196      * supporting engines
197      */
198     /* BEGIN legacy */
199     if (id == -1) {
200         if (pkey != NULL)
201             id = pkey->type;
202         else if (keytype != NULL)
203             id = evp_pkey_name2type(keytype);
204         if (id == NID_undef)
205             id = -1;
206     }
207     /* If no ID was found here, we can only resort to find a keymgmt */
208     if (id == -1)
209         goto common;
210
211     /*
212      * Here, we extract what information we can for the purpose of
213      * supporting usage with implementations from providers, to make
214      * for a smooth transition from legacy stuff to provider based stuff.
215      *
216      * If an engine is given, this is entirely legacy, and we should not
217      * pretend anything else, so we only set the name when no engine is
218      * given.  If both are already given, someone made a mistake, and
219      * since that can only happen internally, it's safe to make an
220      * assertion.
221      */
222     if (!ossl_assert(e == NULL || keytype == NULL))
223         return NULL;
224     if (e == NULL)
225         keytype = OBJ_nid2sn(id);
226
227 # ifndef OPENSSL_NO_ENGINE
228     if (e == NULL && pkey != NULL)
229         e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
230     /* Try to find an ENGINE which implements this method */
231     if (e) {
232         if (!ENGINE_init(e)) {
233             ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
234             return NULL;
235         }
236     } else {
237         e = ENGINE_get_pkey_meth_engine(id);
238     }
239
240     /*
241      * If an ENGINE handled this method look it up. Otherwise use internal
242      * tables.
243      */
244     if (e != NULL)
245         pmeth = ENGINE_get_pkey_meth(e, id);
246     else
247 # endif
248         pmeth = EVP_PKEY_meth_find(id);
249
250     /* END legacy */
251 #endif /* FIPS_MODULE */
252  common:
253     /*
254      * If there's no engine and there's a name, we try fetching a provider
255      * implementation.
256      */
257     if (e == NULL && keytype != NULL) {
258         keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
259         if (keymgmt == NULL)
260             return NULL;   /* EVP_KEYMGMT_fetch() recorded an error */
261
262 #ifndef FIPS_MODULE
263         /*
264          * Chase down the legacy NID, as that might be needed for diverse
265          * purposes, such as ensure that EVP_PKEY_type() can return sensible
266          * values, or that there's a better chance to "downgrade" a key when
267          * needed.  We go through all keymgmt names, because the keytype
268          * that's passed to this function doesn't necessarily translate
269          * directly.
270          * TODO: Remove this when #legacy keys are gone.
271          */
272         if (keymgmt != NULL) {
273             int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt);
274
275             if (tmp_id != NID_undef) {
276                 if (id == -1) {
277                     id = tmp_id;
278                 } else {
279                     /*
280                      * It really really shouldn't differ.  If it still does,
281                      * something is very wrong.
282                      */
283                     if (!ossl_assert(id == tmp_id)) {
284                         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
285                         EVP_KEYMGMT_free(keymgmt);
286                         return NULL;
287                     }
288                 }
289             }
290         }
291 #endif
292     }
293
294     if (pmeth == NULL && keymgmt == NULL) {
295         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
296     } else {
297         ret = OPENSSL_zalloc(sizeof(*ret));
298         if (ret == NULL)
299             ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
300     }
301
302 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
303     if ((ret == NULL || pmeth == NULL) && e != NULL)
304         ENGINE_finish(e);
305 #endif
306
307     if (ret == NULL) {
308         EVP_KEYMGMT_free(keymgmt);
309         return NULL;
310     }
311     if (propquery != NULL) {
312         ret->propquery = OPENSSL_strdup(propquery);
313         if (ret->propquery == NULL) {
314             EVP_KEYMGMT_free(keymgmt);
315             return NULL;
316         }
317     }
318     ret->libctx = libctx;
319     ret->keytype = keytype;
320     ret->keymgmt = keymgmt;
321     ret->legacy_keytype = id;   /* TODO: Remove when #legacy key are gone */
322     ret->engine = e;
323     ret->pmeth = pmeth;
324     ret->operation = EVP_PKEY_OP_UNDEFINED;
325     ret->pkey = pkey;
326     if (pkey != NULL)
327         EVP_PKEY_up_ref(pkey);
328
329     if (pmeth != NULL && pmeth->init != NULL) {
330         if (pmeth->init(ret) <= 0) {
331             ret->pmeth = NULL;
332             EVP_PKEY_CTX_free(ret);
333             return NULL;
334         }
335     }
336
337     return ret;
338 }
339
340 /*- All methods below can also be used in FIPS_MODULE */
341
342 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
343                                          const char *name,
344                                          const char *propquery)
345 {
346     return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
347 }
348
349 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
350                                          const char *propquery)
351 {
352     return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
353 }
354
355 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
356 {
357     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
358         if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
359             ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
360         EVP_SIGNATURE_free(ctx->op.sig.signature);
361         ctx->op.sig.sigprovctx = NULL;
362         ctx->op.sig.signature = NULL;
363     } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
364         if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
365             ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
366         EVP_KEYEXCH_free(ctx->op.kex.exchange);
367         ctx->op.kex.exchprovctx = NULL;
368         ctx->op.kex.exchange = NULL;
369     } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
370         if (ctx->op.encap.kemprovctx != NULL && ctx->op.encap.kem != NULL)
371             ctx->op.encap.kem->freectx(ctx->op.encap.kemprovctx);
372         EVP_KEM_free(ctx->op.encap.kem);
373         ctx->op.encap.kemprovctx = NULL;
374         ctx->op.encap.kem = NULL;
375     }
376     else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
377         if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
378             ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
379         EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
380         ctx->op.ciph.ciphprovctx = NULL;
381         ctx->op.ciph.cipher = NULL;
382     } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
383         if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
384             evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
385     }
386 }
387
388 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
389 {
390     if (ctx == NULL)
391         return;
392     if (ctx->pmeth && ctx->pmeth->cleanup)
393         ctx->pmeth->cleanup(ctx);
394
395     evp_pkey_ctx_free_old_ops(ctx);
396 #ifndef FIPS_MODULE
397     evp_pkey_ctx_free_all_cached_data(ctx);
398 #endif
399     EVP_KEYMGMT_free(ctx->keymgmt);
400
401     OPENSSL_free(ctx->propquery);
402     EVP_PKEY_free(ctx->pkey);
403     EVP_PKEY_free(ctx->peerkey);
404 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
405     ENGINE_finish(ctx->engine);
406 #endif
407     BN_free(ctx->rsa_pubexp);
408     OPENSSL_free(ctx);
409 }
410
411 #ifndef FIPS_MODULE
412
413 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
414                              const EVP_PKEY_METHOD *meth)
415 {
416     if (ppkey_id)
417         *ppkey_id = meth->pkey_id;
418     if (pflags)
419         *pflags = meth->flags;
420 }
421
422 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
423 {
424     int pkey_id = dst->pkey_id;
425     int flags = dst->flags;
426
427     *dst = *src;
428
429     /* We only copy the function pointers so restore the other values */
430     dst->pkey_id = pkey_id;
431     dst->flags = flags;
432 }
433
434 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
435 {
436     if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
437         OPENSSL_free(pmeth);
438 }
439
440 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
441 {
442     return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
443 }
444
445 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
446 {
447     return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
448 }
449
450 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
451 {
452     EVP_PKEY_CTX *rctx;
453
454 # ifndef OPENSSL_NO_ENGINE
455     /* Make sure it's safe to copy a pkey context using an ENGINE */
456     if (pctx->engine && !ENGINE_init(pctx->engine)) {
457         ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
458         return 0;
459     }
460 # endif
461     rctx = OPENSSL_zalloc(sizeof(*rctx));
462     if (rctx == NULL) {
463         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
464         return NULL;
465     }
466
467     if (pctx->pkey != NULL)
468         EVP_PKEY_up_ref(pctx->pkey);
469     rctx->pkey = pctx->pkey;
470     rctx->operation = pctx->operation;
471     rctx->libctx = pctx->libctx;
472     rctx->keytype = pctx->keytype;
473     rctx->propquery = NULL;
474     if (pctx->propquery != NULL) {
475         rctx->propquery = OPENSSL_strdup(pctx->propquery);
476         if (rctx->propquery == NULL)
477             goto err;
478     }
479     rctx->legacy_keytype = pctx->legacy_keytype;
480
481     if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
482         if (pctx->op.kex.exchange != NULL) {
483             rctx->op.kex.exchange = pctx->op.kex.exchange;
484             if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
485                 goto err;
486         }
487         if (pctx->op.kex.exchprovctx != NULL) {
488             if (!ossl_assert(pctx->op.kex.exchange != NULL))
489                 goto err;
490             rctx->op.kex.exchprovctx
491                 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
492             if (rctx->op.kex.exchprovctx == NULL) {
493                 EVP_KEYEXCH_free(rctx->op.kex.exchange);
494                 goto err;
495             }
496             return rctx;
497         }
498     } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
499         if (pctx->op.sig.signature != NULL) {
500             rctx->op.sig.signature = pctx->op.sig.signature;
501             if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
502                 goto err;
503         }
504         if (pctx->op.sig.sigprovctx != NULL) {
505             if (!ossl_assert(pctx->op.sig.signature != NULL))
506                 goto err;
507             rctx->op.sig.sigprovctx
508                 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
509             if (rctx->op.sig.sigprovctx == NULL) {
510                 EVP_SIGNATURE_free(rctx->op.sig.signature);
511                 goto err;
512             }
513             return rctx;
514         }
515     } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
516         if (pctx->op.ciph.cipher != NULL) {
517             rctx->op.ciph.cipher = pctx->op.ciph.cipher;
518             if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
519                 goto err;
520         }
521         if (pctx->op.ciph.ciphprovctx != NULL) {
522             if (!ossl_assert(pctx->op.ciph.cipher != NULL))
523                 goto err;
524             rctx->op.ciph.ciphprovctx
525                 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
526             if (rctx->op.ciph.ciphprovctx == NULL) {
527                 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
528                 goto err;
529             }
530             return rctx;
531         }
532     } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
533         if (pctx->op.encap.kem != NULL) {
534             rctx->op.encap.kem = pctx->op.encap.kem;
535             if (!EVP_KEM_up_ref(rctx->op.encap.kem))
536                 goto err;
537         }
538         if (pctx->op.encap.kemprovctx != NULL) {
539             if (!ossl_assert(pctx->op.encap.kem != NULL))
540                 goto err;
541             rctx->op.encap.kemprovctx
542                 = pctx->op.encap.kem->dupctx(pctx->op.encap.kemprovctx);
543             if (rctx->op.encap.kemprovctx == NULL) {
544                 EVP_KEM_free(rctx->op.encap.kem);
545                 goto err;
546             }
547             return rctx;
548         }
549     } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
550         /* Not supported - This would need a gen_dupctx() to work */
551         goto err;
552     }
553
554     rctx->pmeth = pctx->pmeth;
555 # ifndef OPENSSL_NO_ENGINE
556     rctx->engine = pctx->engine;
557 # endif
558
559     if (pctx->peerkey != NULL)
560         EVP_PKEY_up_ref(pctx->peerkey);
561     rctx->peerkey = pctx->peerkey;
562
563     if (pctx->pmeth == NULL) {
564         if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
565             EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
566             void *provkey;
567
568             provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
569                                                   &tmp_keymgmt, pctx->propquery);
570             if (provkey == NULL)
571                 goto err;
572             if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
573                 goto err;
574             EVP_KEYMGMT_free(rctx->keymgmt);
575             rctx->keymgmt = tmp_keymgmt;
576             return rctx;
577         }
578     } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
579         return rctx;
580     }
581 err:
582     rctx->pmeth = NULL;
583     EVP_PKEY_CTX_free(rctx);
584     return NULL;
585 }
586
587 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
588 {
589     if (app_pkey_methods == NULL) {
590         app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
591         if (app_pkey_methods == NULL){
592             ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
593             return 0;
594         }
595     }
596     if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
597         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
598         return 0;
599     }
600     sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
601     return 1;
602 }
603
604 void evp_app_cleanup_int(void)
605 {
606     if (app_pkey_methods != NULL)
607         sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
608 }
609
610 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
611 {
612     const EVP_PKEY_METHOD *ret;
613
614     ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
615
616     return ret == NULL ? 0 : 1;
617 }
618
619 size_t EVP_PKEY_meth_get_count(void)
620 {
621     size_t rv = OSSL_NELEM(standard_methods);
622
623     if (app_pkey_methods)
624         rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
625     return rv;
626 }
627
628 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
629 {
630     if (idx < OSSL_NELEM(standard_methods))
631         return (standard_methods[idx])();
632     if (app_pkey_methods == NULL)
633         return NULL;
634     idx -= OSSL_NELEM(standard_methods);
635     if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
636         return NULL;
637     return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
638 }
639 #endif
640
641 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
642 {
643     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
644             && ctx->op.kex.exchprovctx != NULL
645             && ctx->op.kex.exchange != NULL
646             && ctx->op.kex.exchange->set_ctx_params != NULL)
647         return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
648                                                     params);
649     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
650             && ctx->op.sig.sigprovctx != NULL
651             && ctx->op.sig.signature != NULL
652             && ctx->op.sig.signature->set_ctx_params != NULL)
653         return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
654                                                      params);
655     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
656             && ctx->op.ciph.ciphprovctx != NULL
657             && ctx->op.ciph.cipher != NULL
658             && ctx->op.ciph.cipher->set_ctx_params != NULL)
659         return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
660                                                      params);
661     if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
662         && ctx->op.keymgmt.genctx != NULL
663         && ctx->keymgmt != NULL
664         && ctx->keymgmt->gen_set_params != NULL)
665         return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
666                                           params);
667     if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
668         && ctx->op.encap.kemprovctx != NULL
669         && ctx->op.encap.kem != NULL
670         && ctx->op.encap.kem->set_ctx_params != NULL)
671         return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.kemprovctx,
672                                                  params);
673     return 0;
674 }
675
676 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
677 {
678     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
679             && ctx->op.kex.exchprovctx != NULL
680             && ctx->op.kex.exchange != NULL
681             && ctx->op.kex.exchange->get_ctx_params != NULL)
682         return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
683                                                     params);
684     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
685             && ctx->op.sig.sigprovctx != NULL
686             && ctx->op.sig.signature != NULL
687             && ctx->op.sig.signature->get_ctx_params != NULL)
688         return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
689                                                      params);
690     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
691             && ctx->op.ciph.ciphprovctx != NULL
692             && ctx->op.ciph.cipher != NULL
693             && ctx->op.ciph.cipher->get_ctx_params != NULL)
694         return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
695                                                    params);
696     if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
697         && ctx->op.encap.kemprovctx != NULL
698         && ctx->op.encap.kem != NULL
699         && ctx->op.encap.kem->get_ctx_params != NULL)
700         return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.kemprovctx,
701                                                  params);
702     return 0;
703 }
704
705 #ifndef FIPS_MODULE
706 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
707 {
708     void *provctx;
709
710     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
711             && ctx->op.kex.exchange != NULL
712             && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
713         provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
714         return ctx->op.kex.exchange->gettable_ctx_params(provctx);
715     }
716     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
717             && ctx->op.sig.signature != NULL
718             && ctx->op.sig.signature->gettable_ctx_params != NULL) {
719         provctx = ossl_provider_ctx(
720                       EVP_SIGNATURE_provider(ctx->op.sig.signature));
721         return ctx->op.sig.signature->gettable_ctx_params(provctx);
722     }
723     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
724             && ctx->op.ciph.cipher != NULL
725             && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
726         provctx = ossl_provider_ctx(
727                       EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
728         return ctx->op.ciph.cipher->gettable_ctx_params(provctx);
729     }
730     if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
731         && ctx->op.encap.kem != NULL
732         && ctx->op.encap.kem->gettable_ctx_params != NULL) {
733         provctx = ossl_provider_ctx(EVP_KEM_provider(ctx->op.encap.kem));
734         return ctx->op.encap.kem->gettable_ctx_params(provctx);
735     }
736     return NULL;
737 }
738
739 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
740 {
741     void *provctx;
742
743     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
744             && ctx->op.kex.exchange != NULL
745             && ctx->op.kex.exchange->settable_ctx_params != NULL) {
746         provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
747         return ctx->op.kex.exchange->settable_ctx_params(provctx);
748     }
749     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
750             && ctx->op.sig.signature != NULL
751             && ctx->op.sig.signature->settable_ctx_params != NULL) {
752         provctx = ossl_provider_ctx(
753                       EVP_SIGNATURE_provider(ctx->op.sig.signature));
754         return ctx->op.sig.signature->settable_ctx_params(provctx);
755     }
756     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
757             && ctx->op.ciph.cipher != NULL
758             && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
759         provctx = ossl_provider_ctx(
760                       EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
761         return ctx->op.ciph.cipher->settable_ctx_params(provctx);
762     }
763     if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
764             && ctx->keymgmt != NULL)
765         return EVP_KEYMGMT_gen_settable_params(ctx->keymgmt);
766     if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
767         && ctx->op.encap.kem != NULL
768         && ctx->op.encap.kem->settable_ctx_params != NULL) {
769         provctx = ossl_provider_ctx(EVP_KEM_provider(ctx->op.encap.kem));
770         return ctx->op.encap.kem->settable_ctx_params(provctx);
771     }
772     return NULL;
773 }
774
775 /*
776  * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
777  *
778  * Return 1 on success, 0 or negative for errors.
779  *
780  * In particular they return -2 if any of the params is not supported.
781  *
782  * They are not available in FIPS_MODULE as they depend on
783  *      - EVP_PKEY_CTX_{get,set}_params()
784  *      - EVP_PKEY_CTX_{gettable,settable}_params()
785  *
786  */
787 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
788 {
789     const OSSL_PARAM *p;
790
791     if (ctx == NULL || params == NULL)
792         return 0;
793
794     for (p = params; p->key != NULL; p++) {
795         /* Check the ctx actually understands this parameter */
796         if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
797                                     p->key) == NULL )
798             return -2;
799     }
800
801     return EVP_PKEY_CTX_set_params(ctx, params);
802 }
803
804 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
805 {
806     const OSSL_PARAM *p;
807
808     if (ctx == NULL || params == NULL)
809         return 0;
810
811     for (p = params; p->key != NULL; p++ ) {
812         /* Check the ctx actually understands this parameter */
813         if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
814                                     p->key) == NULL )
815             return -2;
816     }
817
818     return EVP_PKEY_CTX_get_params(ctx, params);
819 }
820
821 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
822 {
823     OSSL_PARAM sig_md_params[2], *p = sig_md_params;
824     /* 80 should be big enough */
825     char name[80] = "";
826     const EVP_MD *tmp;
827
828     if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
829         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
830         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
831         return -2;
832     }
833
834     /* TODO(3.0): Remove this eventually when no more legacy */
835     if (ctx->op.sig.sigprovctx == NULL)
836         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
837                                  EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
838
839     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
840                                             name,
841                                             sizeof(name));
842     *p = OSSL_PARAM_construct_end();
843
844     if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
845         return 0;
846
847     tmp = evp_get_digestbyname_ex(ctx->libctx, name);
848     if (tmp == NULL)
849         return 0;
850
851     *md = tmp;
852
853     return 1;
854 }
855
856 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
857                                int fallback, const char *param, int op,
858                                int ctrl)
859 {
860     OSSL_PARAM md_params[2], *p = md_params;
861     const char *name;
862
863     if (ctx == NULL || (ctx->operation & op) == 0) {
864         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
865         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
866         return -2;
867     }
868
869     /* TODO(3.0): Remove this eventually when no more legacy */
870     if (fallback)
871         return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
872
873     if (md == NULL) {
874         name = "";
875     } else {
876         name = EVP_MD_name(md);
877     }
878
879     *p++ = OSSL_PARAM_construct_utf8_string(param,
880                                             /*
881                                              * Cast away the const. This is read
882                                              * only so should be safe
883                                              */
884                                             (char *)name, 0);
885     *p = OSSL_PARAM_construct_end();
886
887     return EVP_PKEY_CTX_set_params(ctx, md_params);
888 }
889
890 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
891 {
892     return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.sigprovctx == NULL,
893                                OSSL_SIGNATURE_PARAM_DIGEST,
894                                EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
895 }
896
897 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
898 {
899     return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
900                                OSSL_KDF_PARAM_DIGEST,
901                                EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
902 }
903
904 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
905                                           const char *param, int op, int ctrl,
906                                           const unsigned char *data,
907                                           int datalen)
908 {
909     OSSL_PARAM octet_string_params[2], *p = octet_string_params;
910
911     if (ctx == NULL || (ctx->operation & op) == 0) {
912         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
913         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
914         return -2;
915     }
916
917     /* TODO(3.0): Remove this eventually when no more legacy */
918     if (fallback)
919         return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
920
921     if (datalen < 0) {
922         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
923         return 0;
924     }
925
926     *p++ = OSSL_PARAM_construct_octet_string(param,
927                                             /*
928                                              * Cast away the const. This is read
929                                              * only so should be safe
930                                              */
931                                             (unsigned char *)data,
932                                             (size_t)datalen);
933     *p = OSSL_PARAM_construct_end();
934
935     return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
936 }
937
938 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
939                                       const unsigned char *sec, int seclen)
940 {
941     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
942                                           OSSL_KDF_PARAM_SECRET,
943                                           EVP_PKEY_OP_DERIVE,
944                                           EVP_PKEY_CTRL_TLS_SECRET,
945                                           sec, seclen);
946 }
947
948 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
949                                     const unsigned char *seed, int seedlen)
950 {
951     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
952                                           OSSL_KDF_PARAM_SEED,
953                                           EVP_PKEY_OP_DERIVE,
954                                           EVP_PKEY_CTRL_TLS_SEED,
955                                           seed, seedlen);
956 }
957
958 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
959 {
960     return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
961                                OSSL_KDF_PARAM_DIGEST,
962                                EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
963 }
964
965 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
966                                 const unsigned char *salt, int saltlen)
967 {
968     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
969                                           OSSL_KDF_PARAM_SALT,
970                                           EVP_PKEY_OP_DERIVE,
971                                           EVP_PKEY_CTRL_HKDF_SALT,
972                                           salt, saltlen);
973 }
974
975 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
976                                       const unsigned char *key, int keylen)
977 {
978     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
979                                           OSSL_KDF_PARAM_KEY,
980                                           EVP_PKEY_OP_DERIVE,
981                                           EVP_PKEY_CTRL_HKDF_KEY,
982                                           key, keylen);
983 }
984
985 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
986                                       const unsigned char *info, int infolen)
987 {
988     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
989                                           OSSL_KDF_PARAM_INFO,
990                                           EVP_PKEY_OP_DERIVE,
991                                           EVP_PKEY_CTRL_HKDF_INFO,
992                                           info, infolen);
993 }
994
995 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
996 {
997     OSSL_PARAM int_params[2], *p = int_params;
998
999     if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1000         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1001         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1002         return -2;
1003     }
1004
1005     /* TODO(3.0): Remove this eventually when no more legacy */
1006     if (ctx->op.kex.exchprovctx == NULL)
1007         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1008                                  EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1009
1010
1011     if (mode < 0) {
1012         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1013         return 0;
1014     }
1015
1016     *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1017     *p = OSSL_PARAM_construct_end();
1018
1019     return EVP_PKEY_CTX_set_params(ctx, int_params);
1020 }
1021
1022 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1023                                int passlen)
1024 {
1025     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1026                                           OSSL_KDF_PARAM_PASSWORD,
1027                                           EVP_PKEY_OP_DERIVE,
1028                                           EVP_PKEY_CTRL_PASS,
1029                                           (const unsigned char *)pass, passlen);
1030 }
1031
1032 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1033                                   const unsigned char *salt, int saltlen)
1034 {
1035     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1036                                           OSSL_KDF_PARAM_SALT,
1037                                           EVP_PKEY_OP_DERIVE,
1038                                           EVP_PKEY_CTRL_SCRYPT_SALT,
1039                                           salt, saltlen);
1040 }
1041
1042 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1043                                    int op, int ctrl, uint64_t val)
1044 {
1045     OSSL_PARAM uint64_params[2], *p = uint64_params;
1046
1047     if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1048         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1049         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1050         return -2;
1051     }
1052
1053     /* TODO(3.0): Remove this eventually when no more legacy */
1054     if (ctx->op.kex.exchprovctx == NULL)
1055         return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1056
1057     *p++ = OSSL_PARAM_construct_uint64(param, &val);
1058     *p = OSSL_PARAM_construct_end();
1059
1060     return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1061 }
1062
1063 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1064 {
1065     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1066                                    EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1067                                    n);
1068 }
1069
1070 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1071 {
1072     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1073                                    EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1074                                    r);
1075 }
1076
1077 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1078 {
1079     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1080                                    EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1081                                    p);
1082 }
1083
1084 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1085                                          uint64_t maxmem_bytes)
1086 {
1087     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1088                                    EVP_PKEY_OP_DERIVE,
1089                                    EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1090                                    maxmem_bytes);
1091 }
1092
1093 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1094                              int keylen)
1095 {
1096     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1097                                           OSSL_PKEY_PARAM_PRIV_KEY,
1098                                           EVP_PKEY_OP_KEYGEN,
1099                                           EVP_PKEY_CTRL_SET_MAC_KEY,
1100                                           key, keylen);
1101 }
1102
1103 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1104 {
1105     OSSL_PARAM params[2], *p = params;
1106
1107     if (ctx == NULL || op == NULL) {
1108         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1109         return 0;
1110     }
1111     if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1112         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1113         return -2;
1114     }
1115     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1116                                             (char *)op, 0);
1117     *p = OSSL_PARAM_construct_end();
1118     return EVP_PKEY_CTX_set_params(ctx, params);
1119 }
1120
1121 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
1122 {
1123     OSSL_PARAM params[2], *p = params;
1124     int ret;
1125
1126     if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1127         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1128         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1129         return -2;
1130     }
1131
1132     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
1133                                              /*
1134                                               * Cast away the const. This is
1135                                               * read only so should be safe
1136                                               */
1137                                              (void *)id, (size_t)len);
1138     *p++ = OSSL_PARAM_construct_end();
1139
1140     ret = evp_pkey_ctx_set_params_strict(ctx, params);
1141     if (ret == -2)
1142         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1143     return ret;
1144 }
1145
1146 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1147 {
1148     return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1149                              EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1150 }
1151
1152 static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
1153 {
1154     int ret;
1155     void *tmp_id = NULL;
1156     OSSL_PARAM params[2], *p = params;
1157
1158     if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1159         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1160         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1161         return -2;
1162     }
1163
1164     *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
1165                                           &tmp_id, 0);
1166     *p++ = OSSL_PARAM_construct_end();
1167
1168     ret = evp_pkey_ctx_get_params_strict(ctx, params);
1169     if (ret == -2) {
1170         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1171     } else if (ret > 0) {
1172         size_t tmp_id_len = params[0].return_size;
1173
1174         if (id != NULL)
1175             memcpy(id, tmp_id, tmp_id_len);
1176         if (id_len != NULL)
1177             *id_len = tmp_id_len;
1178     }
1179     return ret;
1180 }
1181
1182 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
1183 {
1184     return get1_id_data(ctx, id, NULL);
1185 }
1186
1187 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
1188 {
1189     return get1_id_data(ctx, NULL, id_len);
1190 }
1191
1192 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1193 {
1194     return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1195 }
1196
1197 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1198 {
1199     return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1200                              EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1201 }
1202
1203 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
1204                                 int cmd, int p1, void *p2)
1205 {
1206     switch (cmd) {
1207     case EVP_PKEY_CTRL_SET1_ID:
1208         return evp_pkey_ctx_set1_id_prov(ctx, p2, p1);
1209     case EVP_PKEY_CTRL_GET1_ID:
1210         return evp_pkey_ctx_get1_id_prov(ctx, p2);
1211     case EVP_PKEY_CTRL_GET1_ID_LEN:
1212         return evp_pkey_ctx_get1_id_len_prov(ctx, p2);
1213     }
1214
1215     if (keytype == EVP_PKEY_DHX) {
1216         switch (cmd) {
1217         case EVP_PKEY_CTRL_DH_KDF_TYPE:
1218             return EVP_PKEY_CTX_set_dh_kdf_type(ctx, p1);
1219         case EVP_PKEY_CTRL_DH_KDF_MD:
1220             return EVP_PKEY_CTX_set_dh_kdf_md(ctx, p2);
1221         case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
1222             return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, p1);
1223         case EVP_PKEY_CTRL_DH_KDF_UKM:
1224             return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p2, p1);
1225         case EVP_PKEY_CTRL_DH_KDF_OID:
1226             return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, p2);
1227         case EVP_PKEY_CTRL_GET_DH_KDF_MD:
1228             return EVP_PKEY_CTX_get_dh_kdf_md(ctx, p2);
1229         case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
1230             return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, p2);
1231         case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
1232             return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p2);
1233         case EVP_PKEY_CTRL_GET_DH_KDF_OID:
1234             return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, p2);
1235         }
1236     }
1237     if (keytype == EVP_PKEY_DH) {
1238         switch (cmd) {
1239             case EVP_PKEY_CTRL_DH_PAD:
1240                 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
1241             case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
1242                 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1);
1243             case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
1244                 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1);
1245             case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
1246                 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1);
1247             case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
1248                 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1);
1249             case EVP_PKEY_CTRL_DH_RFC5114:
1250                 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1);
1251         }
1252     }
1253     if (keytype == EVP_PKEY_DSA) {
1254         switch (cmd) {
1255         case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
1256             return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1);
1257         case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
1258             return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1);
1259         case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
1260             return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2);
1261         }
1262     }
1263     if (keytype == EVP_PKEY_EC) {
1264         switch (cmd) {
1265         case EVP_PKEY_CTRL_EC_PARAM_ENC:
1266             return evp_pkey_ctx_set_ec_param_enc_prov(ctx, p1);
1267         case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
1268             return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
1269         case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
1270             if (p1 == -2) {
1271                 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
1272             } else if (p1 < -1 || p1 > 1) {
1273                 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1274                 return -2;
1275             } else {
1276                 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
1277             }
1278         case EVP_PKEY_CTRL_EC_KDF_TYPE:
1279             if (p1 == -2) {
1280                 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
1281             } else {
1282                 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
1283             }
1284         case EVP_PKEY_CTRL_GET_EC_KDF_MD:
1285             return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
1286         case EVP_PKEY_CTRL_EC_KDF_MD:
1287             return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
1288         case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
1289             return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
1290         case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
1291             return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
1292         case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
1293             return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
1294         case EVP_PKEY_CTRL_EC_KDF_UKM:
1295             return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
1296         }
1297     }
1298     if (keytype == EVP_PKEY_RSA) {
1299         switch (cmd) {
1300         case EVP_PKEY_CTRL_RSA_OAEP_MD:
1301             return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1302         case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
1303             return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1304         case EVP_PKEY_CTRL_RSA_MGF1_MD:
1305             return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1306         case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
1307             return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
1308         case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
1309             return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
1310         case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
1311             return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
1312         case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
1313             return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
1314         case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
1315             return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
1316         }
1317     }
1318     /*
1319      * keytype == -1 is used when several key types share the same structure,
1320      * or for generic controls that are the same across multiple key types.
1321      */
1322     if (keytype == -1) {
1323         if (optype == EVP_PKEY_OP_DERIVE) {
1324             switch (cmd) {
1325             /* TLS1-PRF */
1326             case EVP_PKEY_CTRL_TLS_MD:
1327                 return EVP_PKEY_CTX_set_tls1_prf_md(ctx, p2);
1328             case EVP_PKEY_CTRL_TLS_SECRET:
1329                 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx, p2, p1);
1330             case EVP_PKEY_CTRL_TLS_SEED:
1331                 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx, p2, p1);
1332
1333             /* HKDF */
1334             case EVP_PKEY_CTRL_HKDF_MD:
1335                 return EVP_PKEY_CTX_set_hkdf_md(ctx, p2);
1336             case EVP_PKEY_CTRL_HKDF_SALT :
1337                 return EVP_PKEY_CTX_set1_hkdf_salt(ctx, p2, p1);
1338             case EVP_PKEY_CTRL_HKDF_KEY:
1339                 return EVP_PKEY_CTX_set1_hkdf_key(ctx, p2, p1);
1340             case EVP_PKEY_CTRL_HKDF_INFO:
1341                 return EVP_PKEY_CTX_add1_hkdf_info(ctx, p2, p1);
1342             case EVP_PKEY_CTRL_HKDF_MODE:
1343                 return EVP_PKEY_CTX_hkdf_mode(ctx, p1);
1344
1345             /* Scrypt */
1346             case EVP_PKEY_CTRL_PASS:
1347                 return EVP_PKEY_CTX_set1_pbe_pass(ctx, p2, p1);
1348             case EVP_PKEY_CTRL_SCRYPT_SALT:
1349                 return EVP_PKEY_CTX_set1_scrypt_salt(ctx, p2, p1);
1350             case EVP_PKEY_CTRL_SCRYPT_N:
1351                 return EVP_PKEY_CTX_set_scrypt_N(ctx, p1);
1352             case EVP_PKEY_CTRL_SCRYPT_R:
1353                 return EVP_PKEY_CTX_set_scrypt_r(ctx, p1);
1354             case EVP_PKEY_CTRL_SCRYPT_P:
1355                 return EVP_PKEY_CTX_set_scrypt_p(ctx, p1);
1356             case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
1357                 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx, p1);
1358             }
1359         } else if (optype == EVP_PKEY_OP_KEYGEN) {
1360             OSSL_PARAM params[2], *p = params;
1361
1362             switch (cmd) {
1363             case EVP_PKEY_CTRL_CIPHER:
1364                 {
1365                     char *ciphname = (char *)EVP_CIPHER_name(p2);
1366
1367                     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
1368                                                             ciphname, 0);
1369                     *p = OSSL_PARAM_construct_end();
1370
1371                     return EVP_PKEY_CTX_set_params(ctx, params);
1372                 }
1373             case EVP_PKEY_CTRL_SET_MAC_KEY:
1374                 {
1375                     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
1376                                                              p2, p1);
1377                     *p = OSSL_PARAM_construct_end();
1378
1379                     return EVP_PKEY_CTX_set_params(ctx, params);
1380                 }
1381             }
1382         }
1383         switch (cmd) {
1384         case EVP_PKEY_CTRL_MD:
1385             return EVP_PKEY_CTX_set_signature_md(ctx, p2);
1386         case EVP_PKEY_CTRL_GET_MD:
1387             return EVP_PKEY_CTX_get_signature_md(ctx, p2);
1388         case EVP_PKEY_CTRL_RSA_PADDING:
1389             return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
1390         case EVP_PKEY_CTRL_GET_RSA_PADDING:
1391             return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
1392         case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
1393             return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1394         case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
1395             return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
1396         case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
1397             return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
1398         case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
1399         case EVP_PKEY_CTRL_PKCS7_DECRYPT:
1400 # ifndef OPENSSL_NO_CMS
1401         case EVP_PKEY_CTRL_CMS_DECRYPT:
1402         case EVP_PKEY_CTRL_CMS_ENCRYPT:
1403 # endif
1404             /* TODO (3.0) Temporary hack, this should probe */
1405             if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
1406                 return 1;
1407             ERR_raise(ERR_LIB_EVP,
1408                       EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1409             return -2;
1410         }
1411     }
1412
1413     /*
1414      * GOST CMS format is different for different cipher algorithms.
1415      * Most of other algorithms don't have such a difference
1416      * so this ctrl is just ignored.
1417      */
1418     if (cmd == EVP_PKEY_CTRL_CIPHER)
1419         return -2;
1420
1421     return 0;
1422 }
1423
1424 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1425                                  int cmd, int p1, void *p2)
1426 {
1427     int ret = 0;
1428
1429     /*
1430      * If the method has a |digest_custom| function, we can relax the
1431      * operation type check, since this can be called before the operation
1432      * is initialized.
1433      */
1434     if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1435         if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1436             ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1437             return -1;
1438         }
1439
1440         if ((optype != -1) && !(ctx->operation & optype)) {
1441             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1442             return -1;
1443         }
1444     }
1445
1446     switch (evp_pkey_ctx_state(ctx)) {
1447     case EVP_PKEY_STATE_PROVIDER:
1448         return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1449     case EVP_PKEY_STATE_UNKNOWN:
1450     case EVP_PKEY_STATE_LEGACY:
1451         if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1452             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1453             return -2;
1454         }
1455         if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1456             return -1;
1457
1458         ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1459
1460         if (ret == -2)
1461             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1462         break;
1463     }
1464     return ret;
1465 }
1466
1467 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1468                       int cmd, int p1, void *p2)
1469 {
1470     int ret = 0;
1471
1472     if (ctx == NULL) {
1473         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1474         return -2;
1475     }
1476     /* If unsupported, we don't want that reported here */
1477     ERR_set_mark();
1478     ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1479                                          cmd, NULL, p2, p1);
1480     if (ret == -2) {
1481         ERR_pop_to_mark();
1482     } else {
1483         ERR_clear_last_mark();
1484         /*
1485          * If there was an error, there was an error.
1486          * If the operation isn't initialized yet, we also return, as
1487          * the saved values will be used then anyway.
1488          */
1489         if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1490             return ret;
1491     }
1492     return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1493 }
1494
1495 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1496                              int cmd, uint64_t value)
1497 {
1498     return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1499 }
1500
1501 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
1502                                     const char *value)
1503 {
1504     if (strcmp(name, "md") == 0)
1505         name = OSSL_ALG_PARAM_DIGEST;
1506     else if (strcmp(name, "rsa_padding_mode") == 0)
1507         name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1508     else if (strcmp(name, "rsa_mgf1_md") == 0)
1509         name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1510     else if (strcmp(name, "rsa_oaep_md") == 0)
1511         name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1512     else if (strcmp(name, "rsa_oaep_label") == 0)
1513         name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
1514     else if (strcmp(name, "rsa_pss_saltlen") == 0)
1515         name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
1516     else if (strcmp(name, "rsa_keygen_bits") == 0)
1517         name = OSSL_PKEY_PARAM_RSA_BITS;
1518     else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1519         name = OSSL_PKEY_PARAM_RSA_E;
1520     else if (strcmp(name, "rsa_keygen_primes") == 0)
1521         name = OSSL_PKEY_PARAM_RSA_PRIMES;
1522     else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1523         name = OSSL_PKEY_PARAM_RSA_DIGEST;
1524     else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1525         name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1526     else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1527         name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
1528     else if (strcmp(name, "dsa_paramgen_bits") == 0)
1529         name = OSSL_PKEY_PARAM_FFC_PBITS;
1530     else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1531         name = OSSL_PKEY_PARAM_FFC_QBITS;
1532     else if (strcmp(name, "dsa_paramgen_md") == 0)
1533         name = OSSL_PKEY_PARAM_FFC_DIGEST;
1534     else if (strcmp(name, "dh_paramgen_generator") == 0)
1535         name = OSSL_PKEY_PARAM_DH_GENERATOR;
1536     else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1537         name = OSSL_PKEY_PARAM_FFC_PBITS;
1538     else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1539         name = OSSL_PKEY_PARAM_FFC_QBITS;
1540     else if (strcmp(name, "dh_paramgen_type") == 0) {
1541         name = OSSL_PKEY_PARAM_FFC_TYPE;
1542         value = dh_gen_type_id2name(atoi(value));
1543     } else if (strcmp(name, "dh_param") == 0)
1544         name = OSSL_PKEY_PARAM_GROUP_NAME;
1545     else if (strcmp(name, "dh_rfc5114") == 0) {
1546         int num = atoi(value);
1547
1548         name = OSSL_PKEY_PARAM_GROUP_NAME;
1549         value =
1550             ossl_ffc_named_group_get_name(ossl_ffc_uid_to_dh_named_group(num));
1551     } else if (strcmp(name, "dh_pad") == 0)
1552         name = OSSL_EXCHANGE_PARAM_PAD;
1553     else if (strcmp(name, "ec_paramgen_curve") == 0)
1554         name = OSSL_PKEY_PARAM_GROUP_NAME;
1555     else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1556         name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1557     else if (strcmp(name, "ecdh_kdf_md") == 0)
1558         name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
1559     else if (strcmp(name, "ec_param_enc") == 0)
1560         name = OSSL_PKEY_PARAM_EC_ENCODING;
1561     else if (strcmp(name, "N") == 0)
1562         name = OSSL_KDF_PARAM_SCRYPT_N;
1563
1564     {
1565         /*
1566          * TODO(3.0) reduce the code above to only translate known legacy
1567          * string to the corresponding core name (see core_names.h), but
1568          * otherwise leave it to this code block to do the actual work.
1569          */
1570         const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1571         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1572         int rv = 0;
1573         int exists = 0;
1574
1575         if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
1576                                            strlen(value), &exists)) {
1577             if (!exists) {
1578                 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1579                                "name=%s,value=%s", name, value);
1580                 return -2;
1581             }
1582             return 0;
1583         }
1584         if (EVP_PKEY_CTX_set_params(ctx, params))
1585             rv = 1;
1586         OPENSSL_free(params[0].data);
1587         return rv;
1588     }
1589 }
1590
1591 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1592                                      const char *name, const char *value)
1593 {
1594     int ret = 0;
1595
1596     if (ctx == NULL) {
1597         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1598         return -2;
1599     }
1600
1601     switch (evp_pkey_ctx_state(ctx)) {
1602     case EVP_PKEY_STATE_PROVIDER:
1603         return legacy_ctrl_str_to_param(ctx, name, value);
1604     case EVP_PKEY_STATE_UNKNOWN:
1605     case EVP_PKEY_STATE_LEGACY:
1606         if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1607             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1608             return -2;
1609         }
1610         if (strcmp(name, "digest") == 0)
1611             ret = EVP_PKEY_CTX_md(ctx,
1612                                   EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1613                                   EVP_PKEY_CTRL_MD, value);
1614         else
1615             ret = ctx->pmeth->ctrl_str(ctx, name, value);
1616         break;
1617     }
1618
1619     return ret;
1620 }
1621
1622 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1623                           const char *name, const char *value)
1624 {
1625     int ret = 0;
1626
1627     /* If unsupported, we don't want that reported here */
1628     ERR_set_mark();
1629     ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1630                                          name, value, strlen(value) + 1);
1631     if (ret == -2) {
1632         ERR_pop_to_mark();
1633     } else {
1634         ERR_clear_last_mark();
1635         /*
1636          * If there was an error, there was an error.
1637          * If the operation isn't initialized yet, we also return, as
1638          * the saved values will be used then anyway.
1639          */
1640         if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1641             return ret;
1642     }
1643
1644     return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1645 }
1646
1647 static int decode_cmd(int cmd, const char *name)
1648 {
1649     if (cmd == -1) {
1650         /*
1651          * The consequence of the assertion not being true is that this
1652          * function will return -1, which will cause the calling functions
1653          * to signal that the command is unsupported...  in non-debug mode.
1654          */
1655         if (ossl_assert(name != NULL))
1656             if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1657                 cmd = EVP_PKEY_CTRL_SET1_ID;
1658     }
1659
1660     return cmd;
1661 }
1662
1663 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1664                                           int keytype, int optype,
1665                                           int cmd, const char *name,
1666                                           const void *data, size_t data_len)
1667 {
1668     if ((keytype != -1 && ctx->pmeth->pkey_id != keytype)
1669         || ((optype != -1) && !(ctx->operation & optype))) {
1670         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1671         return -1;
1672     }
1673
1674     cmd = decode_cmd(cmd, name);
1675     switch (cmd) {
1676     case EVP_PKEY_CTRL_SET1_ID:
1677         evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1678         if (name != NULL) {
1679             ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1680             if (ctx->cached_parameters.dist_id_name == NULL) {
1681                 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1682                 return 0;
1683             }
1684         }
1685         if (data_len > 0) {
1686             ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1687             if (ctx->cached_parameters.dist_id == NULL) {
1688                 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1689                 return 0;
1690             }
1691         }
1692         ctx->cached_parameters.dist_id_set = 1;
1693         ctx->cached_parameters.dist_id_len = data_len;
1694         return 1;
1695     }
1696
1697     ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1698     return -2;
1699 }
1700
1701 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1702                                           int cmd, const char *name)
1703 {
1704     cmd = decode_cmd(cmd, name);
1705     switch (cmd) {
1706     case EVP_PKEY_CTRL_SET1_ID:
1707         OPENSSL_free(ctx->cached_parameters.dist_id);
1708         OPENSSL_free(ctx->cached_parameters.dist_id_name);
1709         ctx->cached_parameters.dist_id = NULL;
1710         ctx->cached_parameters.dist_id_name = NULL;
1711         break;
1712     }
1713 }
1714
1715 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1716 {
1717     evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1718 }
1719
1720 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1721 {
1722     int ret = 1;
1723
1724     if (ret && ctx->cached_parameters.dist_id_set) {
1725         const char *name = ctx->cached_parameters.dist_id_name;
1726         const void *val = ctx->cached_parameters.dist_id;
1727         size_t len = ctx->cached_parameters.dist_id_len;
1728
1729         if (name != NULL)
1730             ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1731         else
1732             ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1733                                         EVP_PKEY_CTRL_SET1_ID,
1734                                         (int)len, (void *)val);
1735     }
1736
1737     return ret;
1738 }
1739
1740 OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1741 {
1742     return ctx->libctx;
1743 }
1744
1745 const char *EVP_PKEY_CTX_get0_propq(EVP_PKEY_CTX *ctx)
1746 {
1747     return ctx->propquery;
1748 }
1749
1750 /* Utility functions to send a string of hex string to a ctrl */
1751
1752 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1753 {
1754     size_t len;
1755
1756     len = strlen(str);
1757     if (len > INT_MAX)
1758         return -1;
1759     return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1760 }
1761
1762 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1763 {
1764     unsigned char *bin;
1765     long binlen;
1766     int rv = -1;
1767
1768     bin = OPENSSL_hexstr2buf(hex, &binlen);
1769     if (bin == NULL)
1770         return 0;
1771     if (binlen <= INT_MAX)
1772         rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1773     OPENSSL_free(bin);
1774     return rv;
1775 }
1776
1777 /* Pass a message digest to a ctrl */
1778 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1779 {
1780     const EVP_MD *m;
1781
1782     if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1783         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1784         return 0;
1785     }
1786     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1787 }
1788
1789 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1790 {
1791     return ctx->operation;
1792 }
1793
1794 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1795 {
1796     ctx->keygen_info = dat;
1797     ctx->keygen_info_count = datlen;
1798 }
1799
1800 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1801 {
1802     ctx->data = data;
1803 }
1804
1805 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1806 {
1807     return ctx->data;
1808 }
1809
1810 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1811 {
1812     return ctx->pkey;
1813 }
1814
1815 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1816 {
1817     return ctx->peerkey;
1818 }
1819
1820 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1821 {
1822     ctx->app_data = data;
1823 }
1824
1825 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1826 {
1827     return ctx->app_data;
1828 }
1829
1830 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1831                             int (*init) (EVP_PKEY_CTX *ctx))
1832 {
1833     pmeth->init = init;
1834 }
1835
1836 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1837                             int (*copy) (EVP_PKEY_CTX *dst,
1838                                          const EVP_PKEY_CTX *src))
1839 {
1840     pmeth->copy = copy;
1841 }
1842
1843 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1844                                void (*cleanup) (EVP_PKEY_CTX *ctx))
1845 {
1846     pmeth->cleanup = cleanup;
1847 }
1848
1849 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1850                                 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1851                                 int (*paramgen) (EVP_PKEY_CTX *ctx,
1852                                                  EVP_PKEY *pkey))
1853 {
1854     pmeth->paramgen_init = paramgen_init;
1855     pmeth->paramgen = paramgen;
1856 }
1857
1858 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1859                               int (*keygen_init) (EVP_PKEY_CTX *ctx),
1860                               int (*keygen) (EVP_PKEY_CTX *ctx,
1861                                              EVP_PKEY *pkey))
1862 {
1863     pmeth->keygen_init = keygen_init;
1864     pmeth->keygen = keygen;
1865 }
1866
1867 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1868                             int (*sign_init) (EVP_PKEY_CTX *ctx),
1869                             int (*sign) (EVP_PKEY_CTX *ctx,
1870                                          unsigned char *sig, size_t *siglen,
1871                                          const unsigned char *tbs,
1872                                          size_t tbslen))
1873 {
1874     pmeth->sign_init = sign_init;
1875     pmeth->sign = sign;
1876 }
1877
1878 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1879                               int (*verify_init) (EVP_PKEY_CTX *ctx),
1880                               int (*verify) (EVP_PKEY_CTX *ctx,
1881                                              const unsigned char *sig,
1882                                              size_t siglen,
1883                                              const unsigned char *tbs,
1884                                              size_t tbslen))
1885 {
1886     pmeth->verify_init = verify_init;
1887     pmeth->verify = verify;
1888 }
1889
1890 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1891                                       int (*verify_recover_init) (EVP_PKEY_CTX
1892                                                                   *ctx),
1893                                       int (*verify_recover) (EVP_PKEY_CTX
1894                                                              *ctx,
1895                                                              unsigned char
1896                                                              *sig,
1897                                                              size_t *siglen,
1898                                                              const unsigned
1899                                                              char *tbs,
1900                                                              size_t tbslen))
1901 {
1902     pmeth->verify_recover_init = verify_recover_init;
1903     pmeth->verify_recover = verify_recover;
1904 }
1905
1906 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1907                                int (*signctx_init) (EVP_PKEY_CTX *ctx,
1908                                                     EVP_MD_CTX *mctx),
1909                                int (*signctx) (EVP_PKEY_CTX *ctx,
1910                                                unsigned char *sig,
1911                                                size_t *siglen,
1912                                                EVP_MD_CTX *mctx))
1913 {
1914     pmeth->signctx_init = signctx_init;
1915     pmeth->signctx = signctx;
1916 }
1917
1918 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1919                                  int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1920                                                         EVP_MD_CTX *mctx),
1921                                  int (*verifyctx) (EVP_PKEY_CTX *ctx,
1922                                                    const unsigned char *sig,
1923                                                    int siglen,
1924                                                    EVP_MD_CTX *mctx))
1925 {
1926     pmeth->verifyctx_init = verifyctx_init;
1927     pmeth->verifyctx = verifyctx;
1928 }
1929
1930 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1931                                int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1932                                int (*encryptfn) (EVP_PKEY_CTX *ctx,
1933                                                  unsigned char *out,
1934                                                  size_t *outlen,
1935                                                  const unsigned char *in,
1936                                                  size_t inlen))
1937 {
1938     pmeth->encrypt_init = encrypt_init;
1939     pmeth->encrypt = encryptfn;
1940 }
1941
1942 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1943                                int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1944                                int (*decrypt) (EVP_PKEY_CTX *ctx,
1945                                                unsigned char *out,
1946                                                size_t *outlen,
1947                                                const unsigned char *in,
1948                                                size_t inlen))
1949 {
1950     pmeth->decrypt_init = decrypt_init;
1951     pmeth->decrypt = decrypt;
1952 }
1953
1954 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1955                               int (*derive_init) (EVP_PKEY_CTX *ctx),
1956                               int (*derive) (EVP_PKEY_CTX *ctx,
1957                                              unsigned char *key,
1958                                              size_t *keylen))
1959 {
1960     pmeth->derive_init = derive_init;
1961     pmeth->derive = derive;
1962 }
1963
1964 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1965                             int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1966                                          void *p2),
1967                             int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1968                                              const char *type,
1969                                              const char *value))
1970 {
1971     pmeth->ctrl = ctrl;
1972     pmeth->ctrl_str = ctrl_str;
1973 }
1974
1975 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1976     int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1977                        const unsigned char *tbs, size_t tbslen))
1978 {
1979     pmeth->digestsign = digestsign;
1980 }
1981
1982 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1983     int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1984                          size_t siglen, const unsigned char *tbs,
1985                          size_t tbslen))
1986 {
1987     pmeth->digestverify = digestverify;
1988 }
1989
1990 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1991                              int (*check) (EVP_PKEY *pkey))
1992 {
1993     pmeth->check = check;
1994 }
1995
1996 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1997                                     int (*check) (EVP_PKEY *pkey))
1998 {
1999     pmeth->public_check = check;
2000 }
2001
2002 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
2003                                    int (*check) (EVP_PKEY *pkey))
2004 {
2005     pmeth->param_check = check;
2006 }
2007
2008 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
2009                                      int (*digest_custom) (EVP_PKEY_CTX *ctx,
2010                                                            EVP_MD_CTX *mctx))
2011 {
2012     pmeth->digest_custom = digest_custom;
2013 }
2014
2015 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
2016                             int (**pinit) (EVP_PKEY_CTX *ctx))
2017 {
2018     *pinit = pmeth->init;
2019 }
2020
2021 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
2022                             int (**pcopy) (EVP_PKEY_CTX *dst,
2023                                            const EVP_PKEY_CTX *src))
2024 {
2025     *pcopy = pmeth->copy;
2026 }
2027
2028 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
2029                                void (**pcleanup) (EVP_PKEY_CTX *ctx))
2030 {
2031     *pcleanup = pmeth->cleanup;
2032 }
2033
2034 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
2035                                 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
2036                                 int (**pparamgen) (EVP_PKEY_CTX *ctx,
2037                                                    EVP_PKEY *pkey))
2038 {
2039     if (pparamgen_init)
2040         *pparamgen_init = pmeth->paramgen_init;
2041     if (pparamgen)
2042         *pparamgen = pmeth->paramgen;
2043 }
2044
2045 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
2046                               int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
2047                               int (**pkeygen) (EVP_PKEY_CTX *ctx,
2048                                                EVP_PKEY *pkey))
2049 {
2050     if (pkeygen_init)
2051         *pkeygen_init = pmeth->keygen_init;
2052     if (pkeygen)
2053         *pkeygen = pmeth->keygen;
2054 }
2055
2056 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
2057                             int (**psign_init) (EVP_PKEY_CTX *ctx),
2058                             int (**psign) (EVP_PKEY_CTX *ctx,
2059                                            unsigned char *sig, size_t *siglen,
2060                                            const unsigned char *tbs,
2061                                            size_t tbslen))
2062 {
2063     if (psign_init)
2064         *psign_init = pmeth->sign_init;
2065     if (psign)
2066         *psign = pmeth->sign;
2067 }
2068
2069 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
2070                               int (**pverify_init) (EVP_PKEY_CTX *ctx),
2071                               int (**pverify) (EVP_PKEY_CTX *ctx,
2072                                                const unsigned char *sig,
2073                                                size_t siglen,
2074                                                const unsigned char *tbs,
2075                                                size_t tbslen))
2076 {
2077     if (pverify_init)
2078         *pverify_init = pmeth->verify_init;
2079     if (pverify)
2080         *pverify = pmeth->verify;
2081 }
2082
2083 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
2084                                       int (**pverify_recover_init) (EVP_PKEY_CTX
2085                                                                     *ctx),
2086                                       int (**pverify_recover) (EVP_PKEY_CTX
2087                                                                *ctx,
2088                                                                unsigned char
2089                                                                *sig,
2090                                                                size_t *siglen,
2091                                                                const unsigned
2092                                                                char *tbs,
2093                                                                size_t tbslen))
2094 {
2095     if (pverify_recover_init)
2096         *pverify_recover_init = pmeth->verify_recover_init;
2097     if (pverify_recover)
2098         *pverify_recover = pmeth->verify_recover;
2099 }
2100
2101 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
2102                                int (**psignctx_init) (EVP_PKEY_CTX *ctx,
2103                                                       EVP_MD_CTX *mctx),
2104                                int (**psignctx) (EVP_PKEY_CTX *ctx,
2105                                                  unsigned char *sig,
2106                                                  size_t *siglen,
2107                                                  EVP_MD_CTX *mctx))
2108 {
2109     if (psignctx_init)
2110         *psignctx_init = pmeth->signctx_init;
2111     if (psignctx)
2112         *psignctx = pmeth->signctx;
2113 }
2114
2115 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
2116                                  int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
2117                                                           EVP_MD_CTX *mctx),
2118                                  int (**pverifyctx) (EVP_PKEY_CTX *ctx,
2119                                                      const unsigned char *sig,
2120                                                      int siglen,
2121                                                      EVP_MD_CTX *mctx))
2122 {
2123     if (pverifyctx_init)
2124         *pverifyctx_init = pmeth->verifyctx_init;
2125     if (pverifyctx)
2126         *pverifyctx = pmeth->verifyctx;
2127 }
2128
2129 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
2130                                int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
2131                                int (**pencryptfn) (EVP_PKEY_CTX *ctx,
2132                                                    unsigned char *out,
2133                                                    size_t *outlen,
2134                                                    const unsigned char *in,
2135                                                    size_t inlen))
2136 {
2137     if (pencrypt_init)
2138         *pencrypt_init = pmeth->encrypt_init;
2139     if (pencryptfn)
2140         *pencryptfn = pmeth->encrypt;
2141 }
2142
2143 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
2144                                int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
2145                                int (**pdecrypt) (EVP_PKEY_CTX *ctx,
2146                                                  unsigned char *out,
2147                                                  size_t *outlen,
2148                                                  const unsigned char *in,
2149                                                  size_t inlen))
2150 {
2151     if (pdecrypt_init)
2152         *pdecrypt_init = pmeth->decrypt_init;
2153     if (pdecrypt)
2154         *pdecrypt = pmeth->decrypt;
2155 }
2156
2157 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
2158                               int (**pderive_init) (EVP_PKEY_CTX *ctx),
2159                               int (**pderive) (EVP_PKEY_CTX *ctx,
2160                                                unsigned char *key,
2161                                                size_t *keylen))
2162 {
2163     if (pderive_init)
2164         *pderive_init = pmeth->derive_init;
2165     if (pderive)
2166         *pderive = pmeth->derive;
2167 }
2168
2169 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
2170                             int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2171                                            void *p2),
2172                             int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2173                                                const char *type,
2174                                                const char *value))
2175 {
2176     if (pctrl)
2177         *pctrl = pmeth->ctrl;
2178     if (pctrl_str)
2179         *pctrl_str = pmeth->ctrl_str;
2180 }
2181
2182 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
2183     int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2184                         const unsigned char *tbs, size_t tbslen))
2185 {
2186     if (digestsign)
2187         *digestsign = pmeth->digestsign;
2188 }
2189
2190 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
2191     int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2192                           size_t siglen, const unsigned char *tbs,
2193                           size_t tbslen))
2194 {
2195     if (digestverify)
2196         *digestverify = pmeth->digestverify;
2197 }
2198
2199 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2200                              int (**pcheck) (EVP_PKEY *pkey))
2201 {
2202     if (pcheck != NULL)
2203         *pcheck = pmeth->check;
2204 }
2205
2206 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2207                                     int (**pcheck) (EVP_PKEY *pkey))
2208 {
2209     if (pcheck != NULL)
2210         *pcheck = pmeth->public_check;
2211 }
2212
2213 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2214                                    int (**pcheck) (EVP_PKEY *pkey))
2215 {
2216     if (pcheck != NULL)
2217         *pcheck = pmeth->param_check;
2218 }
2219
2220 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
2221                                      int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2222                                                              EVP_MD_CTX *mctx))
2223 {
2224     if (pdigest_custom != NULL)
2225         *pdigest_custom = pmeth->digest_custom;
2226 }
2227
2228 #endif /* FIPS_MODULE */