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