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