Coverage Report

Created: 2025-06-13 06:58

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