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