EVP_PKEY_CTRL_CIPHER can be used with encrypt/decrypt with GOST
[openssl.git] / crypto / evp / ctrl_params_translate.c
1 /*
2  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * Some ctrls depend on deprecated functionality.  We trust that this is
12  * functionality that remains internally even when 'no-deprecated' is
13  * configured.  When we drop #legacy EVP_PKEYs, this source should be
14  * possible to drop as well.
15  */
16 #include "internal/deprecated.h"
17
18 #include <string.h>
19
20 /* The following includes get us all the EVP_PKEY_CTRL macros */
21 #include <openssl/dh.h>
22 #include <openssl/dsa.h>
23 #include <openssl/ec.h>
24 #include <openssl/rsa.h>
25 #include <openssl/kdf.h>
26
27 /* This include gets us all the OSSL_PARAM key string macros */
28 #include <openssl/core_names.h>
29
30 #include <openssl/err.h>
31 #include <openssl/evperr.h>
32 #include <openssl/params.h>
33 #include "internal/nelem.h"
34 #include "internal/cryptlib.h"
35 #include "internal/ffc.h"
36 #include "crypto/evp.h"
37 #include "crypto/dh.h"
38 #include "crypto/ec.h"
39
40 #include "e_os.h"                /* strcasecmp() for Windows */
41
42 struct translation_ctx_st;       /* Forwarding */
43 struct translation_st;           /* Forwarding */
44
45 /*
46  * The fixup_args functions are called with the following parameters:
47  *
48  * |state|              The state we're called in, explained further at the
49  *                      end of this comment.
50  * |translation|        The translation item, to be pilfered for data as
51  *                      necessary.
52  * |ctx|                The translation context, which contains copies of
53  *                      the following arguments, applicable according to
54  *                      the caller.  All of the attributes in this context
55  *                      may be freely modified by the fixup_args function.
56  *                      For cleanup, call cleanup_translation_ctx().
57  *
58  * The |state| tells the fixup_args function something about the caller and
59  * what they may expect:
60  *
61  * PKEY                         The fixup_args function has been called
62  *                              from an EVP_PKEY payload getter / setter,
63  *                              and is fully responsible for getting or
64  *                              setting the requested data.  With this
65  *                              state, the fixup_args function is expected
66  *                              to use or modify |*params|, depending on
67  *                              |action_type|.
68  *
69  * PRE_CTRL_TO_PARAMS           The fixup_args function has been called
70  * POST_CTRL_TO_PARAMS          from EVP_PKEY_CTX_ctrl(), to help with
71  *                              translating the ctrl data to an OSSL_PARAM
72  *                              element or back.  The calling sequence is
73  *                              as follows:
74  *
75  *                              1. fixup_args(PRE_CTRL_TO_PARAMS, ...)
76  *                              2. EVP_PKEY_CTX_set_params() or
77  *                                 EVP_PKEY_CTX_get_params()
78  *                              3. fixup_args(POST_CTRL_TO_PARAMS, ...)
79  *
80  *                              With the PRE_CTRL_TO_PARAMS state, the
81  *                              fixup_args function is expected to modify
82  *                              the passed |*params| in whatever way
83  *                              necessary, when |action_type == SET|.
84  *                              With the POST_CTRL_TO_PARAMS state, the
85  *                              fixup_args function is expected to modify
86  *                              the passed |p2| in whatever way necessary,
87  *                              when |action_type == GET|.
88  *
89  *                              The return value from the fixup_args call
90  *                              with the POST_CTRL_TO_PARAMS state becomes
91  *                              the return value back to EVP_PKEY_CTX_ctrl().
92  *
93  * CLEANUP_CTRL_TO_PARAMS       The cleanup_args functions has been called
94  *                              from EVP_PKEY_CTX_ctrl(), to clean up what
95  *                              the fixup_args function has done, if needed.
96  *
97  *
98  * PRE_CTRL_STR_TO_PARAMS       The fixup_args function has been called
99  * POST_CTRL_STR_TO_PARAMS      from EVP_PKEY_CTX_ctrl_str(), to help with
100  *                              translating the ctrl_str data to an
101  *                              OSSL_PARAM element or back.  The calling
102  *                              sequence is as follows:
103  *
104  *                              1. fixup_args(PRE_CTRL_STR_TO_PARAMS, ...)
105  *                              2. EVP_PKEY_CTX_set_params() or
106  *                                 EVP_PKEY_CTX_get_params()
107  *                              3. fixup_args(POST_CTRL_STR_TO_PARAMS, ...)
108  *
109  *                              With the PRE_CTRL_STR_TO_PARAMS state,
110  *                              the fixup_args function is expected to
111  *                              modify the passed |*params| in whatever
112  *                              way necessary, when |action_type == SET|.
113  *                              With the POST_CTRL_STR_TO_PARAMS state,
114  *                              the fixup_args function is only expected
115  *                              to return a value.
116  *
117  * CLEANUP_CTRL_STR_TO_PARAMS   The cleanup_args functions has been called
118  *                              from EVP_PKEY_CTX_ctrl_str(), to clean up
119  *                              what the fixup_args function has done, if
120  *                              needed.
121  *
122  * PRE_PARAMS_TO_CTRL           The fixup_args function has been called
123  * POST_PARAMS_TO_CTRL          from EVP_PKEY_CTX_get_params() or
124  *                              EVP_PKEY_CTX_set_params(), to help with
125  *                              translating the OSSL_PARAM data to the
126  *                              corresponding EVP_PKEY_CTX_ctrl() arguments
127  *                              or the other way around.  The calling
128  *                              sequence is as follows:
129  *
130  *                              1. fixup_args(PRE_PARAMS_TO_CTRL, ...)
131  *                              2. EVP_PKEY_CTX_ctrl()
132  *                              3. fixup_args(POST_PARAMS_TO_CTRL, ...)
133  *
134  *                              With the PRE_PARAMS_TO_CTRL state, the
135  *                              fixup_args function is expected to modify
136  *                              the passed |p1| and |p2| in whatever way
137  *                              necessary, when |action_type == SET|.
138  *                              With the POST_PARAMS_TO_CTRL state, the
139  *                              fixup_args function is expected to
140  *                              modify the passed |*params| in whatever
141  *                              way necessary, when |action_type == GET|.
142  *
143  * CLEANUP_PARAMS_TO_CTRL       The cleanup_args functions has been called
144  *                              from EVP_PKEY_CTX_get_params() or
145  *                              EVP_PKEY_CTX_set_params(), to clean up what
146  *                              the fixup_args function has done, if needed.
147  */
148 enum state {
149     PKEY,
150     PRE_CTRL_TO_PARAMS, POST_CTRL_TO_PARAMS, CLEANUP_CTRL_TO_PARAMS,
151     PRE_CTRL_STR_TO_PARAMS, POST_CTRL_STR_TO_PARAMS, CLEANUP_CTRL_STR_TO_PARAMS,
152     PRE_PARAMS_TO_CTRL, POST_PARAMS_TO_CTRL, CLEANUP_PARAMS_TO_CTRL
153 };
154 enum action {
155     NONE = 0, GET = 1, SET = 2
156 };
157 typedef int fixup_args_fn(enum state state,
158                           const struct translation_st *translation,
159                           struct translation_ctx_st *ctx);
160 typedef int cleanup_args_fn(enum state state,
161                             const struct translation_st *translation,
162                             struct translation_ctx_st *ctx);
163
164 struct translation_ctx_st {
165     /*
166      * The EVP_PKEY_CTX, for calls on that structure, to be pilfered for data
167      * as necessary.
168      */
169     EVP_PKEY_CTX *pctx;
170     /*
171      * The action type (GET or SET).  This may be 0 in some cases, and should
172      * be modified by the fixup_args function in the PRE states.  It should
173      * otherwise remain untouched once set.
174      */
175     enum action action_type;
176     /*
177      * For ctrl to params translation, the actual ctrl command number used.
178      * For params to ctrl translation, 0.
179      */
180     int ctrl_cmd;
181     /*
182      * For ctrl_str to params translation, the actual ctrl command string
183      * used.  In this case, the (string) value is always passed as |p2|.
184      * For params to ctrl translation, this is NULL.  Along with it is also
185      * and indicator whether it matched |ctrl_str| or |ctrl_hexstr| in the
186      * translation item.
187      */
188     const char *ctrl_str;
189     int ishex;
190     /* the ctrl-style int argument. */
191     int p1;
192     /* the ctrl-style void* argument. */
193     void *p2;
194     /* a size, for passing back the |p2| size where applicable */
195     size_t sz;
196     /* pointer to the OSSL_PARAM-style params array. */
197     OSSL_PARAM *params;
198
199     /*-
200      * The following are used entirely internally by the fixup_args functions
201      * and should not be touched by the callers, at all.
202      */
203
204     /*
205      * Copy of the ctrl-style void* argument, if the fixup_args function
206      * needs to manipulate |p2| but wants to remember original.
207      */
208     void *orig_p2;
209     /* Diverse types of storage for the needy. */
210     char name_buf[OSSL_MAX_NAME_SIZE];
211     void *allocated_buf;
212     void *bufp;
213     size_t buflen;
214 };
215
216 struct translation_st {
217     /*-
218      * What this table item does.
219      *
220      * If the item has this set to 0, it means that both GET and SET are
221      * supported, and |fixup_args| will determine which it is.  This is to
222      * support translations of ctrls where the action type depends on the
223      * value of |p1| or |p2| (ctrls are really bi-directional, but are
224      * seldom used that way).
225      *
226      * This can be also used in the lookup template when it looks up by
227      * OSSL_PARAM key, to indicate if a setter or a getter called.
228      */
229     enum action action_type;
230
231     /*-
232      * Conditions, for params->ctrl translations.
233      *
234      * In table item, |keytype1| and |keytype2| can be set to -1 to indicate
235      * that this item supports all key types (or rather, that |fixup_args|
236      * will check and return an error if it's not supported).
237      * Any of these may be set to 0 to indicate that they are unset.
238      */
239     int keytype1;    /* The EVP_PKEY_XXX type, i.e. NIDs. #legacy */
240     int keytype2;    /* Another EVP_PKEY_XXX type, used for aliases */
241     int optype;      /* The operation type */
242
243     /*
244      * Lookup and translation attributes
245      *
246      * |ctrl_num|, |ctrl_str|, |ctrl_hexstr| and |param_key| are lookup
247      * attributes.
248      *
249      * |ctrl_num| may be 0 or that |param_key| may be NULL in the table item,
250      * but not at the same time.  If they are, they are simply not used for
251      * lookup.
252      * When |ctrl_num| == 0, no ctrl will be called.  Likewise, when
253      * |param_key| == NULL, no OSSL_PARAM setter/getter will be called.
254      * In that case the treatment of the translation item relies entirely on
255      * |fixup_args|, which is then assumed to have side effects.
256      *
257      * As a special case, it's possible to set |ctrl_hexstr| and assign NULL
258      * to |ctrl_str|.  That will signal to default_fixup_args() that the
259      * value must always be interpreted as hex.
260      */
261     int ctrl_num;            /* EVP_PKEY_CTRL_xxx */
262     const char *ctrl_str;    /* The corresponding ctrl string */
263     const char *ctrl_hexstr; /* The alternative "hex{str}" ctrl string */
264     const char *param_key;   /* The corresponding OSSL_PARAM key */
265     /*
266      * The appropriate OSSL_PARAM data type.  This may be 0 to indicate that
267      * this OSSL_PARAM may have more than one data type, depending on input
268      * material.  In this case, |fixup_args| is expected to check and handle
269      * it.
270      */
271     unsigned int param_data_type;
272
273     /*
274      * Fixer functions
275      *
276      * |fixup_args| is always called before (for SET) or after (for GET)
277      * the actual ctrl / OSSL_PARAM function.
278      */
279     fixup_args_fn *fixup_args;
280 };
281
282 /*-
283  * Fixer function implementations
284  * ==============================
285  */
286
287 /*
288  * default_check isn't a fixer per se, but rather a helper function to
289  * perform certain standard checks.
290  */
291 static int default_check(enum state state,
292                          const struct translation_st *translation,
293                          const struct translation_ctx_st *ctx)
294 {
295     switch (state) {
296     default:
297         break;
298     case PRE_CTRL_TO_PARAMS:
299         if (!ossl_assert(translation != NULL)) {
300             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
301             return -2;
302         }
303         if (!ossl_assert(translation->param_key != 0)
304             || !ossl_assert(translation->param_data_type != 0)) {
305             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
306             return -1;
307         }
308         break;
309     case PRE_CTRL_STR_TO_PARAMS:
310         /*
311          * For ctrl_str to params translation, we allow direct use of
312          * OSSL_PARAM keys as ctrl_str keys.  Therefore, it's possible that
313          * we end up with |translation == NULL|, which is fine.  The fixup
314          * function will have to deal with it carefully.
315          */
316         if (translation != NULL) {
317             if (!ossl_assert(translation->action_type != GET)) {
318                 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
319                 return -2;
320             }
321             if (!ossl_assert(translation->param_key != NULL)
322                 || !ossl_assert(translation->param_data_type != 0)) {
323                 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
324                 return 0;
325             }
326         }
327         break;
328     case PRE_PARAMS_TO_CTRL:
329     case POST_PARAMS_TO_CTRL:
330         if (!ossl_assert(translation != NULL)) {
331             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
332             return -2;
333         }
334         if (!ossl_assert(translation->ctrl_num != 0)
335             || !ossl_assert(translation->param_data_type != 0)) {
336             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
337             return -1;
338         }
339     }
340
341     /* Nothing else to check */
342     return 1;
343 }
344
345 /*-
346  * default_fixup_args fixes up all sorts of arguments, governed by the
347  * diverse attributes in the translation item.  It covers all "standard"
348  * base ctrl functionality, meaning it can handle basic conversion of
349  * data between p1+p2 (SET) or return value+p2 (GET) as long as the values
350  * don't have extra semantics (such as NIDs, OIDs, that sort of stuff).
351  * Extra semantics must be handled via specific fixup_args functions.
352  *
353  * The following states and action type combinations have standard handling
354  * done in this function:
355  *
356  * PRE_CTRL_TO_PARAMS, 0                - ERROR.  action type must be
357  *                                        determined by a fixup function.
358  * PRE_CTRL_TO_PARAMS, SET | GET        - |p1| and |p2| are converted to an
359  *                                        OSSL_PARAM according to the data
360  *                                        type given in |translattion|.
361  *                                        For OSSL_PARAM_UNSIGNED_INTEGER,
362  *                                        a BIGNUM passed as |p2| is accepted.
363  * POST_CTRL_TO_PARAMS, GET             - If the OSSL_PARAM data type is a
364  *                                        STRING or PTR type, |p1| is set
365  *                                        to the OSSL_PARAM return size, and
366  *                                        |p2| is set to the string.
367  * PRE_CTRL_STR_TO_PARAMS, !SET         - ERROR.  That combination is not
368  *                                        supported.
369  * PRE_CTRL_STR_TO_PARAMS, SET          - |p2| is taken as a string, and is
370  *                                        converted to an OSSL_PARAM in a
371  *                                        standard manner, guided by the
372  *                                        param key and data type from
373  *                                        |translation|.
374  * PRE_PARAMS_TO_CTRL, SET              - the OSSL_PARAM is converted to
375  *                                        |p1| and |p2| according to the
376  *                                        data type given in |translation|
377  *                                        For OSSL_PARAM_UNSIGNED_INTEGER,
378  *                                        if |p2| is non-NULL, then |*p2|
379  *                                        is assigned a BIGNUM, otherwise
380  *                                        |p1| is assigned an unsigned int.
381  * POST_PARAMS_TO_CTRL, GET             - |p1| and |p2| are converted to
382  *                                        an OSSL_PARAM, in the same manner
383  *                                        as for the combination of
384  *                                        PRE_CTRL_TO_PARAMS, SET.
385  */
386 static int default_fixup_args(enum state state,
387                               const struct translation_st *translation,
388                               struct translation_ctx_st *ctx)
389 {
390     int ret;
391
392     if ((ret = default_check(state, translation, ctx)) < 0)
393         return ret;
394
395     switch (state) {
396     default:
397         /* For states this function should never have been called with */
398         ERR_raise_data(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
399                        "[action:%d, state:%d]", ctx->action_type, state);
400         return 0;
401
402     /*
403      * PRE_CTRL_TO_PARAMS and POST_CTRL_TO_PARAMS handle ctrl to params
404      * translations.  PRE_CTRL_TO_PARAMS is responsible for preparing
405      * |*params|, and POST_CTRL_TO_PARAMS is responsible for bringing the
406      * result back to |*p2| and the return value.
407      */
408     case PRE_CTRL_TO_PARAMS:
409         /* This is ctrl to params translation, so we need an OSSL_PARAM key */
410         if (ctx->action_type == NONE) {
411             /*
412              * No action type is an error here.  That's a case for a
413              * special fixup function.
414              */
415             ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
416                            "[action:%d, state:%d]", ctx->action_type, state);
417             return 0;
418         }
419
420         if (translation->optype != 0) {
421             if ((EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
422                  && ctx->pctx->op.sig.sigprovctx == NULL)
423                 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx->pctx)
424                     && ctx->pctx->op.kex.exchprovctx == NULL)
425                 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx->pctx)
426                     && ctx->pctx->op.ciph.ciphprovctx == NULL)
427                 || (EVP_PKEY_CTX_IS_KEM_OP(ctx->pctx)
428                     && ctx->pctx->op.encap.kemprovctx == NULL)
429                 /*
430                  * The following may be unnecessary, but we have them
431                  * for good measure...
432                  */
433                 || (EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx)
434                     && ctx->pctx->op.keymgmt.genctx == NULL)
435                 || (EVP_PKEY_CTX_IS_FROMDATA_OP(ctx->pctx)
436                     && ctx->pctx->op.keymgmt.genctx == NULL)) {
437                 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
438                 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
439                 return -2;
440             }
441         }
442
443         /*
444          * OSSL_PARAM_construct_TYPE() works equally well for both SET and GET.
445          */
446         switch (translation->param_data_type) {
447         case OSSL_PARAM_INTEGER:
448             *ctx->params = OSSL_PARAM_construct_int(translation->param_key,
449                                                     &ctx->p1);
450             break;
451         case OSSL_PARAM_UNSIGNED_INTEGER:
452             /*
453              * BIGNUMs are passed via |p2|.  For all ctrl's that just want
454              * to pass a simple integer via |p1|, |p2| is expected to be
455              * NULL.
456              *
457              * Note that this allocates a buffer, which the cleanup function
458              * must deallocate.
459              */
460             if (ctx->p2 != NULL) {
461                 if (ctx->action_type == SET) {
462                     ctx->buflen = BN_num_bytes(ctx->p2);
463                     if ((ctx->allocated_buf =
464                          OPENSSL_malloc(ctx->buflen)) == NULL) {
465                         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
466                         return 0;
467                     }
468                     if (!BN_bn2nativepad(ctx->p2,
469                                          ctx->allocated_buf, ctx->buflen)) {
470                         OPENSSL_free(ctx->allocated_buf);
471                         ctx->allocated_buf = NULL;
472                         return 0;
473                     }
474                     *ctx->params =
475                         OSSL_PARAM_construct_BN(translation->param_key,
476                                                 ctx->allocated_buf,
477                                                 ctx->buflen);
478                 } else {
479                     /*
480                      * No support for getting a BIGNUM by ctrl, this needs
481                      * fixup_args function support.
482                      */
483                     ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
484                                    "[action:%d, state:%d] trying to get a "
485                                    "BIGNUM via ctrl call",
486                                    ctx->action_type, state);
487                     return 0;
488                 }
489             } else {
490                 *ctx->params =
491                     OSSL_PARAM_construct_uint(translation->param_key,
492                                               (unsigned int *)&ctx->p1);
493             }
494             break;
495         case OSSL_PARAM_UTF8_STRING:
496             *ctx->params =
497                 OSSL_PARAM_construct_utf8_string(translation->param_key,
498                                                  ctx->p2, (size_t)ctx->p1);
499             break;
500         case OSSL_PARAM_UTF8_PTR:
501             *ctx->params =
502                 OSSL_PARAM_construct_utf8_ptr(translation->param_key,
503                                               ctx->p2, (size_t)ctx->p1);
504             break;
505         case OSSL_PARAM_OCTET_STRING:
506             *ctx->params =
507                 OSSL_PARAM_construct_octet_string(translation->param_key,
508                                                   ctx->p2, (size_t)ctx->p1);
509             break;
510         case OSSL_PARAM_OCTET_PTR:
511             *ctx->params =
512                 OSSL_PARAM_construct_octet_ptr(translation->param_key,
513                                                ctx->p2, (size_t)ctx->p1);
514             break;
515         }
516         break;
517     case POST_CTRL_TO_PARAMS:
518         /*
519          * Because EVP_PKEY_CTX_ctrl() returns the length of certain objects
520          * as its return value, we need to ensure that we do it here as well,
521          * for the OSSL_PARAM data types where this makes sense.
522          */
523         if (ctx->action_type == GET) {
524             switch (translation->param_data_type) {
525             case OSSL_PARAM_UTF8_STRING:
526             case OSSL_PARAM_UTF8_PTR:
527             case OSSL_PARAM_OCTET_STRING:
528             case OSSL_PARAM_OCTET_PTR:
529                 ctx->p1 = (int)ctx->params[0].return_size;
530                 break;
531             }
532         }
533         break;
534
535     /*
536      * PRE_CTRL_STR_TO_PARAMS and POST_CTRL_STR_TO_PARAMS handle ctrl_str to
537      * params translations.  PRE_CTRL_TO_PARAMS is responsible for preparing
538      * |*params|, and POST_CTRL_TO_PARAMS currently has nothing to do, since
539      * there's no support for getting data via ctrl_str calls.
540      */
541     case PRE_CTRL_STR_TO_PARAMS:
542         {
543             /* This is ctrl_str to params translation */
544             const char *tmp_ctrl_str = ctx->ctrl_str;
545             const char *orig_ctrl_str = ctx->ctrl_str;
546             const char *orig_value = ctx->p2;
547             const OSSL_PARAM *settable = NULL;
548             int exists = 0;
549
550             /* Only setting is supported here */
551             if (ctx->action_type != SET) {
552                 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
553                                    "[action:%d, state:%d] only setting allowed",
554                                    ctx->action_type, state);
555                 return 0;
556             }
557
558             /*
559              * If no translation exists, we simply pass the control string
560              * unmodified.
561              */
562             if (translation != NULL) {
563                 tmp_ctrl_str = ctx->ctrl_str = translation->param_key;
564
565                 if (ctx->ishex) {
566                     strcpy(ctx->name_buf, "hex");
567                     if (OPENSSL_strlcat(ctx->name_buf, tmp_ctrl_str,
568                                         sizeof(ctx->name_buf)) <= 3) {
569                         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
570                         return -1;
571                     }
572                     tmp_ctrl_str = ctx->name_buf;
573                 }
574             }
575
576             settable = EVP_PKEY_CTX_settable_params(ctx->pctx);
577             if (!OSSL_PARAM_allocate_from_text(ctx->params, settable,
578                                                tmp_ctrl_str,
579                                                ctx->p2, strlen(ctx->p2),
580                                                &exists)) {
581                 if (!exists) {
582                     ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
583                                    "[action:%d, state:%d] name=%s, value=%s",
584                                    ctx->action_type, state,
585                                    orig_ctrl_str, orig_value);
586                     return -2;
587                 }
588                 return 0;
589             }
590             ctx->allocated_buf = ctx->params->data;
591             ctx->buflen = ctx->params->data_size;
592         }
593         break;
594     case POST_CTRL_STR_TO_PARAMS:
595         /* Nothing to be done */
596         break;
597
598     /*
599      * PRE_PARAMS_TO_CTRL and POST_PARAMS_TO_CTRL handle params to ctrl
600      * translations.  PRE_PARAMS_TO_CTRL is responsible for preparing
601      * |p1| and |p2|, and POST_PARAMS_TO_CTRL is responsible for bringing
602      * the EVP_PKEY_CTX_ctrl() return value (passed as |p1|) and |p2| back
603      * to |*params|.
604      *
605      * PKEY is treated just like POST_PARAMS_TO_CTRL, making it easy
606      * for the related fixup_args functions to just set |p1| and |p2|
607      * appropriately and leave it to this section of code to fix up
608      * |ctx->params| accordingly.
609      */
610     case PKEY:
611     case POST_PARAMS_TO_CTRL:
612         ret = ctx->p1;
613         /* FALLTHRU */
614     case PRE_PARAMS_TO_CTRL:
615         {
616             /* This is params to ctrl translation */
617             if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
618                 /* For the PRE state, only setting needs some work to be done */
619
620                 /* When setting, we populate |p1| and |p2| from |*params| */
621                 switch (translation->param_data_type) {
622                 case OSSL_PARAM_INTEGER:
623                     return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
624                 case OSSL_PARAM_UNSIGNED_INTEGER:
625                     if (ctx->p2 != NULL) {
626                         /* BIGNUM passed down with p2 */
627                         if (!OSSL_PARAM_get_BN(ctx->params, ctx->p2))
628                             return 0;
629                     } else {
630                         /* Normal C unsigned int passed down */
631                         if (!OSSL_PARAM_get_uint(ctx->params,
632                                                  (unsigned int *)&ctx->p1))
633                             return 0;
634                     }
635                     return 1;
636                 case OSSL_PARAM_UTF8_STRING:
637                     return OSSL_PARAM_get_utf8_string(ctx->params,
638                                                       ctx->p2, ctx->sz);
639                 case OSSL_PARAM_OCTET_STRING:
640                     return OSSL_PARAM_get_octet_string(ctx->params,
641                                                        ctx->p2, ctx->sz,
642                                                        &ctx->sz);
643                 case OSSL_PARAM_OCTET_PTR:
644                     return OSSL_PARAM_get_octet_ptr(ctx->params,
645                                                     ctx->p2, &ctx->sz);
646                 default:
647                     ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
648                                    "[action:%d, state:%d] "
649                                    "unknown OSSL_PARAM data type %d",
650                                    ctx->action_type, state,
651                                    translation->param_data_type);
652                     return 0;
653                 }
654             } else if ((state == POST_PARAMS_TO_CTRL || state == PKEY)
655                        && ctx->action_type == GET) {
656                 /* For the POST state, only getting needs some work to be done */
657
658                 /* When getting, we populate |*params| from |p1| and |p2| */
659                 switch (translation->param_data_type) {
660                 case OSSL_PARAM_INTEGER:
661                     return OSSL_PARAM_set_int(ctx->params, ctx->p1);
662                 case OSSL_PARAM_UNSIGNED_INTEGER:
663                     if (ctx->p2 != NULL) {
664                         /* BIGNUM passed back */
665                         return OSSL_PARAM_set_BN(ctx->params, ctx->p2);
666                     } else {
667                         /* Normal C unsigned int passed back */
668                         return OSSL_PARAM_set_uint(ctx->params,
669                                                    (unsigned int)ctx->p1);
670                     }
671                     return 0;
672                 case OSSL_PARAM_UTF8_STRING:
673                     return OSSL_PARAM_set_utf8_string(ctx->params, ctx->p2);
674                 case OSSL_PARAM_OCTET_STRING:
675                     return OSSL_PARAM_set_octet_string(ctx->params, ctx->p2,
676                                                        (size_t)ctx->p1);
677                 case OSSL_PARAM_OCTET_PTR:
678                     return OSSL_PARAM_set_octet_ptr(ctx->params, ctx->p2,
679                                                     (size_t)ctx->p1);
680                 default:
681                     ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
682                                    "[action:%d, state:%d] "
683                                    "unsupported OSSL_PARAM data type %d",
684                                    ctx->action_type, state,
685                                    translation->param_data_type);
686                     return 0;
687                 }
688             }
689         }
690         /* Any other combination is simply pass-through */
691         break;
692     }
693     return ret;
694 }
695
696 static int
697 cleanup_translation_ctx(enum state state,
698                         const struct translation_st *translation,
699                         struct translation_ctx_st *ctx)
700 {
701     if (ctx->allocated_buf != NULL)
702         OPENSSL_free(ctx->allocated_buf);
703     ctx->allocated_buf = NULL;
704     return 1;
705 }
706
707 /*
708  * fix_cipher_md fixes up an EVP_CIPHER / EVP_MD to its name on SET,
709  * and cipher / md name to EVP_MD on GET.
710  */
711 static const char *get_cipher_name(void *cipher)
712 {
713     return EVP_CIPHER_name(cipher);
714 }
715
716 static const char *get_md_name(void *md)
717 {
718     return EVP_MD_name(md);
719 }
720
721 static const void *get_cipher_by_name(OSSL_LIB_CTX *libctx, const char *name)
722 {
723     return evp_get_cipherbyname_ex(libctx, name);
724 }
725
726 static const void *get_md_by_name(OSSL_LIB_CTX *libctx, const char *name)
727 {
728     return evp_get_digestbyname_ex(libctx, name);
729 }
730
731 static int fix_cipher_md(enum state state,
732                          const struct translation_st *translation,
733                          struct translation_ctx_st *ctx,
734                          const char *(*get_name)(void *algo),
735                          const void *(*get_algo_by_name)(OSSL_LIB_CTX *libctx,
736                                                          const char *name))
737 {
738     int ret = 1;
739
740     if ((ret = default_check(state, translation, ctx)) <= 0)
741         return ret;
742
743     if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
744         /*
745          * |ctx->p2| contains the address to an EVP_CIPHER or EVP_MD pointer
746          * to be filled in.  We need to remember it, then make |ctx->p2|
747          * point at a buffer to be filled in with the name, and |ctx->p1|
748          * with its size.  default_fixup_args() will take care of the rest
749          * for us.
750          */
751         ctx->orig_p2 = ctx->p2;
752         ctx->p2 = ctx->name_buf;
753         ctx->p1 = sizeof(ctx->name_buf);
754     } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
755         /*
756          * In different parts of OpenSSL, this ctrl command is used
757          * differently.  Some calls pass a NID as p1, others pass an
758          * EVP_CIPHER pointer as p2...
759          */
760         ctx->p2 = (char *)(ctx->p2 == NULL
761                            ? OBJ_nid2sn(ctx->p1)
762                            : get_name(ctx->p2));
763         ctx->p1 = strlen(ctx->p2);
764     } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
765         ctx->p2 = (ctx->p2 == NULL ? "" : (char *)get_name(ctx->p2));
766         ctx->p1 = strlen(ctx->p2);
767     }
768
769     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
770         return ret;
771
772     if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
773         /*
774          * Here's how we re-use |ctx->orig_p2| that was set in the
775          * PRE_CTRL_TO_PARAMS state above.
776          */
777         *(void **)ctx->orig_p2 =
778             (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
779         ctx->p1 = 1;
780     } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
781         ctx->p2 = (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
782         ctx->p1 = 0;
783     }
784
785     return ret;
786 }
787
788 static int fix_cipher(enum state state,
789                       const struct translation_st *translation,
790                       struct translation_ctx_st *ctx)
791 {
792     return fix_cipher_md(state, translation, ctx,
793                          get_cipher_name, get_cipher_by_name);
794 }
795
796 static int fix_md(enum state state,
797                   const struct translation_st *translation,
798                   struct translation_ctx_st *ctx)
799 {
800     return fix_cipher_md(state, translation, ctx,
801                          get_md_name, get_md_by_name);
802 }
803
804 static int fix_distid_len(enum state state,
805                           const struct translation_st *translation,
806                           struct translation_ctx_st *ctx)
807 {
808     int ret = default_fixup_args(state, translation, ctx);
809
810     if (ret > 0) {
811         ret = 0;
812         if ((state == POST_CTRL_TO_PARAMS
813              || state == POST_CTRL_STR_TO_PARAMS) && ctx->action_type == GET) {
814             *(size_t *)ctx->p2 = ctx->sz;
815             ret = 1;
816         }
817     }
818     return ret;
819 }
820
821 struct kdf_type_map_st {
822     int kdf_type_num;
823     const char *kdf_type_str;
824 };
825
826 static int fix_kdf_type(enum state state,
827                         const struct translation_st *translation,
828                         struct translation_ctx_st *ctx,
829                         const struct kdf_type_map_st *kdf_type_map)
830 {
831     /*
832      * The EVP_PKEY_CTRL_DH_KDF_TYPE ctrl command is a bit special, in
833      * that it's used both for setting a value, and for getting it, all
834      * depending on the value if |p1|; if |p1| is -2, the backend is
835      * supposed to place the current kdf type in |p2|, and if not, |p1|
836      * is interpreted as the new kdf type.
837      */
838     int ret = 0;
839
840     if ((ret = default_check(state, translation, ctx)) <= 0)
841         return ret;
842
843     if (state == PRE_CTRL_TO_PARAMS) {
844         /*
845          * In |translations|, the initial value for |ctx->action_type| must
846          * be NONE.
847          */
848         if (!ossl_assert(ctx->action_type == NONE))
849             return 0;
850
851         /* The action type depends on the value of *p1 */
852         if (ctx->p1 == -2) {
853             /*
854              * The OSSL_PARAMS getter needs space to store a copy of the kdf
855              * type string.  We use |ctx->name_buf|, which has enough space
856              * allocated.
857              *
858              * (this wouldn't be needed if the OSSL_xxx_PARAM_KDF_TYPE
859              * had the data type OSSL_PARAM_UTF8_PTR)
860              */
861             ctx->p2 = ctx->name_buf;
862             ctx->p1 = sizeof(ctx->name_buf);
863             ctx->action_type = GET;
864         } else {
865             ctx->action_type = SET;
866         }
867     }
868
869     if ((ret = default_check(state, translation, ctx)) <= 0)
870         return ret;
871
872     if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
873         || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
874         ret = -2;
875         /* Convert KDF type numbers to strings */
876         for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
877             if (ctx->p1 == kdf_type_map->kdf_type_num) {
878                 ctx->p2 = (char *)kdf_type_map->kdf_type_str;
879                 ret = 1;
880                 break;
881             }
882         if (ret <= 0)
883             goto end;
884         ctx->p1 = strlen(ctx->p2);
885     }
886
887     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
888         return ret;
889
890     if ((state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)
891         || (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)) {
892         ctx->p1 = ret = -1;
893
894         /* Convert KDF type strings to numbers */
895         for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
896             if (strcasecmp(ctx->p2, kdf_type_map->kdf_type_str) == 0) {
897                 ctx->p1 = kdf_type_map->kdf_type_num;
898                 ret = 1;
899                 break;
900             }
901         ctx->p2 = NULL;
902     } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
903         ctx->p1 = -2;
904     }
905  end:
906     return ret;
907 }
908
909 /* EVP_PKEY_CTRL_DH_KDF_TYPE */
910 static int fix_dh_kdf_type(enum state state,
911                            const struct translation_st *translation,
912                            struct translation_ctx_st *ctx)
913 {
914     static const struct kdf_type_map_st kdf_type_map[] = {
915         { EVP_PKEY_DH_KDF_NONE, "" },
916         { EVP_PKEY_DH_KDF_X9_42, OSSL_KDF_NAME_X942KDF_ASN1 },
917         { 0, NULL }
918     };
919
920     return fix_kdf_type(state, translation, ctx, kdf_type_map);
921 }
922
923 /* EVP_PKEY_CTRL_EC_KDF_TYPE */
924 static int fix_ec_kdf_type(enum state state,
925                            const struct translation_st *translation,
926                            struct translation_ctx_st *ctx)
927 {
928     static const struct kdf_type_map_st kdf_type_map[] = {
929         { EVP_PKEY_ECDH_KDF_NONE, "" },
930         { EVP_PKEY_ECDH_KDF_X9_63, OSSL_KDF_NAME_X963KDF },
931         { 0, NULL }
932     };
933
934     return fix_kdf_type(state, translation, ctx, kdf_type_map);
935 }
936
937 /* EVP_PKEY_CTRL_DH_KDF_OID, EVP_PKEY_CTRL_GET_DH_KDF_OID, ...??? */
938 static int fix_oid(enum state state,
939                    const struct translation_st *translation,
940                    struct translation_ctx_st *ctx)
941 {
942     int ret;
943
944     if ((ret = default_check(state, translation, ctx)) <= 0)
945         return ret;
946
947     if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
948         || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
949         /*
950          * We're translating from ctrl to params and setting the OID, or
951          * we're translating from params to ctrl and getting the OID.
952          * Either way, |ctx->p2| points at an ASN1_OBJECT, and needs to have
953          * that replaced with the corresponding name.
954          * default_fixup_args() will then be able to convert that to the
955          * corresponding OSSL_PARAM.
956          */
957         ctx->p2 = (char *)OBJ_nid2sn(OBJ_obj2nid(ctx->p2));
958         ctx->p1 = 0; /* let default_fixup_args() figure out the length */
959     }
960
961     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
962         return ret;
963
964     if ((state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)
965         || (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)) {
966         /*
967          * We're translating from ctrl to params and setting the OID name,
968          * or we're translating from params to ctrl and getting the OID
969          * name.  Either way, default_fixup_args() has placed the OID name
970          * in |ctx->p2|, all we need to do now is to replace that with the
971          * corresponding ASN1_OBJECT.
972          */
973         ctx->p2 = (ASN1_OBJECT *)OBJ_txt2obj(ctx->p2, 0);
974     }
975
976     return ret;
977 }
978
979 /* EVP_PKEY_CTRL_DH_NID, ...??? */
980 static int fix_dh_nid(enum state state,
981                       const struct translation_st *translation,
982                       struct translation_ctx_st *ctx)
983 {
984     int ret;
985
986     if ((ret = default_check(state, translation, ctx)) <= 0)
987         return ret;
988
989     /* This is currently only settable */
990     if (ctx->action_type != SET)
991         return 0;
992
993     if (state == PRE_CTRL_TO_PARAMS) {
994         ctx->p2 = (char *)ossl_ffc_named_group_get_name
995             (ossl_ffc_uid_to_dh_named_group(ctx->p1));
996         ctx->p1 = 0;
997     }
998
999     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1000         return ret;
1001
1002     if (state == PRE_PARAMS_TO_CTRL) {
1003         ctx->p1 =
1004             ossl_ffc_named_group_get_uid(ossl_ffc_name_to_dh_named_group(ctx->p2));
1005         ctx->p2 = NULL;
1006     }
1007
1008     return ret;
1009 }
1010
1011 /* EVP_PKEY_CTRL_DH_PARAMGEN_TYPE */
1012 static int fix_dh_paramgen_type(enum state state,
1013                                 const struct translation_st *translation,
1014                                 struct translation_ctx_st *ctx)
1015 {
1016     int ret;
1017
1018     if ((ret = default_check(state, translation, ctx)) <= 0)
1019         return ret;
1020
1021     /* This is currently only settable */
1022     if (ctx->action_type != SET)
1023         return 0;
1024
1025     if (state == PRE_CTRL_TO_PARAMS) {
1026         ctx->p2 = (char *)ossl_dh_gen_type_id2name(ctx->p1);
1027         ctx->p1 = 0;
1028     }
1029
1030     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1031         return ret;
1032
1033     if (state == PRE_PARAMS_TO_CTRL) {
1034         ctx->p1 = ossl_dh_gen_type_name2id(ctx->p2);
1035         ctx->p2 = NULL;
1036     }
1037
1038     return ret;
1039 }
1040
1041 /* EVP_PKEY_CTRL_EC_PARAM_ENC */
1042 static int fix_ec_param_enc(enum state state,
1043                             const struct translation_st *translation,
1044                             struct translation_ctx_st *ctx)
1045 {
1046     int ret;
1047
1048     if ((ret = default_check(state, translation, ctx)) <= 0)
1049         return ret;
1050
1051     /* This is currently only settable */
1052     if (ctx->action_type != SET)
1053         return 0;
1054
1055     if (state == PRE_CTRL_TO_PARAMS) {
1056         switch (ctx->p1) {
1057         case OPENSSL_EC_EXPLICIT_CURVE:
1058             ctx->p2 = OSSL_PKEY_EC_ENCODING_EXPLICIT;
1059             break;
1060         case OPENSSL_EC_NAMED_CURVE:
1061             ctx->p2 = OSSL_PKEY_EC_ENCODING_GROUP;
1062             break;
1063         default:
1064             ret = -2;
1065             goto end;
1066         }
1067         ctx->p1 = 0;
1068     }
1069
1070     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1071         return ret;
1072
1073     if (state == PRE_PARAMS_TO_CTRL) {
1074         if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_EXPLICIT) == 0)
1075             ctx->p1 = OPENSSL_EC_EXPLICIT_CURVE;
1076         else if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_GROUP) == 0)
1077             ctx->p1 = OPENSSL_EC_NAMED_CURVE;
1078         else
1079             ctx->p1 = ret = -2;
1080         ctx->p2 = NULL;
1081     }
1082
1083  end:
1084     if (ret == -2)
1085         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1086     return ret;
1087 }
1088
1089 /* EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID */
1090 static int fix_ec_paramgen_curve_nid(enum state state,
1091                                      const struct translation_st *translation,
1092                                      struct translation_ctx_st *ctx)
1093 {
1094     int ret;
1095
1096     if ((ret = default_check(state, translation, ctx)) <= 0)
1097         return ret;
1098
1099     /* This is currently only settable */
1100     if (ctx->action_type != SET)
1101         return 0;
1102
1103     if (state == PRE_CTRL_TO_PARAMS) {
1104         ctx->p2 = (char *)OBJ_nid2sn(ctx->p1);
1105         ctx->p1 = 0;
1106     }
1107
1108     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1109         return ret;
1110
1111     if (state == PRE_PARAMS_TO_CTRL) {
1112         ctx->p1 = OBJ_sn2nid(ctx->p2);
1113         ctx->p2 = NULL;
1114     }
1115
1116     return ret;
1117 }
1118
1119 /* EVP_PKEY_CTRL_EC_ECDH_COFACTOR */
1120 static int fix_ecdh_cofactor(enum state state,
1121                              const struct translation_st *translation,
1122                              struct translation_ctx_st *ctx)
1123 {
1124     /*
1125      * The EVP_PKEY_CTRL_EC_ECDH_COFACTOR ctrl command is a bit special, in
1126      * that it's used both for setting a value, and for getting it, all
1127      * depending on the value if |ctx->p1|; if |ctx->p1| is -2, the backend is
1128      * supposed to place the current cofactor mode in |ctx->p2|, and if not,
1129      * |ctx->p1| is interpreted as the new cofactor mode.
1130      */
1131     int ret = 0;
1132
1133     if (state == PRE_CTRL_TO_PARAMS) {
1134         /*
1135          * The initial value for |ctx->action_type| must be zero.
1136          * evp_pkey_ctrl_to_params() takes it from the translation item.
1137          */
1138         if (!ossl_assert(ctx->action_type == NONE))
1139             return 0;
1140
1141         /* The action type depends on the value of ctx->p1 */
1142         if (ctx->p1 == -2)
1143             ctx->action_type = GET;
1144         else
1145             ctx->action_type = SET;
1146     } else if (state == PRE_CTRL_STR_TO_PARAMS) {
1147         ctx->action_type = SET;
1148     } else if (state == PRE_PARAMS_TO_CTRL) {
1149         /* The initial value for |ctx->action_type| must not be zero. */
1150         if (!ossl_assert(ctx->action_type != NONE))
1151             return 0;
1152     }
1153
1154     if ((ret = default_check(state, translation, ctx)) <= 0)
1155         return ret;
1156
1157     if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1158         if (ctx->p1 < -1 || ctx->p1 > 1) {
1159             /* Uses the same return value of pkey_ec_ctrl() */
1160             return -2;
1161         }
1162     }
1163
1164     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1165         return ret;
1166
1167     if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
1168         if (ctx->p1 < 0 || ctx->p1 > 1) {
1169             /*
1170              * The provider should return either 0 or 1, any other value is a
1171              * provider error.
1172              */
1173             ctx->p1 = ret = -1;
1174         }
1175     } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
1176         ctx->p1 = -2;
1177     }
1178
1179     return ret;
1180 }
1181
1182 /* EVP_PKEY_CTRL_RSA_PADDING, EVP_PKEY_CTRL_GET_RSA_PADDING */
1183 static int fix_rsa_padding_mode(enum state state,
1184                                 const struct translation_st *translation,
1185                                 struct translation_ctx_st *ctx)
1186 {
1187     static const OSSL_ITEM str_value_map[] = {
1188         { RSA_PKCS1_PADDING,            "pkcs1"  },
1189         { RSA_NO_PADDING,               "none"   },
1190         { RSA_PKCS1_OAEP_PADDING,       "oaep"   },
1191         { RSA_PKCS1_OAEP_PADDING,       "oeap"   },
1192         { RSA_X931_PADDING,             "x931"   },
1193         { RSA_PKCS1_PSS_PADDING,        "pss"    },
1194         /* Special case, will pass directly as an integer */
1195         { RSA_PKCS1_WITH_TLS_PADDING,   NULL     }
1196     };
1197     int ret;
1198
1199     if ((ret = default_check(state, translation, ctx)) <= 0)
1200         return ret;
1201
1202     if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1203         /*
1204          * EVP_PKEY_CTRL_GET_RSA_PADDING returns the padding mode in the
1205          * weirdest way for a ctrl.  Instead of doing like all other ctrls
1206          * that return a simple, i.e. just have that as a return value,
1207          * this particular ctrl treats p2 as the address for the int to be
1208          * returned.  We must therefore remember |ctx->p2|, then make
1209          * |ctx->p2| point at a buffer to be filled in with the name, and
1210          * |ctx->p1| with its size.  default_fixup_args() will take care
1211          * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1212          * code section further down.
1213          */
1214         ctx->orig_p2 = ctx->p2;
1215         ctx->p2 = ctx->name_buf;
1216         ctx->p1 = sizeof(ctx->name_buf);
1217     } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1218         /*
1219          * Ideally, we should use utf8 strings for the diverse padding modes.
1220          * We only came here because someone called EVP_PKEY_CTX_ctrl(),
1221          * though, and since that can reasonably be seen as legacy code
1222          * that uses the diverse RSA macros for the padding mode, and we
1223          * know that at least our providers can handle the numeric modes,
1224          * we take the cheap route for now.
1225          *
1226          * The other solution would be to match |ctx->p1| against entries
1227          * in str_value_map and pass the corresponding string.  However,
1228          * since we don't have a string for RSA_PKCS1_WITH_TLS_PADDING,
1229          * we have to do this same hack at least for that one.
1230          *
1231          * Since the "official" data type for the RSA padding mode is utf8
1232          * string, we cannot count on default_fixup_args().  Instead, we
1233          * build the OSSL_PARAM item ourselves and return immediately.
1234          */
1235         ctx->params[0] = OSSL_PARAM_construct_int(translation->param_key,
1236                                                   &ctx->p1);
1237         return 1;
1238     } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
1239         size_t i;
1240
1241         /*
1242          * The EVP_PKEY_CTX_get_params() caller may have asked for a utf8
1243          * string, or may have asked for an integer of some sort.  If they
1244          * ask for an integer, we respond directly.  If not, we translate
1245          * the response from the ctrl function into a string.
1246          */
1247         switch (ctx->params->data_type) {
1248         case OSSL_PARAM_INTEGER:
1249             return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
1250         case OSSL_PARAM_UNSIGNED_INTEGER:
1251             return OSSL_PARAM_get_uint(ctx->params, (unsigned int *)&ctx->p1);
1252         default:
1253             break;
1254         }
1255
1256         for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1257             if (ctx->p1 == (int)str_value_map[i].id)
1258                 break;
1259         }
1260         if (i == OSSL_NELEM(str_value_map)) {
1261             ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1262                            "[action:%d, state:%d] padding number %d",
1263                            ctx->action_type, state, ctx->p1);
1264             return -2;
1265         }
1266         /*
1267          * If we don't have a string, we can't do anything.  The caller
1268          * should have asked for a number...
1269          */
1270         if (str_value_map[i].ptr == NULL) {
1271             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1272             return -2;
1273         }
1274         ctx->p2 = str_value_map[i].ptr;
1275         ctx->p1 = strlen(ctx->p2);
1276     }
1277
1278     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1279         return ret;
1280
1281     if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1282         || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1283         size_t i;
1284
1285         for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1286             if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1287                 break;
1288         }
1289
1290         if (i == OSSL_NELEM(str_value_map)) {
1291             ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1292                            "[action:%d, state:%d] padding name %s",
1293                            ctx->action_type, state, ctx->p1);
1294             ctx->p1 = ret = -2;
1295         } else if (state == POST_CTRL_TO_PARAMS) {
1296             /* EVP_PKEY_CTRL_GET_RSA_PADDING weirdness explained further up */
1297             *(int *)ctx->orig_p2 = str_value_map[i].id;
1298         } else {
1299             ctx->p1 = str_value_map[i].id;
1300         }
1301         ctx->p2 = NULL;
1302     }
1303
1304     return ret;
1305 }
1306
1307 /* EVP_PKEY_CTRL_RSA_PSS_SALTLEN, EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN */
1308 static int fix_rsa_pss_saltlen(enum state state,
1309                                const struct translation_st *translation,
1310                                struct translation_ctx_st *ctx)
1311 {
1312     static const OSSL_ITEM str_value_map[] = {
1313         { (unsigned int)RSA_PSS_SALTLEN_DIGEST, "digest" },
1314         { (unsigned int)RSA_PSS_SALTLEN_MAX,    "max"    },
1315         { (unsigned int)RSA_PSS_SALTLEN_AUTO,   "auto"   }
1316     };
1317     int ret;
1318
1319     if ((ret = default_check(state, translation, ctx)) <= 0)
1320         return ret;
1321
1322     if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1323         /*
1324          * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN returns the saltlen by filling
1325          * in the int pointed at by p2.  This is potentially as weird as
1326          * the way EVP_PKEY_CTRL_GET_RSA_PADDING works, except that saltlen
1327          * might be a negative value, so it wouldn't work as a legitimate
1328          * return value.
1329          * In any case, we must therefore remember |ctx->p2|, then make
1330          * |ctx->p2| point at a buffer to be filled in with the name, and
1331          * |ctx->p1| with its size.  default_fixup_args() will take care
1332          * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1333          * code section further down.
1334          */
1335         ctx->orig_p2 = ctx->p2;
1336         ctx->p2 = ctx->name_buf;
1337         ctx->p1 = sizeof(ctx->name_buf);
1338     } else if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1339         || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1340         size_t i;
1341
1342         for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1343             if (ctx->p1 == (int)str_value_map[i].id)
1344                 break;
1345         }
1346         if (i == OSSL_NELEM(str_value_map)) {
1347             BIO_snprintf(ctx->name_buf, sizeof(ctx->name_buf), "%d", ctx->p1);
1348         } else {
1349             strcpy(ctx->name_buf, str_value_map[i].ptr);
1350         }
1351         ctx->p2 = ctx->name_buf;
1352         ctx->p1 = strlen(ctx->p2);
1353     }
1354
1355     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1356         return ret;
1357
1358     if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1359         || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1360         size_t i;
1361
1362         for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1363             if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1364                 break;
1365         }
1366         if (i == OSSL_NELEM(str_value_map)) {
1367             ctx->p1 = atoi(ctx->p2);
1368         } else if (state == POST_CTRL_TO_PARAMS) {
1369             /*
1370              * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN weirdness explained further
1371              * up
1372              */
1373             *(int *)ctx->orig_p2 = str_value_map[i].id;
1374         } else {
1375             ctx->p1 = (int)str_value_map[i].id;
1376         }
1377         ctx->p2 = NULL;
1378     }
1379
1380     return ret;
1381 }
1382
1383 /* EVP_PKEY_CTRL_HKDF_MODE */
1384 static int fix_hkdf_mode(enum state state,
1385                          const struct translation_st *translation,
1386                          struct translation_ctx_st *ctx)
1387 {
1388     static const OSSL_ITEM str_value_map[] = {
1389         { EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND, "EXTRACT_AND_EXPAND" },
1390         { EVP_KDF_HKDF_MODE_EXTRACT_ONLY,       "EXTRACT_ONLY"       },
1391         { EVP_KDF_HKDF_MODE_EXPAND_ONLY,        "EXPAND_ONLY"        }
1392     };
1393     int ret;
1394
1395     if ((ret = default_check(state, translation, ctx)) <= 0)
1396         return ret;
1397
1398     if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1399         || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1400         size_t i;
1401
1402         for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1403             if (ctx->p1 == (int)str_value_map[i].id)
1404                 break;
1405         }
1406         if (i == OSSL_NELEM(str_value_map))
1407             return 0;
1408         ctx->p2 = str_value_map[i].ptr;
1409         ctx->p1 = strlen(ctx->p2);
1410     }
1411
1412     if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1413         return ret;
1414
1415     if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1416         || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1417         size_t i;
1418
1419         for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1420             if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1421                 break;
1422         }
1423         if (i == OSSL_NELEM(str_value_map))
1424             return 0;
1425         if (state == POST_CTRL_TO_PARAMS)
1426             ret = str_value_map[i].id;
1427         else
1428             ctx->p1 = str_value_map[i].id;
1429         ctx->p2 = NULL;
1430     }
1431
1432     return 1;
1433 }
1434
1435 static int hack_pkcs7_cms(enum state state,
1436                           const struct translation_st *translation,
1437                           struct translation_ctx_st *ctx)
1438 {
1439     int ret = 1;
1440
1441     /* Make sure that this has no further effect */
1442     ctx->action_type = 0;
1443
1444     switch (state) {
1445     case PRE_CTRL_TO_PARAMS:
1446         /* TODO (3.0) Temporary hack, this should probe */
1447         if (EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx->pctx), "RSASSA-PSS")) {
1448             ERR_raise(ERR_LIB_EVP,
1449                       EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1450             ret = -2;
1451         }
1452         break;
1453     case POST_CTRL_TO_PARAMS:
1454         break;
1455     default:
1456         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1457         ret = -2;
1458         break;
1459     }
1460     return ret;
1461 }
1462
1463 /*-
1464  * Payload getters
1465  * ===============
1466  *
1467  * These all get the data they want, then call default_fixup_args() as
1468  * a post-ctrl GET fixup.  They all get NULL ctx, ctrl_cmd, ctrl_str,
1469  * p1, sz
1470  */
1471
1472 /* Pilfering DH, DSA and EC_KEY */
1473 static int get_payload_group_name(enum state state,
1474                                   const struct translation_st *translation,
1475                                   struct translation_ctx_st *ctx)
1476 {
1477     EVP_PKEY *pkey = ctx->p2;
1478
1479     ctx->p2 = NULL;
1480     switch (EVP_PKEY_base_id(pkey)) {
1481 #ifndef OPENSSL_NO_DH
1482     case EVP_PKEY_DH:
1483         {
1484             const DH *dh = EVP_PKEY_get0_DH(pkey);
1485             int uid = DH_get_nid(dh);
1486
1487             if (uid != NID_undef) {
1488                 const DH_NAMED_GROUP *dh_group =
1489                     ossl_ffc_uid_to_dh_named_group(uid);
1490
1491                 ctx->p2 = (char *)ossl_ffc_named_group_get_name(dh_group);
1492             }
1493         }
1494         break;
1495 #endif
1496 #ifndef OPENSSL_NO_EC
1497     case EVP_PKEY_EC:
1498         {
1499             const EC_GROUP *grp =
1500                 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
1501             int nid = NID_undef;
1502
1503             if (grp != NULL)
1504                 nid = EC_GROUP_get_curve_name(grp);
1505             if (nid != NID_undef)
1506                 ctx->p2 = (char *)ossl_ec_curve_nid2name(nid);
1507         }
1508         break;
1509 #endif
1510     default:
1511         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1512         return 0;
1513     }
1514
1515     /*
1516      * Quietly ignoring unknown groups matches the behaviour on the provider
1517      * side.
1518      */
1519     if (ctx->p2 == NULL)
1520         return 1;
1521
1522     ctx->p1 = strlen(ctx->p2);
1523     return default_fixup_args(state, translation, ctx);
1524 }
1525
1526 static int get_payload_private_key(enum state state,
1527                                    const struct translation_st *translation,
1528                                    struct translation_ctx_st *ctx)
1529 {
1530     EVP_PKEY *pkey = ctx->p2;
1531
1532     ctx->p2 = NULL;
1533     if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1534         return 0;
1535
1536     switch (EVP_PKEY_base_id(pkey)) {
1537 #ifndef OPENSSL_NO_DH
1538     case EVP_PKEY_DH:
1539         {
1540             const DH *dh = EVP_PKEY_get0_DH(pkey);
1541
1542             ctx->p2 = (BIGNUM *)DH_get0_priv_key(dh);
1543         }
1544         break;
1545 #endif
1546 #ifndef OPENSSL_NO_EC
1547     case EVP_PKEY_EC:
1548         {
1549             const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1550
1551             ctx->p2 = (BIGNUM *)EC_KEY_get0_private_key(ec);
1552         }
1553         break;
1554 #endif
1555     default:
1556         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1557         return 0;
1558     }
1559
1560     return default_fixup_args(state, translation, ctx);
1561 }
1562
1563 static int get_payload_public_key(enum state state,
1564                                   const struct translation_st *translation,
1565                                   struct translation_ctx_st *ctx)
1566 {
1567     EVP_PKEY *pkey = ctx->p2;
1568     unsigned char *buf = NULL;
1569     int ret;
1570
1571     ctx->p2 = NULL;
1572     switch (EVP_PKEY_base_id(pkey)) {
1573 #ifndef OPENSSL_NO_DH
1574     case EVP_PKEY_DH:
1575         switch (ctx->params->data_type) {
1576         case OSSL_PARAM_OCTET_STRING:
1577             ctx->sz = ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), &buf, 0, 1);
1578             ctx->p2 = buf;
1579             break;
1580         case OSSL_PARAM_UNSIGNED_INTEGER:
1581             ctx->p2 = (void *)DH_get0_pub_key(EVP_PKEY_get0_DH(pkey));
1582             break;
1583         default:
1584             return 0;
1585         }
1586         break;
1587 #endif
1588 #ifndef OPENSSL_NO_DSA
1589     case EVP_PKEY_DSA:
1590         if (ctx->params->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1591             ctx->p2 = (void *)DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey));
1592             break;
1593         }
1594         return 0;
1595 #endif
1596 #ifndef OPENSSL_NO_EC
1597     case EVP_PKEY_EC:
1598         if (ctx->params->data_type == OSSL_PARAM_OCTET_STRING) {
1599             const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
1600             BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
1601             const EC_GROUP *ecg = EC_KEY_get0_group(eckey);
1602             const EC_POINT *point = EC_KEY_get0_public_key(eckey);
1603
1604             ctx->sz = EC_POINT_point2buf(ecg, point,
1605                                          POINT_CONVERSION_COMPRESSED,
1606                                          &buf, bnctx);
1607             ctx->p2 = buf;
1608             break;
1609         }
1610         return 0;
1611 #endif
1612     default:
1613         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1614         return 0;
1615     }
1616
1617     ret = default_fixup_args(state, translation, ctx);
1618     OPENSSL_free(buf);
1619     return ret;
1620 }
1621
1622 static int get_payload_bn(enum state state,
1623                           const struct translation_st *translation,
1624                           struct translation_ctx_st *ctx, const BIGNUM *bn)
1625 {
1626     if (bn == NULL)
1627         return 0;
1628     if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1629         return 0;
1630     ctx->p2 = (BIGNUM *)bn;
1631
1632     return default_fixup_args(state, translation, ctx);
1633 }
1634
1635 static int get_dh_dsa_payload_p(enum state state,
1636                                 const struct translation_st *translation,
1637                                 struct translation_ctx_st *ctx)
1638 {
1639     const BIGNUM *bn = NULL;
1640     EVP_PKEY *pkey = ctx->p2;
1641
1642     switch (EVP_PKEY_base_id(pkey)) {
1643 #ifndef OPENSSL_NO_DH
1644     case EVP_PKEY_DH:
1645         bn = DH_get0_p(EVP_PKEY_get0_DH(pkey));
1646         break;
1647 #endif
1648 #ifndef OPENSSL_NO_DSA
1649     case EVP_PKEY_DSA:
1650         bn = DSA_get0_p(EVP_PKEY_get0_DSA(pkey));
1651         break;
1652 #endif
1653     default:
1654         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1655     }
1656
1657     return get_payload_bn(state, translation, ctx, bn);
1658 }
1659
1660 static int get_dh_dsa_payload_q(enum state state,
1661                                 const struct translation_st *translation,
1662                                 struct translation_ctx_st *ctx)
1663 {
1664     const BIGNUM *bn = NULL;
1665
1666     switch (EVP_PKEY_base_id(ctx->p2)) {
1667 #ifndef OPENSSL_NO_DH
1668     case EVP_PKEY_DH:
1669         bn = DH_get0_q(EVP_PKEY_get0_DH(ctx->p2));
1670         break;
1671 #endif
1672 #ifndef OPENSSL_NO_DSA
1673     case EVP_PKEY_DSA:
1674         bn = DSA_get0_q(EVP_PKEY_get0_DSA(ctx->p2));
1675         break;
1676 #endif
1677     }
1678
1679     return get_payload_bn(state, translation, ctx, bn);
1680 }
1681
1682 static int get_dh_dsa_payload_g(enum state state,
1683                                 const struct translation_st *translation,
1684                                 struct translation_ctx_st *ctx)
1685 {
1686     const BIGNUM *bn = NULL;
1687
1688     switch (EVP_PKEY_base_id(ctx->p2)) {
1689 #ifndef OPENSSL_NO_DH
1690     case EVP_PKEY_DH:
1691         bn = DH_get0_g(EVP_PKEY_get0_DH(ctx->p2));
1692         break;
1693 #endif
1694 #ifndef OPENSSL_NO_DSA
1695     case EVP_PKEY_DSA:
1696         bn = DSA_get0_g(EVP_PKEY_get0_DSA(ctx->p2));
1697         break;
1698 #endif
1699     }
1700
1701     return get_payload_bn(state, translation, ctx, bn);
1702 }
1703
1704 static int get_rsa_payload_n(enum state state,
1705                              const struct translation_st *translation,
1706                              struct translation_ctx_st *ctx)
1707 {
1708     const BIGNUM *bn = NULL;
1709
1710     if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)
1711         return 0;
1712     bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
1713
1714     return get_payload_bn(state, translation, ctx, bn);
1715 }
1716
1717 static int get_rsa_payload_e(enum state state,
1718                              const struct translation_st *translation,
1719                              struct translation_ctx_st *ctx)
1720 {
1721     const BIGNUM *bn = NULL;
1722
1723     if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)
1724         return 0;
1725     bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
1726
1727     return get_payload_bn(state, translation, ctx, bn);
1728 }
1729
1730 static int get_rsa_payload_d(enum state state,
1731                              const struct translation_st *translation,
1732                              struct translation_ctx_st *ctx)
1733 {
1734     const BIGNUM *bn = NULL;
1735
1736     if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)
1737         return 0;
1738     bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
1739
1740     return get_payload_bn(state, translation, ctx, bn);
1741 }
1742
1743 static int get_rsa_payload_factor(enum state state,
1744                                   const struct translation_st *translation,
1745                                   struct translation_ctx_st *ctx,
1746                                   size_t factornum)
1747 {
1748     const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1749     const BIGNUM *bn = NULL;
1750
1751     switch (factornum) {
1752     case 0:
1753         bn = RSA_get0_p(r);
1754         break;
1755     case 1:
1756         bn = RSA_get0_q(r);
1757         break;
1758     default:
1759         {
1760             size_t pnum = RSA_get_multi_prime_extra_count(r);
1761             const BIGNUM *factors[10];
1762
1763             if (factornum - 2 < pnum
1764                 && RSA_get0_multi_prime_factors(r, factors))
1765                 bn = factors[factornum - 2];
1766         }
1767         break;
1768     }
1769
1770     return get_payload_bn(state, translation, ctx, bn);
1771 }
1772
1773 static int get_rsa_payload_exponent(enum state state,
1774                                     const struct translation_st *translation,
1775                                     struct translation_ctx_st *ctx,
1776                                     size_t exponentnum)
1777 {
1778     const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1779     const BIGNUM *bn = NULL;
1780
1781     switch (exponentnum) {
1782     case 0:
1783         bn = RSA_get0_dmp1(r);
1784         break;
1785     case 1:
1786         bn = RSA_get0_dmq1(r);
1787         break;
1788     default:
1789         {
1790             size_t pnum = RSA_get_multi_prime_extra_count(r);
1791             const BIGNUM *exps[10], *coeffs[10];
1792
1793             if (exponentnum - 2 < pnum
1794                 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1795                 bn = exps[exponentnum - 2];
1796         }
1797         break;
1798     }
1799
1800     return get_payload_bn(state, translation, ctx, bn);
1801 }
1802
1803 static int get_rsa_payload_coefficient(enum state state,
1804                                        const struct translation_st *translation,
1805                                        struct translation_ctx_st *ctx,
1806                                        size_t coefficientnum)
1807 {
1808     const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1809     const BIGNUM *bn = NULL;
1810
1811     switch (coefficientnum) {
1812     case 0:
1813         bn = RSA_get0_iqmp(r);
1814         break;
1815     default:
1816         {
1817             size_t pnum = RSA_get_multi_prime_extra_count(r);
1818             const BIGNUM *exps[10], *coeffs[10];
1819
1820             if (coefficientnum - 1 < pnum
1821                 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1822                 bn = coeffs[coefficientnum - 1];
1823         }
1824         break;
1825     }
1826
1827     return get_payload_bn(state, translation, ctx, bn);
1828 }
1829
1830 #define IMPL_GET_RSA_PAYLOAD_FACTOR(n)                                  \
1831     static int                                                          \
1832     get_rsa_payload_f##n(enum state state,                              \
1833                          const struct translation_st *translation,      \
1834                          struct translation_ctx_st *ctx)                \
1835     {                                                                   \
1836         if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)                  \
1837             return 0;                                                   \
1838         return get_rsa_payload_factor(state, translation, ctx, n - 1);  \
1839     }
1840
1841 #define IMPL_GET_RSA_PAYLOAD_EXPONENT(n)                                \
1842     static int                                                          \
1843     get_rsa_payload_e##n(enum state state,                              \
1844                          const struct translation_st *translation,      \
1845                          struct translation_ctx_st *ctx)                \
1846     {                                                                   \
1847         if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)                  \
1848             return 0;                                                   \
1849         return get_rsa_payload_exponent(state, translation, ctx,        \
1850                                         n - 1);                         \
1851     }
1852
1853 #define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n)                             \
1854     static int                                                          \
1855     get_rsa_payload_c##n(enum state state,                              \
1856                          const struct translation_st *translation,      \
1857                          struct translation_ctx_st *ctx)                \
1858     {                                                                   \
1859         if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)                  \
1860             return 0;                                                   \
1861         return get_rsa_payload_coefficient(state, translation, ctx,     \
1862                                            n - 1);                      \
1863     }
1864
1865 IMPL_GET_RSA_PAYLOAD_FACTOR(1)
1866 IMPL_GET_RSA_PAYLOAD_FACTOR(2)
1867 IMPL_GET_RSA_PAYLOAD_FACTOR(3)
1868 IMPL_GET_RSA_PAYLOAD_FACTOR(4)
1869 IMPL_GET_RSA_PAYLOAD_FACTOR(5)
1870 IMPL_GET_RSA_PAYLOAD_FACTOR(6)
1871 IMPL_GET_RSA_PAYLOAD_FACTOR(7)
1872 IMPL_GET_RSA_PAYLOAD_FACTOR(8)
1873 IMPL_GET_RSA_PAYLOAD_FACTOR(9)
1874 IMPL_GET_RSA_PAYLOAD_FACTOR(10)
1875 IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
1876 IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
1877 IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
1878 IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
1879 IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
1880 IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
1881 IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
1882 IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
1883 IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
1884 IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
1885 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
1886 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
1887 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
1888 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
1889 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
1890 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
1891 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
1892 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
1893 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
1894
1895 /*-
1896  * The translation table itself
1897  * ============================
1898  */
1899
1900 static const struct translation_st evp_pkey_ctx_translations[] = {
1901     /*
1902      * DistID: we pass it to the backend as an octet string,
1903      * but get it back as a pointer to an octet string.
1904      *
1905      * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
1906      * that has no separate counterpart in OSSL_PARAM terms, since we get
1907      * the length of the DistID automatically when getting the DistID itself.
1908      */
1909     { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
1910       EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
1911       OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
1912     { GET, -1, -1, -1,
1913       EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
1914       OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
1915     { GET, -1, -1, -1,
1916       EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
1917       OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
1918
1919     /*-
1920      * DH & DHX
1921      * ========
1922      */
1923
1924     /*
1925      * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting.  The
1926      * fixup function has to handle this...
1927      */
1928     { NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1929       EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
1930       OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
1931       fix_dh_kdf_type },
1932     { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1933       EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
1934       OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1935     { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1936       EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
1937       OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1938     { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1939       EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
1940       OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1941     { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1942       EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
1943       OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1944     { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1945       EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
1946       OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
1947     { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1948       EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
1949       OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
1950     { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1951       EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
1952       OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
1953     { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1954       EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
1955       OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
1956
1957     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
1958       EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
1959       OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1960
1961     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
1962       EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
1963       OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
1964     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1965       EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, NULL, NULL,
1966       OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1967     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1968       EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
1969       OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1970     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1971       EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
1972       OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
1973     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1974       EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
1975       OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
1976  /*
1977   * This is know to be incorrect, will be fixed and enabled when the
1978   * underlying code is corrected.
1979   * Until then, we simply don't support it here.
1980   */
1981 #if 0
1982     { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1983       EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
1984       OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_INTEGER, NULL },
1985 #endif
1986
1987     /*-
1988      * DSA
1989      * ===
1990      */
1991     { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
1992       EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
1993       OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1994     { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
1995       EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
1996       OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1997     { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
1998       EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
1999       OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2000
2001     /*-
2002      * EC
2003      * ==
2004      */
2005     { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2006       EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
2007       OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
2008     { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2009       EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
2010       OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2011       fix_ec_paramgen_curve_nid },
2012     /*
2013      * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
2014      * both for setting and getting.  The fixup function has to handle this...
2015      */
2016     { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2017       EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
2018       OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
2019       fix_ecdh_cofactor },
2020     { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2021       EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
2022       OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
2023     { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2024       EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
2025       OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2026     { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2027       EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
2028       OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2029     { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2030       EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
2031       OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2032     { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2033       EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
2034       OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2035     { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2036       EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
2037       OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2038     { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2039       EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
2040       OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2041
2042     /*-
2043      * RSA
2044      * ===
2045      */
2046
2047     /*
2048      * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
2049      * and can be both with OSSL_PARAM.  We standardise on strings here,
2050      * fix_rsa_padding_mode() does the work when the caller has a different
2051      * idea.
2052      */
2053     { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2054       EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2055       EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
2056       OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2057     { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2058       EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2059       EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
2060       OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2061
2062     { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2063       EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2064       EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
2065       OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2066     { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2067       EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2068       EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
2069       OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2070
2071     /*
2072      * RSA-PSS saltlen is essentially numeric, but certain values can be
2073      * expressed as keywords (strings) with ctrl_str.  The corresponding
2074      * OSSL_PARAM allows both forms.
2075      * fix_rsa_pss_saltlen() takes care of the distinction.
2076      */
2077     { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2078       EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
2079       OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2080       fix_rsa_pss_saltlen },
2081     { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2082       EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
2083       OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2084       fix_rsa_pss_saltlen },
2085
2086     { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2087       EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
2088       OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2089     { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2090       EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
2091       OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2092     /*
2093      * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
2094      * This is accomodated by default_fixup_args() above, which mimics that
2095      * expectation for any translation item where |ctrl_str| is NULL and
2096      * |ctrl_hexstr| is non-NULL.
2097      */
2098     { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2099       EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
2100       OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2101     { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2102       EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
2103       OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2104
2105     { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2106       EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
2107       OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2108     { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2109       EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
2110       OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2111     { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2112       EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
2113       OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
2114     { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2115       EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
2116       OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2117     { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2118       EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
2119       OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2120     { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2121       EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
2122       OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2123
2124     /* PKCS#7 and CMS hacks */
2125     { SET, -1, -1, EVP_PKEY_OP_ENCRYPT,
2126       EVP_PKEY_CTRL_PKCS7_ENCRYPT, NULL, NULL, NULL, 0, hack_pkcs7_cms },
2127     { SET, -1, -1, EVP_PKEY_OP_DECRYPT,
2128       EVP_PKEY_CTRL_PKCS7_DECRYPT, NULL, NULL, NULL, 0, hack_pkcs7_cms },
2129     { SET, -1, -1, EVP_PKEY_OP_ENCRYPT,
2130       EVP_PKEY_CTRL_CMS_ENCRYPT, NULL, NULL, NULL, 0, hack_pkcs7_cms },
2131     { SET, -1, -1, EVP_PKEY_OP_DECRYPT,
2132       EVP_PKEY_CTRL_CMS_DECRYPT, NULL, NULL, NULL, 0, hack_pkcs7_cms },
2133
2134     /*-
2135      * TLS1-PRF
2136      * ========
2137      */
2138     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2139       EVP_PKEY_CTRL_TLS_MD, "md", NULL,
2140       OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2141     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2142       EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
2143       OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
2144     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2145       EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
2146       OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
2147
2148     /*-
2149      * HKDF
2150      * ====
2151      */
2152     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2153       EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
2154       OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2155     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2156       EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
2157       OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2158     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2159       EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
2160       OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2161     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2162       EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
2163       OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
2164     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2165       EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
2166       OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
2167
2168     /*-
2169      * Scrypt
2170      * ======
2171      */
2172     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2173       EVP_PKEY_CTRL_PASS, "pass", "hexpass",
2174       OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
2175     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2176       EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
2177       OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2178     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2179       EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
2180       OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2181     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2182       EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
2183       OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2184     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2185       EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
2186       OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2187     { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2188       EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
2189       OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2190
2191     { SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
2192       EVP_PKEY_CTRL_CIPHER, NULL, NULL,
2193       OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
2194     { SET, -1, -1, EVP_PKEY_OP_KEYGEN,
2195       EVP_PKEY_CTRL_SET_MAC_KEY, NULL, NULL,
2196       OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2197
2198     { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2199       EVP_PKEY_CTRL_MD, NULL, NULL,
2200       OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2201     { GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2202       EVP_PKEY_CTRL_GET_MD, NULL, NULL,
2203       OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2204 };
2205
2206 static const struct translation_st evp_pkey_translations[] = {
2207     /*
2208      * The following contain no ctrls, they are exclusively here to extract
2209      * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
2210      * on |fixup_args| to pass the actual data.  The |fixup_args| should
2211      * expect to get the EVP_PKEY pointer through |ctx->p2|.
2212      */
2213
2214     /* DH, DSA & EC */
2215     { GET, -1, -1, -1, 0, NULL, NULL,
2216       OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2217       get_payload_group_name },
2218     { GET, -1, -1, -1, 0, NULL, NULL,
2219       OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
2220       get_payload_private_key },
2221     { GET, -1, -1, -1, 0, NULL, NULL,
2222       OSSL_PKEY_PARAM_PUB_KEY,
2223       0 /* no data type, let get_payload_pub_key() handle that */,
2224       get_payload_public_key },
2225
2226     /* DH and DSA */
2227     { GET, -1, -1, -1, 0, NULL, NULL,
2228       OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
2229       get_dh_dsa_payload_p },
2230     { GET, -1, -1, -1, 0, NULL, NULL,
2231       OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
2232       get_dh_dsa_payload_g },
2233     { GET, -1, -1, -1, 0, NULL, NULL,
2234       OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
2235       get_dh_dsa_payload_q },
2236
2237     /* RSA */
2238     { GET, -1, -1, -1, 0, NULL, NULL,
2239       OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
2240       get_rsa_payload_n },
2241     { GET, -1, -1, -1, 0, NULL, NULL,
2242       OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
2243       get_rsa_payload_e },
2244     { GET, -1, -1, -1, 0, NULL, NULL,
2245       OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
2246       get_rsa_payload_d },
2247     { GET, -1, -1, -1, 0, NULL, NULL,
2248       OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
2249       get_rsa_payload_f1 },
2250     { GET, -1, -1, -1, 0, NULL, NULL,
2251       OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
2252       get_rsa_payload_f2 },
2253     { GET, -1, -1, -1, 0, NULL, NULL,
2254       OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
2255       get_rsa_payload_f3 },
2256     { GET, -1, -1, -1, 0, NULL, NULL,
2257       OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
2258       get_rsa_payload_f4 },
2259     { GET, -1, -1, -1, 0, NULL, NULL,
2260       OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
2261       get_rsa_payload_f5 },
2262     { GET, -1, -1, -1, 0, NULL, NULL,
2263       OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
2264       get_rsa_payload_f6 },
2265     { GET, -1, -1, -1, 0, NULL, NULL,
2266       OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
2267       get_rsa_payload_f7 },
2268     { GET, -1, -1, -1, 0, NULL, NULL,
2269       OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
2270       get_rsa_payload_f8 },
2271     { GET, -1, -1, -1, 0, NULL, NULL,
2272       OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
2273       get_rsa_payload_f9 },
2274     { GET, -1, -1, -1, 0, NULL, NULL,
2275       OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
2276       get_rsa_payload_f10 },
2277     { GET, -1, -1, -1, 0, NULL, NULL,
2278       OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2279       get_rsa_payload_e1 },
2280     { GET, -1, -1, -1, 0, NULL, NULL,
2281       OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2282       get_rsa_payload_e2 },
2283     { GET, -1, -1, -1, 0, NULL, NULL,
2284       OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2285       get_rsa_payload_e3 },
2286     { GET, -1, -1, -1, 0, NULL, NULL,
2287       OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2288       get_rsa_payload_e4 },
2289     { GET, -1, -1, -1, 0, NULL, NULL,
2290       OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2291       get_rsa_payload_e5 },
2292     { GET, -1, -1, -1, 0, NULL, NULL,
2293       OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2294       get_rsa_payload_e6 },
2295     { GET, -1, -1, -1, 0, NULL, NULL,
2296       OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2297       get_rsa_payload_e7 },
2298     { GET, -1, -1, -1, 0, NULL, NULL,
2299       OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2300       get_rsa_payload_e8 },
2301     { GET, -1, -1, -1, 0, NULL, NULL,
2302       OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2303       get_rsa_payload_e9 },
2304     { GET, -1, -1, -1, 0, NULL, NULL,
2305       OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
2306       get_rsa_payload_e10 },
2307     { GET, -1, -1, -1, 0, NULL, NULL,
2308       OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2309       get_rsa_payload_c1 },
2310     { GET, -1, -1, -1, 0, NULL, NULL,
2311       OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2312       get_rsa_payload_c2 },
2313     { GET, -1, -1, -1, 0, NULL, NULL,
2314       OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2315       get_rsa_payload_c3 },
2316     { GET, -1, -1, -1, 0, NULL, NULL,
2317       OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2318       get_rsa_payload_c4 },
2319     { GET, -1, -1, -1, 0, NULL, NULL,
2320       OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2321       get_rsa_payload_c5 },
2322     { GET, -1, -1, -1, 0, NULL, NULL,
2323       OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2324       get_rsa_payload_c6 },
2325     { GET, -1, -1, -1, 0, NULL, NULL,
2326       OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2327       get_rsa_payload_c7 },
2328     { GET, -1, -1, -1, 0, NULL, NULL,
2329       OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2330       get_rsa_payload_c8 },
2331     { GET, -1, -1, -1, 0, NULL, NULL,
2332       OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2333       get_rsa_payload_c9 },
2334 };
2335
2336 static const struct translation_st *
2337 lookup_translation(struct translation_st *tmpl,
2338                    const struct translation_st *translations,
2339                    size_t translations_num)
2340 {
2341     size_t i;
2342
2343     for (i = 0; i < translations_num; i++) {
2344         const struct translation_st *item = &translations[i];
2345
2346         /*
2347          * Sanity check the translation table item.
2348          *
2349          * 1.  Either both keytypes are -1, or neither of them are.
2350          * 2.  TBA...
2351          */
2352         if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
2353             continue;
2354
2355
2356         /*
2357          * Base search criteria: check that the optype and keytypes match,
2358          * if relevant.  All callers must synthesise these bits somehow.
2359          */
2360         if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
2361             continue;
2362         /*
2363          * This expression is stunningly simple thanks to the sanity check
2364          * above.
2365          */
2366         if (item->keytype1 != -1
2367             && tmpl->keytype1 != item->keytype1
2368             && tmpl->keytype2 != item->keytype2)
2369             continue;
2370
2371         /*
2372          * Done with the base search criteria, now we check the criteria for
2373          * the individual types of translations:
2374          * ctrl->params, ctrl_str->params, and params->ctrl
2375          */
2376         if (tmpl->ctrl_num != 0) {
2377             if (tmpl->ctrl_num != item->ctrl_num)
2378                 continue;
2379         } else if (tmpl->ctrl_str != NULL) {
2380             const char *ctrl_str = NULL;
2381             const char *ctrl_hexstr = NULL;
2382
2383             /*
2384              * Search criteria that originates from a ctrl_str is only used
2385              * for setting, never for getting.  Therefore, we only look at
2386              * the setter items.
2387              */
2388             if (item->action_type != NONE
2389                 && item->action_type != SET)
2390                 continue;
2391             /*
2392              * At least one of the ctrl cmd names must be match the ctrl
2393              * cmd name in the template.
2394              */
2395             if (item->ctrl_str != NULL
2396                 && strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
2397                 ctrl_str = tmpl->ctrl_str;
2398             else if (item->ctrl_hexstr != NULL
2399                      && strcasecmp(tmpl->ctrl_hexstr, item->ctrl_hexstr) == 0)
2400                 ctrl_hexstr = tmpl->ctrl_hexstr;
2401             else
2402                 continue;
2403
2404             /* Modify the template to signal which string matched */
2405             tmpl->ctrl_str = ctrl_str;
2406             tmpl->ctrl_hexstr = ctrl_hexstr;
2407         } else if (tmpl->param_key != NULL) {
2408             /*
2409              * Search criteria that originates from a OSSL_PARAM setter or
2410              * getter.
2411              *
2412              * Ctrls were fundamentally bidirectional, with only the ctrl
2413              * command macro name implying direction (if you're lucky).
2414              * A few ctrl commands were even taking advantage of the
2415              * bidirectional nature, making the direction depend in the
2416              * value of the numeric argument.
2417              *
2418              * OSSL_PARAM functions are fundamentally different, in that
2419              * setters and getters are separated, so the data direction is
2420              * implied by the function that's used.  The same OSSL_PARAM
2421              * key name can therefore be used in both directions.  We must
2422              * therefore take the action type into account in this case.
2423              */
2424             if ((item->action_type != NONE
2425                  && tmpl->action_type != item->action_type)
2426                 || (item->param_key != NULL
2427                     && strcasecmp(tmpl->param_key, item->param_key) != 0))
2428                 continue;
2429         } else {
2430             return NULL;
2431         }
2432
2433         return item;
2434     }
2435
2436     return NULL;
2437 }
2438
2439 static const struct translation_st *
2440 lookup_evp_pkey_ctx_translation(struct translation_st *tmpl)
2441 {
2442     return lookup_translation(tmpl, evp_pkey_ctx_translations,
2443                               OSSL_NELEM(evp_pkey_ctx_translations));
2444 }
2445
2446 static const struct translation_st *
2447 lookup_evp_pkey_translation(struct translation_st *tmpl)
2448 {
2449     return lookup_translation(tmpl, evp_pkey_translations,
2450                               OSSL_NELEM(evp_pkey_translations));
2451 }
2452
2453 /* This must ONLY be called for provider side operations */
2454 int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *pctx,
2455                                int keytype, int optype,
2456                                int cmd, int p1, void *p2)
2457 {
2458     struct translation_ctx_st ctx = { 0, };
2459     struct translation_st tmpl = { 0, };
2460     const struct translation_st *translation = NULL;
2461     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2462     int ret;
2463     fixup_args_fn *fixup = default_fixup_args;
2464
2465     if (keytype == -1)
2466         keytype = pctx->legacy_keytype;
2467     tmpl.ctrl_num = cmd;
2468     tmpl.keytype1 = tmpl.keytype2 = keytype;
2469     tmpl.optype = optype;
2470     translation = lookup_evp_pkey_ctx_translation(&tmpl);
2471
2472     if (translation == NULL) {
2473         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
2474         return -2;
2475     }
2476
2477     if (pctx->pmeth != NULL
2478         && pctx->pmeth->pkey_id != translation->keytype1
2479         && pctx->pmeth->pkey_id != translation->keytype2)
2480         return -1;
2481
2482     if (translation->fixup_args != NULL)
2483         fixup = translation->fixup_args;
2484     ctx.action_type = translation->action_type;
2485     ctx.ctrl_cmd = cmd;
2486     ctx.p1 = p1;
2487     ctx.p2 = p2;
2488     ctx.pctx = pctx;
2489     ctx.params = params;
2490
2491     ret = fixup(PRE_CTRL_TO_PARAMS, translation, &ctx);
2492
2493     if (ret > 0) {
2494         switch (ctx.action_type) {
2495         default:
2496             /* fixup_args is expected to make sure this is dead code */
2497             break;
2498         case GET:
2499             ret = evp_pkey_ctx_get_params_strict(pctx, ctx.params);
2500             break;
2501         case SET:
2502             ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2503             break;
2504         }
2505     }
2506
2507     /*
2508      * In POST, we pass the return value as p1, allowing the fixup_args
2509      * function to affect it by changing its value.
2510      */
2511     if (ret > 0) {
2512         ctx.p1 = ret;
2513         fixup(POST_CTRL_TO_PARAMS, translation, &ctx);
2514         ret = ctx.p1;
2515     }
2516
2517     cleanup_translation_ctx(POST_CTRL_TO_PARAMS, translation, &ctx);
2518
2519     return ret;
2520 }
2521
2522 /* This must ONLY be called for provider side operations */
2523 int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *pctx,
2524                                    const char *name, const char *value)
2525 {
2526     struct translation_ctx_st ctx = { 0, };
2527     struct translation_st tmpl = { 0, };
2528     const struct translation_st *translation = NULL;
2529     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2530     int keytype = pctx->legacy_keytype;
2531     int optype = pctx->operation == 0 ? -1 : pctx->operation;
2532     int ret;
2533     fixup_args_fn *fixup = default_fixup_args;
2534
2535     tmpl.action_type = SET;
2536     tmpl.keytype1 = tmpl.keytype2 = keytype;
2537     tmpl.optype = optype;
2538     tmpl.ctrl_str = name;
2539     tmpl.ctrl_hexstr = name;
2540     translation = lookup_evp_pkey_ctx_translation(&tmpl);
2541
2542     if (translation != NULL) {
2543         if (translation->fixup_args != NULL)
2544             fixup = translation->fixup_args;
2545         ctx.action_type = translation->action_type;
2546         ctx.ishex = (tmpl.ctrl_hexstr != NULL);
2547     } else {
2548         /* String controls really only support setting */
2549         ctx.action_type = SET;
2550     }
2551     ctx.ctrl_str = name;
2552     ctx.p1 = (int)strlen(value);
2553     ctx.p2 = (char *)value;
2554     ctx.pctx = pctx;
2555     ctx.params = params;
2556
2557     ret = fixup(PRE_CTRL_STR_TO_PARAMS, translation, &ctx);
2558
2559     if (ret > 0) {
2560         switch (ctx.action_type) {
2561         default:
2562             /* fixup_args is expected to make sure this is dead code */
2563             break;
2564         case GET:
2565             /*
2566              * this is dead code, but must be present, or some compilers
2567              * will complain
2568              */
2569             break;
2570         case SET:
2571             ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2572             break;
2573         }
2574     }
2575
2576     if (ret > 0)
2577         ret = fixup(POST_CTRL_STR_TO_PARAMS, translation, &ctx);
2578
2579     cleanup_translation_ctx(CLEANUP_CTRL_STR_TO_PARAMS, translation, &ctx);
2580
2581     return ret;
2582 }
2583
2584 /* This must ONLY be called for legacy operations */
2585 static int evp_pkey_ctx_setget_params_to_ctrl(EVP_PKEY_CTX *pctx,
2586                                               enum action action_type,
2587                                               OSSL_PARAM *params)
2588 {
2589     int keytype = pctx->legacy_keytype;
2590     int optype = pctx->operation == 0 ? -1 : pctx->operation;
2591
2592     for (; params != NULL && params->key != NULL; params++) {
2593         struct translation_ctx_st ctx = { 0, };
2594         struct translation_st tmpl = { 0, };
2595         const struct translation_st *translation = NULL;
2596         fixup_args_fn *fixup = default_fixup_args;
2597         int ret;
2598
2599         tmpl.action_type = action_type;
2600         tmpl.keytype1 = tmpl.keytype2 = keytype;
2601         tmpl.optype = optype;
2602         tmpl.param_key = params->key;
2603         translation = lookup_evp_pkey_ctx_translation(&tmpl);
2604
2605         if (translation != NULL) {
2606             if (translation->fixup_args != NULL)
2607                 fixup = translation->fixup_args;
2608             ctx.action_type = translation->action_type;
2609         }
2610         ctx.pctx = pctx;
2611         ctx.params = params;
2612
2613         ret = fixup(PRE_PARAMS_TO_CTRL, translation, &ctx);
2614
2615         if (ret > 0 && action_type != NONE)
2616             ret = EVP_PKEY_CTX_ctrl(pctx, keytype, optype,
2617                                     ctx.ctrl_cmd, ctx.p1, ctx.p2);
2618
2619         /*
2620          * In POST, we pass the return value as p1, allowing the fixup_args
2621          * function to put it to good use, or maybe affect it.
2622          */
2623         if (ret > 0) {
2624             ctx.p1 = ret;
2625             fixup(POST_PARAMS_TO_CTRL, translation, &ctx);
2626             ret = ctx.p1;
2627         }
2628
2629         cleanup_translation_ctx(CLEANUP_PARAMS_TO_CTRL, translation, &ctx);
2630
2631         if (ret <= 0)
2632             return 0;
2633     }
2634     return 1;
2635 }
2636
2637 int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
2638 {
2639     return evp_pkey_ctx_setget_params_to_ctrl(ctx, SET, params);
2640 }
2641
2642 int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
2643 {
2644     return evp_pkey_ctx_setget_params_to_ctrl(ctx, GET, params);
2645 }
2646
2647 /* This must ONLY be called for legacy EVP_PKEYs */
2648 static int evp_pkey_setget_params_to_ctrl(const EVP_PKEY *pkey,
2649                                           enum action action_type,
2650                                           OSSL_PARAM *params)
2651 {
2652     int ret = 1;
2653
2654     for (; params != NULL && params->key != NULL; params++) {
2655         struct translation_ctx_st ctx = { 0, };
2656         struct translation_st tmpl = { 0, };
2657         const struct translation_st *translation = NULL;
2658         fixup_args_fn *fixup = default_fixup_args;
2659
2660         tmpl.action_type = action_type;
2661         tmpl.param_key = params->key;
2662         translation = lookup_evp_pkey_translation(&tmpl);
2663
2664         if (translation != NULL) {
2665             if (translation->fixup_args != NULL)
2666                 fixup = translation->fixup_args;
2667             ctx.action_type = translation->action_type;
2668         }
2669         ctx.p2 = (void *)pkey;
2670         ctx.params = params;
2671
2672         /*
2673          * EVP_PKEY doesn't have any ctrl function, so we rely completely
2674          * on fixup_args to do the whole work.  Also, we currently only
2675          * support getting.
2676          */
2677         if (!ossl_assert(translation != NULL)
2678             || !ossl_assert(translation->action_type == GET)
2679             || !ossl_assert(translation->fixup_args != NULL)) {
2680             return -2;
2681         }
2682
2683         ret = fixup(PKEY, translation, &ctx);
2684
2685         cleanup_translation_ctx(PKEY, translation, &ctx);
2686     }
2687     return ret;
2688 }
2689
2690 int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params)
2691 {
2692     return evp_pkey_setget_params_to_ctrl(pkey, GET, params);
2693 }
2694