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