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