Skip to content

Commit 3032fbe

Browse files
committed
fix gcm tls fixed-iv length check so it properly rejects sizes larger than the iv buffer
1 parent 08bd5b1 commit 3032fbe

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

src/wp_aes_aead.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,10 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv,
959959
}
960960
else {
961961
/* Fixed field must be at least 4 bytes and invocation field at least 8
962-
*/
963-
if ((len < EVP_GCM_TLS_FIXED_IV_LEN) ||
964-
(ctx->ivLen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) {
962+
* bytes */
963+
if ((len < EVP_GCM_TLS_FIXED_IV_LEN) || (len > ctx->ivLen) ||
964+
(len > sizeof(ctx->iv)) ||
965+
(ctx->ivLen - len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) {
965966
return 0;
966967
}
967968
if (ctx->enc) {

test/test_aestag.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,74 @@ int test_aes_gcm_bad_tag(void *data)
14161416
return err;
14171417
}
14181418

1419+
static int test_aes_gcm_tls_iv_fixed_oversized_helper(OSSL_LIB_CTX *libCtx,
1420+
const char *cipherName, int keyLen)
1421+
{
1422+
int err;
1423+
EVP_CIPHER *cipher = NULL;
1424+
EVP_CIPHER_CTX *ctx = NULL;
1425+
unsigned char key[32];
1426+
/* ivLen (12, the GCM default) + EVP_GCM_TLS_EXPLICIT_IV_LEN (8) = 20: */
1427+
unsigned char iv[20];
1428+
1429+
memset(key, 0xCC, keyLen);
1430+
memset(iv, 0xDD, sizeof(iv));
1431+
1432+
cipher = EVP_CIPHER_fetch(libCtx, cipherName, "");
1433+
err = cipher == NULL;
1434+
1435+
/* A fixed len that leaves exactly the 8-byte
1436+
* explicit/invocation field must still be accepted. */
1437+
if (err == 0) {
1438+
ctx = EVP_CIPHER_CTX_new();
1439+
err = ctx == NULL;
1440+
}
1441+
if (err == 0) {
1442+
err = EVP_DecryptInit_ex(ctx, cipher, NULL, key, NULL) != 1;
1443+
}
1444+
if (err == 0) {
1445+
err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED,
1446+
EVP_GCM_TLS_FIXED_IV_LEN, iv) != 1;
1447+
}
1448+
EVP_CIPHER_CTX_free(ctx);
1449+
ctx = NULL;
1450+
1451+
/* Oversized fixed len (> ctx->ivLen, default 12) must be rejected. */
1452+
if (err == 0) {
1453+
ctx = EVP_CIPHER_CTX_new();
1454+
err = ctx == NULL;
1455+
}
1456+
if (err == 0) {
1457+
err = EVP_DecryptInit_ex(ctx, cipher, NULL, key, NULL) != 1;
1458+
}
1459+
if (err == 0) {
1460+
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED,
1461+
(int)sizeof(iv), iv) == 1) {
1462+
PRINT_ERR_MSG("%s: EVP_CTRL_GCM_SET_IV_FIXED incorrectly "
1463+
"accepted a fixed IV length (%d) larger than "
1464+
"the IV length", cipherName, (int)sizeof(iv));
1465+
err = 1;
1466+
}
1467+
}
1468+
1469+
EVP_CIPHER_CTX_free(ctx);
1470+
EVP_CIPHER_free(cipher);
1471+
return err;
1472+
}
1473+
1474+
int test_aes_gcm_tls_iv_fixed_oversized(void *data)
1475+
{
1476+
int err;
1477+
1478+
(void)data;
1479+
1480+
PRINT_MSG("AES-128-GCM TLS1 fixed-IV oversized length rejection");
1481+
err = test_aes_gcm_tls_iv_fixed_oversized_helper(wpLibCtx,
1482+
"AES-128-GCM", 16);
1483+
1484+
return err;
1485+
}
1486+
14191487
#endif /* WP_HAVE_AESGCM */
14201488

14211489
/******************************************************************************/

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ TEST_CASE test_case[] = {
288288
TEST_DECL(test_aes128_gcm_set_iv_inv, NULL),
289289
TEST_DECL(test_aes128_gcm_key_then_iv, NULL),
290290
TEST_DECL(test_aes_gcm_bad_tag, NULL),
291+
TEST_DECL(test_aes_gcm_tls_iv_fixed_oversized, NULL),
291292
#endif
292293
#ifdef WP_HAVE_AESCCM
293294
TEST_DECL(test_aes128_ccm, NULL),

test/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ int test_aes128_gcm_tls(void *data);
219219
int test_aes128_gcm_set_iv_inv(void *data);
220220
int test_aes128_gcm_key_then_iv(void *data);
221221
int test_aes_gcm_bad_tag(void *data);
222+
int test_aes_gcm_tls_iv_fixed_oversized(void *data);
222223

223224
#endif /* WP_HAVE_AESGCM */
224225

0 commit comments

Comments
 (0)