]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/ctrl_params_translate.c
Rename the field 'provctx and data' to 'algctx' inside some objects containing
[thirdparty/openssl.git] / crypto / evp / ctrl_params_translate.c
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
42 struct translation_ctx_st; /* Forwarding */
43 struct 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().
92 *
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 *
97 *
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.
116 *
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|.
142 *
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 */
148 enum 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,
152 PRE_PARAMS_TO_CTRL, POST_PARAMS_TO_CTRL, CLEANUP_PARAMS_TO_CTRL
153 };
154 enum action {
155 NONE = 0, GET = 1, SET = 2
156 };
157 typedef int fixup_args_fn(enum state state,
158 const struct translation_st *translation,
159 struct translation_ctx_st *ctx);
160 typedef int cleanup_args_fn(enum state state,
161 const struct translation_st *translation,
162 struct translation_ctx_st *ctx);
163
164 struct 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 /*
205 * Copy of the ctrl-style void* argument, if the fixup_args function
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
216 struct 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 */
291 static 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 */
386 static 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)
422 && ctx->pctx->op.sig.algctx == NULL)
423 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx->pctx)
424 && ctx->pctx->op.kex.algctx == NULL)
425 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx->pctx)
426 && ctx->pctx->op.ciph.algctx == NULL)
427 || (EVP_PKEY_CTX_IS_KEM_OP(ctx->pctx)
428 && ctx->pctx->op.encap.algctx == NULL)
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 }
468 if (!BN_bn2nativepad(ctx->p2,
469 ctx->allocated_buf, ctx->buflen)) {
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 */
657
658 /* When getting, we populate |*params| from |p1| and |p2| */
659 switch (translation->param_data_type) {
660 case OSSL_PARAM_INTEGER:
661 return OSSL_PARAM_set_int(ctx->params, ctx->p1);
662 case OSSL_PARAM_UNSIGNED_INTEGER:
663 if (ctx->p2 != NULL) {
664 /* BIGNUM passed back */
665 return OSSL_PARAM_set_BN(ctx->params, ctx->p2);
666 } else {
667 /* Normal C unsigned int passed back */
668 return OSSL_PARAM_set_uint(ctx->params,
669 (unsigned int)ctx->p1);
670 }
671 return 0;
672 case OSSL_PARAM_UTF8_STRING:
673 return OSSL_PARAM_set_utf8_string(ctx->params, ctx->p2);
674 case OSSL_PARAM_OCTET_STRING:
675 return OSSL_PARAM_set_octet_string(ctx->params, ctx->p2,
676 (size_t)ctx->p1);
677 case OSSL_PARAM_OCTET_PTR:
678 return OSSL_PARAM_set_octet_ptr(ctx->params, ctx->p2,
679 (size_t)ctx->p1);
680 default:
681 ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
682 "[action:%d, state:%d] "
683 "unsupported OSSL_PARAM data type %d",
684 ctx->action_type, state,
685 translation->param_data_type);
686 return 0;
687 }
688 }
689 }
690 /* Any other combination is simply pass-through */
691 break;
692 }
693 return ret;
694 }
695
696 static int
697 cleanup_translation_ctx(enum state state,
698 const struct translation_st *translation,
699 struct translation_ctx_st *ctx)
700 {
701 if (ctx->allocated_buf != NULL)
702 OPENSSL_free(ctx->allocated_buf);
703 ctx->allocated_buf = NULL;
704 return 1;
705 }
706
707 /*
708 * fix_cipher_md fixes up an EVP_CIPHER / EVP_MD to its name on SET,
709 * and cipher / md name to EVP_MD on GET.
710 */
711 static const char *get_cipher_name(void *cipher)
712 {
713 return EVP_CIPHER_name(cipher);
714 }
715
716 static const char *get_md_name(void *md)
717 {
718 return EVP_MD_name(md);
719 }
720
721 static const void *get_cipher_by_name(OSSL_LIB_CTX *libctx, const char *name)
722 {
723 return evp_get_cipherbyname_ex(libctx, name);
724 }
725
726 static const void *get_md_by_name(OSSL_LIB_CTX *libctx, const char *name)
727 {
728 return evp_get_digestbyname_ex(libctx, name);
729 }
730
731 static int fix_cipher_md(enum state state,
732 const struct translation_st *translation,
733 struct translation_ctx_st *ctx,
734 const char *(*get_name)(void *algo),
735 const void *(*get_algo_by_name)(OSSL_LIB_CTX *libctx,
736 const char *name))
737 {
738 int ret = 1;
739
740 if ((ret = default_check(state, translation, ctx)) <= 0)
741 return ret;
742
743 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
744 /*
745 * |ctx->p2| contains the address to an EVP_CIPHER or EVP_MD pointer
746 * to be filled in. We need to remember it, then make |ctx->p2|
747 * point at a buffer to be filled in with the name, and |ctx->p1|
748 * with its size. default_fixup_args() will take care of the rest
749 * for us.
750 */
751 ctx->orig_p2 = ctx->p2;
752 ctx->p2 = ctx->name_buf;
753 ctx->p1 = sizeof(ctx->name_buf);
754 } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
755 /*
756 * In different parts of OpenSSL, this ctrl command is used
757 * differently. Some calls pass a NID as p1, others pass an
758 * EVP_CIPHER pointer as p2...
759 */
760 ctx->p2 = (char *)(ctx->p2 == NULL
761 ? OBJ_nid2sn(ctx->p1)
762 : get_name(ctx->p2));
763 ctx->p1 = strlen(ctx->p2);
764 } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
765 ctx->p2 = (ctx->p2 == NULL ? "" : (char *)get_name(ctx->p2));
766 ctx->p1 = strlen(ctx->p2);
767 }
768
769 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
770 return ret;
771
772 if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
773 /*
774 * Here's how we re-use |ctx->orig_p2| that was set in the
775 * PRE_CTRL_TO_PARAMS state above.
776 */
777 *(void **)ctx->orig_p2 =
778 (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
779 ctx->p1 = 1;
780 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
781 ctx->p2 = (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
782 ctx->p1 = 0;
783 }
784
785 return ret;
786 }
787
788 static int fix_cipher(enum state state,
789 const struct translation_st *translation,
790 struct translation_ctx_st *ctx)
791 {
792 return fix_cipher_md(state, translation, ctx,
793 get_cipher_name, get_cipher_by_name);
794 }
795
796 static int fix_md(enum state state,
797 const struct translation_st *translation,
798 struct translation_ctx_st *ctx)
799 {
800 return fix_cipher_md(state, translation, ctx,
801 get_md_name, get_md_by_name);
802 }
803
804 static int fix_distid_len(enum state state,
805 const struct translation_st *translation,
806 struct translation_ctx_st *ctx)
807 {
808 int ret = default_fixup_args(state, translation, ctx);
809
810 if (ret > 0) {
811 ret = 0;
812 if ((state == POST_CTRL_TO_PARAMS
813 || state == POST_CTRL_STR_TO_PARAMS) && ctx->action_type == GET) {
814 *(size_t *)ctx->p2 = ctx->sz;
815 ret = 1;
816 }
817 }
818 return ret;
819 }
820
821 struct kdf_type_map_st {
822 int kdf_type_num;
823 const char *kdf_type_str;
824 };
825
826 static int fix_kdf_type(enum state state,
827 const struct translation_st *translation,
828 struct translation_ctx_st *ctx,
829 const struct kdf_type_map_st *kdf_type_map)
830 {
831 /*
832 * The EVP_PKEY_CTRL_DH_KDF_TYPE ctrl command is a bit special, in
833 * that it's used both for setting a value, and for getting it, all
834 * depending on the value if |p1|; if |p1| is -2, the backend is
835 * supposed to place the current kdf type in |p2|, and if not, |p1|
836 * is interpreted as the new kdf type.
837 */
838 int ret = 0;
839
840 if ((ret = default_check(state, translation, ctx)) <= 0)
841 return ret;
842
843 if (state == PRE_CTRL_TO_PARAMS) {
844 /*
845 * In |translations|, the initial value for |ctx->action_type| must
846 * be NONE.
847 */
848 if (!ossl_assert(ctx->action_type == NONE))
849 return 0;
850
851 /* The action type depends on the value of *p1 */
852 if (ctx->p1 == -2) {
853 /*
854 * The OSSL_PARAMS getter needs space to store a copy of the kdf
855 * type string. We use |ctx->name_buf|, which has enough space
856 * allocated.
857 *
858 * (this wouldn't be needed if the OSSL_xxx_PARAM_KDF_TYPE
859 * had the data type OSSL_PARAM_UTF8_PTR)
860 */
861 ctx->p2 = ctx->name_buf;
862 ctx->p1 = sizeof(ctx->name_buf);
863 ctx->action_type = GET;
864 } else {
865 ctx->action_type = SET;
866 }
867 }
868
869 if ((ret = default_check(state, translation, ctx)) <= 0)
870 return ret;
871
872 if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
873 || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
874 ret = -2;
875 /* Convert KDF type numbers to strings */
876 for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
877 if (ctx->p1 == kdf_type_map->kdf_type_num) {
878 ctx->p2 = (char *)kdf_type_map->kdf_type_str;
879 ret = 1;
880 break;
881 }
882 if (ret <= 0)
883 goto end;
884 ctx->p1 = strlen(ctx->p2);
885 }
886
887 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
888 return ret;
889
890 if ((state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)
891 || (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)) {
892 ctx->p1 = ret = -1;
893
894 /* Convert KDF type strings to numbers */
895 for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
896 if (strcasecmp(ctx->p2, kdf_type_map->kdf_type_str) == 0) {
897 ctx->p1 = kdf_type_map->kdf_type_num;
898 ret = 1;
899 break;
900 }
901 ctx->p2 = NULL;
902 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
903 ctx->p1 = -2;
904 }
905 end:
906 return ret;
907 }
908
909 /* EVP_PKEY_CTRL_DH_KDF_TYPE */
910 static int fix_dh_kdf_type(enum state state,
911 const struct translation_st *translation,
912 struct translation_ctx_st *ctx)
913 {
914 static const struct kdf_type_map_st kdf_type_map[] = {
915 { EVP_PKEY_DH_KDF_NONE, "" },
916 { EVP_PKEY_DH_KDF_X9_42, OSSL_KDF_NAME_X942KDF_ASN1 },
917 { 0, NULL }
918 };
919
920 return fix_kdf_type(state, translation, ctx, kdf_type_map);
921 }
922
923 /* EVP_PKEY_CTRL_EC_KDF_TYPE */
924 static int fix_ec_kdf_type(enum state state,
925 const struct translation_st *translation,
926 struct translation_ctx_st *ctx)
927 {
928 static const struct kdf_type_map_st kdf_type_map[] = {
929 { EVP_PKEY_ECDH_KDF_NONE, "" },
930 { EVP_PKEY_ECDH_KDF_X9_63, OSSL_KDF_NAME_X963KDF },
931 { 0, NULL }
932 };
933
934 return fix_kdf_type(state, translation, ctx, kdf_type_map);
935 }
936
937 /* EVP_PKEY_CTRL_DH_KDF_OID, EVP_PKEY_CTRL_GET_DH_KDF_OID, ...??? */
938 static int fix_oid(enum state state,
939 const struct translation_st *translation,
940 struct translation_ctx_st *ctx)
941 {
942 int ret;
943
944 if ((ret = default_check(state, translation, ctx)) <= 0)
945 return ret;
946
947 if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
948 || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
949 /*
950 * We're translating from ctrl to params and setting the OID, or
951 * we're translating from params to ctrl and getting the OID.
952 * Either way, |ctx->p2| points at an ASN1_OBJECT, and needs to have
953 * that replaced with the corresponding name.
954 * default_fixup_args() will then be able to convert that to the
955 * corresponding OSSL_PARAM.
956 */
957 OBJ_obj2txt(ctx->name_buf, sizeof(ctx->name_buf), ctx->p2, 0);
958 ctx->p2 = (char *)ctx->name_buf;
959 ctx->p1 = 0; /* let default_fixup_args() figure out the length */
960 }
961
962 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
963 return ret;
964
965 if ((state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)
966 || (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)) {
967 /*
968 * We're translating from ctrl to params and setting the OID name,
969 * or we're translating from params to ctrl and getting the OID
970 * name. Either way, default_fixup_args() has placed the OID name
971 * in |ctx->p2|, all we need to do now is to replace that with the
972 * corresponding ASN1_OBJECT.
973 */
974 ctx->p2 = (ASN1_OBJECT *)OBJ_txt2obj(ctx->p2, 0);
975 }
976
977 return ret;
978 }
979
980 /* EVP_PKEY_CTRL_DH_NID */
981 static int fix_dh_nid(enum state state,
982 const struct translation_st *translation,
983 struct translation_ctx_st *ctx)
984 {
985 int ret;
986
987 if ((ret = default_check(state, translation, ctx)) <= 0)
988 return ret;
989
990 /* This is only settable */
991 if (ctx->action_type != SET)
992 return 0;
993
994 if (state == PRE_CTRL_TO_PARAMS) {
995 ctx->p2 = (char *)ossl_ffc_named_group_get_name
996 (ossl_ffc_uid_to_dh_named_group(ctx->p1));
997 ctx->p1 = 0;
998 }
999
1000 return default_fixup_args(state, translation, ctx);
1001 }
1002
1003 /* EVP_PKEY_CTRL_DH_RFC5114 */
1004 static int fix_dh_nid5114(enum state state,
1005 const struct translation_st *translation,
1006 struct translation_ctx_st *ctx)
1007 {
1008 int ret;
1009
1010 if ((ret = default_check(state, translation, ctx)) <= 0)
1011 return ret;
1012
1013 /* This is only settable */
1014 if (ctx->action_type != SET)
1015 return 0;
1016
1017 if (state == PRE_CTRL_STR_TO_PARAMS) {
1018 ctx->p2 = (char *)ossl_ffc_named_group_get_name
1019 (ossl_ffc_uid_to_dh_named_group(atoi(ctx->p2)));
1020 ctx->p1 = 0;
1021 }
1022
1023 return default_fixup_args(state, translation, ctx);
1024 }
1025
1026 /* EVP_PKEY_CTRL_DH_PARAMGEN_TYPE */
1027 static int fix_dh_paramgen_type(enum state state,
1028 const struct translation_st *translation,
1029 struct translation_ctx_st *ctx)
1030 {
1031 int ret;
1032
1033 if ((ret = default_check(state, translation, ctx)) <= 0)
1034 return ret;
1035
1036 /* This is only settable */
1037 if (ctx->action_type != SET)
1038 return 0;
1039
1040 if (state == PRE_CTRL_STR_TO_PARAMS) {
1041 ctx->p2 = (char *)ossl_dh_gen_type_id2name(atoi(ctx->p2));
1042 ctx->p1 = strlen(ctx->p2);
1043 }
1044
1045 return default_fixup_args(state, translation, ctx);
1046 }
1047
1048 /* EVP_PKEY_CTRL_EC_PARAM_ENC */
1049 static int fix_ec_param_enc(enum state state,
1050 const struct translation_st *translation,
1051 struct translation_ctx_st *ctx)
1052 {
1053 int ret;
1054
1055 if ((ret = default_check(state, translation, ctx)) <= 0)
1056 return ret;
1057
1058 /* This is currently only settable */
1059 if (ctx->action_type != SET)
1060 return 0;
1061
1062 if (state == PRE_CTRL_TO_PARAMS) {
1063 switch (ctx->p1) {
1064 case OPENSSL_EC_EXPLICIT_CURVE:
1065 ctx->p2 = OSSL_PKEY_EC_ENCODING_EXPLICIT;
1066 break;
1067 case OPENSSL_EC_NAMED_CURVE:
1068 ctx->p2 = OSSL_PKEY_EC_ENCODING_GROUP;
1069 break;
1070 default:
1071 ret = -2;
1072 goto end;
1073 }
1074 ctx->p1 = 0;
1075 }
1076
1077 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1078 return ret;
1079
1080 if (state == PRE_PARAMS_TO_CTRL) {
1081 if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_EXPLICIT) == 0)
1082 ctx->p1 = OPENSSL_EC_EXPLICIT_CURVE;
1083 else if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_GROUP) == 0)
1084 ctx->p1 = OPENSSL_EC_NAMED_CURVE;
1085 else
1086 ctx->p1 = ret = -2;
1087 ctx->p2 = NULL;
1088 }
1089
1090 end:
1091 if (ret == -2)
1092 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1093 return ret;
1094 }
1095
1096 /* EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID */
1097 static int fix_ec_paramgen_curve_nid(enum state state,
1098 const struct translation_st *translation,
1099 struct translation_ctx_st *ctx)
1100 {
1101 int ret;
1102
1103 if ((ret = default_check(state, translation, ctx)) <= 0)
1104 return ret;
1105
1106 /* This is currently only settable */
1107 if (ctx->action_type != SET)
1108 return 0;
1109
1110 if (state == PRE_CTRL_TO_PARAMS) {
1111 ctx->p2 = (char *)OBJ_nid2sn(ctx->p1);
1112 ctx->p1 = 0;
1113 }
1114
1115 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1116 return ret;
1117
1118 if (state == PRE_PARAMS_TO_CTRL) {
1119 ctx->p1 = OBJ_sn2nid(ctx->p2);
1120 ctx->p2 = NULL;
1121 }
1122
1123 return ret;
1124 }
1125
1126 /* EVP_PKEY_CTRL_EC_ECDH_COFACTOR */
1127 static int fix_ecdh_cofactor(enum state state,
1128 const struct translation_st *translation,
1129 struct translation_ctx_st *ctx)
1130 {
1131 /*
1132 * The EVP_PKEY_CTRL_EC_ECDH_COFACTOR ctrl command is a bit special, in
1133 * that it's used both for setting a value, and for getting it, all
1134 * depending on the value if |ctx->p1|; if |ctx->p1| is -2, the backend is
1135 * supposed to place the current cofactor mode in |ctx->p2|, and if not,
1136 * |ctx->p1| is interpreted as the new cofactor mode.
1137 */
1138 int ret = 0;
1139
1140 if (state == PRE_CTRL_TO_PARAMS) {
1141 /*
1142 * The initial value for |ctx->action_type| must be zero.
1143 * evp_pkey_ctrl_to_params() takes it from the translation item.
1144 */
1145 if (!ossl_assert(ctx->action_type == NONE))
1146 return 0;
1147
1148 /* The action type depends on the value of ctx->p1 */
1149 if (ctx->p1 == -2)
1150 ctx->action_type = GET;
1151 else
1152 ctx->action_type = SET;
1153 } else if (state == PRE_CTRL_STR_TO_PARAMS) {
1154 ctx->action_type = SET;
1155 } else if (state == PRE_PARAMS_TO_CTRL) {
1156 /* The initial value for |ctx->action_type| must not be zero. */
1157 if (!ossl_assert(ctx->action_type != NONE))
1158 return 0;
1159 }
1160
1161 if ((ret = default_check(state, translation, ctx)) <= 0)
1162 return ret;
1163
1164 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1165 if (ctx->p1 < -1 || ctx->p1 > 1) {
1166 /* Uses the same return value of pkey_ec_ctrl() */
1167 return -2;
1168 }
1169 }
1170
1171 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1172 return ret;
1173
1174 if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
1175 if (ctx->p1 < 0 || ctx->p1 > 1) {
1176 /*
1177 * The provider should return either 0 or 1, any other value is a
1178 * provider error.
1179 */
1180 ctx->p1 = ret = -1;
1181 }
1182 } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
1183 ctx->p1 = -2;
1184 }
1185
1186 return ret;
1187 }
1188
1189 /* EVP_PKEY_CTRL_RSA_PADDING, EVP_PKEY_CTRL_GET_RSA_PADDING */
1190 static int fix_rsa_padding_mode(enum state state,
1191 const struct translation_st *translation,
1192 struct translation_ctx_st *ctx)
1193 {
1194 static const OSSL_ITEM str_value_map[] = {
1195 { RSA_PKCS1_PADDING, "pkcs1" },
1196 { RSA_NO_PADDING, "none" },
1197 { RSA_PKCS1_OAEP_PADDING, "oaep" },
1198 { RSA_PKCS1_OAEP_PADDING, "oeap" },
1199 { RSA_X931_PADDING, "x931" },
1200 { RSA_PKCS1_PSS_PADDING, "pss" },
1201 /* Special case, will pass directly as an integer */
1202 { RSA_PKCS1_WITH_TLS_PADDING, NULL }
1203 };
1204 int ret;
1205
1206 if ((ret = default_check(state, translation, ctx)) <= 0)
1207 return ret;
1208
1209 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1210 /*
1211 * EVP_PKEY_CTRL_GET_RSA_PADDING returns the padding mode in the
1212 * weirdest way for a ctrl. Instead of doing like all other ctrls
1213 * that return a simple, i.e. just have that as a return value,
1214 * this particular ctrl treats p2 as the address for the int to be
1215 * returned. We must therefore remember |ctx->p2|, then make
1216 * |ctx->p2| point at a buffer to be filled in with the name, and
1217 * |ctx->p1| with its size. default_fixup_args() will take care
1218 * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1219 * code section further down.
1220 */
1221 ctx->orig_p2 = ctx->p2;
1222 ctx->p2 = ctx->name_buf;
1223 ctx->p1 = sizeof(ctx->name_buf);
1224 } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
1225 /*
1226 * Ideally, we should use utf8 strings for the diverse padding modes.
1227 * We only came here because someone called EVP_PKEY_CTX_ctrl(),
1228 * though, and since that can reasonably be seen as legacy code
1229 * that uses the diverse RSA macros for the padding mode, and we
1230 * know that at least our providers can handle the numeric modes,
1231 * we take the cheap route for now.
1232 *
1233 * The other solution would be to match |ctx->p1| against entries
1234 * in str_value_map and pass the corresponding string. However,
1235 * since we don't have a string for RSA_PKCS1_WITH_TLS_PADDING,
1236 * we have to do this same hack at least for that one.
1237 *
1238 * Since the "official" data type for the RSA padding mode is utf8
1239 * string, we cannot count on default_fixup_args(). Instead, we
1240 * build the OSSL_PARAM item ourselves and return immediately.
1241 */
1242 ctx->params[0] = OSSL_PARAM_construct_int(translation->param_key,
1243 &ctx->p1);
1244 return 1;
1245 } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
1246 size_t i;
1247
1248 /*
1249 * The EVP_PKEY_CTX_get_params() caller may have asked for a utf8
1250 * string, or may have asked for an integer of some sort. If they
1251 * ask for an integer, we respond directly. If not, we translate
1252 * the response from the ctrl function into a string.
1253 */
1254 switch (ctx->params->data_type) {
1255 case OSSL_PARAM_INTEGER:
1256 return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
1257 case OSSL_PARAM_UNSIGNED_INTEGER:
1258 return OSSL_PARAM_get_uint(ctx->params, (unsigned int *)&ctx->p1);
1259 default:
1260 break;
1261 }
1262
1263 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1264 if (ctx->p1 == (int)str_value_map[i].id)
1265 break;
1266 }
1267 if (i == OSSL_NELEM(str_value_map)) {
1268 ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1269 "[action:%d, state:%d] padding number %d",
1270 ctx->action_type, state, ctx->p1);
1271 return -2;
1272 }
1273 /*
1274 * If we don't have a string, we can't do anything. The caller
1275 * should have asked for a number...
1276 */
1277 if (str_value_map[i].ptr == NULL) {
1278 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1279 return -2;
1280 }
1281 ctx->p2 = str_value_map[i].ptr;
1282 ctx->p1 = strlen(ctx->p2);
1283 }
1284
1285 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1286 return ret;
1287
1288 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1289 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1290 size_t i;
1291
1292 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1293 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1294 break;
1295 }
1296
1297 if (i == OSSL_NELEM(str_value_map)) {
1298 ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
1299 "[action:%d, state:%d] padding name %s",
1300 ctx->action_type, state, ctx->p1);
1301 ctx->p1 = ret = -2;
1302 } else if (state == POST_CTRL_TO_PARAMS) {
1303 /* EVP_PKEY_CTRL_GET_RSA_PADDING weirdness explained further up */
1304 *(int *)ctx->orig_p2 = str_value_map[i].id;
1305 } else {
1306 ctx->p1 = str_value_map[i].id;
1307 }
1308 ctx->p2 = NULL;
1309 }
1310
1311 return ret;
1312 }
1313
1314 /* EVP_PKEY_CTRL_RSA_PSS_SALTLEN, EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN */
1315 static int fix_rsa_pss_saltlen(enum state state,
1316 const struct translation_st *translation,
1317 struct translation_ctx_st *ctx)
1318 {
1319 static const OSSL_ITEM str_value_map[] = {
1320 { (unsigned int)RSA_PSS_SALTLEN_DIGEST, "digest" },
1321 { (unsigned int)RSA_PSS_SALTLEN_MAX, "max" },
1322 { (unsigned int)RSA_PSS_SALTLEN_AUTO, "auto" }
1323 };
1324 int ret;
1325
1326 if ((ret = default_check(state, translation, ctx)) <= 0)
1327 return ret;
1328
1329 if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
1330 /*
1331 * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN returns the saltlen by filling
1332 * in the int pointed at by p2. This is potentially as weird as
1333 * the way EVP_PKEY_CTRL_GET_RSA_PADDING works, except that saltlen
1334 * might be a negative value, so it wouldn't work as a legitimate
1335 * return value.
1336 * In any case, we must therefore remember |ctx->p2|, then make
1337 * |ctx->p2| point at a buffer to be filled in with the name, and
1338 * |ctx->p1| with its size. default_fixup_args() will take care
1339 * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
1340 * code section further down.
1341 */
1342 ctx->orig_p2 = ctx->p2;
1343 ctx->p2 = ctx->name_buf;
1344 ctx->p1 = sizeof(ctx->name_buf);
1345 } else if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1346 || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1347 size_t i;
1348
1349 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1350 if (ctx->p1 == (int)str_value_map[i].id)
1351 break;
1352 }
1353 if (i == OSSL_NELEM(str_value_map)) {
1354 BIO_snprintf(ctx->name_buf, sizeof(ctx->name_buf), "%d", ctx->p1);
1355 } else {
1356 strcpy(ctx->name_buf, str_value_map[i].ptr);
1357 }
1358 ctx->p2 = ctx->name_buf;
1359 ctx->p1 = strlen(ctx->p2);
1360 }
1361
1362 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1363 return ret;
1364
1365 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1366 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1367 size_t i;
1368
1369 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1370 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1371 break;
1372 }
1373 if (i == OSSL_NELEM(str_value_map)) {
1374 ctx->p1 = atoi(ctx->p2);
1375 } else if (state == POST_CTRL_TO_PARAMS) {
1376 /*
1377 * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN weirdness explained further
1378 * up
1379 */
1380 *(int *)ctx->orig_p2 = str_value_map[i].id;
1381 } else {
1382 ctx->p1 = (int)str_value_map[i].id;
1383 }
1384 ctx->p2 = NULL;
1385 }
1386
1387 return ret;
1388 }
1389
1390 /* EVP_PKEY_CTRL_HKDF_MODE */
1391 static int fix_hkdf_mode(enum state state,
1392 const struct translation_st *translation,
1393 struct translation_ctx_st *ctx)
1394 {
1395 static const OSSL_ITEM str_value_map[] = {
1396 { EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND, "EXTRACT_AND_EXPAND" },
1397 { EVP_KDF_HKDF_MODE_EXTRACT_ONLY, "EXTRACT_ONLY" },
1398 { EVP_KDF_HKDF_MODE_EXPAND_ONLY, "EXPAND_ONLY" }
1399 };
1400 int ret;
1401
1402 if ((ret = default_check(state, translation, ctx)) <= 0)
1403 return ret;
1404
1405 if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
1406 || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
1407 size_t i;
1408
1409 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1410 if (ctx->p1 == (int)str_value_map[i].id)
1411 break;
1412 }
1413 if (i == OSSL_NELEM(str_value_map))
1414 return 0;
1415 ctx->p2 = str_value_map[i].ptr;
1416 ctx->p1 = strlen(ctx->p2);
1417 }
1418
1419 if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
1420 return ret;
1421
1422 if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
1423 || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
1424 size_t i;
1425
1426 for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
1427 if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
1428 break;
1429 }
1430 if (i == OSSL_NELEM(str_value_map))
1431 return 0;
1432 if (state == POST_CTRL_TO_PARAMS)
1433 ret = str_value_map[i].id;
1434 else
1435 ctx->p1 = str_value_map[i].id;
1436 ctx->p2 = NULL;
1437 }
1438
1439 return 1;
1440 }
1441
1442 /*-
1443 * Payload getters
1444 * ===============
1445 *
1446 * These all get the data they want, then call default_fixup_args() as
1447 * a post-ctrl GET fixup. They all get NULL ctx, ctrl_cmd, ctrl_str,
1448 * p1, sz
1449 */
1450
1451 /* Pilfering DH, DSA and EC_KEY */
1452 static int get_payload_group_name(enum state state,
1453 const struct translation_st *translation,
1454 struct translation_ctx_st *ctx)
1455 {
1456 EVP_PKEY *pkey = ctx->p2;
1457
1458 ctx->p2 = NULL;
1459 switch (EVP_PKEY_base_id(pkey)) {
1460 #ifndef OPENSSL_NO_DH
1461 case EVP_PKEY_DH:
1462 {
1463 const DH *dh = EVP_PKEY_get0_DH(pkey);
1464 int uid = DH_get_nid(dh);
1465
1466 if (uid != NID_undef) {
1467 const DH_NAMED_GROUP *dh_group =
1468 ossl_ffc_uid_to_dh_named_group(uid);
1469
1470 ctx->p2 = (char *)ossl_ffc_named_group_get_name(dh_group);
1471 }
1472 }
1473 break;
1474 #endif
1475 #ifndef OPENSSL_NO_EC
1476 case EVP_PKEY_EC:
1477 {
1478 const EC_GROUP *grp =
1479 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
1480 int nid = NID_undef;
1481
1482 if (grp != NULL)
1483 nid = EC_GROUP_get_curve_name(grp);
1484 if (nid != NID_undef)
1485 ctx->p2 = (char *)OSSL_EC_curve_nid2name(nid);
1486 }
1487 break;
1488 #endif
1489 default:
1490 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1491 return 0;
1492 }
1493
1494 /*
1495 * Quietly ignoring unknown groups matches the behaviour on the provider
1496 * side.
1497 */
1498 if (ctx->p2 == NULL)
1499 return 1;
1500
1501 ctx->p1 = strlen(ctx->p2);
1502 return default_fixup_args(state, translation, ctx);
1503 }
1504
1505 static int get_payload_private_key(enum state state,
1506 const struct translation_st *translation,
1507 struct translation_ctx_st *ctx)
1508 {
1509 EVP_PKEY *pkey = ctx->p2;
1510
1511 ctx->p2 = NULL;
1512 if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1513 return 0;
1514
1515 switch (EVP_PKEY_base_id(pkey)) {
1516 #ifndef OPENSSL_NO_DH
1517 case EVP_PKEY_DH:
1518 {
1519 const DH *dh = EVP_PKEY_get0_DH(pkey);
1520
1521 ctx->p2 = (BIGNUM *)DH_get0_priv_key(dh);
1522 }
1523 break;
1524 #endif
1525 #ifndef OPENSSL_NO_EC
1526 case EVP_PKEY_EC:
1527 {
1528 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1529
1530 ctx->p2 = (BIGNUM *)EC_KEY_get0_private_key(ec);
1531 }
1532 break;
1533 #endif
1534 default:
1535 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1536 return 0;
1537 }
1538
1539 return default_fixup_args(state, translation, ctx);
1540 }
1541
1542 static int get_payload_public_key(enum state state,
1543 const struct translation_st *translation,
1544 struct translation_ctx_st *ctx)
1545 {
1546 EVP_PKEY *pkey = ctx->p2;
1547 unsigned char *buf = NULL;
1548 int ret;
1549
1550 ctx->p2 = NULL;
1551 switch (EVP_PKEY_base_id(pkey)) {
1552 #ifndef OPENSSL_NO_DH
1553 case EVP_PKEY_DH:
1554 switch (ctx->params->data_type) {
1555 case OSSL_PARAM_OCTET_STRING:
1556 ctx->sz = ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), &buf, 0, 1);
1557 ctx->p2 = buf;
1558 break;
1559 case OSSL_PARAM_UNSIGNED_INTEGER:
1560 ctx->p2 = (void *)DH_get0_pub_key(EVP_PKEY_get0_DH(pkey));
1561 break;
1562 default:
1563 return 0;
1564 }
1565 break;
1566 #endif
1567 #ifndef OPENSSL_NO_DSA
1568 case EVP_PKEY_DSA:
1569 if (ctx->params->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1570 ctx->p2 = (void *)DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey));
1571 break;
1572 }
1573 return 0;
1574 #endif
1575 #ifndef OPENSSL_NO_EC
1576 case EVP_PKEY_EC:
1577 if (ctx->params->data_type == OSSL_PARAM_OCTET_STRING) {
1578 const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
1579 BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
1580 const EC_GROUP *ecg = EC_KEY_get0_group(eckey);
1581 const EC_POINT *point = EC_KEY_get0_public_key(eckey);
1582
1583 ctx->sz = EC_POINT_point2buf(ecg, point,
1584 POINT_CONVERSION_COMPRESSED,
1585 &buf, bnctx);
1586 ctx->p2 = buf;
1587 break;
1588 }
1589 return 0;
1590 #endif
1591 default:
1592 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1593 return 0;
1594 }
1595
1596 ret = default_fixup_args(state, translation, ctx);
1597 OPENSSL_free(buf);
1598 return ret;
1599 }
1600
1601 static int get_payload_bn(enum state state,
1602 const struct translation_st *translation,
1603 struct translation_ctx_st *ctx, const BIGNUM *bn)
1604 {
1605 if (bn == NULL)
1606 return 0;
1607 if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
1608 return 0;
1609 ctx->p2 = (BIGNUM *)bn;
1610
1611 return default_fixup_args(state, translation, ctx);
1612 }
1613
1614 static int get_dh_dsa_payload_p(enum state state,
1615 const struct translation_st *translation,
1616 struct translation_ctx_st *ctx)
1617 {
1618 const BIGNUM *bn = NULL;
1619 EVP_PKEY *pkey = ctx->p2;
1620
1621 switch (EVP_PKEY_base_id(pkey)) {
1622 #ifndef OPENSSL_NO_DH
1623 case EVP_PKEY_DH:
1624 bn = DH_get0_p(EVP_PKEY_get0_DH(pkey));
1625 break;
1626 #endif
1627 #ifndef OPENSSL_NO_DSA
1628 case EVP_PKEY_DSA:
1629 bn = DSA_get0_p(EVP_PKEY_get0_DSA(pkey));
1630 break;
1631 #endif
1632 default:
1633 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1634 }
1635
1636 return get_payload_bn(state, translation, ctx, bn);
1637 }
1638
1639 static int get_dh_dsa_payload_q(enum state state,
1640 const struct translation_st *translation,
1641 struct translation_ctx_st *ctx)
1642 {
1643 const BIGNUM *bn = NULL;
1644
1645 switch (EVP_PKEY_base_id(ctx->p2)) {
1646 #ifndef OPENSSL_NO_DH
1647 case EVP_PKEY_DH:
1648 bn = DH_get0_q(EVP_PKEY_get0_DH(ctx->p2));
1649 break;
1650 #endif
1651 #ifndef OPENSSL_NO_DSA
1652 case EVP_PKEY_DSA:
1653 bn = DSA_get0_q(EVP_PKEY_get0_DSA(ctx->p2));
1654 break;
1655 #endif
1656 }
1657
1658 return get_payload_bn(state, translation, ctx, bn);
1659 }
1660
1661 static int get_dh_dsa_payload_g(enum state state,
1662 const struct translation_st *translation,
1663 struct translation_ctx_st *ctx)
1664 {
1665 const BIGNUM *bn = NULL;
1666
1667 switch (EVP_PKEY_base_id(ctx->p2)) {
1668 #ifndef OPENSSL_NO_DH
1669 case EVP_PKEY_DH:
1670 bn = DH_get0_g(EVP_PKEY_get0_DH(ctx->p2));
1671 break;
1672 #endif
1673 #ifndef OPENSSL_NO_DSA
1674 case EVP_PKEY_DSA:
1675 bn = DSA_get0_g(EVP_PKEY_get0_DSA(ctx->p2));
1676 break;
1677 #endif
1678 }
1679
1680 return get_payload_bn(state, translation, ctx, bn);
1681 }
1682
1683 static int get_rsa_payload_n(enum state state,
1684 const struct translation_st *translation,
1685 struct translation_ctx_st *ctx)
1686 {
1687 const BIGNUM *bn = NULL;
1688
1689 if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)
1690 return 0;
1691 bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
1692
1693 return get_payload_bn(state, translation, ctx, bn);
1694 }
1695
1696 static int get_rsa_payload_e(enum state state,
1697 const struct translation_st *translation,
1698 struct translation_ctx_st *ctx)
1699 {
1700 const BIGNUM *bn = NULL;
1701
1702 if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)
1703 return 0;
1704 bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
1705
1706 return get_payload_bn(state, translation, ctx, bn);
1707 }
1708
1709 static int get_rsa_payload_d(enum state state,
1710 const struct translation_st *translation,
1711 struct translation_ctx_st *ctx)
1712 {
1713 const BIGNUM *bn = NULL;
1714
1715 if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA)
1716 return 0;
1717 bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
1718
1719 return get_payload_bn(state, translation, ctx, bn);
1720 }
1721
1722 static int get_rsa_payload_factor(enum state state,
1723 const struct translation_st *translation,
1724 struct translation_ctx_st *ctx,
1725 size_t factornum)
1726 {
1727 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1728 const BIGNUM *bn = NULL;
1729
1730 switch (factornum) {
1731 case 0:
1732 bn = RSA_get0_p(r);
1733 break;
1734 case 1:
1735 bn = RSA_get0_q(r);
1736 break;
1737 default:
1738 {
1739 size_t pnum = RSA_get_multi_prime_extra_count(r);
1740 const BIGNUM *factors[10];
1741
1742 if (factornum - 2 < pnum
1743 && RSA_get0_multi_prime_factors(r, factors))
1744 bn = factors[factornum - 2];
1745 }
1746 break;
1747 }
1748
1749 return get_payload_bn(state, translation, ctx, bn);
1750 }
1751
1752 static int get_rsa_payload_exponent(enum state state,
1753 const struct translation_st *translation,
1754 struct translation_ctx_st *ctx,
1755 size_t exponentnum)
1756 {
1757 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1758 const BIGNUM *bn = NULL;
1759
1760 switch (exponentnum) {
1761 case 0:
1762 bn = RSA_get0_dmp1(r);
1763 break;
1764 case 1:
1765 bn = RSA_get0_dmq1(r);
1766 break;
1767 default:
1768 {
1769 size_t pnum = RSA_get_multi_prime_extra_count(r);
1770 const BIGNUM *exps[10], *coeffs[10];
1771
1772 if (exponentnum - 2 < pnum
1773 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1774 bn = exps[exponentnum - 2];
1775 }
1776 break;
1777 }
1778
1779 return get_payload_bn(state, translation, ctx, bn);
1780 }
1781
1782 static int get_rsa_payload_coefficient(enum state state,
1783 const struct translation_st *translation,
1784 struct translation_ctx_st *ctx,
1785 size_t coefficientnum)
1786 {
1787 const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
1788 const BIGNUM *bn = NULL;
1789
1790 switch (coefficientnum) {
1791 case 0:
1792 bn = RSA_get0_iqmp(r);
1793 break;
1794 default:
1795 {
1796 size_t pnum = RSA_get_multi_prime_extra_count(r);
1797 const BIGNUM *exps[10], *coeffs[10];
1798
1799 if (coefficientnum - 1 < pnum
1800 && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
1801 bn = coeffs[coefficientnum - 1];
1802 }
1803 break;
1804 }
1805
1806 return get_payload_bn(state, translation, ctx, bn);
1807 }
1808
1809 #define IMPL_GET_RSA_PAYLOAD_FACTOR(n) \
1810 static int \
1811 get_rsa_payload_f##n(enum state state, \
1812 const struct translation_st *translation, \
1813 struct translation_ctx_st *ctx) \
1814 { \
1815 if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA) \
1816 return 0; \
1817 return get_rsa_payload_factor(state, translation, ctx, n - 1); \
1818 }
1819
1820 #define IMPL_GET_RSA_PAYLOAD_EXPONENT(n) \
1821 static int \
1822 get_rsa_payload_e##n(enum state state, \
1823 const struct translation_st *translation, \
1824 struct translation_ctx_st *ctx) \
1825 { \
1826 if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA) \
1827 return 0; \
1828 return get_rsa_payload_exponent(state, translation, ctx, \
1829 n - 1); \
1830 }
1831
1832 #define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n) \
1833 static int \
1834 get_rsa_payload_c##n(enum state state, \
1835 const struct translation_st *translation, \
1836 struct translation_ctx_st *ctx) \
1837 { \
1838 if (EVP_PKEY_base_id(ctx->p2) != EVP_PKEY_RSA) \
1839 return 0; \
1840 return get_rsa_payload_coefficient(state, translation, ctx, \
1841 n - 1); \
1842 }
1843
1844 IMPL_GET_RSA_PAYLOAD_FACTOR(1)
1845 IMPL_GET_RSA_PAYLOAD_FACTOR(2)
1846 IMPL_GET_RSA_PAYLOAD_FACTOR(3)
1847 IMPL_GET_RSA_PAYLOAD_FACTOR(4)
1848 IMPL_GET_RSA_PAYLOAD_FACTOR(5)
1849 IMPL_GET_RSA_PAYLOAD_FACTOR(6)
1850 IMPL_GET_RSA_PAYLOAD_FACTOR(7)
1851 IMPL_GET_RSA_PAYLOAD_FACTOR(8)
1852 IMPL_GET_RSA_PAYLOAD_FACTOR(9)
1853 IMPL_GET_RSA_PAYLOAD_FACTOR(10)
1854 IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
1855 IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
1856 IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
1857 IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
1858 IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
1859 IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
1860 IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
1861 IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
1862 IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
1863 IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
1864 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
1865 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
1866 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
1867 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
1868 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
1869 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
1870 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
1871 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
1872 IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
1873
1874 /*-
1875 * The translation table itself
1876 * ============================
1877 */
1878
1879 static const struct translation_st evp_pkey_ctx_translations[] = {
1880 /*
1881 * DistID: we pass it to the backend as an octet string,
1882 * but get it back as a pointer to an octet string.
1883 *
1884 * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
1885 * that has no separate counterpart in OSSL_PARAM terms, since we get
1886 * the length of the DistID automatically when getting the DistID itself.
1887 */
1888 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
1889 EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
1890 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
1891 { GET, -1, -1, -1,
1892 EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
1893 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
1894 { GET, -1, -1, -1,
1895 EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
1896 OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
1897
1898 /*-
1899 * DH & DHX
1900 * ========
1901 */
1902
1903 /*
1904 * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting. The
1905 * fixup function has to handle this...
1906 */
1907 { NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1908 EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
1909 OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
1910 fix_dh_kdf_type },
1911 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1912 EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
1913 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1914 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1915 EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
1916 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1917 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1918 EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
1919 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1920 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1921 EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
1922 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1923 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1924 EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
1925 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
1926 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1927 EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
1928 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
1929 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1930 EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
1931 OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
1932 { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
1933 EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
1934 OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
1935
1936 /* DHX Keygen Parameters that are shared with DH */
1937 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
1938 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
1939 OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
1940 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
1941 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
1942 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1943 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
1944 EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
1945 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, NULL },
1946 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
1947 EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
1948 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
1949
1950 /* DH Keygen Parameters that are shared with DHX */
1951 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1952 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
1953 OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
1954 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1955 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
1956 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1957 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
1958 EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
1959 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
1960 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
1961 EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
1962 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
1963
1964 /* DH specific Keygen Parameters */
1965 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
1966 EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
1967 OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
1968
1969 /* DHX specific Keygen Parameters */
1970 { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
1971 EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
1972 OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1973
1974 { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
1975 EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
1976 OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1977
1978 /*-
1979 * DSA
1980 * ===
1981 */
1982 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
1983 EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
1984 OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1985 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
1986 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
1987 OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
1988 { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
1989 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
1990 OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
1991
1992 /*-
1993 * EC
1994 * ==
1995 */
1996 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
1997 EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
1998 OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
1999 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
2000 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
2001 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2002 fix_ec_paramgen_curve_nid },
2003 /*
2004 * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
2005 * both for setting and getting. The fixup function has to handle this...
2006 */
2007 { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2008 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
2009 OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
2010 fix_ecdh_cofactor },
2011 { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2012 EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
2013 OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
2014 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2015 EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
2016 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2017 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2018 EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
2019 OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2020 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2021 EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
2022 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2023 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2024 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
2025 OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2026 { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2027 EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
2028 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
2029 { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
2030 EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
2031 OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
2032
2033 /*-
2034 * RSA
2035 * ===
2036 */
2037
2038 /*
2039 * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
2040 * and can be both with OSSL_PARAM. We standardise on strings here,
2041 * fix_rsa_padding_mode() does the work when the caller has a different
2042 * idea.
2043 */
2044 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2045 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2046 EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
2047 OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2048 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2049 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2050 EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
2051 OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
2052
2053 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2054 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2055 EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
2056 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2057 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
2058 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
2059 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
2060 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2061
2062 /*
2063 * RSA-PSS saltlen is essentially numeric, but certain values can be
2064 * expressed as keywords (strings) with ctrl_str. The corresponding
2065 * OSSL_PARAM allows both forms.
2066 * fix_rsa_pss_saltlen() takes care of the distinction.
2067 */
2068 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2069 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
2070 OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2071 fix_rsa_pss_saltlen },
2072 { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
2073 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
2074 OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
2075 fix_rsa_pss_saltlen },
2076
2077 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2078 EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
2079 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2080 { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2081 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
2082 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2083 /*
2084 * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
2085 * This is accomodated by default_fixup_args() above, which mimics that
2086 * expectation for any translation item where |ctrl_str| is NULL and
2087 * |ctrl_hexstr| is non-NULL.
2088 */
2089 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2090 EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
2091 OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2092 { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
2093 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
2094 OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
2095
2096 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2097 EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
2098 OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2099 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2100 EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
2101 OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2102 { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
2103 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
2104 OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
2105 { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
2106 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
2107 OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2108 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2109 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
2110 OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2111 { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
2112 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
2113 OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2114
2115 /*-
2116 * SipHash
2117 * ======
2118 */
2119 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2120 EVP_PKEY_CTRL_SET_DIGEST_SIZE, "digestsize", NULL,
2121 OSSL_MAC_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2122
2123 /*-
2124 * TLS1-PRF
2125 * ========
2126 */
2127 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2128 EVP_PKEY_CTRL_TLS_MD, "md", NULL,
2129 OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2130 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2131 EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
2132 OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
2133 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2134 EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
2135 OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
2136
2137 /*-
2138 * HKDF
2139 * ====
2140 */
2141 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2142 EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
2143 OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2144 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2145 EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
2146 OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2147 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2148 EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
2149 OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2150 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2151 EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
2152 OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
2153 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2154 EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
2155 OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
2156
2157 /*-
2158 * Scrypt
2159 * ======
2160 */
2161 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2162 EVP_PKEY_CTRL_PASS, "pass", "hexpass",
2163 OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
2164 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2165 EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
2166 OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
2167 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2168 EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
2169 OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2170 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2171 EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
2172 OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2173 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2174 EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
2175 OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2176 { SET, -1, -1, EVP_PKEY_OP_DERIVE,
2177 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
2178 OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
2179
2180 { SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
2181 EVP_PKEY_CTRL_CIPHER, NULL, NULL,
2182 OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
2183 { SET, -1, -1, EVP_PKEY_OP_KEYGEN,
2184 EVP_PKEY_CTRL_SET_MAC_KEY, NULL, NULL,
2185 OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
2186
2187 { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2188 EVP_PKEY_CTRL_MD, NULL, NULL,
2189 OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2190 { GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
2191 EVP_PKEY_CTRL_GET_MD, NULL, NULL,
2192 OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
2193 };
2194
2195 static const struct translation_st evp_pkey_translations[] = {
2196 /*
2197 * The following contain no ctrls, they are exclusively here to extract
2198 * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
2199 * on |fixup_args| to pass the actual data. The |fixup_args| should
2200 * expect to get the EVP_PKEY pointer through |ctx->p2|.
2201 */
2202
2203 /* DH, DSA & EC */
2204 { GET, -1, -1, -1, 0, NULL, NULL,
2205 OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
2206 get_payload_group_name },
2207 { GET, -1, -1, -1, 0, NULL, NULL,
2208 OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
2209 get_payload_private_key },
2210 { GET, -1, -1, -1, 0, NULL, NULL,
2211 OSSL_PKEY_PARAM_PUB_KEY,
2212 0 /* no data type, let get_payload_pub_key() handle that */,
2213 get_payload_public_key },
2214
2215 /* DH and DSA */
2216 { GET, -1, -1, -1, 0, NULL, NULL,
2217 OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
2218 get_dh_dsa_payload_p },
2219 { GET, -1, -1, -1, 0, NULL, NULL,
2220 OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
2221 get_dh_dsa_payload_g },
2222 { GET, -1, -1, -1, 0, NULL, NULL,
2223 OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
2224 get_dh_dsa_payload_q },
2225
2226 /* RSA */
2227 { GET, -1, -1, -1, 0, NULL, NULL,
2228 OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
2229 get_rsa_payload_n },
2230 { GET, -1, -1, -1, 0, NULL, NULL,
2231 OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
2232 get_rsa_payload_e },
2233 { GET, -1, -1, -1, 0, NULL, NULL,
2234 OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
2235 get_rsa_payload_d },
2236 { GET, -1, -1, -1, 0, NULL, NULL,
2237 OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
2238 get_rsa_payload_f1 },
2239 { GET, -1, -1, -1, 0, NULL, NULL,
2240 OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
2241 get_rsa_payload_f2 },
2242 { GET, -1, -1, -1, 0, NULL, NULL,
2243 OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
2244 get_rsa_payload_f3 },
2245 { GET, -1, -1, -1, 0, NULL, NULL,
2246 OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
2247 get_rsa_payload_f4 },
2248 { GET, -1, -1, -1, 0, NULL, NULL,
2249 OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
2250 get_rsa_payload_f5 },
2251 { GET, -1, -1, -1, 0, NULL, NULL,
2252 OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
2253 get_rsa_payload_f6 },
2254 { GET, -1, -1, -1, 0, NULL, NULL,
2255 OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
2256 get_rsa_payload_f7 },
2257 { GET, -1, -1, -1, 0, NULL, NULL,
2258 OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
2259 get_rsa_payload_f8 },
2260 { GET, -1, -1, -1, 0, NULL, NULL,
2261 OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
2262 get_rsa_payload_f9 },
2263 { GET, -1, -1, -1, 0, NULL, NULL,
2264 OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
2265 get_rsa_payload_f10 },
2266 { GET, -1, -1, -1, 0, NULL, NULL,
2267 OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2268 get_rsa_payload_e1 },
2269 { GET, -1, -1, -1, 0, NULL, NULL,
2270 OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2271 get_rsa_payload_e2 },
2272 { GET, -1, -1, -1, 0, NULL, NULL,
2273 OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2274 get_rsa_payload_e3 },
2275 { GET, -1, -1, -1, 0, NULL, NULL,
2276 OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2277 get_rsa_payload_e4 },
2278 { GET, -1, -1, -1, 0, NULL, NULL,
2279 OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2280 get_rsa_payload_e5 },
2281 { GET, -1, -1, -1, 0, NULL, NULL,
2282 OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2283 get_rsa_payload_e6 },
2284 { GET, -1, -1, -1, 0, NULL, NULL,
2285 OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2286 get_rsa_payload_e7 },
2287 { GET, -1, -1, -1, 0, NULL, NULL,
2288 OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2289 get_rsa_payload_e8 },
2290 { GET, -1, -1, -1, 0, NULL, NULL,
2291 OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2292 get_rsa_payload_e9 },
2293 { GET, -1, -1, -1, 0, NULL, NULL,
2294 OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
2295 get_rsa_payload_e10 },
2296 { GET, -1, -1, -1, 0, NULL, NULL,
2297 OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
2298 get_rsa_payload_c1 },
2299 { GET, -1, -1, -1, 0, NULL, NULL,
2300 OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
2301 get_rsa_payload_c2 },
2302 { GET, -1, -1, -1, 0, NULL, NULL,
2303 OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
2304 get_rsa_payload_c3 },
2305 { GET, -1, -1, -1, 0, NULL, NULL,
2306 OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
2307 get_rsa_payload_c4 },
2308 { GET, -1, -1, -1, 0, NULL, NULL,
2309 OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
2310 get_rsa_payload_c5 },
2311 { GET, -1, -1, -1, 0, NULL, NULL,
2312 OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
2313 get_rsa_payload_c6 },
2314 { GET, -1, -1, -1, 0, NULL, NULL,
2315 OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
2316 get_rsa_payload_c7 },
2317 { GET, -1, -1, -1, 0, NULL, NULL,
2318 OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
2319 get_rsa_payload_c8 },
2320 { GET, -1, -1, -1, 0, NULL, NULL,
2321 OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
2322 get_rsa_payload_c9 },
2323 };
2324
2325 static const struct translation_st *
2326 lookup_translation(struct translation_st *tmpl,
2327 const struct translation_st *translations,
2328 size_t translations_num)
2329 {
2330 size_t i;
2331
2332 for (i = 0; i < translations_num; i++) {
2333 const struct translation_st *item = &translations[i];
2334
2335 /*
2336 * Sanity check the translation table item.
2337 *
2338 * 1. Either both keytypes are -1, or neither of them are.
2339 * 2. TBA...
2340 */
2341 if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
2342 continue;
2343
2344
2345 /*
2346 * Base search criteria: check that the optype and keytypes match,
2347 * if relevant. All callers must synthesise these bits somehow.
2348 */
2349 if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
2350 continue;
2351 /*
2352 * This expression is stunningly simple thanks to the sanity check
2353 * above.
2354 */
2355 if (item->keytype1 != -1
2356 && tmpl->keytype1 != item->keytype1
2357 && tmpl->keytype2 != item->keytype2)
2358 continue;
2359
2360 /*
2361 * Done with the base search criteria, now we check the criteria for
2362 * the individual types of translations:
2363 * ctrl->params, ctrl_str->params, and params->ctrl
2364 */
2365 if (tmpl->ctrl_num != 0) {
2366 if (tmpl->ctrl_num != item->ctrl_num)
2367 continue;
2368 } else if (tmpl->ctrl_str != NULL) {
2369 const char *ctrl_str = NULL;
2370 const char *ctrl_hexstr = NULL;
2371
2372 /*
2373 * Search criteria that originates from a ctrl_str is only used
2374 * for setting, never for getting. Therefore, we only look at
2375 * the setter items.
2376 */
2377 if (item->action_type != NONE
2378 && item->action_type != SET)
2379 continue;
2380 /*
2381 * At least one of the ctrl cmd names must be match the ctrl
2382 * cmd name in the template.
2383 */
2384 if (item->ctrl_str != NULL
2385 && strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
2386 ctrl_str = tmpl->ctrl_str;
2387 else if (item->ctrl_hexstr != NULL
2388 && strcasecmp(tmpl->ctrl_hexstr, item->ctrl_hexstr) == 0)
2389 ctrl_hexstr = tmpl->ctrl_hexstr;
2390 else
2391 continue;
2392
2393 /* Modify the template to signal which string matched */
2394 tmpl->ctrl_str = ctrl_str;
2395 tmpl->ctrl_hexstr = ctrl_hexstr;
2396 } else if (tmpl->param_key != NULL) {
2397 /*
2398 * Search criteria that originates from a OSSL_PARAM setter or
2399 * getter.
2400 *
2401 * Ctrls were fundamentally bidirectional, with only the ctrl
2402 * command macro name implying direction (if you're lucky).
2403 * A few ctrl commands were even taking advantage of the
2404 * bidirectional nature, making the direction depend in the
2405 * value of the numeric argument.
2406 *
2407 * OSSL_PARAM functions are fundamentally different, in that
2408 * setters and getters are separated, so the data direction is
2409 * implied by the function that's used. The same OSSL_PARAM
2410 * key name can therefore be used in both directions. We must
2411 * therefore take the action type into account in this case.
2412 */
2413 if ((item->action_type != NONE
2414 && tmpl->action_type != item->action_type)
2415 || (item->param_key != NULL
2416 && strcasecmp(tmpl->param_key, item->param_key) != 0))
2417 continue;
2418 } else {
2419 return NULL;
2420 }
2421
2422 return item;
2423 }
2424
2425 return NULL;
2426 }
2427
2428 static const struct translation_st *
2429 lookup_evp_pkey_ctx_translation(struct translation_st *tmpl)
2430 {
2431 return lookup_translation(tmpl, evp_pkey_ctx_translations,
2432 OSSL_NELEM(evp_pkey_ctx_translations));
2433 }
2434
2435 static const struct translation_st *
2436 lookup_evp_pkey_translation(struct translation_st *tmpl)
2437 {
2438 return lookup_translation(tmpl, evp_pkey_translations,
2439 OSSL_NELEM(evp_pkey_translations));
2440 }
2441
2442 /* This must ONLY be called for provider side operations */
2443 int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *pctx,
2444 int keytype, int optype,
2445 int cmd, int p1, void *p2)
2446 {
2447 struct translation_ctx_st ctx = { 0, };
2448 struct translation_st tmpl = { 0, };
2449 const struct translation_st *translation = NULL;
2450 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2451 int ret;
2452 fixup_args_fn *fixup = default_fixup_args;
2453
2454 if (keytype == -1)
2455 keytype = pctx->legacy_keytype;
2456 tmpl.ctrl_num = cmd;
2457 tmpl.keytype1 = tmpl.keytype2 = keytype;
2458 tmpl.optype = optype;
2459 translation = lookup_evp_pkey_ctx_translation(&tmpl);
2460
2461 if (translation == NULL) {
2462 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
2463 return -2;
2464 }
2465
2466 if (pctx->pmeth != NULL
2467 && pctx->pmeth->pkey_id != translation->keytype1
2468 && pctx->pmeth->pkey_id != translation->keytype2)
2469 return -1;
2470
2471 if (translation->fixup_args != NULL)
2472 fixup = translation->fixup_args;
2473 ctx.action_type = translation->action_type;
2474 ctx.ctrl_cmd = cmd;
2475 ctx.p1 = p1;
2476 ctx.p2 = p2;
2477 ctx.pctx = pctx;
2478 ctx.params = params;
2479
2480 ret = fixup(PRE_CTRL_TO_PARAMS, translation, &ctx);
2481
2482 if (ret > 0) {
2483 switch (ctx.action_type) {
2484 default:
2485 /* fixup_args is expected to make sure this is dead code */
2486 break;
2487 case GET:
2488 ret = evp_pkey_ctx_get_params_strict(pctx, ctx.params);
2489 break;
2490 case SET:
2491 ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2492 break;
2493 }
2494 }
2495
2496 /*
2497 * In POST, we pass the return value as p1, allowing the fixup_args
2498 * function to affect it by changing its value.
2499 */
2500 if (ret > 0) {
2501 ctx.p1 = ret;
2502 fixup(POST_CTRL_TO_PARAMS, translation, &ctx);
2503 ret = ctx.p1;
2504 }
2505
2506 cleanup_translation_ctx(POST_CTRL_TO_PARAMS, translation, &ctx);
2507
2508 return ret;
2509 }
2510
2511 /* This must ONLY be called for provider side operations */
2512 int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *pctx,
2513 const char *name, const char *value)
2514 {
2515 struct translation_ctx_st ctx = { 0, };
2516 struct translation_st tmpl = { 0, };
2517 const struct translation_st *translation = NULL;
2518 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2519 int keytype = pctx->legacy_keytype;
2520 int optype = pctx->operation == 0 ? -1 : pctx->operation;
2521 int ret;
2522 fixup_args_fn *fixup = default_fixup_args;
2523
2524 tmpl.action_type = SET;
2525 tmpl.keytype1 = tmpl.keytype2 = keytype;
2526 tmpl.optype = optype;
2527 tmpl.ctrl_str = name;
2528 tmpl.ctrl_hexstr = name;
2529 translation = lookup_evp_pkey_ctx_translation(&tmpl);
2530
2531 if (translation != NULL) {
2532 if (translation->fixup_args != NULL)
2533 fixup = translation->fixup_args;
2534 ctx.action_type = translation->action_type;
2535 ctx.ishex = (tmpl.ctrl_hexstr != NULL);
2536 } else {
2537 /* String controls really only support setting */
2538 ctx.action_type = SET;
2539 }
2540 ctx.ctrl_str = name;
2541 ctx.p1 = (int)strlen(value);
2542 ctx.p2 = (char *)value;
2543 ctx.pctx = pctx;
2544 ctx.params = params;
2545
2546 ret = fixup(PRE_CTRL_STR_TO_PARAMS, translation, &ctx);
2547
2548 if (ret > 0) {
2549 switch (ctx.action_type) {
2550 default:
2551 /* fixup_args is expected to make sure this is dead code */
2552 break;
2553 case GET:
2554 /*
2555 * this is dead code, but must be present, or some compilers
2556 * will complain
2557 */
2558 break;
2559 case SET:
2560 ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
2561 break;
2562 }
2563 }
2564
2565 if (ret > 0)
2566 ret = fixup(POST_CTRL_STR_TO_PARAMS, translation, &ctx);
2567
2568 cleanup_translation_ctx(CLEANUP_CTRL_STR_TO_PARAMS, translation, &ctx);
2569
2570 return ret;
2571 }
2572
2573 /* This must ONLY be called for legacy operations */
2574 static int evp_pkey_ctx_setget_params_to_ctrl(EVP_PKEY_CTX *pctx,
2575 enum action action_type,
2576 OSSL_PARAM *params)
2577 {
2578 int keytype = pctx->legacy_keytype;
2579 int optype = pctx->operation == 0 ? -1 : pctx->operation;
2580
2581 for (; params != NULL && params->key != NULL; params++) {
2582 struct translation_ctx_st ctx = { 0, };
2583 struct translation_st tmpl = { 0, };
2584 const struct translation_st *translation = NULL;
2585 fixup_args_fn *fixup = default_fixup_args;
2586 int ret;
2587
2588 tmpl.action_type = action_type;
2589 tmpl.keytype1 = tmpl.keytype2 = keytype;
2590 tmpl.optype = optype;
2591 tmpl.param_key = params->key;
2592 translation = lookup_evp_pkey_ctx_translation(&tmpl);
2593
2594 if (translation != NULL) {
2595 if (translation->fixup_args != NULL)
2596 fixup = translation->fixup_args;
2597 ctx.action_type = translation->action_type;
2598 }
2599 ctx.pctx = pctx;
2600 ctx.params = params;
2601
2602 ret = fixup(PRE_PARAMS_TO_CTRL, translation, &ctx);
2603
2604 if (ret > 0 && action_type != NONE)
2605 ret = EVP_PKEY_CTX_ctrl(pctx, keytype, optype,
2606 ctx.ctrl_cmd, ctx.p1, ctx.p2);
2607
2608 /*
2609 * In POST, we pass the return value as p1, allowing the fixup_args
2610 * function to put it to good use, or maybe affect it.
2611 */
2612 if (ret > 0) {
2613 ctx.p1 = ret;
2614 fixup(POST_PARAMS_TO_CTRL, translation, &ctx);
2615 ret = ctx.p1;
2616 }
2617
2618 cleanup_translation_ctx(CLEANUP_PARAMS_TO_CTRL, translation, &ctx);
2619
2620 if (ret <= 0)
2621 return 0;
2622 }
2623 return 1;
2624 }
2625
2626 int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
2627 {
2628 return evp_pkey_ctx_setget_params_to_ctrl(ctx, SET, (OSSL_PARAM *)params);
2629 }
2630
2631 int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
2632 {
2633 return evp_pkey_ctx_setget_params_to_ctrl(ctx, GET, params);
2634 }
2635
2636 /* This must ONLY be called for legacy EVP_PKEYs */
2637 static int evp_pkey_setget_params_to_ctrl(const EVP_PKEY *pkey,
2638 enum action action_type,
2639 OSSL_PARAM *params)
2640 {
2641 int ret = 1;
2642
2643 for (; params != NULL && params->key != NULL; params++) {
2644 struct translation_ctx_st ctx = { 0, };
2645 struct translation_st tmpl = { 0, };
2646 const struct translation_st *translation = NULL;
2647 fixup_args_fn *fixup = default_fixup_args;
2648
2649 tmpl.action_type = action_type;
2650 tmpl.param_key = params->key;
2651 translation = lookup_evp_pkey_translation(&tmpl);
2652
2653 if (translation != NULL) {
2654 if (translation->fixup_args != NULL)
2655 fixup = translation->fixup_args;
2656 ctx.action_type = translation->action_type;
2657 }
2658 ctx.p2 = (void *)pkey;
2659 ctx.params = params;
2660
2661 /*
2662 * EVP_PKEY doesn't have any ctrl function, so we rely completely
2663 * on fixup_args to do the whole work. Also, we currently only
2664 * support getting.
2665 */
2666 if (!ossl_assert(translation != NULL)
2667 || !ossl_assert(translation->action_type == GET)
2668 || !ossl_assert(translation->fixup_args != NULL)) {
2669 return -2;
2670 }
2671
2672 ret = fixup(PKEY, translation, &ctx);
2673
2674 cleanup_translation_ctx(PKEY, translation, &ctx);
2675 }
2676 return ret;
2677 }
2678
2679 int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params)
2680 {
2681 return evp_pkey_setget_params_to_ctrl(pkey, GET, params);
2682 }
2683