Coverage Report

Created: 2025-12-10 06:24

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_rsa_payload_n(enum state state,
1837
    const struct translation_st *translation,
1838
    struct translation_ctx_st *ctx)
1839
0
{
1840
0
    const BIGNUM *bn = NULL;
1841
1842
0
    if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA
1843
0
        && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)
1844
0
        return 0;
1845
0
    bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
1846
1847
0
    return get_payload_bn(state, translation, ctx, bn);
1848
0
}
1849
1850
static int get_rsa_payload_e(enum state state,
1851
    const struct translation_st *translation,
1852
    struct translation_ctx_st *ctx)
1853
0
{
1854
0
    const BIGNUM *bn = NULL;
1855
1856
0
    if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA
1857
0
        && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)
1858
0
        return 0;
1859
0
    bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
1860
1861
0
    return get_payload_bn(state, translation, ctx, bn);
1862
0
}
1863
1864
static int get_rsa_payload_d(enum state state,
1865
    const struct translation_st *translation,
1866
    struct translation_ctx_st *ctx)
1867
0
{
1868
0
    const BIGNUM *bn = NULL;
1869
1870
0
    if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA
1871
0
        && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)
1872
0
        return 0;
1873
0
    bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
1874
1875
0
    return get_payload_bn(state, translation, ctx, bn);
1876
0
}
1877
1878
static int get_rsa_payload_factor(enum state state,
1879
    const struct translation_st *translation,
1880
    struct translation_ctx_st *ctx,
1881
    size_t factornum)
1882
0
{
1883
0
    const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1884
0
    const BIGNUM *bn = NULL;
1885
1886
0
    switch (factornum) {
1887
0
    case 0:
1888
0
        bn = RSA_get0_p(r);
1889
0
        break;
1890
0
    case 1:
1891
0
        bn = RSA_get0_q(r);
1892
0
        break;
1893
0
    default: {
1894
0
        size_t pnum = RSA_get_multi_prime_extra_count(r);
1895
0
        const BIGNUM *factors[10];
1896
1897
0
        if (factornum - 2 < pnum
1898
0
            && RSA_get0_multi_prime_factors(r, factors))
1899
0
            bn = factors[factornum - 2];
1900
0
    } break;
1901
0
    }
1902
1903
0
    return get_payload_bn(state, translation, ctx, bn);
1904
0
}
1905
1906
static int get_rsa_payload_exponent(enum state state,
1907
    const struct translation_st *translation,
1908
    struct translation_ctx_st *ctx,
1909
    size_t exponentnum)
1910
0
{
1911
0
    const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1912
0
    const BIGNUM *bn = NULL;
1913
1914
0
    switch (exponentnum) {
1915
0
    case 0:
1916
0
        bn = RSA_get0_dmp1(r);
1917
0
        break;
1918
0
    case 1:
1919
0
        bn = RSA_get0_dmq1(r);
1920
0
        break;
1921
0
    default: {
1922
0
        size_t pnum = RSA_get_multi_prime_extra_count(r);
1923
0
        const BIGNUM *exps[10], *coeffs[10];
1924
1925
0
        if (exponentnum - 2 < pnum
1926
0
            && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1927
0
            bn = exps[exponentnum - 2];
1928
0
    } break;
1929
0
    }
1930
1931
0
    return get_payload_bn(state, translation, ctx, bn);
1932
0
}
1933
1934
static int get_rsa_payload_coefficient(enum state state,
1935
    const struct translation_st *translation,
1936
    struct translation_ctx_st *ctx,
1937
    size_t coefficientnum)
1938
0
{
1939
0
    const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1940
0
    const BIGNUM *bn = NULL;
1941
1942
0
    switch (coefficientnum) {
1943
0
    case 0:
1944
0
        bn = RSA_get0_iqmp(r);
1945
0
        break;
1946
0
    default: {
1947
0
        size_t pnum = RSA_get_multi_prime_extra_count(r);
1948
0
        const BIGNUM *exps[10], *coeffs[10];
1949
1950
0
        if (coefficientnum - 1 < pnum
1951
0
            && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1952
0
            bn = coeffs[coefficientnum - 1];
1953
0
    } break;
1954
0
    }
1955
1956
0
    return get_payload_bn(state, translation, ctx, bn);
1957
0
}
1958
1959
#define IMPL_GET_RSA_PAYLOAD_FACTOR(n)                                 \
1960
    static int                                                         \
1961
    get_rsa_payload_f##n(enum state state,                             \
1962
        const struct translation_st *translation,                      \
1963
        struct translation_ctx_st *ctx)                                \
1964
0
    {                                                                  \
1965
0
        if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA              \
1966
0
            && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)      \
1967
0
            return 0;                                                  \
1968
0
        return get_rsa_payload_factor(state, translation, ctx, n - 1); \
1969
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
1970
1971
#define IMPL_GET_RSA_PAYLOAD_EXPONENT(n)                          \
1972
    static int                                                    \
1973
    get_rsa_payload_e##n(enum state state,                        \
1974
        const struct translation_st *translation,                 \
1975
        struct translation_ctx_st *ctx)                           \
1976
0
    {                                                             \
1977
0
        if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA         \
1978
0
            && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS) \
1979
0
            return 0;                                             \
1980
0
        return get_rsa_payload_exponent(state, translation, ctx,  \
1981
0
            n - 1);                                               \
1982
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
1983
1984
#define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n)                         \
1985
    static int                                                      \
1986
    get_rsa_payload_c##n(enum state state,                          \
1987
        const struct translation_st *translation,                   \
1988
        struct translation_ctx_st *ctx)                             \
1989
0
    {                                                               \
1990
0
        if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA           \
1991
0
            && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)   \
1992
0
            return 0;                                               \
1993
0
        return get_rsa_payload_coefficient(state, translation, ctx, \
1994
0
            n - 1);                                                 \
1995
0
    }
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c1
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c2
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c3
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c4
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c5
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c6
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c7
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c8
Unexecuted instantiation: ctrl_params_translate.c:get_rsa_payload_c9
1996
1997
IMPL_GET_RSA_PAYLOAD_FACTOR(1)
1998
IMPL_GET_RSA_PAYLOAD_FACTOR(2)
1999
IMPL_GET_RSA_PAYLOAD_FACTOR(3)
2000
IMPL_GET_RSA_PAYLOAD_FACTOR(4)
2001
IMPL_GET_RSA_PAYLOAD_FACTOR(5)
2002
IMPL_GET_RSA_PAYLOAD_FACTOR(6)
2003
IMPL_GET_RSA_PAYLOAD_FACTOR(7)
2004
IMPL_GET_RSA_PAYLOAD_FACTOR(8)
2005
IMPL_GET_RSA_PAYLOAD_FACTOR(9)
2006
IMPL_GET_RSA_PAYLOAD_FACTOR(10)
2007
IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
2008
IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
2009
IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
2010
IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
2011
IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
2012
IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
2013
IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
2014
IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
2015
IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
2016
IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
2017
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
2018
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
2019
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
2020
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
2021
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
2022
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
2023
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
2024
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
2025
IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
2026
2027
static int fix_group_ecx(enum state state,
2028
    const struct translation_st *translation,
2029
    struct translation_ctx_st *ctx)
2030
0
{
2031
0
    const char *value = NULL;
2032
2033
0
    switch (state) {
2034
0
    case PRE_PARAMS_TO_CTRL:
2035
0
        if (!EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx))
2036
0
            return 0;
2037
0
        ctx->action_type = OSSL_ACTION_NONE;
2038
0
        return 1;
2039
0
    case POST_PARAMS_TO_CTRL:
2040
0
        if (OSSL_PARAM_get_utf8_string_ptr(ctx->params, &value) == 0 || OPENSSL_strcasecmp(ctx->pctx->keytype, value) != 0) {
2041
0
            ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
2042
0
            ctx->p1 = 0;
2043
0
            return 0;
2044
0
        }
2045
0
        ctx->p1 = 1;
2046
0
        return 1;
2047
0
    default:
2048
0
        return 0;
2049
0
    }
2050
0
}
2051
2052
/*-
2053
 * The translation table itself
2054
 * ============================
2055
 */
2056
2057
static const struct translation_st evp_pkey_ctx_translations[] = {
2058
    /*
2059
     * DistID: we pass it to the backend as an octet string,
2060
     * but get it back as a pointer to an octet string.
2061
     *
2062
     * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
2063
     * that has no separate counterpart in OSSL_PARAM terms, since we get
2064
     * the length of the DistID automatically when getting the DistID itself.
2065
     */
2066
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2067
        EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
2068
        OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
2069
    { OSSL_ACTION_GET, -1, -1, -1,
2070
        EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
2071
        OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
2072
    { OSSL_ACTION_GET, -1, -1, -1,
2073
        EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
2074
        OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
2075
2076
    /*-
2077
     * DH & DHX
2078
     * ========
2079
     */
2080
2081
    /*
2082
     * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting.  The
2083
     * fixup function has to handle this...
2084
     */
2085
    { OSSL_ACTION_NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2086
        EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
2087
        OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
2088
        fix_dh_kdf_type },
2089
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2090
        EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
2091
        OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2092
    { OSSL_ACTION_GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2093
        EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
2094
        OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2095
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2096
        EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
2097
        OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2098
    { OSSL_ACTION_GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2099
        EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
2100
        OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2101
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2102
        EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
2103
        OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2104
    { OSSL_ACTION_GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2105
        EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
2106
        OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2107
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2108
        EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
2109
        OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
2110
    { OSSL_ACTION_GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2111
        EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
2112
        OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
2113
2114
    /* DHX Keygen Parameters that are shared with DH */
2115
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2116
        EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
2117
        OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
2118
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2119
        EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
2120
        OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2121
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2122
        EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
2123
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, NULL },
2124
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2125
        EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
2126
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
2127
2128
    /* DH Keygen Parameters that are shared with DHX */
2129
    { OSSL_ACTION_SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2130
        EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
2131
        OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
2132
    { OSSL_ACTION_SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2133
        EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
2134
        OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2135
    { OSSL_ACTION_SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2136
        EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
2137
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
2138
    { OSSL_ACTION_SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2139
        EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
2140
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
2141
2142
    /* DH specific Keygen Parameters */
2143
    { OSSL_ACTION_SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2144
        EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
2145
        OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
2146
2147
    /* DHX specific Keygen Parameters */
2148
    { OSSL_ACTION_SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2149
        EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
2150
        OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2151
2152
    { OSSL_ACTION_SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
2153
        EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
2154
        OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2155
2156
    /*-
2157
     * DSA
2158
     * ===
2159
     */
2160
    { OSSL_ACTION_SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2161
        EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
2162
        OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2163
    { OSSL_ACTION_SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2164
        EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
2165
        OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2166
    { OSSL_ACTION_SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2167
        EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
2168
        OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2169
2170
    /*-
2171
     * EC
2172
     * ==
2173
     */
2174
    { OSSL_ACTION_SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2175
        EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
2176
        OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
2177
    { OSSL_ACTION_SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2178
        EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
2179
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2180
        fix_ec_paramgen_curve_nid },
2181
    /*
2182
     * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
2183
     * both for setting and getting.  The fixup function has to handle this...
2184
     */
2185
    { OSSL_ACTION_NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2186
        EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
2187
        OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
2188
        fix_ecdh_cofactor },
2189
    { OSSL_ACTION_NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2190
        EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
2191
        OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
2192
    { OSSL_ACTION_SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2193
        EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
2194
        OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2195
    { OSSL_ACTION_GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2196
        EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
2197
        OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2198
    { OSSL_ACTION_SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2199
        EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
2200
        OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2201
    { OSSL_ACTION_GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2202
        EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
2203
        OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2204
    { OSSL_ACTION_SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2205
        EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
2206
        OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2207
    { OSSL_ACTION_GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2208
        EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
2209
        OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2210
2211
    /*-
2212
     * SM2
2213
     * ==
2214
     */
2215
    { OSSL_ACTION_SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2216
        EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
2217
        OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
2218
    { OSSL_ACTION_SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2219
        EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
2220
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2221
        fix_ec_paramgen_curve_nid },
2222
    /*
2223
     * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
2224
     * both for setting and getting.  The fixup function has to handle this...
2225
     */
2226
    { OSSL_ACTION_NONE, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2227
        EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
2228
        OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
2229
        fix_ecdh_cofactor },
2230
    { OSSL_ACTION_NONE, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2231
        EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
2232
        OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
2233
    { OSSL_ACTION_SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2234
        EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
2235
        OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2236
    { OSSL_ACTION_GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2237
        EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
2238
        OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2239
    { OSSL_ACTION_SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2240
        EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
2241
        OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2242
    { OSSL_ACTION_GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2243
        EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
2244
        OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2245
    { OSSL_ACTION_SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2246
        EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
2247
        OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2248
    { OSSL_ACTION_GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
2249
        EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
2250
        OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2251
    /*-
2252
     * RSA
2253
     * ===
2254
     */
2255
2256
    /*
2257
     * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
2258
     * and can be both with OSSL_PARAM.  We standardise on strings here,
2259
     * fix_rsa_padding_mode() does the work when the caller has a different
2260
     * idea.
2261
     */
2262
    { OSSL_ACTION_SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2263
        EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2264
        EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
2265
        OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2266
    { OSSL_ACTION_GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2267
        EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2268
        EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
2269
        OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2270
2271
    { OSSL_ACTION_SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2272
        EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2273
        EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
2274
        OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2275
    { OSSL_ACTION_GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2276
        EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2277
        EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
2278
        OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2279
2280
    /*
2281
     * RSA-PSS saltlen is essentially numeric, but certain values can be
2282
     * expressed as keywords (strings) with ctrl_str.  The corresponding
2283
     * OSSL_PARAM allows both forms.
2284
     * fix_rsa_pss_saltlen() takes care of the distinction.
2285
     */
2286
    { OSSL_ACTION_SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2287
        EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
2288
        OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2289
        fix_rsa_pss_saltlen },
2290
    { OSSL_ACTION_GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2291
        EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
2292
        OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2293
        fix_rsa_pss_saltlen },
2294
2295
    { OSSL_ACTION_SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2296
        EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
2297
        OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2298
    { OSSL_ACTION_GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2299
        EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
2300
        OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2301
    /*
2302
     * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
2303
     * This is accommodated by default_fixup_args() above, which mimics that
2304
     * expectation for any translation item where |ctrl_str| is NULL and
2305
     * |ctrl_hexstr| is non-NULL.
2306
     */
2307
    { OSSL_ACTION_SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2308
        EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
2309
        OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2310
    { OSSL_ACTION_GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2311
        EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
2312
        OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR, NULL },
2313
2314
    { OSSL_ACTION_SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2315
        EVP_PKEY_CTRL_RSA_IMPLICIT_REJECTION, NULL,
2316
        "rsa_pkcs1_implicit_rejection",
2317
        OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, OSSL_PARAM_UNSIGNED_INTEGER,
2318
        NULL },
2319
2320
    { OSSL_ACTION_SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2321
        EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
2322
        OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2323
    { OSSL_ACTION_SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2324
        EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
2325
        OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2326
    { OSSL_ACTION_SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2327
        EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
2328
        OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
2329
    { OSSL_ACTION_SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2330
        EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
2331
        OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2332
    { OSSL_ACTION_SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2333
        EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
2334
        OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2335
    { OSSL_ACTION_SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2336
        EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
2337
        OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2338
2339
    /*-
2340
     * SipHash
2341
     * ======
2342
     */
2343
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2344
        EVP_PKEY_CTRL_SET_DIGEST_SIZE, "digestsize", NULL,
2345
        OSSL_MAC_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2346
2347
    /*-
2348
     * TLS1-PRF
2349
     * ========
2350
     */
2351
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2352
        EVP_PKEY_CTRL_TLS_MD, "md", NULL,
2353
        OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2354
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2355
        EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
2356
        OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
2357
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2358
        EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
2359
        OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
2360
2361
    /*-
2362
     * HKDF
2363
     * ====
2364
     */
2365
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2366
        EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
2367
        OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2368
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2369
        EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
2370
        OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2371
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2372
        EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
2373
        OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2374
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2375
        EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
2376
        OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
2377
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2378
        EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
2379
        OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
2380
2381
    /*-
2382
     * Scrypt
2383
     * ======
2384
     */
2385
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2386
        EVP_PKEY_CTRL_PASS, "pass", "hexpass",
2387
        OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
2388
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2389
        EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
2390
        OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2391
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2392
        EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
2393
        OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2394
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2395
        EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
2396
        OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2397
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2398
        EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
2399
        OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2400
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_DERIVE,
2401
        EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
2402
        OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2403
2404
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
2405
        EVP_PKEY_CTRL_CIPHER, NULL, NULL,
2406
        OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
2407
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_KEYGEN,
2408
        EVP_PKEY_CTRL_SET_MAC_KEY, "key", "hexkey",
2409
        OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2410
2411
    { OSSL_ACTION_SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2412
        EVP_PKEY_CTRL_MD, NULL, NULL,
2413
        OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2414
    { OSSL_ACTION_GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2415
        EVP_PKEY_CTRL_GET_MD, NULL, NULL,
2416
        OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2417
2418
    /*-
2419
     * ECX
2420
     * ===
2421
     */
2422
    { OSSL_ACTION_SET, EVP_PKEY_X25519, EVP_PKEY_X25519, EVP_PKEY_OP_KEYGEN, -1, NULL, NULL,
2423
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
2424
    { OSSL_ACTION_SET, EVP_PKEY_X25519, EVP_PKEY_X25519, EVP_PKEY_OP_PARAMGEN, -1, NULL, NULL,
2425
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
2426
    { OSSL_ACTION_SET, EVP_PKEY_X448, EVP_PKEY_X448, EVP_PKEY_OP_KEYGEN, -1, NULL, NULL,
2427
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
2428
    { OSSL_ACTION_SET, EVP_PKEY_X448, EVP_PKEY_X448, EVP_PKEY_OP_PARAMGEN, -1, NULL, NULL,
2429
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
2430
};
2431
2432
static const struct translation_st evp_pkey_translations[] = {
2433
    /*
2434
     * The following contain no ctrls, they are exclusively here to extract
2435
     * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
2436
     * on |fixup_args| to pass the actual data.  The |fixup_args| should
2437
     * expect to get the EVP_PKEY pointer through |ctx->p2|.
2438
     */
2439
2440
    /* DH, DSA & EC */
2441
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2442
        OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2443
        get_payload_group_name },
2444
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2445
        OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
2446
        get_payload_private_key },
2447
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2448
        OSSL_PKEY_PARAM_PUB_KEY,
2449
        0 /* no data type, let get_payload_public_key() handle that */,
2450
        get_payload_public_key },
2451
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2452
        OSSL_PKEY_PARAM_EC_PUB_X, OSSL_PARAM_UNSIGNED_INTEGER,
2453
        get_payload_public_key_ec },
2454
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2455
        OSSL_PKEY_PARAM_EC_PUB_Y, OSSL_PARAM_UNSIGNED_INTEGER,
2456
        get_payload_public_key_ec },
2457
2458
    /* DH and DSA */
2459
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2460
        OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
2461
        get_dh_dsa_payload_p },
2462
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2463
        OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
2464
        get_dh_dsa_payload_g },
2465
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2466
        OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
2467
        get_dh_dsa_payload_q },
2468
2469
    /* RSA */
2470
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2471
        OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
2472
        get_rsa_payload_n },
2473
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2474
        OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
2475
        get_rsa_payload_e },
2476
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2477
        OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
2478
        get_rsa_payload_d },
2479
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2480
        OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
2481
        get_rsa_payload_f1 },
2482
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2483
        OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
2484
        get_rsa_payload_f2 },
2485
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2486
        OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
2487
        get_rsa_payload_f3 },
2488
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2489
        OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
2490
        get_rsa_payload_f4 },
2491
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2492
        OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
2493
        get_rsa_payload_f5 },
2494
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2495
        OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
2496
        get_rsa_payload_f6 },
2497
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2498
        OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
2499
        get_rsa_payload_f7 },
2500
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2501
        OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
2502
        get_rsa_payload_f8 },
2503
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2504
        OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
2505
        get_rsa_payload_f9 },
2506
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2507
        OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
2508
        get_rsa_payload_f10 },
2509
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2510
        OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2511
        get_rsa_payload_e1 },
2512
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2513
        OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2514
        get_rsa_payload_e2 },
2515
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2516
        OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2517
        get_rsa_payload_e3 },
2518
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2519
        OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2520
        get_rsa_payload_e4 },
2521
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2522
        OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2523
        get_rsa_payload_e5 },
2524
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2525
        OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2526
        get_rsa_payload_e6 },
2527
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2528
        OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2529
        get_rsa_payload_e7 },
2530
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2531
        OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2532
        get_rsa_payload_e8 },
2533
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2534
        OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2535
        get_rsa_payload_e9 },
2536
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2537
        OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
2538
        get_rsa_payload_e10 },
2539
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2540
        OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2541
        get_rsa_payload_c1 },
2542
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2543
        OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2544
        get_rsa_payload_c2 },
2545
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2546
        OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2547
        get_rsa_payload_c3 },
2548
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2549
        OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2550
        get_rsa_payload_c4 },
2551
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2552
        OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2553
        get_rsa_payload_c5 },
2554
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2555
        OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2556
        get_rsa_payload_c6 },
2557
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2558
        OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2559
        get_rsa_payload_c7 },
2560
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2561
        OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2562
        get_rsa_payload_c8 },
2563
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2564
        OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2565
        get_rsa_payload_c9 },
2566
2567
    /* EC */
2568
    { OSSL_ACTION_GET, -1, -1, -1, 0, NULL, NULL,
2569
        OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, OSSL_PARAM_INTEGER,
2570
        get_ec_decoded_from_explicit_params },
2571
};
2572
2573
static const struct translation_st *
2574
lookup_translation(struct translation_st *tmpl,
2575
    const struct translation_st *translations,
2576
    size_t translations_num)
2577
0
{
2578
0
    size_t i;
2579
2580
0
    for (i = 0; i < translations_num; i++) {
2581
0
        const struct translation_st *item = &translations[i];
2582
2583
        /*
2584
         * Sanity check the translation table item.
2585
         *
2586
         * 1.  Either both keytypes are -1, or neither of them are.
2587
         * 2.  TBA...
2588
         */
2589
0
        if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
2590
0
            continue;
2591
2592
        /*
2593
         * Base search criteria: check that the optype and keytypes match,
2594
         * if relevant.  All callers must synthesise these bits somehow.
2595
         */
2596
0
        if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
2597
0
            continue;
2598
        /*
2599
         * This expression is stunningly simple thanks to the sanity check
2600
         * above.
2601
         */
2602
0
        if (item->keytype1 != -1
2603
0
            && tmpl->keytype1 != item->keytype1
2604
0
            && tmpl->keytype2 != item->keytype2)
2605
0
            continue;
2606
2607
        /*
2608
         * Done with the base search criteria, now we check the criteria for
2609
         * the individual types of translations:
2610
         * ctrl->params, ctrl_str->params, and params->ctrl
2611
         */
2612
0
        if (tmpl->ctrl_num != 0) {
2613
0
            if (tmpl->ctrl_num != item->ctrl_num)
2614
0
                continue;
2615
0
        } else if (tmpl->ctrl_str != NULL) {
2616
0
            const char *ctrl_str = NULL;
2617
0
            const char *ctrl_hexstr = NULL;
2618
2619
            /*
2620
             * Search criteria that originates from a ctrl_str is only used
2621
             * for setting, never for getting.  Therefore, we only look at
2622
             * the setter items.
2623
             */
2624
0
            if (item->action_type != OSSL_ACTION_NONE
2625
0
                && item->action_type != OSSL_ACTION_SET)
2626
0
                continue;
2627
            /*
2628
             * At least one of the ctrl cmd names must be match the ctrl
2629
             * cmd name in the template.
2630
             */
2631
0
            if (item->ctrl_str != NULL
2632
0
                && OPENSSL_strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
2633
0
                ctrl_str = tmpl->ctrl_str;
2634
0
            else if (item->ctrl_hexstr != NULL
2635
0
                && OPENSSL_strcasecmp(tmpl->ctrl_hexstr,
2636
0
                       item->ctrl_hexstr)
2637
0
                    == 0)
2638
0
                ctrl_hexstr = tmpl->ctrl_hexstr;
2639
0
            else
2640
0
                continue;
2641
2642
            /* Modify the template to signal which string matched */
2643
0
            tmpl->ctrl_str = ctrl_str;
2644
0
            tmpl->ctrl_hexstr = ctrl_hexstr;
2645
0
        } else if (tmpl->param_key != NULL) {
2646
            /*
2647
             * Search criteria that originates from an OSSL_PARAM setter or
2648
             * getter.
2649
             *
2650
             * Ctrls were fundamentally bidirectional, with only the ctrl
2651
             * command macro name implying direction (if you're lucky).
2652
             * A few ctrl commands were even taking advantage of the
2653
             * bidirectional nature, making the direction depend in the
2654
             * value of the numeric argument.
2655
             *
2656
             * OSSL_PARAM functions are fundamentally different, in that
2657
             * setters and getters are separated, so the data direction is
2658
             * implied by the function that's used.  The same OSSL_PARAM
2659
             * key name can therefore be used in both directions.  We must
2660
             * therefore take the action type into account in this case.
2661
             */
2662
0
            if ((item->action_type != OSSL_ACTION_NONE
2663
0
                    && tmpl->action_type != item->action_type)
2664
0
                || (item->param_key != NULL
2665
0
                    && OPENSSL_strcasecmp(tmpl->param_key,
2666
0
                           item->param_key)
2667
0
                        != 0))
2668
0
                continue;
2669
0
        } else {
2670
0
            return NULL;
2671
0
        }
2672
2673
0
        return item;
2674
0
    }
2675
2676
0
    return NULL;
2677
0
}
2678
2679
static const struct translation_st *
2680
lookup_evp_pkey_ctx_translation(struct translation_st *tmpl)
2681
0
{
2682
0
    return lookup_translation(tmpl, evp_pkey_ctx_translations,
2683
0
        OSSL_NELEM(evp_pkey_ctx_translations));
2684
0
}
2685
2686
static const struct translation_st *
2687
lookup_evp_pkey_translation(struct translation_st *tmpl)
2688
0
{
2689
0
    return lookup_translation(tmpl, evp_pkey_translations,
2690
0
        OSSL_NELEM(evp_pkey_translations));
2691
0
}
2692
2693
/* This must ONLY be called for provider side operations */
2694
int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *pctx,
2695
    int keytype, int optype,
2696
    int cmd, int p1, void *p2)
2697
0
{
2698
0
    struct translation_ctx_st ctx = {
2699
0
        0,
2700
0
    };
2701
0
    struct translation_st tmpl = {
2702
0
        0,
2703
0
    };
2704
0
    const struct translation_st *translation = NULL;
2705
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2706
0
    int ret;
2707
0
    fixup_args_fn *fixup = default_fixup_args;
2708
2709
0
    if (keytype == -1)
2710
0
        keytype = pctx->legacy_keytype;
2711
0
    tmpl.ctrl_num = cmd;
2712
0
    tmpl.keytype1 = tmpl.keytype2 = keytype;
2713
0
    tmpl.optype = optype;
2714
0
    translation = lookup_evp_pkey_ctx_translation(&tmpl);
2715
2716
0
    if (translation == NULL) {
2717
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
2718
0
        return -2;
2719
0
    }
2720
2721
0
    if (pctx->pmeth != NULL
2722
0
        && pctx->pmeth->pkey_id != translation->keytype1
2723
0
        && pctx->pmeth->pkey_id != translation->keytype2)
2724
0
        return -1;
2725
2726
0
    if (translation->fixup_args != NULL)
2727
0
        fixup = translation->fixup_args;
2728
0
    ctx.action_type = translation->action_type;
2729
0
    ctx.ctrl_cmd = cmd;
2730
0
    ctx.p1 = p1;
2731
0
    ctx.p2 = p2;
2732
0
    ctx.pctx = pctx;
2733
0
    ctx.params = params;
2734
2735
0
    ret = fixup(PRE_CTRL_TO_PARAMS, translation, &ctx);
2736
2737
0
    if (ret > 0) {
2738
0
        switch (ctx.action_type) {
2739
0
        default:
2740
            /* fixup_args is expected to make sure this is dead code */
2741
0
            break;
2742
0
        case OSSL_ACTION_GET:
2743
0
            ret = evp_pkey_ctx_get_params_strict(pctx, ctx.params);
2744
0
            break;
2745
0
        case OSSL_ACTION_SET:
2746
0
            ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2747
0
            break;
2748
0
        }
2749
0
    }
2750
2751
    /*
2752
     * In POST, we pass the return value as p1, allowing the fixup_args
2753
     * function to affect it by changing its value.
2754
     */
2755
0
    if (ret > 0) {
2756
0
        ctx.p1 = ret;
2757
0
        fixup(POST_CTRL_TO_PARAMS, translation, &ctx);
2758
0
        ret = ctx.p1;
2759
0
    }
2760
2761
0
    cleanup_translation_ctx(POST_CTRL_TO_PARAMS, translation, &ctx);
2762
2763
0
    return ret;
2764
0
}
2765
2766
/* This must ONLY be called for provider side operations */
2767
int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *pctx,
2768
    const char *name, const char *value)
2769
0
{
2770
0
    struct translation_ctx_st ctx = {
2771
0
        0,
2772
0
    };
2773
0
    struct translation_st tmpl = {
2774
0
        0,
2775
0
    };
2776
0
    const struct translation_st *translation = NULL;
2777
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2778
0
    int keytype = pctx->legacy_keytype;
2779
0
    int optype = pctx->operation == 0 ? -1 : pctx->operation;
2780
0
    int ret;
2781
0
    fixup_args_fn *fixup = default_fixup_args;
2782
2783
0
    tmpl.action_type = OSSL_ACTION_SET;
2784
0
    tmpl.keytype1 = tmpl.keytype2 = keytype;
2785
0
    tmpl.optype = optype;
2786
0
    tmpl.ctrl_str = name;
2787
0
    tmpl.ctrl_hexstr = name;
2788
0
    translation = lookup_evp_pkey_ctx_translation(&tmpl);
2789
2790
0
    if (translation != NULL) {
2791
0
        if (translation->fixup_args != NULL)
2792
0
            fixup = translation->fixup_args;
2793
0
        ctx.action_type = translation->action_type;
2794
0
        ctx.ishex = (tmpl.ctrl_hexstr != NULL);
2795
0
    } else {
2796
        /* String controls really only support setting */
2797
0
        ctx.action_type = OSSL_ACTION_SET;
2798
0
    }
2799
0
    ctx.ctrl_str = name;
2800
0
    ctx.p1 = (int)strlen(value);
2801
0
    ctx.p2 = (char *)value;
2802
0
    ctx.pctx = pctx;
2803
0
    ctx.params = params;
2804
2805
0
    ret = fixup(PRE_CTRL_STR_TO_PARAMS, translation, &ctx);
2806
2807
0
    if (ret > 0) {
2808
0
        switch (ctx.action_type) {
2809
0
        default:
2810
            /* fixup_args is expected to make sure this is dead code */
2811
0
            break;
2812
0
        case OSSL_ACTION_GET:
2813
            /*
2814
             * this is dead code, but must be present, or some compilers
2815
             * will complain
2816
             */
2817
0
            break;
2818
0
        case OSSL_ACTION_SET:
2819
0
            ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2820
0
            break;
2821
0
        }
2822
0
    }
2823
2824
0
    if (ret > 0)
2825
0
        ret = fixup(POST_CTRL_STR_TO_PARAMS, translation, &ctx);
2826
2827
0
    cleanup_translation_ctx(CLEANUP_CTRL_STR_TO_PARAMS, translation, &ctx);
2828
2829
0
    return ret;
2830
0
}
2831
2832
/* This must ONLY be called for legacy operations */
2833
static int evp_pkey_ctx_setget_params_to_ctrl(EVP_PKEY_CTX *pctx,
2834
    enum action action_type,
2835
    OSSL_PARAM *params)
2836
0
{
2837
0
    int keytype = pctx->legacy_keytype;
2838
0
    int optype = pctx->operation == 0 ? -1 : pctx->operation;
2839
2840
0
    for (; params != NULL && params->key != NULL; params++) {
2841
0
        struct translation_ctx_st ctx = {
2842
0
            0,
2843
0
        };
2844
0
        struct translation_st tmpl = {
2845
0
            0,
2846
0
        };
2847
0
        const struct translation_st *translation = NULL;
2848
0
        fixup_args_fn *fixup = default_fixup_args;
2849
0
        int ret;
2850
2851
0
        ctx.action_type = tmpl.action_type = action_type;
2852
0
        tmpl.keytype1 = tmpl.keytype2 = keytype;
2853
0
        tmpl.optype = optype;
2854
0
        tmpl.param_key = params->key;
2855
0
        translation = lookup_evp_pkey_ctx_translation(&tmpl);
2856
2857
0
        if (translation != NULL) {
2858
0
            if (translation->fixup_args != NULL)
2859
0
                fixup = translation->fixup_args;
2860
0
            ctx.ctrl_cmd = translation->ctrl_num;
2861
0
        }
2862
0
        ctx.pctx = pctx;
2863
0
        ctx.params = params;
2864
2865
0
        ret = fixup(PRE_PARAMS_TO_CTRL, translation, &ctx);
2866
2867
0
        if (ret > 0 && ctx.action_type != OSSL_ACTION_NONE)
2868
0
            ret = EVP_PKEY_CTX_ctrl(pctx, keytype, optype,
2869
0
                ctx.ctrl_cmd, ctx.p1, ctx.p2);
2870
2871
        /*
2872
         * In POST, we pass the return value as p1, allowing the fixup_args
2873
         * function to put it to good use, or maybe affect it.
2874
         *
2875
         * NOTE: even though EVP_PKEY_CTX_ctrl return value is documented
2876
         * as return positive on Success and 0 or negative on failure. There
2877
         * maybe parameters (e.g. ecdh_cofactor), which actually return 0
2878
         * as success value. That is why we do POST_PARAMS_TO_CTRL for 0
2879
         * value as well
2880
         */
2881
0
        if (ret >= 0) {
2882
0
            ctx.p1 = ret;
2883
0
            fixup(POST_PARAMS_TO_CTRL, translation, &ctx);
2884
0
            ret = ctx.p1;
2885
0
        }
2886
2887
0
        cleanup_translation_ctx(CLEANUP_PARAMS_TO_CTRL, translation, &ctx);
2888
2889
0
        if (ret <= 0)
2890
0
            return 0;
2891
0
    }
2892
0
    return 1;
2893
0
}
2894
2895
int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
2896
0
{
2897
0
    if (ctx->keymgmt != NULL)
2898
0
        return 0;
2899
0
    return evp_pkey_ctx_setget_params_to_ctrl(ctx, OSSL_ACTION_SET, (OSSL_PARAM *)params);
2900
0
}
2901
2902
int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
2903
0
{
2904
0
    if (ctx->keymgmt != NULL)
2905
0
        return 0;
2906
0
    return evp_pkey_ctx_setget_params_to_ctrl(ctx, OSSL_ACTION_GET, params);
2907
0
}
2908
2909
/* This must ONLY be called for legacy EVP_PKEYs */
2910
static int evp_pkey_setget_params_to_ctrl(const EVP_PKEY *pkey,
2911
    enum action action_type,
2912
    OSSL_PARAM *params)
2913
0
{
2914
0
    int ret = 1;
2915
2916
0
    for (; params != NULL && params->key != NULL; params++) {
2917
0
        struct translation_ctx_st ctx = {
2918
0
            0,
2919
0
        };
2920
0
        struct translation_st tmpl = {
2921
0
            0,
2922
0
        };
2923
0
        const struct translation_st *translation = NULL;
2924
0
        fixup_args_fn *fixup = default_fixup_args;
2925
2926
0
        tmpl.action_type = action_type;
2927
0
        tmpl.param_key = params->key;
2928
0
        translation = lookup_evp_pkey_translation(&tmpl);
2929
2930
0
        if (translation != NULL) {
2931
0
            if (translation->fixup_args != NULL)
2932
0
                fixup = translation->fixup_args;
2933
0
            ctx.action_type = translation->action_type;
2934
0
        }
2935
0
        ctx.p2 = (void *)pkey;
2936
0
        ctx.params = params;
2937
2938
        /*
2939
         * EVP_PKEY doesn't have any ctrl function, so we rely completely
2940
         * on fixup_args to do the whole work.  Also, we currently only
2941
         * support getting.
2942
         */
2943
0
        if (!ossl_assert(translation != NULL)
2944
0
            || !ossl_assert(translation->action_type == OSSL_ACTION_GET)
2945
0
            || !ossl_assert(translation->fixup_args != NULL)) {
2946
0
            return -2;
2947
0
        }
2948
2949
0
        ret = fixup(PKEY, translation, &ctx);
2950
2951
0
        cleanup_translation_ctx(PKEY, translation, &ctx);
2952
0
    }
2953
0
    return ret;
2954
0
}
2955
2956
int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params)
2957
0
{
2958
0
    return evp_pkey_setget_params_to_ctrl(pkey, OSSL_ACTION_GET, params);
2959
0
}