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