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