]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
Prepare for 3.0 alpha 4
[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>
35aca9ec 24#include "internal/cryptlib.h"
25f2138b
DMSP
25#include "crypto/asn1.h"
26#include "crypto/evp.h"
7165593c
SL
27#include "crypto/dh.h"
28#include "internal/ffc.h"
99119000 29#include "internal/numbers.h"
390acbeb 30#include "internal/provider.h"
706457b7 31#include "evp_local.h"
0b6f3c66 32
f844f9eb 33#ifndef FIPS_MODULE
e683582b 34
19bd1fa1 35typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
0f113f3e 36typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
5ce278a7 37
df2ee0e2 38static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
0b6f3c66 39
cefa762e 40/* This array needs to be in order of NIDs */
19bd1fa1 41static pmeth_fn standard_methods[] = {
e683582b 42# ifndef OPENSSL_NO_RSA
19bd1fa1 43 rsa_pkey_method,
e683582b
SL
44# endif
45# ifndef OPENSSL_NO_DH
19bd1fa1 46 dh_pkey_method,
e683582b
SL
47# endif
48# ifndef OPENSSL_NO_DSA
19bd1fa1 49 dsa_pkey_method,
e683582b
SL
50# endif
51# ifndef OPENSSL_NO_EC
19bd1fa1 52 ec_pkey_method,
e683582b 53# endif
19bd1fa1 54 hmac_pkey_method,
e683582b 55# ifndef OPENSSL_NO_CMAC
19bd1fa1 56 cmac_pkey_method,
e683582b
SL
57# endif
58# ifndef OPENSSL_NO_RSA
19bd1fa1 59 rsa_pss_pkey_method,
e683582b
SL
60# endif
61# ifndef OPENSSL_NO_DH
19bd1fa1 62 dhx_pkey_method,
e683582b
SL
63# endif
64# ifndef OPENSSL_NO_SCRYPT
19bd1fa1 65 scrypt_pkey_method,
e683582b 66# endif
19bd1fa1 67 tls1_prf_pkey_method,
e683582b 68# ifndef OPENSSL_NO_EC
19bd1fa1
PS
69 ecx25519_pkey_method,
70 ecx448_pkey_method,
e683582b 71# endif
19bd1fa1 72 hkdf_pkey_method,
e683582b 73# ifndef OPENSSL_NO_POLY1305
19bd1fa1 74 poly1305_pkey_method,
e683582b
SL
75# endif
76# ifndef OPENSSL_NO_SIPHASH
19bd1fa1 77 siphash_pkey_method,
e683582b
SL
78# endif
79# ifndef OPENSSL_NO_EC
19bd1fa1
PS
80 ed25519_pkey_method,
81 ed448_pkey_method,
e683582b
SL
82# endif
83# ifndef OPENSSL_NO_SM2
19bd1fa1 84 sm2_pkey_method,
e683582b 85# endif
0f113f3e 86};
0b6f3c66 87
19bd1fa1
PS
88DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
89
90static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
91{
92 return ((*a)->pkey_id - ((**b)())->pkey_id);
93}
94
95IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
babb3798 96
0f113f3e
MC
97static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
98 const EVP_PKEY_METHOD *const *b)
99{
100 return ((*a)->pkey_id - (*b)->pkey_id);
101}
0b6f3c66 102
c9777d26 103const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
0f113f3e 104{
19bd1fa1 105 pmeth_fn *ret;
0f113f3e 106 EVP_PKEY_METHOD tmp;
19bd1fa1 107 const EVP_PKEY_METHOD *t = &tmp;
12a765a5 108
0f113f3e
MC
109 tmp.pkey_id = type;
110 if (app_pkey_methods) {
111 int idx;
112 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
113 if (idx >= 0)
114 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
115 }
19bd1fa1
PS
116 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
117 sizeof(standard_methods) /
118 sizeof(pmeth_fn));
12a765a5 119 if (ret == NULL || *ret == NULL)
0f113f3e 120 return NULL;
19bd1fa1 121 return (**ret)();
0f113f3e 122}
0b6f3c66 123
e683582b
SL
124EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
125{
126 EVP_PKEY_METHOD *pmeth;
127
128 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
129 if (pmeth == NULL) {
130 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
131 return NULL;
132 }
133
134 pmeth->pkey_id = id;
135 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
136 return pmeth;
137}
f844f9eb 138#endif /* FIPS_MODULE */
e683582b 139
b533510f
MC
140static int is_legacy_alg(int id, const char *keytype)
141{
142#ifndef FIPS_MODULE
143 /* Certain EVP_PKEY keytypes are only available in legacy form */
144 if (id == -1) {
145 id = OBJ_sn2nid(keytype);
146 if (id == NID_undef)
147 id = OBJ_ln2nid(keytype);
148 if (id == NID_undef)
149 return 0;
150 }
151 switch (id) {
152 /*
153 * TODO(3.0): Remove SM2 and DHX when they are converted to have provider
154 * support
155 */
156 case EVP_PKEY_SM2:
157 case EVP_PKEY_DHX:
158 case EVP_PKEY_SCRYPT:
159 case EVP_PKEY_TLS1_PRF:
160 case EVP_PKEY_HKDF:
161 case EVP_PKEY_CMAC:
162 case EVP_PKEY_HMAC:
163 case EVP_PKEY_SIPHASH:
164 case EVP_PKEY_POLY1305:
165 return 1;
166 default:
167 return 0;
168 }
169#else
170 return 0;
171#endif
172}
173
3ee348b0
RL
174static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
175 EVP_PKEY *pkey, ENGINE *e,
4b9e90f4 176 const char *keytype, const char *propquery,
a07c17ef 177 int id)
e683582b 178
0f113f3e
MC
179{
180 EVP_PKEY_CTX *ret;
d0ea49a8 181 const EVP_PKEY_METHOD *pmeth = NULL;
4b9e90f4 182 EVP_KEYMGMT *keymgmt = NULL;
d0ea49a8
RL
183
184 /*
185 * When using providers, the context is bound to the algo implementation
186 * later.
187 */
188 if (pkey == NULL && e == NULL && id == -1)
189 goto common;
2f2e6b62 190
982efd77 191 /*
5e5bc836
RL
192 * If the internal key is provided, we extract the keytype from its
193 * keymgmt and skip over the legacy code.
982efd77 194 */
5e5bc836 195 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
982efd77
RL
196 /* If we have an engine, something went wrong somewhere... */
197 if (!ossl_assert(e == NULL))
198 return NULL;
4b9e90f4 199 keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
982efd77
RL
200 goto common;
201 }
f844f9eb 202#ifndef FIPS_MODULE
d0ea49a8
RL
203 /* TODO(3.0) Legacy code should be removed when all is provider based */
204 /* BEGIN legacy */
0f113f3e 205 if (id == -1) {
a6465b3f 206 if (pkey == NULL)
982efd77 207 return NULL;
2f2e6b62 208 id = pkey->type;
0f113f3e 209 }
60653e5b
RL
210
211 /*
212 * Here, we extract what information we can for the purpose of
213 * supporting usage with implementations from providers, to make
214 * for a smooth transition from legacy stuff to provider based stuff.
215 *
216 * If an engine is given, this is entirely legacy, and we should not
217 * pretend anything else, so we only set the name when no engine is
218 * given. If both are already given, someone made a mistake, and
219 * since that can only happen internally, it's safe to make an
220 * assertion.
221 */
4b9e90f4 222 if (!ossl_assert(e == NULL || keytype == NULL))
60653e5b
RL
223 return NULL;
224 if (e == NULL)
4b9e90f4 225 keytype = OBJ_nid2sn(id);
60653e5b 226
e683582b 227# ifndef OPENSSL_NO_ENGINE
c2976edf 228 if (e == NULL && pkey != NULL)
d19b01ad 229 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
0f113f3e
MC
230 /* Try to find an ENGINE which implements this method */
231 if (e) {
232 if (!ENGINE_init(e)) {
233 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
234 return NULL;
235 }
c2976edf 236 } else {
0f113f3e 237 e = ENGINE_get_pkey_meth_engine(id);
c2976edf 238 }
0f113f3e
MC
239
240 /*
0d4fb843 241 * If an ENGINE handled this method look it up. Otherwise use internal
0f113f3e
MC
242 * tables.
243 */
0f113f3e
MC
244 if (e)
245 pmeth = ENGINE_get_pkey_meth(e, id);
246 else
e683582b 247# endif
0f113f3e 248 pmeth = EVP_PKEY_meth_find(id);
c9777d26 249
0f113f3e 250 if (pmeth == NULL) {
e683582b 251# ifndef OPENSSL_NO_ENGINE
918a27fa 252 ENGINE_finish(e);
e683582b 253# endif
0f113f3e
MC
254 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
255 return NULL;
256 }
d0ea49a8 257 /* END legacy */
f844f9eb 258#endif /* FIPS_MODULE */
d0ea49a8 259 common:
4b9e90f4
RL
260 /*
261 * If there's no engine and there's a name, we try fetching a provider
262 * implementation.
263 */
5fcb97c6 264 if (e == NULL && keytype != NULL) {
b533510f
MC
265 int legacy = is_legacy_alg(id, keytype);
266
267 if (legacy) {
268 /* This could fail so ignore errors */
269 ERR_set_mark();
270 }
271
4b9e90f4 272 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
b533510f
MC
273 if (legacy) {
274 ERR_pop_to_mark();
275 } else if (keymgmt == NULL) {
276 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_FETCH_FAILED);
277 return NULL;
278 }
5fcb97c6 279 }
4b9e90f4 280
64b25758 281 ret = OPENSSL_zalloc(sizeof(*ret));
90945fa3 282 if (ret == NULL) {
b5bcc053 283 EVP_KEYMGMT_free(keymgmt);
f844f9eb 284#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
7c96dbcd 285 ENGINE_finish(e);
a63bf2c5 286#endif
0f113f3e
MC
287 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
288 return NULL;
289 }
3ee348b0 290 ret->libctx = libctx;
a07c17ef 291 ret->propquery = propquery;
4b9e90f4
RL
292 ret->keytype = keytype;
293 ret->keymgmt = keymgmt;
0f113f3e
MC
294 ret->engine = e;
295 ret->pmeth = pmeth;
296 ret->operation = EVP_PKEY_OP_UNDEFINED;
297 ret->pkey = pkey;
a6465b3f 298 if (pkey != NULL)
03273d61 299 EVP_PKEY_up_ref(pkey);
0f113f3e 300
8b84b075 301 if (pmeth != NULL && pmeth->init != NULL) {
0f113f3e 302 if (pmeth->init(ret) <= 0) {
83b4049a 303 ret->pmeth = NULL;
0f113f3e
MC
304 EVP_PKEY_CTX_free(ret);
305 return NULL;
306 }
307 }
308
309 return ret;
310}
311
f844f9eb 312/*- All methods below can also be used in FIPS_MODULE */
e683582b
SL
313
314EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx,
315 const char *name,
316 const char *propquery)
317{
318 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
319}
320
2ee4a50a
MC
321EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey,
322 const char *propquery)
e683582b 323{
2ee4a50a 324 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
e683582b
SL
325}
326
864b89ce
MC
327void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
328{
e683582b 329 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
864b89ce
MC
330 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
331 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
332 EVP_SIGNATURE_free(ctx->op.sig.signature);
fb1ecf85
RL
333 ctx->op.sig.sigprovctx = NULL;
334 ctx->op.sig.signature = NULL;
62f49b90 335 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
e683582b
SL
336 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
337 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
338 EVP_KEYEXCH_free(ctx->op.kex.exchange);
339 ctx->op.kex.exchprovctx = NULL;
340 ctx->op.kex.exchange = NULL;
62f49b90
SL
341 }
342/* TODO(3.0): add dependancies and uncomment this when available for fips mode */
f844f9eb 343#ifndef FIPS_MODULE
62f49b90 344 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
2c938e2e
MC
345 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
346 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
347 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
348 ctx->op.ciph.ciphprovctx = NULL;
349 ctx->op.ciph.cipher = NULL;
62924755
RL
350 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
351 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
352 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
864b89ce 353 }
e683582b 354#endif
864b89ce
MC
355}
356
e683582b 357void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
0f113f3e 358{
e683582b
SL
359 if (ctx == NULL)
360 return;
361 if (ctx->pmeth && ctx->pmeth->cleanup)
362 ctx->pmeth->cleanup(ctx);
b4faea50 363
e683582b
SL
364 evp_pkey_ctx_free_old_ops(ctx);
365 EVP_KEYMGMT_free(ctx->keymgmt);
0f113f3e 366
e683582b
SL
367 EVP_PKEY_free(ctx->pkey);
368 EVP_PKEY_free(ctx->peerkey);
f844f9eb 369#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
e683582b
SL
370 ENGINE_finish(ctx->engine);
371#endif
372 OPENSSL_free(ctx);
0f113f3e 373}
ba30bad5 374
f844f9eb 375#ifndef FIPS_MODULE
e683582b 376
f830c68f 377void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
0f113f3e
MC
378 const EVP_PKEY_METHOD *meth)
379{
380 if (ppkey_id)
381 *ppkey_id = meth->pkey_id;
382 if (pflags)
383 *pflags = meth->flags;
384}
f830c68f
DSH
385
386void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
0f113f3e 387{
f830c68f 388
0f113f3e
MC
389 dst->init = src->init;
390 dst->copy = src->copy;
391 dst->cleanup = src->cleanup;
f830c68f 392
0f113f3e
MC
393 dst->paramgen_init = src->paramgen_init;
394 dst->paramgen = src->paramgen;
f830c68f 395
0f113f3e
MC
396 dst->keygen_init = src->keygen_init;
397 dst->keygen = src->keygen;
f830c68f 398
0f113f3e
MC
399 dst->sign_init = src->sign_init;
400 dst->sign = src->sign;
f830c68f 401
0f113f3e
MC
402 dst->verify_init = src->verify_init;
403 dst->verify = src->verify;
f830c68f 404
0f113f3e
MC
405 dst->verify_recover_init = src->verify_recover_init;
406 dst->verify_recover = src->verify_recover;
f830c68f 407
0f113f3e
MC
408 dst->signctx_init = src->signctx_init;
409 dst->signctx = src->signctx;
f830c68f 410
0f113f3e
MC
411 dst->verifyctx_init = src->verifyctx_init;
412 dst->verifyctx = src->verifyctx;
f830c68f 413
0f113f3e
MC
414 dst->encrypt_init = src->encrypt_init;
415 dst->encrypt = src->encrypt;
f830c68f 416
0f113f3e
MC
417 dst->decrypt_init = src->decrypt_init;
418 dst->decrypt = src->decrypt;
f830c68f 419
0f113f3e
MC
420 dst->derive_init = src->derive_init;
421 dst->derive = src->derive;
f830c68f 422
0f113f3e
MC
423 dst->ctrl = src->ctrl;
424 dst->ctrl_str = src->ctrl_str;
2aee35d3
PY
425
426 dst->check = src->check;
0f113f3e 427}
f830c68f 428
ba30bad5 429void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
430{
431 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
432 OPENSSL_free(pmeth);
433}
ba30bad5 434
f5cda4cb 435EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
0f113f3e 436{
3ee348b0 437 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
0f113f3e 438}
f5cda4cb
DSH
439
440EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
0f113f3e 441{
3ee348b0 442 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
a07c17ef
RL
443}
444
9fdcc21f 445EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
0f113f3e
MC
446{
447 EVP_PKEY_CTX *rctx;
ff64702b
MC
448
449 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
864b89ce
MC
450 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
451 && pctx->op.kex.exchprovctx == NULL)
452 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
453 && pctx->op.sig.sigprovctx == NULL)))
0f113f3e 454 return NULL;
e683582b 455# ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
456 /* Make sure it's safe to copy a pkey context using an ENGINE */
457 if (pctx->engine && !ENGINE_init(pctx->engine)) {
458 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
459 return 0;
460 }
e683582b 461# endif
ff64702b 462 rctx = OPENSSL_zalloc(sizeof(*rctx));
3484236d
F
463 if (rctx == NULL) {
464 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
0f113f3e 465 return NULL;
3484236d 466 }
8bdcef40 467
ff64702b
MC
468 if (pctx->pkey != NULL)
469 EVP_PKEY_up_ref(pctx->pkey);
470 rctx->pkey = pctx->pkey;
471 rctx->operation = pctx->operation;
3ee348b0 472 rctx->libctx = pctx->libctx;
f23bc0b7 473 rctx->keytype = pctx->keytype;
a07c17ef 474 rctx->propquery = pctx->propquery;
ff64702b 475
864b89ce
MC
476 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
477 if (pctx->op.kex.exchange != NULL) {
478 rctx->op.kex.exchange = pctx->op.kex.exchange;
479 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
480 OPENSSL_free(rctx);
481 return NULL;
482 }
ff64702b 483 }
864b89ce
MC
484 if (pctx->op.kex.exchprovctx != NULL) {
485 if (!ossl_assert(pctx->op.kex.exchange != NULL))
486 return NULL;
487 rctx->op.kex.exchprovctx
488 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
489 if (rctx->op.kex.exchprovctx == NULL) {
490 EVP_KEYEXCH_free(rctx->op.kex.exchange);
491 OPENSSL_free(rctx);
492 return NULL;
493 }
494 return rctx;
495 }
496 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
497 if (pctx->op.sig.signature != NULL) {
498 rctx->op.sig.signature = pctx->op.sig.signature;
499 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
500 OPENSSL_free(rctx);
501 return NULL;
502 }
503 }
504 if (pctx->op.sig.sigprovctx != NULL) {
505 if (!ossl_assert(pctx->op.sig.signature != NULL))
506 return NULL;
507 rctx->op.sig.sigprovctx
508 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
509 if (rctx->op.sig.sigprovctx == NULL) {
510 EVP_SIGNATURE_free(rctx->op.sig.signature);
511 OPENSSL_free(rctx);
512 return NULL;
513 }
514 return rctx;
ff64702b 515 }
2c938e2e
MC
516 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
517 if (pctx->op.ciph.cipher != NULL) {
518 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
519 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
520 OPENSSL_free(rctx);
521 return NULL;
522 }
523 }
524 if (pctx->op.ciph.ciphprovctx != NULL) {
525 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
526 return NULL;
527 rctx->op.ciph.ciphprovctx
528 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
529 if (rctx->op.ciph.ciphprovctx == NULL) {
530 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
531 OPENSSL_free(rctx);
532 return NULL;
533 }
534 return rctx;
535 }
ff64702b
MC
536 }
537
0f113f3e 538 rctx->pmeth = pctx->pmeth;
e683582b 539# ifndef OPENSSL_NO_ENGINE
0f113f3e 540 rctx->engine = pctx->engine;
e683582b 541# endif
8bdcef40 542
0f113f3e 543 if (pctx->peerkey)
03273d61 544 EVP_PKEY_up_ref(pctx->peerkey);
0f113f3e 545 rctx->peerkey = pctx->peerkey;
8bdcef40 546
0f113f3e
MC
547 if (pctx->pmeth->copy(rctx, pctx) > 0)
548 return rctx;
8bdcef40 549
83b4049a 550 rctx->pmeth = NULL;
0f113f3e
MC
551 EVP_PKEY_CTX_free(rctx);
552 return NULL;
8bdcef40 553
0f113f3e 554}
8bdcef40 555
ba30bad5 556int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
557{
558 if (app_pkey_methods == NULL) {
559 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
3484236d
F
560 if (app_pkey_methods == NULL){
561 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 562 return 0;
3484236d 563 }
0f113f3e 564 }
3484236d
F
565 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
566 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 567 return 0;
3484236d 568 }
0f113f3e
MC
569 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
570 return 1;
571}
ba30bad5 572
0822e89a
PY
573void evp_app_cleanup_int(void)
574{
575 if (app_pkey_methods != NULL)
576 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
577}
578
579int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
580{
581 const EVP_PKEY_METHOD *ret;
582
583 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
584
585 return ret == NULL ? 0 : 1;
586}
587
48ed9c23
DSH
588size_t EVP_PKEY_meth_get_count(void)
589{
590 size_t rv = OSSL_NELEM(standard_methods);
591
592 if (app_pkey_methods)
593 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
594 return rv;
595}
596
597const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
598{
599 if (idx < OSSL_NELEM(standard_methods))
19bd1fa1 600 return (standard_methods[idx])();
48ed9c23
DSH
601 if (app_pkey_methods == NULL)
602 return NULL;
603 idx -= OSSL_NELEM(standard_methods);
604 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
605 return NULL;
606 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
607}
e683582b 608#endif
48ed9c23 609
e683582b 610int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
0f113f3e 611{
e683582b
SL
612 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
613 && ctx->op.kex.exchprovctx != NULL
614 && ctx->op.kex.exchange != NULL
615 && ctx->op.kex.exchange->set_ctx_params != NULL)
616 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
617 params);
618 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
619 && ctx->op.sig.sigprovctx != NULL
620 && ctx->op.sig.signature != NULL
621 && ctx->op.sig.signature->set_ctx_params != NULL)
622 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
623 params);
624 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
625 && ctx->op.ciph.ciphprovctx != NULL
626 && ctx->op.ciph.cipher != NULL
627 && ctx->op.ciph.cipher->set_ctx_params != NULL)
628 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
629 params);
62924755
RL
630 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
631 && ctx->op.keymgmt.genctx != NULL
632 && ctx->keymgmt != NULL
633 && ctx->keymgmt->gen_set_params != NULL)
634 return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
635 params);
e683582b 636 return 0;
0f113f3e 637}
5da98aa6 638
f844f9eb 639#ifndef FIPS_MODULE
9c45222d
MC
640int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
641{
4fe54d67
NT
642 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
643 && ctx->op.kex.exchprovctx != NULL
644 && ctx->op.kex.exchange != NULL
645 && ctx->op.kex.exchange->get_ctx_params != NULL)
646 return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
647 params);
864b89ce
MC
648 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
649 && ctx->op.sig.sigprovctx != NULL
650 && ctx->op.sig.signature != NULL
651 && ctx->op.sig.signature->get_ctx_params != NULL)
652 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
653 params);
2c938e2e
MC
654 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
655 && ctx->op.ciph.ciphprovctx != NULL
656 && ctx->op.ciph.cipher != NULL
657 && ctx->op.ciph.cipher->get_ctx_params != NULL)
658 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
659 params);
9c45222d
MC
660 return 0;
661}
662
663const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
664{
4fe54d67
NT
665 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
666 && ctx->op.kex.exchange != NULL
667 && ctx->op.kex.exchange->gettable_ctx_params != NULL)
668 return ctx->op.kex.exchange->gettable_ctx_params();
864b89ce
MC
669 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
670 && ctx->op.sig.signature != NULL
671 && ctx->op.sig.signature->gettable_ctx_params != NULL)
672 return ctx->op.sig.signature->gettable_ctx_params();
2c938e2e
MC
673 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
674 && ctx->op.ciph.cipher != NULL
675 && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
676 return ctx->op.ciph.cipher->gettable_ctx_params();
9c45222d
MC
677 return NULL;
678}
679
9c45222d
MC
680const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
681{
864b89ce
MC
682 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
683 && ctx->op.kex.exchange != NULL
684 && ctx->op.kex.exchange->settable_ctx_params != NULL)
685 return ctx->op.kex.exchange->settable_ctx_params();
686 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
687 && ctx->op.sig.signature != NULL
688 && ctx->op.sig.signature->settable_ctx_params != NULL)
689 return ctx->op.sig.signature->settable_ctx_params();
2c938e2e
MC
690 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
691 && ctx->op.ciph.cipher != NULL
692 && ctx->op.ciph.cipher->settable_ctx_params != NULL)
693 return ctx->op.ciph.cipher->settable_ctx_params();
62924755 694 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
5e77b79a 695 && ctx->keymgmt != NULL)
62924755 696 return evp_keymgmt_gen_settable_params(ctx->keymgmt);
9c45222d
MC
697
698 return NULL;
699}
700
4fe54d67
NT
701/*
702 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
703 *
704 * Return 1 on success, 0 or negative for errors.
705 *
706 * In particular they return -2 if any of the params is not supported.
707 *
f844f9eb 708 * They are not available in FIPS_MODULE as they depend on
4fe54d67
NT
709 * - EVP_PKEY_CTX_{get,set}_params()
710 * - EVP_PKEY_CTX_{gettable,settable}_params()
711 *
712 */
713int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
714{
715 const OSSL_PARAM *p;
716
717 if (ctx == NULL || params == NULL)
718 return 0;
719
720 for (p = params; p->key != NULL; p++) {
721 /* Check the ctx actually understands this parameter */
722 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
723 p->key) == NULL )
724 return -2;
725 }
726
727 return EVP_PKEY_CTX_set_params(ctx, params);
728}
729
730int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
731{
732 const OSSL_PARAM *p;
733
734 if (ctx == NULL || params == NULL)
735 return 0;
736
737 for (p = params; p->key != NULL; p++ ) {
738 /* Check the ctx actually understands this parameter */
739 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
740 p->key) == NULL )
741 return -2;
742 }
743
744 return EVP_PKEY_CTX_get_params(ctx, params);
745}
746
e683582b 747# ifndef OPENSSL_NO_DH
35aca9ec
MC
748int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
749{
750 OSSL_PARAM dh_pad_params[2];
1c3ace68 751 unsigned int upad = pad;
35aca9ec 752
864b89ce
MC
753 /* We use EVP_PKEY_CTX_ctrl return values */
754 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
755 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
756 return -2;
757 }
758
35aca9ec 759 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 760 if (ctx->op.kex.exchprovctx == NULL)
35aca9ec
MC
761 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
762 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
763
1c3ace68 764 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
35aca9ec
MC
765 dh_pad_params[1] = OSSL_PARAM_construct_end();
766
767 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
768}
e683582b 769# endif
35aca9ec 770
9c45222d
MC
771int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
772{
773 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
774 /* 80 should be big enough */
775 char name[80] = "";
776 const EVP_MD *tmp;
777
864b89ce 778 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
779 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
780 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
781 return -2;
782 }
783
784 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 785 if (ctx->op.sig.sigprovctx == NULL)
9c45222d
MC
786 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
787 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
788
789 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
790 name,
791 sizeof(name));
792 *p++ = OSSL_PARAM_construct_end();
793
794 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
795 return 0;
796
7606bed9 797 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
9c45222d
MC
798 if (tmp == NULL)
799 return 0;
800
801 *md = tmp;
802
803 return 1;
804}
805
4889dadc
MC
806int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
807{
00bc1ad9 808 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
4889dadc
MC
809 const char *name;
810
864b89ce 811 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
812 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
813 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
814 return -2;
815 }
816
4889dadc 817 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 818 if (ctx->op.sig.sigprovctx == NULL)
4889dadc
MC
819 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
820 EVP_PKEY_CTRL_MD, 0, (void *)(md));
821
9c45222d
MC
822 if (md == NULL) {
823 name = "";
9c45222d 824 } else {
9c45222d
MC
825 name = EVP_MD_name(md);
826 }
4889dadc 827
9c45222d
MC
828 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
829 /*
830 * Cast away the const. This is read
831 * only so should be safe
832 */
8b6ffd40 833 (char *)name, 0);
9c45222d 834 *p++ = OSSL_PARAM_construct_end();
4889dadc 835
9c45222d 836 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
4889dadc
MC
837}
838
35aca9ec
MC
839static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
840 int cmd, int p1, void *p2)
841{
71434aed
DB
842 /*
843 * GOST CMS format is different for different cipher algorithms.
844 * Most of other algorithms don't have such a difference
845 * so this ctrl is just ignored.
846 */
847 if (cmd == EVP_PKEY_CTRL_CIPHER)
848 return -2;
7165593c 849
e683582b 850# ifndef OPENSSL_NO_DH
4fe54d67
NT
851 if (keytype == EVP_PKEY_DH) {
852 switch (cmd) {
853 case EVP_PKEY_CTRL_DH_PAD:
854 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
7165593c
SL
855 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
856 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1);
857 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
858 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1);
859 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
860 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1);
861 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
862 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1);
863 case EVP_PKEY_CTRL_DH_RFC5114:
864 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1);
4fe54d67
NT
865 }
866 }
867# endif
b03ec3b5
SL
868# ifndef OPENSSL_NO_DSA
869 if (keytype == EVP_PKEY_DSA) {
870 switch (cmd) {
871 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
872 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1);
873 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
874 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1);
875 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
876 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2);
877 }
878 }
879# endif
4fe54d67
NT
880# ifndef OPENSSL_NO_EC
881 if (keytype == EVP_PKEY_EC) {
882 switch (cmd) {
10d756a7
RL
883 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
884 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
6f4b7663
RL
885 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
886 if (p1 == -2) {
887 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
888 } else if (p1 < -1 || p1 > 1) {
889 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
890 return -2;
891 } else {
892 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
893 }
894 case EVP_PKEY_CTRL_EC_KDF_TYPE:
895 if (p1 == -2) {
896 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
897 } else {
898 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
899 }
900 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
901 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
902 case EVP_PKEY_CTRL_EC_KDF_MD:
903 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
904 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
905 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
906 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
907 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
908 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
909 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
910 case EVP_PKEY_CTRL_EC_KDF_UKM:
911 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
4fe54d67
NT
912 }
913 }
e683582b 914# endif
2decdad3
RL
915 if (keytype == EVP_PKEY_RSA) {
916 switch (cmd) {
917 case EVP_PKEY_CTRL_RSA_OAEP_MD:
918 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
919 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
920 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
921 case EVP_PKEY_CTRL_RSA_MGF1_MD:
922 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
923 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
924 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
925 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
926 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
927 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
928 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
929 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
930 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
931 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
932 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
933 }
934 }
935 /*
936 * keytype == -1 is used when several key types share the same structure,
937 * or for generic controls that are the same across multiple key types.
938 */
4fe54d67
NT
939 if (keytype == -1) {
940 switch (cmd) {
6f4b7663
RL
941 case EVP_PKEY_CTRL_MD:
942 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
943 case EVP_PKEY_CTRL_GET_MD:
944 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
945 case EVP_PKEY_CTRL_RSA_PADDING:
946 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
947 case EVP_PKEY_CTRL_GET_RSA_PADDING:
948 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
6f4b7663
RL
949 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
950 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
6f4b7663
RL
951 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
952 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
953 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
954 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
955 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
956 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
e683582b 957# ifndef OPENSSL_NO_CMS
6f4b7663
RL
958 case EVP_PKEY_CTRL_CMS_DECRYPT:
959 case EVP_PKEY_CTRL_CMS_ENCRYPT:
e683582b 960# endif
cc572c25
RL
961 /* TODO (3.0) Temporary hack, this should probe */
962 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
6f4b7663
RL
963 return 1;
964 ERR_raise(ERR_LIB_EVP,
965 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
966 return -2;
4fe54d67 967 }
35aca9ec
MC
968 }
969 return 0;
970}
971
0b6f3c66 972int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
0f113f3e
MC
973 int cmd, int p1, void *p2)
974{
975 int ret;
4803717f 976
35aca9ec
MC
977 if (ctx == NULL) {
978 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
979 return -2;
980 }
981
864b89ce 982 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
48bb9792 983 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
2c938e2e
MC
984 && ctx->op.sig.sigprovctx != NULL)
985 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
62924755
RL
986 && ctx->op.ciph.ciphprovctx != NULL)
987 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
988 && ctx->op.keymgmt.genctx != NULL))
35aca9ec
MC
989 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
990
991 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
0f113f3e
MC
992 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
993 return -2;
994 }
995 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
996 return -1;
997
4803717f
PY
998 /* Skip the operation checks since this is called in a very early stage */
999 if (ctx->pmeth->digest_custom != NULL)
1000 goto doit;
1001
0f113f3e
MC
1002 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1003 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
1004 return -1;
1005 }
1006
1007 if ((optype != -1) && !(ctx->operation & optype)) {
1008 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
1009 return -1;
1010 }
1011
4803717f 1012 doit:
0f113f3e
MC
1013 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1014
1015 if (ret == -2)
1016 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
1017
1018 return ret;
0f113f3e 1019}
0b6f3c66 1020
cefa762e 1021int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 1022 int cmd, uint64_t value)
cefa762e
JB
1023{
1024 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1025}
1026
35aca9ec
MC
1027static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
1028 const char *value)
1029{
10d756a7
RL
1030
1031 /* Special cases that we intercept */
1032# ifndef OPENSSL_NO_EC
1033 /*
1034 * We don't support encoding settings for providers, i.e. the only
1035 * possible encoding is "named_curve", so we simply fail when something
1036 * else is given, and otherwise just pretend all is fine.
1037 */
1038 if (strcmp(name, "ec_param_enc") == 0) {
1039 if (strcmp(value, "named_curve") == 0) {
1040 return 1;
1041 } else {
1042 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1043 return -2;
1044 }
1045 }
1046# endif
1047
972fa318
RL
1048 if (strcmp(name, "rsa_padding_mode") == 0)
1049 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1050 else if (strcmp(name, "rsa_mgf1_md") == 0)
1051 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1052 else if (strcmp(name, "rsa_oaep_md") == 0)
1053 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1054 else if (strcmp(name, "rsa_oaep_label") == 0)
1055 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
6f4b7663
RL
1056 else if (strcmp(name, "rsa_pss_saltlen") == 0)
1057 name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
2972af10
RL
1058 else if (strcmp(name, "rsa_keygen_bits") == 0)
1059 name = OSSL_PKEY_PARAM_RSA_BITS;
1060 else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1061 name = OSSL_PKEY_PARAM_RSA_E;
1062 else if (strcmp(name, "rsa_keygen_primes") == 0)
1063 name = OSSL_PKEY_PARAM_RSA_PRIMES;
e25761b1
RL
1064 else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1065 name = OSSL_PKEY_PARAM_RSA_DIGEST;
1066 else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1067 name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1068 else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1069 name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
b03ec3b5
SL
1070# ifndef OPENSSL_NO_DSA
1071 else if (strcmp(name, "dsa_paramgen_bits") == 0)
1072 name = OSSL_PKEY_PARAM_FFC_PBITS;
1073 else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1074 name = OSSL_PKEY_PARAM_FFC_QBITS;
1075 else if (strcmp(name, "dsa_paramgen_md") == 0)
1076 name = OSSL_PKEY_PARAM_FFC_DIGEST;
1077# endif
e683582b 1078# ifndef OPENSSL_NO_DH
7165593c 1079 else if (strcmp(name, "dh_paramgen_generator") == 0)
b8086652 1080 name = OSSL_PKEY_PARAM_DH_GENERATOR;
7165593c
SL
1081 else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1082 name = OSSL_PKEY_PARAM_FFC_PBITS;
1083 else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1084 name = OSSL_PKEY_PARAM_FFC_QBITS;
1085 else if (strcmp(name, "dh_paramgen_type") == 0) {
1086 name = OSSL_PKEY_PARAM_FFC_TYPE;
1087 value = dh_gen_type_id2name(atoi(value));
1088 } else if (strcmp(name, "dh_param") == 0)
b8086652 1089 name = OSSL_PKEY_PARAM_DH_GROUP;
7165593c 1090 else if (strcmp(name, "dh_rfc5114") == 0) {
b8086652 1091 name = OSSL_PKEY_PARAM_DH_GROUP;
7165593c
SL
1092 value = ffc_named_group_from_uid(atoi(value));
1093 } else if (strcmp(name, "dh_pad") == 0)
972fa318 1094 name = OSSL_EXCHANGE_PARAM_PAD;
e683582b 1095# endif
4fe54d67 1096# ifndef OPENSSL_NO_EC
10d756a7
RL
1097 else if (strcmp(name, "ec_paramgen_curve") == 0)
1098 name = OSSL_PKEY_PARAM_EC_NAME;
4fe54d67
NT
1099 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1100 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1101 else if (strcmp(name, "ecdh_kdf_md") == 0)
f552d900 1102 name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
4fe54d67 1103# endif
89abd1b6 1104
972fa318
RL
1105 {
1106 /*
1107 * TODO(3.0) reduce the code above to only translate known legacy
1108 * string to the corresponding core name (see core_names.h), but
1109 * otherwise leave it to this code block to do the actual work.
1110 */
1111 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1112 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1113 int rv = 0;
292c8bdc 1114 int exists = 0;
89abd1b6 1115
972fa318 1116 if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
292c8bdc
P
1117 strlen(value), &exists)) {
1118 if (!exists) {
e25761b1
RL
1119 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1120 "name=%s,value=%s", name, value);
292c8bdc
P
1121 return -2;
1122 }
89abd1b6 1123 return 0;
292c8bdc 1124 }
972fa318
RL
1125 if (EVP_PKEY_CTX_set_params(ctx, params))
1126 rv = 1;
1127 OPENSSL_free(params[0].data);
1128 return rv;
89abd1b6 1129 }
35aca9ec
MC
1130}
1131
4a3dc3c0 1132int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
0f113f3e
MC
1133 const char *name, const char *value)
1134{
35aca9ec
MC
1135 if (ctx == NULL) {
1136 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1137 return -2;
1138 }
1139
864b89ce
MC
1140 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
1141 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
2c938e2e
MC
1142 && ctx->op.sig.sigprovctx != NULL)
1143 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
62924755
RL
1144 && ctx->op.ciph.ciphprovctx != NULL)
1145 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1146 && ctx->op.keymgmt.genctx != NULL))
35aca9ec
MC
1147 return legacy_ctrl_str_to_param(ctx, name, value);
1148
0f113f3e
MC
1149 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
1150 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1151 return -2;
1152 }
410877ba
DSH
1153 if (strcmp(name, "digest") == 0)
1154 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
1155 value);
0f113f3e
MC
1156 return ctx->pmeth->ctrl_str(ctx, name, value);
1157}
f5cda4cb 1158
99119000
DSH
1159/* Utility functions to send a string of hex string to a ctrl */
1160
1161int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1162{
1163 size_t len;
1164
1165 len = strlen(str);
1166 if (len > INT_MAX)
1167 return -1;
1168 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1169}
1170
1171int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1172{
1173 unsigned char *bin;
1174 long binlen;
1175 int rv = -1;
1176
14f051a0 1177 bin = OPENSSL_hexstr2buf(hex, &binlen);
99119000
DSH
1178 if (bin == NULL)
1179 return 0;
1180 if (binlen <= INT_MAX)
1181 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1182 OPENSSL_free(bin);
1183 return rv;
1184}
52ad523c 1185
410877ba
DSH
1186/* Pass a message digest to a ctrl */
1187int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1188{
1189 const EVP_MD *m;
c82bafc5 1190
410877ba
DSH
1191 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1192 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
1193 return 0;
1194 }
1195 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1196}
99119000 1197
b28dea4e 1198int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1199{
1200 return ctx->operation;
1201}
b28dea4e
DSH
1202
1203void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
0f113f3e
MC
1204{
1205 ctx->keygen_info = dat;
1206 ctx->keygen_info_count = datlen;
1207}
b28dea4e 1208
f5cda4cb 1209void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1210{
1211 ctx->data = data;
1212}
f5cda4cb 1213
9fdcc21f 1214void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
0f113f3e
MC
1215{
1216 return ctx->data;
1217}
f5cda4cb 1218
81cebb8b 1219EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1220{
1221 return ctx->pkey;
1222}
81cebb8b 1223
0e1dba93 1224EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1225{
1226 return ctx->peerkey;
1227}
1228
f5cda4cb 1229void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1230{
1231 ctx->app_data = data;
1232}
f5cda4cb
DSH
1233
1234void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1235{
1236 return ctx->app_data;
1237}
ba30bad5
DSH
1238
1239void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1240 int (*init) (EVP_PKEY_CTX *ctx))
1241{
1242 pmeth->init = init;
1243}
8bdcef40
DSH
1244
1245void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
0f113f3e 1246 int (*copy) (EVP_PKEY_CTX *dst,
9fdcc21f 1247 const EVP_PKEY_CTX *src))
0f113f3e
MC
1248{
1249 pmeth->copy = copy;
1250}
ba30bad5
DSH
1251
1252void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1253 void (*cleanup) (EVP_PKEY_CTX *ctx))
1254{
1255 pmeth->cleanup = cleanup;
1256}
ba30bad5
DSH
1257
1258void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1259 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1260 int (*paramgen) (EVP_PKEY_CTX *ctx,
1261 EVP_PKEY *pkey))
1262{
1263 pmeth->paramgen_init = paramgen_init;
1264 pmeth->paramgen = paramgen;
1265}
ba30bad5
DSH
1266
1267void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1268 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1269 int (*keygen) (EVP_PKEY_CTX *ctx,
1270 EVP_PKEY *pkey))
1271{
1272 pmeth->keygen_init = keygen_init;
1273 pmeth->keygen = keygen;
1274}
ba30bad5
DSH
1275
1276void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1277 int (*sign_init) (EVP_PKEY_CTX *ctx),
1278 int (*sign) (EVP_PKEY_CTX *ctx,
1279 unsigned char *sig, size_t *siglen,
1280 const unsigned char *tbs,
1281 size_t tbslen))
1282{
1283 pmeth->sign_init = sign_init;
1284 pmeth->sign = sign;
1285}
ba30bad5
DSH
1286
1287void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1288 int (*verify_init) (EVP_PKEY_CTX *ctx),
1289 int (*verify) (EVP_PKEY_CTX *ctx,
1290 const unsigned char *sig,
1291 size_t siglen,
1292 const unsigned char *tbs,
1293 size_t tbslen))
1294{
1295 pmeth->verify_init = verify_init;
1296 pmeth->verify = verify;
1297}
ba30bad5
DSH
1298
1299void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1300 int (*verify_recover_init) (EVP_PKEY_CTX
1301 *ctx),
1302 int (*verify_recover) (EVP_PKEY_CTX
1303 *ctx,
1304 unsigned char
1305 *sig,
1306 size_t *siglen,
1307 const unsigned
1308 char *tbs,
1309 size_t tbslen))
1310{
1311 pmeth->verify_recover_init = verify_recover_init;
1312 pmeth->verify_recover = verify_recover;
1313}
ba30bad5
DSH
1314
1315void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1316 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1317 EVP_MD_CTX *mctx),
1318 int (*signctx) (EVP_PKEY_CTX *ctx,
1319 unsigned char *sig,
1320 size_t *siglen,
1321 EVP_MD_CTX *mctx))
1322{
1323 pmeth->signctx_init = signctx_init;
1324 pmeth->signctx = signctx;
1325}
ba30bad5
DSH
1326
1327void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1328 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1329 EVP_MD_CTX *mctx),
1330 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1331 const unsigned char *sig,
1332 int siglen,
1333 EVP_MD_CTX *mctx))
1334{
1335 pmeth->verifyctx_init = verifyctx_init;
1336 pmeth->verifyctx = verifyctx;
1337}
ba30bad5
DSH
1338
1339void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1340 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1341 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1342 unsigned char *out,
1343 size_t *outlen,
1344 const unsigned char *in,
1345 size_t inlen))
1346{
1347 pmeth->encrypt_init = encrypt_init;
1348 pmeth->encrypt = encryptfn;
1349}
ba30bad5
DSH
1350
1351void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1352 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1353 int (*decrypt) (EVP_PKEY_CTX *ctx,
1354 unsigned char *out,
1355 size_t *outlen,
1356 const unsigned char *in,
1357 size_t inlen))
1358{
1359 pmeth->decrypt_init = decrypt_init;
1360 pmeth->decrypt = decrypt;
1361}
ba30bad5
DSH
1362
1363void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1364 int (*derive_init) (EVP_PKEY_CTX *ctx),
1365 int (*derive) (EVP_PKEY_CTX *ctx,
1366 unsigned char *key,
1367 size_t *keylen))
1368{
1369 pmeth->derive_init = derive_init;
1370 pmeth->derive = derive;
1371}
ba30bad5
DSH
1372
1373void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1374 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1375 void *p2),
1376 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1377 const char *type,
1378 const char *value))
1379{
1380 pmeth->ctrl = ctrl;
1381 pmeth->ctrl_str = ctrl_str;
1382}
e7451ed1 1383
2555285f
AH
1384void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1385 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1386 const unsigned char *tbs, size_t tbslen))
1387{
1388 pmeth->digestsign = digestsign;
1389}
1390
1391void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1392 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1393 size_t siglen, const unsigned char *tbs,
1394 size_t tbslen))
1395{
1396 pmeth->digestverify = digestverify;
1397}
1398
2aee35d3
PY
1399void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1400 int (*check) (EVP_PKEY *pkey))
1401{
1402 pmeth->check = check;
1403}
1404
b0004708
PY
1405void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1406 int (*check) (EVP_PKEY *pkey))
1407{
1408 pmeth->public_check = check;
1409}
1410
1411void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1412 int (*check) (EVP_PKEY *pkey))
1413{
1414 pmeth->param_check = check;
1415}
1416
0a8fdef7
PY
1417void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1418 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1419 EVP_MD_CTX *mctx))
1420{
1421 pmeth->digest_custom = digest_custom;
1422}
1423
693be9a2 1424void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1425 int (**pinit) (EVP_PKEY_CTX *ctx))
1426{
1427 *pinit = pmeth->init;
1428}
1429
693be9a2 1430void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
e7451ed1 1431 int (**pcopy) (EVP_PKEY_CTX *dst,
9fdcc21f 1432 const EVP_PKEY_CTX *src))
e7451ed1
DSH
1433{
1434 *pcopy = pmeth->copy;
1435}
1436
693be9a2 1437void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1438 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1439{
1440 *pcleanup = pmeth->cleanup;
1441}
1442
693be9a2 1443void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1444 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1445 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1446 EVP_PKEY *pkey))
1447{
1448 if (pparamgen_init)
1449 *pparamgen_init = pmeth->paramgen_init;
1450 if (pparamgen)
1451 *pparamgen = pmeth->paramgen;
1452}
1453
693be9a2 1454void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1455 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1456 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1457 EVP_PKEY *pkey))
1458{
1459 if (pkeygen_init)
1460 *pkeygen_init = pmeth->keygen_init;
1461 if (pkeygen)
1462 *pkeygen = pmeth->keygen;
1463}
1464
693be9a2 1465void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1466 int (**psign_init) (EVP_PKEY_CTX *ctx),
1467 int (**psign) (EVP_PKEY_CTX *ctx,
1468 unsigned char *sig, size_t *siglen,
1469 const unsigned char *tbs,
1470 size_t tbslen))
1471{
1472 if (psign_init)
1473 *psign_init = pmeth->sign_init;
1474 if (psign)
1475 *psign = pmeth->sign;
1476}
1477
693be9a2 1478void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1479 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1480 int (**pverify) (EVP_PKEY_CTX *ctx,
1481 const unsigned char *sig,
1482 size_t siglen,
1483 const unsigned char *tbs,
1484 size_t tbslen))
1485{
1486 if (pverify_init)
1487 *pverify_init = pmeth->verify_init;
1488 if (pverify)
1489 *pverify = pmeth->verify;
1490}
1491
693be9a2 1492void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1493 int (**pverify_recover_init) (EVP_PKEY_CTX
1494 *ctx),
1495 int (**pverify_recover) (EVP_PKEY_CTX
1496 *ctx,
1497 unsigned char
1498 *sig,
1499 size_t *siglen,
1500 const unsigned
1501 char *tbs,
1502 size_t tbslen))
1503{
1504 if (pverify_recover_init)
1505 *pverify_recover_init = pmeth->verify_recover_init;
1506 if (pverify_recover)
1507 *pverify_recover = pmeth->verify_recover;
1508}
1509
693be9a2 1510void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1511 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1512 EVP_MD_CTX *mctx),
1513 int (**psignctx) (EVP_PKEY_CTX *ctx,
1514 unsigned char *sig,
1515 size_t *siglen,
1516 EVP_MD_CTX *mctx))
1517{
1518 if (psignctx_init)
1519 *psignctx_init = pmeth->signctx_init;
1520 if (psignctx)
1521 *psignctx = pmeth->signctx;
1522}
1523
693be9a2 1524void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1525 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1526 EVP_MD_CTX *mctx),
1527 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1528 const unsigned char *sig,
1529 int siglen,
1530 EVP_MD_CTX *mctx))
1531{
1532 if (pverifyctx_init)
1533 *pverifyctx_init = pmeth->verifyctx_init;
1534 if (pverifyctx)
1535 *pverifyctx = pmeth->verifyctx;
1536}
1537
693be9a2 1538void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1539 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1540 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1541 unsigned char *out,
1542 size_t *outlen,
1543 const unsigned char *in,
1544 size_t inlen))
1545{
1546 if (pencrypt_init)
1547 *pencrypt_init = pmeth->encrypt_init;
1548 if (pencryptfn)
1549 *pencryptfn = pmeth->encrypt;
1550}
1551
693be9a2 1552void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1553 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1554 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1555 unsigned char *out,
1556 size_t *outlen,
1557 const unsigned char *in,
1558 size_t inlen))
1559{
1560 if (pdecrypt_init)
1561 *pdecrypt_init = pmeth->decrypt_init;
1562 if (pdecrypt)
1563 *pdecrypt = pmeth->decrypt;
1564}
1565
693be9a2 1566void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1567 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1568 int (**pderive) (EVP_PKEY_CTX *ctx,
1569 unsigned char *key,
1570 size_t *keylen))
1571{
1572 if (pderive_init)
1573 *pderive_init = pmeth->derive_init;
1574 if (pderive)
1575 *pderive = pmeth->derive;
1576}
1577
693be9a2 1578void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1579 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1580 void *p2),
1581 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1582 const char *type,
1583 const char *value))
1584{
1585 if (pctrl)
1586 *pctrl = pmeth->ctrl;
1587 if (pctrl_str)
1588 *pctrl_str = pmeth->ctrl_str;
1589}
2aee35d3 1590
2555285f
AH
1591void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1592 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1593 const unsigned char *tbs, size_t tbslen))
1594{
1595 if (digestsign)
1596 *digestsign = pmeth->digestsign;
1597}
1598
1599void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1600 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1601 size_t siglen, const unsigned char *tbs,
1602 size_t tbslen))
1603{
1604 if (digestverify)
1605 *digestverify = pmeth->digestverify;
1606}
1607
693be9a2 1608void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2aee35d3
PY
1609 int (**pcheck) (EVP_PKEY *pkey))
1610{
34f5c8b1 1611 if (pcheck != NULL)
2aee35d3
PY
1612 *pcheck = pmeth->check;
1613}
b0004708 1614
693be9a2 1615void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1616 int (**pcheck) (EVP_PKEY *pkey))
1617{
34f5c8b1 1618 if (pcheck != NULL)
b0004708
PY
1619 *pcheck = pmeth->public_check;
1620}
1621
693be9a2 1622void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1623 int (**pcheck) (EVP_PKEY *pkey))
1624{
34f5c8b1 1625 if (pcheck != NULL)
b0004708
PY
1626 *pcheck = pmeth->param_check;
1627}
0a8fdef7
PY
1628
1629void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1630 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1631 EVP_MD_CTX *mctx))
1632{
675f4cee 1633 if (pdigest_custom != NULL)
0a8fdef7
PY
1634 *pdigest_custom = pmeth->digest_custom;
1635}
e683582b 1636
f844f9eb 1637#endif /* FIPS_MODULE */