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