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