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