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