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