Coverage Report

Created: 2026-08-31 06:56

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