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