2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
19 # include <openssl/engine.h>
21 #include <openssl/evp.h>
22 #include <openssl/core_names.h>
23 #include <openssl/dh.h>
24 #include <openssl/rsa.h>
25 #include <openssl/kdf.h>
26 #include "internal/cryptlib.h"
28 # include "crypto/asn1.h"
30 #include "crypto/evp.h"
31 #include "crypto/dh.h"
32 #include "crypto/ec.h"
33 #include "internal/ffc.h"
34 #include "internal/numbers.h"
35 #include "internal/provider.h"
36 #include "evp_local.h"
40 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
41 int keytype, int optype,
42 int cmd, const char *name,
43 const void *data, size_t data_len);
44 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
45 int cmd, const char *name);
46 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
48 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
49 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
51 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
53 /* This array needs to be in order of NIDs */
54 static pmeth_fn standard_methods[] = {
56 # ifndef OPENSSL_NO_DH
59 # ifndef OPENSSL_NO_DSA
62 # ifndef OPENSSL_NO_EC
65 ossl_rsa_pss_pkey_method,
66 # ifndef OPENSSL_NO_DH
69 # ifndef OPENSSL_NO_EC
70 ossl_ecx25519_pkey_method,
71 ossl_ecx448_pkey_method,
73 # ifndef OPENSSL_NO_EC
74 ossl_ed25519_pkey_method,
75 ossl_ed448_pkey_method,
79 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
81 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
83 return ((*a)->pkey_id - ((**b)())->pkey_id);
86 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
88 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
89 const EVP_PKEY_METHOD *const *b)
91 return ((*a)->pkey_id - (*b)->pkey_id);
94 static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
96 if (app_pkey_methods != NULL) {
101 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
103 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
108 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
112 const EVP_PKEY_METHOD *t;
114 if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
119 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
120 OSSL_NELEM(standard_methods));
121 if (ret == NULL || *ret == NULL)
126 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
128 EVP_PKEY_METHOD *pmeth;
130 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
135 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
139 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
144 if (*type == NID_undef)
145 *type = evp_pkey_name2type(keytype);
148 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
150 int type = NID_undef;
152 EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
156 #endif /* FIPS_MODULE */
158 int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
160 if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
161 return EVP_PKEY_STATE_UNKNOWN;
163 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
164 && ctx->op.kex.algctx != NULL)
165 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
166 && ctx->op.sig.algctx != NULL)
167 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
168 && ctx->op.ciph.algctx != NULL)
169 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
170 && ctx->op.keymgmt.genctx != NULL)
171 || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
172 && ctx->op.encap.algctx != NULL))
173 return EVP_PKEY_STATE_PROVIDER;
175 return EVP_PKEY_STATE_LEGACY;
178 static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
179 EVP_PKEY *pkey, ENGINE *e,
180 const char *keytype, const char *propquery,
184 EVP_PKEY_CTX *ret = NULL;
185 const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
186 EVP_KEYMGMT *keymgmt = NULL;
188 /* Code below to be removed when legacy support is dropped. */
191 if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
195 /* Must be provided if we get here */
196 keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
199 if (keytype != NULL) {
200 id = evp_pkey_name2type(keytype);
207 /* If no ID was found here, we can only resort to find a keymgmt */
210 /* Using engine with a key without id will not work */
212 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
221 * Here, we extract what information we can for the purpose of
222 * supporting usage with implementations from providers, to make
223 * for a smooth transition from legacy stuff to provider based stuff.
225 * If an engine is given, this is entirely legacy, and we should not
226 * pretend anything else, so we clear the name.
230 if (e == NULL && (pkey == NULL || pkey->foreign == 0))
231 keytype = OBJ_nid2sn(id);
233 # ifndef OPENSSL_NO_ENGINE
234 if (e == NULL && pkey != NULL)
235 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
236 /* Try to find an ENGINE which implements this method */
238 if (!ENGINE_init(e)) {
239 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
243 e = ENGINE_get_pkey_meth_engine(id);
247 * If an ENGINE handled this method look it up. Otherwise use internal
251 pmeth = ENGINE_get_pkey_meth(e, id);
252 else if (pkey != NULL && pkey->foreign)
253 pmeth = EVP_PKEY_meth_find(id);
256 app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
259 #endif /* FIPS_MODULE */
262 * If there's no engine and no app supplied pmeth and there's a name, we try
263 * fetching a provider implementation.
265 if (e == NULL && app_pmeth == NULL && keytype != NULL) {
267 * If |pkey| is given and is provided, we take a reference to its
268 * keymgmt. Otherwise, we fetch one for the keytype we got. This
269 * is to ensure that operation init functions can access what they
270 * need through this single pointer.
272 if (pkey != NULL && pkey->keymgmt != NULL) {
273 if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
274 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
276 keymgmt = pkey->keymgmt;
278 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
281 return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
285 * Chase down the legacy NID, as that might be needed for diverse
286 * purposes, such as ensure that EVP_PKEY_type() can return sensible
287 * values. We go through all keymgmt names, because the keytype
288 * that's passed to this function doesn't necessarily translate
291 if (keymgmt != NULL) {
292 int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt);
294 if (tmp_id != NID_undef) {
299 * It really really shouldn't differ. If it still does,
300 * something is very wrong.
302 if (!ossl_assert(id == tmp_id)) {
303 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
304 EVP_KEYMGMT_free(keymgmt);
313 if (pmeth == NULL && keymgmt == NULL) {
314 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
316 ret = OPENSSL_zalloc(sizeof(*ret));
319 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
320 if ((ret == NULL || pmeth == NULL) && e != NULL)
325 EVP_KEYMGMT_free(keymgmt);
328 if (propquery != NULL) {
329 ret->propquery = OPENSSL_strdup(propquery);
330 if (ret->propquery == NULL) {
332 EVP_KEYMGMT_free(keymgmt);
336 ret->libctx = libctx;
337 ret->keytype = keytype;
338 ret->keymgmt = keymgmt;
339 ret->legacy_keytype = id;
342 ret->operation = EVP_PKEY_OP_UNDEFINED;
345 EVP_PKEY_up_ref(pkey);
347 if (pmeth != NULL && pmeth->init != NULL) {
348 if (pmeth->init(ret) <= 0) {
350 EVP_PKEY_CTX_free(ret);
358 /*- All methods below can also be used in FIPS_MODULE */
360 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
362 const char *propquery)
364 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
367 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
368 const char *propquery)
370 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
373 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
375 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
376 if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
377 ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
378 EVP_SIGNATURE_free(ctx->op.sig.signature);
379 ctx->op.sig.algctx = NULL;
380 ctx->op.sig.signature = NULL;
381 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
382 if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
383 ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
384 EVP_KEYEXCH_free(ctx->op.kex.exchange);
385 ctx->op.kex.algctx = NULL;
386 ctx->op.kex.exchange = NULL;
387 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
388 if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
389 ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
390 EVP_KEM_free(ctx->op.encap.kem);
391 ctx->op.encap.algctx = NULL;
392 ctx->op.encap.kem = NULL;
394 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
395 if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
396 ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
397 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
398 ctx->op.ciph.algctx = NULL;
399 ctx->op.ciph.cipher = NULL;
400 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
401 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
402 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
406 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
410 if (ctx->pmeth && ctx->pmeth->cleanup)
411 ctx->pmeth->cleanup(ctx);
413 evp_pkey_ctx_free_old_ops(ctx);
415 evp_pkey_ctx_free_all_cached_data(ctx);
417 EVP_KEYMGMT_free(ctx->keymgmt);
419 OPENSSL_free(ctx->propquery);
420 EVP_PKEY_free(ctx->pkey);
421 EVP_PKEY_free(ctx->peerkey);
422 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
423 ENGINE_finish(ctx->engine);
425 BN_free(ctx->rsa_pubexp);
431 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
432 const EVP_PKEY_METHOD *meth)
435 *ppkey_id = meth->pkey_id;
437 *pflags = meth->flags;
440 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
442 int pkey_id = dst->pkey_id;
443 int flags = dst->flags;
447 /* We only copy the function pointers so restore the other values */
448 dst->pkey_id = pkey_id;
452 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
454 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
458 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
460 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
463 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
465 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
468 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
472 # ifndef OPENSSL_NO_ENGINE
473 /* Make sure it's safe to copy a pkey context using an ENGINE */
474 if (pctx->engine && !ENGINE_init(pctx->engine)) {
475 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
479 rctx = OPENSSL_zalloc(sizeof(*rctx));
483 if (pctx->pkey != NULL)
484 EVP_PKEY_up_ref(pctx->pkey);
485 rctx->pkey = pctx->pkey;
486 rctx->operation = pctx->operation;
487 rctx->libctx = pctx->libctx;
488 rctx->keytype = pctx->keytype;
489 rctx->propquery = NULL;
490 if (pctx->propquery != NULL) {
491 rctx->propquery = OPENSSL_strdup(pctx->propquery);
492 if (rctx->propquery == NULL)
495 rctx->legacy_keytype = pctx->legacy_keytype;
497 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
498 if (pctx->op.kex.exchange != NULL) {
499 rctx->op.kex.exchange = pctx->op.kex.exchange;
500 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
503 if (pctx->op.kex.algctx != NULL) {
504 if (!ossl_assert(pctx->op.kex.exchange != NULL))
507 = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
508 if (rctx->op.kex.algctx == NULL) {
509 EVP_KEYEXCH_free(rctx->op.kex.exchange);
510 rctx->op.kex.exchange = NULL;
515 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
516 if (pctx->op.sig.signature != NULL) {
517 rctx->op.sig.signature = pctx->op.sig.signature;
518 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
521 if (pctx->op.sig.algctx != NULL) {
522 if (!ossl_assert(pctx->op.sig.signature != NULL))
525 = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
526 if (rctx->op.sig.algctx == NULL) {
527 EVP_SIGNATURE_free(rctx->op.sig.signature);
528 rctx->op.sig.signature = NULL;
533 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
534 if (pctx->op.ciph.cipher != NULL) {
535 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
536 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
539 if (pctx->op.ciph.algctx != NULL) {
540 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
543 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
544 if (rctx->op.ciph.algctx == NULL) {
545 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
546 rctx->op.ciph.cipher = NULL;
551 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
552 if (pctx->op.encap.kem != NULL) {
553 rctx->op.encap.kem = pctx->op.encap.kem;
554 if (!EVP_KEM_up_ref(rctx->op.encap.kem))
557 if (pctx->op.encap.algctx != NULL) {
558 if (!ossl_assert(pctx->op.encap.kem != NULL))
560 rctx->op.encap.algctx
561 = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
562 if (rctx->op.encap.algctx == NULL) {
563 EVP_KEM_free(rctx->op.encap.kem);
564 rctx->op.encap.kem = NULL;
569 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
570 /* Not supported - This would need a gen_dupctx() to work */
574 rctx->pmeth = pctx->pmeth;
575 # ifndef OPENSSL_NO_ENGINE
576 rctx->engine = pctx->engine;
579 if (pctx->peerkey != NULL)
580 EVP_PKEY_up_ref(pctx->peerkey);
581 rctx->peerkey = pctx->peerkey;
583 if (pctx->pmeth == NULL) {
584 if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
585 EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
588 provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
589 &tmp_keymgmt, pctx->propquery);
592 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
594 EVP_KEYMGMT_free(rctx->keymgmt);
595 rctx->keymgmt = tmp_keymgmt;
598 } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
603 EVP_PKEY_CTX_free(rctx);
607 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
609 if (app_pkey_methods == NULL) {
610 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
611 if (app_pkey_methods == NULL) {
612 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
616 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
617 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
620 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
624 void evp_app_cleanup_int(void)
626 if (app_pkey_methods != NULL)
627 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
630 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
632 const EVP_PKEY_METHOD *ret;
634 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
636 return ret == NULL ? 0 : 1;
639 size_t EVP_PKEY_meth_get_count(void)
641 size_t rv = OSSL_NELEM(standard_methods);
643 if (app_pkey_methods)
644 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
648 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
650 if (idx < OSSL_NELEM(standard_methods))
651 return (standard_methods[idx])();
652 if (app_pkey_methods == NULL)
654 idx -= OSSL_NELEM(standard_methods);
655 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
657 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
661 int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
664 if (evp_pkey_ctx_is_legacy(ctx))
665 return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
667 return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
670 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
672 switch (evp_pkey_ctx_state(ctx)) {
673 case EVP_PKEY_STATE_PROVIDER:
674 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
675 && ctx->op.kex.exchange != NULL
676 && ctx->op.kex.exchange->set_ctx_params != NULL)
678 ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
680 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
681 && ctx->op.sig.signature != NULL
682 && ctx->op.sig.signature->set_ctx_params != NULL)
684 ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
686 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
687 && ctx->op.ciph.cipher != NULL
688 && ctx->op.ciph.cipher->set_ctx_params != NULL)
690 ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
692 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
693 && ctx->keymgmt != NULL
694 && ctx->keymgmt->gen_set_params != NULL)
696 evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
698 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
699 && ctx->op.encap.kem != NULL
700 && ctx->op.encap.kem->set_ctx_params != NULL)
702 ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
706 case EVP_PKEY_STATE_UNKNOWN:
707 case EVP_PKEY_STATE_LEGACY:
708 return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
714 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
716 switch (evp_pkey_ctx_state(ctx)) {
717 case EVP_PKEY_STATE_PROVIDER:
718 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
719 && ctx->op.kex.exchange != NULL
720 && ctx->op.kex.exchange->get_ctx_params != NULL)
722 ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
724 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
725 && ctx->op.sig.signature != NULL
726 && ctx->op.sig.signature->get_ctx_params != NULL)
728 ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
730 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
731 && ctx->op.ciph.cipher != NULL
732 && ctx->op.ciph.cipher->get_ctx_params != NULL)
734 ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
736 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
737 && ctx->op.encap.kem != NULL
738 && ctx->op.encap.kem->get_ctx_params != NULL)
740 ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
744 case EVP_PKEY_STATE_UNKNOWN:
745 case EVP_PKEY_STATE_LEGACY:
746 return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
753 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
757 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
758 && ctx->op.kex.exchange != NULL
759 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
760 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
761 return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
764 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
765 && ctx->op.sig.signature != NULL
766 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
767 provctx = ossl_provider_ctx(
768 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
769 return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
772 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
773 && ctx->op.ciph.cipher != NULL
774 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
775 provctx = ossl_provider_ctx(
776 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
777 return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
780 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
781 && ctx->op.encap.kem != NULL
782 && ctx->op.encap.kem->gettable_ctx_params != NULL) {
783 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
784 return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
790 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
794 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
795 && ctx->op.kex.exchange != NULL
796 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
797 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
798 return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
801 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
802 && ctx->op.sig.signature != NULL
803 && ctx->op.sig.signature->settable_ctx_params != NULL) {
804 provctx = ossl_provider_ctx(
805 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
806 return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
809 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
810 && ctx->op.ciph.cipher != NULL
811 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
812 provctx = ossl_provider_ctx(
813 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
814 return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
817 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
818 && ctx->keymgmt != NULL
819 && ctx->keymgmt->gen_settable_params != NULL) {
820 provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
821 return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
824 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
825 && ctx->op.encap.kem != NULL
826 && ctx->op.encap.kem->settable_ctx_params != NULL) {
827 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
828 return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
835 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
837 * Return 1 on success, 0 or negative for errors.
839 * In particular they return -2 if any of the params is not supported.
841 * They are not available in FIPS_MODULE as they depend on
842 * - EVP_PKEY_CTX_{get,set}_params()
843 * - EVP_PKEY_CTX_{gettable,settable}_params()
846 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
848 if (ctx == NULL || params == NULL)
852 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
853 * depend on the translation that happens in EVP_PKEY_CTX_set_params()
854 * call, and that the resulting ctrl call will return -2 if it doesn't
855 * known the ctrl command number.
857 if (evp_pkey_ctx_is_provided(ctx)) {
858 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
861 for (p = params; p->key != NULL; p++) {
862 /* Check the ctx actually understands this parameter */
863 if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
868 return EVP_PKEY_CTX_set_params(ctx, params);
871 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
873 if (ctx == NULL || params == NULL)
877 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
878 * depend on the translation that happens in EVP_PKEY_CTX_get_params()
879 * call, and that the resulting ctrl call will return -2 if it doesn't
880 * known the ctrl command number.
882 if (evp_pkey_ctx_is_provided(ctx)) {
883 const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
886 for (p = params; p->key != NULL; p++) {
887 /* Check the ctx actually understands this parameter */
888 if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
893 return EVP_PKEY_CTX_get_params(ctx, params);
896 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
898 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
899 /* 80 should be big enough */
903 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
904 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
905 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
909 if (ctx->op.sig.algctx == NULL)
910 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
911 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
913 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
916 *p = OSSL_PARAM_construct_end();
918 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
921 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
930 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
931 int fallback, const char *param, int op,
934 OSSL_PARAM md_params[2], *p = md_params;
937 if (ctx == NULL || (ctx->operation & op) == 0) {
938 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
939 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
944 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
949 name = EVP_MD_get0_name(md);
952 *p++ = OSSL_PARAM_construct_utf8_string(param,
954 * Cast away the const. This is read
955 * only so should be safe
958 *p = OSSL_PARAM_construct_end();
960 return EVP_PKEY_CTX_set_params(ctx, md_params);
963 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
965 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
966 OSSL_SIGNATURE_PARAM_DIGEST,
967 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
970 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
972 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
973 OSSL_KDF_PARAM_DIGEST,
974 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
977 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
978 const char *param, int op, int ctrl,
979 const unsigned char *data,
982 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
984 if (ctx == NULL || (ctx->operation & op) == 0) {
985 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
986 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
990 /* Code below to be removed when legacy support is dropped. */
992 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
993 /* end of legacy support */
996 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1000 *p++ = OSSL_PARAM_construct_octet_string(param,
1002 * Cast away the const. This is read
1003 * only so should be safe
1005 (unsigned char *)data,
1007 *p = OSSL_PARAM_construct_end();
1009 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
1012 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1013 const unsigned char *sec, int seclen)
1015 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1016 OSSL_KDF_PARAM_SECRET,
1018 EVP_PKEY_CTRL_TLS_SECRET,
1022 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1023 const unsigned char *seed, int seedlen)
1025 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1026 OSSL_KDF_PARAM_SEED,
1028 EVP_PKEY_CTRL_TLS_SEED,
1032 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1034 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1035 OSSL_KDF_PARAM_DIGEST,
1036 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1039 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1040 const unsigned char *salt, int saltlen)
1042 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1043 OSSL_KDF_PARAM_SALT,
1045 EVP_PKEY_CTRL_HKDF_SALT,
1049 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1050 const unsigned char *key, int keylen)
1052 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1055 EVP_PKEY_CTRL_HKDF_KEY,
1059 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1060 const unsigned char *info, int infolen)
1062 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1063 OSSL_KDF_PARAM_INFO,
1065 EVP_PKEY_CTRL_HKDF_INFO,
1069 int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1071 OSSL_PARAM int_params[2], *p = int_params;
1073 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1074 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1075 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1079 /* Code below to be removed when legacy support is dropped. */
1080 if (ctx->op.kex.algctx == NULL)
1081 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1082 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1083 /* end of legacy support */
1086 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1090 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1091 *p = OSSL_PARAM_construct_end();
1093 return EVP_PKEY_CTX_set_params(ctx, int_params);
1096 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1099 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1100 OSSL_KDF_PARAM_PASSWORD,
1103 (const unsigned char *)pass, passlen);
1106 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1107 const unsigned char *salt, int saltlen)
1109 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1110 OSSL_KDF_PARAM_SALT,
1112 EVP_PKEY_CTRL_SCRYPT_SALT,
1116 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1117 int op, int ctrl, uint64_t val)
1119 OSSL_PARAM uint64_params[2], *p = uint64_params;
1121 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1122 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1123 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1127 /* Code below to be removed when legacy support is dropped. */
1128 if (ctx->op.kex.algctx == NULL)
1129 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1130 /* end of legacy support */
1132 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1133 *p = OSSL_PARAM_construct_end();
1135 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1138 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1140 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1141 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1145 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1147 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1148 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1152 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1154 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1155 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1159 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1160 uint64_t maxmem_bytes)
1162 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1164 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1168 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1171 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1172 OSSL_PKEY_PARAM_PRIV_KEY,
1174 EVP_PKEY_CTRL_SET_MAC_KEY,
1178 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1180 OSSL_PARAM params[2], *p = params;
1182 if (ctx == NULL || op == NULL) {
1183 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1186 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1187 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1190 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1192 *p = OSSL_PARAM_construct_end();
1193 return EVP_PKEY_CTX_set_params(ctx, params);
1196 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
1198 OSSL_PARAM params[2], *p = params;
1201 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1202 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1203 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1207 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
1209 * Cast away the const. This is
1210 * read only so should be safe
1212 (void *)id, (size_t)len);
1213 *p++ = OSSL_PARAM_construct_end();
1215 ret = evp_pkey_ctx_set_params_strict(ctx, params);
1217 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1221 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1223 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1224 EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1227 static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
1230 void *tmp_id = NULL;
1231 OSSL_PARAM params[2], *p = params;
1233 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1234 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1235 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1239 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
1241 *p++ = OSSL_PARAM_construct_end();
1243 ret = evp_pkey_ctx_get_params_strict(ctx, params);
1245 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1246 } else if (ret > 0) {
1247 size_t tmp_id_len = params[0].return_size;
1250 memcpy(id, tmp_id, tmp_id_len);
1252 *id_len = tmp_id_len;
1257 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
1259 return get1_id_data(ctx, id, NULL);
1262 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
1264 return get1_id_data(ctx, NULL, id_len);
1267 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1269 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1272 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1274 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1275 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1278 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1279 int cmd, int p1, void *p2)
1284 * If the method has a |digest_custom| function, we can relax the
1285 * operation type check, since this can be called before the operation
1288 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1289 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1290 ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1294 if ((optype != -1) && !(ctx->operation & optype)) {
1295 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1300 switch (evp_pkey_ctx_state(ctx)) {
1301 case EVP_PKEY_STATE_PROVIDER:
1302 return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1303 case EVP_PKEY_STATE_UNKNOWN:
1304 case EVP_PKEY_STATE_LEGACY:
1305 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1306 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1309 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1312 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1315 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1321 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1322 int cmd, int p1, void *p2)
1327 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1330 /* If unsupported, we don't want that reported here */
1332 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1337 ERR_clear_last_mark();
1339 * If there was an error, there was an error.
1340 * If the operation isn't initialized yet, we also return, as
1341 * the saved values will be used then anyway.
1343 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1346 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1349 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1350 int cmd, uint64_t value)
1352 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1356 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1357 const char *name, const char *value)
1362 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1366 switch (evp_pkey_ctx_state(ctx)) {
1367 case EVP_PKEY_STATE_PROVIDER:
1368 return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1369 case EVP_PKEY_STATE_UNKNOWN:
1370 case EVP_PKEY_STATE_LEGACY:
1371 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1372 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1375 if (strcmp(name, "digest") == 0)
1376 ret = EVP_PKEY_CTX_md(ctx,
1377 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1378 EVP_PKEY_CTRL_MD, value);
1380 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1387 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1388 const char *name, const char *value)
1392 /* If unsupported, we don't want that reported here */
1394 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1395 name, value, strlen(value) + 1);
1399 ERR_clear_last_mark();
1401 * If there was an error, there was an error.
1402 * If the operation isn't initialized yet, we also return, as
1403 * the saved values will be used then anyway.
1405 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1409 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1412 static int decode_cmd(int cmd, const char *name)
1416 * The consequence of the assertion not being true is that this
1417 * function will return -1, which will cause the calling functions
1418 * to signal that the command is unsupported... in non-debug mode.
1420 if (ossl_assert(name != NULL))
1421 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1422 cmd = EVP_PKEY_CTRL_SET1_ID;
1428 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1429 int keytype, int optype,
1430 int cmd, const char *name,
1431 const void *data, size_t data_len)
1434 * Check that it's one of the supported commands. The ctrl commands
1435 * number cases here must correspond to the cases in the bottom switch
1438 switch (cmd = decode_cmd(cmd, name)) {
1439 case EVP_PKEY_CTRL_SET1_ID:
1442 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1446 if (keytype != -1) {
1447 switch (evp_pkey_ctx_state(ctx)) {
1448 case EVP_PKEY_STATE_PROVIDER:
1449 if (ctx->keymgmt == NULL) {
1450 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1453 if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1454 evp_pkey_type2name(keytype))) {
1455 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1459 case EVP_PKEY_STATE_UNKNOWN:
1460 case EVP_PKEY_STATE_LEGACY:
1461 if (ctx->pmeth == NULL) {
1462 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1465 if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1466 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1472 if (optype != -1 && (ctx->operation & optype) == 0) {
1473 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1478 case EVP_PKEY_CTRL_SET1_ID:
1479 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1481 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1482 if (ctx->cached_parameters.dist_id_name == NULL)
1486 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1487 if (ctx->cached_parameters.dist_id == NULL)
1490 ctx->cached_parameters.dist_id_set = 1;
1491 ctx->cached_parameters.dist_id_len = data_len;
1497 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1498 int cmd, const char *name)
1500 cmd = decode_cmd(cmd, name);
1502 case EVP_PKEY_CTRL_SET1_ID:
1503 OPENSSL_free(ctx->cached_parameters.dist_id);
1504 OPENSSL_free(ctx->cached_parameters.dist_id_name);
1505 ctx->cached_parameters.dist_id = NULL;
1506 ctx->cached_parameters.dist_id_name = NULL;
1511 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1513 evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1516 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1520 if (ret && ctx->cached_parameters.dist_id_set) {
1521 const char *name = ctx->cached_parameters.dist_id_name;
1522 const void *val = ctx->cached_parameters.dist_id;
1523 size_t len = ctx->cached_parameters.dist_id_len;
1526 ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1528 ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1529 EVP_PKEY_CTRL_SET1_ID,
1530 (int)len, (void *)val);
1536 OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1541 const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1543 return ctx->propquery;
1546 const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1548 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1549 if (ctx->op.sig.signature != NULL)
1550 return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1551 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1552 if (ctx->op.kex.exchange != NULL)
1553 return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1554 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1555 if (ctx->op.encap.kem != NULL)
1556 return EVP_KEM_get0_provider(ctx->op.encap.kem);
1557 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1558 if (ctx->op.ciph.cipher != NULL)
1559 return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1560 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1561 if (ctx->keymgmt != NULL)
1562 return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1568 /* Utility functions to send a string of hex string to a ctrl */
1570 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1577 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1580 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1586 bin = OPENSSL_hexstr2buf(hex, &binlen);
1589 if (binlen <= INT_MAX)
1590 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1595 /* Pass a message digest to a ctrl */
1596 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1600 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1601 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1604 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1607 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1609 return ctx->operation;
1612 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1614 ctx->keygen_info = dat;
1615 ctx->keygen_info_count = datlen;
1618 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1623 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1628 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1633 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1635 return ctx->peerkey;
1638 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1640 ctx->app_data = data;
1643 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1645 return ctx->app_data;
1648 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1649 int (*init) (EVP_PKEY_CTX *ctx))
1654 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1655 int (*copy) (EVP_PKEY_CTX *dst,
1656 const EVP_PKEY_CTX *src))
1661 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1662 void (*cleanup) (EVP_PKEY_CTX *ctx))
1664 pmeth->cleanup = cleanup;
1667 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1668 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1669 int (*paramgen) (EVP_PKEY_CTX *ctx,
1672 pmeth->paramgen_init = paramgen_init;
1673 pmeth->paramgen = paramgen;
1676 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1677 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1678 int (*keygen) (EVP_PKEY_CTX *ctx,
1681 pmeth->keygen_init = keygen_init;
1682 pmeth->keygen = keygen;
1685 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1686 int (*sign_init) (EVP_PKEY_CTX *ctx),
1687 int (*sign) (EVP_PKEY_CTX *ctx,
1688 unsigned char *sig, size_t *siglen,
1689 const unsigned char *tbs,
1692 pmeth->sign_init = sign_init;
1696 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1697 int (*verify_init) (EVP_PKEY_CTX *ctx),
1698 int (*verify) (EVP_PKEY_CTX *ctx,
1699 const unsigned char *sig,
1701 const unsigned char *tbs,
1704 pmeth->verify_init = verify_init;
1705 pmeth->verify = verify;
1708 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1709 int (*verify_recover_init) (EVP_PKEY_CTX
1711 int (*verify_recover) (EVP_PKEY_CTX
1720 pmeth->verify_recover_init = verify_recover_init;
1721 pmeth->verify_recover = verify_recover;
1724 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1725 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1727 int (*signctx) (EVP_PKEY_CTX *ctx,
1732 pmeth->signctx_init = signctx_init;
1733 pmeth->signctx = signctx;
1736 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1737 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1739 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1740 const unsigned char *sig,
1744 pmeth->verifyctx_init = verifyctx_init;
1745 pmeth->verifyctx = verifyctx;
1748 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1749 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1750 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1753 const unsigned char *in,
1756 pmeth->encrypt_init = encrypt_init;
1757 pmeth->encrypt = encryptfn;
1760 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1761 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1762 int (*decrypt) (EVP_PKEY_CTX *ctx,
1765 const unsigned char *in,
1768 pmeth->decrypt_init = decrypt_init;
1769 pmeth->decrypt = decrypt;
1772 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1773 int (*derive_init) (EVP_PKEY_CTX *ctx),
1774 int (*derive) (EVP_PKEY_CTX *ctx,
1778 pmeth->derive_init = derive_init;
1779 pmeth->derive = derive;
1782 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1783 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1785 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1790 pmeth->ctrl_str = ctrl_str;
1793 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1794 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1795 const unsigned char *tbs, size_t tbslen))
1797 pmeth->digestsign = digestsign;
1800 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1801 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1802 size_t siglen, const unsigned char *tbs,
1805 pmeth->digestverify = digestverify;
1808 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1809 int (*check) (EVP_PKEY *pkey))
1811 pmeth->check = check;
1814 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1815 int (*check) (EVP_PKEY *pkey))
1817 pmeth->public_check = check;
1820 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1821 int (*check) (EVP_PKEY *pkey))
1823 pmeth->param_check = check;
1826 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1827 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1830 pmeth->digest_custom = digest_custom;
1833 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1834 int (**pinit) (EVP_PKEY_CTX *ctx))
1836 *pinit = pmeth->init;
1839 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1840 int (**pcopy) (EVP_PKEY_CTX *dst,
1841 const EVP_PKEY_CTX *src))
1843 *pcopy = pmeth->copy;
1846 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1847 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1849 *pcleanup = pmeth->cleanup;
1852 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1853 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1854 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1858 *pparamgen_init = pmeth->paramgen_init;
1860 *pparamgen = pmeth->paramgen;
1863 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1864 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1865 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1869 *pkeygen_init = pmeth->keygen_init;
1871 *pkeygen = pmeth->keygen;
1874 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1875 int (**psign_init) (EVP_PKEY_CTX *ctx),
1876 int (**psign) (EVP_PKEY_CTX *ctx,
1877 unsigned char *sig, size_t *siglen,
1878 const unsigned char *tbs,
1882 *psign_init = pmeth->sign_init;
1884 *psign = pmeth->sign;
1887 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1888 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1889 int (**pverify) (EVP_PKEY_CTX *ctx,
1890 const unsigned char *sig,
1892 const unsigned char *tbs,
1896 *pverify_init = pmeth->verify_init;
1898 *pverify = pmeth->verify;
1901 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1902 int (**pverify_recover_init) (EVP_PKEY_CTX
1904 int (**pverify_recover) (EVP_PKEY_CTX
1913 if (pverify_recover_init)
1914 *pverify_recover_init = pmeth->verify_recover_init;
1915 if (pverify_recover)
1916 *pverify_recover = pmeth->verify_recover;
1919 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1920 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1922 int (**psignctx) (EVP_PKEY_CTX *ctx,
1928 *psignctx_init = pmeth->signctx_init;
1930 *psignctx = pmeth->signctx;
1933 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1934 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1936 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1937 const unsigned char *sig,
1941 if (pverifyctx_init)
1942 *pverifyctx_init = pmeth->verifyctx_init;
1944 *pverifyctx = pmeth->verifyctx;
1947 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1948 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1949 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1952 const unsigned char *in,
1956 *pencrypt_init = pmeth->encrypt_init;
1958 *pencryptfn = pmeth->encrypt;
1961 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1962 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1963 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1966 const unsigned char *in,
1970 *pdecrypt_init = pmeth->decrypt_init;
1972 *pdecrypt = pmeth->decrypt;
1975 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1976 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1977 int (**pderive) (EVP_PKEY_CTX *ctx,
1982 *pderive_init = pmeth->derive_init;
1984 *pderive = pmeth->derive;
1987 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1988 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1990 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1995 *pctrl = pmeth->ctrl;
1997 *pctrl_str = pmeth->ctrl_str;
2000 void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
2001 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2002 const unsigned char *tbs, size_t tbslen))
2005 *digestsign = pmeth->digestsign;
2008 void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
2009 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2010 size_t siglen, const unsigned char *tbs,
2014 *digestverify = pmeth->digestverify;
2017 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2018 int (**pcheck) (EVP_PKEY *pkey))
2021 *pcheck = pmeth->check;
2024 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2025 int (**pcheck) (EVP_PKEY *pkey))
2028 *pcheck = pmeth->public_check;
2031 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2032 int (**pcheck) (EVP_PKEY *pkey))
2035 *pcheck = pmeth->param_check;
2038 void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
2039 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2042 if (pdigest_custom != NULL)
2043 *pdigest_custom = pmeth->digest_custom;
2046 #endif /* FIPS_MODULE */