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