Skip to content

Commit 91ddeba

Browse files
t8mmattcaswell
authored andcommitted
DH_check(): Do not try checking q properties if it is obviously invalid
If |q| >= |p| then the q value is obviously wrong as q is supposed to be a prime divisor of p-1. We check if p is overly large so this added test implies that q is not large either when performing subsequent tests using that q value. Otherwise if it is too large these additional checks of the q value such as the primality test can then trigger DoS by doing overly long computations. Fixes CVE-2023-3817 Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from #21551)
1 parent eec805e commit 91ddeba

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

crypto/dh/dh_check.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int DH_check_ex(const DH *dh)
9797

9898
int DH_check(const DH *dh, int *ret)
9999
{
100-
int ok = 0, r;
100+
int ok = 0, r, q_good = 0;
101101
BN_CTX *ctx = NULL;
102102
BIGNUM *t1 = NULL, *t2 = NULL;
103103

@@ -120,7 +120,14 @@ int DH_check(const DH *dh, int *ret)
120120
if (t2 == NULL)
121121
goto err;
122122

123-
if (dh->q) {
123+
if (dh->q != NULL) {
124+
if (BN_ucmp(dh->p, dh->q) > 0)
125+
q_good = 1;
126+
else
127+
*ret |= DH_CHECK_INVALID_Q_VALUE;
128+
}
129+
130+
if (q_good) {
124131
if (BN_cmp(dh->g, BN_value_one()) <= 0)
125132
*ret |= DH_NOT_SUITABLE_GENERATOR;
126133
else if (BN_cmp(dh->g, dh->p) >= 0)

0 commit comments

Comments
 (0)