Coverage Report

Created: 2026-02-22 06:11

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