]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
EVP: Implement data-driven translation between known ctrl and OSSL_PARAMs
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
CommitLineData
0f113f3e 1/*
a28d06f3 2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
0b6f3c66 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
0b6f3c66
DSH
8 */
9
ada66e78 10/*
b03ec3b5 11 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
ada66e78
P
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
0b6f3c66
DSH
16#include <stdio.h>
17#include <stdlib.h>
3c27208f 18#include <openssl/engine.h>
33bed28b 19#include <openssl/evp.h>
99119000 20#include <openssl/x509v3.h>
35aca9ec
MC
21#include <openssl/core_names.h>
22#include <openssl/dh.h>
89abd1b6 23#include <openssl/rsa.h>
ac2d58c7 24#include <openssl/kdf.h>
35aca9ec 25#include "internal/cryptlib.h"
25f2138b
DMSP
26#include "crypto/asn1.h"
27#include "crypto/evp.h"
7165593c 28#include "crypto/dh.h"
7229a2f4 29#include "crypto/ec.h"
7165593c 30#include "internal/ffc.h"
99119000 31#include "internal/numbers.h"
390acbeb 32#include "internal/provider.h"
706457b7 33#include "evp_local.h"
0b6f3c66 34
f844f9eb 35#ifndef FIPS_MODULE
e683582b 36
86df26b3
RL
37static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
38 int keytype, int optype,
39 int cmd, const char *name,
40 const void *data, size_t data_len);
41static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
42 int cmd, const char *name);
43static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
44
19bd1fa1 45typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
0f113f3e 46typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
5ce278a7 47
df2ee0e2 48static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
0b6f3c66 49
cefa762e 50/* This array needs to be in order of NIDs */
19bd1fa1 51static pmeth_fn standard_methods[] = {
23b2fc0b 52 ossl_rsa_pkey_method,
e683582b 53# ifndef OPENSSL_NO_DH
19bd1fa1 54 dh_pkey_method,
e683582b
SL
55# endif
56# ifndef OPENSSL_NO_DSA
19bd1fa1 57 dsa_pkey_method,
e683582b
SL
58# endif
59# ifndef OPENSSL_NO_EC
19bd1fa1 60 ec_pkey_method,
e683582b 61# endif
23b2fc0b 62 ossl_rsa_pss_pkey_method,
e683582b 63# ifndef OPENSSL_NO_DH
19bd1fa1 64 dhx_pkey_method,
e683582b 65# endif
e683582b 66# ifndef OPENSSL_NO_EC
19bd1fa1
PS
67 ecx25519_pkey_method,
68 ecx448_pkey_method,
e683582b 69# endif
e683582b 70# ifndef OPENSSL_NO_EC
19bd1fa1
PS
71 ed25519_pkey_method,
72 ed448_pkey_method,
e683582b 73# endif
0f113f3e 74};
0b6f3c66 75
19bd1fa1
PS
76DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
77
78static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
79{
80 return ((*a)->pkey_id - ((**b)())->pkey_id);
81}
82
83IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
babb3798 84
0f113f3e
MC
85static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
86 const EVP_PKEY_METHOD *const *b)
87{
88 return ((*a)->pkey_id - (*b)->pkey_id);
89}
0b6f3c66 90
60488d24 91static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
0f113f3e 92{
60488d24 93 if (app_pkey_methods != NULL) {
0f113f3e 94 int idx;
60488d24
RL
95 EVP_PKEY_METHOD tmp;
96
97 tmp.pkey_id = type;
0f113f3e
MC
98 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
99 if (idx >= 0)
100 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
101 }
60488d24
RL
102 return NULL;
103}
104
105const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
106{
107 pmeth_fn *ret;
108 EVP_PKEY_METHOD tmp;
109 const EVP_PKEY_METHOD *t;
110
111 if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
112 return t;
113
114 tmp.pkey_id = type;
115 t = &tmp;
19bd1fa1 116 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
60488d24 117 OSSL_NELEM(standard_methods));
12a765a5 118 if (ret == NULL || *ret == NULL)
0f113f3e 119 return NULL;
19bd1fa1 120 return (**ret)();
0f113f3e 121}
0b6f3c66 122
e683582b
SL
123EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
124{
125 EVP_PKEY_METHOD *pmeth;
126
127 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
128 if (pmeth == NULL) {
9311d0c4 129 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
e683582b
SL
130 return NULL;
131 }
132
133 pmeth->pkey_id = id;
134 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
135 return pmeth;
136}
50914496 137
e19246dc
RL
138static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
139 void *arg)
140{
141 int *type = arg;
142
143 if (*type == NID_undef)
144 *type = evp_pkey_name2type(keytype);
145}
86df26b3 146
e19246dc
RL
147static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
148{
149 int type = NID_undef;
150
151 EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
152 &type);
153 return type;
154}
155#endif /* FIPS_MODULE */
156
157int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
86df26b3
RL
158{
159 if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
160 return EVP_PKEY_STATE_UNKNOWN;
161
162 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
163 && ctx->op.kex.exchprovctx != NULL)
164 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
165 && ctx->op.sig.sigprovctx != NULL)
166 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
167 && ctx->op.ciph.ciphprovctx != NULL)
168 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
80f4fd18
SL
169 && ctx->op.keymgmt.genctx != NULL)
170 || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
171 && ctx->op.encap.kemprovctx != NULL))
86df26b3
RL
172 return EVP_PKEY_STATE_PROVIDER;
173
174 return EVP_PKEY_STATE_LEGACY;
175}
176
b4250010 177static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
3ee348b0 178 EVP_PKEY *pkey, ENGINE *e,
4b9e90f4 179 const char *keytype, const char *propquery,
a07c17ef 180 int id)
e683582b 181
0f113f3e 182{
50914496 183 EVP_PKEY_CTX *ret = NULL;
d0ea49a8 184 const EVP_PKEY_METHOD *pmeth = NULL;
4b9e90f4 185 EVP_KEYMGMT *keymgmt = NULL;
d0ea49a8
RL
186
187 /*
50914496 188 * If the given |pkey| is provided, we extract the keytype from its
5e5bc836 189 * keymgmt and skip over the legacy code.
982efd77 190 */
5e5bc836 191 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
982efd77
RL
192 /* If we have an engine, something went wrong somewhere... */
193 if (!ossl_assert(e == NULL))
194 return NULL;
4b9e90f4 195 keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
982efd77
RL
196 goto common;
197 }
50914496 198
f844f9eb 199#ifndef FIPS_MODULE
50914496
RL
200 /*
201 * TODO(3.0) This legacy code section should be removed when we stop
202 * supporting engines
203 */
d0ea49a8 204 /* BEGIN legacy */
0f113f3e 205 if (id == -1) {
50914496
RL
206 if (pkey != NULL)
207 id = pkey->type;
208 else if (keytype != NULL)
209 id = evp_pkey_name2type(keytype);
210 if (id == NID_undef)
211 id = -1;
0f113f3e 212 }
50914496
RL
213 /* If no ID was found here, we can only resort to find a keymgmt */
214 if (id == -1)
215 goto common;
60653e5b
RL
216
217 /*
218 * Here, we extract what information we can for the purpose of
219 * supporting usage with implementations from providers, to make
220 * for a smooth transition from legacy stuff to provider based stuff.
221 *
222 * If an engine is given, this is entirely legacy, and we should not
223 * pretend anything else, so we only set the name when no engine is
224 * given. If both are already given, someone made a mistake, and
225 * since that can only happen internally, it's safe to make an
226 * assertion.
227 */
4b9e90f4 228 if (!ossl_assert(e == NULL || keytype == NULL))
60653e5b
RL
229 return NULL;
230 if (e == NULL)
4b9e90f4 231 keytype = OBJ_nid2sn(id);
60653e5b 232
e683582b 233# ifndef OPENSSL_NO_ENGINE
c2976edf 234 if (e == NULL && pkey != NULL)
d19b01ad 235 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
0f113f3e
MC
236 /* Try to find an ENGINE which implements this method */
237 if (e) {
238 if (!ENGINE_init(e)) {
9311d0c4 239 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
240 return NULL;
241 }
c2976edf 242 } else {
0f113f3e 243 e = ENGINE_get_pkey_meth_engine(id);
c2976edf 244 }
0f113f3e
MC
245
246 /*
0d4fb843 247 * If an ENGINE handled this method look it up. Otherwise use internal
0f113f3e
MC
248 * tables.
249 */
50914496 250 if (e != NULL)
0f113f3e 251 pmeth = ENGINE_get_pkey_meth(e, id);
50914496 252 else
e683582b 253# endif
60488d24 254 pmeth = evp_pkey_meth_find_added_by_application(id);
c9777d26 255
d0ea49a8 256 /* END legacy */
f844f9eb 257#endif /* FIPS_MODULE */
d0ea49a8 258 common:
4b9e90f4
RL
259 /*
260 * If there's no engine and there's a name, we try fetching a provider
261 * implementation.
262 */
5fcb97c6 263 if (e == NULL && keytype != NULL) {
4b9e90f4 264 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
d0b79f86 265 if (keymgmt == NULL)
50914496
RL
266 return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
267
268#ifndef FIPS_MODULE
269 /*
270 * Chase down the legacy NID, as that might be needed for diverse
271 * purposes, such as ensure that EVP_PKEY_type() can return sensible
272 * values, or that there's a better chance to "downgrade" a key when
273 * needed. We go through all keymgmt names, because the keytype
274 * that's passed to this function doesn't necessarily translate
275 * directly.
276 * TODO: Remove this when #legacy keys are gone.
277 */
278 if (keymgmt != NULL) {
279 int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt);
280
281 if (tmp_id != NID_undef) {
282 if (id == -1) {
283 id = tmp_id;
284 } else {
285 /*
286 * It really really shouldn't differ. If it still does,
287 * something is very wrong.
288 */
289 if (!ossl_assert(id == tmp_id)) {
9311d0c4 290 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
50914496
RL
291 EVP_KEYMGMT_free(keymgmt);
292 return NULL;
293 }
294 }
295 }
b533510f 296 }
50914496
RL
297#endif
298 }
299
300 if (pmeth == NULL && keymgmt == NULL) {
9311d0c4 301 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
50914496
RL
302 } else {
303 ret = OPENSSL_zalloc(sizeof(*ret));
304 if (ret == NULL)
9311d0c4 305 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
5fcb97c6 306 }
4b9e90f4 307
f844f9eb 308#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
50914496 309 if ((ret == NULL || pmeth == NULL) && e != NULL)
7c96dbcd 310 ENGINE_finish(e);
a63bf2c5 311#endif
50914496
RL
312
313 if (ret == NULL) {
314 EVP_KEYMGMT_free(keymgmt);
0f113f3e
MC
315 return NULL;
316 }
ddfd7182
SL
317 if (propquery != NULL) {
318 ret->propquery = OPENSSL_strdup(propquery);
319 if (ret->propquery == NULL) {
320 EVP_KEYMGMT_free(keymgmt);
321 return NULL;
322 }
323 }
3ee348b0 324 ret->libctx = libctx;
4b9e90f4
RL
325 ret->keytype = keytype;
326 ret->keymgmt = keymgmt;
50914496 327 ret->legacy_keytype = id; /* TODO: Remove when #legacy key are gone */
0f113f3e
MC
328 ret->engine = e;
329 ret->pmeth = pmeth;
330 ret->operation = EVP_PKEY_OP_UNDEFINED;
331 ret->pkey = pkey;
a6465b3f 332 if (pkey != NULL)
03273d61 333 EVP_PKEY_up_ref(pkey);
0f113f3e 334
8b84b075 335 if (pmeth != NULL && pmeth->init != NULL) {
0f113f3e 336 if (pmeth->init(ret) <= 0) {
83b4049a 337 ret->pmeth = NULL;
0f113f3e
MC
338 EVP_PKEY_CTX_free(ret);
339 return NULL;
340 }
341 }
342
343 return ret;
344}
345
f844f9eb 346/*- All methods below can also be used in FIPS_MODULE */
e683582b 347
b4250010 348EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
e683582b
SL
349 const char *name,
350 const char *propquery)
351{
352 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
353}
354
b4250010 355EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
2ee4a50a 356 const char *propquery)
e683582b 357{
2ee4a50a 358 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
e683582b
SL
359}
360
864b89ce
MC
361void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
362{
e683582b 363 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
864b89ce
MC
364 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
365 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
366 EVP_SIGNATURE_free(ctx->op.sig.signature);
fb1ecf85
RL
367 ctx->op.sig.sigprovctx = NULL;
368 ctx->op.sig.signature = NULL;
62f49b90 369 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
e683582b
SL
370 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
371 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
372 EVP_KEYEXCH_free(ctx->op.kex.exchange);
373 ctx->op.kex.exchprovctx = NULL;
374 ctx->op.kex.exchange = NULL;
80f4fd18
SL
375 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
376 if (ctx->op.encap.kemprovctx != NULL && ctx->op.encap.kem != NULL)
377 ctx->op.encap.kem->freectx(ctx->op.encap.kemprovctx);
378 EVP_KEM_free(ctx->op.encap.kem);
379 ctx->op.encap.kemprovctx = NULL;
380 ctx->op.encap.kem = NULL;
381 }
382 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
2c938e2e
MC
383 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
384 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
385 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
386 ctx->op.ciph.ciphprovctx = NULL;
387 ctx->op.ciph.cipher = NULL;
62924755
RL
388 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
389 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
390 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
864b89ce
MC
391 }
392}
393
e683582b 394void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
0f113f3e 395{
e683582b
SL
396 if (ctx == NULL)
397 return;
398 if (ctx->pmeth && ctx->pmeth->cleanup)
399 ctx->pmeth->cleanup(ctx);
b4faea50 400
e683582b 401 evp_pkey_ctx_free_old_ops(ctx);
86df26b3
RL
402#ifndef FIPS_MODULE
403 evp_pkey_ctx_free_all_cached_data(ctx);
404#endif
e683582b 405 EVP_KEYMGMT_free(ctx->keymgmt);
0f113f3e 406
ddfd7182 407 OPENSSL_free(ctx->propquery);
e683582b
SL
408 EVP_PKEY_free(ctx->pkey);
409 EVP_PKEY_free(ctx->peerkey);
f844f9eb 410#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
e683582b
SL
411 ENGINE_finish(ctx->engine);
412#endif
3786d748 413 BN_free(ctx->rsa_pubexp);
e683582b 414 OPENSSL_free(ctx);
0f113f3e 415}
ba30bad5 416
f844f9eb 417#ifndef FIPS_MODULE
e683582b 418
f830c68f 419void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
0f113f3e
MC
420 const EVP_PKEY_METHOD *meth)
421{
422 if (ppkey_id)
423 *ppkey_id = meth->pkey_id;
424 if (pflags)
425 *pflags = meth->flags;
426}
f830c68f
DSH
427
428void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
0f113f3e 429{
4cbb196b
AT
430 int pkey_id = dst->pkey_id;
431 int flags = dst->flags;
f830c68f 432
4cbb196b 433 *dst = *src;
f830c68f 434
4cbb196b
AT
435 /* We only copy the function pointers so restore the other values */
436 dst->pkey_id = pkey_id;
437 dst->flags = flags;
0f113f3e 438}
f830c68f 439
ba30bad5 440void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
441{
442 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
443 OPENSSL_free(pmeth);
444}
ba30bad5 445
f5cda4cb 446EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
0f113f3e 447{
3ee348b0 448 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
0f113f3e 449}
f5cda4cb
DSH
450
451EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
0f113f3e 452{
3ee348b0 453 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
a07c17ef
RL
454}
455
9fdcc21f 456EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
0f113f3e
MC
457{
458 EVP_PKEY_CTX *rctx;
ff64702b 459
e683582b 460# ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
461 /* Make sure it's safe to copy a pkey context using an ENGINE */
462 if (pctx->engine && !ENGINE_init(pctx->engine)) {
9311d0c4 463 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
464 return 0;
465 }
e683582b 466# endif
ff64702b 467 rctx = OPENSSL_zalloc(sizeof(*rctx));
3484236d 468 if (rctx == NULL) {
9311d0c4 469 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e 470 return NULL;
3484236d 471 }
8bdcef40 472
ff64702b
MC
473 if (pctx->pkey != NULL)
474 EVP_PKEY_up_ref(pctx->pkey);
475 rctx->pkey = pctx->pkey;
476 rctx->operation = pctx->operation;
3ee348b0 477 rctx->libctx = pctx->libctx;
f23bc0b7 478 rctx->keytype = pctx->keytype;
ddfd7182
SL
479 rctx->propquery = NULL;
480 if (pctx->propquery != NULL) {
481 rctx->propquery = OPENSSL_strdup(pctx->propquery);
74cd923a
RL
482 if (rctx->propquery == NULL)
483 goto err;
ddfd7182 484 }
ac7750bb 485 rctx->legacy_keytype = pctx->legacy_keytype;
ff64702b 486
864b89ce
MC
487 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
488 if (pctx->op.kex.exchange != NULL) {
489 rctx->op.kex.exchange = pctx->op.kex.exchange;
ac7750bb 490 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
74cd923a 491 goto err;
ff64702b 492 }
864b89ce
MC
493 if (pctx->op.kex.exchprovctx != NULL) {
494 if (!ossl_assert(pctx->op.kex.exchange != NULL))
74cd923a 495 goto err;
864b89ce
MC
496 rctx->op.kex.exchprovctx
497 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
498 if (rctx->op.kex.exchprovctx == NULL) {
499 EVP_KEYEXCH_free(rctx->op.kex.exchange);
74cd923a 500 goto err;
864b89ce
MC
501 }
502 return rctx;
503 }
504 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
505 if (pctx->op.sig.signature != NULL) {
506 rctx->op.sig.signature = pctx->op.sig.signature;
ac7750bb 507 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
74cd923a 508 goto err;
864b89ce
MC
509 }
510 if (pctx->op.sig.sigprovctx != NULL) {
511 if (!ossl_assert(pctx->op.sig.signature != NULL))
74cd923a 512 goto err;
864b89ce
MC
513 rctx->op.sig.sigprovctx
514 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
515 if (rctx->op.sig.sigprovctx == NULL) {
516 EVP_SIGNATURE_free(rctx->op.sig.signature);
74cd923a 517 goto err;
864b89ce
MC
518 }
519 return rctx;
ff64702b 520 }
2c938e2e
MC
521 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
522 if (pctx->op.ciph.cipher != NULL) {
523 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
ac7750bb 524 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
74cd923a 525 goto err;
2c938e2e
MC
526 }
527 if (pctx->op.ciph.ciphprovctx != NULL) {
528 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
74cd923a 529 goto err;
2c938e2e
MC
530 rctx->op.ciph.ciphprovctx
531 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
532 if (rctx->op.ciph.ciphprovctx == NULL) {
533 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
74cd923a 534 goto err;
2c938e2e
MC
535 }
536 return rctx;
537 }
80f4fd18
SL
538 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
539 if (pctx->op.encap.kem != NULL) {
540 rctx->op.encap.kem = pctx->op.encap.kem;
ac7750bb 541 if (!EVP_KEM_up_ref(rctx->op.encap.kem))
74cd923a 542 goto err;
80f4fd18
SL
543 }
544 if (pctx->op.encap.kemprovctx != NULL) {
545 if (!ossl_assert(pctx->op.encap.kem != NULL))
74cd923a 546 goto err;
80f4fd18
SL
547 rctx->op.encap.kemprovctx
548 = pctx->op.encap.kem->dupctx(pctx->op.encap.kemprovctx);
549 if (rctx->op.encap.kemprovctx == NULL) {
550 EVP_KEM_free(rctx->op.encap.kem);
74cd923a 551 goto err;
80f4fd18
SL
552 }
553 return rctx;
554 }
ac7750bb
SL
555 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
556 /* Not supported - This would need a gen_dupctx() to work */
74cd923a 557 goto err;
ff64702b
MC
558 }
559
0f113f3e 560 rctx->pmeth = pctx->pmeth;
e683582b 561# ifndef OPENSSL_NO_ENGINE
0f113f3e 562 rctx->engine = pctx->engine;
e683582b 563# endif
8bdcef40 564
ac7750bb 565 if (pctx->peerkey != NULL)
03273d61 566 EVP_PKEY_up_ref(pctx->peerkey);
0f113f3e 567 rctx->peerkey = pctx->peerkey;
8bdcef40 568
ac7750bb
SL
569 if (pctx->pmeth == NULL) {
570 if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
571 EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
572 void *provkey;
573
574 provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
575 &tmp_keymgmt, pctx->propquery);
576 if (provkey == NULL)
577 goto err;
578 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
579 goto err;
580 EVP_KEYMGMT_free(rctx->keymgmt);
581 rctx->keymgmt = tmp_keymgmt;
582 return rctx;
583 }
74cd923a 584 } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
0f113f3e 585 return rctx;
74cd923a 586 }
ac7750bb 587err:
83b4049a 588 rctx->pmeth = NULL;
0f113f3e
MC
589 EVP_PKEY_CTX_free(rctx);
590 return NULL;
0f113f3e 591}
8bdcef40 592
ba30bad5 593int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
594{
595 if (app_pkey_methods == NULL) {
596 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
3484236d 597 if (app_pkey_methods == NULL){
9311d0c4 598 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e 599 return 0;
3484236d 600 }
0f113f3e 601 }
3484236d 602 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
9311d0c4 603 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e 604 return 0;
3484236d 605 }
0f113f3e
MC
606 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
607 return 1;
608}
ba30bad5 609
0822e89a
PY
610void evp_app_cleanup_int(void)
611{
612 if (app_pkey_methods != NULL)
613 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
614}
615
616int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
617{
618 const EVP_PKEY_METHOD *ret;
619
620 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
621
622 return ret == NULL ? 0 : 1;
623}
624
48ed9c23
DSH
625size_t EVP_PKEY_meth_get_count(void)
626{
627 size_t rv = OSSL_NELEM(standard_methods);
628
629 if (app_pkey_methods)
630 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
631 return rv;
632}
633
634const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
635{
636 if (idx < OSSL_NELEM(standard_methods))
19bd1fa1 637 return (standard_methods[idx])();
48ed9c23
DSH
638 if (app_pkey_methods == NULL)
639 return NULL;
640 idx -= OSSL_NELEM(standard_methods);
641 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
642 return NULL;
643 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
644}
e683582b 645#endif
48ed9c23 646
6179dfc7
RL
647int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
648{
649#ifndef FIPS_MODULE
650 if (evp_pkey_ctx_is_legacy(ctx))
651 return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
652#endif
653 return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
654}
655
e683582b 656int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
0f113f3e 657{
e683582b
SL
658 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
659 && ctx->op.kex.exchprovctx != NULL
660 && ctx->op.kex.exchange != NULL
661 && ctx->op.kex.exchange->set_ctx_params != NULL)
662 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
663 params);
664 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
665 && ctx->op.sig.sigprovctx != NULL
666 && ctx->op.sig.signature != NULL
667 && ctx->op.sig.signature->set_ctx_params != NULL)
668 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
669 params);
670 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
671 && ctx->op.ciph.ciphprovctx != NULL
672 && ctx->op.ciph.cipher != NULL
673 && ctx->op.ciph.cipher->set_ctx_params != NULL)
674 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
675 params);
62924755
RL
676 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
677 && ctx->op.keymgmt.genctx != NULL
678 && ctx->keymgmt != NULL
679 && ctx->keymgmt->gen_set_params != NULL)
680 return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
681 params);
80f4fd18
SL
682 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
683 && ctx->op.encap.kemprovctx != NULL
684 && ctx->op.encap.kem != NULL
685 && ctx->op.encap.kem->set_ctx_params != NULL)
686 return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.kemprovctx,
687 params);
e683582b 688 return 0;
0f113f3e 689}
5da98aa6 690
9c45222d
MC
691int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
692{
4fe54d67
NT
693 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
694 && ctx->op.kex.exchprovctx != NULL
695 && ctx->op.kex.exchange != NULL
696 && ctx->op.kex.exchange->get_ctx_params != NULL)
697 return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
698 params);
864b89ce
MC
699 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
700 && ctx->op.sig.sigprovctx != NULL
701 && ctx->op.sig.signature != NULL
702 && ctx->op.sig.signature->get_ctx_params != NULL)
703 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
704 params);
2c938e2e
MC
705 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
706 && ctx->op.ciph.ciphprovctx != NULL
707 && ctx->op.ciph.cipher != NULL
708 && ctx->op.ciph.cipher->get_ctx_params != NULL)
709 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
710 params);
80f4fd18
SL
711 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
712 && ctx->op.encap.kemprovctx != NULL
713 && ctx->op.encap.kem != NULL
714 && ctx->op.encap.kem->get_ctx_params != NULL)
715 return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.kemprovctx,
716 params);
9c45222d
MC
717 return 0;
718}
719
11a1b341 720#ifndef FIPS_MODULE
9c45222d
MC
721const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
722{
18ec26ba
P
723 void *provctx;
724
4fe54d67
NT
725 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
726 && ctx->op.kex.exchange != NULL
18ec26ba
P
727 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
728 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
729 return ctx->op.kex.exchange->gettable_ctx_params(provctx);
730 }
864b89ce
MC
731 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
732 && ctx->op.sig.signature != NULL
18ec26ba
P
733 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
734 provctx = ossl_provider_ctx(
735 EVP_SIGNATURE_provider(ctx->op.sig.signature));
736 return ctx->op.sig.signature->gettable_ctx_params(provctx);
737 }
2c938e2e
MC
738 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
739 && ctx->op.ciph.cipher != NULL
18ec26ba
P
740 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
741 provctx = ossl_provider_ctx(
742 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
743 return ctx->op.ciph.cipher->gettable_ctx_params(provctx);
744 }
80f4fd18
SL
745 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
746 && ctx->op.encap.kem != NULL
747 && ctx->op.encap.kem->gettable_ctx_params != NULL) {
748 provctx = ossl_provider_ctx(EVP_KEM_provider(ctx->op.encap.kem));
749 return ctx->op.encap.kem->gettable_ctx_params(provctx);
750 }
9c45222d
MC
751 return NULL;
752}
753
9c45222d
MC
754const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
755{
18ec26ba
P
756 void *provctx;
757
864b89ce
MC
758 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
759 && ctx->op.kex.exchange != NULL
18ec26ba
P
760 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
761 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
762 return ctx->op.kex.exchange->settable_ctx_params(provctx);
763 }
864b89ce
MC
764 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
765 && ctx->op.sig.signature != NULL
18ec26ba
P
766 && ctx->op.sig.signature->settable_ctx_params != NULL) {
767 provctx = ossl_provider_ctx(
768 EVP_SIGNATURE_provider(ctx->op.sig.signature));
769 return ctx->op.sig.signature->settable_ctx_params(provctx);
770 }
2c938e2e
MC
771 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
772 && ctx->op.ciph.cipher != NULL
18ec26ba
P
773 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
774 provctx = ossl_provider_ctx(
775 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
776 return ctx->op.ciph.cipher->settable_ctx_params(provctx);
777 }
62924755 778 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
5e77b79a 779 && ctx->keymgmt != NULL)
e3efe7a5 780 return EVP_KEYMGMT_gen_settable_params(ctx->keymgmt);
80f4fd18
SL
781 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
782 && ctx->op.encap.kem != NULL
783 && ctx->op.encap.kem->settable_ctx_params != NULL) {
784 provctx = ossl_provider_ctx(EVP_KEM_provider(ctx->op.encap.kem));
785 return ctx->op.encap.kem->settable_ctx_params(provctx);
786 }
9c45222d
MC
787 return NULL;
788}
789
4fe54d67
NT
790/*
791 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
792 *
793 * Return 1 on success, 0 or negative for errors.
794 *
795 * In particular they return -2 if any of the params is not supported.
796 *
f844f9eb 797 * They are not available in FIPS_MODULE as they depend on
4fe54d67
NT
798 * - EVP_PKEY_CTX_{get,set}_params()
799 * - EVP_PKEY_CTX_{gettable,settable}_params()
800 *
801 */
802int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
803{
804 const OSSL_PARAM *p;
805
806 if (ctx == NULL || params == NULL)
807 return 0;
808
809 for (p = params; p->key != NULL; p++) {
810 /* Check the ctx actually understands this parameter */
811 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
812 p->key) == NULL )
813 return -2;
814 }
815
816 return EVP_PKEY_CTX_set_params(ctx, params);
817}
818
819int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
820{
821 const OSSL_PARAM *p;
822
823 if (ctx == NULL || params == NULL)
824 return 0;
825
826 for (p = params; p->key != NULL; p++ ) {
827 /* Check the ctx actually understands this parameter */
828 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
829 p->key) == NULL )
830 return -2;
831 }
832
833 return EVP_PKEY_CTX_get_params(ctx, params);
834}
835
9c45222d
MC
836int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
837{
ac2d58c7 838 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
9c45222d
MC
839 /* 80 should be big enough */
840 char name[80] = "";
841 const EVP_MD *tmp;
842
864b89ce 843 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
844 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
845 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
846 return -2;
847 }
848
849 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 850 if (ctx->op.sig.sigprovctx == NULL)
9c45222d
MC
851 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
852 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
853
854 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
855 name,
856 sizeof(name));
ac2d58c7 857 *p = OSSL_PARAM_construct_end();
9c45222d
MC
858
859 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
860 return 0;
861
7606bed9 862 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
9c45222d
MC
863 if (tmp == NULL)
864 return 0;
865
866 *md = tmp;
867
868 return 1;
869}
870
05d2f72e
MC
871static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
872 int fallback, const char *param, int op,
873 int ctrl)
4889dadc 874{
05d2f72e 875 OSSL_PARAM md_params[2], *p = md_params;
4889dadc
MC
876 const char *name;
877
05d2f72e 878 if (ctx == NULL || (ctx->operation & op) == 0) {
9c45222d
MC
879 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
880 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
881 return -2;
882 }
883
4889dadc 884 /* TODO(3.0): Remove this eventually when no more legacy */
05d2f72e
MC
885 if (fallback)
886 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
4889dadc 887
9c45222d
MC
888 if (md == NULL) {
889 name = "";
9c45222d 890 } else {
9c45222d
MC
891 name = EVP_MD_name(md);
892 }
4889dadc 893
05d2f72e 894 *p++ = OSSL_PARAM_construct_utf8_string(param,
9c45222d
MC
895 /*
896 * Cast away the const. This is read
897 * only so should be safe
898 */
8b6ffd40 899 (char *)name, 0);
ac2d58c7 900 *p = OSSL_PARAM_construct_end();
4889dadc 901
05d2f72e
MC
902 return EVP_PKEY_CTX_set_params(ctx, md_params);
903}
904
905int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
906{
907 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.sigprovctx == NULL,
908 OSSL_SIGNATURE_PARAM_DIGEST,
909 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
4889dadc
MC
910}
911
ac2d58c7
MC
912int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
913{
05d2f72e
MC
914 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
915 OSSL_KDF_PARAM_DIGEST,
916 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
917}
918
919static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
920 const char *param, int op, int ctrl,
921 const unsigned char *data,
922 int datalen)
923{
924 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
ac2d58c7 925
5d51925a 926 if (ctx == NULL || (ctx->operation & op) == 0) {
ac2d58c7
MC
927 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
928 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
929 return -2;
930 }
931
932 /* TODO(3.0): Remove this eventually when no more legacy */
05d2f72e
MC
933 if (fallback)
934 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
ac2d58c7 935
05d2f72e
MC
936 if (datalen < 0) {
937 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
938 return 0;
ac2d58c7
MC
939 }
940
05d2f72e 941 *p++ = OSSL_PARAM_construct_octet_string(param,
ac2d58c7
MC
942 /*
943 * Cast away the const. This is read
944 * only so should be safe
945 */
05d2f72e
MC
946 (unsigned char *)data,
947 (size_t)datalen);
194de849 948 *p = OSSL_PARAM_construct_end();
ac2d58c7 949
05d2f72e 950 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
ac2d58c7
MC
951}
952
953int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
954 const unsigned char *sec, int seclen)
955{
05d2f72e
MC
956 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
957 OSSL_KDF_PARAM_SECRET,
958 EVP_PKEY_OP_DERIVE,
959 EVP_PKEY_CTRL_TLS_SECRET,
960 sec, seclen);
961}
ac2d58c7 962
05d2f72e
MC
963int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
964 const unsigned char *seed, int seedlen)
965{
966 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
967 OSSL_KDF_PARAM_SEED,
968 EVP_PKEY_OP_DERIVE,
969 EVP_PKEY_CTRL_TLS_SEED,
970 seed, seedlen);
971}
ac2d58c7 972
05d2f72e
MC
973int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
974{
975 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
976 OSSL_KDF_PARAM_DIGEST,
977 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
978}
ac2d58c7 979
05d2f72e
MC
980int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
981 const unsigned char *salt, int saltlen)
982{
983 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
984 OSSL_KDF_PARAM_SALT,
985 EVP_PKEY_OP_DERIVE,
986 EVP_PKEY_CTRL_HKDF_SALT,
987 salt, saltlen);
988}
ac2d58c7 989
05d2f72e
MC
990int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
991 const unsigned char *key, int keylen)
992{
993 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
994 OSSL_KDF_PARAM_KEY,
995 EVP_PKEY_OP_DERIVE,
996 EVP_PKEY_CTRL_HKDF_KEY,
997 key, keylen);
998}
ac2d58c7 999
05d2f72e
MC
1000int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1001 const unsigned char *info, int infolen)
1002{
1003 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1004 OSSL_KDF_PARAM_INFO,
1005 EVP_PKEY_OP_DERIVE,
1006 EVP_PKEY_CTRL_HKDF_INFO,
1007 info, infolen);
ac2d58c7
MC
1008}
1009
05d2f72e 1010int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
ac2d58c7 1011{
05d2f72e 1012 OSSL_PARAM int_params[2], *p = int_params;
ac2d58c7
MC
1013
1014 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1015 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1016 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1017 return -2;
1018 }
1019
1020 /* TODO(3.0): Remove this eventually when no more legacy */
1021 if (ctx->op.kex.exchprovctx == NULL)
1022 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
05d2f72e
MC
1023 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1024
ac2d58c7 1025
05d2f72e
MC
1026 if (mode < 0) {
1027 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
ac2d58c7
MC
1028 return 0;
1029 }
1030
05d2f72e 1031 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
194de849 1032 *p = OSSL_PARAM_construct_end();
ac2d58c7 1033
05d2f72e 1034 return EVP_PKEY_CTX_set_params(ctx, int_params);
ac2d58c7
MC
1035}
1036
194de849
MC
1037int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1038 int passlen)
1039{
1040 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1041 OSSL_KDF_PARAM_PASSWORD,
1042 EVP_PKEY_OP_DERIVE,
1043 EVP_PKEY_CTRL_PASS,
1044 (const unsigned char *)pass, passlen);
1045}
1046
1047int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1048 const unsigned char *salt, int saltlen)
1049{
1050 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1051 OSSL_KDF_PARAM_SALT,
1052 EVP_PKEY_OP_DERIVE,
1053 EVP_PKEY_CTRL_SCRYPT_SALT,
1054 salt, saltlen);
1055}
1056
1057static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1058 int op, int ctrl, uint64_t val)
1059{
1060 OSSL_PARAM uint64_params[2], *p = uint64_params;
1061
1062 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1063 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1064 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1065 return -2;
1066 }
1067
1068 /* TODO(3.0): Remove this eventually when no more legacy */
1069 if (ctx->op.kex.exchprovctx == NULL)
1070 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1071
1072 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1073 *p = OSSL_PARAM_construct_end();
1074
1075 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1076}
1077
1078int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1079{
1080 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1081 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1082 n);
1083}
1084
1085int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1086{
1087 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1088 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1089 r);
1090}
1091
1092int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1093{
1094 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1095 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1096 p);
1097}
1098
1099int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1100 uint64_t maxmem_bytes)
1101{
1102 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1103 EVP_PKEY_OP_DERIVE,
1104 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1105 maxmem_bytes);
1106}
1107
5d51925a
MC
1108int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1109 int keylen)
1110{
1111 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1112 OSSL_PKEY_PARAM_PRIV_KEY,
1113 EVP_PKEY_OP_KEYGEN,
1114 EVP_PKEY_CTRL_SET_MAC_KEY,
1115 key, keylen);
1116}
1117
80f4fd18
SL
1118int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1119{
1120 OSSL_PARAM params[2], *p = params;
1121
1122 if (ctx == NULL || op == NULL) {
1123 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1124 return 0;
1125 }
1126 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1127 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1128 return -2;
1129 }
1130 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1131 (char *)op, 0);
1132 *p = OSSL_PARAM_construct_end();
1133 return EVP_PKEY_CTX_set_params(ctx, params);
1134}
1135
8d6481f5
RL
1136int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
1137{
1138 OSSL_PARAM params[2], *p = params;
1139 int ret;
1140
1141 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1142 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1143 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1144 return -2;
1145 }
1146
1147 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
1148 /*
1149 * Cast away the const. This is
1150 * read only so should be safe
1151 */
1152 (void *)id, (size_t)len);
1153 *p++ = OSSL_PARAM_construct_end();
1154
1155 ret = evp_pkey_ctx_set_params_strict(ctx, params);
1156 if (ret == -2)
1157 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1158 return ret;
1159}
1160
1161int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1162{
1163 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1164 EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1165}
1166
1167static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
1168{
1169 int ret;
1170 void *tmp_id = NULL;
1171 OSSL_PARAM params[2], *p = params;
1172
1173 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1174 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1175 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1176 return -2;
1177 }
1178
1179 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
1180 &tmp_id, 0);
1181 *p++ = OSSL_PARAM_construct_end();
1182
1183 ret = evp_pkey_ctx_get_params_strict(ctx, params);
1184 if (ret == -2) {
1185 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1186 } else if (ret > 0) {
1187 size_t tmp_id_len = params[0].return_size;
1188
1189 if (id != NULL)
1190 memcpy(id, tmp_id, tmp_id_len);
1191 if (id_len != NULL)
1192 *id_len = tmp_id_len;
1193 }
1194 return ret;
1195}
1196
1197int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
1198{
1199 return get1_id_data(ctx, id, NULL);
1200}
1201
1202int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
1203{
1204 return get1_id_data(ctx, NULL, id_len);
1205}
1206
1207int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1208{
1209 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1210}
1211
1212int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1213{
1214 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1215 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1216}
1217
35aca9ec
MC
1218static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
1219 int cmd, int p1, void *p2)
1220{
86df26b3
RL
1221 switch (cmd) {
1222 case EVP_PKEY_CTRL_SET1_ID:
1223 return evp_pkey_ctx_set1_id_prov(ctx, p2, p1);
1224 case EVP_PKEY_CTRL_GET1_ID:
1225 return evp_pkey_ctx_get1_id_prov(ctx, p2);
1226 case EVP_PKEY_CTRL_GET1_ID_LEN:
1227 return evp_pkey_ctx_get1_id_len_prov(ctx, p2);
1228 }
1229
116d2510
SL
1230 if (keytype == EVP_PKEY_DHX) {
1231 switch (cmd) {
1232 case EVP_PKEY_CTRL_DH_KDF_TYPE:
1233 return EVP_PKEY_CTX_set_dh_kdf_type(ctx, p1);
1234 case EVP_PKEY_CTRL_DH_KDF_MD:
1235 return EVP_PKEY_CTX_set_dh_kdf_md(ctx, p2);
1236 case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
1237 return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, p1);
1238 case EVP_PKEY_CTRL_DH_KDF_UKM:
1239 return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p2, p1);
1240 case EVP_PKEY_CTRL_DH_KDF_OID:
1241 return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, p2);
1242 case EVP_PKEY_CTRL_GET_DH_KDF_MD:
1243 return EVP_PKEY_CTX_get_dh_kdf_md(ctx, p2);
1244 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
1245 return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, p2);
1246 case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
1247 return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p2);
1248 case EVP_PKEY_CTRL_GET_DH_KDF_OID:
1249 return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, p2);
1250 }
1251 }
4fe54d67
NT
1252 if (keytype == EVP_PKEY_DH) {
1253 switch (cmd) {
1254 case EVP_PKEY_CTRL_DH_PAD:
1255 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
7165593c
SL
1256 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
1257 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1);
1258 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
1259 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1);
1260 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
1261 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1);
1262 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
1263 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1);
1264 case EVP_PKEY_CTRL_DH_RFC5114:
1265 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1);
4fe54d67
NT
1266 }
1267 }
b03ec3b5
SL
1268 if (keytype == EVP_PKEY_DSA) {
1269 switch (cmd) {
1270 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
1271 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1);
1272 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
1273 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1);
1274 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
1275 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2);
1276 }
1277 }
4fe54d67
NT
1278 if (keytype == EVP_PKEY_EC) {
1279 switch (cmd) {
7229a2f4
RL
1280 case EVP_PKEY_CTRL_EC_PARAM_ENC:
1281 return evp_pkey_ctx_set_ec_param_enc_prov(ctx, p1);
10d756a7
RL
1282 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
1283 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
6f4b7663
RL
1284 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
1285 if (p1 == -2) {
1286 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
1287 } else if (p1 < -1 || p1 > 1) {
1288 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1289 return -2;
1290 } else {
1291 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
1292 }
1293 case EVP_PKEY_CTRL_EC_KDF_TYPE:
1294 if (p1 == -2) {
1295 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
1296 } else {
1297 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
1298 }
1299 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
1300 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
1301 case EVP_PKEY_CTRL_EC_KDF_MD:
1302 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
1303 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
1304 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
1305 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
1306 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
1307 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
1308 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
1309 case EVP_PKEY_CTRL_EC_KDF_UKM:
1310 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
4fe54d67
NT
1311 }
1312 }
2decdad3
RL
1313 if (keytype == EVP_PKEY_RSA) {
1314 switch (cmd) {
1315 case EVP_PKEY_CTRL_RSA_OAEP_MD:
1316 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1317 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
1318 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1319 case EVP_PKEY_CTRL_RSA_MGF1_MD:
1320 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1321 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
1322 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
1323 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
1324 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
1325 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
1326 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
1327 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
1328 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
1329 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
1330 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
1331 }
1332 }
e947a064
DB
1333
1334 if (keytype == EVP_PKEY_RSA_PSS) {
1335 switch(cmd) {
1336 case EVP_PKEY_CTRL_MD:
1337 return EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, p2);
1338 }
1339 }
1340
2decdad3
RL
1341 /*
1342 * keytype == -1 is used when several key types share the same structure,
1343 * or for generic controls that are the same across multiple key types.
1344 */
4fe54d67 1345 if (keytype == -1) {
ac2d58c7
MC
1346 if (optype == EVP_PKEY_OP_DERIVE) {
1347 switch (cmd) {
05d2f72e 1348 /* TLS1-PRF */
ac2d58c7
MC
1349 case EVP_PKEY_CTRL_TLS_MD:
1350 return EVP_PKEY_CTX_set_tls1_prf_md(ctx, p2);
1351 case EVP_PKEY_CTRL_TLS_SECRET:
1352 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx, p2, p1);
1353 case EVP_PKEY_CTRL_TLS_SEED:
1354 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx, p2, p1);
05d2f72e
MC
1355
1356 /* HKDF */
1357 case EVP_PKEY_CTRL_HKDF_MD:
1358 return EVP_PKEY_CTX_set_hkdf_md(ctx, p2);
1359 case EVP_PKEY_CTRL_HKDF_SALT :
1360 return EVP_PKEY_CTX_set1_hkdf_salt(ctx, p2, p1);
1361 case EVP_PKEY_CTRL_HKDF_KEY:
1362 return EVP_PKEY_CTX_set1_hkdf_key(ctx, p2, p1);
1363 case EVP_PKEY_CTRL_HKDF_INFO:
1364 return EVP_PKEY_CTX_add1_hkdf_info(ctx, p2, p1);
1365 case EVP_PKEY_CTRL_HKDF_MODE:
1366 return EVP_PKEY_CTX_hkdf_mode(ctx, p1);
194de849
MC
1367
1368 /* Scrypt */
1369 case EVP_PKEY_CTRL_PASS:
1370 return EVP_PKEY_CTX_set1_pbe_pass(ctx, p2, p1);
1371 case EVP_PKEY_CTRL_SCRYPT_SALT:
1372 return EVP_PKEY_CTX_set1_scrypt_salt(ctx, p2, p1);
1373 case EVP_PKEY_CTRL_SCRYPT_N:
1374 return EVP_PKEY_CTX_set_scrypt_N(ctx, p1);
1375 case EVP_PKEY_CTRL_SCRYPT_R:
1376 return EVP_PKEY_CTX_set_scrypt_r(ctx, p1);
1377 case EVP_PKEY_CTRL_SCRYPT_P:
1378 return EVP_PKEY_CTX_set_scrypt_p(ctx, p1);
1379 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
1380 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx, p1);
ac2d58c7 1381 }
a540ef90
MC
1382 } else if (optype == EVP_PKEY_OP_KEYGEN) {
1383 OSSL_PARAM params[2], *p = params;
1384
1385 switch (cmd) {
1386 case EVP_PKEY_CTRL_CIPHER:
1387 {
1388 char *ciphname = (char *)EVP_CIPHER_name(p2);
1389
1390 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
1391 ciphname, 0);
1392 *p = OSSL_PARAM_construct_end();
1393
1394 return EVP_PKEY_CTX_set_params(ctx, params);
1395 }
1396 case EVP_PKEY_CTRL_SET_MAC_KEY:
1397 {
1398 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
1399 p2, p1);
1400 *p = OSSL_PARAM_construct_end();
1401
1402 return EVP_PKEY_CTX_set_params(ctx, params);
1403 }
1404 }
ac2d58c7 1405 }
4fe54d67 1406 switch (cmd) {
6f4b7663
RL
1407 case EVP_PKEY_CTRL_MD:
1408 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
1409 case EVP_PKEY_CTRL_GET_MD:
1410 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
1411 case EVP_PKEY_CTRL_RSA_PADDING:
1412 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
1413 case EVP_PKEY_CTRL_GET_RSA_PADDING:
1414 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
6f4b7663
RL
1415 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
1416 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
6f4b7663
RL
1417 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
1418 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
1419 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
1420 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
1421 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
1422 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
e683582b 1423# ifndef OPENSSL_NO_CMS
6f4b7663
RL
1424 case EVP_PKEY_CTRL_CMS_DECRYPT:
1425 case EVP_PKEY_CTRL_CMS_ENCRYPT:
e683582b 1426# endif
cc572c25
RL
1427 /* TODO (3.0) Temporary hack, this should probe */
1428 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
6f4b7663
RL
1429 return 1;
1430 ERR_raise(ERR_LIB_EVP,
1431 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1432 return -2;
4fe54d67 1433 }
35aca9ec 1434 }
a540ef90
MC
1435
1436 /*
1437 * GOST CMS format is different for different cipher algorithms.
1438 * Most of other algorithms don't have such a difference
1439 * so this ctrl is just ignored.
1440 */
1441 if (cmd == EVP_PKEY_CTRL_CIPHER)
1442 return -2;
1443
35aca9ec
MC
1444 return 0;
1445}
1446
86df26b3
RL
1447static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1448 int cmd, int p1, void *p2)
0f113f3e 1449{
86df26b3 1450 int ret = 0;
4803717f 1451
86df26b3
RL
1452 /*
1453 * If the method has a |digest_custom| function, we can relax the
1454 * operation type check, since this can be called before the operation
1455 * is initialized.
1456 */
1457 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1458 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
9311d0c4 1459 ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
86df26b3
RL
1460 return -1;
1461 }
35aca9ec 1462
86df26b3 1463 if ((optype != -1) && !(ctx->operation & optype)) {
9311d0c4 1464 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
86df26b3
RL
1465 return -1;
1466 }
0f113f3e 1467 }
0f113f3e 1468
86df26b3
RL
1469 switch (evp_pkey_ctx_state(ctx)) {
1470 case EVP_PKEY_STATE_PROVIDER:
1471 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1472 case EVP_PKEY_STATE_UNKNOWN:
1473 case EVP_PKEY_STATE_LEGACY:
1474 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
9311d0c4 1475 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3
RL
1476 return -2;
1477 }
1478 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1479 return -1;
4803717f 1480
86df26b3 1481 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
0f113f3e 1482
86df26b3 1483 if (ret == -2)
9311d0c4 1484 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3 1485 break;
0f113f3e 1486 }
86df26b3
RL
1487 return ret;
1488}
0f113f3e 1489
86df26b3
RL
1490int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1491 int cmd, int p1, void *p2)
1492{
1493 int ret = 0;
1494
d65ab22e 1495 if (ctx == NULL) {
9311d0c4 1496 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
d65ab22e
SL
1497 return -2;
1498 }
86df26b3
RL
1499 /* If unsupported, we don't want that reported here */
1500 ERR_set_mark();
1501 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1502 cmd, NULL, p2, p1);
1503 if (ret == -2) {
1504 ERR_pop_to_mark();
1505 } else {
1506 ERR_clear_last_mark();
1507 /*
1508 * If there was an error, there was an error.
1509 * If the operation isn't initialized yet, we also return, as
1510 * the saved values will be used then anyway.
1511 */
1512 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1513 return ret;
1514 }
86df26b3 1515 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
0f113f3e 1516}
0b6f3c66 1517
cefa762e 1518int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 1519 int cmd, uint64_t value)
cefa762e
JB
1520{
1521 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1522}
1523
35aca9ec
MC
1524static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
1525 const char *value)
1526{
ac2d58c7
MC
1527 if (strcmp(name, "md") == 0)
1528 name = OSSL_ALG_PARAM_DIGEST;
1529 else if (strcmp(name, "rsa_padding_mode") == 0)
972fa318
RL
1530 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1531 else if (strcmp(name, "rsa_mgf1_md") == 0)
1532 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1533 else if (strcmp(name, "rsa_oaep_md") == 0)
1534 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1535 else if (strcmp(name, "rsa_oaep_label") == 0)
1536 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
6f4b7663
RL
1537 else if (strcmp(name, "rsa_pss_saltlen") == 0)
1538 name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
2972af10
RL
1539 else if (strcmp(name, "rsa_keygen_bits") == 0)
1540 name = OSSL_PKEY_PARAM_RSA_BITS;
1541 else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1542 name = OSSL_PKEY_PARAM_RSA_E;
1543 else if (strcmp(name, "rsa_keygen_primes") == 0)
1544 name = OSSL_PKEY_PARAM_RSA_PRIMES;
e25761b1
RL
1545 else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1546 name = OSSL_PKEY_PARAM_RSA_DIGEST;
1547 else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1548 name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1549 else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1550 name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
b03ec3b5
SL
1551 else if (strcmp(name, "dsa_paramgen_bits") == 0)
1552 name = OSSL_PKEY_PARAM_FFC_PBITS;
1553 else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1554 name = OSSL_PKEY_PARAM_FFC_QBITS;
1555 else if (strcmp(name, "dsa_paramgen_md") == 0)
1556 name = OSSL_PKEY_PARAM_FFC_DIGEST;
7165593c 1557 else if (strcmp(name, "dh_paramgen_generator") == 0)
b8086652 1558 name = OSSL_PKEY_PARAM_DH_GENERATOR;
7165593c
SL
1559 else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1560 name = OSSL_PKEY_PARAM_FFC_PBITS;
1561 else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1562 name = OSSL_PKEY_PARAM_FFC_QBITS;
1563 else if (strcmp(name, "dh_paramgen_type") == 0) {
1564 name = OSSL_PKEY_PARAM_FFC_TYPE;
1565 value = dh_gen_type_id2name(atoi(value));
1566 } else if (strcmp(name, "dh_param") == 0)
023b188c 1567 name = OSSL_PKEY_PARAM_GROUP_NAME;
7165593c 1568 else if (strcmp(name, "dh_rfc5114") == 0) {
c829c23b
RL
1569 int num = atoi(value);
1570
023b188c 1571 name = OSSL_PKEY_PARAM_GROUP_NAME;
c829c23b
RL
1572 value =
1573 ossl_ffc_named_group_get_name(ossl_ffc_uid_to_dh_named_group(num));
7165593c 1574 } else if (strcmp(name, "dh_pad") == 0)
972fa318 1575 name = OSSL_EXCHANGE_PARAM_PAD;
10d756a7 1576 else if (strcmp(name, "ec_paramgen_curve") == 0)
11a1b341 1577 name = OSSL_PKEY_PARAM_GROUP_NAME;
4fe54d67
NT
1578 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1579 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1580 else if (strcmp(name, "ecdh_kdf_md") == 0)
f552d900 1581 name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
c0f39ded
SL
1582 else if (strcmp(name, "ec_param_enc") == 0)
1583 name = OSSL_PKEY_PARAM_EC_ENCODING;
194de849
MC
1584 else if (strcmp(name, "N") == 0)
1585 name = OSSL_KDF_PARAM_SCRYPT_N;
89abd1b6 1586
972fa318
RL
1587 {
1588 /*
1589 * TODO(3.0) reduce the code above to only translate known legacy
1590 * string to the corresponding core name (see core_names.h), but
1591 * otherwise leave it to this code block to do the actual work.
1592 */
1593 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1594 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1595 int rv = 0;
292c8bdc 1596 int exists = 0;
89abd1b6 1597
972fa318 1598 if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
292c8bdc
P
1599 strlen(value), &exists)) {
1600 if (!exists) {
e25761b1
RL
1601 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1602 "name=%s,value=%s", name, value);
292c8bdc
P
1603 return -2;
1604 }
89abd1b6 1605 return 0;
292c8bdc 1606 }
972fa318
RL
1607 if (EVP_PKEY_CTX_set_params(ctx, params))
1608 rv = 1;
1609 OPENSSL_free(params[0].data);
1610 return rv;
89abd1b6 1611 }
35aca9ec
MC
1612}
1613
86df26b3
RL
1614static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1615 const char *name, const char *value)
0f113f3e 1616{
86df26b3
RL
1617 int ret = 0;
1618
35aca9ec 1619 if (ctx == NULL) {
9311d0c4 1620 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
35aca9ec
MC
1621 return -2;
1622 }
1623
86df26b3
RL
1624 switch (evp_pkey_ctx_state(ctx)) {
1625 case EVP_PKEY_STATE_PROVIDER:
35aca9ec 1626 return legacy_ctrl_str_to_param(ctx, name, value);
86df26b3
RL
1627 case EVP_PKEY_STATE_UNKNOWN:
1628 case EVP_PKEY_STATE_LEGACY:
1629 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
9311d0c4 1630 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3
RL
1631 return -2;
1632 }
1633 if (strcmp(name, "digest") == 0)
b9689452
RL
1634 ret = EVP_PKEY_CTX_md(ctx,
1635 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
86df26b3
RL
1636 EVP_PKEY_CTRL_MD, value);
1637 else
1638 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1639 break;
1640 }
35aca9ec 1641
86df26b3
RL
1642 return ret;
1643}
1644
1645int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1646 const char *name, const char *value)
1647{
1648 int ret = 0;
1649
1650 /* If unsupported, we don't want that reported here */
1651 ERR_set_mark();
1652 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1653 name, value, strlen(value) + 1);
1654 if (ret == -2) {
1655 ERR_pop_to_mark();
1656 } else {
1657 ERR_clear_last_mark();
1658 /*
1659 * If there was an error, there was an error.
1660 * If the operation isn't initialized yet, we also return, as
1661 * the saved values will be used then anyway.
1662 */
1663 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1664 return ret;
1665 }
1666
1667 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1668}
1669
1670static int decode_cmd(int cmd, const char *name)
1671{
1672 if (cmd == -1) {
1673 /*
1674 * The consequence of the assertion not being true is that this
1675 * function will return -1, which will cause the calling functions
1676 * to signal that the command is unsupported... in non-debug mode.
1677 */
1678 if (ossl_assert(name != NULL))
1679 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1680 cmd = EVP_PKEY_CTRL_SET1_ID;
1681 }
1682
1683 return cmd;
1684}
1685
1686static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1687 int keytype, int optype,
1688 int cmd, const char *name,
1689 const void *data, size_t data_len)
1690{
977e95b9
RL
1691 if (keytype != -1) {
1692 switch (evp_pkey_ctx_state(ctx)) {
1693 case EVP_PKEY_STATE_PROVIDER:
1694 if (ctx->keymgmt == NULL) {
1695 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1696 return -2;
1697 }
1698 if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1699 evp_pkey_type2name(keytype))) {
1700 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1701 return -1;
1702 }
1703 break;
1704 case EVP_PKEY_STATE_UNKNOWN:
1705 case EVP_PKEY_STATE_LEGACY:
1706 if (ctx->pmeth == NULL) {
1707 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1708 return -2;
1709 }
1710 if (ctx->pmeth->pkey_id != keytype) {
1711 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1712 return -1;
1713 }
1714 break;
1715 }
1716 }
1717 if (optype != -1 && (ctx->operation & optype) == 0) {
86df26b3
RL
1718 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1719 return -1;
0f113f3e 1720 }
86df26b3
RL
1721
1722 cmd = decode_cmd(cmd, name);
1723 switch (cmd) {
1724 case EVP_PKEY_CTRL_SET1_ID:
1725 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1726 if (name != NULL) {
1727 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1728 if (ctx->cached_parameters.dist_id_name == NULL) {
1729 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1730 return 0;
1731 }
1732 }
1733 if (data_len > 0) {
1734 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1735 if (ctx->cached_parameters.dist_id == NULL) {
1736 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1737 return 0;
1738 }
1739 }
1740 ctx->cached_parameters.dist_id_set = 1;
1741 ctx->cached_parameters.dist_id_len = data_len;
1742 return 1;
1743 }
1744
1745 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1746 return -2;
1747}
1748
1749static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1750 int cmd, const char *name)
1751{
1752 cmd = decode_cmd(cmd, name);
1753 switch (cmd) {
1754 case EVP_PKEY_CTRL_SET1_ID:
1755 OPENSSL_free(ctx->cached_parameters.dist_id);
1756 OPENSSL_free(ctx->cached_parameters.dist_id_name);
1757 ctx->cached_parameters.dist_id = NULL;
1758 ctx->cached_parameters.dist_id_name = NULL;
1759 break;
1760 }
1761}
1762
1763static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1764{
1765 evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1766}
1767
1768int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1769{
1770 int ret = 1;
1771
1772 if (ret && ctx->cached_parameters.dist_id_set) {
1773 const char *name = ctx->cached_parameters.dist_id_name;
1774 const void *val = ctx->cached_parameters.dist_id;
1775 size_t len = ctx->cached_parameters.dist_id_len;
1776
1777 if (name != NULL)
1778 ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1779 else
1780 ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1781 EVP_PKEY_CTRL_SET1_ID,
1782 (int)len, (void *)val);
1783 }
1784
1785 return ret;
0f113f3e 1786}
f5cda4cb 1787
b4250010 1788OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
0b3a4ef2
MC
1789{
1790 return ctx->libctx;
1791}
1792
29000e43 1793const char *EVP_PKEY_CTX_get0_propq(EVP_PKEY_CTX *ctx)
0b3a4ef2
MC
1794{
1795 return ctx->propquery;
1796}
1797
99119000
DSH
1798/* Utility functions to send a string of hex string to a ctrl */
1799
1800int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1801{
1802 size_t len;
1803
1804 len = strlen(str);
1805 if (len > INT_MAX)
1806 return -1;
1807 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1808}
1809
1810int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1811{
1812 unsigned char *bin;
1813 long binlen;
1814 int rv = -1;
1815
14f051a0 1816 bin = OPENSSL_hexstr2buf(hex, &binlen);
99119000
DSH
1817 if (bin == NULL)
1818 return 0;
1819 if (binlen <= INT_MAX)
1820 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1821 OPENSSL_free(bin);
1822 return rv;
1823}
52ad523c 1824
410877ba
DSH
1825/* Pass a message digest to a ctrl */
1826int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1827{
1828 const EVP_MD *m;
c82bafc5 1829
410877ba 1830 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
9311d0c4 1831 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
410877ba
DSH
1832 return 0;
1833 }
1834 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1835}
99119000 1836
b28dea4e 1837int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1838{
1839 return ctx->operation;
1840}
b28dea4e
DSH
1841
1842void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
0f113f3e
MC
1843{
1844 ctx->keygen_info = dat;
1845 ctx->keygen_info_count = datlen;
1846}
b28dea4e 1847
f5cda4cb 1848void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1849{
1850 ctx->data = data;
1851}
f5cda4cb 1852
9fdcc21f 1853void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
0f113f3e
MC
1854{
1855 return ctx->data;
1856}
f5cda4cb 1857
81cebb8b 1858EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1859{
1860 return ctx->pkey;
1861}
81cebb8b 1862
0e1dba93 1863EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1864{
1865 return ctx->peerkey;
1866}
1867
f5cda4cb 1868void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1869{
1870 ctx->app_data = data;
1871}
f5cda4cb
DSH
1872
1873void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1874{
1875 return ctx->app_data;
1876}
ba30bad5
DSH
1877
1878void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1879 int (*init) (EVP_PKEY_CTX *ctx))
1880{
1881 pmeth->init = init;
1882}
8bdcef40
DSH
1883
1884void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
0f113f3e 1885 int (*copy) (EVP_PKEY_CTX *dst,
9fdcc21f 1886 const EVP_PKEY_CTX *src))
0f113f3e
MC
1887{
1888 pmeth->copy = copy;
1889}
ba30bad5
DSH
1890
1891void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1892 void (*cleanup) (EVP_PKEY_CTX *ctx))
1893{
1894 pmeth->cleanup = cleanup;
1895}
ba30bad5
DSH
1896
1897void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1898 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1899 int (*paramgen) (EVP_PKEY_CTX *ctx,
1900 EVP_PKEY *pkey))
1901{
1902 pmeth->paramgen_init = paramgen_init;
1903 pmeth->paramgen = paramgen;
1904}
ba30bad5
DSH
1905
1906void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1907 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1908 int (*keygen) (EVP_PKEY_CTX *ctx,
1909 EVP_PKEY *pkey))
1910{
1911 pmeth->keygen_init = keygen_init;
1912 pmeth->keygen = keygen;
1913}
ba30bad5
DSH
1914
1915void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1916 int (*sign_init) (EVP_PKEY_CTX *ctx),
1917 int (*sign) (EVP_PKEY_CTX *ctx,
1918 unsigned char *sig, size_t *siglen,
1919 const unsigned char *tbs,
1920 size_t tbslen))
1921{
1922 pmeth->sign_init = sign_init;
1923 pmeth->sign = sign;
1924}
ba30bad5
DSH
1925
1926void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1927 int (*verify_init) (EVP_PKEY_CTX *ctx),
1928 int (*verify) (EVP_PKEY_CTX *ctx,
1929 const unsigned char *sig,
1930 size_t siglen,
1931 const unsigned char *tbs,
1932 size_t tbslen))
1933{
1934 pmeth->verify_init = verify_init;
1935 pmeth->verify = verify;
1936}
ba30bad5
DSH
1937
1938void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1939 int (*verify_recover_init) (EVP_PKEY_CTX
1940 *ctx),
1941 int (*verify_recover) (EVP_PKEY_CTX
1942 *ctx,
1943 unsigned char
1944 *sig,
1945 size_t *siglen,
1946 const unsigned
1947 char *tbs,
1948 size_t tbslen))
1949{
1950 pmeth->verify_recover_init = verify_recover_init;
1951 pmeth->verify_recover = verify_recover;
1952}
ba30bad5
DSH
1953
1954void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1955 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1956 EVP_MD_CTX *mctx),
1957 int (*signctx) (EVP_PKEY_CTX *ctx,
1958 unsigned char *sig,
1959 size_t *siglen,
1960 EVP_MD_CTX *mctx))
1961{
1962 pmeth->signctx_init = signctx_init;
1963 pmeth->signctx = signctx;
1964}
ba30bad5
DSH
1965
1966void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1967 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1968 EVP_MD_CTX *mctx),
1969 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1970 const unsigned char *sig,
1971 int siglen,
1972 EVP_MD_CTX *mctx))
1973{
1974 pmeth->verifyctx_init = verifyctx_init;
1975 pmeth->verifyctx = verifyctx;
1976}
ba30bad5
DSH
1977
1978void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1979 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1980 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1981 unsigned char *out,
1982 size_t *outlen,
1983 const unsigned char *in,
1984 size_t inlen))
1985{
1986 pmeth->encrypt_init = encrypt_init;
1987 pmeth->encrypt = encryptfn;
1988}
ba30bad5
DSH
1989
1990void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1991 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1992 int (*decrypt) (EVP_PKEY_CTX *ctx,
1993 unsigned char *out,
1994 size_t *outlen,
1995 const unsigned char *in,
1996 size_t inlen))
1997{
1998 pmeth->decrypt_init = decrypt_init;
1999 pmeth->decrypt = decrypt;
2000}
ba30bad5
DSH
2001
2002void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
2003 int (*derive_init) (EVP_PKEY_CTX *ctx),
2004 int (*derive) (EVP_PKEY_CTX *ctx,
2005 unsigned char *key,
2006 size_t *keylen))
2007{
2008 pmeth->derive_init = derive_init;
2009 pmeth->derive = derive;
2010}
ba30bad5
DSH
2011
2012void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
2013 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2014 void *p2),
2015 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
2016 const char *type,
2017 const char *value))
2018{
2019 pmeth->ctrl = ctrl;
2020 pmeth->ctrl_str = ctrl_str;
2021}
e7451ed1 2022
2555285f
AH
2023void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
2024 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2025 const unsigned char *tbs, size_t tbslen))
2026{
2027 pmeth->digestsign = digestsign;
2028}
2029
2030void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
2031 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2032 size_t siglen, const unsigned char *tbs,
2033 size_t tbslen))
2034{
2035 pmeth->digestverify = digestverify;
2036}
2037
2aee35d3
PY
2038void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
2039 int (*check) (EVP_PKEY *pkey))
2040{
2041 pmeth->check = check;
2042}
2043
b0004708
PY
2044void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
2045 int (*check) (EVP_PKEY *pkey))
2046{
2047 pmeth->public_check = check;
2048}
2049
2050void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
2051 int (*check) (EVP_PKEY *pkey))
2052{
2053 pmeth->param_check = check;
2054}
2055
0a8fdef7
PY
2056void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
2057 int (*digest_custom) (EVP_PKEY_CTX *ctx,
2058 EVP_MD_CTX *mctx))
2059{
2060 pmeth->digest_custom = digest_custom;
2061}
2062
693be9a2 2063void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2064 int (**pinit) (EVP_PKEY_CTX *ctx))
2065{
2066 *pinit = pmeth->init;
2067}
2068
693be9a2 2069void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
e7451ed1 2070 int (**pcopy) (EVP_PKEY_CTX *dst,
9fdcc21f 2071 const EVP_PKEY_CTX *src))
e7451ed1
DSH
2072{
2073 *pcopy = pmeth->copy;
2074}
2075
693be9a2 2076void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2077 void (**pcleanup) (EVP_PKEY_CTX *ctx))
2078{
2079 *pcleanup = pmeth->cleanup;
2080}
2081
693be9a2 2082void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2083 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
2084 int (**pparamgen) (EVP_PKEY_CTX *ctx,
2085 EVP_PKEY *pkey))
2086{
2087 if (pparamgen_init)
2088 *pparamgen_init = pmeth->paramgen_init;
2089 if (pparamgen)
2090 *pparamgen = pmeth->paramgen;
2091}
2092
693be9a2 2093void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2094 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
2095 int (**pkeygen) (EVP_PKEY_CTX *ctx,
2096 EVP_PKEY *pkey))
2097{
2098 if (pkeygen_init)
2099 *pkeygen_init = pmeth->keygen_init;
2100 if (pkeygen)
2101 *pkeygen = pmeth->keygen;
2102}
2103
693be9a2 2104void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2105 int (**psign_init) (EVP_PKEY_CTX *ctx),
2106 int (**psign) (EVP_PKEY_CTX *ctx,
2107 unsigned char *sig, size_t *siglen,
2108 const unsigned char *tbs,
2109 size_t tbslen))
2110{
2111 if (psign_init)
2112 *psign_init = pmeth->sign_init;
2113 if (psign)
2114 *psign = pmeth->sign;
2115}
2116
693be9a2 2117void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2118 int (**pverify_init) (EVP_PKEY_CTX *ctx),
2119 int (**pverify) (EVP_PKEY_CTX *ctx,
2120 const unsigned char *sig,
2121 size_t siglen,
2122 const unsigned char *tbs,
2123 size_t tbslen))
2124{
2125 if (pverify_init)
2126 *pverify_init = pmeth->verify_init;
2127 if (pverify)
2128 *pverify = pmeth->verify;
2129}
2130
693be9a2 2131void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2132 int (**pverify_recover_init) (EVP_PKEY_CTX
2133 *ctx),
2134 int (**pverify_recover) (EVP_PKEY_CTX
2135 *ctx,
2136 unsigned char
2137 *sig,
2138 size_t *siglen,
2139 const unsigned
2140 char *tbs,
2141 size_t tbslen))
2142{
2143 if (pverify_recover_init)
2144 *pverify_recover_init = pmeth->verify_recover_init;
2145 if (pverify_recover)
2146 *pverify_recover = pmeth->verify_recover;
2147}
2148
693be9a2 2149void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2150 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
2151 EVP_MD_CTX *mctx),
2152 int (**psignctx) (EVP_PKEY_CTX *ctx,
2153 unsigned char *sig,
2154 size_t *siglen,
2155 EVP_MD_CTX *mctx))
2156{
2157 if (psignctx_init)
2158 *psignctx_init = pmeth->signctx_init;
2159 if (psignctx)
2160 *psignctx = pmeth->signctx;
2161}
2162
693be9a2 2163void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2164 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
2165 EVP_MD_CTX *mctx),
2166 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
2167 const unsigned char *sig,
2168 int siglen,
2169 EVP_MD_CTX *mctx))
2170{
2171 if (pverifyctx_init)
2172 *pverifyctx_init = pmeth->verifyctx_init;
2173 if (pverifyctx)
2174 *pverifyctx = pmeth->verifyctx;
2175}
2176
693be9a2 2177void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2178 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
2179 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
2180 unsigned char *out,
2181 size_t *outlen,
2182 const unsigned char *in,
2183 size_t inlen))
2184{
2185 if (pencrypt_init)
2186 *pencrypt_init = pmeth->encrypt_init;
2187 if (pencryptfn)
2188 *pencryptfn = pmeth->encrypt;
2189}
2190
693be9a2 2191void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2192 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
2193 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
2194 unsigned char *out,
2195 size_t *outlen,
2196 const unsigned char *in,
2197 size_t inlen))
2198{
2199 if (pdecrypt_init)
2200 *pdecrypt_init = pmeth->decrypt_init;
2201 if (pdecrypt)
2202 *pdecrypt = pmeth->decrypt;
2203}
2204
693be9a2 2205void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2206 int (**pderive_init) (EVP_PKEY_CTX *ctx),
2207 int (**pderive) (EVP_PKEY_CTX *ctx,
2208 unsigned char *key,
2209 size_t *keylen))
2210{
2211 if (pderive_init)
2212 *pderive_init = pmeth->derive_init;
2213 if (pderive)
2214 *pderive = pmeth->derive;
2215}
2216
693be9a2 2217void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2218 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2219 void *p2),
2220 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2221 const char *type,
2222 const char *value))
2223{
2224 if (pctrl)
2225 *pctrl = pmeth->ctrl;
2226 if (pctrl_str)
2227 *pctrl_str = pmeth->ctrl_str;
2228}
2aee35d3 2229
2555285f
AH
2230void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
2231 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2232 const unsigned char *tbs, size_t tbslen))
2233{
2234 if (digestsign)
2235 *digestsign = pmeth->digestsign;
2236}
2237
2238void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
2239 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2240 size_t siglen, const unsigned char *tbs,
2241 size_t tbslen))
2242{
2243 if (digestverify)
2244 *digestverify = pmeth->digestverify;
2245}
2246
693be9a2 2247void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2aee35d3
PY
2248 int (**pcheck) (EVP_PKEY *pkey))
2249{
34f5c8b1 2250 if (pcheck != NULL)
2aee35d3
PY
2251 *pcheck = pmeth->check;
2252}
b0004708 2253
693be9a2 2254void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
2255 int (**pcheck) (EVP_PKEY *pkey))
2256{
34f5c8b1 2257 if (pcheck != NULL)
b0004708
PY
2258 *pcheck = pmeth->public_check;
2259}
2260
693be9a2 2261void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
2262 int (**pcheck) (EVP_PKEY *pkey))
2263{
34f5c8b1 2264 if (pcheck != NULL)
b0004708
PY
2265 *pcheck = pmeth->param_check;
2266}
0a8fdef7
PY
2267
2268void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
2269 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2270 EVP_MD_CTX *mctx))
2271{
675f4cee 2272 if (pdigest_custom != NULL)
0a8fdef7
PY
2273 *pdigest_custom = pmeth->digest_custom;
2274}
e683582b 2275
f844f9eb 2276#endif /* FIPS_MODULE */