]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
Raise an error on syscall failure in tls_retry_write_records
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
CommitLineData
0f113f3e 1/*
b6461792 2 * Copyright 2006-2024 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>
615525bd 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
4032cd9a 69# ifndef OPENSSL_NO_ECX
32ab57cb
SL
70 ossl_ecx25519_pkey_method,
71 ossl_ecx448_pkey_method,
32ab57cb
SL
72 ossl_ed25519_pkey_method,
73 ossl_ed448_pkey_method,
e683582b 74# endif
0f113f3e 75};
0b6f3c66 76
19bd1fa1
PS
77DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
78
79static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
80{
81 return ((*a)->pkey_id - ((**b)())->pkey_id);
82}
83
84IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
babb3798 85
0f113f3e
MC
86static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
87 const EVP_PKEY_METHOD *const *b)
88{
89 return ((*a)->pkey_id - (*b)->pkey_id);
90}
0b6f3c66 91
60488d24 92static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
0f113f3e 93{
60488d24 94 if (app_pkey_methods != NULL) {
0f113f3e 95 int idx;
60488d24
RL
96 EVP_PKEY_METHOD tmp;
97
98 tmp.pkey_id = type;
0f113f3e
MC
99 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
100 if (idx >= 0)
101 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
102 }
60488d24
RL
103 return NULL;
104}
105
106const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
107{
108 pmeth_fn *ret;
109 EVP_PKEY_METHOD tmp;
110 const EVP_PKEY_METHOD *t;
111
112 if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
113 return t;
114
115 tmp.pkey_id = type;
116 t = &tmp;
19bd1fa1 117 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
60488d24 118 OSSL_NELEM(standard_methods));
12a765a5 119 if (ret == NULL || *ret == NULL)
0f113f3e 120 return NULL;
19bd1fa1 121 return (**ret)();
0f113f3e 122}
0b6f3c66 123
e683582b
SL
124EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
125{
126 EVP_PKEY_METHOD *pmeth;
127
128 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
e077455e 129 if (pmeth == NULL)
e683582b 130 return NULL;
e683582b
SL
131
132 pmeth->pkey_id = id;
133 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
134 return pmeth;
135}
e19246dc
RL
136#endif /* FIPS_MODULE */
137
138int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
86df26b3
RL
139{
140 if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
141 return EVP_PKEY_STATE_UNKNOWN;
142
143 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
7c14d0c1 144 && ctx->op.kex.algctx != NULL)
86df26b3 145 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
7c14d0c1 146 && ctx->op.sig.algctx != NULL)
86df26b3 147 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
7c14d0c1 148 && ctx->op.ciph.algctx != NULL)
86df26b3 149 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
80f4fd18
SL
150 && ctx->op.keymgmt.genctx != NULL)
151 || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
7c14d0c1 152 && ctx->op.encap.algctx != NULL))
86df26b3
RL
153 return EVP_PKEY_STATE_PROVIDER;
154
155 return EVP_PKEY_STATE_LEGACY;
156}
157
b4250010 158static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
3ee348b0 159 EVP_PKEY *pkey, ENGINE *e,
4b9e90f4 160 const char *keytype, const char *propquery,
a07c17ef 161 int id)
e683582b 162
0f113f3e 163{
50914496 164 EVP_PKEY_CTX *ret = NULL;
929f651e 165 const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
4b9e90f4 166 EVP_KEYMGMT *keymgmt = NULL;
d0ea49a8 167
0be6cf0c 168 /* Code below to be removed when legacy support is dropped. */
d0ea49a8 169 /* BEGIN legacy */
0f113f3e 170 if (id == -1) {
929f651e 171 if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
50914496 172 id = pkey->type;
4d4de19e 173 } else {
929f651e
MC
174 if (pkey != NULL) {
175 /* Must be provided if we get here */
176 keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
177 }
178#ifndef FIPS_MODULE
179 if (keytype != NULL) {
180 id = evp_pkey_name2type(keytype);
181 if (id == NID_undef)
182 id = -1;
183 }
184#endif
185 }
0f113f3e 186 }
50914496 187 /* If no ID was found here, we can only resort to find a keymgmt */
4d4de19e
TM
188 if (id == -1) {
189#ifndef FIPS_MODULE
190 /* Using engine with a key without id will not work */
191 if (e != NULL) {
192 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
193 return NULL;
194 }
195#endif
50914496 196 goto common;
4d4de19e 197 }
60653e5b 198
929f651e 199#ifndef FIPS_MODULE
60653e5b
RL
200 /*
201 * Here, we extract what information we can for the purpose of
202 * supporting usage with implementations from providers, to make
203 * for a smooth transition from legacy stuff to provider based stuff.
204 *
205 * If an engine is given, this is entirely legacy, and we should not
4d4de19e 206 * pretend anything else, so we clear the name.
60653e5b 207 */
4d4de19e
TM
208 if (e != NULL)
209 keytype = NULL;
b247113c 210 if (e == NULL && (pkey == NULL || pkey->foreign == 0))
4b9e90f4 211 keytype = OBJ_nid2sn(id);
60653e5b 212
e683582b 213# ifndef OPENSSL_NO_ENGINE
c2976edf 214 if (e == NULL && pkey != NULL)
d19b01ad 215 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
0f113f3e 216 /* Try to find an ENGINE which implements this method */
4d4de19e 217 if (e != NULL) {
0f113f3e 218 if (!ENGINE_init(e)) {
9311d0c4 219 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
220 return NULL;
221 }
c2976edf 222 } else {
0f113f3e 223 e = ENGINE_get_pkey_meth_engine(id);
c2976edf 224 }
0f113f3e
MC
225
226 /*
0d4fb843 227 * If an ENGINE handled this method look it up. Otherwise use internal
0f113f3e
MC
228 * tables.
229 */
50914496 230 if (e != NULL)
0f113f3e 231 pmeth = ENGINE_get_pkey_meth(e, id);
1acc3e8c
MSP
232 else
233# endif /* OPENSSL_NO_ENGINE */
234 if (pkey != NULL && pkey->foreign)
b247113c 235 pmeth = EVP_PKEY_meth_find(id);
50914496 236 else
929f651e 237 app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
c9777d26 238
d0ea49a8 239 /* END legacy */
f844f9eb 240#endif /* FIPS_MODULE */
d0ea49a8 241 common:
4b9e90f4 242 /*
929f651e
MC
243 * If there's no engine and no app supplied pmeth and there's a name, we try
244 * fetching a provider implementation.
4b9e90f4 245 */
929f651e 246 if (e == NULL && app_pmeth == NULL && keytype != NULL) {
5246183e
RL
247 /*
248 * If |pkey| is given and is provided, we take a reference to its
249 * keymgmt. Otherwise, we fetch one for the keytype we got. This
250 * is to ensure that operation init functions can access what they
251 * need through this single pointer.
252 */
253 if (pkey != NULL && pkey->keymgmt != NULL) {
254 if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
255 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
256 else
257 keymgmt = pkey->keymgmt;
258 } else {
259 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
260 }
d0b79f86 261 if (keymgmt == NULL)
50914496
RL
262 return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
263
264#ifndef FIPS_MODULE
265 /*
266 * Chase down the legacy NID, as that might be needed for diverse
267 * purposes, such as ensure that EVP_PKEY_type() can return sensible
b574c6a9 268 * values. We go through all keymgmt names, because the keytype
50914496
RL
269 * that's passed to this function doesn't necessarily translate
270 * directly.
50914496
RL
271 */
272 if (keymgmt != NULL) {
8aa3781b 273 int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt);
50914496
RL
274
275 if (tmp_id != NID_undef) {
276 if (id == -1) {
277 id = tmp_id;
278 } else {
279 /*
280 * It really really shouldn't differ. If it still does,
281 * something is very wrong.
282 */
283 if (!ossl_assert(id == tmp_id)) {
9311d0c4 284 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
50914496
RL
285 EVP_KEYMGMT_free(keymgmt);
286 return NULL;
287 }
288 }
289 }
b533510f 290 }
50914496
RL
291#endif
292 }
293
294 if (pmeth == NULL && keymgmt == NULL) {
9311d0c4 295 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
50914496
RL
296 } else {
297 ret = OPENSSL_zalloc(sizeof(*ret));
5fcb97c6 298 }
4b9e90f4 299
f844f9eb 300#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
50914496 301 if ((ret == NULL || pmeth == NULL) && e != NULL)
7c96dbcd 302 ENGINE_finish(e);
a63bf2c5 303#endif
50914496
RL
304
305 if (ret == NULL) {
306 EVP_KEYMGMT_free(keymgmt);
0f113f3e
MC
307 return NULL;
308 }
ddfd7182
SL
309 if (propquery != NULL) {
310 ret->propquery = OPENSSL_strdup(propquery);
311 if (ret->propquery == NULL) {
7153f544 312 OPENSSL_free(ret);
ddfd7182
SL
313 EVP_KEYMGMT_free(keymgmt);
314 return NULL;
315 }
316 }
3ee348b0 317 ret->libctx = libctx;
4b9e90f4
RL
318 ret->keytype = keytype;
319 ret->keymgmt = keymgmt;
4c3c2633 320 ret->legacy_keytype = id;
0f113f3e
MC
321 ret->engine = e;
322 ret->pmeth = pmeth;
323 ret->operation = EVP_PKEY_OP_UNDEFINED;
324 ret->pkey = pkey;
a6465b3f 325 if (pkey != NULL)
03273d61 326 EVP_PKEY_up_ref(pkey);
0f113f3e 327
8b84b075 328 if (pmeth != NULL && pmeth->init != NULL) {
0f113f3e 329 if (pmeth->init(ret) <= 0) {
83b4049a 330 ret->pmeth = NULL;
0f113f3e
MC
331 EVP_PKEY_CTX_free(ret);
332 return NULL;
333 }
334 }
335
336 return ret;
337}
338
f844f9eb 339/*- All methods below can also be used in FIPS_MODULE */
e683582b 340
b4250010 341EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
e683582b
SL
342 const char *name,
343 const char *propquery)
344{
345 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
346}
347
b4250010 348EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
2ee4a50a 349 const char *propquery)
e683582b 350{
2ee4a50a 351 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
e683582b
SL
352}
353
864b89ce
MC
354void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
355{
e683582b 356 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
7c14d0c1
SL
357 if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
358 ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
864b89ce 359 EVP_SIGNATURE_free(ctx->op.sig.signature);
7c14d0c1 360 ctx->op.sig.algctx = NULL;
fb1ecf85 361 ctx->op.sig.signature = NULL;
62f49b90 362 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
7c14d0c1
SL
363 if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
364 ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
e683582b 365 EVP_KEYEXCH_free(ctx->op.kex.exchange);
7c14d0c1 366 ctx->op.kex.algctx = NULL;
e683582b 367 ctx->op.kex.exchange = NULL;
80f4fd18 368 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
7c14d0c1
SL
369 if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
370 ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
80f4fd18 371 EVP_KEM_free(ctx->op.encap.kem);
7c14d0c1 372 ctx->op.encap.algctx = NULL;
80f4fd18
SL
373 ctx->op.encap.kem = NULL;
374 }
375 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
7c14d0c1
SL
376 if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
377 ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
2c938e2e 378 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
7c14d0c1 379 ctx->op.ciph.algctx = NULL;
2c938e2e 380 ctx->op.ciph.cipher = NULL;
62924755
RL
381 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
382 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
383 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
864b89ce
MC
384 }
385}
386
e683582b 387void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
0f113f3e 388{
e683582b
SL
389 if (ctx == NULL)
390 return;
391 if (ctx->pmeth && ctx->pmeth->cleanup)
392 ctx->pmeth->cleanup(ctx);
b4faea50 393
e683582b 394 evp_pkey_ctx_free_old_ops(ctx);
86df26b3
RL
395#ifndef FIPS_MODULE
396 evp_pkey_ctx_free_all_cached_data(ctx);
397#endif
e683582b 398 EVP_KEYMGMT_free(ctx->keymgmt);
0f113f3e 399
ddfd7182 400 OPENSSL_free(ctx->propquery);
e683582b
SL
401 EVP_PKEY_free(ctx->pkey);
402 EVP_PKEY_free(ctx->peerkey);
f844f9eb 403#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
e683582b
SL
404 ENGINE_finish(ctx->engine);
405#endif
3786d748 406 BN_free(ctx->rsa_pubexp);
e683582b 407 OPENSSL_free(ctx);
0f113f3e 408}
ba30bad5 409
f844f9eb 410#ifndef FIPS_MODULE
e683582b 411
f830c68f 412void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
0f113f3e
MC
413 const EVP_PKEY_METHOD *meth)
414{
415 if (ppkey_id)
416 *ppkey_id = meth->pkey_id;
417 if (pflags)
418 *pflags = meth->flags;
419}
f830c68f
DSH
420
421void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
0f113f3e 422{
4cbb196b
AT
423 int pkey_id = dst->pkey_id;
424 int flags = dst->flags;
f830c68f 425
4cbb196b 426 *dst = *src;
f830c68f 427
4cbb196b
AT
428 /* We only copy the function pointers so restore the other values */
429 dst->pkey_id = pkey_id;
430 dst->flags = flags;
0f113f3e 431}
f830c68f 432
ba30bad5 433void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
434{
435 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
436 OPENSSL_free(pmeth);
437}
ba30bad5 438
f5cda4cb 439EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
0f113f3e 440{
3ee348b0 441 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
0f113f3e 442}
f5cda4cb
DSH
443
444EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
0f113f3e 445{
3ee348b0 446 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
a07c17ef
RL
447}
448
9fdcc21f 449EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
0f113f3e
MC
450{
451 EVP_PKEY_CTX *rctx;
ff64702b 452
e683582b 453# ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
454 /* Make sure it's safe to copy a pkey context using an ENGINE */
455 if (pctx->engine && !ENGINE_init(pctx->engine)) {
9311d0c4 456 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
457 return 0;
458 }
e683582b 459# endif
ff64702b 460 rctx = OPENSSL_zalloc(sizeof(*rctx));
e077455e 461 if (rctx == NULL)
0f113f3e 462 return NULL;
8bdcef40 463
ff64702b
MC
464 if (pctx->pkey != NULL)
465 EVP_PKEY_up_ref(pctx->pkey);
466 rctx->pkey = pctx->pkey;
467 rctx->operation = pctx->operation;
3ee348b0 468 rctx->libctx = pctx->libctx;
f23bc0b7 469 rctx->keytype = pctx->keytype;
ddfd7182
SL
470 rctx->propquery = NULL;
471 if (pctx->propquery != NULL) {
472 rctx->propquery = OPENSSL_strdup(pctx->propquery);
74cd923a
RL
473 if (rctx->propquery == NULL)
474 goto err;
ddfd7182 475 }
ac7750bb 476 rctx->legacy_keytype = pctx->legacy_keytype;
ff64702b 477
864b89ce
MC
478 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
479 if (pctx->op.kex.exchange != NULL) {
480 rctx->op.kex.exchange = pctx->op.kex.exchange;
ac7750bb 481 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
74cd923a 482 goto err;
ff64702b 483 }
7c14d0c1 484 if (pctx->op.kex.algctx != NULL) {
864b89ce 485 if (!ossl_assert(pctx->op.kex.exchange != NULL))
74cd923a 486 goto err;
864c70e4 487
488 if (pctx->op.kex.exchange->dupctx != NULL)
489 rctx->op.kex.algctx
490 = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
491
7c14d0c1 492 if (rctx->op.kex.algctx == NULL) {
864b89ce 493 EVP_KEYEXCH_free(rctx->op.kex.exchange);
85407b77 494 rctx->op.kex.exchange = NULL;
74cd923a 495 goto err;
864b89ce
MC
496 }
497 return rctx;
498 }
499 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
500 if (pctx->op.sig.signature != NULL) {
501 rctx->op.sig.signature = pctx->op.sig.signature;
ac7750bb 502 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
74cd923a 503 goto err;
864b89ce 504 }
7c14d0c1 505 if (pctx->op.sig.algctx != NULL) {
864b89ce 506 if (!ossl_assert(pctx->op.sig.signature != NULL))
74cd923a 507 goto err;
864c70e4 508
509 if (pctx->op.sig.signature->dupctx != NULL)
510 rctx->op.sig.algctx
511 = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
512
7c14d0c1 513 if (rctx->op.sig.algctx == NULL) {
864b89ce 514 EVP_SIGNATURE_free(rctx->op.sig.signature);
85407b77 515 rctx->op.sig.signature = NULL;
74cd923a 516 goto err;
864b89ce
MC
517 }
518 return rctx;
ff64702b 519 }
2c938e2e
MC
520 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
521 if (pctx->op.ciph.cipher != NULL) {
522 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
ac7750bb 523 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
74cd923a 524 goto err;
2c938e2e 525 }
7c14d0c1 526 if (pctx->op.ciph.algctx != NULL) {
2c938e2e 527 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
74cd923a 528 goto err;
864c70e4 529
530 if (pctx->op.ciph.cipher->dupctx != NULL)
531 rctx->op.ciph.algctx
532 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
533
7c14d0c1 534 if (rctx->op.ciph.algctx == NULL) {
2c938e2e 535 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
85407b77 536 rctx->op.ciph.cipher = NULL;
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;
864c70e4 550
551 if (pctx->op.encap.kem->dupctx != NULL)
552 rctx->op.encap.algctx
553 = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
554
7c14d0c1 555 if (rctx->op.encap.algctx == NULL) {
80f4fd18 556 EVP_KEM_free(rctx->op.encap.kem);
85407b77 557 rctx->op.encap.kem = NULL;
74cd923a 558 goto err;
80f4fd18
SL
559 }
560 return rctx;
561 }
ac7750bb
SL
562 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
563 /* Not supported - This would need a gen_dupctx() to work */
74cd923a 564 goto err;
ff64702b
MC
565 }
566
0f113f3e 567 rctx->pmeth = pctx->pmeth;
e683582b 568# ifndef OPENSSL_NO_ENGINE
0f113f3e 569 rctx->engine = pctx->engine;
e683582b 570# endif
8bdcef40 571
ac7750bb 572 if (pctx->peerkey != NULL)
03273d61 573 EVP_PKEY_up_ref(pctx->peerkey);
0f113f3e 574 rctx->peerkey = pctx->peerkey;
8bdcef40 575
ac7750bb
SL
576 if (pctx->pmeth == NULL) {
577 if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
578 EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
579 void *provkey;
580
581 provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
582 &tmp_keymgmt, pctx->propquery);
583 if (provkey == NULL)
584 goto err;
585 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
586 goto err;
587 EVP_KEYMGMT_free(rctx->keymgmt);
588 rctx->keymgmt = tmp_keymgmt;
589 return rctx;
590 }
74cd923a 591 } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
0f113f3e 592 return rctx;
74cd923a 593 }
ac7750bb 594err:
83b4049a 595 rctx->pmeth = NULL;
0f113f3e
MC
596 EVP_PKEY_CTX_free(rctx);
597 return NULL;
0f113f3e 598}
8bdcef40 599
ba30bad5 600int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
601{
602 if (app_pkey_methods == NULL) {
603 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
1287dabd 604 if (app_pkey_methods == NULL) {
e077455e 605 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
0f113f3e 606 return 0;
3484236d 607 }
0f113f3e 608 }
3484236d 609 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
e077455e 610 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
0f113f3e 611 return 0;
3484236d 612 }
0f113f3e
MC
613 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
614 return 1;
615}
ba30bad5 616
0822e89a
PY
617void evp_app_cleanup_int(void)
618{
619 if (app_pkey_methods != NULL)
620 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
621}
622
623int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
624{
625 const EVP_PKEY_METHOD *ret;
626
627 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
628
629 return ret == NULL ? 0 : 1;
630}
631
48ed9c23
DSH
632size_t EVP_PKEY_meth_get_count(void)
633{
634 size_t rv = OSSL_NELEM(standard_methods);
635
636 if (app_pkey_methods)
637 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
638 return rv;
639}
640
641const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
642{
643 if (idx < OSSL_NELEM(standard_methods))
19bd1fa1 644 return (standard_methods[idx])();
48ed9c23
DSH
645 if (app_pkey_methods == NULL)
646 return NULL;
647 idx -= OSSL_NELEM(standard_methods);
648 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
649 return NULL;
650 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
651}
e683582b 652#endif
48ed9c23 653
6179dfc7
RL
654int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
655{
656#ifndef FIPS_MODULE
657 if (evp_pkey_ctx_is_legacy(ctx))
658 return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
659#endif
660 return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
661}
662
56784203 663int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
0f113f3e 664{
6fcd92d3
RL
665 switch (evp_pkey_ctx_state(ctx)) {
666 case EVP_PKEY_STATE_PROVIDER:
667 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
e683582b
SL
668 && ctx->op.kex.exchange != NULL
669 && ctx->op.kex.exchange->set_ctx_params != NULL)
6fcd92d3 670 return
7c14d0c1 671 ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
6fcd92d3
RL
672 params);
673 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
e683582b
SL
674 && ctx->op.sig.signature != NULL
675 && ctx->op.sig.signature->set_ctx_params != NULL)
6fcd92d3 676 return
7c14d0c1 677 ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
6fcd92d3
RL
678 params);
679 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
e683582b
SL
680 && ctx->op.ciph.cipher != NULL
681 && ctx->op.ciph.cipher->set_ctx_params != NULL)
6fcd92d3 682 return
7c14d0c1 683 ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
6fcd92d3
RL
684 params);
685 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
686 && ctx->keymgmt != NULL
687 && ctx->keymgmt->gen_set_params != NULL)
688 return
689 evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
690 params);
691 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
692 && ctx->op.encap.kem != NULL
693 && ctx->op.encap.kem->set_ctx_params != NULL)
694 return
7c14d0c1 695 ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
6fcd92d3
RL
696 params);
697 break;
698#ifndef FIPS_MODULE
699 case EVP_PKEY_STATE_UNKNOWN:
700 case EVP_PKEY_STATE_LEGACY:
701 return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
702#endif
703 }
e683582b 704 return 0;
0f113f3e 705}
5da98aa6 706
9c45222d
MC
707int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
708{
6fcd92d3
RL
709 switch (evp_pkey_ctx_state(ctx)) {
710 case EVP_PKEY_STATE_PROVIDER:
711 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
4fe54d67
NT
712 && ctx->op.kex.exchange != NULL
713 && ctx->op.kex.exchange->get_ctx_params != NULL)
6fcd92d3 714 return
7c14d0c1 715 ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
6fcd92d3
RL
716 params);
717 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
864b89ce
MC
718 && ctx->op.sig.signature != NULL
719 && ctx->op.sig.signature->get_ctx_params != NULL)
6fcd92d3 720 return
7c14d0c1 721 ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
6fcd92d3
RL
722 params);
723 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
2c938e2e
MC
724 && ctx->op.ciph.cipher != NULL
725 && ctx->op.ciph.cipher->get_ctx_params != NULL)
6fcd92d3 726 return
7c14d0c1 727 ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
6fcd92d3
RL
728 params);
729 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
730 && ctx->op.encap.kem != NULL
731 && ctx->op.encap.kem->get_ctx_params != NULL)
732 return
7c14d0c1 733 ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
6fcd92d3
RL
734 params);
735 break;
736#ifndef FIPS_MODULE
737 case EVP_PKEY_STATE_UNKNOWN:
738 case EVP_PKEY_STATE_LEGACY:
739 return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
740#endif
741 }
9c45222d
MC
742 return 0;
743}
744
11a1b341 745#ifndef FIPS_MODULE
56784203 746const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
9c45222d 747{
18ec26ba
P
748 void *provctx;
749
4fe54d67
NT
750 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
751 && ctx->op.kex.exchange != NULL
18ec26ba 752 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
ed576acd 753 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
7c14d0c1 754 return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
fb67126e 755 provctx);
18ec26ba 756 }
864b89ce
MC
757 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
758 && ctx->op.sig.signature != NULL
18ec26ba
P
759 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
760 provctx = ossl_provider_ctx(
ed576acd 761 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
7c14d0c1 762 return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
fb67126e 763 provctx);
18ec26ba 764 }
2c938e2e
MC
765 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
766 && ctx->op.ciph.cipher != NULL
18ec26ba
P
767 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
768 provctx = ossl_provider_ctx(
ed576acd 769 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
7c14d0c1 770 return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
fb67126e 771 provctx);
18ec26ba 772 }
80f4fd18
SL
773 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
774 && ctx->op.encap.kem != NULL
775 && ctx->op.encap.kem->gettable_ctx_params != NULL) {
ed576acd 776 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
7c14d0c1 777 return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
fb67126e 778 provctx);
80f4fd18 779 }
9c45222d
MC
780 return NULL;
781}
782
56784203 783const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
9c45222d 784{
18ec26ba
P
785 void *provctx;
786
864b89ce
MC
787 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
788 && ctx->op.kex.exchange != NULL
18ec26ba 789 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
ed576acd 790 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
7c14d0c1 791 return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
fb67126e 792 provctx);
18ec26ba 793 }
864b89ce
MC
794 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
795 && ctx->op.sig.signature != NULL
18ec26ba
P
796 && ctx->op.sig.signature->settable_ctx_params != NULL) {
797 provctx = ossl_provider_ctx(
ed576acd 798 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
7c14d0c1 799 return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
fb67126e 800 provctx);
18ec26ba 801 }
2c938e2e
MC
802 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
803 && ctx->op.ciph.cipher != NULL
18ec26ba
P
804 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
805 provctx = ossl_provider_ctx(
ed576acd 806 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
7c14d0c1 807 return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
fb67126e 808 provctx);
18ec26ba 809 }
62924755 810 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
fb67126e
TM
811 && ctx->keymgmt != NULL
812 && ctx->keymgmt->gen_settable_params != NULL) {
ed576acd 813 provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
fb67126e
TM
814 return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
815 provctx);
816 }
80f4fd18
SL
817 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
818 && ctx->op.encap.kem != NULL
819 && ctx->op.encap.kem->settable_ctx_params != NULL) {
ed576acd 820 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
7c14d0c1 821 return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
fb67126e 822 provctx);
80f4fd18 823 }
9c45222d
MC
824 return NULL;
825}
826
4fe54d67
NT
827/*
828 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
829 *
830 * Return 1 on success, 0 or negative for errors.
831 *
832 * In particular they return -2 if any of the params is not supported.
833 *
f844f9eb 834 * They are not available in FIPS_MODULE as they depend on
4fe54d67
NT
835 * - EVP_PKEY_CTX_{get,set}_params()
836 * - EVP_PKEY_CTX_{gettable,settable}_params()
837 *
838 */
839int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
840{
4fe54d67
NT
841 if (ctx == NULL || params == NULL)
842 return 0;
843
51373129
RL
844 /*
845 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
846 * depend on the translation that happens in EVP_PKEY_CTX_set_params()
847 * call, and that the resulting ctrl call will return -2 if it doesn't
848 * known the ctrl command number.
849 */
850 if (evp_pkey_ctx_is_provided(ctx)) {
851 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
852 const OSSL_PARAM *p;
853
854 for (p = params; p->key != NULL; p++) {
855 /* Check the ctx actually understands this parameter */
1287dabd 856 if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
51373129
RL
857 return -2;
858 }
4fe54d67
NT
859 }
860
861 return EVP_PKEY_CTX_set_params(ctx, params);
862}
863
864int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
865{
4fe54d67
NT
866 if (ctx == NULL || params == NULL)
867 return 0;
868
51373129
RL
869 /*
870 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
871 * depend on the translation that happens in EVP_PKEY_CTX_get_params()
872 * call, and that the resulting ctrl call will return -2 if it doesn't
873 * known the ctrl command number.
874 */
875 if (evp_pkey_ctx_is_provided(ctx)) {
876 const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
877 const OSSL_PARAM *p;
878
1287dabd 879 for (p = params; p->key != NULL; p++) {
51373129 880 /* Check the ctx actually understands this parameter */
1287dabd 881 if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
51373129
RL
882 return -2;
883 }
4fe54d67
NT
884 }
885
886 return EVP_PKEY_CTX_get_params(ctx, params);
887}
888
9c45222d
MC
889int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
890{
ac2d58c7 891 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
9c45222d
MC
892 /* 80 should be big enough */
893 char name[80] = "";
894 const EVP_MD *tmp;
895
864b89ce 896 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
897 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
898 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
899 return -2;
900 }
901
7c14d0c1 902 if (ctx->op.sig.algctx == NULL)
9c45222d
MC
903 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
904 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
905
906 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
907 name,
908 sizeof(name));
ac2d58c7 909 *p = OSSL_PARAM_construct_end();
9c45222d
MC
910
911 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
912 return 0;
913
7606bed9 914 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
9c45222d
MC
915 if (tmp == NULL)
916 return 0;
917
918 *md = tmp;
919
920 return 1;
921}
922
05d2f72e
MC
923static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
924 int fallback, const char *param, int op,
925 int ctrl)
4889dadc 926{
05d2f72e 927 OSSL_PARAM md_params[2], *p = md_params;
4889dadc
MC
928 const char *name;
929
05d2f72e 930 if (ctx == NULL || (ctx->operation & op) == 0) {
9c45222d
MC
931 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
932 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
933 return -2;
934 }
935
05d2f72e
MC
936 if (fallback)
937 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
4889dadc 938
9c45222d
MC
939 if (md == NULL) {
940 name = "";
9c45222d 941 } else {
ed576acd 942 name = EVP_MD_get0_name(md);
9c45222d 943 }
4889dadc 944
05d2f72e 945 *p++ = OSSL_PARAM_construct_utf8_string(param,
9c45222d
MC
946 /*
947 * Cast away the const. This is read
948 * only so should be safe
949 */
8b6ffd40 950 (char *)name, 0);
ac2d58c7 951 *p = OSSL_PARAM_construct_end();
4889dadc 952
05d2f72e
MC
953 return EVP_PKEY_CTX_set_params(ctx, md_params);
954}
955
956int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
957{
7c14d0c1 958 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
05d2f72e
MC
959 OSSL_SIGNATURE_PARAM_DIGEST,
960 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
4889dadc
MC
961}
962
ac2d58c7
MC
963int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
964{
7c14d0c1 965 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
05d2f72e
MC
966 OSSL_KDF_PARAM_DIGEST,
967 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
968}
969
970static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
971 const char *param, int op, int ctrl,
972 const unsigned char *data,
973 int datalen)
974{
975 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
ac2d58c7 976
5d51925a 977 if (ctx == NULL || (ctx->operation & op) == 0) {
ac2d58c7
MC
978 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
979 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
980 return -2;
981 }
982
0be6cf0c 983 /* Code below to be removed when legacy support is dropped. */
05d2f72e
MC
984 if (fallback)
985 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
0be6cf0c 986 /* end of legacy support */
ac2d58c7 987
05d2f72e
MC
988 if (datalen < 0) {
989 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
990 return 0;
ac2d58c7
MC
991 }
992
05d2f72e 993 *p++ = OSSL_PARAM_construct_octet_string(param,
ac2d58c7
MC
994 /*
995 * Cast away the const. This is read
996 * only so should be safe
997 */
05d2f72e
MC
998 (unsigned char *)data,
999 (size_t)datalen);
194de849 1000 *p = OSSL_PARAM_construct_end();
ac2d58c7 1001
05d2f72e 1002 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
ac2d58c7
MC
1003}
1004
6b566687
TS
1005static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
1006 const char *param, int op, int ctrl,
1007 const unsigned char *data,
1008 int datalen)
1009{
1010 OSSL_PARAM os_params[2];
1011 unsigned char *info = NULL;
1012 size_t info_len = 0;
1013 size_t info_alloc = 0;
1014 int ret = 0;
1015
1016 if (ctx == NULL || (ctx->operation & op) == 0) {
1017 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1018 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1019 return -2;
1020 }
1021
1022 /* Code below to be removed when legacy support is dropped. */
1023 if (fallback)
1024 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1025 /* end of legacy support */
1026
1027 if (datalen < 0) {
1028 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1029 return 0;
299996fb 1030 } else if (datalen == 0) {
1031 return 1;
6b566687
TS
1032 }
1033
1034 /* Get the original value length */
1035 os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
1036 os_params[1] = OSSL_PARAM_construct_end();
1037
1038 if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1039 return 0;
1040
1041 /* Older provider that doesn't support getting this parameter */
1042 if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
1043 return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl, data, datalen);
1044
1045 info_alloc = os_params[0].return_size + datalen;
1046 if (info_alloc == 0)
1047 return 0;
1048 info = OPENSSL_zalloc(info_alloc);
1049 if (info == NULL)
1050 return 0;
1051 info_len = os_params[0].return_size;
1052
1053 os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
1054
1055 /* if we have data, then go get it */
1056 if (info_len > 0) {
1057 if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1058 goto error;
1059 }
1060
1061 /* Copy the input data */
1062 memcpy(&info[info_len], data, datalen);
1063 ret = EVP_PKEY_CTX_set_params(ctx, os_params);
1064
1065 error:
1066 OPENSSL_clear_free(info, info_alloc);
1067 return ret;
1068}
1069
ac2d58c7
MC
1070int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1071 const unsigned char *sec, int seclen)
1072{
7c14d0c1 1073 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
05d2f72e
MC
1074 OSSL_KDF_PARAM_SECRET,
1075 EVP_PKEY_OP_DERIVE,
1076 EVP_PKEY_CTRL_TLS_SECRET,
1077 sec, seclen);
1078}
ac2d58c7 1079
05d2f72e
MC
1080int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1081 const unsigned char *seed, int seedlen)
1082{
7c14d0c1 1083 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
05d2f72e
MC
1084 OSSL_KDF_PARAM_SEED,
1085 EVP_PKEY_OP_DERIVE,
1086 EVP_PKEY_CTRL_TLS_SEED,
1087 seed, seedlen);
1088}
ac2d58c7 1089
05d2f72e
MC
1090int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1091{
7c14d0c1 1092 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
05d2f72e
MC
1093 OSSL_KDF_PARAM_DIGEST,
1094 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1095}
ac2d58c7 1096
05d2f72e
MC
1097int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1098 const unsigned char *salt, int saltlen)
1099{
7c14d0c1 1100 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
05d2f72e
MC
1101 OSSL_KDF_PARAM_SALT,
1102 EVP_PKEY_OP_DERIVE,
1103 EVP_PKEY_CTRL_HKDF_SALT,
1104 salt, saltlen);
1105}
ac2d58c7 1106
05d2f72e
MC
1107int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1108 const unsigned char *key, int keylen)
1109{
7c14d0c1 1110 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
05d2f72e
MC
1111 OSSL_KDF_PARAM_KEY,
1112 EVP_PKEY_OP_DERIVE,
1113 EVP_PKEY_CTRL_HKDF_KEY,
1114 key, keylen);
1115}
ac2d58c7 1116
05d2f72e
MC
1117int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1118 const unsigned char *info, int infolen)
1119{
6b566687 1120 return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
05d2f72e
MC
1121 OSSL_KDF_PARAM_INFO,
1122 EVP_PKEY_OP_DERIVE,
1123 EVP_PKEY_CTRL_HKDF_INFO,
1124 info, infolen);
ac2d58c7
MC
1125}
1126
ed576acd 1127int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
ac2d58c7 1128{
05d2f72e 1129 OSSL_PARAM int_params[2], *p = int_params;
ac2d58c7
MC
1130
1131 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1132 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1133 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1134 return -2;
1135 }
1136
0be6cf0c 1137 /* Code below to be removed when legacy support is dropped. */
7c14d0c1 1138 if (ctx->op.kex.algctx == NULL)
ac2d58c7 1139 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
05d2f72e 1140 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
0be6cf0c 1141 /* end of legacy support */
ac2d58c7 1142
05d2f72e
MC
1143 if (mode < 0) {
1144 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
ac2d58c7
MC
1145 return 0;
1146 }
1147
05d2f72e 1148 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
194de849 1149 *p = OSSL_PARAM_construct_end();
ac2d58c7 1150
05d2f72e 1151 return EVP_PKEY_CTX_set_params(ctx, int_params);
ac2d58c7
MC
1152}
1153
194de849
MC
1154int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1155 int passlen)
1156{
7c14d0c1 1157 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
194de849
MC
1158 OSSL_KDF_PARAM_PASSWORD,
1159 EVP_PKEY_OP_DERIVE,
1160 EVP_PKEY_CTRL_PASS,
1161 (const unsigned char *)pass, passlen);
1162}
1163
1164int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1165 const unsigned char *salt, int saltlen)
1166{
7c14d0c1 1167 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
194de849
MC
1168 OSSL_KDF_PARAM_SALT,
1169 EVP_PKEY_OP_DERIVE,
1170 EVP_PKEY_CTRL_SCRYPT_SALT,
1171 salt, saltlen);
1172}
1173
1174static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1175 int op, int ctrl, uint64_t val)
1176{
1177 OSSL_PARAM uint64_params[2], *p = uint64_params;
1178
1179 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1180 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1181 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1182 return -2;
1183 }
1184
0be6cf0c 1185 /* Code below to be removed when legacy support is dropped. */
7c14d0c1 1186 if (ctx->op.kex.algctx == NULL)
194de849 1187 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
0be6cf0c 1188 /* end of legacy support */
194de849
MC
1189
1190 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1191 *p = OSSL_PARAM_construct_end();
1192
1193 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1194}
1195
1196int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1197{
1198 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1199 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1200 n);
1201}
1202
1203int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1204{
1205 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1206 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1207 r);
1208}
1209
1210int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1211{
1212 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1213 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1214 p);
1215}
1216
1217int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1218 uint64_t maxmem_bytes)
1219{
1220 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1221 EVP_PKEY_OP_DERIVE,
1222 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1223 maxmem_bytes);
1224}
1225
5d51925a
MC
1226int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1227 int keylen)
1228{
1229 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1230 OSSL_PKEY_PARAM_PRIV_KEY,
1231 EVP_PKEY_OP_KEYGEN,
1232 EVP_PKEY_CTRL_SET_MAC_KEY,
1233 key, keylen);
1234}
1235
80f4fd18
SL
1236int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1237{
1238 OSSL_PARAM params[2], *p = params;
1239
1240 if (ctx == NULL || op == NULL) {
1241 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1242 return 0;
1243 }
1244 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1245 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1246 return -2;
1247 }
1248 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1249 (char *)op, 0);
1250 *p = OSSL_PARAM_construct_end();
1251 return EVP_PKEY_CTX_set_params(ctx, params);
1252}
1253
8d6481f5
RL
1254int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1255{
1256 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1257 EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1258}
1259
8d6481f5
RL
1260int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1261{
1262 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1263}
1264
1265int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1266{
1267 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1268 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1269}
1270
86df26b3
RL
1271static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1272 int cmd, int p1, void *p2)
0f113f3e 1273{
86df26b3 1274 int ret = 0;
4803717f 1275
86df26b3
RL
1276 /*
1277 * If the method has a |digest_custom| function, we can relax the
1278 * operation type check, since this can be called before the operation
1279 * is initialized.
1280 */
1281 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1282 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
9311d0c4 1283 ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
86df26b3
RL
1284 return -1;
1285 }
35aca9ec 1286
86df26b3 1287 if ((optype != -1) && !(ctx->operation & optype)) {
9311d0c4 1288 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
86df26b3
RL
1289 return -1;
1290 }
0f113f3e 1291 }
0f113f3e 1292
86df26b3
RL
1293 switch (evp_pkey_ctx_state(ctx)) {
1294 case EVP_PKEY_STATE_PROVIDER:
5524580b 1295 return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
86df26b3
RL
1296 case EVP_PKEY_STATE_UNKNOWN:
1297 case EVP_PKEY_STATE_LEGACY:
1298 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
9311d0c4 1299 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3
RL
1300 return -2;
1301 }
1302 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1303 return -1;
4803717f 1304
86df26b3 1305 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
0f113f3e 1306
86df26b3 1307 if (ret == -2)
9311d0c4 1308 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3 1309 break;
0f113f3e 1310 }
86df26b3
RL
1311 return ret;
1312}
0f113f3e 1313
86df26b3
RL
1314int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1315 int cmd, int p1, void *p2)
1316{
1317 int ret = 0;
1318
d65ab22e 1319 if (ctx == NULL) {
9311d0c4 1320 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
d65ab22e
SL
1321 return -2;
1322 }
86df26b3
RL
1323 /* If unsupported, we don't want that reported here */
1324 ERR_set_mark();
1325 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1326 cmd, NULL, p2, p1);
1327 if (ret == -2) {
1328 ERR_pop_to_mark();
1329 } else {
1330 ERR_clear_last_mark();
1331 /*
1332 * If there was an error, there was an error.
1333 * If the operation isn't initialized yet, we also return, as
1334 * the saved values will be used then anyway.
1335 */
1336 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1337 return ret;
1338 }
86df26b3 1339 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
0f113f3e 1340}
0b6f3c66 1341
cefa762e 1342int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 1343 int cmd, uint64_t value)
cefa762e
JB
1344{
1345 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1346}
1347
19dbb742 1348
86df26b3
RL
1349static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1350 const char *name, const char *value)
0f113f3e 1351{
86df26b3
RL
1352 int ret = 0;
1353
35aca9ec 1354 if (ctx == NULL) {
9311d0c4 1355 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
35aca9ec
MC
1356 return -2;
1357 }
1358
86df26b3
RL
1359 switch (evp_pkey_ctx_state(ctx)) {
1360 case EVP_PKEY_STATE_PROVIDER:
5524580b 1361 return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
86df26b3
RL
1362 case EVP_PKEY_STATE_UNKNOWN:
1363 case EVP_PKEY_STATE_LEGACY:
1364 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
9311d0c4 1365 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
86df26b3
RL
1366 return -2;
1367 }
1368 if (strcmp(name, "digest") == 0)
b9689452
RL
1369 ret = EVP_PKEY_CTX_md(ctx,
1370 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
86df26b3
RL
1371 EVP_PKEY_CTRL_MD, value);
1372 else
1373 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1374 break;
1375 }
35aca9ec 1376
86df26b3
RL
1377 return ret;
1378}
1379
1380int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1381 const char *name, const char *value)
1382{
1383 int ret = 0;
1384
1385 /* If unsupported, we don't want that reported here */
1386 ERR_set_mark();
1387 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1388 name, value, strlen(value) + 1);
1389 if (ret == -2) {
1390 ERR_pop_to_mark();
1391 } else {
1392 ERR_clear_last_mark();
1393 /*
1394 * If there was an error, there was an error.
1395 * If the operation isn't initialized yet, we also return, as
1396 * the saved values will be used then anyway.
1397 */
1398 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1399 return ret;
1400 }
1401
1402 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1403}
1404
1405static int decode_cmd(int cmd, const char *name)
1406{
1407 if (cmd == -1) {
1408 /*
1409 * The consequence of the assertion not being true is that this
1410 * function will return -1, which will cause the calling functions
1411 * to signal that the command is unsupported... in non-debug mode.
1412 */
1413 if (ossl_assert(name != NULL))
1414 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1415 cmd = EVP_PKEY_CTRL_SET1_ID;
1416 }
1417
1418 return cmd;
1419}
1420
1421static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1422 int keytype, int optype,
1423 int cmd, const char *name,
1424 const void *data, size_t data_len)
1425{
bbf4dc96
RL
1426 /*
1427 * Check that it's one of the supported commands. The ctrl commands
1428 * number cases here must correspond to the cases in the bottom switch
1429 * in this function.
1430 */
1431 switch (cmd = decode_cmd(cmd, name)) {
1432 case EVP_PKEY_CTRL_SET1_ID:
1433 break;
1434 default:
1435 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1436 return -2;
1437 }
1438
977e95b9
RL
1439 if (keytype != -1) {
1440 switch (evp_pkey_ctx_state(ctx)) {
1441 case EVP_PKEY_STATE_PROVIDER:
1442 if (ctx->keymgmt == NULL) {
1443 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1444 return -2;
1445 }
1446 if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1447 evp_pkey_type2name(keytype))) {
1448 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1449 return -1;
1450 }
1451 break;
1452 case EVP_PKEY_STATE_UNKNOWN:
1453 case EVP_PKEY_STATE_LEGACY:
1454 if (ctx->pmeth == NULL) {
1455 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1456 return -2;
1457 }
bbf4dc96 1458 if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
977e95b9
RL
1459 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1460 return -1;
1461 }
1462 break;
1463 }
1464 }
1465 if (optype != -1 && (ctx->operation & optype) == 0) {
86df26b3
RL
1466 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1467 return -1;
0f113f3e 1468 }
86df26b3 1469
86df26b3
RL
1470 switch (cmd) {
1471 case EVP_PKEY_CTRL_SET1_ID:
1472 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1473 if (name != NULL) {
1474 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
e077455e 1475 if (ctx->cached_parameters.dist_id_name == NULL)
86df26b3 1476 return 0;
86df26b3
RL
1477 }
1478 if (data_len > 0) {
1479 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
e077455e 1480 if (ctx->cached_parameters.dist_id == NULL)
86df26b3 1481 return 0;
86df26b3
RL
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 */