]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
Modify libssl to discover supported groups based on available providers
[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{
4cbb196b
AT
388 int pkey_id = dst->pkey_id;
389 int flags = dst->flags;
f830c68f 390
4cbb196b 391 *dst = *src;
f830c68f 392
4cbb196b
AT
393 /* We only copy the function pointers so restore the other values */
394 dst->pkey_id = pkey_id;
395 dst->flags = flags;
0f113f3e 396}
f830c68f 397
ba30bad5 398void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
399{
400 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
401 OPENSSL_free(pmeth);
402}
ba30bad5 403
f5cda4cb 404EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
0f113f3e 405{
3ee348b0 406 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
0f113f3e 407}
f5cda4cb
DSH
408
409EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
0f113f3e 410{
3ee348b0 411 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
a07c17ef
RL
412}
413
9fdcc21f 414EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
0f113f3e
MC
415{
416 EVP_PKEY_CTX *rctx;
ff64702b
MC
417
418 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
864b89ce
MC
419 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
420 && pctx->op.kex.exchprovctx == NULL)
421 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
422 && pctx->op.sig.sigprovctx == NULL)))
0f113f3e 423 return NULL;
e683582b 424# ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
425 /* Make sure it's safe to copy a pkey context using an ENGINE */
426 if (pctx->engine && !ENGINE_init(pctx->engine)) {
427 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
428 return 0;
429 }
e683582b 430# endif
ff64702b 431 rctx = OPENSSL_zalloc(sizeof(*rctx));
3484236d
F
432 if (rctx == NULL) {
433 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
0f113f3e 434 return NULL;
3484236d 435 }
8bdcef40 436
ff64702b
MC
437 if (pctx->pkey != NULL)
438 EVP_PKEY_up_ref(pctx->pkey);
439 rctx->pkey = pctx->pkey;
440 rctx->operation = pctx->operation;
3ee348b0 441 rctx->libctx = pctx->libctx;
f23bc0b7 442 rctx->keytype = pctx->keytype;
a07c17ef 443 rctx->propquery = pctx->propquery;
ff64702b 444
864b89ce
MC
445 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
446 if (pctx->op.kex.exchange != NULL) {
447 rctx->op.kex.exchange = pctx->op.kex.exchange;
448 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
449 OPENSSL_free(rctx);
450 return NULL;
451 }
ff64702b 452 }
864b89ce
MC
453 if (pctx->op.kex.exchprovctx != NULL) {
454 if (!ossl_assert(pctx->op.kex.exchange != NULL))
455 return NULL;
456 rctx->op.kex.exchprovctx
457 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
458 if (rctx->op.kex.exchprovctx == NULL) {
459 EVP_KEYEXCH_free(rctx->op.kex.exchange);
460 OPENSSL_free(rctx);
461 return NULL;
462 }
463 return rctx;
464 }
465 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
466 if (pctx->op.sig.signature != NULL) {
467 rctx->op.sig.signature = pctx->op.sig.signature;
468 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
469 OPENSSL_free(rctx);
470 return NULL;
471 }
472 }
473 if (pctx->op.sig.sigprovctx != NULL) {
474 if (!ossl_assert(pctx->op.sig.signature != NULL))
475 return NULL;
476 rctx->op.sig.sigprovctx
477 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
478 if (rctx->op.sig.sigprovctx == NULL) {
479 EVP_SIGNATURE_free(rctx->op.sig.signature);
480 OPENSSL_free(rctx);
481 return NULL;
482 }
483 return rctx;
ff64702b 484 }
2c938e2e
MC
485 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
486 if (pctx->op.ciph.cipher != NULL) {
487 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
488 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
489 OPENSSL_free(rctx);
490 return NULL;
491 }
492 }
493 if (pctx->op.ciph.ciphprovctx != NULL) {
494 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
495 return NULL;
496 rctx->op.ciph.ciphprovctx
497 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
498 if (rctx->op.ciph.ciphprovctx == NULL) {
499 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
500 OPENSSL_free(rctx);
501 return NULL;
502 }
503 return rctx;
504 }
ff64702b
MC
505 }
506
0f113f3e 507 rctx->pmeth = pctx->pmeth;
e683582b 508# ifndef OPENSSL_NO_ENGINE
0f113f3e 509 rctx->engine = pctx->engine;
e683582b 510# endif
8bdcef40 511
0f113f3e 512 if (pctx->peerkey)
03273d61 513 EVP_PKEY_up_ref(pctx->peerkey);
0f113f3e 514 rctx->peerkey = pctx->peerkey;
8bdcef40 515
0f113f3e
MC
516 if (pctx->pmeth->copy(rctx, pctx) > 0)
517 return rctx;
8bdcef40 518
83b4049a 519 rctx->pmeth = NULL;
0f113f3e
MC
520 EVP_PKEY_CTX_free(rctx);
521 return NULL;
8bdcef40 522
0f113f3e 523}
8bdcef40 524
ba30bad5 525int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
526{
527 if (app_pkey_methods == NULL) {
528 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
3484236d
F
529 if (app_pkey_methods == NULL){
530 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 531 return 0;
3484236d 532 }
0f113f3e 533 }
3484236d
F
534 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
535 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 536 return 0;
3484236d 537 }
0f113f3e
MC
538 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
539 return 1;
540}
ba30bad5 541
0822e89a
PY
542void evp_app_cleanup_int(void)
543{
544 if (app_pkey_methods != NULL)
545 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
546}
547
548int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
549{
550 const EVP_PKEY_METHOD *ret;
551
552 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
553
554 return ret == NULL ? 0 : 1;
555}
556
48ed9c23
DSH
557size_t EVP_PKEY_meth_get_count(void)
558{
559 size_t rv = OSSL_NELEM(standard_methods);
560
561 if (app_pkey_methods)
562 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
563 return rv;
564}
565
566const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
567{
568 if (idx < OSSL_NELEM(standard_methods))
19bd1fa1 569 return (standard_methods[idx])();
48ed9c23
DSH
570 if (app_pkey_methods == NULL)
571 return NULL;
572 idx -= OSSL_NELEM(standard_methods);
573 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
574 return NULL;
575 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
576}
e683582b 577#endif
48ed9c23 578
e683582b 579int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
0f113f3e 580{
e683582b
SL
581 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
582 && ctx->op.kex.exchprovctx != NULL
583 && ctx->op.kex.exchange != NULL
584 && ctx->op.kex.exchange->set_ctx_params != NULL)
585 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
586 params);
587 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
588 && ctx->op.sig.sigprovctx != NULL
589 && ctx->op.sig.signature != NULL
590 && ctx->op.sig.signature->set_ctx_params != NULL)
591 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
592 params);
593 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
594 && ctx->op.ciph.ciphprovctx != NULL
595 && ctx->op.ciph.cipher != NULL
596 && ctx->op.ciph.cipher->set_ctx_params != NULL)
597 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
598 params);
62924755
RL
599 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
600 && ctx->op.keymgmt.genctx != NULL
601 && ctx->keymgmt != NULL
602 && ctx->keymgmt->gen_set_params != NULL)
603 return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
604 params);
e683582b 605 return 0;
0f113f3e 606}
5da98aa6 607
f844f9eb 608#ifndef FIPS_MODULE
9c45222d
MC
609int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
610{
4fe54d67
NT
611 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
612 && ctx->op.kex.exchprovctx != NULL
613 && ctx->op.kex.exchange != NULL
614 && ctx->op.kex.exchange->get_ctx_params != NULL)
615 return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
616 params);
864b89ce
MC
617 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
618 && ctx->op.sig.sigprovctx != NULL
619 && ctx->op.sig.signature != NULL
620 && ctx->op.sig.signature->get_ctx_params != NULL)
621 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
622 params);
2c938e2e
MC
623 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
624 && ctx->op.ciph.ciphprovctx != NULL
625 && ctx->op.ciph.cipher != NULL
626 && ctx->op.ciph.cipher->get_ctx_params != NULL)
627 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
628 params);
9c45222d
MC
629 return 0;
630}
631
632const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
633{
4fe54d67
NT
634 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
635 && ctx->op.kex.exchange != NULL
636 && ctx->op.kex.exchange->gettable_ctx_params != NULL)
637 return ctx->op.kex.exchange->gettable_ctx_params();
864b89ce
MC
638 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
639 && ctx->op.sig.signature != NULL
640 && ctx->op.sig.signature->gettable_ctx_params != NULL)
641 return ctx->op.sig.signature->gettable_ctx_params();
2c938e2e
MC
642 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
643 && ctx->op.ciph.cipher != NULL
644 && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
645 return ctx->op.ciph.cipher->gettable_ctx_params();
9c45222d
MC
646 return NULL;
647}
648
9c45222d
MC
649const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
650{
864b89ce
MC
651 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
652 && ctx->op.kex.exchange != NULL
653 && ctx->op.kex.exchange->settable_ctx_params != NULL)
654 return ctx->op.kex.exchange->settable_ctx_params();
655 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
656 && ctx->op.sig.signature != NULL
657 && ctx->op.sig.signature->settable_ctx_params != NULL)
658 return ctx->op.sig.signature->settable_ctx_params();
2c938e2e
MC
659 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
660 && ctx->op.ciph.cipher != NULL
661 && ctx->op.ciph.cipher->settable_ctx_params != NULL)
662 return ctx->op.ciph.cipher->settable_ctx_params();
62924755 663 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
5e77b79a 664 && ctx->keymgmt != NULL)
62924755 665 return evp_keymgmt_gen_settable_params(ctx->keymgmt);
9c45222d
MC
666
667 return NULL;
668}
669
4fe54d67
NT
670/*
671 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
672 *
673 * Return 1 on success, 0 or negative for errors.
674 *
675 * In particular they return -2 if any of the params is not supported.
676 *
f844f9eb 677 * They are not available in FIPS_MODULE as they depend on
4fe54d67
NT
678 * - EVP_PKEY_CTX_{get,set}_params()
679 * - EVP_PKEY_CTX_{gettable,settable}_params()
680 *
681 */
682int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
683{
684 const OSSL_PARAM *p;
685
686 if (ctx == NULL || params == NULL)
687 return 0;
688
689 for (p = params; p->key != NULL; p++) {
690 /* Check the ctx actually understands this parameter */
691 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
692 p->key) == NULL )
693 return -2;
694 }
695
696 return EVP_PKEY_CTX_set_params(ctx, params);
697}
698
699int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
700{
701 const OSSL_PARAM *p;
702
703 if (ctx == NULL || params == NULL)
704 return 0;
705
706 for (p = params; p->key != NULL; p++ ) {
707 /* Check the ctx actually understands this parameter */
708 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
709 p->key) == NULL )
710 return -2;
711 }
712
713 return EVP_PKEY_CTX_get_params(ctx, params);
714}
715
e683582b 716# ifndef OPENSSL_NO_DH
35aca9ec
MC
717int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
718{
719 OSSL_PARAM dh_pad_params[2];
1c3ace68 720 unsigned int upad = pad;
35aca9ec 721
864b89ce
MC
722 /* We use EVP_PKEY_CTX_ctrl return values */
723 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
724 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
725 return -2;
726 }
727
35aca9ec 728 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 729 if (ctx->op.kex.exchprovctx == NULL)
35aca9ec
MC
730 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
731 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
732
1c3ace68 733 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
35aca9ec
MC
734 dh_pad_params[1] = OSSL_PARAM_construct_end();
735
736 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
737}
e683582b 738# endif
35aca9ec 739
9c45222d
MC
740int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
741{
742 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
743 /* 80 should be big enough */
744 char name[80] = "";
745 const EVP_MD *tmp;
746
864b89ce 747 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
748 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
749 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
750 return -2;
751 }
752
753 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 754 if (ctx->op.sig.sigprovctx == NULL)
9c45222d
MC
755 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
756 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
757
758 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
759 name,
760 sizeof(name));
761 *p++ = OSSL_PARAM_construct_end();
762
763 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
764 return 0;
765
7606bed9 766 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
9c45222d
MC
767 if (tmp == NULL)
768 return 0;
769
770 *md = tmp;
771
772 return 1;
773}
774
4889dadc
MC
775int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
776{
00bc1ad9 777 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
4889dadc
MC
778 const char *name;
779
864b89ce 780 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
781 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
782 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
783 return -2;
784 }
785
4889dadc 786 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 787 if (ctx->op.sig.sigprovctx == NULL)
4889dadc
MC
788 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
789 EVP_PKEY_CTRL_MD, 0, (void *)(md));
790
9c45222d
MC
791 if (md == NULL) {
792 name = "";
9c45222d 793 } else {
9c45222d
MC
794 name = EVP_MD_name(md);
795 }
4889dadc 796
9c45222d
MC
797 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
798 /*
799 * Cast away the const. This is read
800 * only so should be safe
801 */
8b6ffd40 802 (char *)name, 0);
9c45222d 803 *p++ = OSSL_PARAM_construct_end();
4889dadc 804
9c45222d 805 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
4889dadc
MC
806}
807
35aca9ec
MC
808static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
809 int cmd, int p1, void *p2)
810{
71434aed
DB
811 /*
812 * GOST CMS format is different for different cipher algorithms.
813 * Most of other algorithms don't have such a difference
814 * so this ctrl is just ignored.
815 */
816 if (cmd == EVP_PKEY_CTRL_CIPHER)
817 return -2;
7165593c 818
e683582b 819# ifndef OPENSSL_NO_DH
4fe54d67
NT
820 if (keytype == EVP_PKEY_DH) {
821 switch (cmd) {
822 case EVP_PKEY_CTRL_DH_PAD:
823 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
7165593c
SL
824 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
825 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1);
826 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
827 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1);
828 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
829 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1);
830 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
831 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1);
832 case EVP_PKEY_CTRL_DH_RFC5114:
833 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1);
4fe54d67
NT
834 }
835 }
836# endif
b03ec3b5
SL
837# ifndef OPENSSL_NO_DSA
838 if (keytype == EVP_PKEY_DSA) {
839 switch (cmd) {
840 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
841 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1);
842 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
843 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1);
844 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
845 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2);
846 }
847 }
848# endif
4fe54d67
NT
849# ifndef OPENSSL_NO_EC
850 if (keytype == EVP_PKEY_EC) {
851 switch (cmd) {
10d756a7
RL
852 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
853 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
6f4b7663
RL
854 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
855 if (p1 == -2) {
856 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
857 } else if (p1 < -1 || p1 > 1) {
858 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
859 return -2;
860 } else {
861 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
862 }
863 case EVP_PKEY_CTRL_EC_KDF_TYPE:
864 if (p1 == -2) {
865 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
866 } else {
867 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
868 }
869 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
870 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
871 case EVP_PKEY_CTRL_EC_KDF_MD:
872 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
873 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
874 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
875 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
876 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
877 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
878 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
879 case EVP_PKEY_CTRL_EC_KDF_UKM:
880 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
4fe54d67
NT
881 }
882 }
e683582b 883# endif
2decdad3
RL
884 if (keytype == EVP_PKEY_RSA) {
885 switch (cmd) {
886 case EVP_PKEY_CTRL_RSA_OAEP_MD:
887 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
888 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
889 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
890 case EVP_PKEY_CTRL_RSA_MGF1_MD:
891 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
892 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
893 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
894 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
895 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
896 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
897 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
898 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
899 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
900 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
901 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
902 }
903 }
904 /*
905 * keytype == -1 is used when several key types share the same structure,
906 * or for generic controls that are the same across multiple key types.
907 */
4fe54d67
NT
908 if (keytype == -1) {
909 switch (cmd) {
6f4b7663
RL
910 case EVP_PKEY_CTRL_MD:
911 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
912 case EVP_PKEY_CTRL_GET_MD:
913 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
914 case EVP_PKEY_CTRL_RSA_PADDING:
915 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
916 case EVP_PKEY_CTRL_GET_RSA_PADDING:
917 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
6f4b7663
RL
918 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
919 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
6f4b7663
RL
920 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
921 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
922 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
923 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
924 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
925 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
e683582b 926# ifndef OPENSSL_NO_CMS
6f4b7663
RL
927 case EVP_PKEY_CTRL_CMS_DECRYPT:
928 case EVP_PKEY_CTRL_CMS_ENCRYPT:
e683582b 929# endif
cc572c25
RL
930 /* TODO (3.0) Temporary hack, this should probe */
931 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
6f4b7663
RL
932 return 1;
933 ERR_raise(ERR_LIB_EVP,
934 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
935 return -2;
4fe54d67 936 }
35aca9ec
MC
937 }
938 return 0;
939}
940
0b6f3c66 941int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
0f113f3e
MC
942 int cmd, int p1, void *p2)
943{
944 int ret;
4803717f 945
35aca9ec
MC
946 if (ctx == NULL) {
947 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
948 return -2;
949 }
950
864b89ce 951 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
48bb9792 952 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
2c938e2e
MC
953 && ctx->op.sig.sigprovctx != NULL)
954 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
62924755
RL
955 && ctx->op.ciph.ciphprovctx != NULL)
956 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
957 && ctx->op.keymgmt.genctx != NULL))
35aca9ec
MC
958 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
959
960 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
0f113f3e
MC
961 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
962 return -2;
963 }
964 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
965 return -1;
966
4803717f
PY
967 /* Skip the operation checks since this is called in a very early stage */
968 if (ctx->pmeth->digest_custom != NULL)
969 goto doit;
970
0f113f3e
MC
971 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
972 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
973 return -1;
974 }
975
976 if ((optype != -1) && !(ctx->operation & optype)) {
977 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
978 return -1;
979 }
980
4803717f 981 doit:
0f113f3e
MC
982 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
983
984 if (ret == -2)
985 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
986
987 return ret;
0f113f3e 988}
0b6f3c66 989
cefa762e 990int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 991 int cmd, uint64_t value)
cefa762e
JB
992{
993 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
994}
995
35aca9ec
MC
996static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
997 const char *value)
998{
10d756a7
RL
999
1000 /* Special cases that we intercept */
1001# ifndef OPENSSL_NO_EC
1002 /*
1003 * We don't support encoding settings for providers, i.e. the only
1004 * possible encoding is "named_curve", so we simply fail when something
1005 * else is given, and otherwise just pretend all is fine.
1006 */
1007 if (strcmp(name, "ec_param_enc") == 0) {
1008 if (strcmp(value, "named_curve") == 0) {
1009 return 1;
1010 } else {
1011 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1012 return -2;
1013 }
1014 }
1015# endif
1016
972fa318
RL
1017 if (strcmp(name, "rsa_padding_mode") == 0)
1018 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1019 else if (strcmp(name, "rsa_mgf1_md") == 0)
1020 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1021 else if (strcmp(name, "rsa_oaep_md") == 0)
1022 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1023 else if (strcmp(name, "rsa_oaep_label") == 0)
1024 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
6f4b7663
RL
1025 else if (strcmp(name, "rsa_pss_saltlen") == 0)
1026 name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
2972af10
RL
1027 else if (strcmp(name, "rsa_keygen_bits") == 0)
1028 name = OSSL_PKEY_PARAM_RSA_BITS;
1029 else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1030 name = OSSL_PKEY_PARAM_RSA_E;
1031 else if (strcmp(name, "rsa_keygen_primes") == 0)
1032 name = OSSL_PKEY_PARAM_RSA_PRIMES;
e25761b1
RL
1033 else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1034 name = OSSL_PKEY_PARAM_RSA_DIGEST;
1035 else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1036 name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1037 else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1038 name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
b03ec3b5
SL
1039# ifndef OPENSSL_NO_DSA
1040 else if (strcmp(name, "dsa_paramgen_bits") == 0)
1041 name = OSSL_PKEY_PARAM_FFC_PBITS;
1042 else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1043 name = OSSL_PKEY_PARAM_FFC_QBITS;
1044 else if (strcmp(name, "dsa_paramgen_md") == 0)
1045 name = OSSL_PKEY_PARAM_FFC_DIGEST;
1046# endif
e683582b 1047# ifndef OPENSSL_NO_DH
7165593c 1048 else if (strcmp(name, "dh_paramgen_generator") == 0)
b8086652 1049 name = OSSL_PKEY_PARAM_DH_GENERATOR;
7165593c
SL
1050 else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1051 name = OSSL_PKEY_PARAM_FFC_PBITS;
1052 else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1053 name = OSSL_PKEY_PARAM_FFC_QBITS;
1054 else if (strcmp(name, "dh_paramgen_type") == 0) {
1055 name = OSSL_PKEY_PARAM_FFC_TYPE;
1056 value = dh_gen_type_id2name(atoi(value));
1057 } else if (strcmp(name, "dh_param") == 0)
b8086652 1058 name = OSSL_PKEY_PARAM_DH_GROUP;
7165593c 1059 else if (strcmp(name, "dh_rfc5114") == 0) {
b8086652 1060 name = OSSL_PKEY_PARAM_DH_GROUP;
7165593c
SL
1061 value = ffc_named_group_from_uid(atoi(value));
1062 } else if (strcmp(name, "dh_pad") == 0)
972fa318 1063 name = OSSL_EXCHANGE_PARAM_PAD;
e683582b 1064# endif
4fe54d67 1065# ifndef OPENSSL_NO_EC
10d756a7
RL
1066 else if (strcmp(name, "ec_paramgen_curve") == 0)
1067 name = OSSL_PKEY_PARAM_EC_NAME;
4fe54d67
NT
1068 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1069 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1070 else if (strcmp(name, "ecdh_kdf_md") == 0)
f552d900 1071 name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
4fe54d67 1072# endif
89abd1b6 1073
972fa318
RL
1074 {
1075 /*
1076 * TODO(3.0) reduce the code above to only translate known legacy
1077 * string to the corresponding core name (see core_names.h), but
1078 * otherwise leave it to this code block to do the actual work.
1079 */
1080 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1081 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1082 int rv = 0;
292c8bdc 1083 int exists = 0;
89abd1b6 1084
972fa318 1085 if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
292c8bdc
P
1086 strlen(value), &exists)) {
1087 if (!exists) {
e25761b1
RL
1088 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1089 "name=%s,value=%s", name, value);
292c8bdc
P
1090 return -2;
1091 }
89abd1b6 1092 return 0;
292c8bdc 1093 }
972fa318
RL
1094 if (EVP_PKEY_CTX_set_params(ctx, params))
1095 rv = 1;
1096 OPENSSL_free(params[0].data);
1097 return rv;
89abd1b6 1098 }
35aca9ec
MC
1099}
1100
4a3dc3c0 1101int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
0f113f3e
MC
1102 const char *name, const char *value)
1103{
35aca9ec
MC
1104 if (ctx == NULL) {
1105 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1106 return -2;
1107 }
1108
864b89ce
MC
1109 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
1110 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
2c938e2e
MC
1111 && ctx->op.sig.sigprovctx != NULL)
1112 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
62924755
RL
1113 && ctx->op.ciph.ciphprovctx != NULL)
1114 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1115 && ctx->op.keymgmt.genctx != NULL))
35aca9ec
MC
1116 return legacy_ctrl_str_to_param(ctx, name, value);
1117
0f113f3e
MC
1118 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
1119 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1120 return -2;
1121 }
410877ba
DSH
1122 if (strcmp(name, "digest") == 0)
1123 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
1124 value);
0f113f3e
MC
1125 return ctx->pmeth->ctrl_str(ctx, name, value);
1126}
f5cda4cb 1127
99119000
DSH
1128/* Utility functions to send a string of hex string to a ctrl */
1129
1130int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1131{
1132 size_t len;
1133
1134 len = strlen(str);
1135 if (len > INT_MAX)
1136 return -1;
1137 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1138}
1139
1140int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1141{
1142 unsigned char *bin;
1143 long binlen;
1144 int rv = -1;
1145
14f051a0 1146 bin = OPENSSL_hexstr2buf(hex, &binlen);
99119000
DSH
1147 if (bin == NULL)
1148 return 0;
1149 if (binlen <= INT_MAX)
1150 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1151 OPENSSL_free(bin);
1152 return rv;
1153}
52ad523c 1154
410877ba
DSH
1155/* Pass a message digest to a ctrl */
1156int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1157{
1158 const EVP_MD *m;
c82bafc5 1159
410877ba
DSH
1160 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1161 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
1162 return 0;
1163 }
1164 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1165}
99119000 1166
b28dea4e 1167int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1168{
1169 return ctx->operation;
1170}
b28dea4e
DSH
1171
1172void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
0f113f3e
MC
1173{
1174 ctx->keygen_info = dat;
1175 ctx->keygen_info_count = datlen;
1176}
b28dea4e 1177
f5cda4cb 1178void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1179{
1180 ctx->data = data;
1181}
f5cda4cb 1182
9fdcc21f 1183void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
0f113f3e
MC
1184{
1185 return ctx->data;
1186}
f5cda4cb 1187
81cebb8b 1188EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1189{
1190 return ctx->pkey;
1191}
81cebb8b 1192
0e1dba93 1193EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1194{
1195 return ctx->peerkey;
1196}
1197
f5cda4cb 1198void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
1199{
1200 ctx->app_data = data;
1201}
f5cda4cb
DSH
1202
1203void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1204{
1205 return ctx->app_data;
1206}
ba30bad5
DSH
1207
1208void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1209 int (*init) (EVP_PKEY_CTX *ctx))
1210{
1211 pmeth->init = init;
1212}
8bdcef40
DSH
1213
1214void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
0f113f3e 1215 int (*copy) (EVP_PKEY_CTX *dst,
9fdcc21f 1216 const EVP_PKEY_CTX *src))
0f113f3e
MC
1217{
1218 pmeth->copy = copy;
1219}
ba30bad5
DSH
1220
1221void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1222 void (*cleanup) (EVP_PKEY_CTX *ctx))
1223{
1224 pmeth->cleanup = cleanup;
1225}
ba30bad5
DSH
1226
1227void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1228 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1229 int (*paramgen) (EVP_PKEY_CTX *ctx,
1230 EVP_PKEY *pkey))
1231{
1232 pmeth->paramgen_init = paramgen_init;
1233 pmeth->paramgen = paramgen;
1234}
ba30bad5
DSH
1235
1236void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1237 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1238 int (*keygen) (EVP_PKEY_CTX *ctx,
1239 EVP_PKEY *pkey))
1240{
1241 pmeth->keygen_init = keygen_init;
1242 pmeth->keygen = keygen;
1243}
ba30bad5
DSH
1244
1245void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1246 int (*sign_init) (EVP_PKEY_CTX *ctx),
1247 int (*sign) (EVP_PKEY_CTX *ctx,
1248 unsigned char *sig, size_t *siglen,
1249 const unsigned char *tbs,
1250 size_t tbslen))
1251{
1252 pmeth->sign_init = sign_init;
1253 pmeth->sign = sign;
1254}
ba30bad5
DSH
1255
1256void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1257 int (*verify_init) (EVP_PKEY_CTX *ctx),
1258 int (*verify) (EVP_PKEY_CTX *ctx,
1259 const unsigned char *sig,
1260 size_t siglen,
1261 const unsigned char *tbs,
1262 size_t tbslen))
1263{
1264 pmeth->verify_init = verify_init;
1265 pmeth->verify = verify;
1266}
ba30bad5
DSH
1267
1268void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1269 int (*verify_recover_init) (EVP_PKEY_CTX
1270 *ctx),
1271 int (*verify_recover) (EVP_PKEY_CTX
1272 *ctx,
1273 unsigned char
1274 *sig,
1275 size_t *siglen,
1276 const unsigned
1277 char *tbs,
1278 size_t tbslen))
1279{
1280 pmeth->verify_recover_init = verify_recover_init;
1281 pmeth->verify_recover = verify_recover;
1282}
ba30bad5
DSH
1283
1284void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1285 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1286 EVP_MD_CTX *mctx),
1287 int (*signctx) (EVP_PKEY_CTX *ctx,
1288 unsigned char *sig,
1289 size_t *siglen,
1290 EVP_MD_CTX *mctx))
1291{
1292 pmeth->signctx_init = signctx_init;
1293 pmeth->signctx = signctx;
1294}
ba30bad5
DSH
1295
1296void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1297 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1298 EVP_MD_CTX *mctx),
1299 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1300 const unsigned char *sig,
1301 int siglen,
1302 EVP_MD_CTX *mctx))
1303{
1304 pmeth->verifyctx_init = verifyctx_init;
1305 pmeth->verifyctx = verifyctx;
1306}
ba30bad5
DSH
1307
1308void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1309 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1310 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1311 unsigned char *out,
1312 size_t *outlen,
1313 const unsigned char *in,
1314 size_t inlen))
1315{
1316 pmeth->encrypt_init = encrypt_init;
1317 pmeth->encrypt = encryptfn;
1318}
ba30bad5
DSH
1319
1320void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1321 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1322 int (*decrypt) (EVP_PKEY_CTX *ctx,
1323 unsigned char *out,
1324 size_t *outlen,
1325 const unsigned char *in,
1326 size_t inlen))
1327{
1328 pmeth->decrypt_init = decrypt_init;
1329 pmeth->decrypt = decrypt;
1330}
ba30bad5
DSH
1331
1332void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1333 int (*derive_init) (EVP_PKEY_CTX *ctx),
1334 int (*derive) (EVP_PKEY_CTX *ctx,
1335 unsigned char *key,
1336 size_t *keylen))
1337{
1338 pmeth->derive_init = derive_init;
1339 pmeth->derive = derive;
1340}
ba30bad5
DSH
1341
1342void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
1343 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1344 void *p2),
1345 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1346 const char *type,
1347 const char *value))
1348{
1349 pmeth->ctrl = ctrl;
1350 pmeth->ctrl_str = ctrl_str;
1351}
e7451ed1 1352
2555285f
AH
1353void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1354 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1355 const unsigned char *tbs, size_t tbslen))
1356{
1357 pmeth->digestsign = digestsign;
1358}
1359
1360void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1361 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1362 size_t siglen, const unsigned char *tbs,
1363 size_t tbslen))
1364{
1365 pmeth->digestverify = digestverify;
1366}
1367
2aee35d3
PY
1368void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1369 int (*check) (EVP_PKEY *pkey))
1370{
1371 pmeth->check = check;
1372}
1373
b0004708
PY
1374void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1375 int (*check) (EVP_PKEY *pkey))
1376{
1377 pmeth->public_check = check;
1378}
1379
1380void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1381 int (*check) (EVP_PKEY *pkey))
1382{
1383 pmeth->param_check = check;
1384}
1385
0a8fdef7
PY
1386void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1387 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1388 EVP_MD_CTX *mctx))
1389{
1390 pmeth->digest_custom = digest_custom;
1391}
1392
693be9a2 1393void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1394 int (**pinit) (EVP_PKEY_CTX *ctx))
1395{
1396 *pinit = pmeth->init;
1397}
1398
693be9a2 1399void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
e7451ed1 1400 int (**pcopy) (EVP_PKEY_CTX *dst,
9fdcc21f 1401 const EVP_PKEY_CTX *src))
e7451ed1
DSH
1402{
1403 *pcopy = pmeth->copy;
1404}
1405
693be9a2 1406void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1407 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1408{
1409 *pcleanup = pmeth->cleanup;
1410}
1411
693be9a2 1412void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1413 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1414 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1415 EVP_PKEY *pkey))
1416{
1417 if (pparamgen_init)
1418 *pparamgen_init = pmeth->paramgen_init;
1419 if (pparamgen)
1420 *pparamgen = pmeth->paramgen;
1421}
1422
693be9a2 1423void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1424 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1425 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1426 EVP_PKEY *pkey))
1427{
1428 if (pkeygen_init)
1429 *pkeygen_init = pmeth->keygen_init;
1430 if (pkeygen)
1431 *pkeygen = pmeth->keygen;
1432}
1433
693be9a2 1434void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1435 int (**psign_init) (EVP_PKEY_CTX *ctx),
1436 int (**psign) (EVP_PKEY_CTX *ctx,
1437 unsigned char *sig, size_t *siglen,
1438 const unsigned char *tbs,
1439 size_t tbslen))
1440{
1441 if (psign_init)
1442 *psign_init = pmeth->sign_init;
1443 if (psign)
1444 *psign = pmeth->sign;
1445}
1446
693be9a2 1447void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1448 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1449 int (**pverify) (EVP_PKEY_CTX *ctx,
1450 const unsigned char *sig,
1451 size_t siglen,
1452 const unsigned char *tbs,
1453 size_t tbslen))
1454{
1455 if (pverify_init)
1456 *pverify_init = pmeth->verify_init;
1457 if (pverify)
1458 *pverify = pmeth->verify;
1459}
1460
693be9a2 1461void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1462 int (**pverify_recover_init) (EVP_PKEY_CTX
1463 *ctx),
1464 int (**pverify_recover) (EVP_PKEY_CTX
1465 *ctx,
1466 unsigned char
1467 *sig,
1468 size_t *siglen,
1469 const unsigned
1470 char *tbs,
1471 size_t tbslen))
1472{
1473 if (pverify_recover_init)
1474 *pverify_recover_init = pmeth->verify_recover_init;
1475 if (pverify_recover)
1476 *pverify_recover = pmeth->verify_recover;
1477}
1478
693be9a2 1479void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1480 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1481 EVP_MD_CTX *mctx),
1482 int (**psignctx) (EVP_PKEY_CTX *ctx,
1483 unsigned char *sig,
1484 size_t *siglen,
1485 EVP_MD_CTX *mctx))
1486{
1487 if (psignctx_init)
1488 *psignctx_init = pmeth->signctx_init;
1489 if (psignctx)
1490 *psignctx = pmeth->signctx;
1491}
1492
693be9a2 1493void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1494 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1495 EVP_MD_CTX *mctx),
1496 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1497 const unsigned char *sig,
1498 int siglen,
1499 EVP_MD_CTX *mctx))
1500{
1501 if (pverifyctx_init)
1502 *pverifyctx_init = pmeth->verifyctx_init;
1503 if (pverifyctx)
1504 *pverifyctx = pmeth->verifyctx;
1505}
1506
693be9a2 1507void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1508 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1509 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1510 unsigned char *out,
1511 size_t *outlen,
1512 const unsigned char *in,
1513 size_t inlen))
1514{
1515 if (pencrypt_init)
1516 *pencrypt_init = pmeth->encrypt_init;
1517 if (pencryptfn)
1518 *pencryptfn = pmeth->encrypt;
1519}
1520
693be9a2 1521void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1522 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1523 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1524 unsigned char *out,
1525 size_t *outlen,
1526 const unsigned char *in,
1527 size_t inlen))
1528{
1529 if (pdecrypt_init)
1530 *pdecrypt_init = pmeth->decrypt_init;
1531 if (pdecrypt)
1532 *pdecrypt = pmeth->decrypt;
1533}
1534
693be9a2 1535void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1536 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1537 int (**pderive) (EVP_PKEY_CTX *ctx,
1538 unsigned char *key,
1539 size_t *keylen))
1540{
1541 if (pderive_init)
1542 *pderive_init = pmeth->derive_init;
1543 if (pderive)
1544 *pderive = pmeth->derive;
1545}
1546
693be9a2 1547void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1548 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1549 void *p2),
1550 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1551 const char *type,
1552 const char *value))
1553{
1554 if (pctrl)
1555 *pctrl = pmeth->ctrl;
1556 if (pctrl_str)
1557 *pctrl_str = pmeth->ctrl_str;
1558}
2aee35d3 1559
2555285f
AH
1560void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1561 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1562 const unsigned char *tbs, size_t tbslen))
1563{
1564 if (digestsign)
1565 *digestsign = pmeth->digestsign;
1566}
1567
1568void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1569 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1570 size_t siglen, const unsigned char *tbs,
1571 size_t tbslen))
1572{
1573 if (digestverify)
1574 *digestverify = pmeth->digestverify;
1575}
1576
693be9a2 1577void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2aee35d3
PY
1578 int (**pcheck) (EVP_PKEY *pkey))
1579{
34f5c8b1 1580 if (pcheck != NULL)
2aee35d3
PY
1581 *pcheck = pmeth->check;
1582}
b0004708 1583
693be9a2 1584void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1585 int (**pcheck) (EVP_PKEY *pkey))
1586{
34f5c8b1 1587 if (pcheck != NULL)
b0004708
PY
1588 *pcheck = pmeth->public_check;
1589}
1590
693be9a2 1591void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1592 int (**pcheck) (EVP_PKEY *pkey))
1593{
34f5c8b1 1594 if (pcheck != NULL)
b0004708
PY
1595 *pcheck = pmeth->param_check;
1596}
0a8fdef7
PY
1597
1598void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1599 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1600 EVP_MD_CTX *mctx))
1601{
675f4cee 1602 if (pdigest_custom != NULL)
0a8fdef7
PY
1603 *pdigest_custom = pmeth->digest_custom;
1604}
e683582b 1605
f844f9eb 1606#endif /* FIPS_MODULE */