2 * Copyright 2006-2021 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));
132 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
137 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
141 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
146 if (*type == NID_undef)
147 *type = evp_pkey_name2type(keytype);
150 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
152 int type = NID_undef;
154 EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
158 #endif /* FIPS_MODULE */
160 int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
162 if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
163 return EVP_PKEY_STATE_UNKNOWN;
165 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
166 && ctx->op.kex.algctx != NULL)
167 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
168 && ctx->op.sig.algctx != NULL)
169 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
170 && ctx->op.ciph.algctx != NULL)
171 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
172 && ctx->op.keymgmt.genctx != NULL)
173 || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
174 && ctx->op.encap.algctx != NULL))
175 return EVP_PKEY_STATE_PROVIDER;
177 return EVP_PKEY_STATE_LEGACY;
180 static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
181 EVP_PKEY *pkey, ENGINE *e,
182 const char *keytype, const char *propquery,
186 EVP_PKEY_CTX *ret = NULL;
187 const EVP_PKEY_METHOD *pmeth = NULL;
188 EVP_KEYMGMT *keymgmt = NULL;
191 * If the given |pkey| is provided, we extract the keytype from its
192 * keymgmt and skip over the legacy code.
194 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
195 /* If we have an engine, something went wrong somewhere... */
196 if (!ossl_assert(e == NULL))
198 keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
203 /* Code below to be removed when legacy support is dropped. */
208 else if (keytype != NULL)
209 id = evp_pkey_name2type(keytype);
213 /* If no ID was found here, we can only resort to find a keymgmt */
218 * Here, we extract what information we can for the purpose of
219 * supporting usage with implementations from providers, to make
220 * for a smooth transition from legacy stuff to provider based stuff.
222 * If an engine is given, this is entirely legacy, and we should not
223 * pretend anything else, so we only set the name when no engine is
224 * given. If both are already given, someone made a mistake, and
225 * since that can only happen internally, it's safe to make an
228 if (!ossl_assert(e == NULL || keytype == NULL))
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 pmeth = evp_pkey_meth_find_added_by_application(id);
259 #endif /* FIPS_MODULE */
262 * If there's no engine and there's a name, we try fetching a provider
265 if (e == NULL && keytype != NULL) {
266 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
268 return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
272 * Chase down the legacy NID, as that might be needed for diverse
273 * purposes, such as ensure that EVP_PKEY_type() can return sensible
274 * values. We go through all keymgmt names, because the keytype
275 * that's passed to this function doesn't necessarily translate
278 if (keymgmt != NULL) {
279 int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt);
281 if (tmp_id != NID_undef) {
286 * It really really shouldn't differ. If it still does,
287 * something is very wrong.
289 if (!ossl_assert(id == tmp_id)) {
290 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
291 EVP_KEYMGMT_free(keymgmt);
300 if (pmeth == NULL && keymgmt == NULL) {
301 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
303 ret = OPENSSL_zalloc(sizeof(*ret));
305 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
308 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
309 if ((ret == NULL || pmeth == NULL) && e != NULL)
314 EVP_KEYMGMT_free(keymgmt);
317 if (propquery != NULL) {
318 ret->propquery = OPENSSL_strdup(propquery);
319 if (ret->propquery == NULL) {
321 EVP_KEYMGMT_free(keymgmt);
325 ret->libctx = libctx;
326 ret->keytype = keytype;
327 ret->keymgmt = keymgmt;
328 ret->legacy_keytype = id;
331 ret->operation = EVP_PKEY_OP_UNDEFINED;
334 EVP_PKEY_up_ref(pkey);
336 if (pmeth != NULL && pmeth->init != NULL) {
337 if (pmeth->init(ret) <= 0) {
339 EVP_PKEY_CTX_free(ret);
347 /*- All methods below can also be used in FIPS_MODULE */
349 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
351 const char *propquery)
353 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
356 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
357 const char *propquery)
359 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
362 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
364 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
365 if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
366 ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
367 EVP_SIGNATURE_free(ctx->op.sig.signature);
368 ctx->op.sig.algctx = NULL;
369 ctx->op.sig.signature = NULL;
370 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
371 if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
372 ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
373 EVP_KEYEXCH_free(ctx->op.kex.exchange);
374 ctx->op.kex.algctx = NULL;
375 ctx->op.kex.exchange = NULL;
376 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
377 if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
378 ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
379 EVP_KEM_free(ctx->op.encap.kem);
380 ctx->op.encap.algctx = NULL;
381 ctx->op.encap.kem = NULL;
383 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
384 if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
385 ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
386 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
387 ctx->op.ciph.algctx = NULL;
388 ctx->op.ciph.cipher = NULL;
389 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
390 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
391 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
395 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
399 if (ctx->pmeth && ctx->pmeth->cleanup)
400 ctx->pmeth->cleanup(ctx);
402 evp_pkey_ctx_free_old_ops(ctx);
404 evp_pkey_ctx_free_all_cached_data(ctx);
406 EVP_KEYMGMT_free(ctx->keymgmt);
408 OPENSSL_free(ctx->propquery);
409 EVP_PKEY_free(ctx->pkey);
410 EVP_PKEY_free(ctx->peerkey);
411 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
412 ENGINE_finish(ctx->engine);
414 BN_free(ctx->rsa_pubexp);
420 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
421 const EVP_PKEY_METHOD *meth)
424 *ppkey_id = meth->pkey_id;
426 *pflags = meth->flags;
429 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
431 int pkey_id = dst->pkey_id;
432 int flags = dst->flags;
436 /* We only copy the function pointers so restore the other values */
437 dst->pkey_id = pkey_id;
441 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
443 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
447 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
449 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
452 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
454 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
457 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
461 # ifndef OPENSSL_NO_ENGINE
462 /* Make sure it's safe to copy a pkey context using an ENGINE */
463 if (pctx->engine && !ENGINE_init(pctx->engine)) {
464 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
468 rctx = OPENSSL_zalloc(sizeof(*rctx));
470 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
474 if (pctx->pkey != NULL)
475 EVP_PKEY_up_ref(pctx->pkey);
476 rctx->pkey = pctx->pkey;
477 rctx->operation = pctx->operation;
478 rctx->libctx = pctx->libctx;
479 rctx->keytype = pctx->keytype;
480 rctx->propquery = NULL;
481 if (pctx->propquery != NULL) {
482 rctx->propquery = OPENSSL_strdup(pctx->propquery);
483 if (rctx->propquery == NULL)
486 rctx->legacy_keytype = pctx->legacy_keytype;
488 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
489 if (pctx->op.kex.exchange != NULL) {
490 rctx->op.kex.exchange = pctx->op.kex.exchange;
491 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
494 if (pctx->op.kex.algctx != NULL) {
495 if (!ossl_assert(pctx->op.kex.exchange != NULL))
498 = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
499 if (rctx->op.kex.algctx == NULL) {
500 EVP_KEYEXCH_free(rctx->op.kex.exchange);
505 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
506 if (pctx->op.sig.signature != NULL) {
507 rctx->op.sig.signature = pctx->op.sig.signature;
508 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
511 if (pctx->op.sig.algctx != NULL) {
512 if (!ossl_assert(pctx->op.sig.signature != NULL))
515 = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
516 if (rctx->op.sig.algctx == NULL) {
517 EVP_SIGNATURE_free(rctx->op.sig.signature);
522 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
523 if (pctx->op.ciph.cipher != NULL) {
524 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
525 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
528 if (pctx->op.ciph.algctx != NULL) {
529 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
532 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
533 if (rctx->op.ciph.algctx == NULL) {
534 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
539 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
540 if (pctx->op.encap.kem != NULL) {
541 rctx->op.encap.kem = pctx->op.encap.kem;
542 if (!EVP_KEM_up_ref(rctx->op.encap.kem))
545 if (pctx->op.encap.algctx != NULL) {
546 if (!ossl_assert(pctx->op.encap.kem != NULL))
548 rctx->op.encap.algctx
549 = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
550 if (rctx->op.encap.algctx == NULL) {
551 EVP_KEM_free(rctx->op.encap.kem);
556 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
557 /* Not supported - This would need a gen_dupctx() to work */
561 rctx->pmeth = pctx->pmeth;
562 # ifndef OPENSSL_NO_ENGINE
563 rctx->engine = pctx->engine;
566 if (pctx->peerkey != NULL)
567 EVP_PKEY_up_ref(pctx->peerkey);
568 rctx->peerkey = pctx->peerkey;
570 if (pctx->pmeth == NULL) {
571 if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
572 EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
575 provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
576 &tmp_keymgmt, pctx->propquery);
579 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
581 EVP_KEYMGMT_free(rctx->keymgmt);
582 rctx->keymgmt = tmp_keymgmt;
585 } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
590 EVP_PKEY_CTX_free(rctx);
594 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
596 if (app_pkey_methods == NULL) {
597 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
598 if (app_pkey_methods == NULL){
599 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
603 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
604 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
607 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
611 void evp_app_cleanup_int(void)
613 if (app_pkey_methods != NULL)
614 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
617 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
619 const EVP_PKEY_METHOD *ret;
621 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
623 return ret == NULL ? 0 : 1;
626 size_t EVP_PKEY_meth_get_count(void)
628 size_t rv = OSSL_NELEM(standard_methods);
630 if (app_pkey_methods)
631 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
635 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
637 if (idx < OSSL_NELEM(standard_methods))
638 return (standard_methods[idx])();
639 if (app_pkey_methods == NULL)
641 idx -= OSSL_NELEM(standard_methods);
642 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
644 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
648 int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
651 if (evp_pkey_ctx_is_legacy(ctx))
652 return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
654 return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
657 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
659 switch (evp_pkey_ctx_state(ctx)) {
660 case EVP_PKEY_STATE_PROVIDER:
661 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
662 && ctx->op.kex.exchange != NULL
663 && ctx->op.kex.exchange->set_ctx_params != NULL)
665 ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
667 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
668 && ctx->op.sig.signature != NULL
669 && ctx->op.sig.signature->set_ctx_params != NULL)
671 ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
673 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
674 && ctx->op.ciph.cipher != NULL
675 && ctx->op.ciph.cipher->set_ctx_params != NULL)
677 ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
679 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
680 && ctx->keymgmt != NULL
681 && ctx->keymgmt->gen_set_params != NULL)
683 evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
685 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
686 && ctx->op.encap.kem != NULL
687 && ctx->op.encap.kem->set_ctx_params != NULL)
689 ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
693 case EVP_PKEY_STATE_UNKNOWN:
694 case EVP_PKEY_STATE_LEGACY:
695 return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
701 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
703 switch (evp_pkey_ctx_state(ctx)) {
704 case EVP_PKEY_STATE_PROVIDER:
705 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
706 && ctx->op.kex.exchange != NULL
707 && ctx->op.kex.exchange->get_ctx_params != NULL)
709 ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
711 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
712 && ctx->op.sig.signature != NULL
713 && ctx->op.sig.signature->get_ctx_params != NULL)
715 ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
717 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
718 && ctx->op.ciph.cipher != NULL
719 && ctx->op.ciph.cipher->get_ctx_params != NULL)
721 ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
723 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
724 && ctx->op.encap.kem != NULL
725 && ctx->op.encap.kem->get_ctx_params != NULL)
727 ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
731 case EVP_PKEY_STATE_UNKNOWN:
732 case EVP_PKEY_STATE_LEGACY:
733 return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
740 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
744 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
745 && ctx->op.kex.exchange != NULL
746 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
747 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
748 return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
751 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
752 && ctx->op.sig.signature != NULL
753 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
754 provctx = ossl_provider_ctx(
755 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
756 return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
759 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
760 && ctx->op.ciph.cipher != NULL
761 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
762 provctx = ossl_provider_ctx(
763 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
764 return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
767 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
768 && ctx->op.encap.kem != NULL
769 && ctx->op.encap.kem->gettable_ctx_params != NULL) {
770 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
771 return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
777 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
781 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
782 && ctx->op.kex.exchange != NULL
783 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
784 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
785 return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
788 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
789 && ctx->op.sig.signature != NULL
790 && ctx->op.sig.signature->settable_ctx_params != NULL) {
791 provctx = ossl_provider_ctx(
792 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
793 return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
796 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
797 && ctx->op.ciph.cipher != NULL
798 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
799 provctx = ossl_provider_ctx(
800 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
801 return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
804 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
805 && ctx->keymgmt != NULL
806 && ctx->keymgmt->gen_settable_params != NULL) {
807 provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
808 return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
811 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
812 && ctx->op.encap.kem != NULL
813 && ctx->op.encap.kem->settable_ctx_params != NULL) {
814 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
815 return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
822 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
824 * Return 1 on success, 0 or negative for errors.
826 * In particular they return -2 if any of the params is not supported.
828 * They are not available in FIPS_MODULE as they depend on
829 * - EVP_PKEY_CTX_{get,set}_params()
830 * - EVP_PKEY_CTX_{gettable,settable}_params()
833 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
835 if (ctx == NULL || params == NULL)
839 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
840 * depend on the translation that happens in EVP_PKEY_CTX_set_params()
841 * call, and that the resulting ctrl call will return -2 if it doesn't
842 * known the ctrl command number.
844 if (evp_pkey_ctx_is_provided(ctx)) {
845 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
848 for (p = params; p->key != NULL; p++) {
849 /* Check the ctx actually understands this parameter */
850 if (OSSL_PARAM_locate_const(settable, p->key) == NULL )
855 return EVP_PKEY_CTX_set_params(ctx, params);
858 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
860 if (ctx == NULL || params == NULL)
864 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
865 * depend on the translation that happens in EVP_PKEY_CTX_get_params()
866 * call, and that the resulting ctrl call will return -2 if it doesn't
867 * known the ctrl command number.
869 if (evp_pkey_ctx_is_provided(ctx)) {
870 const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
873 for (p = params; p->key != NULL; p++ ) {
874 /* Check the ctx actually understands this parameter */
875 if (OSSL_PARAM_locate_const(gettable, p->key) == NULL )
880 return EVP_PKEY_CTX_get_params(ctx, params);
883 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
885 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
886 /* 80 should be big enough */
890 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
891 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
892 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
896 if (ctx->op.sig.algctx == NULL)
897 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
898 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
900 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
903 *p = OSSL_PARAM_construct_end();
905 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
908 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
917 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
918 int fallback, const char *param, int op,
921 OSSL_PARAM md_params[2], *p = md_params;
924 if (ctx == NULL || (ctx->operation & op) == 0) {
925 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
926 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
931 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
936 name = EVP_MD_get0_name(md);
939 *p++ = OSSL_PARAM_construct_utf8_string(param,
941 * Cast away the const. This is read
942 * only so should be safe
945 *p = OSSL_PARAM_construct_end();
947 return EVP_PKEY_CTX_set_params(ctx, md_params);
950 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
952 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
953 OSSL_SIGNATURE_PARAM_DIGEST,
954 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
957 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
959 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
960 OSSL_KDF_PARAM_DIGEST,
961 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
964 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
965 const char *param, int op, int ctrl,
966 const unsigned char *data,
969 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
971 if (ctx == NULL || (ctx->operation & op) == 0) {
972 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
973 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
977 /* Code below to be removed when legacy support is dropped. */
979 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
980 /* end of legacy support */
983 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
987 *p++ = OSSL_PARAM_construct_octet_string(param,
989 * Cast away the const. This is read
990 * only so should be safe
992 (unsigned char *)data,
994 *p = OSSL_PARAM_construct_end();
996 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
999 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1000 const unsigned char *sec, int seclen)
1002 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1003 OSSL_KDF_PARAM_SECRET,
1005 EVP_PKEY_CTRL_TLS_SECRET,
1009 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1010 const unsigned char *seed, int seedlen)
1012 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1013 OSSL_KDF_PARAM_SEED,
1015 EVP_PKEY_CTRL_TLS_SEED,
1019 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1021 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1022 OSSL_KDF_PARAM_DIGEST,
1023 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1026 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1027 const unsigned char *salt, int saltlen)
1029 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1030 OSSL_KDF_PARAM_SALT,
1032 EVP_PKEY_CTRL_HKDF_SALT,
1036 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1037 const unsigned char *key, int keylen)
1039 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1042 EVP_PKEY_CTRL_HKDF_KEY,
1046 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1047 const unsigned char *info, int infolen)
1049 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1050 OSSL_KDF_PARAM_INFO,
1052 EVP_PKEY_CTRL_HKDF_INFO,
1056 int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1058 OSSL_PARAM int_params[2], *p = int_params;
1060 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1061 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1062 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1066 /* Code below to be removed when legacy support is dropped. */
1067 if (ctx->op.kex.algctx == NULL)
1068 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1069 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1070 /* end of legacy support */
1073 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1077 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1078 *p = OSSL_PARAM_construct_end();
1080 return EVP_PKEY_CTX_set_params(ctx, int_params);
1083 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1086 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1087 OSSL_KDF_PARAM_PASSWORD,
1090 (const unsigned char *)pass, passlen);
1093 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1094 const unsigned char *salt, int saltlen)
1096 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1097 OSSL_KDF_PARAM_SALT,
1099 EVP_PKEY_CTRL_SCRYPT_SALT,
1103 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1104 int op, int ctrl, uint64_t val)
1106 OSSL_PARAM uint64_params[2], *p = uint64_params;
1108 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1109 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1110 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1114 /* Code below to be removed when legacy support is dropped. */
1115 if (ctx->op.kex.algctx == NULL)
1116 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1117 /* end of legacy support */
1119 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1120 *p = OSSL_PARAM_construct_end();
1122 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1125 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1127 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1128 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1132 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1134 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1135 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1139 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1141 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1142 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1146 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1147 uint64_t maxmem_bytes)
1149 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1151 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1155 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1158 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1159 OSSL_PKEY_PARAM_PRIV_KEY,
1161 EVP_PKEY_CTRL_SET_MAC_KEY,
1165 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1167 OSSL_PARAM params[2], *p = params;
1169 if (ctx == NULL || op == NULL) {
1170 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1173 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1174 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1177 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1179 *p = OSSL_PARAM_construct_end();
1180 return EVP_PKEY_CTX_set_params(ctx, params);
1183 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
1185 OSSL_PARAM params[2], *p = params;
1188 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1189 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1190 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1194 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
1196 * Cast away the const. This is
1197 * read only so should be safe
1199 (void *)id, (size_t)len);
1200 *p++ = OSSL_PARAM_construct_end();
1202 ret = evp_pkey_ctx_set_params_strict(ctx, params);
1204 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1208 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1210 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1211 EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1214 static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
1217 void *tmp_id = NULL;
1218 OSSL_PARAM params[2], *p = params;
1220 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1221 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1222 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1226 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
1228 *p++ = OSSL_PARAM_construct_end();
1230 ret = evp_pkey_ctx_get_params_strict(ctx, params);
1232 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1233 } else if (ret > 0) {
1234 size_t tmp_id_len = params[0].return_size;
1237 memcpy(id, tmp_id, tmp_id_len);
1239 *id_len = tmp_id_len;
1244 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
1246 return get1_id_data(ctx, id, NULL);
1249 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
1251 return get1_id_data(ctx, NULL, id_len);
1254 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1256 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1259 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1261 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1262 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1265 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1266 int cmd, int p1, void *p2)
1271 * If the method has a |digest_custom| function, we can relax the
1272 * operation type check, since this can be called before the operation
1275 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1276 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1277 ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1281 if ((optype != -1) && !(ctx->operation & optype)) {
1282 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1287 switch (evp_pkey_ctx_state(ctx)) {
1288 case EVP_PKEY_STATE_PROVIDER:
1289 return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1290 case EVP_PKEY_STATE_UNKNOWN:
1291 case EVP_PKEY_STATE_LEGACY:
1292 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1293 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1296 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1299 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1302 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1308 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1309 int cmd, int p1, void *p2)
1314 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1317 /* If unsupported, we don't want that reported here */
1319 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1324 ERR_clear_last_mark();
1326 * If there was an error, there was an error.
1327 * If the operation isn't initialized yet, we also return, as
1328 * the saved values will be used then anyway.
1330 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1333 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1336 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1337 int cmd, uint64_t value)
1339 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1343 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1344 const char *name, const char *value)
1349 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1353 switch (evp_pkey_ctx_state(ctx)) {
1354 case EVP_PKEY_STATE_PROVIDER:
1355 return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1356 case EVP_PKEY_STATE_UNKNOWN:
1357 case EVP_PKEY_STATE_LEGACY:
1358 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1359 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1362 if (strcmp(name, "digest") == 0)
1363 ret = EVP_PKEY_CTX_md(ctx,
1364 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1365 EVP_PKEY_CTRL_MD, value);
1367 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1374 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1375 const char *name, const char *value)
1379 /* If unsupported, we don't want that reported here */
1381 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1382 name, value, strlen(value) + 1);
1386 ERR_clear_last_mark();
1388 * If there was an error, there was an error.
1389 * If the operation isn't initialized yet, we also return, as
1390 * the saved values will be used then anyway.
1392 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1396 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1399 static int decode_cmd(int cmd, const char *name)
1403 * The consequence of the assertion not being true is that this
1404 * function will return -1, which will cause the calling functions
1405 * to signal that the command is unsupported... in non-debug mode.
1407 if (ossl_assert(name != NULL))
1408 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1409 cmd = EVP_PKEY_CTRL_SET1_ID;
1415 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1416 int keytype, int optype,
1417 int cmd, const char *name,
1418 const void *data, size_t data_len)
1421 * Check that it's one of the supported commands. The ctrl commands
1422 * number cases here must correspond to the cases in the bottom switch
1425 switch (cmd = decode_cmd(cmd, name)) {
1426 case EVP_PKEY_CTRL_SET1_ID:
1429 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1433 if (keytype != -1) {
1434 switch (evp_pkey_ctx_state(ctx)) {
1435 case EVP_PKEY_STATE_PROVIDER:
1436 if (ctx->keymgmt == NULL) {
1437 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1440 if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1441 evp_pkey_type2name(keytype))) {
1442 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1446 case EVP_PKEY_STATE_UNKNOWN:
1447 case EVP_PKEY_STATE_LEGACY:
1448 if (ctx->pmeth == NULL) {
1449 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1452 if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1453 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1459 if (optype != -1 && (ctx->operation & optype) == 0) {
1460 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1465 case EVP_PKEY_CTRL_SET1_ID:
1466 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1468 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1469 if (ctx->cached_parameters.dist_id_name == NULL) {
1470 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1475 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1476 if (ctx->cached_parameters.dist_id == NULL) {
1477 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1481 ctx->cached_parameters.dist_id_set = 1;
1482 ctx->cached_parameters.dist_id_len = data_len;
1488 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1489 int cmd, const char *name)
1491 cmd = decode_cmd(cmd, name);
1493 case EVP_PKEY_CTRL_SET1_ID:
1494 OPENSSL_free(ctx->cached_parameters.dist_id);
1495 OPENSSL_free(ctx->cached_parameters.dist_id_name);
1496 ctx->cached_parameters.dist_id = NULL;
1497 ctx->cached_parameters.dist_id_name = NULL;
1502 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1504 evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1507 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1511 if (ret && ctx->cached_parameters.dist_id_set) {
1512 const char *name = ctx->cached_parameters.dist_id_name;
1513 const void *val = ctx->cached_parameters.dist_id;
1514 size_t len = ctx->cached_parameters.dist_id_len;
1517 ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1519 ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1520 EVP_PKEY_CTRL_SET1_ID,
1521 (int)len, (void *)val);
1527 OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1532 const char *EVP_PKEY_CTX_get0_propq(EVP_PKEY_CTX *ctx)
1534 return ctx->propquery;
1537 /* Utility functions to send a string of hex string to a ctrl */
1539 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1546 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1549 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1555 bin = OPENSSL_hexstr2buf(hex, &binlen);
1558 if (binlen <= INT_MAX)
1559 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1564 /* Pass a message digest to a ctrl */
1565 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1569 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1570 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1573 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1576 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1578 return ctx->operation;
1581 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1583 ctx->keygen_info = dat;
1584 ctx->keygen_info_count = datlen;
1587 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1592 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1597 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1602 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1604 return ctx->peerkey;
1607 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1609 ctx->app_data = data;
1612 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1614 return ctx->app_data;
1617 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1618 int (*init) (EVP_PKEY_CTX *ctx))
1623 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1624 int (*copy) (EVP_PKEY_CTX *dst,
1625 const EVP_PKEY_CTX *src))
1630 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1631 void (*cleanup) (EVP_PKEY_CTX *ctx))
1633 pmeth->cleanup = cleanup;
1636 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1637 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1638 int (*paramgen) (EVP_PKEY_CTX *ctx,
1641 pmeth->paramgen_init = paramgen_init;
1642 pmeth->paramgen = paramgen;
1645 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1646 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1647 int (*keygen) (EVP_PKEY_CTX *ctx,
1650 pmeth->keygen_init = keygen_init;
1651 pmeth->keygen = keygen;
1654 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1655 int (*sign_init) (EVP_PKEY_CTX *ctx),
1656 int (*sign) (EVP_PKEY_CTX *ctx,
1657 unsigned char *sig, size_t *siglen,
1658 const unsigned char *tbs,
1661 pmeth->sign_init = sign_init;
1665 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1666 int (*verify_init) (EVP_PKEY_CTX *ctx),
1667 int (*verify) (EVP_PKEY_CTX *ctx,
1668 const unsigned char *sig,
1670 const unsigned char *tbs,
1673 pmeth->verify_init = verify_init;
1674 pmeth->verify = verify;
1677 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1678 int (*verify_recover_init) (EVP_PKEY_CTX
1680 int (*verify_recover) (EVP_PKEY_CTX
1689 pmeth->verify_recover_init = verify_recover_init;
1690 pmeth->verify_recover = verify_recover;
1693 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1694 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1696 int (*signctx) (EVP_PKEY_CTX *ctx,
1701 pmeth->signctx_init = signctx_init;
1702 pmeth->signctx = signctx;
1705 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1706 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1708 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1709 const unsigned char *sig,
1713 pmeth->verifyctx_init = verifyctx_init;
1714 pmeth->verifyctx = verifyctx;
1717 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1718 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1719 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1722 const unsigned char *in,
1725 pmeth->encrypt_init = encrypt_init;
1726 pmeth->encrypt = encryptfn;
1729 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1730 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1731 int (*decrypt) (EVP_PKEY_CTX *ctx,
1734 const unsigned char *in,
1737 pmeth->decrypt_init = decrypt_init;
1738 pmeth->decrypt = decrypt;
1741 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1742 int (*derive_init) (EVP_PKEY_CTX *ctx),
1743 int (*derive) (EVP_PKEY_CTX *ctx,
1747 pmeth->derive_init = derive_init;
1748 pmeth->derive = derive;
1751 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1752 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1754 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1759 pmeth->ctrl_str = ctrl_str;
1762 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1763 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1764 const unsigned char *tbs, size_t tbslen))
1766 pmeth->digestsign = digestsign;
1769 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1770 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1771 size_t siglen, const unsigned char *tbs,
1774 pmeth->digestverify = digestverify;
1777 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1778 int (*check) (EVP_PKEY *pkey))
1780 pmeth->check = check;
1783 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1784 int (*check) (EVP_PKEY *pkey))
1786 pmeth->public_check = check;
1789 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1790 int (*check) (EVP_PKEY *pkey))
1792 pmeth->param_check = check;
1795 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1796 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1799 pmeth->digest_custom = digest_custom;
1802 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1803 int (**pinit) (EVP_PKEY_CTX *ctx))
1805 *pinit = pmeth->init;
1808 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1809 int (**pcopy) (EVP_PKEY_CTX *dst,
1810 const EVP_PKEY_CTX *src))
1812 *pcopy = pmeth->copy;
1815 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1816 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1818 *pcleanup = pmeth->cleanup;
1821 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1822 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1823 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1827 *pparamgen_init = pmeth->paramgen_init;
1829 *pparamgen = pmeth->paramgen;
1832 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1833 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1834 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1838 *pkeygen_init = pmeth->keygen_init;
1840 *pkeygen = pmeth->keygen;
1843 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1844 int (**psign_init) (EVP_PKEY_CTX *ctx),
1845 int (**psign) (EVP_PKEY_CTX *ctx,
1846 unsigned char *sig, size_t *siglen,
1847 const unsigned char *tbs,
1851 *psign_init = pmeth->sign_init;
1853 *psign = pmeth->sign;
1856 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1857 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1858 int (**pverify) (EVP_PKEY_CTX *ctx,
1859 const unsigned char *sig,
1861 const unsigned char *tbs,
1865 *pverify_init = pmeth->verify_init;
1867 *pverify = pmeth->verify;
1870 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1871 int (**pverify_recover_init) (EVP_PKEY_CTX
1873 int (**pverify_recover) (EVP_PKEY_CTX
1882 if (pverify_recover_init)
1883 *pverify_recover_init = pmeth->verify_recover_init;
1884 if (pverify_recover)
1885 *pverify_recover = pmeth->verify_recover;
1888 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1889 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1891 int (**psignctx) (EVP_PKEY_CTX *ctx,
1897 *psignctx_init = pmeth->signctx_init;
1899 *psignctx = pmeth->signctx;
1902 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1903 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1905 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1906 const unsigned char *sig,
1910 if (pverifyctx_init)
1911 *pverifyctx_init = pmeth->verifyctx_init;
1913 *pverifyctx = pmeth->verifyctx;
1916 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1917 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1918 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1921 const unsigned char *in,
1925 *pencrypt_init = pmeth->encrypt_init;
1927 *pencryptfn = pmeth->encrypt;
1930 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1931 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1932 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1935 const unsigned char *in,
1939 *pdecrypt_init = pmeth->decrypt_init;
1941 *pdecrypt = pmeth->decrypt;
1944 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1945 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1946 int (**pderive) (EVP_PKEY_CTX *ctx,
1951 *pderive_init = pmeth->derive_init;
1953 *pderive = pmeth->derive;
1956 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1957 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1959 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1964 *pctrl = pmeth->ctrl;
1966 *pctrl_str = pmeth->ctrl_str;
1969 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1970 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1971 const unsigned char *tbs, size_t tbslen))
1974 *digestsign = pmeth->digestsign;
1977 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1978 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1979 size_t siglen, const unsigned char *tbs,
1983 *digestverify = pmeth->digestverify;
1986 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1987 int (**pcheck) (EVP_PKEY *pkey))
1990 *pcheck = pmeth->check;
1993 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1994 int (**pcheck) (EVP_PKEY *pkey))
1997 *pcheck = pmeth->public_check;
2000 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2001 int (**pcheck) (EVP_PKEY *pkey))
2004 *pcheck = pmeth->param_check;
2007 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
2008 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2011 if (pdigest_custom != NULL)
2012 *pdigest_custom = pmeth->digest_custom;
2015 #endif /* FIPS_MODULE */