2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
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
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.
16 #include "internal/deprecated.h"
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>
27 /* This include gets us all the OSSL_PARAM key string macros */
28 #include <openssl/core_names.h>
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"
40 struct translation_ctx_st; /* Forwarding */
41 struct translation_st; /* Forwarding */
44 * The fixup_args functions are called with the following parameters:
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
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().
56 * The |state| tells the fixup_args function something about the caller and
57 * what they may expect:
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
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
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, ...)
78 * With the PRE_CTRL_TO_PARAMS state, the
79 * fixup_args function is expected to modify
80 * the passed |*params| in whatever way
81 * necessary, when |action_type == SET|.
82 * With the POST_CTRL_TO_PARAMS state, the
83 * fixup_args function is expected to modify
84 * the passed |p2| in whatever way necessary,
85 * when |action_type == GET|.
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().
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.
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:
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, ...)
107 * With the PRE_CTRL_STR_TO_PARAMS state,
108 * the fixup_args function is expected to
109 * modify the passed |*params| in whatever
110 * way necessary, when |action_type == SET|.
111 * With the POST_CTRL_STR_TO_PARAMS state,
112 * the fixup_args function is only expected
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
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:
128 * 1. fixup_args(PRE_PARAMS_TO_CTRL, ...)
129 * 2. EVP_PKEY_CTX_ctrl()
130 * 3. fixup_args(POST_PARAMS_TO_CTRL, ...)
132 * With the PRE_PARAMS_TO_CTRL state, the
133 * fixup_args function is expected to modify
134 * the passed |p1| and |p2| in whatever way
135 * necessary, when |action_type == SET|.
136 * With the POST_PARAMS_TO_CTRL state, the
137 * fixup_args function is expected to
138 * modify the passed |*params| in whatever
139 * way necessary, when |action_type == GET|.
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.
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
153 NONE = 0, GET = 1, SET = 2
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);
162 struct translation_ctx_st {
164 * The EVP_PKEY_CTX, for calls on that structure, to be pilfered for data
169 * The action type (GET or SET). This may be 0 in some cases, and should
170 * be modified by the fixup_args function in the PRE states. It should
171 * otherwise remain untouched once set.
173 enum action action_type;
175 * For ctrl to params translation, the actual ctrl command number used.
176 * For params to ctrl translation, 0.
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
186 const char *ctrl_str;
188 /* the ctrl-style int argument. */
190 /* the ctrl-style void* argument. */
192 /* a size, for passing back the |p2| size where applicable */
194 /* pointer to the OSSL_PARAM-style params array. */
198 * The following are used entirely internally by the fixup_args functions
199 * and should not be touched by the callers, at all.
203 * Copy of the ctrl-style void* argument, if the fixup_args function
204 * needs to manipulate |p2| but wants to remember original.
207 /* Diverse types of storage for the needy. */
208 char name_buf[OSSL_MAX_NAME_SIZE];
214 struct translation_st {
216 * What this table item does.
218 * If the item has this set to 0, it means that both GET and SET are
219 * supported, and |fixup_args| will determine which it is. This is to
220 * support translations of ctrls where the action type depends on the
221 * value of |p1| or |p2| (ctrls are really bi-directional, but are
222 * seldom used that way).
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.
227 enum action action_type;
230 * Conditions, for params->ctrl translations.
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.
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 */
242 * Lookup and translation attributes
244 * |ctrl_num|, |ctrl_str|, |ctrl_hexstr| and |param_key| are lookup
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
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.
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.
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 */
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
269 unsigned int param_data_type;
274 * |fixup_args| is always called before (for SET) or after (for GET)
275 * the actual ctrl / OSSL_PARAM function.
277 fixup_args_fn *fixup_args;
281 * Fixer function implementations
282 * ==============================
286 * default_check isn't a fixer per se, but rather a helper function to
287 * perform certain standard checks.
289 static int default_check(enum state state,
290 const struct translation_st *translation,
291 const struct translation_ctx_st *ctx)
296 case PRE_CTRL_TO_PARAMS:
297 if (!ossl_assert(translation != NULL)) {
298 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
301 if (!ossl_assert(translation->param_key != 0)
302 || !ossl_assert(translation->param_data_type != 0)) {
303 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
307 case PRE_CTRL_STR_TO_PARAMS:
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.
314 if (translation != NULL) {
315 if (!ossl_assert(translation->action_type != GET)) {
316 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
319 if (!ossl_assert(translation->param_key != NULL)
320 || !ossl_assert(translation->param_data_type != 0)) {
321 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
326 case PRE_PARAMS_TO_CTRL:
327 case POST_PARAMS_TO_CTRL:
328 if (!ossl_assert(translation != NULL)) {
329 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
332 if (!ossl_assert(translation->ctrl_num != 0)
333 || !ossl_assert(translation->param_data_type != 0)) {
334 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
339 /* Nothing else to check */
344 * default_fixup_args fixes up all sorts of arguments, governed by the
345 * diverse attributes in the translation item. It covers all "standard"
346 * base ctrl functionality, meaning it can handle basic conversion of
347 * data between p1+p2 (SET) or return value+p2 (GET) as long as the values
348 * don't have extra semantics (such as NIDs, OIDs, that sort of stuff).
349 * Extra semantics must be handled via specific fixup_args functions.
351 * The following states and action type combinations have standard handling
352 * done in this function:
354 * PRE_CTRL_TO_PARAMS, 0 - ERROR. action type must be
355 * determined by a fixup function.
356 * PRE_CTRL_TO_PARAMS, SET | GET - |p1| and |p2| are converted to an
357 * OSSL_PARAM according to the data
358 * type given in |translattion|.
359 * For OSSL_PARAM_UNSIGNED_INTEGER,
360 * a BIGNUM passed as |p2| is accepted.
361 * POST_CTRL_TO_PARAMS, GET - If the OSSL_PARAM data type is a
362 * STRING or PTR type, |p1| is set
363 * to the OSSL_PARAM return size, and
364 * |p2| is set to the string.
365 * PRE_CTRL_STR_TO_PARAMS, !SET - ERROR. That combination is not
367 * PRE_CTRL_STR_TO_PARAMS, SET - |p2| is taken as a string, and is
368 * converted to an OSSL_PARAM in a
369 * standard manner, guided by the
370 * param key and data type from
372 * PRE_PARAMS_TO_CTRL, SET - the OSSL_PARAM is converted to
373 * |p1| and |p2| according to the
374 * data type given in |translation|
375 * For OSSL_PARAM_UNSIGNED_INTEGER,
376 * if |p2| is non-NULL, then |*p2|
377 * is assigned a BIGNUM, otherwise
378 * |p1| is assigned an unsigned int.
379 * POST_PARAMS_TO_CTRL, GET - |p1| and |p2| are converted to
380 * an OSSL_PARAM, in the same manner
381 * as for the combination of
382 * PRE_CTRL_TO_PARAMS, SET.
384 static int default_fixup_args(enum state state,
385 const struct translation_st *translation,
386 struct translation_ctx_st *ctx)
390 if ((ret = default_check(state, translation, ctx)) < 0)
395 /* For states this function should never have been called with */
396 ERR_raise_data(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
397 "[action:%d, state:%d]", ctx->action_type, state);
401 * PRE_CTRL_TO_PARAMS and POST_CTRL_TO_PARAMS handle ctrl to params
402 * translations. PRE_CTRL_TO_PARAMS is responsible for preparing
403 * |*params|, and POST_CTRL_TO_PARAMS is responsible for bringing the
404 * result back to |*p2| and the return value.
406 case PRE_CTRL_TO_PARAMS:
407 /* This is ctrl to params translation, so we need an OSSL_PARAM key */
408 if (ctx->action_type == NONE) {
410 * No action type is an error here. That's a case for a
411 * special fixup function.
413 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
414 "[action:%d, state:%d]", ctx->action_type, state);
418 if (translation->optype != 0) {
419 if ((EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
420 && ctx->pctx->op.sig.algctx == NULL)
421 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx->pctx)
422 && ctx->pctx->op.kex.algctx == NULL)
423 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx->pctx)
424 && ctx->pctx->op.ciph.algctx == NULL)
425 || (EVP_PKEY_CTX_IS_KEM_OP(ctx->pctx)
426 && ctx->pctx->op.encap.algctx == NULL)
428 * The following may be unnecessary, but we have them
429 * for good measure...
431 || (EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx)
432 && ctx->pctx->op.keymgmt.genctx == NULL)
433 || (EVP_PKEY_CTX_IS_FROMDATA_OP(ctx->pctx)
434 && ctx->pctx->op.keymgmt.genctx == NULL)) {
435 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
436 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
442 * OSSL_PARAM_construct_TYPE() works equally well for both SET and GET.
444 switch (translation->param_data_type) {
445 case OSSL_PARAM_INTEGER:
446 *ctx->params = OSSL_PARAM_construct_int(translation->param_key,
449 case OSSL_PARAM_UNSIGNED_INTEGER:
451 * BIGNUMs are passed via |p2|. For all ctrl's that just want
452 * to pass a simple integer via |p1|, |p2| is expected to be
455 * Note that this allocates a buffer, which the cleanup function
458 if (ctx->p2 != NULL) {
459 if (ctx->action_type == SET) {
460 ctx->buflen = BN_num_bytes(ctx->p2);
461 if ((ctx->allocated_buf =
462 OPENSSL_malloc(ctx->buflen)) == NULL) {
463 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
466 if (BN_bn2nativepad(ctx->p2,
467 ctx->allocated_buf, ctx->buflen) < 0) {
468 OPENSSL_free(ctx->allocated_buf);
469 ctx->allocated_buf = NULL;
473 OSSL_PARAM_construct_BN(translation->param_key,
478 * No support for getting a BIGNUM by ctrl, this needs
479 * fixup_args function support.
481 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
482 "[action:%d, state:%d] trying to get a "
483 "BIGNUM via ctrl call",
484 ctx->action_type, state);
489 OSSL_PARAM_construct_uint(translation->param_key,
490 (unsigned int *)&ctx->p1);
493 case OSSL_PARAM_UTF8_STRING:
495 OSSL_PARAM_construct_utf8_string(translation->param_key,
496 ctx->p2, (size_t)ctx->p1);
498 case OSSL_PARAM_UTF8_PTR:
500 OSSL_PARAM_construct_utf8_ptr(translation->param_key,
501 ctx->p2, (size_t)ctx->p1);
503 case OSSL_PARAM_OCTET_STRING:
505 OSSL_PARAM_construct_octet_string(translation->param_key,
506 ctx->p2, (size_t)ctx->p1);
508 case OSSL_PARAM_OCTET_PTR:
510 OSSL_PARAM_construct_octet_ptr(translation->param_key,
511 ctx->p2, (size_t)ctx->p1);
515 case POST_CTRL_TO_PARAMS:
517 * Because EVP_PKEY_CTX_ctrl() returns the length of certain objects
518 * as its return value, we need to ensure that we do it here as well,
519 * for the OSSL_PARAM data types where this makes sense.
521 if (ctx->action_type == GET) {
522 switch (translation->param_data_type) {
523 case OSSL_PARAM_UTF8_STRING:
524 case OSSL_PARAM_UTF8_PTR:
525 case OSSL_PARAM_OCTET_STRING:
526 case OSSL_PARAM_OCTET_PTR:
527 ctx->p1 = (int)ctx->params[0].return_size;
534 * PRE_CTRL_STR_TO_PARAMS and POST_CTRL_STR_TO_PARAMS handle ctrl_str to
535 * params translations. PRE_CTRL_TO_PARAMS is responsible for preparing
536 * |*params|, and POST_CTRL_TO_PARAMS currently has nothing to do, since
537 * there's no support for getting data via ctrl_str calls.
539 case PRE_CTRL_STR_TO_PARAMS:
541 /* This is ctrl_str to params translation */
542 const char *tmp_ctrl_str = ctx->ctrl_str;
543 const char *orig_ctrl_str = ctx->ctrl_str;
544 const char *orig_value = ctx->p2;
545 const OSSL_PARAM *settable = NULL;
548 /* Only setting is supported here */
549 if (ctx->action_type != SET) {
550 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
551 "[action:%d, state:%d] only setting allowed",
552 ctx->action_type, state);
557 * If no translation exists, we simply pass the control string
560 if (translation != NULL) {
561 tmp_ctrl_str = ctx->ctrl_str = translation->param_key;
564 strcpy(ctx->name_buf, "hex");
565 if (OPENSSL_strlcat(ctx->name_buf, tmp_ctrl_str,
566 sizeof(ctx->name_buf)) <= 3) {
567 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
570 tmp_ctrl_str = ctx->name_buf;
574 settable = EVP_PKEY_CTX_settable_params(ctx->pctx);
575 if (!OSSL_PARAM_allocate_from_text(ctx->params, settable,
577 ctx->p2, strlen(ctx->p2),
580 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
581 "[action:%d, state:%d] name=%s, value=%s",
582 ctx->action_type, state,
583 orig_ctrl_str, orig_value);
588 ctx->allocated_buf = ctx->params->data;
589 ctx->buflen = ctx->params->data_size;
592 case POST_CTRL_STR_TO_PARAMS:
593 /* Nothing to be done */
597 * PRE_PARAMS_TO_CTRL and POST_PARAMS_TO_CTRL handle params to ctrl
598 * translations. PRE_PARAMS_TO_CTRL is responsible for preparing
599 * |p1| and |p2|, and POST_PARAMS_TO_CTRL is responsible for bringing
600 * the EVP_PKEY_CTX_ctrl() return value (passed as |p1|) and |p2| back
603 * PKEY is treated just like POST_PARAMS_TO_CTRL, making it easy
604 * for the related fixup_args functions to just set |p1| and |p2|
605 * appropriately and leave it to this section of code to fix up
606 * |ctx->params| accordingly.
609 case POST_PARAMS_TO_CTRL:
612 case PRE_PARAMS_TO_CTRL:
614 /* This is params to ctrl translation */
615 if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
616 /* For the PRE state, only setting needs some work to be done */
618 /* When setting, we populate |p1| and |p2| from |*params| */
619 switch (translation->param_data_type) {
620 case OSSL_PARAM_INTEGER:
621 return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
622 case OSSL_PARAM_UNSIGNED_INTEGER:
623 if (ctx->p2 != NULL) {
624 /* BIGNUM passed down with p2 */
625 if (!OSSL_PARAM_get_BN(ctx->params, ctx->p2))
628 /* Normal C unsigned int passed down */
629 if (!OSSL_PARAM_get_uint(ctx->params,
630 (unsigned int *)&ctx->p1))
634 case OSSL_PARAM_UTF8_STRING:
635 return OSSL_PARAM_get_utf8_string(ctx->params,
637 case OSSL_PARAM_OCTET_STRING:
638 return OSSL_PARAM_get_octet_string(ctx->params,
641 case OSSL_PARAM_OCTET_PTR:
642 return OSSL_PARAM_get_octet_ptr(ctx->params,
645 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
646 "[action:%d, state:%d] "
647 "unknown OSSL_PARAM data type %d",
648 ctx->action_type, state,
649 translation->param_data_type);
652 } else if ((state == POST_PARAMS_TO_CTRL || state == PKEY)
653 && ctx->action_type == GET) {
654 /* For the POST state, only getting needs some work to be done */
655 unsigned int param_data_type = translation->param_data_type;
656 size_t size = (size_t)ctx->p1;
660 if (param_data_type == 0) {
661 /* we must have a fixup_args function to work */
662 if (!ossl_assert(translation->fixup_args != NULL)) {
663 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
666 param_data_type = ctx->params->data_type;
668 /* When getting, we populate |*params| from |p1| and |p2| */
669 switch (param_data_type) {
670 case OSSL_PARAM_INTEGER:
671 return OSSL_PARAM_set_int(ctx->params, ctx->p1);
672 case OSSL_PARAM_UNSIGNED_INTEGER:
673 if (ctx->p2 != NULL) {
674 /* BIGNUM passed back */
675 return OSSL_PARAM_set_BN(ctx->params, ctx->p2);
677 /* Normal C unsigned int passed back */
678 return OSSL_PARAM_set_uint(ctx->params,
679 (unsigned int)ctx->p1);
682 case OSSL_PARAM_UTF8_STRING:
683 return OSSL_PARAM_set_utf8_string(ctx->params, ctx->p2);
684 case OSSL_PARAM_OCTET_STRING:
685 return OSSL_PARAM_set_octet_string(ctx->params, ctx->p2,
687 case OSSL_PARAM_OCTET_PTR:
688 return OSSL_PARAM_set_octet_ptr(ctx->params, ctx->p2,
691 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
692 "[action:%d, state:%d] "
693 "unsupported OSSL_PARAM data type %d",
694 ctx->action_type, state,
695 translation->param_data_type);
700 /* Any other combination is simply pass-through */
707 cleanup_translation_ctx(enum state state,
708 const struct translation_st *translation,
709 struct translation_ctx_st *ctx)
711 if (ctx->allocated_buf != NULL)
712 OPENSSL_free(ctx->allocated_buf);
713 ctx->allocated_buf = NULL;
718 * fix_cipher_md fixes up an EVP_CIPHER / EVP_MD to its name on SET,
719 * and cipher / md name to EVP_MD on GET.
721 static const char *get_cipher_name(void *cipher)
723 return EVP_CIPHER_get0_name(cipher);
726 static const char *get_md_name(void *md)
728 return EVP_MD_get0_name(md);
731 static const void *get_cipher_by_name(OSSL_LIB_CTX *libctx, const char *name)
733 return evp_get_cipherbyname_ex(libctx, name);
736 static const void *get_md_by_name(OSSL_LIB_CTX *libctx, const char *name)
738 return evp_get_digestbyname_ex(libctx, name);
741 static int fix_cipher_md(enum state state,
742 const struct translation_st *translation,
743 struct translation_ctx_st *ctx,
744 const char *(*get_name)(void *algo),
745 const void *(*get_algo_by_name)(OSSL_LIB_CTX *libctx,
750 if ((ret = default_check(state, translation, ctx)) <= 0)
753 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
755 * |ctx->p2| contains the address to an EVP_CIPHER or EVP_MD pointer
756 * to be filled in. We need to remember it, then make |ctx->p2|
757 * point at a buffer to be filled in with the name, and |ctx->p1|
758 * with its size. default_fixup_args() will take care of the rest
761 ctx->orig_p2 = ctx->p2;
762 ctx->p2 = ctx->name_buf;
763 ctx->p1 = sizeof(ctx->name_buf);
764 } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
766 * In different parts of OpenSSL, this ctrl command is used
767 * differently. Some calls pass a NID as p1, others pass an
768 * EVP_CIPHER pointer as p2...
770 ctx->p2 = (char *)(ctx->p2 == NULL
771 ? OBJ_nid2sn(ctx->p1)
772 : get_name(ctx->p2));
773 ctx->p1 = strlen(ctx->p2);
774 } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
775 ctx->p2 = (ctx->p2 == NULL ? "" : (char *)get_name(ctx->p2));
776 ctx->p1 = strlen(ctx->p2);
779 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
782 if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
784 * Here's how we re-use |ctx->orig_p2| that was set in the
785 * PRE_CTRL_TO_PARAMS state above.
787 *(void **)ctx->orig_p2 =
788 (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
790 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
791 ctx->p2 = (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
798 static int fix_cipher(enum state state,
799 const struct translation_st *translation,
800 struct translation_ctx_st *ctx)
802 return fix_cipher_md(state, translation, ctx,
803 get_cipher_name, get_cipher_by_name);
806 static int fix_md(enum state state,
807 const struct translation_st *translation,
808 struct translation_ctx_st *ctx)
810 return fix_cipher_md(state, translation, ctx,
811 get_md_name, get_md_by_name);
814 static int fix_distid_len(enum state state,
815 const struct translation_st *translation,
816 struct translation_ctx_st *ctx)
818 int ret = default_fixup_args(state, translation, ctx);
822 if ((state == POST_CTRL_TO_PARAMS
823 || state == POST_CTRL_STR_TO_PARAMS) && ctx->action_type == GET) {
824 *(size_t *)ctx->p2 = ctx->sz;
831 struct kdf_type_map_st {
833 const char *kdf_type_str;
836 static int fix_kdf_type(enum state state,
837 const struct translation_st *translation,
838 struct translation_ctx_st *ctx,
839 const struct kdf_type_map_st *kdf_type_map)
842 * The EVP_PKEY_CTRL_DH_KDF_TYPE ctrl command is a bit special, in
843 * that it's used both for setting a value, and for getting it, all
844 * depending on the value if |p1|; if |p1| is -2, the backend is
845 * supposed to place the current kdf type in |p2|, and if not, |p1|
846 * is interpreted as the new kdf type.
850 if ((ret = default_check(state, translation, ctx)) <= 0)
853 if (state == PRE_CTRL_TO_PARAMS) {
855 * In |translations|, the initial value for |ctx->action_type| must
858 if (!ossl_assert(ctx->action_type == NONE))
861 /* The action type depends on the value of *p1 */
864 * The OSSL_PARAMS getter needs space to store a copy of the kdf
865 * type string. We use |ctx->name_buf|, which has enough space
868 * (this wouldn't be needed if the OSSL_xxx_PARAM_KDF_TYPE
869 * had the data type OSSL_PARAM_UTF8_PTR)
871 ctx->p2 = ctx->name_buf;
872 ctx->p1 = sizeof(ctx->name_buf);
873 ctx->action_type = GET;
875 ctx->action_type = SET;
879 if ((ret = default_check(state, translation, ctx)) <= 0)
882 if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
883 || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
885 /* Convert KDF type numbers to strings */
886 for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
887 if (ctx->p1 == kdf_type_map->kdf_type_num) {
888 ctx->p2 = (char *)kdf_type_map->kdf_type_str;
894 ctx->p1 = strlen(ctx->p2);
897 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
900 if ((state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)
901 || (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)) {
904 /* Convert KDF type strings to numbers */
905 for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
906 if (OPENSSL_strcasecmp(ctx->p2, kdf_type_map->kdf_type_str) == 0) {
907 ctx->p1 = kdf_type_map->kdf_type_num;
912 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
919 /* EVP_PKEY_CTRL_DH_KDF_TYPE */
920 static int fix_dh_kdf_type(enum state state,
921 const struct translation_st *translation,
922 struct translation_ctx_st *ctx)
924 static const struct kdf_type_map_st kdf_type_map[] = {
925 { EVP_PKEY_DH_KDF_NONE, "" },
926 { EVP_PKEY_DH_KDF_X9_42, OSSL_KDF_NAME_X942KDF_ASN1 },
930 return fix_kdf_type(state, translation, ctx, kdf_type_map);
933 /* EVP_PKEY_CTRL_EC_KDF_TYPE */
934 static int fix_ec_kdf_type(enum state state,
935 const struct translation_st *translation,
936 struct translation_ctx_st *ctx)
938 static const struct kdf_type_map_st kdf_type_map[] = {
939 { EVP_PKEY_ECDH_KDF_NONE, "" },
940 { EVP_PKEY_ECDH_KDF_X9_63, OSSL_KDF_NAME_X963KDF },
944 return fix_kdf_type(state, translation, ctx, kdf_type_map);
947 /* EVP_PKEY_CTRL_DH_KDF_OID, EVP_PKEY_CTRL_GET_DH_KDF_OID, ...??? */
948 static int fix_oid(enum state state,
949 const struct translation_st *translation,
950 struct translation_ctx_st *ctx)
954 if ((ret = default_check(state, translation, ctx)) <= 0)
957 if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
958 || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
960 * We're translating from ctrl to params and setting the OID, or
961 * we're translating from params to ctrl and getting the OID.
962 * Either way, |ctx->p2| points at an ASN1_OBJECT, and needs to have
963 * that replaced with the corresponding name.
964 * default_fixup_args() will then be able to convert that to the
965 * corresponding OSSL_PARAM.
967 OBJ_obj2txt(ctx->name_buf, sizeof(ctx->name_buf), ctx->p2, 0);
968 ctx->p2 = (char *)ctx->name_buf;
969 ctx->p1 = 0; /* let default_fixup_args() figure out the length */
972 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
975 if ((state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)
976 || (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)) {
978 * We're translating from ctrl to params and setting the OID name,
979 * or we're translating from params to ctrl and getting the OID
980 * name. Either way, default_fixup_args() has placed the OID name
981 * in |ctx->p2|, all we need to do now is to replace that with the
982 * corresponding ASN1_OBJECT.
984 ctx->p2 = (ASN1_OBJECT *)OBJ_txt2obj(ctx->p2, 0);
990 /* EVP_PKEY_CTRL_DH_NID */
991 static int fix_dh_nid(enum state state,
992 const struct translation_st *translation,
993 struct translation_ctx_st *ctx)
997 if ((ret = default_check(state, translation, ctx)) <= 0)
1000 /* This is only settable */
1001 if (ctx->action_type != SET)
1004 if (state == PRE_CTRL_TO_PARAMS) {
1005 if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
1006 (ossl_ffc_uid_to_dh_named_group(ctx->p1))) == NULL) {
1007 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1013 return default_fixup_args(state, translation, ctx);
1016 /* EVP_PKEY_CTRL_DH_RFC5114 */
1017 static int fix_dh_nid5114(enum state state,
1018 const struct translation_st *translation,
1019 struct translation_ctx_st *ctx)
1023 if ((ret = default_check(state, translation, ctx)) <= 0)
1026 /* This is only settable */
1027 if (ctx->action_type != SET)
1031 case PRE_CTRL_TO_PARAMS:
1032 if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
1033 (ossl_ffc_uid_to_dh_named_group(ctx->p1))) == NULL) {
1034 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1041 case PRE_CTRL_STR_TO_PARAMS:
1042 if (ctx->p2 == NULL)
1044 if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
1045 (ossl_ffc_uid_to_dh_named_group(atoi(ctx->p2)))) == NULL) {
1046 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1057 return default_fixup_args(state, translation, ctx);
1060 /* EVP_PKEY_CTRL_DH_PARAMGEN_TYPE */
1061 static int fix_dh_paramgen_type(enum state state,
1062 const struct translation_st *translation,
1063 struct translation_ctx_st *ctx)
1067 if ((ret = default_check(state, translation, ctx)) <= 0)
1070 /* This is only settable */
1071 if (ctx->action_type != SET)
1074 if (state == PRE_CTRL_STR_TO_PARAMS) {
1075 ctx->p2 = (char *)ossl_dh_gen_type_id2name(atoi(ctx->p2));
1076 ctx->p1 = strlen(ctx->p2);
1079 return default_fixup_args(state, translation, ctx);
1082 /* EVP_PKEY_CTRL_EC_PARAM_ENC */
1083 static int fix_ec_param_enc(enum state state,
1084 const struct translation_st *translation,
1085 struct translation_ctx_st *ctx)
1089 if ((ret = default_check(state, translation, ctx)) <= 0)
1092 /* This is currently only settable */
1093 if (ctx->action_type != SET)
1096 if (state == PRE_CTRL_TO_PARAMS) {
1098 case OPENSSL_EC_EXPLICIT_CURVE:
1099 ctx->p2 = OSSL_PKEY_EC_ENCODING_EXPLICIT;
1101 case OPENSSL_EC_NAMED_CURVE:
1102 ctx->p2 = OSSL_PKEY_EC_ENCODING_GROUP;
1111 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1114 if (state == PRE_PARAMS_TO_CTRL) {
1115 if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_EXPLICIT) == 0)
1116 ctx->p1 = OPENSSL_EC_EXPLICIT_CURVE;
1117 else if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_GROUP) == 0)
1118 ctx->p1 = OPENSSL_EC_NAMED_CURVE;
1126 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1130 /* EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID */
1131 static int fix_ec_paramgen_curve_nid(enum state state,
1132 const struct translation_st *translation,
1133 struct translation_ctx_st *ctx)
1137 if ((ret = default_check(state, translation, ctx)) <= 0)
1140 /* This is currently only settable */
1141 if (ctx->action_type != SET)
1144 if (state == PRE_CTRL_TO_PARAMS) {
1145 ctx->p2 = (char *)OBJ_nid2sn(ctx->p1);
1149 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1152 if (state == PRE_PARAMS_TO_CTRL) {
1153 ctx->p1 = OBJ_sn2nid(ctx->p2);
1160 /* EVP_PKEY_CTRL_EC_ECDH_COFACTOR */
1161 static int fix_ecdh_cofactor(enum state state,
1162 const struct translation_st *translation,
1163 struct translation_ctx_st *ctx)
1166 * The EVP_PKEY_CTRL_EC_ECDH_COFACTOR ctrl command is a bit special, in
1167 * that it's used both for setting a value, and for getting it, all
1168 * depending on the value if |ctx->p1|; if |ctx->p1| is -2, the backend is
1169 * supposed to place the current cofactor mode in |ctx->p2|, and if not,
1170 * |ctx->p1| is interpreted as the new cofactor mode.
1174 if (state == PRE_CTRL_TO_PARAMS) {
1176 * The initial value for |ctx->action_type| must be zero.
1177 * evp_pkey_ctrl_to_params() takes it from the translation item.
1179 if (!ossl_assert(ctx->action_type == NONE))
1182 /* The action type depends on the value of ctx->p1 */
1184 ctx->action_type = GET;
1186 ctx->action_type = SET;
1187 } else if (state == PRE_CTRL_STR_TO_PARAMS) {
1188 ctx->action_type = SET;
1189 } else if (state == PRE_PARAMS_TO_CTRL) {
1190 /* The initial value for |ctx->action_type| must not be zero. */
1191 if (!ossl_assert(ctx->action_type != NONE))
1195 if ((ret = default_check(state, translation, ctx)) <= 0)
1198 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1199 if (ctx->p1 < -1 || ctx->p1 > 1) {
1200 /* Uses the same return value of pkey_ec_ctrl() */
1205 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1208 if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
1209 if (ctx->p1 < 0 || ctx->p1 > 1) {
1211 * The provider should return either 0 or 1, any other value is a
1216 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
1223 /* EVP_PKEY_CTRL_RSA_PADDING, EVP_PKEY_CTRL_GET_RSA_PADDING */
1224 static int fix_rsa_padding_mode(enum state state,
1225 const struct translation_st *translation,
1226 struct translation_ctx_st *ctx)
1228 static const OSSL_ITEM str_value_map[] = {
1229 { RSA_PKCS1_PADDING, "pkcs1" },
1230 { RSA_NO_PADDING, "none" },
1231 { RSA_PKCS1_OAEP_PADDING, "oaep" },
1232 { RSA_PKCS1_OAEP_PADDING, "oeap" },
1233 { RSA_X931_PADDING, "x931" },
1234 { RSA_PKCS1_PSS_PADDING, "pss" },
1235 /* Special case, will pass directly as an integer */
1236 { RSA_PKCS1_WITH_TLS_PADDING, NULL }
1240 if ((ret = default_check(state, translation, ctx)) <= 0)
1243 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1245 * EVP_PKEY_CTRL_GET_RSA_PADDING returns the padding mode in the
1246 * weirdest way for a ctrl. Instead of doing like all other ctrls
1247 * that return a simple, i.e. just have that as a return value,
1248 * this particular ctrl treats p2 as the address for the int to be
1249 * returned. We must therefore remember |ctx->p2|, then make
1250 * |ctx->p2| point at a buffer to be filled in with the name, and
1251 * |ctx->p1| with its size. default_fixup_args() will take care
1252 * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1253 * code section further down.
1255 ctx->orig_p2 = ctx->p2;
1256 ctx->p2 = ctx->name_buf;
1257 ctx->p1 = sizeof(ctx->name_buf);
1258 } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1260 * Ideally, we should use utf8 strings for the diverse padding modes.
1261 * We only came here because someone called EVP_PKEY_CTX_ctrl(),
1262 * though, and since that can reasonably be seen as legacy code
1263 * that uses the diverse RSA macros for the padding mode, and we
1264 * know that at least our providers can handle the numeric modes,
1265 * we take the cheap route for now.
1267 * The other solution would be to match |ctx->p1| against entries
1268 * in str_value_map and pass the corresponding string. However,
1269 * since we don't have a string for RSA_PKCS1_WITH_TLS_PADDING,
1270 * we have to do this same hack at least for that one.
1272 * Since the "official" data type for the RSA padding mode is utf8
1273 * string, we cannot count on default_fixup_args(). Instead, we
1274 * build the OSSL_PARAM item ourselves and return immediately.
1276 ctx->params[0] = OSSL_PARAM_construct_int(translation->param_key,
1279 } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
1283 * The EVP_PKEY_CTX_get_params() caller may have asked for a utf8
1284 * string, or may have asked for an integer of some sort. If they
1285 * ask for an integer, we respond directly. If not, we translate
1286 * the response from the ctrl function into a string.
1288 switch (ctx->params->data_type) {
1289 case OSSL_PARAM_INTEGER:
1290 return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
1291 case OSSL_PARAM_UNSIGNED_INTEGER:
1292 return OSSL_PARAM_get_uint(ctx->params, (unsigned int *)&ctx->p1);
1297 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1298 if (ctx->p1 == (int)str_value_map[i].id)
1301 if (i == OSSL_NELEM(str_value_map)) {
1302 ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1303 "[action:%d, state:%d] padding number %d",
1304 ctx->action_type, state, ctx->p1);
1308 * If we don't have a string, we can't do anything. The caller
1309 * should have asked for a number...
1311 if (str_value_map[i].ptr == NULL) {
1312 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1315 ctx->p2 = str_value_map[i].ptr;
1316 ctx->p1 = strlen(ctx->p2);
1319 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1322 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1323 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1326 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1327 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1331 if (i == OSSL_NELEM(str_value_map)) {
1332 ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1333 "[action:%d, state:%d] padding name %s",
1334 ctx->action_type, state, ctx->p1);
1336 } else if (state == POST_CTRL_TO_PARAMS) {
1337 /* EVP_PKEY_CTRL_GET_RSA_PADDING weirdness explained further up */
1338 *(int *)ctx->orig_p2 = str_value_map[i].id;
1340 ctx->p1 = str_value_map[i].id;
1348 /* EVP_PKEY_CTRL_RSA_PSS_SALTLEN, EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN */
1349 static int fix_rsa_pss_saltlen(enum state state,
1350 const struct translation_st *translation,
1351 struct translation_ctx_st *ctx)
1353 static const OSSL_ITEM str_value_map[] = {
1354 { (unsigned int)RSA_PSS_SALTLEN_DIGEST, "digest" },
1355 { (unsigned int)RSA_PSS_SALTLEN_MAX, "max" },
1356 { (unsigned int)RSA_PSS_SALTLEN_AUTO, "auto" }
1360 if ((ret = default_check(state, translation, ctx)) <= 0)
1363 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1365 * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN returns the saltlen by filling
1366 * in the int pointed at by p2. This is potentially as weird as
1367 * the way EVP_PKEY_CTRL_GET_RSA_PADDING works, except that saltlen
1368 * might be a negative value, so it wouldn't work as a legitimate
1370 * In any case, we must therefore remember |ctx->p2|, then make
1371 * |ctx->p2| point at a buffer to be filled in with the name, and
1372 * |ctx->p1| with its size. default_fixup_args() will take care
1373 * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1374 * code section further down.
1376 ctx->orig_p2 = ctx->p2;
1377 ctx->p2 = ctx->name_buf;
1378 ctx->p1 = sizeof(ctx->name_buf);
1379 } else if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1380 || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1383 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1384 if (ctx->p1 == (int)str_value_map[i].id)
1387 if (i == OSSL_NELEM(str_value_map)) {
1388 BIO_snprintf(ctx->name_buf, sizeof(ctx->name_buf), "%d", ctx->p1);
1390 /* This won't truncate but it will quiet static analysers */
1391 strncpy(ctx->name_buf, str_value_map[i].ptr, sizeof(ctx->name_buf) - 1);
1392 ctx->name_buf[sizeof(ctx->name_buf) - 1] = '\0';
1394 ctx->p2 = ctx->name_buf;
1395 ctx->p1 = strlen(ctx->p2);
1398 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1401 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1402 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1406 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1407 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1411 val = i == OSSL_NELEM(str_value_map) ? atoi(ctx->p2)
1412 : (int)str_value_map[i].id;
1413 if (state == POST_CTRL_TO_PARAMS) {
1415 * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN weirdness explained further
1418 *(int *)ctx->orig_p2 = val;
1428 /* EVP_PKEY_CTRL_HKDF_MODE */
1429 static int fix_hkdf_mode(enum state state,
1430 const struct translation_st *translation,
1431 struct translation_ctx_st *ctx)
1433 static const OSSL_ITEM str_value_map[] = {
1434 { EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND, "EXTRACT_AND_EXPAND" },
1435 { EVP_KDF_HKDF_MODE_EXTRACT_ONLY, "EXTRACT_ONLY" },
1436 { EVP_KDF_HKDF_MODE_EXPAND_ONLY, "EXPAND_ONLY" }
1440 if ((ret = default_check(state, translation, ctx)) <= 0)
1443 if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1444 || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1447 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1448 if (ctx->p1 == (int)str_value_map[i].id)
1451 if (i == OSSL_NELEM(str_value_map))
1453 ctx->p2 = str_value_map[i].ptr;
1454 ctx->p1 = strlen(ctx->p2);
1457 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1460 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1461 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1464 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1465 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1468 if (i == OSSL_NELEM(str_value_map))
1470 if (state == POST_CTRL_TO_PARAMS)
1471 ret = str_value_map[i].id;
1473 ctx->p1 = str_value_map[i].id;
1484 * These all get the data they want, then call default_fixup_args() as
1485 * a post-ctrl GET fixup. They all get NULL ctx, ctrl_cmd, ctrl_str,
1489 /* Pilfering DH, DSA and EC_KEY */
1490 static int get_payload_group_name(enum state state,
1491 const struct translation_st *translation,
1492 struct translation_ctx_st *ctx)
1494 EVP_PKEY *pkey = ctx->p2;
1497 switch (EVP_PKEY_get_base_id(pkey)) {
1498 #ifndef OPENSSL_NO_DH
1501 const DH *dh = EVP_PKEY_get0_DH(pkey);
1502 int uid = DH_get_nid(dh);
1504 if (uid != NID_undef) {
1505 const DH_NAMED_GROUP *dh_group =
1506 ossl_ffc_uid_to_dh_named_group(uid);
1508 ctx->p2 = (char *)ossl_ffc_named_group_get_name(dh_group);
1513 #ifndef OPENSSL_NO_EC
1516 const EC_GROUP *grp =
1517 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
1518 int nid = NID_undef;
1521 nid = EC_GROUP_get_curve_name(grp);
1522 if (nid != NID_undef)
1523 ctx->p2 = (char *)OSSL_EC_curve_nid2name(nid);
1528 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1533 * Quietly ignoring unknown groups matches the behaviour on the provider
1536 if (ctx->p2 == NULL)
1539 ctx->p1 = strlen(ctx->p2);
1540 return default_fixup_args(state, translation, ctx);
1543 static int get_payload_private_key(enum state state,
1544 const struct translation_st *translation,
1545 struct translation_ctx_st *ctx)
1547 EVP_PKEY *pkey = ctx->p2;
1550 if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1553 switch (EVP_PKEY_get_base_id(pkey)) {
1554 #ifndef OPENSSL_NO_DH
1557 const DH *dh = EVP_PKEY_get0_DH(pkey);
1559 ctx->p2 = (BIGNUM *)DH_get0_priv_key(dh);
1563 #ifndef OPENSSL_NO_EC
1566 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1568 ctx->p2 = (BIGNUM *)EC_KEY_get0_private_key(ec);
1573 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1577 return default_fixup_args(state, translation, ctx);
1580 static int get_payload_public_key(enum state state,
1581 const struct translation_st *translation,
1582 struct translation_ctx_st *ctx)
1584 EVP_PKEY *pkey = ctx->p2;
1585 unsigned char *buf = NULL;
1589 switch (EVP_PKEY_get_base_id(pkey)) {
1590 #ifndef OPENSSL_NO_DH
1593 switch (ctx->params->data_type) {
1594 case OSSL_PARAM_OCTET_STRING:
1595 ctx->sz = ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), &buf, 0, 1);
1598 case OSSL_PARAM_UNSIGNED_INTEGER:
1599 ctx->p2 = (void *)DH_get0_pub_key(EVP_PKEY_get0_DH(pkey));
1606 #ifndef OPENSSL_NO_DSA
1608 if (ctx->params->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1609 ctx->p2 = (void *)DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey));
1614 #ifndef OPENSSL_NO_EC
1616 if (ctx->params->data_type == OSSL_PARAM_OCTET_STRING) {
1617 const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
1618 BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
1619 const EC_GROUP *ecg = EC_KEY_get0_group(eckey);
1620 const EC_POINT *point = EC_KEY_get0_public_key(eckey);
1624 ctx->sz = EC_POINT_point2buf(ecg, point,
1625 POINT_CONVERSION_COMPRESSED,
1634 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1638 ret = default_fixup_args(state, translation, ctx);
1643 static int get_payload_bn(enum state state,
1644 const struct translation_st *translation,
1645 struct translation_ctx_st *ctx, const BIGNUM *bn)
1649 if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1651 ctx->p2 = (BIGNUM *)bn;
1653 return default_fixup_args(state, translation, ctx);
1656 static int get_dh_dsa_payload_p(enum state state,
1657 const struct translation_st *translation,
1658 struct translation_ctx_st *ctx)
1660 const BIGNUM *bn = NULL;
1661 EVP_PKEY *pkey = ctx->p2;
1663 switch (EVP_PKEY_get_base_id(pkey)) {
1664 #ifndef OPENSSL_NO_DH
1666 bn = DH_get0_p(EVP_PKEY_get0_DH(pkey));
1669 #ifndef OPENSSL_NO_DSA
1671 bn = DSA_get0_p(EVP_PKEY_get0_DSA(pkey));
1675 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1678 return get_payload_bn(state, translation, ctx, bn);
1681 static int get_dh_dsa_payload_q(enum state state,
1682 const struct translation_st *translation,
1683 struct translation_ctx_st *ctx)
1685 const BIGNUM *bn = NULL;
1687 switch (EVP_PKEY_get_base_id(ctx->p2)) {
1688 #ifndef OPENSSL_NO_DH
1690 bn = DH_get0_q(EVP_PKEY_get0_DH(ctx->p2));
1693 #ifndef OPENSSL_NO_DSA
1695 bn = DSA_get0_q(EVP_PKEY_get0_DSA(ctx->p2));
1700 return get_payload_bn(state, translation, ctx, bn);
1703 static int get_dh_dsa_payload_g(enum state state,
1704 const struct translation_st *translation,
1705 struct translation_ctx_st *ctx)
1707 const BIGNUM *bn = NULL;
1709 switch (EVP_PKEY_get_base_id(ctx->p2)) {
1710 #ifndef OPENSSL_NO_DH
1712 bn = DH_get0_g(EVP_PKEY_get0_DH(ctx->p2));
1715 #ifndef OPENSSL_NO_DSA
1717 bn = DSA_get0_g(EVP_PKEY_get0_DSA(ctx->p2));
1722 return get_payload_bn(state, translation, ctx, bn);
1725 static int get_payload_int(enum state state,
1726 const struct translation_st *translation,
1727 struct translation_ctx_st *ctx,
1730 if (ctx->params->data_type != OSSL_PARAM_INTEGER)
1735 return default_fixup_args(state, translation, ctx);
1738 static int get_ec_decoded_from_explicit_params(enum state state,
1739 const struct translation_st *translation,
1740 struct translation_ctx_st *ctx)
1743 EVP_PKEY *pkey = ctx->p2;
1745 switch (EVP_PKEY_base_id(pkey)) {
1746 #ifndef OPENSSL_NO_EC
1748 val = EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey));
1750 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
1756 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1760 return get_payload_int(state, translation, ctx, val);
1763 static int get_rsa_payload_n(enum state state,
1764 const struct translation_st *translation,
1765 struct translation_ctx_st *ctx)
1767 const BIGNUM *bn = NULL;
1769 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
1771 bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
1773 return get_payload_bn(state, translation, ctx, bn);
1776 static int get_rsa_payload_e(enum state state,
1777 const struct translation_st *translation,
1778 struct translation_ctx_st *ctx)
1780 const BIGNUM *bn = NULL;
1782 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
1784 bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
1786 return get_payload_bn(state, translation, ctx, bn);
1789 static int get_rsa_payload_d(enum state state,
1790 const struct translation_st *translation,
1791 struct translation_ctx_st *ctx)
1793 const BIGNUM *bn = NULL;
1795 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
1797 bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
1799 return get_payload_bn(state, translation, ctx, bn);
1802 static int get_rsa_payload_factor(enum state state,
1803 const struct translation_st *translation,
1804 struct translation_ctx_st *ctx,
1807 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1808 const BIGNUM *bn = NULL;
1810 switch (factornum) {
1819 size_t pnum = RSA_get_multi_prime_extra_count(r);
1820 const BIGNUM *factors[10];
1822 if (factornum - 2 < pnum
1823 && RSA_get0_multi_prime_factors(r, factors))
1824 bn = factors[factornum - 2];
1829 return get_payload_bn(state, translation, ctx, bn);
1832 static int get_rsa_payload_exponent(enum state state,
1833 const struct translation_st *translation,
1834 struct translation_ctx_st *ctx,
1837 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1838 const BIGNUM *bn = NULL;
1840 switch (exponentnum) {
1842 bn = RSA_get0_dmp1(r);
1845 bn = RSA_get0_dmq1(r);
1849 size_t pnum = RSA_get_multi_prime_extra_count(r);
1850 const BIGNUM *exps[10], *coeffs[10];
1852 if (exponentnum - 2 < pnum
1853 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1854 bn = exps[exponentnum - 2];
1859 return get_payload_bn(state, translation, ctx, bn);
1862 static int get_rsa_payload_coefficient(enum state state,
1863 const struct translation_st *translation,
1864 struct translation_ctx_st *ctx,
1865 size_t coefficientnum)
1867 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1868 const BIGNUM *bn = NULL;
1870 switch (coefficientnum) {
1872 bn = RSA_get0_iqmp(r);
1876 size_t pnum = RSA_get_multi_prime_extra_count(r);
1877 const BIGNUM *exps[10], *coeffs[10];
1879 if (coefficientnum - 1 < pnum
1880 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1881 bn = coeffs[coefficientnum - 1];
1886 return get_payload_bn(state, translation, ctx, bn);
1889 #define IMPL_GET_RSA_PAYLOAD_FACTOR(n) \
1891 get_rsa_payload_f##n(enum state state, \
1892 const struct translation_st *translation, \
1893 struct translation_ctx_st *ctx) \
1895 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
1897 return get_rsa_payload_factor(state, translation, ctx, n - 1); \
1900 #define IMPL_GET_RSA_PAYLOAD_EXPONENT(n) \
1902 get_rsa_payload_e##n(enum state state, \
1903 const struct translation_st *translation, \
1904 struct translation_ctx_st *ctx) \
1906 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
1908 return get_rsa_payload_exponent(state, translation, ctx, \
1912 #define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n) \
1914 get_rsa_payload_c##n(enum state state, \
1915 const struct translation_st *translation, \
1916 struct translation_ctx_st *ctx) \
1918 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
1920 return get_rsa_payload_coefficient(state, translation, ctx, \
1924 IMPL_GET_RSA_PAYLOAD_FACTOR(1)
1925 IMPL_GET_RSA_PAYLOAD_FACTOR(2)
1926 IMPL_GET_RSA_PAYLOAD_FACTOR(3)
1927 IMPL_GET_RSA_PAYLOAD_FACTOR(4)
1928 IMPL_GET_RSA_PAYLOAD_FACTOR(5)
1929 IMPL_GET_RSA_PAYLOAD_FACTOR(6)
1930 IMPL_GET_RSA_PAYLOAD_FACTOR(7)
1931 IMPL_GET_RSA_PAYLOAD_FACTOR(8)
1932 IMPL_GET_RSA_PAYLOAD_FACTOR(9)
1933 IMPL_GET_RSA_PAYLOAD_FACTOR(10)
1934 IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
1935 IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
1936 IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
1937 IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
1938 IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
1939 IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
1940 IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
1941 IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
1942 IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
1943 IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
1944 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
1945 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
1946 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
1947 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
1948 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
1949 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
1950 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
1951 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
1952 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
1955 * The translation table itself
1956 * ============================
1959 static const struct translation_st evp_pkey_ctx_translations[] = {
1961 * DistID: we pass it to the backend as an octet string,
1962 * but get it back as a pointer to an octet string.
1964 * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
1965 * that has no separate counterpart in OSSL_PARAM terms, since we get
1966 * the length of the DistID automatically when getting the DistID itself.
1968 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
1969 EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
1970 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
1972 EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
1973 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
1975 EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
1976 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
1984 * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting. The
1985 * fixup function has to handle this...
1987 { NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1988 EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
1989 OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
1991 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1992 EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
1993 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1994 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1995 EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
1996 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1997 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1998 EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
1999 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2000 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2001 EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
2002 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2003 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2004 EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
2005 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2006 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2007 EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
2008 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2009 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2010 EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
2011 OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
2012 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2013 EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
2014 OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
2016 /* DHX Keygen Parameters that are shared with DH */
2017 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2018 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
2019 OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
2020 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2021 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
2022 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2023 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2024 EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
2025 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, NULL },
2026 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2027 EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
2028 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
2030 /* DH Keygen Parameters that are shared with DHX */
2031 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2032 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
2033 OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
2034 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2035 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
2036 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2037 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2038 EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
2039 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
2040 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2041 EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
2042 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
2044 /* DH specific Keygen Parameters */
2045 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2046 EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
2047 OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
2049 /* DHX specific Keygen Parameters */
2050 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2051 EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
2052 OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2054 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
2055 EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
2056 OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2062 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2063 EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
2064 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2065 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2066 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
2067 OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2068 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2069 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
2070 OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2076 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2077 EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
2078 OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
2079 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2080 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
2081 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2082 fix_ec_paramgen_curve_nid },
2084 * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
2085 * both for setting and getting. The fixup function has to handle this...
2087 { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2088 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
2089 OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
2090 fix_ecdh_cofactor },
2091 { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2092 EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
2093 OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
2094 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2095 EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
2096 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2097 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2098 EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
2099 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2100 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2101 EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
2102 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2103 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2104 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
2105 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2106 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2107 EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
2108 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2109 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2110 EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
2111 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2119 * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
2120 * and can be both with OSSL_PARAM. We standardise on strings here,
2121 * fix_rsa_padding_mode() does the work when the caller has a different
2124 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2125 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2126 EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
2127 OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2128 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2129 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2130 EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
2131 OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2133 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2134 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2135 EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
2136 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2137 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2138 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2139 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
2140 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2143 * RSA-PSS saltlen is essentially numeric, but certain values can be
2144 * expressed as keywords (strings) with ctrl_str. The corresponding
2145 * OSSL_PARAM allows both forms.
2146 * fix_rsa_pss_saltlen() takes care of the distinction.
2148 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2149 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
2150 OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2151 fix_rsa_pss_saltlen },
2152 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2153 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
2154 OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2155 fix_rsa_pss_saltlen },
2157 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2158 EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
2159 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2160 { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2161 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
2162 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2164 * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
2165 * This is accommodated by default_fixup_args() above, which mimics that
2166 * expectation for any translation item where |ctrl_str| is NULL and
2167 * |ctrl_hexstr| is non-NULL.
2169 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2170 EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
2171 OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2172 { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2173 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
2174 OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2176 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2177 EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
2178 OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2179 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2180 EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
2181 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2182 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2183 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
2184 OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
2185 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2186 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
2187 OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2188 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2189 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
2190 OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2191 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2192 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
2193 OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2199 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2200 EVP_PKEY_CTRL_SET_DIGEST_SIZE, "digestsize", NULL,
2201 OSSL_MAC_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2207 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2208 EVP_PKEY_CTRL_TLS_MD, "md", NULL,
2209 OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2210 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2211 EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
2212 OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
2213 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2214 EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
2215 OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
2221 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2222 EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
2223 OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2224 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2225 EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
2226 OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2227 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2228 EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
2229 OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2230 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2231 EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
2232 OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
2233 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2234 EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
2235 OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
2241 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2242 EVP_PKEY_CTRL_PASS, "pass", "hexpass",
2243 OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
2244 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2245 EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
2246 OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2247 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2248 EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
2249 OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2250 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2251 EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
2252 OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2253 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2254 EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
2255 OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2256 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2257 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
2258 OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2260 { SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
2261 EVP_PKEY_CTRL_CIPHER, NULL, NULL,
2262 OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
2263 { SET, -1, -1, EVP_PKEY_OP_KEYGEN,
2264 EVP_PKEY_CTRL_SET_MAC_KEY, "key", "hexkey",
2265 OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2267 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2268 EVP_PKEY_CTRL_MD, NULL, NULL,
2269 OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2270 { GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2271 EVP_PKEY_CTRL_GET_MD, NULL, NULL,
2272 OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2275 static const struct translation_st evp_pkey_translations[] = {
2277 * The following contain no ctrls, they are exclusively here to extract
2278 * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
2279 * on |fixup_args| to pass the actual data. The |fixup_args| should
2280 * expect to get the EVP_PKEY pointer through |ctx->p2|.
2284 { GET, -1, -1, -1, 0, NULL, NULL,
2285 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2286 get_payload_group_name },
2287 { GET, -1, -1, -1, 0, NULL, NULL,
2288 OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
2289 get_payload_private_key },
2290 { GET, -1, -1, -1, 0, NULL, NULL,
2291 OSSL_PKEY_PARAM_PUB_KEY,
2292 0 /* no data type, let get_payload_public_key() handle that */,
2293 get_payload_public_key },
2296 { GET, -1, -1, -1, 0, NULL, NULL,
2297 OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
2298 get_dh_dsa_payload_p },
2299 { GET, -1, -1, -1, 0, NULL, NULL,
2300 OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
2301 get_dh_dsa_payload_g },
2302 { GET, -1, -1, -1, 0, NULL, NULL,
2303 OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
2304 get_dh_dsa_payload_q },
2307 { GET, -1, -1, -1, 0, NULL, NULL,
2308 OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
2309 get_rsa_payload_n },
2310 { GET, -1, -1, -1, 0, NULL, NULL,
2311 OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
2312 get_rsa_payload_e },
2313 { GET, -1, -1, -1, 0, NULL, NULL,
2314 OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
2315 get_rsa_payload_d },
2316 { GET, -1, -1, -1, 0, NULL, NULL,
2317 OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
2318 get_rsa_payload_f1 },
2319 { GET, -1, -1, -1, 0, NULL, NULL,
2320 OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
2321 get_rsa_payload_f2 },
2322 { GET, -1, -1, -1, 0, NULL, NULL,
2323 OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
2324 get_rsa_payload_f3 },
2325 { GET, -1, -1, -1, 0, NULL, NULL,
2326 OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
2327 get_rsa_payload_f4 },
2328 { GET, -1, -1, -1, 0, NULL, NULL,
2329 OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
2330 get_rsa_payload_f5 },
2331 { GET, -1, -1, -1, 0, NULL, NULL,
2332 OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
2333 get_rsa_payload_f6 },
2334 { GET, -1, -1, -1, 0, NULL, NULL,
2335 OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
2336 get_rsa_payload_f7 },
2337 { GET, -1, -1, -1, 0, NULL, NULL,
2338 OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
2339 get_rsa_payload_f8 },
2340 { GET, -1, -1, -1, 0, NULL, NULL,
2341 OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
2342 get_rsa_payload_f9 },
2343 { GET, -1, -1, -1, 0, NULL, NULL,
2344 OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
2345 get_rsa_payload_f10 },
2346 { GET, -1, -1, -1, 0, NULL, NULL,
2347 OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2348 get_rsa_payload_e1 },
2349 { GET, -1, -1, -1, 0, NULL, NULL,
2350 OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2351 get_rsa_payload_e2 },
2352 { GET, -1, -1, -1, 0, NULL, NULL,
2353 OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2354 get_rsa_payload_e3 },
2355 { GET, -1, -1, -1, 0, NULL, NULL,
2356 OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2357 get_rsa_payload_e4 },
2358 { GET, -1, -1, -1, 0, NULL, NULL,
2359 OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2360 get_rsa_payload_e5 },
2361 { GET, -1, -1, -1, 0, NULL, NULL,
2362 OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2363 get_rsa_payload_e6 },
2364 { GET, -1, -1, -1, 0, NULL, NULL,
2365 OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2366 get_rsa_payload_e7 },
2367 { GET, -1, -1, -1, 0, NULL, NULL,
2368 OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2369 get_rsa_payload_e8 },
2370 { GET, -1, -1, -1, 0, NULL, NULL,
2371 OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2372 get_rsa_payload_e9 },
2373 { GET, -1, -1, -1, 0, NULL, NULL,
2374 OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
2375 get_rsa_payload_e10 },
2376 { GET, -1, -1, -1, 0, NULL, NULL,
2377 OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2378 get_rsa_payload_c1 },
2379 { GET, -1, -1, -1, 0, NULL, NULL,
2380 OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2381 get_rsa_payload_c2 },
2382 { GET, -1, -1, -1, 0, NULL, NULL,
2383 OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2384 get_rsa_payload_c3 },
2385 { GET, -1, -1, -1, 0, NULL, NULL,
2386 OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2387 get_rsa_payload_c4 },
2388 { GET, -1, -1, -1, 0, NULL, NULL,
2389 OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2390 get_rsa_payload_c5 },
2391 { GET, -1, -1, -1, 0, NULL, NULL,
2392 OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2393 get_rsa_payload_c6 },
2394 { GET, -1, -1, -1, 0, NULL, NULL,
2395 OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2396 get_rsa_payload_c7 },
2397 { GET, -1, -1, -1, 0, NULL, NULL,
2398 OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2399 get_rsa_payload_c8 },
2400 { GET, -1, -1, -1, 0, NULL, NULL,
2401 OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2402 get_rsa_payload_c9 },
2405 { GET, -1, -1, -1, 0, NULL, NULL,
2406 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, OSSL_PARAM_INTEGER,
2407 get_ec_decoded_from_explicit_params },
2410 static const struct translation_st *
2411 lookup_translation(struct translation_st *tmpl,
2412 const struct translation_st *translations,
2413 size_t translations_num)
2417 for (i = 0; i < translations_num; i++) {
2418 const struct translation_st *item = &translations[i];
2421 * Sanity check the translation table item.
2423 * 1. Either both keytypes are -1, or neither of them are.
2426 if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
2431 * Base search criteria: check that the optype and keytypes match,
2432 * if relevant. All callers must synthesise these bits somehow.
2434 if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
2437 * This expression is stunningly simple thanks to the sanity check
2440 if (item->keytype1 != -1
2441 && tmpl->keytype1 != item->keytype1
2442 && tmpl->keytype2 != item->keytype2)
2446 * Done with the base search criteria, now we check the criteria for
2447 * the individual types of translations:
2448 * ctrl->params, ctrl_str->params, and params->ctrl
2450 if (tmpl->ctrl_num != 0) {
2451 if (tmpl->ctrl_num != item->ctrl_num)
2453 } else if (tmpl->ctrl_str != NULL) {
2454 const char *ctrl_str = NULL;
2455 const char *ctrl_hexstr = NULL;
2458 * Search criteria that originates from a ctrl_str is only used
2459 * for setting, never for getting. Therefore, we only look at
2462 if (item->action_type != NONE
2463 && item->action_type != SET)
2466 * At least one of the ctrl cmd names must be match the ctrl
2467 * cmd name in the template.
2469 if (item->ctrl_str != NULL
2470 && OPENSSL_strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
2471 ctrl_str = tmpl->ctrl_str;
2472 else if (item->ctrl_hexstr != NULL
2473 && OPENSSL_strcasecmp(tmpl->ctrl_hexstr,
2474 item->ctrl_hexstr) == 0)
2475 ctrl_hexstr = tmpl->ctrl_hexstr;
2479 /* Modify the template to signal which string matched */
2480 tmpl->ctrl_str = ctrl_str;
2481 tmpl->ctrl_hexstr = ctrl_hexstr;
2482 } else if (tmpl->param_key != NULL) {
2484 * Search criteria that originates from a OSSL_PARAM setter or
2487 * Ctrls were fundamentally bidirectional, with only the ctrl
2488 * command macro name implying direction (if you're lucky).
2489 * A few ctrl commands were even taking advantage of the
2490 * bidirectional nature, making the direction depend in the
2491 * value of the numeric argument.
2493 * OSSL_PARAM functions are fundamentally different, in that
2494 * setters and getters are separated, so the data direction is
2495 * implied by the function that's used. The same OSSL_PARAM
2496 * key name can therefore be used in both directions. We must
2497 * therefore take the action type into account in this case.
2499 if ((item->action_type != NONE
2500 && tmpl->action_type != item->action_type)
2501 || (item->param_key != NULL
2502 && OPENSSL_strcasecmp(tmpl->param_key,
2503 item->param_key) != 0))
2515 static const struct translation_st *
2516 lookup_evp_pkey_ctx_translation(struct translation_st *tmpl)
2518 return lookup_translation(tmpl, evp_pkey_ctx_translations,
2519 OSSL_NELEM(evp_pkey_ctx_translations));
2522 static const struct translation_st *
2523 lookup_evp_pkey_translation(struct translation_st *tmpl)
2525 return lookup_translation(tmpl, evp_pkey_translations,
2526 OSSL_NELEM(evp_pkey_translations));
2529 /* This must ONLY be called for provider side operations */
2530 int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *pctx,
2531 int keytype, int optype,
2532 int cmd, int p1, void *p2)
2534 struct translation_ctx_st ctx = { 0, };
2535 struct translation_st tmpl = { 0, };
2536 const struct translation_st *translation = NULL;
2537 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2539 fixup_args_fn *fixup = default_fixup_args;
2542 keytype = pctx->legacy_keytype;
2543 tmpl.ctrl_num = cmd;
2544 tmpl.keytype1 = tmpl.keytype2 = keytype;
2545 tmpl.optype = optype;
2546 translation = lookup_evp_pkey_ctx_translation(&tmpl);
2548 if (translation == NULL) {
2549 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
2553 if (pctx->pmeth != NULL
2554 && pctx->pmeth->pkey_id != translation->keytype1
2555 && pctx->pmeth->pkey_id != translation->keytype2)
2558 if (translation->fixup_args != NULL)
2559 fixup = translation->fixup_args;
2560 ctx.action_type = translation->action_type;
2565 ctx.params = params;
2567 ret = fixup(PRE_CTRL_TO_PARAMS, translation, &ctx);
2570 switch (ctx.action_type) {
2572 /* fixup_args is expected to make sure this is dead code */
2575 ret = evp_pkey_ctx_get_params_strict(pctx, ctx.params);
2578 ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2584 * In POST, we pass the return value as p1, allowing the fixup_args
2585 * function to affect it by changing its value.
2589 fixup(POST_CTRL_TO_PARAMS, translation, &ctx);
2593 cleanup_translation_ctx(POST_CTRL_TO_PARAMS, translation, &ctx);
2598 /* This must ONLY be called for provider side operations */
2599 int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *pctx,
2600 const char *name, const char *value)
2602 struct translation_ctx_st ctx = { 0, };
2603 struct translation_st tmpl = { 0, };
2604 const struct translation_st *translation = NULL;
2605 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2606 int keytype = pctx->legacy_keytype;
2607 int optype = pctx->operation == 0 ? -1 : pctx->operation;
2609 fixup_args_fn *fixup = default_fixup_args;
2611 tmpl.action_type = SET;
2612 tmpl.keytype1 = tmpl.keytype2 = keytype;
2613 tmpl.optype = optype;
2614 tmpl.ctrl_str = name;
2615 tmpl.ctrl_hexstr = name;
2616 translation = lookup_evp_pkey_ctx_translation(&tmpl);
2618 if (translation != NULL) {
2619 if (translation->fixup_args != NULL)
2620 fixup = translation->fixup_args;
2621 ctx.action_type = translation->action_type;
2622 ctx.ishex = (tmpl.ctrl_hexstr != NULL);
2624 /* String controls really only support setting */
2625 ctx.action_type = SET;
2627 ctx.ctrl_str = name;
2628 ctx.p1 = (int)strlen(value);
2629 ctx.p2 = (char *)value;
2631 ctx.params = params;
2633 ret = fixup(PRE_CTRL_STR_TO_PARAMS, translation, &ctx);
2636 switch (ctx.action_type) {
2638 /* fixup_args is expected to make sure this is dead code */
2642 * this is dead code, but must be present, or some compilers
2647 ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2653 ret = fixup(POST_CTRL_STR_TO_PARAMS, translation, &ctx);
2655 cleanup_translation_ctx(CLEANUP_CTRL_STR_TO_PARAMS, translation, &ctx);
2660 /* This must ONLY be called for legacy operations */
2661 static int evp_pkey_ctx_setget_params_to_ctrl(EVP_PKEY_CTX *pctx,
2662 enum action action_type,
2665 int keytype = pctx->legacy_keytype;
2666 int optype = pctx->operation == 0 ? -1 : pctx->operation;
2668 for (; params != NULL && params->key != NULL; params++) {
2669 struct translation_ctx_st ctx = { 0, };
2670 struct translation_st tmpl = { 0, };
2671 const struct translation_st *translation = NULL;
2672 fixup_args_fn *fixup = default_fixup_args;
2675 tmpl.action_type = action_type;
2676 tmpl.keytype1 = tmpl.keytype2 = keytype;
2677 tmpl.optype = optype;
2678 tmpl.param_key = params->key;
2679 translation = lookup_evp_pkey_ctx_translation(&tmpl);
2681 if (translation != NULL) {
2682 if (translation->fixup_args != NULL)
2683 fixup = translation->fixup_args;
2684 ctx.action_type = translation->action_type;
2687 ctx.params = params;
2689 ret = fixup(PRE_PARAMS_TO_CTRL, translation, &ctx);
2691 if (ret > 0 && action_type != NONE)
2692 ret = EVP_PKEY_CTX_ctrl(pctx, keytype, optype,
2693 ctx.ctrl_cmd, ctx.p1, ctx.p2);
2696 * In POST, we pass the return value as p1, allowing the fixup_args
2697 * function to put it to good use, or maybe affect it.
2701 fixup(POST_PARAMS_TO_CTRL, translation, &ctx);
2705 cleanup_translation_ctx(CLEANUP_PARAMS_TO_CTRL, translation, &ctx);
2713 int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
2715 return evp_pkey_ctx_setget_params_to_ctrl(ctx, SET, (OSSL_PARAM *)params);
2718 int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
2720 return evp_pkey_ctx_setget_params_to_ctrl(ctx, GET, params);
2723 /* This must ONLY be called for legacy EVP_PKEYs */
2724 static int evp_pkey_setget_params_to_ctrl(const EVP_PKEY *pkey,
2725 enum action action_type,
2730 for (; params != NULL && params->key != NULL; params++) {
2731 struct translation_ctx_st ctx = { 0, };
2732 struct translation_st tmpl = { 0, };
2733 const struct translation_st *translation = NULL;
2734 fixup_args_fn *fixup = default_fixup_args;
2736 tmpl.action_type = action_type;
2737 tmpl.param_key = params->key;
2738 translation = lookup_evp_pkey_translation(&tmpl);
2740 if (translation != NULL) {
2741 if (translation->fixup_args != NULL)
2742 fixup = translation->fixup_args;
2743 ctx.action_type = translation->action_type;
2745 ctx.p2 = (void *)pkey;
2746 ctx.params = params;
2749 * EVP_PKEY doesn't have any ctrl function, so we rely completely
2750 * on fixup_args to do the whole work. Also, we currently only
2753 if (!ossl_assert(translation != NULL)
2754 || !ossl_assert(translation->action_type == GET)
2755 || !ossl_assert(translation->fixup_args != NULL)) {
2759 ret = fixup(PKEY, translation, &ctx);
2761 cleanup_translation_ctx(PKEY, translation, &ctx);
2766 int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params)
2768 return evp_pkey_setget_params_to_ctrl(pkey, GET, params);