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