]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
Fix leak with no-ec config
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
CommitLineData
d0ea49a8 1
0f113f3e 2/*
b0edda11 3 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
0b6f3c66 4 *
4a8b0c55 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
0b6f3c66
DSH
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
3c27208f 13#include <openssl/engine.h>
33bed28b 14#include <openssl/evp.h>
99119000 15#include <openssl/x509v3.h>
35aca9ec
MC
16#include <openssl/core_names.h>
17#include <openssl/dh.h>
18#include "internal/cryptlib.h"
25f2138b
DMSP
19#include "crypto/asn1.h"
20#include "crypto/evp.h"
99119000 21#include "internal/numbers.h"
390acbeb 22#include "internal/provider.h"
706457b7 23#include "evp_local.h"
0b6f3c66 24
19bd1fa1 25typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
0f113f3e 26typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
5ce278a7 27
df2ee0e2 28static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
0b6f3c66 29
cefa762e 30/* This array needs to be in order of NIDs */
19bd1fa1 31static pmeth_fn standard_methods[] = {
d4f0339c 32#ifndef OPENSSL_NO_RSA
19bd1fa1 33 rsa_pkey_method,
d4f0339c
DSH
34#endif
35#ifndef OPENSSL_NO_DH
19bd1fa1 36 dh_pkey_method,
d4f0339c
DSH
37#endif
38#ifndef OPENSSL_NO_DSA
19bd1fa1 39 dsa_pkey_method,
d4f0339c 40#endif
ef236ec3 41#ifndef OPENSSL_NO_EC
19bd1fa1 42 ec_pkey_method,
ef236ec3 43#endif
19bd1fa1 44 hmac_pkey_method,
b4a3aeeb 45#ifndef OPENSSL_NO_CMAC
19bd1fa1 46 cmac_pkey_method,
b4a3aeeb 47#endif
6577e008 48#ifndef OPENSSL_NO_RSA
19bd1fa1 49 rsa_pss_pkey_method,
6577e008 50#endif
afb14cda 51#ifndef OPENSSL_NO_DH
19bd1fa1 52 dhx_pkey_method,
cefa762e
JB
53#endif
54#ifndef OPENSSL_NO_SCRYPT
19bd1fa1 55 scrypt_pkey_method,
afb14cda 56#endif
19bd1fa1 57 tls1_prf_pkey_method,
262bd85f 58#ifndef OPENSSL_NO_EC
19bd1fa1
PS
59 ecx25519_pkey_method,
60 ecx448_pkey_method,
262bd85f 61#endif
19bd1fa1 62 hkdf_pkey_method,
52ad5b60 63#ifndef OPENSSL_NO_POLY1305
19bd1fa1 64 poly1305_pkey_method,
52ad5b60 65#endif
3f5616d7 66#ifndef OPENSSL_NO_SIPHASH
19bd1fa1 67 siphash_pkey_method,
3f5616d7 68#endif
42a3008a 69#ifndef OPENSSL_NO_EC
19bd1fa1
PS
70 ed25519_pkey_method,
71 ed448_pkey_method,
42a3008a 72#endif
ddb634fe 73#ifndef OPENSSL_NO_SM2
19bd1fa1 74 sm2_pkey_method,
ddb634fe 75#endif
0f113f3e 76};
0b6f3c66 77
19bd1fa1
PS
78DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
79
80static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
81{
82 return ((*a)->pkey_id - ((**b)())->pkey_id);
83}
84
85IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
babb3798 86
0f113f3e
MC
87static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
88 const EVP_PKEY_METHOD *const *b)
89{
90 return ((*a)->pkey_id - (*b)->pkey_id);
91}
0b6f3c66 92
c9777d26 93const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
0f113f3e 94{
19bd1fa1 95 pmeth_fn *ret;
0f113f3e 96 EVP_PKEY_METHOD tmp;
19bd1fa1 97 const EVP_PKEY_METHOD *t = &tmp;
12a765a5 98
0f113f3e
MC
99 tmp.pkey_id = type;
100 if (app_pkey_methods) {
101 int idx;
102 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
103 if (idx >= 0)
104 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
105 }
19bd1fa1
PS
106 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
107 sizeof(standard_methods) /
108 sizeof(pmeth_fn));
12a765a5 109 if (ret == NULL || *ret == NULL)
0f113f3e 110 return NULL;
19bd1fa1 111 return (**ret)();
0f113f3e 112}
0b6f3c66 113
a07c17ef
RL
114static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e,
115 const char *name, const char *propquery,
116 int id)
0f113f3e
MC
117{
118 EVP_PKEY_CTX *ret;
d0ea49a8
RL
119 const EVP_PKEY_METHOD *pmeth = NULL;
120
121 /*
122 * When using providers, the context is bound to the algo implementation
123 * later.
124 */
125 if (pkey == NULL && e == NULL && id == -1)
126 goto common;
2f2e6b62 127
d0ea49a8
RL
128 /* TODO(3.0) Legacy code should be removed when all is provider based */
129 /* BEGIN legacy */
0f113f3e 130 if (id == -1) {
a6465b3f
P
131 if (pkey == NULL)
132 return 0;
2f2e6b62 133 id = pkey->type;
0f113f3e 134 }
a07c17ef
RL
135 name = OBJ_nid2sn(id);
136 propquery = NULL;
a63bf2c5 137#ifndef OPENSSL_NO_ENGINE
c2976edf 138 if (e == NULL && pkey != NULL)
d19b01ad 139 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
0f113f3e
MC
140 /* Try to find an ENGINE which implements this method */
141 if (e) {
142 if (!ENGINE_init(e)) {
143 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
144 return NULL;
145 }
c2976edf 146 } else {
0f113f3e 147 e = ENGINE_get_pkey_meth_engine(id);
c2976edf 148 }
0f113f3e
MC
149
150 /*
0d4fb843 151 * If an ENGINE handled this method look it up. Otherwise use internal
0f113f3e
MC
152 * tables.
153 */
0f113f3e
MC
154 if (e)
155 pmeth = ENGINE_get_pkey_meth(e, id);
156 else
a63bf2c5 157#endif
0f113f3e 158 pmeth = EVP_PKEY_meth_find(id);
c9777d26 159
0f113f3e 160 if (pmeth == NULL) {
918a27fa
DSH
161#ifndef OPENSSL_NO_ENGINE
162 ENGINE_finish(e);
163#endif
0f113f3e
MC
164 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
165 return NULL;
166 }
d0ea49a8 167 /* END legacy */
c9777d26 168
d0ea49a8 169 common:
64b25758 170 ret = OPENSSL_zalloc(sizeof(*ret));
90945fa3 171 if (ret == NULL) {
a63bf2c5 172#ifndef OPENSSL_NO_ENGINE
7c96dbcd 173 ENGINE_finish(e);
a63bf2c5 174#endif
0f113f3e
MC
175 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
176 return NULL;
177 }
a07c17ef
RL
178 ret->algorithm = name;
179 ret->propquery = propquery;
0f113f3e
MC
180 ret->engine = e;
181 ret->pmeth = pmeth;
182 ret->operation = EVP_PKEY_OP_UNDEFINED;
183 ret->pkey = pkey;
a6465b3f 184 if (pkey != NULL)
03273d61 185 EVP_PKEY_up_ref(pkey);
0f113f3e 186
8b84b075 187 if (pmeth != NULL && pmeth->init != NULL) {
0f113f3e 188 if (pmeth->init(ret) <= 0) {
83b4049a 189 ret->pmeth = NULL;
0f113f3e
MC
190 EVP_PKEY_CTX_free(ret);
191 return NULL;
192 }
193 }
194
195 return ret;
196}
197
864b89ce
MC
198void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
199{
200 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
201 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
202 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
203 EVP_KEYEXCH_free(ctx->op.kex.exchange);
204 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
205 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
206 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
207 EVP_SIGNATURE_free(ctx->op.sig.signature);
208 }
209}
210
0f113f3e
MC
211EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
212{
213 EVP_PKEY_METHOD *pmeth;
b4faea50 214
b51bce94 215 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
3484236d
F
216 if (pmeth == NULL) {
217 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
0f113f3e 218 return NULL;
3484236d 219 }
0f113f3e 220
0f113f3e
MC
221 pmeth->pkey_id = id;
222 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
0f113f3e
MC
223 return pmeth;
224}
ba30bad5 225
f830c68f 226void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
0f113f3e
MC
227 const EVP_PKEY_METHOD *meth)
228{
229 if (ppkey_id)
230 *ppkey_id = meth->pkey_id;
231 if (pflags)
232 *pflags = meth->flags;
233}
f830c68f
DSH
234
235void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
0f113f3e 236{
f830c68f 237
0f113f3e
MC
238 dst->init = src->init;
239 dst->copy = src->copy;
240 dst->cleanup = src->cleanup;
f830c68f 241
0f113f3e
MC
242 dst->paramgen_init = src->paramgen_init;
243 dst->paramgen = src->paramgen;
f830c68f 244
0f113f3e
MC
245 dst->keygen_init = src->keygen_init;
246 dst->keygen = src->keygen;
f830c68f 247
0f113f3e
MC
248 dst->sign_init = src->sign_init;
249 dst->sign = src->sign;
f830c68f 250
0f113f3e
MC
251 dst->verify_init = src->verify_init;
252 dst->verify = src->verify;
f830c68f 253
0f113f3e
MC
254 dst->verify_recover_init = src->verify_recover_init;
255 dst->verify_recover = src->verify_recover;
f830c68f 256
0f113f3e
MC
257 dst->signctx_init = src->signctx_init;
258 dst->signctx = src->signctx;
f830c68f 259
0f113f3e
MC
260 dst->verifyctx_init = src->verifyctx_init;
261 dst->verifyctx = src->verifyctx;
f830c68f 262
0f113f3e
MC
263 dst->encrypt_init = src->encrypt_init;
264 dst->encrypt = src->encrypt;
f830c68f 265
0f113f3e
MC
266 dst->decrypt_init = src->decrypt_init;
267 dst->decrypt = src->decrypt;
f830c68f 268
0f113f3e
MC
269 dst->derive_init = src->derive_init;
270 dst->derive = src->derive;
f830c68f 271
0f113f3e
MC
272 dst->ctrl = src->ctrl;
273 dst->ctrl_str = src->ctrl_str;
2aee35d3
PY
274
275 dst->check = src->check;
0f113f3e 276}
f830c68f 277
ba30bad5 278void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
279{
280 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
281 OPENSSL_free(pmeth);
282}
ba30bad5 283
f5cda4cb 284EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
0f113f3e 285{
a07c17ef 286 return int_ctx_new(pkey, e, NULL, NULL, -1);
0f113f3e 287}
f5cda4cb
DSH
288
289EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
0f113f3e 290{
a07c17ef
RL
291 return int_ctx_new(NULL, e, NULL, NULL, id);
292}
293
294EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(const char *name,
295 const char *propquery)
296{
297 return int_ctx_new(NULL, NULL, name, propquery, -1);
0f113f3e 298}
f5cda4cb 299
9fdcc21f 300EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
0f113f3e
MC
301{
302 EVP_PKEY_CTX *rctx;
ff64702b
MC
303
304 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
864b89ce
MC
305 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
306 && pctx->op.kex.exchprovctx == NULL)
307 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
308 && pctx->op.sig.sigprovctx == NULL)))
0f113f3e 309 return NULL;
c9777d26 310#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
311 /* Make sure it's safe to copy a pkey context using an ENGINE */
312 if (pctx->engine && !ENGINE_init(pctx->engine)) {
313 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
314 return 0;
315 }
c9777d26 316#endif
ff64702b 317 rctx = OPENSSL_zalloc(sizeof(*rctx));
3484236d
F
318 if (rctx == NULL) {
319 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
0f113f3e 320 return NULL;
3484236d 321 }
8bdcef40 322
ff64702b
MC
323 if (pctx->pkey != NULL)
324 EVP_PKEY_up_ref(pctx->pkey);
325 rctx->pkey = pctx->pkey;
326 rctx->operation = pctx->operation;
a07c17ef
RL
327 rctx->algorithm = pctx->algorithm;
328 rctx->propquery = pctx->propquery;
ff64702b 329
864b89ce
MC
330 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
331 if (pctx->op.kex.exchange != NULL) {
332 rctx->op.kex.exchange = pctx->op.kex.exchange;
333 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
334 OPENSSL_free(rctx);
335 return NULL;
336 }
ff64702b 337 }
864b89ce
MC
338 if (pctx->op.kex.exchprovctx != NULL) {
339 if (!ossl_assert(pctx->op.kex.exchange != NULL))
340 return NULL;
341 rctx->op.kex.exchprovctx
342 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
343 if (rctx->op.kex.exchprovctx == NULL) {
344 EVP_KEYEXCH_free(rctx->op.kex.exchange);
345 OPENSSL_free(rctx);
346 return NULL;
347 }
348 return rctx;
349 }
350 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
351 if (pctx->op.sig.signature != NULL) {
352 rctx->op.sig.signature = pctx->op.sig.signature;
353 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
354 OPENSSL_free(rctx);
355 return NULL;
356 }
357 }
358 if (pctx->op.sig.sigprovctx != NULL) {
359 if (!ossl_assert(pctx->op.sig.signature != NULL))
360 return NULL;
361 rctx->op.sig.sigprovctx
362 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
363 if (rctx->op.sig.sigprovctx == NULL) {
364 EVP_SIGNATURE_free(rctx->op.sig.signature);
365 OPENSSL_free(rctx);
366 return NULL;
367 }
368 return rctx;
ff64702b 369 }
ff64702b
MC
370 }
371
0f113f3e 372 rctx->pmeth = pctx->pmeth;
c9777d26 373#ifndef OPENSSL_NO_ENGINE
0f113f3e 374 rctx->engine = pctx->engine;
c9777d26 375#endif
8bdcef40 376
0f113f3e 377 if (pctx->peerkey)
03273d61 378 EVP_PKEY_up_ref(pctx->peerkey);
0f113f3e 379 rctx->peerkey = pctx->peerkey;
8bdcef40 380
0f113f3e
MC
381 if (pctx->pmeth->copy(rctx, pctx) > 0)
382 return rctx;
8bdcef40 383
83b4049a 384 rctx->pmeth = NULL;
0f113f3e
MC
385 EVP_PKEY_CTX_free(rctx);
386 return NULL;
8bdcef40 387
0f113f3e 388}
8bdcef40 389
ba30bad5 390int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
391{
392 if (app_pkey_methods == NULL) {
393 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
3484236d
F
394 if (app_pkey_methods == NULL){
395 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 396 return 0;
3484236d 397 }
0f113f3e 398 }
3484236d
F
399 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
400 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 401 return 0;
3484236d 402 }
0f113f3e
MC
403 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
404 return 1;
405}
ba30bad5 406
0822e89a
PY
407void evp_app_cleanup_int(void)
408{
409 if (app_pkey_methods != NULL)
410 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
411}
412
413int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
414{
415 const EVP_PKEY_METHOD *ret;
416
417 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
418
419 return ret == NULL ? 0 : 1;
420}
421
48ed9c23
DSH
422size_t EVP_PKEY_meth_get_count(void)
423{
424 size_t rv = OSSL_NELEM(standard_methods);
425
426 if (app_pkey_methods)
427 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
428 return rv;
429}
430
431const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
432{
433 if (idx < OSSL_NELEM(standard_methods))
19bd1fa1 434 return (standard_methods[idx])();
48ed9c23
DSH
435 if (app_pkey_methods == NULL)
436 return NULL;
437 idx -= OSSL_NELEM(standard_methods);
438 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
439 return NULL;
440 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
441}
442
5da98aa6 443void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
0f113f3e
MC
444{
445 if (ctx == NULL)
446 return;
447 if (ctx->pmeth && ctx->pmeth->cleanup)
448 ctx->pmeth->cleanup(ctx);
ff64702b 449
864b89ce 450 evp_pkey_ctx_free_old_ops(ctx);
dfcb5d29 451
c5ba2d99
RS
452 EVP_PKEY_free(ctx->pkey);
453 EVP_PKEY_free(ctx->peerkey);
c9777d26 454#ifndef OPENSSL_NO_ENGINE
7c96dbcd 455 ENGINE_finish(ctx->engine);
c9777d26 456#endif
0f113f3e
MC
457 OPENSSL_free(ctx);
458}
5da98aa6 459
9c45222d
MC
460int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
461{
864b89ce
MC
462 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
463 && ctx->op.sig.sigprovctx != NULL
464 && ctx->op.sig.signature != NULL
465 && ctx->op.sig.signature->get_ctx_params != NULL)
466 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
467 params);
9c45222d
MC
468 return 0;
469}
470
471const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
472{
864b89ce
MC
473 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
474 && ctx->op.sig.signature != NULL
475 && ctx->op.sig.signature->gettable_ctx_params != NULL)
476 return ctx->op.sig.signature->gettable_ctx_params();
9c45222d
MC
477
478 return NULL;
479}
480
35aca9ec
MC
481int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
482{
864b89ce
MC
483 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
484 && ctx->op.kex.exchprovctx != NULL
485 && ctx->op.kex.exchange != NULL
486 && ctx->op.kex.exchange->set_ctx_params != NULL)
487 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
488 params);
489 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
490 && ctx->op.sig.sigprovctx != NULL
491 && ctx->op.sig.signature != NULL
492 && ctx->op.sig.signature->set_ctx_params != NULL)
493 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
494 params);
35aca9ec
MC
495 return 0;
496}
497
9c45222d
MC
498const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
499{
864b89ce
MC
500 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
501 && ctx->op.kex.exchange != NULL
502 && ctx->op.kex.exchange->settable_ctx_params != NULL)
503 return ctx->op.kex.exchange->settable_ctx_params();
504 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
505 && ctx->op.sig.signature != NULL
506 && ctx->op.sig.signature->settable_ctx_params != NULL)
507 return ctx->op.sig.signature->settable_ctx_params();
9c45222d
MC
508
509 return NULL;
510}
511
76ca35e7 512#ifndef OPENSSL_NO_DH
35aca9ec
MC
513int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
514{
515 OSSL_PARAM dh_pad_params[2];
1c3ace68 516 unsigned int upad = pad;
35aca9ec 517
864b89ce
MC
518 /* We use EVP_PKEY_CTX_ctrl return values */
519 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
520 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
521 return -2;
522 }
523
35aca9ec 524 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 525 if (ctx->op.kex.exchprovctx == NULL)
35aca9ec
MC
526 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
527 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
528
1c3ace68 529 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
35aca9ec
MC
530 dh_pad_params[1] = OSSL_PARAM_construct_end();
531
532 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
533}
76ca35e7 534#endif
35aca9ec 535
9c45222d
MC
536int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
537{
538 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
539 /* 80 should be big enough */
540 char name[80] = "";
541 const EVP_MD *tmp;
542
864b89ce 543 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
544 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
545 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
546 return -2;
547 }
548
549 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 550 if (ctx->op.sig.sigprovctx == NULL)
9c45222d
MC
551 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
552 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
553
554 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
555 name,
556 sizeof(name));
557 *p++ = OSSL_PARAM_construct_end();
558
559 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
560 return 0;
561
562 tmp = EVP_get_digestbyname(name);
563 if (tmp == NULL)
564 return 0;
565
566 *md = tmp;
567
568 return 1;
569}
570
4889dadc
MC
571int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
572{
9c45222d 573 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
4889dadc
MC
574 size_t mdsize;
575 const char *name;
576
864b89ce 577 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
9c45222d
MC
578 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
579 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
580 return -2;
581 }
582
4889dadc 583 /* TODO(3.0): Remove this eventually when no more legacy */
864b89ce 584 if (ctx->op.sig.sigprovctx == NULL)
4889dadc
MC
585 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
586 EVP_PKEY_CTRL_MD, 0, (void *)(md));
587
9c45222d
MC
588 if (md == NULL) {
589 name = "";
590 mdsize = 0;
591 } else {
592 mdsize = EVP_MD_size(md);
593 name = EVP_MD_name(md);
594 }
4889dadc 595
9c45222d
MC
596 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
597 /*
598 * Cast away the const. This is read
599 * only so should be safe
600 */
601 (char *)name,
602 strlen(name) + 1);
603 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
604 &mdsize);
605 *p++ = OSSL_PARAM_construct_end();
4889dadc 606
9c45222d 607 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
4889dadc
MC
608}
609
35aca9ec
MC
610static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
611 int cmd, int p1, void *p2)
612{
613 switch (cmd) {
76ca35e7 614#ifndef OPENSSL_NO_DH
35aca9ec
MC
615 case EVP_PKEY_CTRL_DH_PAD:
616 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
76ca35e7 617#endif
390acbeb
MC
618 case EVP_PKEY_CTRL_MD:
619 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
864b89ce
MC
620 case EVP_PKEY_CTRL_GET_MD:
621 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
35aca9ec
MC
622 }
623 return 0;
624}
625
0b6f3c66 626int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
0f113f3e
MC
627 int cmd, int p1, void *p2)
628{
629 int ret;
4803717f 630
35aca9ec
MC
631 if (ctx == NULL) {
632 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
633 return -2;
634 }
635
864b89ce
MC
636 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
637 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
638 && ctx->op.sig.sigprovctx != NULL))
35aca9ec
MC
639 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
640
641 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
0f113f3e
MC
642 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
643 return -2;
644 }
645 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
646 return -1;
647
4803717f
PY
648 /* Skip the operation checks since this is called in a very early stage */
649 if (ctx->pmeth->digest_custom != NULL)
650 goto doit;
651
0f113f3e
MC
652 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
653 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
654 return -1;
655 }
656
657 if ((optype != -1) && !(ctx->operation & optype)) {
658 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
659 return -1;
660 }
661
4803717f 662 doit:
0f113f3e
MC
663 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
664
665 if (ret == -2)
666 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
667
668 return ret;
0f113f3e 669}
0b6f3c66 670
cefa762e 671int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 672 int cmd, uint64_t value)
cefa762e
JB
673{
674 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
675}
676
35aca9ec
MC
677static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
678 const char *value)
679{
76ca35e7 680#ifndef OPENSSL_NO_DH
35aca9ec
MC
681 if (strcmp(name, "dh_pad") == 0) {
682 int pad;
683
684 pad = atoi(value);
685 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
686 }
76ca35e7 687#endif
390acbeb
MC
688 if (strcmp(name, "digest") == 0) {
689 int ret;
864b89ce
MC
690 EVP_MD *md;
691
692 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
693 return 0;
694 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
695 value, NULL);
390acbeb
MC
696 if (md == NULL)
697 return 0;
698 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
699 EVP_MD_meth_free(md);
700 return ret;
701 }
702
35aca9ec
MC
703 return 0;
704}
705
4a3dc3c0 706int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
0f113f3e
MC
707 const char *name, const char *value)
708{
35aca9ec
MC
709 if (ctx == NULL) {
710 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
711 return -2;
712 }
713
864b89ce
MC
714 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
715 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
716 && ctx->op.sig.sigprovctx != NULL))
35aca9ec
MC
717 return legacy_ctrl_str_to_param(ctx, name, value);
718
0f113f3e
MC
719 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
720 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
721 return -2;
722 }
410877ba
DSH
723 if (strcmp(name, "digest") == 0)
724 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
725 value);
0f113f3e
MC
726 return ctx->pmeth->ctrl_str(ctx, name, value);
727}
f5cda4cb 728
99119000
DSH
729/* Utility functions to send a string of hex string to a ctrl */
730
731int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
732{
733 size_t len;
734
735 len = strlen(str);
736 if (len > INT_MAX)
737 return -1;
738 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
739}
740
741int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
742{
743 unsigned char *bin;
744 long binlen;
745 int rv = -1;
746
14f051a0 747 bin = OPENSSL_hexstr2buf(hex, &binlen);
99119000
DSH
748 if (bin == NULL)
749 return 0;
750 if (binlen <= INT_MAX)
751 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
752 OPENSSL_free(bin);
753 return rv;
754}
52ad523c 755
410877ba
DSH
756/* Pass a message digest to a ctrl */
757int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
758{
759 const EVP_MD *m;
c82bafc5 760
410877ba
DSH
761 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
762 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
763 return 0;
764 }
765 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
766}
99119000 767
b28dea4e 768int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
0f113f3e
MC
769{
770 return ctx->operation;
771}
b28dea4e
DSH
772
773void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
0f113f3e
MC
774{
775 ctx->keygen_info = dat;
776 ctx->keygen_info_count = datlen;
777}
b28dea4e 778
f5cda4cb 779void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
780{
781 ctx->data = data;
782}
f5cda4cb 783
9fdcc21f 784void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
0f113f3e
MC
785{
786 return ctx->data;
787}
f5cda4cb 788
81cebb8b 789EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
790{
791 return ctx->pkey;
792}
81cebb8b 793
0e1dba93 794EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
795{
796 return ctx->peerkey;
797}
798
f5cda4cb 799void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
800{
801 ctx->app_data = data;
802}
f5cda4cb
DSH
803
804void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
0f113f3e
MC
805{
806 return ctx->app_data;
807}
ba30bad5
DSH
808
809void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
810 int (*init) (EVP_PKEY_CTX *ctx))
811{
812 pmeth->init = init;
813}
8bdcef40
DSH
814
815void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
0f113f3e 816 int (*copy) (EVP_PKEY_CTX *dst,
9fdcc21f 817 const EVP_PKEY_CTX *src))
0f113f3e
MC
818{
819 pmeth->copy = copy;
820}
ba30bad5
DSH
821
822void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
823 void (*cleanup) (EVP_PKEY_CTX *ctx))
824{
825 pmeth->cleanup = cleanup;
826}
ba30bad5
DSH
827
828void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
829 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
830 int (*paramgen) (EVP_PKEY_CTX *ctx,
831 EVP_PKEY *pkey))
832{
833 pmeth->paramgen_init = paramgen_init;
834 pmeth->paramgen = paramgen;
835}
ba30bad5
DSH
836
837void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
838 int (*keygen_init) (EVP_PKEY_CTX *ctx),
839 int (*keygen) (EVP_PKEY_CTX *ctx,
840 EVP_PKEY *pkey))
841{
842 pmeth->keygen_init = keygen_init;
843 pmeth->keygen = keygen;
844}
ba30bad5
DSH
845
846void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
847 int (*sign_init) (EVP_PKEY_CTX *ctx),
848 int (*sign) (EVP_PKEY_CTX *ctx,
849 unsigned char *sig, size_t *siglen,
850 const unsigned char *tbs,
851 size_t tbslen))
852{
853 pmeth->sign_init = sign_init;
854 pmeth->sign = sign;
855}
ba30bad5
DSH
856
857void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
858 int (*verify_init) (EVP_PKEY_CTX *ctx),
859 int (*verify) (EVP_PKEY_CTX *ctx,
860 const unsigned char *sig,
861 size_t siglen,
862 const unsigned char *tbs,
863 size_t tbslen))
864{
865 pmeth->verify_init = verify_init;
866 pmeth->verify = verify;
867}
ba30bad5
DSH
868
869void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
870 int (*verify_recover_init) (EVP_PKEY_CTX
871 *ctx),
872 int (*verify_recover) (EVP_PKEY_CTX
873 *ctx,
874 unsigned char
875 *sig,
876 size_t *siglen,
877 const unsigned
878 char *tbs,
879 size_t tbslen))
880{
881 pmeth->verify_recover_init = verify_recover_init;
882 pmeth->verify_recover = verify_recover;
883}
ba30bad5
DSH
884
885void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
886 int (*signctx_init) (EVP_PKEY_CTX *ctx,
887 EVP_MD_CTX *mctx),
888 int (*signctx) (EVP_PKEY_CTX *ctx,
889 unsigned char *sig,
890 size_t *siglen,
891 EVP_MD_CTX *mctx))
892{
893 pmeth->signctx_init = signctx_init;
894 pmeth->signctx = signctx;
895}
ba30bad5
DSH
896
897void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
898 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
899 EVP_MD_CTX *mctx),
900 int (*verifyctx) (EVP_PKEY_CTX *ctx,
901 const unsigned char *sig,
902 int siglen,
903 EVP_MD_CTX *mctx))
904{
905 pmeth->verifyctx_init = verifyctx_init;
906 pmeth->verifyctx = verifyctx;
907}
ba30bad5
DSH
908
909void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
910 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
911 int (*encryptfn) (EVP_PKEY_CTX *ctx,
912 unsigned char *out,
913 size_t *outlen,
914 const unsigned char *in,
915 size_t inlen))
916{
917 pmeth->encrypt_init = encrypt_init;
918 pmeth->encrypt = encryptfn;
919}
ba30bad5
DSH
920
921void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
922 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
923 int (*decrypt) (EVP_PKEY_CTX *ctx,
924 unsigned char *out,
925 size_t *outlen,
926 const unsigned char *in,
927 size_t inlen))
928{
929 pmeth->decrypt_init = decrypt_init;
930 pmeth->decrypt = decrypt;
931}
ba30bad5
DSH
932
933void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
934 int (*derive_init) (EVP_PKEY_CTX *ctx),
935 int (*derive) (EVP_PKEY_CTX *ctx,
936 unsigned char *key,
937 size_t *keylen))
938{
939 pmeth->derive_init = derive_init;
940 pmeth->derive = derive;
941}
ba30bad5
DSH
942
943void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
944 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
945 void *p2),
946 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
947 const char *type,
948 const char *value))
949{
950 pmeth->ctrl = ctrl;
951 pmeth->ctrl_str = ctrl_str;
952}
e7451ed1 953
2aee35d3
PY
954void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
955 int (*check) (EVP_PKEY *pkey))
956{
957 pmeth->check = check;
958}
959
b0004708
PY
960void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
961 int (*check) (EVP_PKEY *pkey))
962{
963 pmeth->public_check = check;
964}
965
966void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
967 int (*check) (EVP_PKEY *pkey))
968{
969 pmeth->param_check = check;
970}
971
0a8fdef7
PY
972void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
973 int (*digest_custom) (EVP_PKEY_CTX *ctx,
974 EVP_MD_CTX *mctx))
975{
976 pmeth->digest_custom = digest_custom;
977}
978
693be9a2 979void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
980 int (**pinit) (EVP_PKEY_CTX *ctx))
981{
982 *pinit = pmeth->init;
983}
984
693be9a2 985void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
e7451ed1 986 int (**pcopy) (EVP_PKEY_CTX *dst,
9fdcc21f 987 const EVP_PKEY_CTX *src))
e7451ed1
DSH
988{
989 *pcopy = pmeth->copy;
990}
991
693be9a2 992void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
993 void (**pcleanup) (EVP_PKEY_CTX *ctx))
994{
995 *pcleanup = pmeth->cleanup;
996}
997
693be9a2 998void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
999 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1000 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1001 EVP_PKEY *pkey))
1002{
1003 if (pparamgen_init)
1004 *pparamgen_init = pmeth->paramgen_init;
1005 if (pparamgen)
1006 *pparamgen = pmeth->paramgen;
1007}
1008
693be9a2 1009void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1010 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1011 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1012 EVP_PKEY *pkey))
1013{
1014 if (pkeygen_init)
1015 *pkeygen_init = pmeth->keygen_init;
1016 if (pkeygen)
1017 *pkeygen = pmeth->keygen;
1018}
1019
693be9a2 1020void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1021 int (**psign_init) (EVP_PKEY_CTX *ctx),
1022 int (**psign) (EVP_PKEY_CTX *ctx,
1023 unsigned char *sig, size_t *siglen,
1024 const unsigned char *tbs,
1025 size_t tbslen))
1026{
1027 if (psign_init)
1028 *psign_init = pmeth->sign_init;
1029 if (psign)
1030 *psign = pmeth->sign;
1031}
1032
693be9a2 1033void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1034 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1035 int (**pverify) (EVP_PKEY_CTX *ctx,
1036 const unsigned char *sig,
1037 size_t siglen,
1038 const unsigned char *tbs,
1039 size_t tbslen))
1040{
1041 if (pverify_init)
1042 *pverify_init = pmeth->verify_init;
1043 if (pverify)
1044 *pverify = pmeth->verify;
1045}
1046
693be9a2 1047void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1048 int (**pverify_recover_init) (EVP_PKEY_CTX
1049 *ctx),
1050 int (**pverify_recover) (EVP_PKEY_CTX
1051 *ctx,
1052 unsigned char
1053 *sig,
1054 size_t *siglen,
1055 const unsigned
1056 char *tbs,
1057 size_t tbslen))
1058{
1059 if (pverify_recover_init)
1060 *pverify_recover_init = pmeth->verify_recover_init;
1061 if (pverify_recover)
1062 *pverify_recover = pmeth->verify_recover;
1063}
1064
693be9a2 1065void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1066 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1067 EVP_MD_CTX *mctx),
1068 int (**psignctx) (EVP_PKEY_CTX *ctx,
1069 unsigned char *sig,
1070 size_t *siglen,
1071 EVP_MD_CTX *mctx))
1072{
1073 if (psignctx_init)
1074 *psignctx_init = pmeth->signctx_init;
1075 if (psignctx)
1076 *psignctx = pmeth->signctx;
1077}
1078
693be9a2 1079void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1080 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1081 EVP_MD_CTX *mctx),
1082 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1083 const unsigned char *sig,
1084 int siglen,
1085 EVP_MD_CTX *mctx))
1086{
1087 if (pverifyctx_init)
1088 *pverifyctx_init = pmeth->verifyctx_init;
1089 if (pverifyctx)
1090 *pverifyctx = pmeth->verifyctx;
1091}
1092
693be9a2 1093void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1094 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1095 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1096 unsigned char *out,
1097 size_t *outlen,
1098 const unsigned char *in,
1099 size_t inlen))
1100{
1101 if (pencrypt_init)
1102 *pencrypt_init = pmeth->encrypt_init;
1103 if (pencryptfn)
1104 *pencryptfn = pmeth->encrypt;
1105}
1106
693be9a2 1107void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1108 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1109 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1110 unsigned char *out,
1111 size_t *outlen,
1112 const unsigned char *in,
1113 size_t inlen))
1114{
1115 if (pdecrypt_init)
1116 *pdecrypt_init = pmeth->decrypt_init;
1117 if (pdecrypt)
1118 *pdecrypt = pmeth->decrypt;
1119}
1120
693be9a2 1121void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1122 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1123 int (**pderive) (EVP_PKEY_CTX *ctx,
1124 unsigned char *key,
1125 size_t *keylen))
1126{
1127 if (pderive_init)
1128 *pderive_init = pmeth->derive_init;
1129 if (pderive)
1130 *pderive = pmeth->derive;
1131}
1132
693be9a2 1133void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1134 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1135 void *p2),
1136 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1137 const char *type,
1138 const char *value))
1139{
1140 if (pctrl)
1141 *pctrl = pmeth->ctrl;
1142 if (pctrl_str)
1143 *pctrl_str = pmeth->ctrl_str;
1144}
2aee35d3 1145
693be9a2 1146void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2aee35d3
PY
1147 int (**pcheck) (EVP_PKEY *pkey))
1148{
34f5c8b1 1149 if (pcheck != NULL)
2aee35d3
PY
1150 *pcheck = pmeth->check;
1151}
b0004708 1152
693be9a2 1153void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1154 int (**pcheck) (EVP_PKEY *pkey))
1155{
34f5c8b1 1156 if (pcheck != NULL)
b0004708
PY
1157 *pcheck = pmeth->public_check;
1158}
1159
693be9a2 1160void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1161 int (**pcheck) (EVP_PKEY *pkey))
1162{
34f5c8b1 1163 if (pcheck != NULL)
b0004708
PY
1164 *pcheck = pmeth->param_check;
1165}
0a8fdef7
PY
1166
1167void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1168 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1169 EVP_MD_CTX *mctx))
1170{
675f4cee 1171 if (pdigest_custom != NULL)
0a8fdef7
PY
1172 *pdigest_custom = pmeth->digest_custom;
1173}