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