Skip to content

Commit 218952b

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 generate a known unsafe prime to verify the function rejects it appropriately. Affected function: DoKexDhGexGroup. Issue: F-1688
1 parent 543a6c2 commit 218952b

File tree

3 files changed

+226
-3
lines changed

3 files changed

+226
-3
lines changed

src/internal.c

Lines changed: 156 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6284,6 +6284,144 @@ static int DoKexDhGexRequest(WOLFSSH* ssh,
62846284
}
62856285

62866286

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

63076443
if (ret == WS_SUCCESS)
63086444
ret = GetMpint(&generatorSz, &generator, buf, len, &begin);
63096445

6446+
if (ret == WS_SUCCESS) {
6447+
ret = ValidateKexDhGexGroup(primeGroup, primeGroupSz,
6448+
generator, generatorSz,
6449+
ssh->handshake->dhGexMinSz,
6450+
ssh->handshake->dhGexMaxSz,
6451+
ssh->rng);
6452+
}
6453+
63106454
if (ret == WS_SUCCESS) {
63116455
if (ssh->handshake->primeGroup)
63126456
WFREE(ssh->handshake->primeGroup, ssh->ctx->heap, DYNTYPE_MPINT);
@@ -6340,7 +6484,17 @@ static int DoKexDhGexGroup(WOLFSSH* ssh,
63406484

63416485
return ret;
63426486
}
6487+
6488+
#ifdef WOLFSSH_TEST_INTERNAL
6489+
int wolfSSH_TestValidateKexDhGexGroup(const byte* primeGroup,
6490+
word32 primeGroupSz, const byte* generator, word32 generatorSz,
6491+
word32 minBits, word32 maxBits, WC_RNG* rng)
6492+
{
6493+
return ValidateKexDhGexGroup(primeGroup, primeGroupSz,
6494+
generator, generatorSz, minBits, maxBits, rng);
6495+
}
63436496
#endif
6497+
#endif /* !WOLFSSH_NO_DH_GEX_SHA256 */
63446498

63456499

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

tests/unit.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
#include <stdio.h>
3232
#include <wolfssh/ssh.h>
3333
#include <wolfssh/keygen.h>
34+
#include <wolfssh/error.h>
3435
#include <wolfssh/internal.h>
36+
#include <wolfssl/wolfcrypt/random.h>
37+
#include <wolfssl/wolfcrypt/integer.h>
3538
#include <wolfssl/wolfcrypt/hmac.h>
3639

3740
#define WOLFSSH_TEST_HEX2BIN
@@ -439,6 +442,57 @@ static int test_DoReceive_VerifyMacFailure(void)
439442
#endif /* WOLFSSH_TEST_INTERNAL && any HMAC SHA variant enabled */
440443

441444

445+
#if defined(WOLFSSH_TEST_INTERNAL) && !defined(WOLFSSH_NO_DH_GEX_SHA256)
446+
447+
/*
448+
* For testing the ValidateKexDhGexGroup() function, we need to verify that
449+
* that the function detects unsafe primes. The following unsafePrime is
450+
* the prime used with GOST-ECC. It is fine for that. For DH, it isn't safe.
451+
*/
452+
453+
const char unsafePrime[] =
454+
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
455+
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7";
456+
457+
458+
static int test_DhGexGroupValidate(void)
459+
{
460+
WC_RNG rng;
461+
byte* pFlat = NULL;
462+
word32 pFlatSz = 0;
463+
byte gFlat = 2;
464+
int result = 0, ret;
465+
466+
if (wc_InitRng(&rng) != 0) {
467+
printf("DhGexGroupValidate: wc_InitRng failed\n");
468+
result = -110;
469+
goto cleanup;
470+
}
471+
472+
ret = ConvertHexToBin(unsafePrime, &pFlat, &pFlatSz,
473+
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
474+
if (ret != 0) {
475+
result = -113;
476+
goto cleanup;
477+
}
478+
479+
ret = wolfSSH_TestValidateKexDhGexGroup(pFlat, pFlatSz, &gFlat, 1,
480+
512, 8192, &rng);
481+
if (ret != WS_CRYPTO_FAILED) {
482+
printf("DhGexGroupValidate: validator returned %d, expected %d\n",
483+
ret, WS_CRYPTO_FAILED);
484+
result = -121;
485+
}
486+
487+
cleanup:
488+
FreeBins(pFlat, NULL, NULL, NULL);
489+
wc_FreeRng(&rng);
490+
return result;
491+
}
492+
493+
#endif /* WOLFSSH_TEST_INTERNAL && !WOLFSSH_NO_DH_GEX_SHA256 */
494+
495+
442496
/* Error Code And Message Test */
443497

444498
static int test_Errors(void)
@@ -520,6 +574,13 @@ int wolfSSH_UnitTest(int argc, char** argv)
520574
testResult = testResult || unitResult;
521575
#endif
522576

577+
#if defined(WOLFSSH_TEST_INTERNAL) && !defined(WOLFSSH_NO_DH_GEX_SHA256)
578+
unitResult = test_DhGexGroupValidate();
579+
printf("DhGexGroupValidate: %s\n",
580+
(unitResult == 0 ? "SUCCESS" : "FAILED"));
581+
testResult = testResult || unitResult;
582+
#endif
583+
523584
#ifdef WOLFSSH_KEYGEN
524585
#ifndef WOLFSSH_NO_RSA
525586
unitResult = test_RsaKeyGen();

wolfssh/internal.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ enum NameIdType {
476476
#define MAX_KEX_KEY_SZ (WOLFSSH_DEFAULT_GEXDH_MAX / 8)
477477
#endif
478478
#endif
479+
#ifndef WOLFSSH_MR_ROUNDS
480+
#define WOLFSSH_MR_ROUNDS 8
481+
#endif
479482
#ifndef WOLFSSH_MAX_FILE_SIZE
480483
#define WOLFSSH_MAX_FILE_SIZE (1024ul * 1024ul * 4)
481484
#endif
@@ -1324,7 +1327,12 @@ enum WS_MessageIdLimits {
13241327
WOLFSSH_API int wolfSSH_TestIsMessageAllowed(WOLFSSH* ssh, byte msg,
13251328
byte state);
13261329
WOLFSSH_API int wolfSSH_TestDoReceive(WOLFSSH* ssh);
1327-
#endif
1330+
#ifndef WOLFSSH_NO_DH_GEX_SHA256
1331+
WOLFSSH_API int wolfSSH_TestValidateKexDhGexGroup(const byte* primeGroup,
1332+
word32 primeGroupSz, const byte* generator, word32 generatorSz,
1333+
word32 minBits, word32 maxBits, WC_RNG* rng);
1334+
#endif /* !WOLFSSH_NO_DH_GEX_SHA256 */
1335+
#endif /* WOLFSSH_TEST_INTERNAL */
13281336

13291337
/* dynamic memory types */
13301338
enum WS_DynamicTypes {

0 commit comments

Comments
 (0)