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 #include "e_os.h" /* strcasecmp() for Windows */
42 struct translation_ctx_st; /* Forwarding */
43 struct translation_st; /* Forwarding */
46 * The fixup_args functions are called with the following parameters:
48 * |state| The state we're called in, explained further at the
49 * end of this comment.
50 * |translation| The translation item, to be pilfered for data as
52 * |ctx| The translation context, which contains copies of
53 * the following arguments, applicable according to
54 * the caller. All of the attributes in this context
55 * may be freely modified by the fixup_args function.
56 * For cleanup, call cleanup_translation_ctx().
58 * The |state| tells the fixup_args function something about the caller and
59 * what they may expect:
61 * PKEY The fixup_args function has been called
62 * from an EVP_PKEY payload getter / setter,
63 * and is fully responsible for getting or
64 * setting the requested data. With this
65 * state, the fixup_args function is expected
66 * to use or modify |*params|, depending on
69 * PRE_CTRL_TO_PARAMS The fixup_args function has been called
70 * POST_CTRL_TO_PARAMS from EVP_PKEY_CTX_ctrl(), to help with
71 * translating the ctrl data to an OSSL_PARAM
72 * element or back. The calling sequence is
75 * 1. fixup_args(PRE_CTRL_TO_PARAMS, ...)
76 * 2. EVP_PKEY_CTX_set_params() or
77 * EVP_PKEY_CTX_get_params()
78 * 3. fixup_args(POST_CTRL_TO_PARAMS, ...)
80 * With the PRE_CTRL_TO_PARAMS state, the
81 * fixup_args function is expected to modify
82 * the passed |*params| in whatever way
83 * necessary, when |action_type == SET|.
84 * With the POST_CTRL_TO_PARAMS state, the
85 * fixup_args function is expected to modify
86 * the passed |p2| in whatever way necessary,
87 * when |action_type == GET|.
89 * The return value from the fixup_args call
90 * with the POST_CTRL_TO_PARAMS state becomes
91 * the return value back to EVP_PKEY_CTX_ctrl().
93 * CLEANUP_CTRL_TO_PARAMS The cleanup_args functions has been called
94 * from EVP_PKEY_CTX_ctrl(), to clean up what
95 * the fixup_args function has done, if needed.
98 * PRE_CTRL_STR_TO_PARAMS The fixup_args function has been called
99 * POST_CTRL_STR_TO_PARAMS from EVP_PKEY_CTX_ctrl_str(), to help with
100 * translating the ctrl_str data to an
101 * OSSL_PARAM element or back. The calling
102 * sequence is as follows:
104 * 1. fixup_args(PRE_CTRL_STR_TO_PARAMS, ...)
105 * 2. EVP_PKEY_CTX_set_params() or
106 * EVP_PKEY_CTX_get_params()
107 * 3. fixup_args(POST_CTRL_STR_TO_PARAMS, ...)
109 * With the PRE_CTRL_STR_TO_PARAMS state,
110 * the fixup_args function is expected to
111 * modify the passed |*params| in whatever
112 * way necessary, when |action_type == SET|.
113 * With the POST_CTRL_STR_TO_PARAMS state,
114 * the fixup_args function is only expected
117 * CLEANUP_CTRL_STR_TO_PARAMS The cleanup_args functions has been called
118 * from EVP_PKEY_CTX_ctrl_str(), to clean up
119 * what the fixup_args function has done, if
122 * PRE_PARAMS_TO_CTRL The fixup_args function has been called
123 * POST_PARAMS_TO_CTRL from EVP_PKEY_CTX_get_params() or
124 * EVP_PKEY_CTX_set_params(), to help with
125 * translating the OSSL_PARAM data to the
126 * corresponding EVP_PKEY_CTX_ctrl() arguments
127 * or the other way around. The calling
128 * sequence is as follows:
130 * 1. fixup_args(PRE_PARAMS_TO_CTRL, ...)
131 * 2. EVP_PKEY_CTX_ctrl()
132 * 3. fixup_args(POST_PARAMS_TO_CTRL, ...)
134 * With the PRE_PARAMS_TO_CTRL state, the
135 * fixup_args function is expected to modify
136 * the passed |p1| and |p2| in whatever way
137 * necessary, when |action_type == SET|.
138 * With the POST_PARAMS_TO_CTRL state, the
139 * fixup_args function is expected to
140 * modify the passed |*params| in whatever
141 * way necessary, when |action_type == GET|.
143 * CLEANUP_PARAMS_TO_CTRL The cleanup_args functions has been called
144 * from EVP_PKEY_CTX_get_params() or
145 * EVP_PKEY_CTX_set_params(), to clean up what
146 * the fixup_args function has done, if needed.
150 PRE_CTRL_TO_PARAMS, POST_CTRL_TO_PARAMS, CLEANUP_CTRL_TO_PARAMS,
151 PRE_CTRL_STR_TO_PARAMS, POST_CTRL_STR_TO_PARAMS, CLEANUP_CTRL_STR_TO_PARAMS,
152 PRE_PARAMS_TO_CTRL, POST_PARAMS_TO_CTRL, CLEANUP_PARAMS_TO_CTRL
155 NONE = 0, GET = 1, SET = 2
157 typedef int fixup_args_fn(enum state state,
158 const struct translation_st *translation,
159 struct translation_ctx_st *ctx);
160 typedef int cleanup_args_fn(enum state state,
161 const struct translation_st *translation,
162 struct translation_ctx_st *ctx);
164 struct translation_ctx_st {
166 * The EVP_PKEY_CTX, for calls on that structure, to be pilfered for data
171 * The action type (GET or SET). This may be 0 in some cases, and should
172 * be modified by the fixup_args function in the PRE states. It should
173 * otherwise remain untouched once set.
175 enum action action_type;
177 * For ctrl to params translation, the actual ctrl command number used.
178 * For params to ctrl translation, 0.
182 * For ctrl_str to params translation, the actual ctrl command string
183 * used. In this case, the (string) value is always passed as |p2|.
184 * For params to ctrl translation, this is NULL. Along with it is also
185 * and indicator whether it matched |ctrl_str| or |ctrl_hexstr| in the
188 const char *ctrl_str;
190 /* the ctrl-style int argument. */
192 /* the ctrl-style void* argument. */
194 /* a size, for passing back the |p2| size where applicable */
196 /* pointer to the OSSL_PARAM-style params array. */
200 * The following are used entirely internally by the fixup_args functions
201 * and should not be touched by the callers, at all.
205 * Copy of the ctrl-style void* argument, if the fixup_args function
206 * needs to manipulate |p2| but wants to remember original.
209 /* Diverse types of storage for the needy. */
210 char name_buf[OSSL_MAX_NAME_SIZE];
216 struct translation_st {
218 * What this table item does.
220 * If the item has this set to 0, it means that both GET and SET are
221 * supported, and |fixup_args| will determine which it is. This is to
222 * support translations of ctrls where the action type depends on the
223 * value of |p1| or |p2| (ctrls are really bi-directional, but are
224 * seldom used that way).
226 * This can be also used in the lookup template when it looks up by
227 * OSSL_PARAM key, to indicate if a setter or a getter called.
229 enum action action_type;
232 * Conditions, for params->ctrl translations.
234 * In table item, |keytype1| and |keytype2| can be set to -1 to indicate
235 * that this item supports all key types (or rather, that |fixup_args|
236 * will check and return an error if it's not supported).
237 * Any of these may be set to 0 to indicate that they are unset.
239 int keytype1; /* The EVP_PKEY_XXX type, i.e. NIDs. #legacy */
240 int keytype2; /* Another EVP_PKEY_XXX type, used for aliases */
241 int optype; /* The operation type */
244 * Lookup and translation attributes
246 * |ctrl_num|, |ctrl_str|, |ctrl_hexstr| and |param_key| are lookup
249 * |ctrl_num| may be 0 or that |param_key| may be NULL in the table item,
250 * but not at the same time. If they are, they are simply not used for
252 * When |ctrl_num| == 0, no ctrl will be called. Likewise, when
253 * |param_key| == NULL, no OSSL_PARAM setter/getter will be called.
254 * In that case the treatment of the translation item relies entirely on
255 * |fixup_args|, which is then assumed to have side effects.
257 * As a special case, it's possible to set |ctrl_hexstr| and assign NULL
258 * to |ctrl_str|. That will signal to default_fixup_args() that the
259 * value must always be interpreted as hex.
261 int ctrl_num; /* EVP_PKEY_CTRL_xxx */
262 const char *ctrl_str; /* The corresponding ctrl string */
263 const char *ctrl_hexstr; /* The alternative "hex{str}" ctrl string */
264 const char *param_key; /* The corresponding OSSL_PARAM key */
266 * The appropriate OSSL_PARAM data type. This may be 0 to indicate that
267 * this OSSL_PARAM may have more than one data type, depending on input
268 * material. In this case, |fixup_args| is expected to check and handle
271 unsigned int param_data_type;
276 * |fixup_args| is always called before (for SET) or after (for GET)
277 * the actual ctrl / OSSL_PARAM function.
279 fixup_args_fn *fixup_args;
283 * Fixer function implementations
284 * ==============================
288 * default_check isn't a fixer per se, but rather a helper function to
289 * perform certain standard checks.
291 static int default_check(enum state state,
292 const struct translation_st *translation,
293 const struct translation_ctx_st *ctx)
298 case PRE_CTRL_TO_PARAMS:
299 if (!ossl_assert(translation != NULL)) {
300 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
303 if (!ossl_assert(translation->param_key != 0)
304 || !ossl_assert(translation->param_data_type != 0)) {
305 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
309 case PRE_CTRL_STR_TO_PARAMS:
311 * For ctrl_str to params translation, we allow direct use of
312 * OSSL_PARAM keys as ctrl_str keys. Therefore, it's possible that
313 * we end up with |translation == NULL|, which is fine. The fixup
314 * function will have to deal with it carefully.
316 if (translation != NULL) {
317 if (!ossl_assert(translation->action_type != GET)) {
318 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
321 if (!ossl_assert(translation->param_key != NULL)
322 || !ossl_assert(translation->param_data_type != 0)) {
323 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
328 case PRE_PARAMS_TO_CTRL:
329 case POST_PARAMS_TO_CTRL:
330 if (!ossl_assert(translation != NULL)) {
331 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
334 if (!ossl_assert(translation->ctrl_num != 0)
335 || !ossl_assert(translation->param_data_type != 0)) {
336 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
341 /* Nothing else to check */
346 * default_fixup_args fixes up all sorts of arguments, governed by the
347 * diverse attributes in the translation item. It covers all "standard"
348 * base ctrl functionality, meaning it can handle basic conversion of
349 * data between p1+p2 (SET) or return value+p2 (GET) as long as the values
350 * don't have extra semantics (such as NIDs, OIDs, that sort of stuff).
351 * Extra semantics must be handled via specific fixup_args functions.
353 * The following states and action type combinations have standard handling
354 * done in this function:
356 * PRE_CTRL_TO_PARAMS, 0 - ERROR. action type must be
357 * determined by a fixup function.
358 * PRE_CTRL_TO_PARAMS, SET | GET - |p1| and |p2| are converted to an
359 * OSSL_PARAM according to the data
360 * type given in |translattion|.
361 * For OSSL_PARAM_UNSIGNED_INTEGER,
362 * a BIGNUM passed as |p2| is accepted.
363 * POST_CTRL_TO_PARAMS, GET - If the OSSL_PARAM data type is a
364 * STRING or PTR type, |p1| is set
365 * to the OSSL_PARAM return size, and
366 * |p2| is set to the string.
367 * PRE_CTRL_STR_TO_PARAMS, !SET - ERROR. That combination is not
369 * PRE_CTRL_STR_TO_PARAMS, SET - |p2| is taken as a string, and is
370 * converted to an OSSL_PARAM in a
371 * standard manner, guided by the
372 * param key and data type from
374 * PRE_PARAMS_TO_CTRL, SET - the OSSL_PARAM is converted to
375 * |p1| and |p2| according to the
376 * data type given in |translation|
377 * For OSSL_PARAM_UNSIGNED_INTEGER,
378 * if |p2| is non-NULL, then |*p2|
379 * is assigned a BIGNUM, otherwise
380 * |p1| is assigned an unsigned int.
381 * POST_PARAMS_TO_CTRL, GET - |p1| and |p2| are converted to
382 * an OSSL_PARAM, in the same manner
383 * as for the combination of
384 * PRE_CTRL_TO_PARAMS, SET.
386 static int default_fixup_args(enum state state,
387 const struct translation_st *translation,
388 struct translation_ctx_st *ctx)
392 if ((ret = default_check(state, translation, ctx)) < 0)
397 /* For states this function should never have been called with */
398 ERR_raise_data(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
399 "[action:%d, state:%d]", ctx->action_type, state);
403 * PRE_CTRL_TO_PARAMS and POST_CTRL_TO_PARAMS handle ctrl to params
404 * translations. PRE_CTRL_TO_PARAMS is responsible for preparing
405 * |*params|, and POST_CTRL_TO_PARAMS is responsible for bringing the
406 * result back to |*p2| and the return value.
408 case PRE_CTRL_TO_PARAMS:
409 /* This is ctrl to params translation, so we need an OSSL_PARAM key */
410 if (ctx->action_type == NONE) {
412 * No action type is an error here. That's a case for a
413 * special fixup function.
415 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
416 "[action:%d, state:%d]", ctx->action_type, state);
420 if (translation->optype != 0) {
421 if ((EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
422 && ctx->pctx->op.sig.algctx == NULL)
423 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx->pctx)
424 && ctx->pctx->op.kex.algctx == NULL)
425 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx->pctx)
426 && ctx->pctx->op.ciph.algctx == NULL)
427 || (EVP_PKEY_CTX_IS_KEM_OP(ctx->pctx)
428 && ctx->pctx->op.encap.algctx == NULL)
430 * The following may be unnecessary, but we have them
431 * for good measure...
433 || (EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx)
434 && ctx->pctx->op.keymgmt.genctx == NULL)
435 || (EVP_PKEY_CTX_IS_FROMDATA_OP(ctx->pctx)
436 && ctx->pctx->op.keymgmt.genctx == NULL)) {
437 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
438 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
444 * OSSL_PARAM_construct_TYPE() works equally well for both SET and GET.
446 switch (translation->param_data_type) {
447 case OSSL_PARAM_INTEGER:
448 *ctx->params = OSSL_PARAM_construct_int(translation->param_key,
451 case OSSL_PARAM_UNSIGNED_INTEGER:
453 * BIGNUMs are passed via |p2|. For all ctrl's that just want
454 * to pass a simple integer via |p1|, |p2| is expected to be
457 * Note that this allocates a buffer, which the cleanup function
460 if (ctx->p2 != NULL) {
461 if (ctx->action_type == SET) {
462 ctx->buflen = BN_num_bytes(ctx->p2);
463 if ((ctx->allocated_buf =
464 OPENSSL_malloc(ctx->buflen)) == NULL) {
465 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
468 if (BN_bn2nativepad(ctx->p2,
469 ctx->allocated_buf, ctx->buflen) < 0) {
470 OPENSSL_free(ctx->allocated_buf);
471 ctx->allocated_buf = NULL;
475 OSSL_PARAM_construct_BN(translation->param_key,
480 * No support for getting a BIGNUM by ctrl, this needs
481 * fixup_args function support.
483 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
484 "[action:%d, state:%d] trying to get a "
485 "BIGNUM via ctrl call",
486 ctx->action_type, state);
491 OSSL_PARAM_construct_uint(translation->param_key,
492 (unsigned int *)&ctx->p1);
495 case OSSL_PARAM_UTF8_STRING:
497 OSSL_PARAM_construct_utf8_string(translation->param_key,
498 ctx->p2, (size_t)ctx->p1);
500 case OSSL_PARAM_UTF8_PTR:
502 OSSL_PARAM_construct_utf8_ptr(translation->param_key,
503 ctx->p2, (size_t)ctx->p1);
505 case OSSL_PARAM_OCTET_STRING:
507 OSSL_PARAM_construct_octet_string(translation->param_key,
508 ctx->p2, (size_t)ctx->p1);
510 case OSSL_PARAM_OCTET_PTR:
512 OSSL_PARAM_construct_octet_ptr(translation->param_key,
513 ctx->p2, (size_t)ctx->p1);
517 case POST_CTRL_TO_PARAMS:
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.
523 if (ctx->action_type == GET) {
524 switch (translation->param_data_type) {
525 case OSSL_PARAM_UTF8_STRING:
526 case OSSL_PARAM_UTF8_PTR:
527 case OSSL_PARAM_OCTET_STRING:
528 case OSSL_PARAM_OCTET_PTR:
529 ctx->p1 = (int)ctx->params[0].return_size;
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.
541 case PRE_CTRL_STR_TO_PARAMS:
543 /* This is ctrl_str to params translation */
544 const char *tmp_ctrl_str = ctx->ctrl_str;
545 const char *orig_ctrl_str = ctx->ctrl_str;
546 const char *orig_value = ctx->p2;
547 const OSSL_PARAM *settable = NULL;
550 /* Only setting is supported here */
551 if (ctx->action_type != SET) {
552 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
553 "[action:%d, state:%d] only setting allowed",
554 ctx->action_type, state);
559 * If no translation exists, we simply pass the control string
562 if (translation != NULL) {
563 tmp_ctrl_str = ctx->ctrl_str = translation->param_key;
566 strcpy(ctx->name_buf, "hex");
567 if (OPENSSL_strlcat(ctx->name_buf, tmp_ctrl_str,
568 sizeof(ctx->name_buf)) <= 3) {
569 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
572 tmp_ctrl_str = ctx->name_buf;
576 settable = EVP_PKEY_CTX_settable_params(ctx->pctx);
577 if (!OSSL_PARAM_allocate_from_text(ctx->params, settable,
579 ctx->p2, strlen(ctx->p2),
582 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
583 "[action:%d, state:%d] name=%s, value=%s",
584 ctx->action_type, state,
585 orig_ctrl_str, orig_value);
590 ctx->allocated_buf = ctx->params->data;
591 ctx->buflen = ctx->params->data_size;
594 case POST_CTRL_STR_TO_PARAMS:
595 /* Nothing to be done */
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
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.
611 case POST_PARAMS_TO_CTRL:
614 case PRE_PARAMS_TO_CTRL:
616 /* This is params to ctrl translation */
617 if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
618 /* For the PRE state, only setting needs some work to be done */
620 /* When setting, we populate |p1| and |p2| from |*params| */
621 switch (translation->param_data_type) {
622 case OSSL_PARAM_INTEGER:
623 return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
624 case OSSL_PARAM_UNSIGNED_INTEGER:
625 if (ctx->p2 != NULL) {
626 /* BIGNUM passed down with p2 */
627 if (!OSSL_PARAM_get_BN(ctx->params, ctx->p2))
630 /* Normal C unsigned int passed down */
631 if (!OSSL_PARAM_get_uint(ctx->params,
632 (unsigned int *)&ctx->p1))
636 case OSSL_PARAM_UTF8_STRING:
637 return OSSL_PARAM_get_utf8_string(ctx->params,
639 case OSSL_PARAM_OCTET_STRING:
640 return OSSL_PARAM_get_octet_string(ctx->params,
643 case OSSL_PARAM_OCTET_PTR:
644 return OSSL_PARAM_get_octet_ptr(ctx->params,
647 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
648 "[action:%d, state:%d] "
649 "unknown OSSL_PARAM data type %d",
650 ctx->action_type, state,
651 translation->param_data_type);
654 } else if ((state == POST_PARAMS_TO_CTRL || state == PKEY)
655 && ctx->action_type == GET) {
656 /* For the POST state, only getting needs some work to be done */
657 unsigned int param_data_type = translation->param_data_type;
658 size_t size = (size_t)ctx->p1;
662 if (param_data_type == 0) {
663 /* we must have a fixup_args function to work */
664 if (!ossl_assert(translation->fixup_args != NULL)) {
665 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
668 param_data_type = ctx->params->data_type;
670 /* When getting, we populate |*params| from |p1| and |p2| */
671 switch (param_data_type) {
672 case OSSL_PARAM_INTEGER:
673 return OSSL_PARAM_set_int(ctx->params, ctx->p1);
674 case OSSL_PARAM_UNSIGNED_INTEGER:
675 if (ctx->p2 != NULL) {
676 /* BIGNUM passed back */
677 return OSSL_PARAM_set_BN(ctx->params, ctx->p2);
679 /* Normal C unsigned int passed back */
680 return OSSL_PARAM_set_uint(ctx->params,
681 (unsigned int)ctx->p1);
684 case OSSL_PARAM_UTF8_STRING:
685 return OSSL_PARAM_set_utf8_string(ctx->params, ctx->p2);
686 case OSSL_PARAM_OCTET_STRING:
687 return OSSL_PARAM_set_octet_string(ctx->params, ctx->p2,
689 case OSSL_PARAM_OCTET_PTR:
690 return OSSL_PARAM_set_octet_ptr(ctx->params, ctx->p2,
693 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
694 "[action:%d, state:%d] "
695 "unsupported OSSL_PARAM data type %d",
696 ctx->action_type, state,
697 translation->param_data_type);
702 /* Any other combination is simply pass-through */
709 cleanup_translation_ctx(enum state state,
710 const struct translation_st *translation,
711 struct translation_ctx_st *ctx)
713 if (ctx->allocated_buf != NULL)
714 OPENSSL_free(ctx->allocated_buf);
715 ctx->allocated_buf = NULL;
720 * fix_cipher_md fixes up an EVP_CIPHER / EVP_MD to its name on SET,
721 * and cipher / md name to EVP_MD on GET.
723 static const char *get_cipher_name(void *cipher)
725 return EVP_CIPHER_get0_name(cipher);
728 static const char *get_md_name(void *md)
730 return EVP_MD_get0_name(md);
733 static const void *get_cipher_by_name(OSSL_LIB_CTX *libctx, const char *name)
735 return evp_get_cipherbyname_ex(libctx, name);
738 static const void *get_md_by_name(OSSL_LIB_CTX *libctx, const char *name)
740 return evp_get_digestbyname_ex(libctx, name);
743 static int fix_cipher_md(enum state state,
744 const struct translation_st *translation,
745 struct translation_ctx_st *ctx,
746 const char *(*get_name)(void *algo),
747 const void *(*get_algo_by_name)(OSSL_LIB_CTX *libctx,
752 if ((ret = default_check(state, translation, ctx)) <= 0)
755 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
757 * |ctx->p2| contains the address to an EVP_CIPHER or EVP_MD pointer
758 * to be filled in. We need to remember it, then make |ctx->p2|
759 * point at a buffer to be filled in with the name, and |ctx->p1|
760 * with its size. default_fixup_args() will take care of the rest
763 ctx->orig_p2 = ctx->p2;
764 ctx->p2 = ctx->name_buf;
765 ctx->p1 = sizeof(ctx->name_buf);
766 } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
768 * In different parts of OpenSSL, this ctrl command is used
769 * differently. Some calls pass a NID as p1, others pass an
770 * EVP_CIPHER pointer as p2...
772 ctx->p2 = (char *)(ctx->p2 == NULL
773 ? OBJ_nid2sn(ctx->p1)
774 : get_name(ctx->p2));
775 ctx->p1 = strlen(ctx->p2);
776 } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
777 ctx->p2 = (ctx->p2 == NULL ? "" : (char *)get_name(ctx->p2));
778 ctx->p1 = strlen(ctx->p2);
781 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
784 if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
786 * Here's how we re-use |ctx->orig_p2| that was set in the
787 * PRE_CTRL_TO_PARAMS state above.
789 *(void **)ctx->orig_p2 =
790 (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
792 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
793 ctx->p2 = (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
800 static int fix_cipher(enum state state,
801 const struct translation_st *translation,
802 struct translation_ctx_st *ctx)
804 return fix_cipher_md(state, translation, ctx,
805 get_cipher_name, get_cipher_by_name);
808 static int fix_md(enum state state,
809 const struct translation_st *translation,
810 struct translation_ctx_st *ctx)
812 return fix_cipher_md(state, translation, ctx,
813 get_md_name, get_md_by_name);
816 static int fix_distid_len(enum state state,
817 const struct translation_st *translation,
818 struct translation_ctx_st *ctx)
820 int ret = default_fixup_args(state, translation, ctx);
824 if ((state == POST_CTRL_TO_PARAMS
825 || state == POST_CTRL_STR_TO_PARAMS) && ctx->action_type == GET) {
826 *(size_t *)ctx->p2 = ctx->sz;
833 struct kdf_type_map_st {
835 const char *kdf_type_str;
838 static int fix_kdf_type(enum state state,
839 const struct translation_st *translation,
840 struct translation_ctx_st *ctx,
841 const struct kdf_type_map_st *kdf_type_map)
844 * The EVP_PKEY_CTRL_DH_KDF_TYPE ctrl command is a bit special, in
845 * that it's used both for setting a value, and for getting it, all
846 * depending on the value if |p1|; if |p1| is -2, the backend is
847 * supposed to place the current kdf type in |p2|, and if not, |p1|
848 * is interpreted as the new kdf type.
852 if ((ret = default_check(state, translation, ctx)) <= 0)
855 if (state == PRE_CTRL_TO_PARAMS) {
857 * In |translations|, the initial value for |ctx->action_type| must
860 if (!ossl_assert(ctx->action_type == NONE))
863 /* The action type depends on the value of *p1 */
866 * The OSSL_PARAMS getter needs space to store a copy of the kdf
867 * type string. We use |ctx->name_buf|, which has enough space
870 * (this wouldn't be needed if the OSSL_xxx_PARAM_KDF_TYPE
871 * had the data type OSSL_PARAM_UTF8_PTR)
873 ctx->p2 = ctx->name_buf;
874 ctx->p1 = sizeof(ctx->name_buf);
875 ctx->action_type = GET;
877 ctx->action_type = SET;
881 if ((ret = default_check(state, translation, ctx)) <= 0)
884 if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
885 || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
887 /* Convert KDF type numbers to strings */
888 for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
889 if (ctx->p1 == kdf_type_map->kdf_type_num) {
890 ctx->p2 = (char *)kdf_type_map->kdf_type_str;
896 ctx->p1 = strlen(ctx->p2);
899 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
902 if ((state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)
903 || (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)) {
906 /* Convert KDF type strings to numbers */
907 for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
908 if (strcasecmp(ctx->p2, kdf_type_map->kdf_type_str) == 0) {
909 ctx->p1 = kdf_type_map->kdf_type_num;
914 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
921 /* EVP_PKEY_CTRL_DH_KDF_TYPE */
922 static int fix_dh_kdf_type(enum state state,
923 const struct translation_st *translation,
924 struct translation_ctx_st *ctx)
926 static const struct kdf_type_map_st kdf_type_map[] = {
927 { EVP_PKEY_DH_KDF_NONE, "" },
928 { EVP_PKEY_DH_KDF_X9_42, OSSL_KDF_NAME_X942KDF_ASN1 },
932 return fix_kdf_type(state, translation, ctx, kdf_type_map);
935 /* EVP_PKEY_CTRL_EC_KDF_TYPE */
936 static int fix_ec_kdf_type(enum state state,
937 const struct translation_st *translation,
938 struct translation_ctx_st *ctx)
940 static const struct kdf_type_map_st kdf_type_map[] = {
941 { EVP_PKEY_ECDH_KDF_NONE, "" },
942 { EVP_PKEY_ECDH_KDF_X9_63, OSSL_KDF_NAME_X963KDF },
946 return fix_kdf_type(state, translation, ctx, kdf_type_map);
949 /* EVP_PKEY_CTRL_DH_KDF_OID, EVP_PKEY_CTRL_GET_DH_KDF_OID, ...??? */
950 static int fix_oid(enum state state,
951 const struct translation_st *translation,
952 struct translation_ctx_st *ctx)
956 if ((ret = default_check(state, translation, ctx)) <= 0)
959 if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
960 || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
962 * We're translating from ctrl to params and setting the OID, or
963 * we're translating from params to ctrl and getting the OID.
964 * Either way, |ctx->p2| points at an ASN1_OBJECT, and needs to have
965 * that replaced with the corresponding name.
966 * default_fixup_args() will then be able to convert that to the
967 * corresponding OSSL_PARAM.
969 OBJ_obj2txt(ctx->name_buf, sizeof(ctx->name_buf), ctx->p2, 0);
970 ctx->p2 = (char *)ctx->name_buf;
971 ctx->p1 = 0; /* let default_fixup_args() figure out the length */
974 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
977 if ((state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)
978 || (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)) {
980 * We're translating from ctrl to params and setting the OID name,
981 * or we're translating from params to ctrl and getting the OID
982 * name. Either way, default_fixup_args() has placed the OID name
983 * in |ctx->p2|, all we need to do now is to replace that with the
984 * corresponding ASN1_OBJECT.
986 ctx->p2 = (ASN1_OBJECT *)OBJ_txt2obj(ctx->p2, 0);
992 /* EVP_PKEY_CTRL_DH_NID */
993 static int fix_dh_nid(enum state state,
994 const struct translation_st *translation,
995 struct translation_ctx_st *ctx)
999 if ((ret = default_check(state, translation, ctx)) <= 0)
1002 /* This is only settable */
1003 if (ctx->action_type != SET)
1006 if (state == PRE_CTRL_TO_PARAMS) {
1007 if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
1008 (ossl_ffc_uid_to_dh_named_group(ctx->p1))) == NULL) {
1009 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1015 return default_fixup_args(state, translation, ctx);
1018 /* EVP_PKEY_CTRL_DH_RFC5114 */
1019 static int fix_dh_nid5114(enum state state,
1020 const struct translation_st *translation,
1021 struct translation_ctx_st *ctx)
1025 if ((ret = default_check(state, translation, ctx)) <= 0)
1028 /* This is only settable */
1029 if (ctx->action_type != SET)
1033 case PRE_CTRL_TO_PARAMS:
1034 if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
1035 (ossl_ffc_uid_to_dh_named_group(ctx->p1))) == NULL) {
1036 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1043 case PRE_CTRL_STR_TO_PARAMS:
1044 if (ctx->p2 == NULL)
1046 if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
1047 (ossl_ffc_uid_to_dh_named_group(atoi(ctx->p2)))) == NULL) {
1048 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1059 return default_fixup_args(state, translation, ctx);
1062 /* EVP_PKEY_CTRL_DH_PARAMGEN_TYPE */
1063 static int fix_dh_paramgen_type(enum state state,
1064 const struct translation_st *translation,
1065 struct translation_ctx_st *ctx)
1069 if ((ret = default_check(state, translation, ctx)) <= 0)
1072 /* This is only settable */
1073 if (ctx->action_type != SET)
1076 if (state == PRE_CTRL_STR_TO_PARAMS) {
1077 ctx->p2 = (char *)ossl_dh_gen_type_id2name(atoi(ctx->p2));
1078 ctx->p1 = strlen(ctx->p2);
1081 return default_fixup_args(state, translation, ctx);
1084 /* EVP_PKEY_CTRL_EC_PARAM_ENC */
1085 static int fix_ec_param_enc(enum state state,
1086 const struct translation_st *translation,
1087 struct translation_ctx_st *ctx)
1091 if ((ret = default_check(state, translation, ctx)) <= 0)
1094 /* This is currently only settable */
1095 if (ctx->action_type != SET)
1098 if (state == PRE_CTRL_TO_PARAMS) {
1100 case OPENSSL_EC_EXPLICIT_CURVE:
1101 ctx->p2 = OSSL_PKEY_EC_ENCODING_EXPLICIT;
1103 case OPENSSL_EC_NAMED_CURVE:
1104 ctx->p2 = OSSL_PKEY_EC_ENCODING_GROUP;
1113 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1116 if (state == PRE_PARAMS_TO_CTRL) {
1117 if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_EXPLICIT) == 0)
1118 ctx->p1 = OPENSSL_EC_EXPLICIT_CURVE;
1119 else if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_GROUP) == 0)
1120 ctx->p1 = OPENSSL_EC_NAMED_CURVE;
1128 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1132 /* EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID */
1133 static int fix_ec_paramgen_curve_nid(enum state state,
1134 const struct translation_st *translation,
1135 struct translation_ctx_st *ctx)
1139 if ((ret = default_check(state, translation, ctx)) <= 0)
1142 /* This is currently only settable */
1143 if (ctx->action_type != SET)
1146 if (state == PRE_CTRL_TO_PARAMS) {
1147 ctx->p2 = (char *)OBJ_nid2sn(ctx->p1);
1151 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1154 if (state == PRE_PARAMS_TO_CTRL) {
1155 ctx->p1 = OBJ_sn2nid(ctx->p2);
1162 /* EVP_PKEY_CTRL_EC_ECDH_COFACTOR */
1163 static int fix_ecdh_cofactor(enum state state,
1164 const struct translation_st *translation,
1165 struct translation_ctx_st *ctx)
1168 * The EVP_PKEY_CTRL_EC_ECDH_COFACTOR ctrl command is a bit special, in
1169 * that it's used both for setting a value, and for getting it, all
1170 * depending on the value if |ctx->p1|; if |ctx->p1| is -2, the backend is
1171 * supposed to place the current cofactor mode in |ctx->p2|, and if not,
1172 * |ctx->p1| is interpreted as the new cofactor mode.
1176 if (state == PRE_CTRL_TO_PARAMS) {
1178 * The initial value for |ctx->action_type| must be zero.
1179 * evp_pkey_ctrl_to_params() takes it from the translation item.
1181 if (!ossl_assert(ctx->action_type == NONE))
1184 /* The action type depends on the value of ctx->p1 */
1186 ctx->action_type = GET;
1188 ctx->action_type = SET;
1189 } else if (state == PRE_CTRL_STR_TO_PARAMS) {
1190 ctx->action_type = SET;
1191 } else if (state == PRE_PARAMS_TO_CTRL) {
1192 /* The initial value for |ctx->action_type| must not be zero. */
1193 if (!ossl_assert(ctx->action_type != NONE))
1197 if ((ret = default_check(state, translation, ctx)) <= 0)
1200 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1201 if (ctx->p1 < -1 || ctx->p1 > 1) {
1202 /* Uses the same return value of pkey_ec_ctrl() */
1207 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1210 if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
1211 if (ctx->p1 < 0 || ctx->p1 > 1) {
1213 * The provider should return either 0 or 1, any other value is a
1218 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
1225 /* EVP_PKEY_CTRL_RSA_PADDING, EVP_PKEY_CTRL_GET_RSA_PADDING */
1226 static int fix_rsa_padding_mode(enum state state,
1227 const struct translation_st *translation,
1228 struct translation_ctx_st *ctx)
1230 static const OSSL_ITEM str_value_map[] = {
1231 { RSA_PKCS1_PADDING, "pkcs1" },
1232 { RSA_NO_PADDING, "none" },
1233 { RSA_PKCS1_OAEP_PADDING, "oaep" },
1234 { RSA_PKCS1_OAEP_PADDING, "oeap" },
1235 { RSA_X931_PADDING, "x931" },
1236 { RSA_PKCS1_PSS_PADDING, "pss" },
1237 /* Special case, will pass directly as an integer */
1238 { RSA_PKCS1_WITH_TLS_PADDING, NULL }
1242 if ((ret = default_check(state, translation, ctx)) <= 0)
1245 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1247 * EVP_PKEY_CTRL_GET_RSA_PADDING returns the padding mode in the
1248 * weirdest way for a ctrl. Instead of doing like all other ctrls
1249 * that return a simple, i.e. just have that as a return value,
1250 * this particular ctrl treats p2 as the address for the int to be
1251 * returned. We must therefore remember |ctx->p2|, then make
1252 * |ctx->p2| point at a buffer to be filled in with the name, and
1253 * |ctx->p1| with its size. default_fixup_args() will take care
1254 * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1255 * code section further down.
1257 ctx->orig_p2 = ctx->p2;
1258 ctx->p2 = ctx->name_buf;
1259 ctx->p1 = sizeof(ctx->name_buf);
1260 } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1262 * Ideally, we should use utf8 strings for the diverse padding modes.
1263 * We only came here because someone called EVP_PKEY_CTX_ctrl(),
1264 * though, and since that can reasonably be seen as legacy code
1265 * that uses the diverse RSA macros for the padding mode, and we
1266 * know that at least our providers can handle the numeric modes,
1267 * we take the cheap route for now.
1269 * The other solution would be to match |ctx->p1| against entries
1270 * in str_value_map and pass the corresponding string. However,
1271 * since we don't have a string for RSA_PKCS1_WITH_TLS_PADDING,
1272 * we have to do this same hack at least for that one.
1274 * Since the "official" data type for the RSA padding mode is utf8
1275 * string, we cannot count on default_fixup_args(). Instead, we
1276 * build the OSSL_PARAM item ourselves and return immediately.
1278 ctx->params[0] = OSSL_PARAM_construct_int(translation->param_key,
1281 } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
1285 * The EVP_PKEY_CTX_get_params() caller may have asked for a utf8
1286 * string, or may have asked for an integer of some sort. If they
1287 * ask for an integer, we respond directly. If not, we translate
1288 * the response from the ctrl function into a string.
1290 switch (ctx->params->data_type) {
1291 case OSSL_PARAM_INTEGER:
1292 return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
1293 case OSSL_PARAM_UNSIGNED_INTEGER:
1294 return OSSL_PARAM_get_uint(ctx->params, (unsigned int *)&ctx->p1);
1299 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1300 if (ctx->p1 == (int)str_value_map[i].id)
1303 if (i == OSSL_NELEM(str_value_map)) {
1304 ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1305 "[action:%d, state:%d] padding number %d",
1306 ctx->action_type, state, ctx->p1);
1310 * If we don't have a string, we can't do anything. The caller
1311 * should have asked for a number...
1313 if (str_value_map[i].ptr == NULL) {
1314 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1317 ctx->p2 = str_value_map[i].ptr;
1318 ctx->p1 = strlen(ctx->p2);
1321 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1324 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1325 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1328 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1329 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1333 if (i == OSSL_NELEM(str_value_map)) {
1334 ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1335 "[action:%d, state:%d] padding name %s",
1336 ctx->action_type, state, ctx->p1);
1338 } else if (state == POST_CTRL_TO_PARAMS) {
1339 /* EVP_PKEY_CTRL_GET_RSA_PADDING weirdness explained further up */
1340 *(int *)ctx->orig_p2 = str_value_map[i].id;
1342 ctx->p1 = str_value_map[i].id;
1350 /* EVP_PKEY_CTRL_RSA_PSS_SALTLEN, EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN */
1351 static int fix_rsa_pss_saltlen(enum state state,
1352 const struct translation_st *translation,
1353 struct translation_ctx_st *ctx)
1355 static const OSSL_ITEM str_value_map[] = {
1356 { (unsigned int)RSA_PSS_SALTLEN_DIGEST, "digest" },
1357 { (unsigned int)RSA_PSS_SALTLEN_MAX, "max" },
1358 { (unsigned int)RSA_PSS_SALTLEN_AUTO, "auto" }
1362 if ((ret = default_check(state, translation, ctx)) <= 0)
1365 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1367 * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN returns the saltlen by filling
1368 * in the int pointed at by p2. This is potentially as weird as
1369 * the way EVP_PKEY_CTRL_GET_RSA_PADDING works, except that saltlen
1370 * might be a negative value, so it wouldn't work as a legitimate
1372 * In any case, we must therefore remember |ctx->p2|, then make
1373 * |ctx->p2| point at a buffer to be filled in with the name, and
1374 * |ctx->p1| with its size. default_fixup_args() will take care
1375 * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1376 * code section further down.
1378 ctx->orig_p2 = ctx->p2;
1379 ctx->p2 = ctx->name_buf;
1380 ctx->p1 = sizeof(ctx->name_buf);
1381 } else if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1382 || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1385 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1386 if (ctx->p1 == (int)str_value_map[i].id)
1389 if (i == OSSL_NELEM(str_value_map)) {
1390 BIO_snprintf(ctx->name_buf, sizeof(ctx->name_buf), "%d", ctx->p1);
1392 /* This won't truncate but it will quiet static analysers */
1393 strncpy(ctx->name_buf, str_value_map[i].ptr, sizeof(ctx->name_buf) - 1);
1394 ctx->name_buf[sizeof(ctx->name_buf) - 1] = '\0';
1396 ctx->p2 = ctx->name_buf;
1397 ctx->p1 = strlen(ctx->p2);
1400 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1403 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1404 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1408 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1409 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1413 val = i == OSSL_NELEM(str_value_map) ? atoi(ctx->p2)
1414 : (int)str_value_map[i].id;
1415 if (state == POST_CTRL_TO_PARAMS) {
1417 * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN weirdness explained further
1420 *(int *)ctx->orig_p2 = val;
1430 /* EVP_PKEY_CTRL_HKDF_MODE */
1431 static int fix_hkdf_mode(enum state state,
1432 const struct translation_st *translation,
1433 struct translation_ctx_st *ctx)
1435 static const OSSL_ITEM str_value_map[] = {
1436 { EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND, "EXTRACT_AND_EXPAND" },
1437 { EVP_KDF_HKDF_MODE_EXTRACT_ONLY, "EXTRACT_ONLY" },
1438 { EVP_KDF_HKDF_MODE_EXPAND_ONLY, "EXPAND_ONLY" }
1442 if ((ret = default_check(state, translation, ctx)) <= 0)
1445 if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1446 || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1449 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1450 if (ctx->p1 == (int)str_value_map[i].id)
1453 if (i == OSSL_NELEM(str_value_map))
1455 ctx->p2 = str_value_map[i].ptr;
1456 ctx->p1 = strlen(ctx->p2);
1459 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1462 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1463 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1466 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1467 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1470 if (i == OSSL_NELEM(str_value_map))
1472 if (state == POST_CTRL_TO_PARAMS)
1473 ret = str_value_map[i].id;
1475 ctx->p1 = str_value_map[i].id;
1486 * These all get the data they want, then call default_fixup_args() as
1487 * a post-ctrl GET fixup. They all get NULL ctx, ctrl_cmd, ctrl_str,
1491 /* Pilfering DH, DSA and EC_KEY */
1492 static int get_payload_group_name(enum state state,
1493 const struct translation_st *translation,
1494 struct translation_ctx_st *ctx)
1496 EVP_PKEY *pkey = ctx->p2;
1499 switch (EVP_PKEY_get_base_id(pkey)) {
1500 #ifndef OPENSSL_NO_DH
1503 const DH *dh = EVP_PKEY_get0_DH(pkey);
1504 int uid = DH_get_nid(dh);
1506 if (uid != NID_undef) {
1507 const DH_NAMED_GROUP *dh_group =
1508 ossl_ffc_uid_to_dh_named_group(uid);
1510 ctx->p2 = (char *)ossl_ffc_named_group_get_name(dh_group);
1515 #ifndef OPENSSL_NO_EC
1518 const EC_GROUP *grp =
1519 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
1520 int nid = NID_undef;
1523 nid = EC_GROUP_get_curve_name(grp);
1524 if (nid != NID_undef)
1525 ctx->p2 = (char *)OSSL_EC_curve_nid2name(nid);
1530 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1535 * Quietly ignoring unknown groups matches the behaviour on the provider
1538 if (ctx->p2 == NULL)
1541 ctx->p1 = strlen(ctx->p2);
1542 return default_fixup_args(state, translation, ctx);
1545 static int get_payload_private_key(enum state state,
1546 const struct translation_st *translation,
1547 struct translation_ctx_st *ctx)
1549 EVP_PKEY *pkey = ctx->p2;
1552 if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1555 switch (EVP_PKEY_get_base_id(pkey)) {
1556 #ifndef OPENSSL_NO_DH
1559 const DH *dh = EVP_PKEY_get0_DH(pkey);
1561 ctx->p2 = (BIGNUM *)DH_get0_priv_key(dh);
1565 #ifndef OPENSSL_NO_EC
1568 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1570 ctx->p2 = (BIGNUM *)EC_KEY_get0_private_key(ec);
1575 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1579 return default_fixup_args(state, translation, ctx);
1582 static int get_payload_public_key(enum state state,
1583 const struct translation_st *translation,
1584 struct translation_ctx_st *ctx)
1586 EVP_PKEY *pkey = ctx->p2;
1587 unsigned char *buf = NULL;
1591 switch (EVP_PKEY_get_base_id(pkey)) {
1592 #ifndef OPENSSL_NO_DH
1595 switch (ctx->params->data_type) {
1596 case OSSL_PARAM_OCTET_STRING:
1597 ctx->sz = ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), &buf, 0, 1);
1600 case OSSL_PARAM_UNSIGNED_INTEGER:
1601 ctx->p2 = (void *)DH_get0_pub_key(EVP_PKEY_get0_DH(pkey));
1608 #ifndef OPENSSL_NO_DSA
1610 if (ctx->params->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1611 ctx->p2 = (void *)DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey));
1616 #ifndef OPENSSL_NO_EC
1618 if (ctx->params->data_type == OSSL_PARAM_OCTET_STRING) {
1619 const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
1620 BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
1621 const EC_GROUP *ecg = EC_KEY_get0_group(eckey);
1622 const EC_POINT *point = EC_KEY_get0_public_key(eckey);
1626 ctx->sz = EC_POINT_point2buf(ecg, point,
1627 POINT_CONVERSION_COMPRESSED,
1636 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1640 ret = default_fixup_args(state, translation, ctx);
1645 static int get_payload_bn(enum state state,
1646 const struct translation_st *translation,
1647 struct translation_ctx_st *ctx, const BIGNUM *bn)
1651 if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1653 ctx->p2 = (BIGNUM *)bn;
1655 return default_fixup_args(state, translation, ctx);
1658 static int get_dh_dsa_payload_p(enum state state,
1659 const struct translation_st *translation,
1660 struct translation_ctx_st *ctx)
1662 const BIGNUM *bn = NULL;
1663 EVP_PKEY *pkey = ctx->p2;
1665 switch (EVP_PKEY_get_base_id(pkey)) {
1666 #ifndef OPENSSL_NO_DH
1668 bn = DH_get0_p(EVP_PKEY_get0_DH(pkey));
1671 #ifndef OPENSSL_NO_DSA
1673 bn = DSA_get0_p(EVP_PKEY_get0_DSA(pkey));
1677 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1680 return get_payload_bn(state, translation, ctx, bn);
1683 static int get_dh_dsa_payload_q(enum state state,
1684 const struct translation_st *translation,
1685 struct translation_ctx_st *ctx)
1687 const BIGNUM *bn = NULL;
1689 switch (EVP_PKEY_get_base_id(ctx->p2)) {
1690 #ifndef OPENSSL_NO_DH
1692 bn = DH_get0_q(EVP_PKEY_get0_DH(ctx->p2));
1695 #ifndef OPENSSL_NO_DSA
1697 bn = DSA_get0_q(EVP_PKEY_get0_DSA(ctx->p2));
1702 return get_payload_bn(state, translation, ctx, bn);
1705 static int get_dh_dsa_payload_g(enum state state,
1706 const struct translation_st *translation,
1707 struct translation_ctx_st *ctx)
1709 const BIGNUM *bn = NULL;
1711 switch (EVP_PKEY_get_base_id(ctx->p2)) {
1712 #ifndef OPENSSL_NO_DH
1714 bn = DH_get0_g(EVP_PKEY_get0_DH(ctx->p2));
1717 #ifndef OPENSSL_NO_DSA
1719 bn = DSA_get0_g(EVP_PKEY_get0_DSA(ctx->p2));
1724 return get_payload_bn(state, translation, ctx, bn);
1727 static int get_payload_int(enum state state,
1728 const struct translation_st *translation,
1729 struct translation_ctx_st *ctx,
1732 if (ctx->params->data_type != OSSL_PARAM_INTEGER)
1737 return default_fixup_args(state, translation, ctx);
1740 static int get_ec_decoded_from_explicit_params(enum state state,
1741 const struct translation_st *translation,
1742 struct translation_ctx_st *ctx)
1745 EVP_PKEY *pkey = ctx->p2;
1747 switch (EVP_PKEY_base_id(pkey)) {
1748 #ifndef OPENSSL_NO_EC
1750 val = EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey));
1752 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
1758 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1762 return get_payload_int(state, translation, ctx, val);
1765 static int get_rsa_payload_n(enum state state,
1766 const struct translation_st *translation,
1767 struct translation_ctx_st *ctx)
1769 const BIGNUM *bn = NULL;
1771 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
1773 bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
1775 return get_payload_bn(state, translation, ctx, bn);
1778 static int get_rsa_payload_e(enum state state,
1779 const struct translation_st *translation,
1780 struct translation_ctx_st *ctx)
1782 const BIGNUM *bn = NULL;
1784 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
1786 bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
1788 return get_payload_bn(state, translation, ctx, bn);
1791 static int get_rsa_payload_d(enum state state,
1792 const struct translation_st *translation,
1793 struct translation_ctx_st *ctx)
1795 const BIGNUM *bn = NULL;
1797 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
1799 bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
1801 return get_payload_bn(state, translation, ctx, bn);
1804 static int get_rsa_payload_factor(enum state state,
1805 const struct translation_st *translation,
1806 struct translation_ctx_st *ctx,
1809 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1810 const BIGNUM *bn = NULL;
1812 switch (factornum) {
1821 size_t pnum = RSA_get_multi_prime_extra_count(r);
1822 const BIGNUM *factors[10];
1824 if (factornum - 2 < pnum
1825 && RSA_get0_multi_prime_factors(r, factors))
1826 bn = factors[factornum - 2];
1831 return get_payload_bn(state, translation, ctx, bn);
1834 static int get_rsa_payload_exponent(enum state state,
1835 const struct translation_st *translation,
1836 struct translation_ctx_st *ctx,
1839 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1840 const BIGNUM *bn = NULL;
1842 switch (exponentnum) {
1844 bn = RSA_get0_dmp1(r);
1847 bn = RSA_get0_dmq1(r);
1851 size_t pnum = RSA_get_multi_prime_extra_count(r);
1852 const BIGNUM *exps[10], *coeffs[10];
1854 if (exponentnum - 2 < pnum
1855 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1856 bn = exps[exponentnum - 2];
1861 return get_payload_bn(state, translation, ctx, bn);
1864 static int get_rsa_payload_coefficient(enum state state,
1865 const struct translation_st *translation,
1866 struct translation_ctx_st *ctx,
1867 size_t coefficientnum)
1869 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1870 const BIGNUM *bn = NULL;
1872 switch (coefficientnum) {
1874 bn = RSA_get0_iqmp(r);
1878 size_t pnum = RSA_get_multi_prime_extra_count(r);
1879 const BIGNUM *exps[10], *coeffs[10];
1881 if (coefficientnum - 1 < pnum
1882 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1883 bn = coeffs[coefficientnum - 1];
1888 return get_payload_bn(state, translation, ctx, bn);
1891 #define IMPL_GET_RSA_PAYLOAD_FACTOR(n) \
1893 get_rsa_payload_f##n(enum state state, \
1894 const struct translation_st *translation, \
1895 struct translation_ctx_st *ctx) \
1897 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
1899 return get_rsa_payload_factor(state, translation, ctx, n - 1); \
1902 #define IMPL_GET_RSA_PAYLOAD_EXPONENT(n) \
1904 get_rsa_payload_e##n(enum state state, \
1905 const struct translation_st *translation, \
1906 struct translation_ctx_st *ctx) \
1908 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
1910 return get_rsa_payload_exponent(state, translation, ctx, \
1914 #define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n) \
1916 get_rsa_payload_c##n(enum state state, \
1917 const struct translation_st *translation, \
1918 struct translation_ctx_st *ctx) \
1920 if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
1922 return get_rsa_payload_coefficient(state, translation, ctx, \
1926 IMPL_GET_RSA_PAYLOAD_FACTOR(1)
1927 IMPL_GET_RSA_PAYLOAD_FACTOR(2)
1928 IMPL_GET_RSA_PAYLOAD_FACTOR(3)
1929 IMPL_GET_RSA_PAYLOAD_FACTOR(4)
1930 IMPL_GET_RSA_PAYLOAD_FACTOR(5)
1931 IMPL_GET_RSA_PAYLOAD_FACTOR(6)
1932 IMPL_GET_RSA_PAYLOAD_FACTOR(7)
1933 IMPL_GET_RSA_PAYLOAD_FACTOR(8)
1934 IMPL_GET_RSA_PAYLOAD_FACTOR(9)
1935 IMPL_GET_RSA_PAYLOAD_FACTOR(10)
1936 IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
1937 IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
1938 IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
1939 IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
1940 IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
1941 IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
1942 IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
1943 IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
1944 IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
1945 IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
1946 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
1947 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
1948 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
1949 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
1950 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
1951 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
1952 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
1953 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
1954 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
1957 * The translation table itself
1958 * ============================
1961 static const struct translation_st evp_pkey_ctx_translations[] = {
1963 * DistID: we pass it to the backend as an octet string,
1964 * but get it back as a pointer to an octet string.
1966 * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
1967 * that has no separate counterpart in OSSL_PARAM terms, since we get
1968 * the length of the DistID automatically when getting the DistID itself.
1970 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
1971 EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
1972 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
1974 EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
1975 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
1977 EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
1978 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
1986 * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting. The
1987 * fixup function has to handle this...
1989 { NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1990 EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
1991 OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
1993 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1994 EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
1995 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1996 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1997 EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
1998 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1999 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2000 EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
2001 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2002 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2003 EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
2004 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2005 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2006 EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
2007 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2008 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2009 EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
2010 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2011 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2012 EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
2013 OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
2014 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
2015 EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
2016 OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
2018 /* DHX Keygen Parameters that are shared with DH */
2019 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2020 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
2021 OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
2022 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2023 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
2024 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2025 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2026 EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
2027 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, NULL },
2028 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2029 EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
2030 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
2032 /* DH Keygen Parameters that are shared with DHX */
2033 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2034 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
2035 OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
2036 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2037 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
2038 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2039 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2040 EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
2041 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
2042 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2043 EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
2044 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
2046 /* DH specific Keygen Parameters */
2047 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
2048 EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
2049 OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
2051 /* DHX specific Keygen Parameters */
2052 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
2053 EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
2054 OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2056 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
2057 EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
2058 OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2064 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2065 EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
2066 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2067 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2068 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
2069 OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2070 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
2071 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
2072 OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2078 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2079 EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
2080 OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
2081 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2082 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
2083 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2084 fix_ec_paramgen_curve_nid },
2086 * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
2087 * both for setting and getting. The fixup function has to handle this...
2089 { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2090 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
2091 OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
2092 fix_ecdh_cofactor },
2093 { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2094 EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
2095 OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
2096 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2097 EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
2098 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2099 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2100 EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
2101 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2102 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2103 EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
2104 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2105 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2106 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
2107 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2108 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2109 EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
2110 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2111 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2112 EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
2113 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2121 * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
2122 * and can be both with OSSL_PARAM. We standardise on strings here,
2123 * fix_rsa_padding_mode() does the work when the caller has a different
2126 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2127 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2128 EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
2129 OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2130 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2131 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2132 EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
2133 OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2135 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2136 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2137 EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
2138 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2139 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2140 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2141 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
2142 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2145 * RSA-PSS saltlen is essentially numeric, but certain values can be
2146 * expressed as keywords (strings) with ctrl_str. The corresponding
2147 * OSSL_PARAM allows both forms.
2148 * fix_rsa_pss_saltlen() takes care of the distinction.
2150 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2151 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
2152 OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2153 fix_rsa_pss_saltlen },
2154 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2155 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
2156 OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2157 fix_rsa_pss_saltlen },
2159 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2160 EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
2161 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2162 { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2163 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
2164 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2166 * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
2167 * This is accommodated by default_fixup_args() above, which mimics that
2168 * expectation for any translation item where |ctrl_str| is NULL and
2169 * |ctrl_hexstr| is non-NULL.
2171 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2172 EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
2173 OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2174 { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2175 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
2176 OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2178 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2179 EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
2180 OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2181 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2182 EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
2183 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2184 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2185 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
2186 OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
2187 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2188 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
2189 OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2190 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2191 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
2192 OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2193 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2194 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
2195 OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2201 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2202 EVP_PKEY_CTRL_SET_DIGEST_SIZE, "digestsize", NULL,
2203 OSSL_MAC_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2209 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2210 EVP_PKEY_CTRL_TLS_MD, "md", NULL,
2211 OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2212 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2213 EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
2214 OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
2215 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2216 EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
2217 OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
2223 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2224 EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
2225 OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2226 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2227 EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
2228 OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2229 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2230 EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
2231 OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2232 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2233 EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
2234 OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
2235 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2236 EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
2237 OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
2243 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2244 EVP_PKEY_CTRL_PASS, "pass", "hexpass",
2245 OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
2246 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2247 EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
2248 OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2249 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2250 EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
2251 OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2252 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2253 EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
2254 OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2255 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2256 EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
2257 OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2258 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2259 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
2260 OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2262 { SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
2263 EVP_PKEY_CTRL_CIPHER, NULL, NULL,
2264 OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
2265 { SET, -1, -1, EVP_PKEY_OP_KEYGEN,
2266 EVP_PKEY_CTRL_SET_MAC_KEY, "key", "hexkey",
2267 OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2269 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2270 EVP_PKEY_CTRL_MD, NULL, NULL,
2271 OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2272 { GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2273 EVP_PKEY_CTRL_GET_MD, NULL, NULL,
2274 OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2277 static const struct translation_st evp_pkey_translations[] = {
2279 * The following contain no ctrls, they are exclusively here to extract
2280 * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
2281 * on |fixup_args| to pass the actual data. The |fixup_args| should
2282 * expect to get the EVP_PKEY pointer through |ctx->p2|.
2286 { GET, -1, -1, -1, 0, NULL, NULL,
2287 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2288 get_payload_group_name },
2289 { GET, -1, -1, -1, 0, NULL, NULL,
2290 OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
2291 get_payload_private_key },
2292 { GET, -1, -1, -1, 0, NULL, NULL,
2293 OSSL_PKEY_PARAM_PUB_KEY,
2294 0 /* no data type, let get_payload_public_key() handle that */,
2295 get_payload_public_key },
2298 { GET, -1, -1, -1, 0, NULL, NULL,
2299 OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
2300 get_dh_dsa_payload_p },
2301 { GET, -1, -1, -1, 0, NULL, NULL,
2302 OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
2303 get_dh_dsa_payload_g },
2304 { GET, -1, -1, -1, 0, NULL, NULL,
2305 OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
2306 get_dh_dsa_payload_q },
2309 { GET, -1, -1, -1, 0, NULL, NULL,
2310 OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
2311 get_rsa_payload_n },
2312 { GET, -1, -1, -1, 0, NULL, NULL,
2313 OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
2314 get_rsa_payload_e },
2315 { GET, -1, -1, -1, 0, NULL, NULL,
2316 OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
2317 get_rsa_payload_d },
2318 { GET, -1, -1, -1, 0, NULL, NULL,
2319 OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
2320 get_rsa_payload_f1 },
2321 { GET, -1, -1, -1, 0, NULL, NULL,
2322 OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
2323 get_rsa_payload_f2 },
2324 { GET, -1, -1, -1, 0, NULL, NULL,
2325 OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
2326 get_rsa_payload_f3 },
2327 { GET, -1, -1, -1, 0, NULL, NULL,
2328 OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
2329 get_rsa_payload_f4 },
2330 { GET, -1, -1, -1, 0, NULL, NULL,
2331 OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
2332 get_rsa_payload_f5 },
2333 { GET, -1, -1, -1, 0, NULL, NULL,
2334 OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
2335 get_rsa_payload_f6 },
2336 { GET, -1, -1, -1, 0, NULL, NULL,
2337 OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
2338 get_rsa_payload_f7 },
2339 { GET, -1, -1, -1, 0, NULL, NULL,
2340 OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
2341 get_rsa_payload_f8 },
2342 { GET, -1, -1, -1, 0, NULL, NULL,
2343 OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
2344 get_rsa_payload_f9 },
2345 { GET, -1, -1, -1, 0, NULL, NULL,
2346 OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
2347 get_rsa_payload_f10 },
2348 { GET, -1, -1, -1, 0, NULL, NULL,
2349 OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2350 get_rsa_payload_e1 },
2351 { GET, -1, -1, -1, 0, NULL, NULL,
2352 OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2353 get_rsa_payload_e2 },
2354 { GET, -1, -1, -1, 0, NULL, NULL,
2355 OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2356 get_rsa_payload_e3 },
2357 { GET, -1, -1, -1, 0, NULL, NULL,
2358 OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2359 get_rsa_payload_e4 },
2360 { GET, -1, -1, -1, 0, NULL, NULL,
2361 OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2362 get_rsa_payload_e5 },
2363 { GET, -1, -1, -1, 0, NULL, NULL,
2364 OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2365 get_rsa_payload_e6 },
2366 { GET, -1, -1, -1, 0, NULL, NULL,
2367 OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2368 get_rsa_payload_e7 },
2369 { GET, -1, -1, -1, 0, NULL, NULL,
2370 OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2371 get_rsa_payload_e8 },
2372 { GET, -1, -1, -1, 0, NULL, NULL,
2373 OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2374 get_rsa_payload_e9 },
2375 { GET, -1, -1, -1, 0, NULL, NULL,
2376 OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
2377 get_rsa_payload_e10 },
2378 { GET, -1, -1, -1, 0, NULL, NULL,
2379 OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2380 get_rsa_payload_c1 },
2381 { GET, -1, -1, -1, 0, NULL, NULL,
2382 OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2383 get_rsa_payload_c2 },
2384 { GET, -1, -1, -1, 0, NULL, NULL,
2385 OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2386 get_rsa_payload_c3 },
2387 { GET, -1, -1, -1, 0, NULL, NULL,
2388 OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2389 get_rsa_payload_c4 },
2390 { GET, -1, -1, -1, 0, NULL, NULL,
2391 OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2392 get_rsa_payload_c5 },
2393 { GET, -1, -1, -1, 0, NULL, NULL,
2394 OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2395 get_rsa_payload_c6 },
2396 { GET, -1, -1, -1, 0, NULL, NULL,
2397 OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2398 get_rsa_payload_c7 },
2399 { GET, -1, -1, -1, 0, NULL, NULL,
2400 OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2401 get_rsa_payload_c8 },
2402 { GET, -1, -1, -1, 0, NULL, NULL,
2403 OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2404 get_rsa_payload_c9 },
2407 { GET, -1, -1, -1, 0, NULL, NULL,
2408 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, OSSL_PARAM_INTEGER,
2409 get_ec_decoded_from_explicit_params },
2412 static const struct translation_st *
2413 lookup_translation(struct translation_st *tmpl,
2414 const struct translation_st *translations,
2415 size_t translations_num)
2419 for (i = 0; i < translations_num; i++) {
2420 const struct translation_st *item = &translations[i];
2423 * Sanity check the translation table item.
2425 * 1. Either both keytypes are -1, or neither of them are.
2428 if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
2433 * Base search criteria: check that the optype and keytypes match,
2434 * if relevant. All callers must synthesise these bits somehow.
2436 if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
2439 * This expression is stunningly simple thanks to the sanity check
2442 if (item->keytype1 != -1
2443 && tmpl->keytype1 != item->keytype1
2444 && tmpl->keytype2 != item->keytype2)
2448 * Done with the base search criteria, now we check the criteria for
2449 * the individual types of translations:
2450 * ctrl->params, ctrl_str->params, and params->ctrl
2452 if (tmpl->ctrl_num != 0) {
2453 if (tmpl->ctrl_num != item->ctrl_num)
2455 } else if (tmpl->ctrl_str != NULL) {
2456 const char *ctrl_str = NULL;
2457 const char *ctrl_hexstr = NULL;
2460 * Search criteria that originates from a ctrl_str is only used
2461 * for setting, never for getting. Therefore, we only look at
2464 if (item->action_type != NONE
2465 && item->action_type != SET)
2468 * At least one of the ctrl cmd names must be match the ctrl
2469 * cmd name in the template.
2471 if (item->ctrl_str != NULL
2472 && strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
2473 ctrl_str = tmpl->ctrl_str;
2474 else if (item->ctrl_hexstr != NULL
2475 && strcasecmp(tmpl->ctrl_hexstr, item->ctrl_hexstr) == 0)
2476 ctrl_hexstr = tmpl->ctrl_hexstr;
2480 /* Modify the template to signal which string matched */
2481 tmpl->ctrl_str = ctrl_str;
2482 tmpl->ctrl_hexstr = ctrl_hexstr;
2483 } else if (tmpl->param_key != NULL) {
2485 * Search criteria that originates from a OSSL_PARAM setter or
2488 * Ctrls were fundamentally bidirectional, with only the ctrl
2489 * command macro name implying direction (if you're lucky).
2490 * A few ctrl commands were even taking advantage of the
2491 * bidirectional nature, making the direction depend in the
2492 * value of the numeric argument.
2494 * OSSL_PARAM functions are fundamentally different, in that
2495 * setters and getters are separated, so the data direction is
2496 * implied by the function that's used. The same OSSL_PARAM
2497 * key name can therefore be used in both directions. We must
2498 * therefore take the action type into account in this case.
2500 if ((item->action_type != NONE
2501 && tmpl->action_type != item->action_type)
2502 || (item->param_key != NULL
2503 && strcasecmp(tmpl->param_key, 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);