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