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