Coverage Report

Created: 2023-06-08 06:40

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