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