Coverage Report

Created: 2025-12-31 06:58

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