]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
Fix external symbols related to dh keys
[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
19dbb742 54 ossl_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
19dbb742 64 ossl_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{
6fcd92d3
RL
658 switch (evp_pkey_ctx_state(ctx)) {
659 case EVP_PKEY_STATE_PROVIDER:
660 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
e683582b
SL
661 && ctx->op.kex.exchange != NULL
662 && ctx->op.kex.exchange->set_ctx_params != NULL)
6fcd92d3
RL
663 return
664 ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
665 params);
666 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
e683582b
SL
667 && ctx->op.sig.signature != NULL
668 && ctx->op.sig.signature->set_ctx_params != NULL)
6fcd92d3
RL
669 return
670 ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
671 params);
672 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
e683582b
SL
673 && ctx->op.ciph.cipher != NULL
674 && ctx->op.ciph.cipher->set_ctx_params != NULL)
6fcd92d3
RL
675 return
676 ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
677 params);
678 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
679 && ctx->keymgmt != NULL
680 && ctx->keymgmt->gen_set_params != NULL)
681 return
682 evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
683 params);
684 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
685 && ctx->op.encap.kem != NULL
686 && ctx->op.encap.kem->set_ctx_params != NULL)
687 return
688 ctx->op.encap.kem->set_ctx_params(ctx->op.encap.kemprovctx,
689 params);
690 break;
691#ifndef FIPS_MODULE
692 case EVP_PKEY_STATE_UNKNOWN:
693 case EVP_PKEY_STATE_LEGACY:
694 return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
695#endif
696 }
e683582b 697 return 0;
0f113f3e 698}
5da98aa6 699
9c45222d
MC
700int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
701{
6fcd92d3
RL
702 switch (evp_pkey_ctx_state(ctx)) {
703 case EVP_PKEY_STATE_PROVIDER:
704 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
4fe54d67
NT
705 && ctx->op.kex.exchange != NULL
706 && ctx->op.kex.exchange->get_ctx_params != NULL)
6fcd92d3
RL
707 return
708 ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
709 params);
710 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
864b89ce
MC
711 && ctx->op.sig.signature != NULL
712 && ctx->op.sig.signature->get_ctx_params != NULL)
6fcd92d3
RL
713 return
714 ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
715 params);
716 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
2c938e2e
MC
717 && ctx->op.ciph.cipher != NULL
718 && ctx->op.ciph.cipher->get_ctx_params != NULL)
6fcd92d3
RL
719 return
720 ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
721 params);
722 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
723 && ctx->op.encap.kem != NULL
724 && ctx->op.encap.kem->get_ctx_params != NULL)
725 return
726 ctx->op.encap.kem->get_ctx_params(ctx->op.encap.kemprovctx,
727 params);
728 break;
729#ifndef FIPS_MODULE
730 case EVP_PKEY_STATE_UNKNOWN:
731 case EVP_PKEY_STATE_LEGACY:
732 return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
733#endif
734 }
9c45222d
MC
735 return 0;
736}
737
11a1b341 738#ifndef FIPS_MODULE
9c45222d
MC
739const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
740{
18ec26ba
P
741 void *provctx;
742
4fe54d67
NT
743 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
744 && ctx->op.kex.exchange != NULL
18ec26ba
P
745 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
746 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
747 return ctx->op.kex.exchange->gettable_ctx_params(provctx);
748 }
864b89ce
MC
749 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
750 && ctx->op.sig.signature != NULL
18ec26ba
P
751 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
752 provctx = ossl_provider_ctx(
753 EVP_SIGNATURE_provider(ctx->op.sig.signature));
754 return ctx->op.sig.signature->gettable_ctx_params(provctx);
755 }
2c938e2e
MC
756 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
757 && ctx->op.ciph.cipher != NULL
18ec26ba
P
758 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
759 provctx = ossl_provider_ctx(
760 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
761 return ctx->op.ciph.cipher->gettable_ctx_params(provctx);
762 }
80f4fd18
SL
763 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
764 && ctx->op.encap.kem != NULL
765 && ctx->op.encap.kem->gettable_ctx_params != NULL) {
766 provctx = ossl_provider_ctx(EVP_KEM_provider(ctx->op.encap.kem));
767 return ctx->op.encap.kem->gettable_ctx_params(provctx);
768 }
9c45222d
MC
769 return NULL;
770}
771
9c45222d
MC
772const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
773{
18ec26ba
P
774 void *provctx;
775
864b89ce
MC
776 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
777 && ctx->op.kex.exchange != NULL
18ec26ba
P
778 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
779 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
780 return ctx->op.kex.exchange->settable_ctx_params(provctx);
781 }
864b89ce
MC
782 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
783 && ctx->op.sig.signature != NULL
18ec26ba
P
784 && ctx->op.sig.signature->settable_ctx_params != NULL) {
785 provctx = ossl_provider_ctx(
786 EVP_SIGNATURE_provider(ctx->op.sig.signature));
787 return ctx->op.sig.signature->settable_ctx_params(provctx);
788 }
2c938e2e
MC
789 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
790 && ctx->op.ciph.cipher != NULL
18ec26ba
P
791 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
792 provctx = ossl_provider_ctx(
793 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
794 return ctx->op.ciph.cipher->settable_ctx_params(provctx);
795 }
62924755 796 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
5e77b79a 797 && ctx->keymgmt != NULL)
e3efe7a5 798 return EVP_KEYMGMT_gen_settable_params(ctx->keymgmt);
80f4fd18
SL
799 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
800 && ctx->op.encap.kem != NULL
801 && ctx->op.encap.kem->settable_ctx_params != NULL) {
802 provctx = ossl_provider_ctx(EVP_KEM_provider(ctx->op.encap.kem));
803 return ctx->op.encap.kem->settable_ctx_params(provctx);
804 }
9c45222d
MC
805 return NULL;
806}
807
4fe54d67
NT
808/*
809 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
810 *
811 * Return 1 on success, 0 or negative for errors.
812 *
813 * In particular they return -2 if any of the params is not supported.
814 *
f844f9eb 815 * They are not available in FIPS_MODULE as they depend on
4fe54d67
NT
816 * - EVP_PKEY_CTX_{get,set}_params()
817 * - EVP_PKEY_CTX_{gettable,settable}_params()
818 *
819 */
820int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
821{
4fe54d67
NT
822 if (ctx == NULL || params == NULL)
823 return 0;
824
51373129
RL
825 /*
826 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
827 * depend on the translation that happens in EVP_PKEY_CTX_set_params()
828 * call, and that the resulting ctrl call will return -2 if it doesn't
829 * known the ctrl command number.
830 */
831 if (evp_pkey_ctx_is_provided(ctx)) {
832 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
833 const OSSL_PARAM *p;
834
835 for (p = params; p->key != NULL; p++) {
836 /* Check the ctx actually understands this parameter */
837 if (OSSL_PARAM_locate_const(settable, p->key) == NULL )
838 return -2;
839 }
4fe54d67
NT
840 }
841
842 return EVP_PKEY_CTX_set_params(ctx, params);
843}
844
845int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
846{
4fe54d67
NT
847 if (ctx == NULL || params == NULL)
848 return 0;
849
51373129
RL
850 /*
851 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
852 * depend on the translation that happens in EVP_PKEY_CTX_get_params()
853 * call, and that the resulting ctrl call will return -2 if it doesn't
854 * known the ctrl command number.
855 */
856 if (evp_pkey_ctx_is_provided(ctx)) {
857 const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
858 const OSSL_PARAM *p;
859
860 for (p = params; p->key != NULL; p++ ) {
861 /* Check the ctx actually understands this parameter */
862 if (OSSL_PARAM_locate_const(gettable, p->key) == NULL )
863 return -2;
864 }
4fe54d67
NT
865 }
866
867 return EVP_PKEY_CTX_get_params(ctx, params);
868}
869
9c45222d
MC
870int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
871{
ac2d58c7 872 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
9c45222d
MC
873 /* 80 should be big enough */
874 char name[80] = "";
875 const EVP_MD *tmp;
876
864b89ce 877 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
878 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
879 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
880 return -2;
881 }
882
883 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 884 if (ctx->op.sig.sigprovctx == NULL)
9c45222d
MC
885 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
886 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
887
888 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
889 name,
890 sizeof(name));
ac2d58c7 891 *p = OSSL_PARAM_construct_end();
9c45222d
MC
892
893 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
894 return 0;
895
7606bed9 896 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
9c45222d
MC
897 if (tmp == NULL)
898 return 0;
899
900 *md = tmp;
901
902 return 1;
903}
904
05d2f72e
MC
905static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
906 int fallback, const char *param, int op,
907 int ctrl)
4889dadc 908{
05d2f72e 909 OSSL_PARAM md_params[2], *p = md_params;
4889dadc
MC
910 const char *name;
911
05d2f72e 912 if (ctx == NULL || (ctx->operation & op) == 0) {
9c45222d
MC
913 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
914 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
915 return -2;
916 }
917
4889dadc 918 /* TODO(3.0): Remove this eventually when no more legacy */
05d2f72e
MC
919 if (fallback)
920 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
4889dadc 921
9c45222d
MC
922 if (md == NULL) {
923 name = "";
9c45222d 924 } else {
9c45222d
MC
925 name = EVP_MD_name(md);
926 }
4889dadc 927
05d2f72e 928 *p++ = OSSL_PARAM_construct_utf8_string(param,
9c45222d
MC
929 /*
930 * Cast away the const. This is read
931 * only so should be safe
932 */
8b6ffd40 933 (char *)name, 0);
ac2d58c7 934 *p = OSSL_PARAM_construct_end();
4889dadc 935
05d2f72e
MC
936 return EVP_PKEY_CTX_set_params(ctx, md_params);
937}
938
939int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
940{
941 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.sigprovctx == NULL,
942 OSSL_SIGNATURE_PARAM_DIGEST,
943 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
4889dadc
MC
944}
945
ac2d58c7
MC
946int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
947{
05d2f72e
MC
948 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
949 OSSL_KDF_PARAM_DIGEST,
950 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
951}
952
953static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
954 const char *param, int op, int ctrl,
955 const unsigned char *data,
956 int datalen)
957{
958 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
ac2d58c7 959
5d51925a 960 if (ctx == NULL || (ctx->operation & op) == 0) {
ac2d58c7
MC
961 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
962 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
963 return -2;
964 }
965
966 /* TODO(3.0): Remove this eventually when no more legacy */
05d2f72e
MC
967 if (fallback)
968 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
ac2d58c7 969
05d2f72e
MC
970 if (datalen < 0) {
971 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
972 return 0;
ac2d58c7
MC
973 }
974
05d2f72e 975 *p++ = OSSL_PARAM_construct_octet_string(param,
ac2d58c7
MC
976 /*
977 * Cast away the const. This is read
978 * only so should be safe
979 */
05d2f72e
MC
980 (unsigned char *)data,
981 (size_t)datalen);
194de849 982 *p = OSSL_PARAM_construct_end();
ac2d58c7 983
05d2f72e 984 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
ac2d58c7
MC
985}
986
987int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
988 const unsigned char *sec, int seclen)
989{
05d2f72e
MC
990 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
991 OSSL_KDF_PARAM_SECRET,
992 EVP_PKEY_OP_DERIVE,
993 EVP_PKEY_CTRL_TLS_SECRET,
994 sec, seclen);
995}
ac2d58c7 996
05d2f72e
MC
997int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
998 const unsigned char *seed, int seedlen)
999{
1000 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1001 OSSL_KDF_PARAM_SEED,
1002 EVP_PKEY_OP_DERIVE,
1003 EVP_PKEY_CTRL_TLS_SEED,
1004 seed, seedlen);
1005}
ac2d58c7 1006
05d2f72e
MC
1007int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1008{
1009 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
1010 OSSL_KDF_PARAM_DIGEST,
1011 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1012}
ac2d58c7 1013
05d2f72e
MC
1014int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1015 const unsigned char *salt, int saltlen)
1016{
1017 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1018 OSSL_KDF_PARAM_SALT,
1019 EVP_PKEY_OP_DERIVE,
1020 EVP_PKEY_CTRL_HKDF_SALT,
1021 salt, saltlen);
1022}
ac2d58c7 1023
05d2f72e
MC
1024int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1025 const unsigned char *key, int keylen)
1026{
1027 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1028 OSSL_KDF_PARAM_KEY,
1029 EVP_PKEY_OP_DERIVE,
1030 EVP_PKEY_CTRL_HKDF_KEY,
1031 key, keylen);
1032}
ac2d58c7 1033
05d2f72e
MC
1034int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1035 const unsigned char *info, int infolen)
1036{
1037 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1038 OSSL_KDF_PARAM_INFO,
1039 EVP_PKEY_OP_DERIVE,
1040 EVP_PKEY_CTRL_HKDF_INFO,
1041 info, infolen);
ac2d58c7
MC
1042}
1043
05d2f72e 1044int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
ac2d58c7 1045{
05d2f72e 1046 OSSL_PARAM int_params[2], *p = int_params;
ac2d58c7
MC
1047
1048 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1049 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1050 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1051 return -2;
1052 }
1053
1054 /* TODO(3.0): Remove this eventually when no more legacy */
1055 if (ctx->op.kex.exchprovctx == NULL)
1056 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
05d2f72e
MC
1057 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1058
ac2d58c7 1059
05d2f72e
MC
1060 if (mode < 0) {
1061 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
ac2d58c7
MC
1062 return 0;
1063 }
1064
05d2f72e 1065 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
194de849 1066 *p = OSSL_PARAM_construct_end();
ac2d58c7 1067
05d2f72e 1068 return EVP_PKEY_CTX_set_params(ctx, int_params);
ac2d58c7
MC
1069}
1070
194de849
MC
1071int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1072 int passlen)
1073{
1074 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1075 OSSL_KDF_PARAM_PASSWORD,
1076 EVP_PKEY_OP_DERIVE,
1077 EVP_PKEY_CTRL_PASS,
1078 (const unsigned char *)pass, passlen);
1079}
1080
1081int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1082 const unsigned char *salt, int saltlen)
1083{
1084 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1085 OSSL_KDF_PARAM_SALT,
1086 EVP_PKEY_OP_DERIVE,
1087 EVP_PKEY_CTRL_SCRYPT_SALT,
1088 salt, saltlen);
1089}
1090
1091static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1092 int op, int ctrl, uint64_t val)
1093{
1094 OSSL_PARAM uint64_params[2], *p = uint64_params;
1095
1096 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1097 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1098 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1099 return -2;
1100 }
1101
1102 /* TODO(3.0): Remove this eventually when no more legacy */
1103 if (ctx->op.kex.exchprovctx == NULL)
1104 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1105
1106 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1107 *p = OSSL_PARAM_construct_end();
1108
1109 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1110}
1111
1112int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1113{
1114 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1115 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1116 n);
1117}
1118
1119int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1120{
1121 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1122 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1123 r);
1124}
1125
1126int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1127{
1128 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1129 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1130 p);
1131}
1132
1133int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1134 uint64_t maxmem_bytes)
1135{
1136 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1137 EVP_PKEY_OP_DERIVE,
1138 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1139 maxmem_bytes);
1140}
1141
5d51925a
MC
1142int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1143 int keylen)
1144{
1145 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1146 OSSL_PKEY_PARAM_PRIV_KEY,
1147 EVP_PKEY_OP_KEYGEN,
1148 EVP_PKEY_CTRL_SET_MAC_KEY,
1149 key, keylen);
1150}
1151
80f4fd18
SL
1152int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1153{
1154 OSSL_PARAM params[2], *p = params;
1155
1156 if (ctx == NULL || op == NULL) {
1157 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1158 return 0;
1159 }
1160 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1161 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1162 return -2;
1163 }
1164 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1165 (char *)op, 0);
1166 *p = OSSL_PARAM_construct_end();
1167 return EVP_PKEY_CTX_set_params(ctx, params);
1168}
1169
8d6481f5
RL
1170int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
1171{
1172 OSSL_PARAM params[2], *p = params;
1173 int ret;
1174
1175 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1176 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1177 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1178 return -2;
1179 }
1180
1181 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
1182 /*
1183 * Cast away the const. This is
1184 * read only so should be safe
1185 */
1186 (void *)id, (size_t)len);
1187 *p++ = OSSL_PARAM_construct_end();
1188
1189 ret = evp_pkey_ctx_set_params_strict(ctx, params);
1190 if (ret == -2)
1191 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1192 return ret;
1193}
1194
1195int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1196{
1197 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1198 EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1199}
1200
1201static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
1202{
1203 int ret;
1204 void *tmp_id = NULL;
1205 OSSL_PARAM params[2], *p = params;
1206
1207 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1208 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1209 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1210 return -2;
1211 }
1212
1213 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
1214 &tmp_id, 0);
1215 *p++ = OSSL_PARAM_construct_end();
1216
1217 ret = evp_pkey_ctx_get_params_strict(ctx, params);
1218 if (ret == -2) {
1219 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1220 } else if (ret > 0) {
1221 size_t tmp_id_len = params[0].return_size;
1222
1223 if (id != NULL)
1224 memcpy(id, tmp_id, tmp_id_len);
1225 if (id_len != NULL)
1226 *id_len = tmp_id_len;
1227 }
1228 return ret;
1229}
1230
1231int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
1232{
1233 return get1_id_data(ctx, id, NULL);
1234}
1235
1236int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
1237{
1238 return get1_id_data(ctx, NULL, id_len);
1239}
1240
1241int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1242{
1243 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1244}
1245
1246int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1247{
1248 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1249 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1250}
1251
86df26b3
RL
1252static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1253 int cmd, int p1, void *p2)
0f113f3e 1254{
86df26b3 1255 int ret = 0;
4803717f 1256
86df26b3
RL
1257 /*
1258 * If the method has a |digest_custom| function, we can relax the
1259 * operation type check, since this can be called before the operation
1260 * is initialized.
1261 */
1262 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1263 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
9311d0c4 1264 ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
86df26b3
RL
1265 return -1;
1266 }
35aca9ec 1267
86df26b3 1268 if ((optype != -1) && !(ctx->operation & optype)) {
9311d0c4 1269 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
86df26b3
RL
1270 return -1;
1271 }
0f113f3e 1272 }
0f113f3e 1273
86df26b3
RL
1274 switch (evp_pkey_ctx_state(ctx)) {
1275 case EVP_PKEY_STATE_PROVIDER:
5524580b 1276 return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
86df26b3
RL
1277 case EVP_PKEY_STATE_UNKNOWN:
1278 case EVP_PKEY_STATE_LEGACY:
1279 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
9311d0c4 1280 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3
RL
1281 return -2;
1282 }
1283 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1284 return -1;
4803717f 1285
86df26b3 1286 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
0f113f3e 1287
86df26b3 1288 if (ret == -2)
9311d0c4 1289 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3 1290 break;
0f113f3e 1291 }
86df26b3
RL
1292 return ret;
1293}
0f113f3e 1294
86df26b3
RL
1295int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1296 int cmd, int p1, void *p2)
1297{
1298 int ret = 0;
1299
d65ab22e 1300 if (ctx == NULL) {
9311d0c4 1301 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
d65ab22e
SL
1302 return -2;
1303 }
86df26b3
RL
1304 /* If unsupported, we don't want that reported here */
1305 ERR_set_mark();
1306 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1307 cmd, NULL, p2, p1);
1308 if (ret == -2) {
1309 ERR_pop_to_mark();
1310 } else {
1311 ERR_clear_last_mark();
1312 /*
1313 * If there was an error, there was an error.
1314 * If the operation isn't initialized yet, we also return, as
1315 * the saved values will be used then anyway.
1316 */
1317 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1318 return ret;
1319 }
86df26b3 1320 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
0f113f3e 1321}
0b6f3c66 1322
cefa762e 1323int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 1324 int cmd, uint64_t value)
cefa762e
JB
1325{
1326 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1327}
1328
19dbb742
SL
1329<<<<<<< HEAD
1330=======
1331static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
1332 const char *value)
1333{
1334 if (strcmp(name, "md") == 0)
1335 name = OSSL_ALG_PARAM_DIGEST;
1336 else if (strcmp(name, "rsa_padding_mode") == 0)
1337 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1338 else if (strcmp(name, "rsa_mgf1_md") == 0)
1339 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1340 else if (strcmp(name, "rsa_oaep_md") == 0)
1341 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1342 else if (strcmp(name, "rsa_oaep_label") == 0)
1343 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
1344 else if (strcmp(name, "rsa_pss_saltlen") == 0)
1345 name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
1346 else if (strcmp(name, "rsa_keygen_bits") == 0)
1347 name = OSSL_PKEY_PARAM_RSA_BITS;
1348 else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1349 name = OSSL_PKEY_PARAM_RSA_E;
1350 else if (strcmp(name, "rsa_keygen_primes") == 0)
1351 name = OSSL_PKEY_PARAM_RSA_PRIMES;
1352 else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1353 name = OSSL_PKEY_PARAM_RSA_DIGEST;
1354 else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1355 name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1356 else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1357 name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
1358 else if (strcmp(name, "dsa_paramgen_bits") == 0)
1359 name = OSSL_PKEY_PARAM_FFC_PBITS;
1360 else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1361 name = OSSL_PKEY_PARAM_FFC_QBITS;
1362 else if (strcmp(name, "dsa_paramgen_md") == 0)
1363 name = OSSL_PKEY_PARAM_FFC_DIGEST;
1364 else if (strcmp(name, "dh_paramgen_generator") == 0)
1365 name = OSSL_PKEY_PARAM_DH_GENERATOR;
1366 else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1367 name = OSSL_PKEY_PARAM_FFC_PBITS;
1368 else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1369 name = OSSL_PKEY_PARAM_FFC_QBITS;
1370 else if (strcmp(name, "dh_paramgen_type") == 0) {
1371 name = OSSL_PKEY_PARAM_FFC_TYPE;
1372 value = ossl_dh_gen_type_id2name(atoi(value));
1373 } else if (strcmp(name, "dh_param") == 0)
1374 name = OSSL_PKEY_PARAM_GROUP_NAME;
1375 else if (strcmp(name, "dh_rfc5114") == 0) {
1376 int num = atoi(value);
1377
1378 name = OSSL_PKEY_PARAM_GROUP_NAME;
1379 value =
1380 ossl_ffc_named_group_get_name(ossl_ffc_uid_to_dh_named_group(num));
1381 } else if (strcmp(name, "dh_pad") == 0)
1382 name = OSSL_EXCHANGE_PARAM_PAD;
1383 else if (strcmp(name, "ec_paramgen_curve") == 0)
1384 name = OSSL_PKEY_PARAM_GROUP_NAME;
1385 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1386 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1387 else if (strcmp(name, "ecdh_kdf_md") == 0)
1388 name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
1389 else if (strcmp(name, "ec_param_enc") == 0)
1390 name = OSSL_PKEY_PARAM_EC_ENCODING;
1391 else if (strcmp(name, "N") == 0)
1392 name = OSSL_KDF_PARAM_SCRYPT_N;
1393
1394 {
1395 /*
1396 * TODO(3.0) reduce the code above to only translate known legacy
1397 * string to the corresponding core name (see core_names.h), but
1398 * otherwise leave it to this code block to do the actual work.
1399 */
1400 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1401 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1402 int rv = 0;
1403 int exists = 0;
1404
1405 if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
1406 strlen(value), &exists)) {
1407 if (!exists) {
1408 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1409 "name=%s,value=%s", name, value);
1410 return -2;
1411 }
1412 return 0;
1413 }
1414 if (EVP_PKEY_CTX_set_params(ctx, params))
1415 rv = 1;
1416 OPENSSL_free(params[0].data);
1417 return rv;
1418 }
1419}
1420
1421>>>>>>> 4651c47010... Fix external symbols related to dh keys
86df26b3
RL
1422static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1423 const char *name, const char *value)
0f113f3e 1424{
86df26b3
RL
1425 int ret = 0;
1426
35aca9ec 1427 if (ctx == NULL) {
9311d0c4 1428 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
35aca9ec
MC
1429 return -2;
1430 }
1431
86df26b3
RL
1432 switch (evp_pkey_ctx_state(ctx)) {
1433 case EVP_PKEY_STATE_PROVIDER:
5524580b 1434 return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
86df26b3
RL
1435 case EVP_PKEY_STATE_UNKNOWN:
1436 case EVP_PKEY_STATE_LEGACY:
1437 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
9311d0c4 1438 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3
RL
1439 return -2;
1440 }
1441 if (strcmp(name, "digest") == 0)
b9689452
RL
1442 ret = EVP_PKEY_CTX_md(ctx,
1443 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
86df26b3
RL
1444 EVP_PKEY_CTRL_MD, value);
1445 else
1446 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1447 break;
1448 }
35aca9ec 1449
86df26b3
RL
1450 return ret;
1451}
1452
1453int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1454 const char *name, const char *value)
1455{
1456 int ret = 0;
1457
1458 /* If unsupported, we don't want that reported here */
1459 ERR_set_mark();
1460 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1461 name, value, strlen(value) + 1);
1462 if (ret == -2) {
1463 ERR_pop_to_mark();
1464 } else {
1465 ERR_clear_last_mark();
1466 /*
1467 * If there was an error, there was an error.
1468 * If the operation isn't initialized yet, we also return, as
1469 * the saved values will be used then anyway.
1470 */
1471 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1472 return ret;
1473 }
1474
1475 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1476}
1477
1478static int decode_cmd(int cmd, const char *name)
1479{
1480 if (cmd == -1) {
1481 /*
1482 * The consequence of the assertion not being true is that this
1483 * function will return -1, which will cause the calling functions
1484 * to signal that the command is unsupported... in non-debug mode.
1485 */
1486 if (ossl_assert(name != NULL))
1487 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1488 cmd = EVP_PKEY_CTRL_SET1_ID;
1489 }
1490
1491 return cmd;
1492}
1493
1494static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1495 int keytype, int optype,
1496 int cmd, const char *name,
1497 const void *data, size_t data_len)
1498{
bbf4dc96
RL
1499 /*
1500 * Check that it's one of the supported commands. The ctrl commands
1501 * number cases here must correspond to the cases in the bottom switch
1502 * in this function.
1503 */
1504 switch (cmd = decode_cmd(cmd, name)) {
1505 case EVP_PKEY_CTRL_SET1_ID:
1506 break;
1507 default:
1508 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1509 return -2;
1510 }
1511
977e95b9
RL
1512 if (keytype != -1) {
1513 switch (evp_pkey_ctx_state(ctx)) {
1514 case EVP_PKEY_STATE_PROVIDER:
1515 if (ctx->keymgmt == NULL) {
1516 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1517 return -2;
1518 }
1519 if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1520 evp_pkey_type2name(keytype))) {
1521 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1522 return -1;
1523 }
1524 break;
1525 case EVP_PKEY_STATE_UNKNOWN:
1526 case EVP_PKEY_STATE_LEGACY:
1527 if (ctx->pmeth == NULL) {
1528 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1529 return -2;
1530 }
bbf4dc96 1531 if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
977e95b9
RL
1532 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1533 return -1;
1534 }
1535 break;
1536 }
1537 }
1538 if (optype != -1 && (ctx->operation & optype) == 0) {
86df26b3
RL
1539 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1540 return -1;
0f113f3e 1541 }
86df26b3 1542
86df26b3
RL
1543 switch (cmd) {
1544 case EVP_PKEY_CTRL_SET1_ID:
1545 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1546 if (name != NULL) {
1547 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1548 if (ctx->cached_parameters.dist_id_name == NULL) {
1549 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1550 return 0;
1551 }
1552 }
1553 if (data_len > 0) {
1554 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1555 if (ctx->cached_parameters.dist_id == NULL) {
1556 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1557 return 0;
1558 }
1559 }
1560 ctx->cached_parameters.dist_id_set = 1;
1561 ctx->cached_parameters.dist_id_len = data_len;
bbf4dc96 1562 break;
86df26b3 1563 }
bbf4dc96 1564 return 1;
86df26b3
RL
1565}
1566
1567static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1568 int cmd, const char *name)
1569{
1570 cmd = decode_cmd(cmd, name);
1571 switch (cmd) {
1572 case EVP_PKEY_CTRL_SET1_ID:
1573 OPENSSL_free(ctx->cached_parameters.dist_id);
1574 OPENSSL_free(ctx->cached_parameters.dist_id_name);
1575 ctx->cached_parameters.dist_id = NULL;
1576 ctx->cached_parameters.dist_id_name = NULL;
1577 break;
1578 }
1579}
1580
1581static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1582{
1583 evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1584}
1585
1586int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1587{
1588 int ret = 1;
1589
1590 if (ret && ctx->cached_parameters.dist_id_set) {
1591 const char *name = ctx->cached_parameters.dist_id_name;
1592 const void *val = ctx->cached_parameters.dist_id;
1593 size_t len = ctx->cached_parameters.dist_id_len;
1594
1595 if (name != NULL)
1596 ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1597 else
1598 ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1599 EVP_PKEY_CTRL_SET1_ID,
1600 (int)len, (void *)val);
1601 }
1602
1603 return ret;
0f113f3e 1604}
f5cda4cb 1605
b4250010 1606OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
0b3a4ef2
MC
1607{
1608 return ctx->libctx;
1609}
1610
29000e43 1611const char *EVP_PKEY_CTX_get0_propq(EVP_PKEY_CTX *ctx)
0b3a4ef2
MC
1612{
1613 return ctx->propquery;
1614}
1615
99119000
DSH
1616/* Utility functions to send a string of hex string to a ctrl */
1617
1618int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1619{
1620 size_t len;
1621
1622 len = strlen(str);
1623 if (len > INT_MAX)
1624 return -1;
1625 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1626}
1627
1628int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1629{
1630 unsigned char *bin;
1631 long binlen;
1632 int rv = -1;
1633
14f051a0 1634 bin = OPENSSL_hexstr2buf(hex, &binlen);
99119000
DSH
1635 if (bin == NULL)
1636 return 0;
1637 if (binlen <= INT_MAX)
1638 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1639 OPENSSL_free(bin);
1640 return rv;
1641}
52ad523c 1642
410877ba
DSH
1643/* Pass a message digest to a ctrl */
1644int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1645{
1646 const EVP_MD *m;
c82bafc5 1647
410877ba 1648 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
9311d0c4 1649 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
410877ba
DSH
1650 return 0;
1651 }
1652 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1653}
99119000 1654
b28dea4e 1655int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1656{
1657 return ctx->operation;
1658}
b28dea4e
DSH
1659
1660void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
0f113f3e
MC
1661{
1662 ctx->keygen_info = dat;
1663 ctx->keygen_info_count = datlen;
1664}
b28dea4e 1665
f5cda4cb 1666void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1667{
1668 ctx->data = data;
1669}
f5cda4cb 1670
9fdcc21f 1671void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
0f113f3e
MC
1672{
1673 return ctx->data;
1674}
f5cda4cb 1675
81cebb8b 1676EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1677{
1678 return ctx->pkey;
1679}
81cebb8b 1680
0e1dba93 1681EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1682{
1683 return ctx->peerkey;
1684}
1685
f5cda4cb 1686void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1687{
1688 ctx->app_data = data;
1689}
f5cda4cb
DSH
1690
1691void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1692{
1693 return ctx->app_data;
1694}
ba30bad5
DSH
1695
1696void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1697 int (*init) (EVP_PKEY_CTX *ctx))
1698{
1699 pmeth->init = init;
1700}
8bdcef40
DSH
1701
1702void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
0f113f3e 1703 int (*copy) (EVP_PKEY_CTX *dst,
9fdcc21f 1704 const EVP_PKEY_CTX *src))
0f113f3e
MC
1705{
1706 pmeth->copy = copy;
1707}
ba30bad5
DSH
1708
1709void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1710 void (*cleanup) (EVP_PKEY_CTX *ctx))
1711{
1712 pmeth->cleanup = cleanup;
1713}
ba30bad5
DSH
1714
1715void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1716 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1717 int (*paramgen) (EVP_PKEY_CTX *ctx,
1718 EVP_PKEY *pkey))
1719{
1720 pmeth->paramgen_init = paramgen_init;
1721 pmeth->paramgen = paramgen;
1722}
ba30bad5
DSH
1723
1724void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1725 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1726 int (*keygen) (EVP_PKEY_CTX *ctx,
1727 EVP_PKEY *pkey))
1728{
1729 pmeth->keygen_init = keygen_init;
1730 pmeth->keygen = keygen;
1731}
ba30bad5
DSH
1732
1733void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1734 int (*sign_init) (EVP_PKEY_CTX *ctx),
1735 int (*sign) (EVP_PKEY_CTX *ctx,
1736 unsigned char *sig, size_t *siglen,
1737 const unsigned char *tbs,
1738 size_t tbslen))
1739{
1740 pmeth->sign_init = sign_init;
1741 pmeth->sign = sign;
1742}
ba30bad5
DSH
1743
1744void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1745 int (*verify_init) (EVP_PKEY_CTX *ctx),
1746 int (*verify) (EVP_PKEY_CTX *ctx,
1747 const unsigned char *sig,
1748 size_t siglen,
1749 const unsigned char *tbs,
1750 size_t tbslen))
1751{
1752 pmeth->verify_init = verify_init;
1753 pmeth->verify = verify;
1754}
ba30bad5
DSH
1755
1756void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1757 int (*verify_recover_init) (EVP_PKEY_CTX
1758 *ctx),
1759 int (*verify_recover) (EVP_PKEY_CTX
1760 *ctx,
1761 unsigned char
1762 *sig,
1763 size_t *siglen,
1764 const unsigned
1765 char *tbs,
1766 size_t tbslen))
1767{
1768 pmeth->verify_recover_init = verify_recover_init;
1769 pmeth->verify_recover = verify_recover;
1770}
ba30bad5
DSH
1771
1772void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1773 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1774 EVP_MD_CTX *mctx),
1775 int (*signctx) (EVP_PKEY_CTX *ctx,
1776 unsigned char *sig,
1777 size_t *siglen,
1778 EVP_MD_CTX *mctx))
1779{
1780 pmeth->signctx_init = signctx_init;
1781 pmeth->signctx = signctx;
1782}
ba30bad5
DSH
1783
1784void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1785 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1786 EVP_MD_CTX *mctx),
1787 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1788 const unsigned char *sig,
1789 int siglen,
1790 EVP_MD_CTX *mctx))
1791{
1792 pmeth->verifyctx_init = verifyctx_init;
1793 pmeth->verifyctx = verifyctx;
1794}
ba30bad5
DSH
1795
1796void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1797 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1798 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1799 unsigned char *out,
1800 size_t *outlen,
1801 const unsigned char *in,
1802 size_t inlen))
1803{
1804 pmeth->encrypt_init = encrypt_init;
1805 pmeth->encrypt = encryptfn;
1806}
ba30bad5
DSH
1807
1808void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1809 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1810 int (*decrypt) (EVP_PKEY_CTX *ctx,
1811 unsigned char *out,
1812 size_t *outlen,
1813 const unsigned char *in,
1814 size_t inlen))
1815{
1816 pmeth->decrypt_init = decrypt_init;
1817 pmeth->decrypt = decrypt;
1818}
ba30bad5
DSH
1819
1820void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1821 int (*derive_init) (EVP_PKEY_CTX *ctx),
1822 int (*derive) (EVP_PKEY_CTX *ctx,
1823 unsigned char *key,
1824 size_t *keylen))
1825{
1826 pmeth->derive_init = derive_init;
1827 pmeth->derive = derive;
1828}
ba30bad5
DSH
1829
1830void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1831 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1832 void *p2),
1833 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1834 const char *type,
1835 const char *value))
1836{
1837 pmeth->ctrl = ctrl;
1838 pmeth->ctrl_str = ctrl_str;
1839}
e7451ed1 1840
2555285f
AH
1841void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1842 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1843 const unsigned char *tbs, size_t tbslen))
1844{
1845 pmeth->digestsign = digestsign;
1846}
1847
1848void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1849 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1850 size_t siglen, const unsigned char *tbs,
1851 size_t tbslen))
1852{
1853 pmeth->digestverify = digestverify;
1854}
1855
2aee35d3
PY
1856void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1857 int (*check) (EVP_PKEY *pkey))
1858{
1859 pmeth->check = check;
1860}
1861
b0004708
PY
1862void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1863 int (*check) (EVP_PKEY *pkey))
1864{
1865 pmeth->public_check = check;
1866}
1867
1868void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1869 int (*check) (EVP_PKEY *pkey))
1870{
1871 pmeth->param_check = check;
1872}
1873
0a8fdef7
PY
1874void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1875 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1876 EVP_MD_CTX *mctx))
1877{
1878 pmeth->digest_custom = digest_custom;
1879}
1880
693be9a2 1881void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1882 int (**pinit) (EVP_PKEY_CTX *ctx))
1883{
1884 *pinit = pmeth->init;
1885}
1886
693be9a2 1887void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
e7451ed1 1888 int (**pcopy) (EVP_PKEY_CTX *dst,
9fdcc21f 1889 const EVP_PKEY_CTX *src))
e7451ed1
DSH
1890{
1891 *pcopy = pmeth->copy;
1892}
1893
693be9a2 1894void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1895 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1896{
1897 *pcleanup = pmeth->cleanup;
1898}
1899
693be9a2 1900void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1901 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1902 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1903 EVP_PKEY *pkey))
1904{
1905 if (pparamgen_init)
1906 *pparamgen_init = pmeth->paramgen_init;
1907 if (pparamgen)
1908 *pparamgen = pmeth->paramgen;
1909}
1910
693be9a2 1911void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1912 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1913 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1914 EVP_PKEY *pkey))
1915{
1916 if (pkeygen_init)
1917 *pkeygen_init = pmeth->keygen_init;
1918 if (pkeygen)
1919 *pkeygen = pmeth->keygen;
1920}
1921
693be9a2 1922void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1923 int (**psign_init) (EVP_PKEY_CTX *ctx),
1924 int (**psign) (EVP_PKEY_CTX *ctx,
1925 unsigned char *sig, size_t *siglen,
1926 const unsigned char *tbs,
1927 size_t tbslen))
1928{
1929 if (psign_init)
1930 *psign_init = pmeth->sign_init;
1931 if (psign)
1932 *psign = pmeth->sign;
1933}
1934
693be9a2 1935void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1936 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1937 int (**pverify) (EVP_PKEY_CTX *ctx,
1938 const unsigned char *sig,
1939 size_t siglen,
1940 const unsigned char *tbs,
1941 size_t tbslen))
1942{
1943 if (pverify_init)
1944 *pverify_init = pmeth->verify_init;
1945 if (pverify)
1946 *pverify = pmeth->verify;
1947}
1948
693be9a2 1949void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1950 int (**pverify_recover_init) (EVP_PKEY_CTX
1951 *ctx),
1952 int (**pverify_recover) (EVP_PKEY_CTX
1953 *ctx,
1954 unsigned char
1955 *sig,
1956 size_t *siglen,
1957 const unsigned
1958 char *tbs,
1959 size_t tbslen))
1960{
1961 if (pverify_recover_init)
1962 *pverify_recover_init = pmeth->verify_recover_init;
1963 if (pverify_recover)
1964 *pverify_recover = pmeth->verify_recover;
1965}
1966
693be9a2 1967void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1968 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1969 EVP_MD_CTX *mctx),
1970 int (**psignctx) (EVP_PKEY_CTX *ctx,
1971 unsigned char *sig,
1972 size_t *siglen,
1973 EVP_MD_CTX *mctx))
1974{
1975 if (psignctx_init)
1976 *psignctx_init = pmeth->signctx_init;
1977 if (psignctx)
1978 *psignctx = pmeth->signctx;
1979}
1980
693be9a2 1981void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1982 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1983 EVP_MD_CTX *mctx),
1984 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1985 const unsigned char *sig,
1986 int siglen,
1987 EVP_MD_CTX *mctx))
1988{
1989 if (pverifyctx_init)
1990 *pverifyctx_init = pmeth->verifyctx_init;
1991 if (pverifyctx)
1992 *pverifyctx = pmeth->verifyctx;
1993}
1994
693be9a2 1995void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1996 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1997 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1998 unsigned char *out,
1999 size_t *outlen,
2000 const unsigned char *in,
2001 size_t inlen))
2002{
2003 if (pencrypt_init)
2004 *pencrypt_init = pmeth->encrypt_init;
2005 if (pencryptfn)
2006 *pencryptfn = pmeth->encrypt;
2007}
2008
693be9a2 2009void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2010 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
2011 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
2012 unsigned char *out,
2013 size_t *outlen,
2014 const unsigned char *in,
2015 size_t inlen))
2016{
2017 if (pdecrypt_init)
2018 *pdecrypt_init = pmeth->decrypt_init;
2019 if (pdecrypt)
2020 *pdecrypt = pmeth->decrypt;
2021}
2022
693be9a2 2023void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2024 int (**pderive_init) (EVP_PKEY_CTX *ctx),
2025 int (**pderive) (EVP_PKEY_CTX *ctx,
2026 unsigned char *key,
2027 size_t *keylen))
2028{
2029 if (pderive_init)
2030 *pderive_init = pmeth->derive_init;
2031 if (pderive)
2032 *pderive = pmeth->derive;
2033}
2034
693be9a2 2035void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
2036 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2037 void *p2),
2038 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2039 const char *type,
2040 const char *value))
2041{
2042 if (pctrl)
2043 *pctrl = pmeth->ctrl;
2044 if (pctrl_str)
2045 *pctrl_str = pmeth->ctrl_str;
2046}
2aee35d3 2047
2555285f
AH
2048void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
2049 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2050 const unsigned char *tbs, size_t tbslen))
2051{
2052 if (digestsign)
2053 *digestsign = pmeth->digestsign;
2054}
2055
2056void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
2057 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2058 size_t siglen, const unsigned char *tbs,
2059 size_t tbslen))
2060{
2061 if (digestverify)
2062 *digestverify = pmeth->digestverify;
2063}
2064
693be9a2 2065void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2aee35d3
PY
2066 int (**pcheck) (EVP_PKEY *pkey))
2067{
34f5c8b1 2068 if (pcheck != NULL)
2aee35d3
PY
2069 *pcheck = pmeth->check;
2070}
b0004708 2071
693be9a2 2072void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
2073 int (**pcheck) (EVP_PKEY *pkey))
2074{
34f5c8b1 2075 if (pcheck != NULL)
b0004708
PY
2076 *pcheck = pmeth->public_check;
2077}
2078
693be9a2 2079void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
2080 int (**pcheck) (EVP_PKEY *pkey))
2081{
34f5c8b1 2082 if (pcheck != NULL)
b0004708
PY
2083 *pcheck = pmeth->param_check;
2084}
0a8fdef7
PY
2085
2086void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
2087 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2088 EVP_MD_CTX *mctx))
2089{
675f4cee 2090 if (pdigest_custom != NULL)
0a8fdef7
PY
2091 *pdigest_custom = pmeth->digest_custom;
2092}
e683582b 2093
f844f9eb 2094#endif /* FIPS_MODULE */