Coverage Report

Created: 2025-07-11 06:57

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