Coverage Report

Created: 2025-06-13 06:58

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