diff options
author | Neil Horman <nhorman@openssl.org> | 2023-12-08 14:59:23 -0500 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2023-12-14 11:14:51 +0100 |
commit | b697864cb85145ba39a1ef1192c0b8812947e8a3 (patch) | |
tree | 85e8811d54a94561704a623c6c4f4cac1592171f /providers/implementations/keymgmt | |
parent | 260d97229c467d17934ca3e2e0455b1b5c0994a6 (diff) |
Avoid setting gen_type to -1 in dh_gen_common_set_params
gh_gen_type_common_set_params looks up a dh contexts gen_type using
name2id, but if it returns error, we set gctx->gen_type to -1, which
is an invalid value, which may lead to undefined behavior in
future calls, in the event that said future calls preform an operation
of the form;
if (gen_type == <VALID VALUE>) {
do_stuff
else {
do_other_stuff
}
Technically it is not correct to continue with the operations on the
gen context after failed parameters setting but this makes it more
predictable.
Fix it by assigning the result of a lookup to a stack variable, and only
update gctx->gen_value if the lookup returns a non-failing value
In leiu of testing this specific case, also add an ossl_assert in dh_gen
to validate the gen_val input prior to continuing, should other code
points attempt to do the same thing
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22991)
Diffstat (limited to 'providers/implementations/keymgmt')
-rw-r--r-- | providers/implementations/keymgmt/dh_kmgmt.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/providers/implementations/keymgmt/dh_kmgmt.c b/providers/implementations/keymgmt/dh_kmgmt.c index 795a3f2fab..183738b61b 100644 --- a/providers/implementations/keymgmt/dh_kmgmt.c +++ b/providers/implementations/keymgmt/dh_kmgmt.c @@ -12,6 +12,7 @@ * internal use. */ #include "internal/deprecated.h" +#include "internal/common.h" #include <string.h> /* strcmp */ #include <openssl/core_dispatch.h> @@ -524,6 +525,7 @@ static int dh_gen_common_set_params(void *genctx, const OSSL_PARAM params[]) { struct dh_gen_ctx *gctx = genctx; const OSSL_PARAM *p; + int gen_type = -1; if (gctx == NULL) return 0; @@ -533,11 +535,13 @@ static int dh_gen_common_set_params(void *genctx, const OSSL_PARAM params[]) p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE); if (p != NULL) { if (p->data_type != OSSL_PARAM_UTF8_STRING - || ((gctx->gen_type = + || ((gen_type = dh_gen_type_name2id_w_default(p->data, gctx->dh_type)) == -1)) { ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } + if (gen_type != -1) + gctx->gen_type = gen_type; } p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); if (p != NULL) { @@ -706,6 +710,14 @@ static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) if (gctx->group_nid != NID_undef) gctx->gen_type = DH_PARAMGEN_TYPE_GROUP; + /* Bounds check on context gen_type */ + if (!ossl_assert((gctx->gen_type >= DH_PARAMGEN_TYPE_GENERATOR) + && (gctx->gen_type <= DH_PARAMGEN_TYPE_GROUP))) { + ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, + "gen_type set to unsupported value %d", gctx->gen_type); + return NULL; + } + /* For parameter generation - If there is a group name just create it */ if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP && gctx->ffc_params == NULL) { |