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