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