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