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