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