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