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