]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_lib.c
Revise EVP_PKEY param handling
[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>
18#include "internal/cryptlib.h"
5fe736e5 19#include "internal/asn1_int.h"
27af42f9 20#include "internal/evp_int.h"
99119000 21#include "internal/numbers.h"
390acbeb 22#include "internal/provider.h"
ff64702b 23#include "evp_locl.h"
0b6f3c66 24
0f113f3e 25typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
5ce278a7 26
df2ee0e2 27static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
0b6f3c66 28
cefa762e 29/* This array needs to be in order of NIDs */
0f113f3e 30static const EVP_PKEY_METHOD *standard_methods[] = {
d4f0339c 31#ifndef OPENSSL_NO_RSA
0f113f3e 32 &rsa_pkey_meth,
d4f0339c
DSH
33#endif
34#ifndef OPENSSL_NO_DH
0f113f3e 35 &dh_pkey_meth,
d4f0339c
DSH
36#endif
37#ifndef OPENSSL_NO_DSA
0f113f3e 38 &dsa_pkey_meth,
d4f0339c 39#endif
ef236ec3 40#ifndef OPENSSL_NO_EC
0f113f3e 41 &ec_pkey_meth,
ef236ec3 42#endif
0f113f3e 43 &hmac_pkey_meth,
b4a3aeeb 44#ifndef OPENSSL_NO_CMAC
0f113f3e 45 &cmac_pkey_meth,
b4a3aeeb 46#endif
6577e008
DSH
47#ifndef OPENSSL_NO_RSA
48 &rsa_pss_pkey_meth,
49#endif
afb14cda 50#ifndef OPENSSL_NO_DH
1eff3485 51 &dhx_pkey_meth,
cefa762e
JB
52#endif
53#ifndef OPENSSL_NO_SCRYPT
54 &scrypt_pkey_meth,
afb14cda 55#endif
aacfb134 56 &tls1_prf_pkey_meth,
262bd85f
DSH
57#ifndef OPENSSL_NO_EC
58 &ecx25519_pkey_meth,
13735cfe 59 &ecx448_pkey_meth,
262bd85f 60#endif
52ad5b60
TS
61 &hkdf_pkey_meth,
62#ifndef OPENSSL_NO_POLY1305
63 &poly1305_pkey_meth,
64#endif
3f5616d7
TS
65#ifndef OPENSSL_NO_SIPHASH
66 &siphash_pkey_meth,
67#endif
42a3008a
DSH
68#ifndef OPENSSL_NO_EC
69 &ed25519_pkey_meth,
13735cfe 70 &ed448_pkey_meth,
42a3008a 71#endif
ddb634fe
JL
72#ifndef OPENSSL_NO_SM2
73 &sm2_pkey_meth,
74#endif
0f113f3e 75};
0b6f3c66 76
606f6c47 77DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
0f113f3e 78 pmeth);
babb3798 79
0f113f3e
MC
80static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
81 const EVP_PKEY_METHOD *const *b)
82{
83 return ((*a)->pkey_id - (*b)->pkey_id);
84}
0b6f3c66 85
606f6c47 86IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
0f113f3e 87 pmeth);
babb3798 88
c9777d26 89const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
0f113f3e
MC
90{
91 EVP_PKEY_METHOD tmp;
92 const EVP_PKEY_METHOD *t = &tmp, **ret;
93 tmp.pkey_id = type;
94 if (app_pkey_methods) {
95 int idx;
96 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
97 if (idx >= 0)
98 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
99 }
100 ret = OBJ_bsearch_pmeth(&t, standard_methods,
101 sizeof(standard_methods) /
102 sizeof(EVP_PKEY_METHOD *));
103 if (!ret || !*ret)
104 return NULL;
105 return *ret;
106}
0b6f3c66 107
f5cda4cb 108static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
0f113f3e
MC
109{
110 EVP_PKEY_CTX *ret;
d0ea49a8
RL
111 const EVP_PKEY_METHOD *pmeth = NULL;
112
113 /*
114 * When using providers, the context is bound to the algo implementation
115 * later.
116 */
117 if (pkey == NULL && e == NULL && id == -1)
118 goto common;
2f2e6b62 119
d0ea49a8
RL
120 /* TODO(3.0) Legacy code should be removed when all is provider based */
121 /* BEGIN legacy */
0f113f3e 122 if (id == -1) {
a6465b3f
P
123 if (pkey == NULL)
124 return 0;
2f2e6b62 125 id = pkey->type;
0f113f3e 126 }
a63bf2c5 127#ifndef OPENSSL_NO_ENGINE
c2976edf 128 if (e == NULL && pkey != NULL)
d19b01ad 129 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
0f113f3e
MC
130 /* Try to find an ENGINE which implements this method */
131 if (e) {
132 if (!ENGINE_init(e)) {
133 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
134 return NULL;
135 }
c2976edf 136 } else {
0f113f3e 137 e = ENGINE_get_pkey_meth_engine(id);
c2976edf 138 }
0f113f3e
MC
139
140 /*
0d4fb843 141 * If an ENGINE handled this method look it up. Otherwise use internal
0f113f3e
MC
142 * tables.
143 */
0f113f3e
MC
144 if (e)
145 pmeth = ENGINE_get_pkey_meth(e, id);
146 else
a63bf2c5 147#endif
0f113f3e 148 pmeth = EVP_PKEY_meth_find(id);
c9777d26 149
0f113f3e 150 if (pmeth == NULL) {
918a27fa
DSH
151#ifndef OPENSSL_NO_ENGINE
152 ENGINE_finish(e);
153#endif
0f113f3e
MC
154 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
155 return NULL;
156 }
d0ea49a8 157 /* END legacy */
c9777d26 158
d0ea49a8 159 common:
64b25758 160 ret = OPENSSL_zalloc(sizeof(*ret));
90945fa3 161 if (ret == NULL) {
a63bf2c5 162#ifndef OPENSSL_NO_ENGINE
7c96dbcd 163 ENGINE_finish(e);
a63bf2c5 164#endif
0f113f3e
MC
165 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
166 return NULL;
167 }
168 ret->engine = e;
169 ret->pmeth = pmeth;
170 ret->operation = EVP_PKEY_OP_UNDEFINED;
171 ret->pkey = pkey;
a6465b3f 172 if (pkey != NULL)
03273d61 173 EVP_PKEY_up_ref(pkey);
0f113f3e 174
8b84b075 175 if (pmeth != NULL && pmeth->init != NULL) {
0f113f3e 176 if (pmeth->init(ret) <= 0) {
83b4049a 177 ret->pmeth = NULL;
0f113f3e
MC
178 EVP_PKEY_CTX_free(ret);
179 return NULL;
180 }
181 }
182
183 return ret;
184}
185
186EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
187{
188 EVP_PKEY_METHOD *pmeth;
b4faea50 189
b51bce94 190 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
3484236d
F
191 if (pmeth == NULL) {
192 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
0f113f3e 193 return NULL;
3484236d 194 }
0f113f3e 195
0f113f3e
MC
196 pmeth->pkey_id = id;
197 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
0f113f3e
MC
198 return pmeth;
199}
ba30bad5 200
f830c68f 201void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
0f113f3e
MC
202 const EVP_PKEY_METHOD *meth)
203{
204 if (ppkey_id)
205 *ppkey_id = meth->pkey_id;
206 if (pflags)
207 *pflags = meth->flags;
208}
f830c68f
DSH
209
210void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
0f113f3e 211{
f830c68f 212
0f113f3e
MC
213 dst->init = src->init;
214 dst->copy = src->copy;
215 dst->cleanup = src->cleanup;
f830c68f 216
0f113f3e
MC
217 dst->paramgen_init = src->paramgen_init;
218 dst->paramgen = src->paramgen;
f830c68f 219
0f113f3e
MC
220 dst->keygen_init = src->keygen_init;
221 dst->keygen = src->keygen;
f830c68f 222
0f113f3e
MC
223 dst->sign_init = src->sign_init;
224 dst->sign = src->sign;
f830c68f 225
0f113f3e
MC
226 dst->verify_init = src->verify_init;
227 dst->verify = src->verify;
f830c68f 228
0f113f3e
MC
229 dst->verify_recover_init = src->verify_recover_init;
230 dst->verify_recover = src->verify_recover;
f830c68f 231
0f113f3e
MC
232 dst->signctx_init = src->signctx_init;
233 dst->signctx = src->signctx;
f830c68f 234
0f113f3e
MC
235 dst->verifyctx_init = src->verifyctx_init;
236 dst->verifyctx = src->verifyctx;
f830c68f 237
0f113f3e
MC
238 dst->encrypt_init = src->encrypt_init;
239 dst->encrypt = src->encrypt;
f830c68f 240
0f113f3e
MC
241 dst->decrypt_init = src->decrypt_init;
242 dst->decrypt = src->decrypt;
f830c68f 243
0f113f3e
MC
244 dst->derive_init = src->derive_init;
245 dst->derive = src->derive;
f830c68f 246
0f113f3e
MC
247 dst->ctrl = src->ctrl;
248 dst->ctrl_str = src->ctrl_str;
2aee35d3
PY
249
250 dst->check = src->check;
0f113f3e 251}
f830c68f 252
ba30bad5 253void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
254{
255 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
256 OPENSSL_free(pmeth);
257}
ba30bad5 258
f5cda4cb 259EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
0f113f3e
MC
260{
261 return int_ctx_new(pkey, e, -1);
262}
f5cda4cb
DSH
263
264EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
0f113f3e
MC
265{
266 return int_ctx_new(NULL, e, id);
267}
f5cda4cb 268
9fdcc21f 269EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
0f113f3e
MC
270{
271 EVP_PKEY_CTX *rctx;
ff64702b
MC
272
273 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
274 && pctx->exchprovctx == NULL)
0f113f3e 275 return NULL;
c9777d26 276#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
277 /* Make sure it's safe to copy a pkey context using an ENGINE */
278 if (pctx->engine && !ENGINE_init(pctx->engine)) {
279 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
280 return 0;
281 }
c9777d26 282#endif
ff64702b 283 rctx = OPENSSL_zalloc(sizeof(*rctx));
3484236d
F
284 if (rctx == NULL) {
285 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
0f113f3e 286 return NULL;
3484236d 287 }
8bdcef40 288
ff64702b
MC
289 if (pctx->pkey != NULL)
290 EVP_PKEY_up_ref(pctx->pkey);
291 rctx->pkey = pctx->pkey;
292 rctx->operation = pctx->operation;
293
294 if (pctx->exchprovctx != NULL) {
295 if (!ossl_assert(pctx->exchange != NULL))
296 return NULL;
297 rctx->exchange = pctx->exchange;
298 if (!EVP_KEYEXCH_up_ref(rctx->exchange)) {
299 OPENSSL_free(rctx);
300 return NULL;
301 }
302 rctx->exchprovctx = pctx->exchange->dupctx(pctx->exchprovctx);
303 if (rctx->exchprovctx == NULL) {
304 EVP_KEYEXCH_free(rctx->exchange);
305 OPENSSL_free(rctx);
306 return NULL;
307 }
308 return rctx;
309 }
310
0f113f3e 311 rctx->pmeth = pctx->pmeth;
c9777d26 312#ifndef OPENSSL_NO_ENGINE
0f113f3e 313 rctx->engine = pctx->engine;
c9777d26 314#endif
8bdcef40 315
0f113f3e 316 if (pctx->peerkey)
03273d61 317 EVP_PKEY_up_ref(pctx->peerkey);
0f113f3e 318 rctx->peerkey = pctx->peerkey;
8bdcef40 319
0f113f3e
MC
320 if (pctx->pmeth->copy(rctx, pctx) > 0)
321 return rctx;
8bdcef40 322
83b4049a 323 rctx->pmeth = NULL;
0f113f3e
MC
324 EVP_PKEY_CTX_free(rctx);
325 return NULL;
8bdcef40 326
0f113f3e 327}
8bdcef40 328
ba30bad5 329int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
0f113f3e
MC
330{
331 if (app_pkey_methods == NULL) {
332 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
3484236d
F
333 if (app_pkey_methods == NULL){
334 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 335 return 0;
3484236d 336 }
0f113f3e 337 }
3484236d
F
338 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
339 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
0f113f3e 340 return 0;
3484236d 341 }
0f113f3e
MC
342 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
343 return 1;
344}
ba30bad5 345
0822e89a
PY
346void evp_app_cleanup_int(void)
347{
348 if (app_pkey_methods != NULL)
349 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
350}
351
352int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
353{
354 const EVP_PKEY_METHOD *ret;
355
356 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
357
358 return ret == NULL ? 0 : 1;
359}
360
48ed9c23
DSH
361size_t EVP_PKEY_meth_get_count(void)
362{
363 size_t rv = OSSL_NELEM(standard_methods);
364
365 if (app_pkey_methods)
366 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
367 return rv;
368}
369
370const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
371{
372 if (idx < OSSL_NELEM(standard_methods))
373 return standard_methods[idx];
374 if (app_pkey_methods == NULL)
375 return NULL;
376 idx -= OSSL_NELEM(standard_methods);
377 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
378 return NULL;
379 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
380}
381
5da98aa6 382void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
0f113f3e
MC
383{
384 if (ctx == NULL)
385 return;
386 if (ctx->pmeth && ctx->pmeth->cleanup)
387 ctx->pmeth->cleanup(ctx);
ff64702b
MC
388
389 if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
390 ctx->exchange->freectx(ctx->exchprovctx);
391
392 EVP_KEYEXCH_free(ctx->exchange);
393
dfcb5d29
MC
394 if (ctx->sigprovctx != NULL && ctx->signature != NULL)
395 ctx->signature->freectx(ctx->sigprovctx);
396
397 EVP_SIGNATURE_free(ctx->signature);
398
c5ba2d99
RS
399 EVP_PKEY_free(ctx->pkey);
400 EVP_PKEY_free(ctx->peerkey);
c9777d26 401#ifndef OPENSSL_NO_ENGINE
7c96dbcd 402 ENGINE_finish(ctx->engine);
c9777d26 403#endif
0f113f3e
MC
404 OPENSSL_free(ctx);
405}
5da98aa6 406
9c45222d
MC
407int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
408{
409 if (ctx->sigprovctx != NULL
410 && ctx->signature != NULL
411 && ctx->signature->get_ctx_params != NULL)
412 return ctx->signature->get_ctx_params(ctx->sigprovctx, params);
413 return 0;
414}
415
416const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
417{
418 if (ctx->signature != NULL
419 && ctx->signature->gettable_ctx_params != NULL)
420 return ctx->signature->gettable_ctx_params();
421
422 return NULL;
423}
424
35aca9ec
MC
425int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
426{
9c45222d
MC
427 if (ctx->exchprovctx != NULL
428 && ctx->exchange != NULL
429 && ctx->exchange->set_ctx_params != NULL)
430 return ctx->exchange->set_ctx_params(ctx->exchprovctx, params);
431 if (ctx->sigprovctx != NULL
432 && ctx->signature != NULL
433 && ctx->signature->set_ctx_params != NULL)
434 return ctx->signature->set_ctx_params(ctx->sigprovctx, params);
35aca9ec
MC
435 return 0;
436}
437
9c45222d
MC
438const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
439{
440 if (ctx->exchange != NULL
441 && ctx->exchange->settable_ctx_params != NULL)
442 return ctx->exchange->settable_ctx_params();
443 if (ctx->signature != NULL
444 && ctx->signature->settable_ctx_params != NULL)
445 return ctx->signature->settable_ctx_params();
446
447 return NULL;
448}
449
76ca35e7 450#ifndef OPENSSL_NO_DH
35aca9ec
MC
451int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
452{
453 OSSL_PARAM dh_pad_params[2];
1c3ace68 454 unsigned int upad = pad;
35aca9ec
MC
455
456 /* TODO(3.0): Remove this eventually when no more legacy */
457 if (ctx->exchprovctx == NULL)
458 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
459 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
460
1c3ace68 461 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
35aca9ec
MC
462 dh_pad_params[1] = OSSL_PARAM_construct_end();
463
464 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
465}
76ca35e7 466#endif
35aca9ec 467
9c45222d
MC
468int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
469{
470 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
471 /* 80 should be big enough */
472 char name[80] = "";
473 const EVP_MD *tmp;
474
475 if (ctx == NULL) {
476 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
477 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
478 return -2;
479 }
480
481 /* TODO(3.0): Remove this eventually when no more legacy */
482 if (ctx->sigprovctx == NULL)
483 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
484 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
485
486 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
487 name,
488 sizeof(name));
489 *p++ = OSSL_PARAM_construct_end();
490
491 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
492 return 0;
493
494 tmp = EVP_get_digestbyname(name);
495 if (tmp == NULL)
496 return 0;
497
498 *md = tmp;
499
500 return 1;
501}
502
4889dadc
MC
503int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
504{
9c45222d 505 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
4889dadc
MC
506 size_t mdsize;
507 const char *name;
508
9c45222d
MC
509 if (ctx == NULL) {
510 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
511 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
512 return -2;
513 }
514
4889dadc
MC
515 /* TODO(3.0): Remove this eventually when no more legacy */
516 if (ctx->sigprovctx == NULL)
517 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
518 EVP_PKEY_CTRL_MD, 0, (void *)(md));
519
9c45222d
MC
520 if (md == NULL) {
521 name = "";
522 mdsize = 0;
523 } else {
524 mdsize = EVP_MD_size(md);
525 name = EVP_MD_name(md);
526 }
4889dadc 527
9c45222d
MC
528 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
529 /*
530 * Cast away the const. This is read
531 * only so should be safe
532 */
533 (char *)name,
534 strlen(name) + 1);
535 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
536 &mdsize);
537 *p++ = OSSL_PARAM_construct_end();
4889dadc 538
9c45222d 539 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
4889dadc
MC
540}
541
35aca9ec
MC
542static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
543 int cmd, int p1, void *p2)
544{
545 switch (cmd) {
76ca35e7 546#ifndef OPENSSL_NO_DH
35aca9ec
MC
547 case EVP_PKEY_CTRL_DH_PAD:
548 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
76ca35e7 549#endif
390acbeb
MC
550 case EVP_PKEY_CTRL_MD:
551 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
35aca9ec
MC
552 }
553 return 0;
554}
555
0b6f3c66 556int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
0f113f3e
MC
557 int cmd, int p1, void *p2)
558{
559 int ret;
4803717f 560
35aca9ec
MC
561 if (ctx == NULL) {
562 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
563 return -2;
564 }
565
390acbeb 566 if (ctx->exchprovctx != NULL || ctx->sigprovctx != NULL)
35aca9ec
MC
567 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
568
569 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
0f113f3e
MC
570 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
571 return -2;
572 }
573 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
574 return -1;
575
4803717f
PY
576 /* Skip the operation checks since this is called in a very early stage */
577 if (ctx->pmeth->digest_custom != NULL)
578 goto doit;
579
0f113f3e
MC
580 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
581 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
582 return -1;
583 }
584
585 if ((optype != -1) && !(ctx->operation & optype)) {
586 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
587 return -1;
588 }
589
4803717f 590 doit:
0f113f3e
MC
591 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
592
593 if (ret == -2)
594 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
595
596 return ret;
0f113f3e 597}
0b6f3c66 598
cefa762e 599int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
64bf1016 600 int cmd, uint64_t value)
cefa762e
JB
601{
602 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
603}
604
35aca9ec
MC
605static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
606 const char *value)
607{
76ca35e7 608#ifndef OPENSSL_NO_DH
35aca9ec
MC
609 if (strcmp(name, "dh_pad") == 0) {
610 int pad;
611
612 pad = atoi(value);
613 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
614 }
76ca35e7 615#endif
390acbeb
MC
616 if (strcmp(name, "digest") == 0) {
617 int ret;
618 EVP_MD *md
619 = EVP_MD_fetch(ossl_provider_library_context(ctx->signature->prov),
620 value, NULL);
621 if (md == NULL)
622 return 0;
623 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
624 EVP_MD_meth_free(md);
625 return ret;
626 }
627
35aca9ec
MC
628 return 0;
629}
630
4a3dc3c0 631int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
0f113f3e
MC
632 const char *name, const char *value)
633{
35aca9ec
MC
634 if (ctx == NULL) {
635 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
636 return -2;
637 }
638
390acbeb 639 if (ctx->exchprovctx != NULL || ctx->sigprovctx != NULL)
35aca9ec
MC
640 return legacy_ctrl_str_to_param(ctx, name, value);
641
0f113f3e
MC
642 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
643 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
644 return -2;
645 }
410877ba
DSH
646 if (strcmp(name, "digest") == 0)
647 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
648 value);
0f113f3e
MC
649 return ctx->pmeth->ctrl_str(ctx, name, value);
650}
f5cda4cb 651
99119000
DSH
652/* Utility functions to send a string of hex string to a ctrl */
653
654int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
655{
656 size_t len;
657
658 len = strlen(str);
659 if (len > INT_MAX)
660 return -1;
661 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
662}
663
664int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
665{
666 unsigned char *bin;
667 long binlen;
668 int rv = -1;
669
14f051a0 670 bin = OPENSSL_hexstr2buf(hex, &binlen);
99119000
DSH
671 if (bin == NULL)
672 return 0;
673 if (binlen <= INT_MAX)
674 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
675 OPENSSL_free(bin);
676 return rv;
677}
52ad523c 678
410877ba
DSH
679/* Pass a message digest to a ctrl */
680int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
681{
682 const EVP_MD *m;
c82bafc5 683
410877ba
DSH
684 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
685 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
686 return 0;
687 }
688 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
689}
99119000 690
b28dea4e 691int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
0f113f3e
MC
692{
693 return ctx->operation;
694}
b28dea4e
DSH
695
696void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
0f113f3e
MC
697{
698 ctx->keygen_info = dat;
699 ctx->keygen_info_count = datlen;
700}
b28dea4e 701
f5cda4cb 702void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
703{
704 ctx->data = data;
705}
f5cda4cb 706
9fdcc21f 707void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
0f113f3e
MC
708{
709 return ctx->data;
710}
f5cda4cb 711
81cebb8b 712EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
713{
714 return ctx->pkey;
715}
81cebb8b 716
0e1dba93 717EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
0f113f3e
MC
718{
719 return ctx->peerkey;
720}
721
f5cda4cb 722void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
0f113f3e
MC
723{
724 ctx->app_data = data;
725}
f5cda4cb
DSH
726
727void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
0f113f3e
MC
728{
729 return ctx->app_data;
730}
ba30bad5
DSH
731
732void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
733 int (*init) (EVP_PKEY_CTX *ctx))
734{
735 pmeth->init = init;
736}
8bdcef40
DSH
737
738void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
0f113f3e 739 int (*copy) (EVP_PKEY_CTX *dst,
9fdcc21f 740 const EVP_PKEY_CTX *src))
0f113f3e
MC
741{
742 pmeth->copy = copy;
743}
ba30bad5
DSH
744
745void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
746 void (*cleanup) (EVP_PKEY_CTX *ctx))
747{
748 pmeth->cleanup = cleanup;
749}
ba30bad5
DSH
750
751void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
752 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
753 int (*paramgen) (EVP_PKEY_CTX *ctx,
754 EVP_PKEY *pkey))
755{
756 pmeth->paramgen_init = paramgen_init;
757 pmeth->paramgen = paramgen;
758}
ba30bad5
DSH
759
760void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
761 int (*keygen_init) (EVP_PKEY_CTX *ctx),
762 int (*keygen) (EVP_PKEY_CTX *ctx,
763 EVP_PKEY *pkey))
764{
765 pmeth->keygen_init = keygen_init;
766 pmeth->keygen = keygen;
767}
ba30bad5
DSH
768
769void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
770 int (*sign_init) (EVP_PKEY_CTX *ctx),
771 int (*sign) (EVP_PKEY_CTX *ctx,
772 unsigned char *sig, size_t *siglen,
773 const unsigned char *tbs,
774 size_t tbslen))
775{
776 pmeth->sign_init = sign_init;
777 pmeth->sign = sign;
778}
ba30bad5
DSH
779
780void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
781 int (*verify_init) (EVP_PKEY_CTX *ctx),
782 int (*verify) (EVP_PKEY_CTX *ctx,
783 const unsigned char *sig,
784 size_t siglen,
785 const unsigned char *tbs,
786 size_t tbslen))
787{
788 pmeth->verify_init = verify_init;
789 pmeth->verify = verify;
790}
ba30bad5
DSH
791
792void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
793 int (*verify_recover_init) (EVP_PKEY_CTX
794 *ctx),
795 int (*verify_recover) (EVP_PKEY_CTX
796 *ctx,
797 unsigned char
798 *sig,
799 size_t *siglen,
800 const unsigned
801 char *tbs,
802 size_t tbslen))
803{
804 pmeth->verify_recover_init = verify_recover_init;
805 pmeth->verify_recover = verify_recover;
806}
ba30bad5
DSH
807
808void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
809 int (*signctx_init) (EVP_PKEY_CTX *ctx,
810 EVP_MD_CTX *mctx),
811 int (*signctx) (EVP_PKEY_CTX *ctx,
812 unsigned char *sig,
813 size_t *siglen,
814 EVP_MD_CTX *mctx))
815{
816 pmeth->signctx_init = signctx_init;
817 pmeth->signctx = signctx;
818}
ba30bad5
DSH
819
820void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
821 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
822 EVP_MD_CTX *mctx),
823 int (*verifyctx) (EVP_PKEY_CTX *ctx,
824 const unsigned char *sig,
825 int siglen,
826 EVP_MD_CTX *mctx))
827{
828 pmeth->verifyctx_init = verifyctx_init;
829 pmeth->verifyctx = verifyctx;
830}
ba30bad5
DSH
831
832void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
833 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
834 int (*encryptfn) (EVP_PKEY_CTX *ctx,
835 unsigned char *out,
836 size_t *outlen,
837 const unsigned char *in,
838 size_t inlen))
839{
840 pmeth->encrypt_init = encrypt_init;
841 pmeth->encrypt = encryptfn;
842}
ba30bad5
DSH
843
844void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
845 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
846 int (*decrypt) (EVP_PKEY_CTX *ctx,
847 unsigned char *out,
848 size_t *outlen,
849 const unsigned char *in,
850 size_t inlen))
851{
852 pmeth->decrypt_init = decrypt_init;
853 pmeth->decrypt = decrypt;
854}
ba30bad5
DSH
855
856void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
857 int (*derive_init) (EVP_PKEY_CTX *ctx),
858 int (*derive) (EVP_PKEY_CTX *ctx,
859 unsigned char *key,
860 size_t *keylen))
861{
862 pmeth->derive_init = derive_init;
863 pmeth->derive = derive;
864}
ba30bad5
DSH
865
866void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
0f113f3e
MC
867 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
868 void *p2),
869 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
870 const char *type,
871 const char *value))
872{
873 pmeth->ctrl = ctrl;
874 pmeth->ctrl_str = ctrl_str;
875}
e7451ed1 876
2aee35d3
PY
877void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
878 int (*check) (EVP_PKEY *pkey))
879{
880 pmeth->check = check;
881}
882
b0004708
PY
883void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
884 int (*check) (EVP_PKEY *pkey))
885{
886 pmeth->public_check = check;
887}
888
889void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
890 int (*check) (EVP_PKEY *pkey))
891{
892 pmeth->param_check = check;
893}
894
0a8fdef7
PY
895void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
896 int (*digest_custom) (EVP_PKEY_CTX *ctx,
897 EVP_MD_CTX *mctx))
898{
899 pmeth->digest_custom = digest_custom;
900}
901
693be9a2 902void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
903 int (**pinit) (EVP_PKEY_CTX *ctx))
904{
905 *pinit = pmeth->init;
906}
907
693be9a2 908void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
e7451ed1 909 int (**pcopy) (EVP_PKEY_CTX *dst,
9fdcc21f 910 const EVP_PKEY_CTX *src))
e7451ed1
DSH
911{
912 *pcopy = pmeth->copy;
913}
914
693be9a2 915void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
916 void (**pcleanup) (EVP_PKEY_CTX *ctx))
917{
918 *pcleanup = pmeth->cleanup;
919}
920
693be9a2 921void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
922 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
923 int (**pparamgen) (EVP_PKEY_CTX *ctx,
924 EVP_PKEY *pkey))
925{
926 if (pparamgen_init)
927 *pparamgen_init = pmeth->paramgen_init;
928 if (pparamgen)
929 *pparamgen = pmeth->paramgen;
930}
931
693be9a2 932void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
933 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
934 int (**pkeygen) (EVP_PKEY_CTX *ctx,
935 EVP_PKEY *pkey))
936{
937 if (pkeygen_init)
938 *pkeygen_init = pmeth->keygen_init;
939 if (pkeygen)
940 *pkeygen = pmeth->keygen;
941}
942
693be9a2 943void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
944 int (**psign_init) (EVP_PKEY_CTX *ctx),
945 int (**psign) (EVP_PKEY_CTX *ctx,
946 unsigned char *sig, size_t *siglen,
947 const unsigned char *tbs,
948 size_t tbslen))
949{
950 if (psign_init)
951 *psign_init = pmeth->sign_init;
952 if (psign)
953 *psign = pmeth->sign;
954}
955
693be9a2 956void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
957 int (**pverify_init) (EVP_PKEY_CTX *ctx),
958 int (**pverify) (EVP_PKEY_CTX *ctx,
959 const unsigned char *sig,
960 size_t siglen,
961 const unsigned char *tbs,
962 size_t tbslen))
963{
964 if (pverify_init)
965 *pverify_init = pmeth->verify_init;
966 if (pverify)
967 *pverify = pmeth->verify;
968}
969
693be9a2 970void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
971 int (**pverify_recover_init) (EVP_PKEY_CTX
972 *ctx),
973 int (**pverify_recover) (EVP_PKEY_CTX
974 *ctx,
975 unsigned char
976 *sig,
977 size_t *siglen,
978 const unsigned
979 char *tbs,
980 size_t tbslen))
981{
982 if (pverify_recover_init)
983 *pverify_recover_init = pmeth->verify_recover_init;
984 if (pverify_recover)
985 *pverify_recover = pmeth->verify_recover;
986}
987
693be9a2 988void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
989 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
990 EVP_MD_CTX *mctx),
991 int (**psignctx) (EVP_PKEY_CTX *ctx,
992 unsigned char *sig,
993 size_t *siglen,
994 EVP_MD_CTX *mctx))
995{
996 if (psignctx_init)
997 *psignctx_init = pmeth->signctx_init;
998 if (psignctx)
999 *psignctx = pmeth->signctx;
1000}
1001
693be9a2 1002void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1003 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1004 EVP_MD_CTX *mctx),
1005 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1006 const unsigned char *sig,
1007 int siglen,
1008 EVP_MD_CTX *mctx))
1009{
1010 if (pverifyctx_init)
1011 *pverifyctx_init = pmeth->verifyctx_init;
1012 if (pverifyctx)
1013 *pverifyctx = pmeth->verifyctx;
1014}
1015
693be9a2 1016void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1017 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1018 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1019 unsigned char *out,
1020 size_t *outlen,
1021 const unsigned char *in,
1022 size_t inlen))
1023{
1024 if (pencrypt_init)
1025 *pencrypt_init = pmeth->encrypt_init;
1026 if (pencryptfn)
1027 *pencryptfn = pmeth->encrypt;
1028}
1029
693be9a2 1030void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1031 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1032 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1033 unsigned char *out,
1034 size_t *outlen,
1035 const unsigned char *in,
1036 size_t inlen))
1037{
1038 if (pdecrypt_init)
1039 *pdecrypt_init = pmeth->decrypt_init;
1040 if (pdecrypt)
1041 *pdecrypt = pmeth->decrypt;
1042}
1043
693be9a2 1044void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1045 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1046 int (**pderive) (EVP_PKEY_CTX *ctx,
1047 unsigned char *key,
1048 size_t *keylen))
1049{
1050 if (pderive_init)
1051 *pderive_init = pmeth->derive_init;
1052 if (pderive)
1053 *pderive = pmeth->derive;
1054}
1055
693be9a2 1056void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
e7451ed1
DSH
1057 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1058 void *p2),
1059 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1060 const char *type,
1061 const char *value))
1062{
1063 if (pctrl)
1064 *pctrl = pmeth->ctrl;
1065 if (pctrl_str)
1066 *pctrl_str = pmeth->ctrl_str;
1067}
2aee35d3 1068
693be9a2 1069void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2aee35d3
PY
1070 int (**pcheck) (EVP_PKEY *pkey))
1071{
34f5c8b1 1072 if (pcheck != NULL)
2aee35d3
PY
1073 *pcheck = pmeth->check;
1074}
b0004708 1075
693be9a2 1076void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1077 int (**pcheck) (EVP_PKEY *pkey))
1078{
34f5c8b1 1079 if (pcheck != NULL)
b0004708
PY
1080 *pcheck = pmeth->public_check;
1081}
1082
693be9a2 1083void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
b0004708
PY
1084 int (**pcheck) (EVP_PKEY *pkey))
1085{
34f5c8b1 1086 if (pcheck != NULL)
b0004708
PY
1087 *pcheck = pmeth->param_check;
1088}
0a8fdef7
PY
1089
1090void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1091 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1092 EVP_MD_CTX *mctx))
1093{
675f4cee 1094 if (pdigest_custom != NULL)
0a8fdef7
PY
1095 *pdigest_custom = pmeth->digest_custom;
1096}