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+ server's prime group when using GEX key exchange. The default is 64.
188191*/
189192
190193static const char sshProtoIdStr[] = "SSH-2.0-wolfSSHv"
@@ -6284,6 +6287,141 @@ 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+ }
6336+ else {
6337+ pgqInit = 1;
6338+ }
6339+ }
6340+
6341+ if (ret == WS_SUCCESS) {
6342+ if (mp_read_unsigned_bin(&p, primeGroup, primeGroupSz) != MP_OKAY)
6343+ ret = WS_CRYPTO_FAILED;
6344+ }
6345+ if (ret == WS_SUCCESS) {
6346+ if (mp_read_unsigned_bin(&g, generator, generatorSz) != MP_OKAY)
6347+ ret = WS_CRYPTO_FAILED;
6348+ }
6349+
6350+ if (ret == WS_SUCCESS) {
6351+ bits = mp_count_bits(&p);
6352+ if (bits < (int)minBits || bits > (int)maxBits) {
6353+ WLOG(WS_LOG_DEBUG,
6354+ "DH GEX: prime size %d outside requested [%u, %u]",
6355+ bits, minBits, maxBits);
6356+ ret = WS_DH_SIZE_E;
6357+ }
6358+ }
6359+
6360+ if (ret == WS_SUCCESS) {
6361+ if (!mp_isodd(&p)) {
6362+ WLOG(WS_LOG_DEBUG, "DH GEX: prime is even");
6363+ ret = WS_CRYPTO_FAILED;
6364+ }
6365+ }
6366+
6367+ /* 2 <= g: reject g == 0 and g == 1. */
6368+ if (ret == WS_SUCCESS) {
6369+ if (mp_cmp_d(&g, 1) != MP_GT) {
6370+ WLOG(WS_LOG_DEBUG, "DH GEX: generator < 2");
6371+ ret = WS_CRYPTO_FAILED;
6372+ }
6373+ }
6374+
6375+ /* g <= p - 2: reject g == p - 1 (order 2) and anything larger. */
6376+ if (ret == WS_SUCCESS) {
6377+ if (mp_sub_d(&p, 1, &q) != MP_OKAY)
6378+ ret = WS_CRYPTO_FAILED;
6379+ else if (mp_cmp(&g, &q) != MP_LT) {
6380+ WLOG(WS_LOG_DEBUG, "DH GEX: generator >= p - 1");
6381+ ret = WS_CRYPTO_FAILED;
6382+ }
6383+ }
6384+
6385+ /* Miller-Rabin: p must be prime. */
6386+ if (ret == WS_SUCCESS) {
6387+ isPrime = MP_NO;
6388+ ret = mp_prime_is_prime_ex(&p, WOLFSSH_MR_ROUNDS, &isPrime, rng);
6389+ if (ret != MP_OKAY || isPrime == MP_NO) {
6390+ WLOG(WS_LOG_DEBUG, "DH GEX: p is not prime");
6391+ ret = WS_CRYPTO_FAILED;
6392+ }
6393+ else {
6394+ ret = WS_SUCCESS;
6395+ }
6396+ }
6397+
6398+ /* Safe prime check: q = (p - 1) / 2 must also be prime. */
6399+ if (ret == WS_SUCCESS) {
6400+ if (mp_sub_d(&p, 1, &q) != MP_OKAY || mp_div_2(&q, &q) != MP_OKAY)
6401+ ret = WS_CRYPTO_FAILED;
6402+ }
6403+ if (ret == WS_SUCCESS) {
6404+ isPrime = MP_NO;
6405+ ret = mp_prime_is_prime_ex(&q, WOLFSSH_MR_ROUNDS, &isPrime, rng);
6406+ if (ret != MP_OKAY || isPrime == MP_NO) {
6407+ WLOG(WS_LOG_DEBUG, "DH GEX: (p-1)/2 is not prime, p is not safe");
6408+ ret = WS_CRYPTO_FAILED;
6409+ }
6410+ else {
6411+ ret = WS_SUCCESS;
6412+ }
6413+ }
6414+
6415+ if (pgqInit) {
6416+ mp_clear(&q);
6417+ mp_clear(&g);
6418+ mp_clear(&p);
6419+ }
6420+
6421+ return ret;
6422+ }
6423+
6424+
62876425static int DoKexDhGexGroup(WOLFSSH* ssh,
62886426 byte* buf, word32 len, word32* idx)
62896427{
@@ -6300,13 +6438,22 @@ 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))
6441+ if (ret == WS_SUCCESS && primeGroupSz > (MAX_KEX_KEY_SZ + 1)) {
63046442 ret = WS_DH_SIZE_E;
6443+ }
63056444 }
63066445
63076446 if (ret == WS_SUCCESS)
63086447 ret = GetMpint(&generatorSz, &generator, buf, len, &begin);
63096448
6449+ if (ret == WS_SUCCESS) {
6450+ ret = ValidateKexDhGexGroup(primeGroup, primeGroupSz,
6451+ generator, generatorSz,
6452+ ssh->handshake->dhGexMinSz,
6453+ ssh->handshake->dhGexMaxSz,
6454+ ssh->rng);
6455+ }
6456+
63106457 if (ret == WS_SUCCESS) {
63116458 if (ssh->handshake->primeGroup)
63126459 WFREE(ssh->handshake->primeGroup, ssh->ctx->heap, DYNTYPE_MPINT);
@@ -6340,7 +6487,17 @@ static int DoKexDhGexGroup(WOLFSSH* ssh,
63406487
63416488 return ret;
63426489}
6490+
6491+ #ifdef WOLFSSH_TEST_INTERNAL
6492+ int wolfSSH_TestValidateKexDhGexGroup(const byte* primeGroup,
6493+ word32 primeGroupSz, const byte* generator, word32 generatorSz,
6494+ word32 minBits, word32 maxBits, WC_RNG* rng)
6495+ {
6496+ return ValidateKexDhGexGroup(primeGroup, primeGroupSz,
6497+ generator, generatorSz, minBits, maxBits, rng);
6498+ }
63436499#endif
6500+ #endif /* !WOLFSSH_NO_DH_GEX_SHA256 */
63446501
63456502
63466503static int DoIgnore(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
0 commit comments