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