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