Skip to content

Commit cd1b667

Browse files
committed
Validate server's group
The client wasn't validating the DH group parameters in the KEX DH GEX Group message. This adds a function to perform the validation of the prime `p` to verify it is safe. (Prime and that ((p - 1) / 2) is prime.) Also adds a test to a known unsafe prime and known safe prime to verify the validate function. Affected function: DoKexDhGexGroup. Issue: F-1688
1 parent 543a6c2 commit cd1b667

File tree

3 files changed

+358
-3
lines changed

3 files changed

+358
-3
lines changed

src/internal.c

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@
185185
WOLFSSH_NO_CURVE25519_SHA256
186186
Set when Curve25519 or SHA2-256 are disabled in wolfSSL. Set to disable use
187187
of Curve25519 key exchange.
188+
WOLFSSH_MR_ROUNDS
189+
Set the number of Miller-Rabin rounds used when the client checks the
190+
servers prime group when using GEX key exchange. The default is 64.
188191
*/
189192

190193
static const char sshProtoIdStr[] = "SSH-2.0-wolfSSHv"
@@ -6284,6 +6287,139 @@ static int DoKexDhGexRequest(WOLFSSH* ssh,
62846287
}
62856288

62866289

6290+
/*
6291+
* Validate a DH GEX group received from the server against RFC 4419 sec. 3
6292+
* requirements, with an additional hardening/policy check:
6293+
* - p's bit length falls within the client's requested [minBits, maxBits]
6294+
* - p is odd and probably prime
6295+
* - (additional hardening requirement) (p-1)/2 is also probably prime,
6296+
* i.e. p is a safe prime, which bounds the order of g to q or 2q and
6297+
* closes the small-subgroup attack
6298+
* Returns WS_SUCCESS if the group is acceptable.
6299+
*/
6300+
static int ValidateKexDhGexGroup(const byte* primeGroup, word32 primeGroupSz,
6301+
const byte* generator, word32 generatorSz,
6302+
word32 minBits, word32 maxBits, WC_RNG* rng)
6303+
{
6304+
mp_int p, g, q;
6305+
int pgqInit = 0;
6306+
int bits;
6307+
int isPrime = MP_NO;
6308+
int ret = WS_SUCCESS;
6309+
6310+
if (primeGroup == NULL || primeGroupSz == 0
6311+
|| generator == NULL || generatorSz == 0
6312+
|| rng == NULL)
6313+
ret = WS_BAD_ARGUMENT;
6314+
6315+
/*
6316+
* Check the bounds on the size of the flat prime group and generator
6317+
* values. The prime group shall be LE maxBits. The generator size
6318+
* shall be LE prime group size. This is a check on the sizes of values
6319+
* sent by the peer before reading them in and checking them as mp_ints.
6320+
*/
6321+
if (ret == WS_SUCCESS) {
6322+
word32 maxBytes = (maxBits / 8) + ((maxBits % 8) ? 1 : 0);
6323+
/* Adjust the sizes for signed padding. */
6324+
word32 adjPrimeGroupSz = primeGroupSz - ((primeGroup[0] == 0) ? 1 : 0);
6325+
word32 adjGeneratorSz = generatorSz - ((generator[0] == 0) ? 1 : 0);
6326+
6327+
if (adjPrimeGroupSz > maxBytes || adjGeneratorSz > adjPrimeGroupSz) {
6328+
ret = WS_DH_SIZE_E;
6329+
}
6330+
}
6331+
6332+
if (ret == WS_SUCCESS) {
6333+
if (mp_init_multi(&p, &g, &q, NULL, NULL, NULL) != MP_OKAY) {
6334+
ret = WS_CRYPTO_FAILED;
6335+
pgqInit = 1;
6336+
}
6337+
}
6338+
6339+
if (ret == WS_SUCCESS) {
6340+
if (mp_read_unsigned_bin(&p, primeGroup, primeGroupSz) != MP_OKAY)
6341+
ret = WS_CRYPTO_FAILED;
6342+
}
6343+
if (ret == WS_SUCCESS) {
6344+
if (mp_read_unsigned_bin(&g, generator, generatorSz) != MP_OKAY)
6345+
ret = WS_CRYPTO_FAILED;
6346+
}
6347+
6348+
if (ret == WS_SUCCESS) {
6349+
bits = mp_count_bits(&p);
6350+
if (bits < (int)minBits || bits > (int)maxBits) {
6351+
WLOG(WS_LOG_DEBUG,
6352+
"DH GEX: prime size %d outside requested [%u, %u]",
6353+
bits, minBits, maxBits);
6354+
ret = WS_DH_SIZE_E;
6355+
}
6356+
}
6357+
6358+
if (ret == WS_SUCCESS) {
6359+
if (!mp_isodd(&p)) {
6360+
WLOG(WS_LOG_DEBUG, "DH GEX: prime is even");
6361+
ret = WS_CRYPTO_FAILED;
6362+
}
6363+
}
6364+
6365+
/* 2 <= g: reject g == 0 and g == 1. */
6366+
if (ret == WS_SUCCESS) {
6367+
if (mp_cmp_d(&g, 1) != MP_GT) {
6368+
WLOG(WS_LOG_DEBUG, "DH GEX: generator < 2");
6369+
ret = WS_CRYPTO_FAILED;
6370+
}
6371+
}
6372+
6373+
/* g <= p - 2: reject g == p - 1 (order 2) and anything larger. */
6374+
if (ret == WS_SUCCESS) {
6375+
if (mp_sub_d(&p, 1, &q) != MP_OKAY)
6376+
ret = WS_CRYPTO_FAILED;
6377+
else if (mp_cmp(&g, &q) != MP_LT) {
6378+
WLOG(WS_LOG_DEBUG, "DH GEX: generator >= p - 1");
6379+
ret = WS_CRYPTO_FAILED;
6380+
}
6381+
}
6382+
6383+
/* Miller-Rabin: p must be prime. */
6384+
if (ret == WS_SUCCESS) {
6385+
isPrime = MP_NO;
6386+
ret = mp_prime_is_prime_ex(&p, WOLFSSH_MR_ROUNDS, &isPrime, rng);
6387+
if (ret != MP_OKAY || isPrime == MP_NO) {
6388+
WLOG(WS_LOG_DEBUG, "DH GEX: p is not prime");
6389+
ret = WS_CRYPTO_FAILED;
6390+
}
6391+
else {
6392+
ret = WS_SUCCESS;
6393+
}
6394+
}
6395+
6396+
/* Safe prime check: q = (p - 1) / 2 must also be prime. */
6397+
if (ret == WS_SUCCESS) {
6398+
if (mp_sub_d(&p, 1, &q) != MP_OKAY || mp_div_2(&q, &q) != MP_OKAY)
6399+
ret = WS_CRYPTO_FAILED;
6400+
}
6401+
if (ret == WS_SUCCESS) {
6402+
isPrime = MP_NO;
6403+
ret = mp_prime_is_prime_ex(&q, WOLFSSH_MR_ROUNDS, &isPrime, rng);
6404+
if (ret != MP_OKAY || isPrime == MP_NO) {
6405+
WLOG(WS_LOG_DEBUG, "DH GEX: (p-1)/2 is not prime, p is not safe");
6406+
ret = WS_CRYPTO_FAILED;
6407+
}
6408+
else {
6409+
ret = WS_SUCCESS;
6410+
}
6411+
}
6412+
6413+
if (pgqInit) {
6414+
mp_clear(&q);
6415+
mp_clear(&g);
6416+
mp_clear(&p);
6417+
}
6418+
6419+
return ret;
6420+
}
6421+
6422+
62876423
static int DoKexDhGexGroup(WOLFSSH* ssh,
62886424
byte* buf, word32 len, word32* idx)
62896425
{
@@ -6300,13 +6436,19 @@ static int DoKexDhGexGroup(WOLFSSH* ssh,
63006436
if (ret == WS_SUCCESS) {
63016437
begin = *idx;
63026438
ret = GetMpint(&primeGroupSz, &primeGroup, buf, len, &begin);
6303-
if (ret == WS_SUCCESS && primeGroupSz > (MAX_KEX_KEY_SZ + 1))
6304-
ret = WS_DH_SIZE_E;
63056439
}
63066440

63076441
if (ret == WS_SUCCESS)
63086442
ret = GetMpint(&generatorSz, &generator, buf, len, &begin);
63096443

6444+
if (ret == WS_SUCCESS) {
6445+
ret = ValidateKexDhGexGroup(primeGroup, primeGroupSz,
6446+
generator, generatorSz,
6447+
ssh->handshake->dhGexMinSz,
6448+
ssh->handshake->dhGexMaxSz,
6449+
ssh->rng);
6450+
}
6451+
63106452
if (ret == WS_SUCCESS) {
63116453
if (ssh->handshake->primeGroup)
63126454
WFREE(ssh->handshake->primeGroup, ssh->ctx->heap, DYNTYPE_MPINT);
@@ -6340,7 +6482,17 @@ static int DoKexDhGexGroup(WOLFSSH* ssh,
63406482

63416483
return ret;
63426484
}
6485+
6486+
#ifdef WOLFSSH_TEST_INTERNAL
6487+
int wolfSSH_TestValidateKexDhGexGroup(const byte* primeGroup,
6488+
word32 primeGroupSz, const byte* generator, word32 generatorSz,
6489+
word32 minBits, word32 maxBits, WC_RNG* rng)
6490+
{
6491+
return ValidateKexDhGexGroup(primeGroup, primeGroupSz,
6492+
generator, generatorSz, minBits, maxBits, rng);
6493+
}
63436494
#endif
6495+
#endif /* !WOLFSSH_NO_DH_GEX_SHA256 */
63446496

63456497

63466498
static int DoIgnore(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)

0 commit comments

Comments
 (0)