Avoid setting gen_type to -1 in dsa_gen_set_params
authorNeil Horman <nhorman@openssl.org>
Fri, 8 Dec 2023 20:41:51 +0000 (15:41 -0500)
committerTomas Mraz <tomas@openssl.org>
Thu, 14 Dec 2023 10:17:48 +0000 (11:17 +0100)
commit5056133cc7d6c52033c25b2e1f7762bcafcce760
treec42b453bea75de5ab7dc7d420c8cb4a5cf2f7dfd
parentb697864cb85145ba39a1ef1192c0b8812947e8a3
Avoid setting gen_type to -1 in dsa_gen_set_params

gh_gen_type_common_set_params looks up a dsa contexts gen_type using
name2id, but if it returns error, we inadvertently set gctx->gen_type to
-1, which is an invalid value, which may lead to improper 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 dsa_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)
include/crypto/dsa.h
include/openssl/dh.h
providers/implementations/keymgmt/dh_kmgmt.c
providers/implementations/keymgmt/dsa_kmgmt.c