From 8c62a7f819e9cdb3f5867c9aa7778f04b5c2a0c6 Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Tue, 1 Sep 2026 15:45:28 -0500 Subject: [PATCH] Add delay to TOTP confirmation validation --- .../storage/AccountsManager.java | 27 ++++++----- .../storage/AccountsManagerTest.java | 45 +++++++++++++++++-- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index b6d308255..be45727a9 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -2086,7 +2086,7 @@ public class AccountsManager extends RedisPubSubAdapter implemen final TotpKey pendingTotpKey = maybePendingTotpKey.get(); try { - if (TOTP.validateOneTimePassword(pendingTotpKey, timestamp, oneTimePassword)) { + if (verifyTotp(pendingTotpKey, timestamp, oneTimePassword)) { final AtomicInteger keyId = new AtomicInteger(); update(accountIdentifier, account -> { @@ -2129,7 +2129,7 @@ public class AccountsManager extends RedisPubSubAdapter implemen .max(Map.Entry.comparingByKey()) .filter(entry -> { try { - return TOTP.validateOneTimePassword((AnnotatedTotpKey) entry.getValue(), timestamp, oneTimePassword); + return verifyTotp((AnnotatedTotpKey) entry.getValue(), timestamp, oneTimePassword); } catch (final InvalidKeyException e) { ImpossibleEvents.logImpossible(logger, "Invalid TOTP key for account {}", accountIdentifier, e); return false; @@ -2149,18 +2149,25 @@ public class AccountsManager extends RedisPubSubAdapter implemen return false; } - for (final Instant timestamp : new Instant[] { validationTimestamp, validationTimestamp.minus(maxTotpValidationDelay) }) { - for (final SecretKey totpKey : totpKeys) { - try { - if (TOTP.validateOneTimePassword(totpKey, timestamp, oneTimePassword)) { - return true; - } - } catch (final InvalidKeyException e) { - ImpossibleEvents.logImpossible(logger, "Invalid TOTP key for account {}", account.getAccountIdentifier(), e); + for (final SecretKey totpKey : totpKeys) { + try { + if (verifyTotp(totpKey, validationTimestamp, oneTimePassword)) { + return true; } + } catch (final InvalidKeyException e) { + ImpossibleEvents.logImpossible(logger, "Invalid TOTP key for account {}", account.getAccountIdentifier(), e); } } return false; } + + private boolean verifyTotp(final SecretKey totpKey, final Instant validationTimestamp, final int oneTimePassword) throws InvalidKeyException { + for (final Instant timestamp : new Instant[]{validationTimestamp, validationTimestamp.minus(maxTotpValidationDelay)}) { + if (TOTP.validateOneTimePassword(totpKey, timestamp, oneTimePassword)) { + return true; + } + } + return false; + } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java index 1db51140f..090c3ecec 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java @@ -1994,6 +1994,41 @@ class AccountsManagerTest { verify(account).setPendingTotpKey(null); } + @Test + void confirmPendingTotpKeyWithDelay() throws InvalidKeyException, TooManyTotpKeysException, TooManyMfaKeysException, NoSuchAlgorithmException { + final UUID accountIdentifier = UUID.randomUUID(); + + final Account account = mock(Account.class); + when(account.getAccountIdentifier()).thenReturn(accountIdentifier); + + when(accounts.getByAccountIdentifier(accountIdentifier)) + .thenReturn(Optional.of(account)); + + final TotpKey pendingTotpKey = accountsManager.generatePendingTotpKey(accountIdentifier); + final byte nextTotpKeyId = (byte) ThreadLocalRandom.current().nextInt(); + + when(account.getPendingTotpKey()).thenReturn(Optional.of(pendingTotpKey)); + when(account.getNextMfaKeyId()).thenReturn(nextTotpKeyId); + + final Instant beginningOfTotpWindow = totpWindowStart(Instant.now()); + final int oneTimePassword = AccountsManager.TOTP.generateOneTimePassword(pendingTotpKey, beginningOfTotpWindow); + + // Should be rejected since even after delay allowance, our otp is from the previous window + assertEquals(Optional.empty(), accountsManager.confirmPendingTotpKey(accountIdentifier, + oneTimePassword, + beginningOfTotpWindow.plus(AccountsManager.TOTP.getTimeStep()).plus(MAX_TOTP_VALIDATION_DELAY), + TestRandomUtil.nextBytes(16))); + + // Should still be accepted as MAX_TOTP_VALIDATION_DELAY delay brings us to the otp's window + assertEquals(Optional.of(nextTotpKeyId), accountsManager.confirmPendingTotpKey(accountIdentifier, + oneTimePassword, + beginningOfTotpWindow.plus(AccountsManager.TOTP.getTimeStep()) + .plus(MAX_TOTP_VALIDATION_DELAY) + .minus(Duration.ofMillis(1)), + TestRandomUtil.nextBytes(16))); + } + + @Test void confirmPendingTotpKeyPreviouslyConfirmed() throws InvalidKeyException, NoSuchAlgorithmException, TooManyMfaKeysException { final UUID accountIdentifier = UUID.randomUUID(); @@ -2148,10 +2183,7 @@ class AccountsManagerTest { final Account account = mock(Account.class); when(account.getMfaKeys()).thenReturn(Map.of((byte) 1, totpKey)); - final Instant beginningOfTotpWindow = - Instant.ofEpochMilli((Instant.now().toEpochMilli() / AccountsManager.TOTP.getTimeStep().toMillis()) * - AccountsManager.TOTP.getTimeStep().toMillis()); - + final Instant beginningOfTotpWindow = totpWindowStart(Instant.now()); final int oneTimePassword = AccountsManager.TOTP.generateOneTimePassword(totpKey, beginningOfTotpWindow); assertTrue(accountsManager.verifyTotp(account, beginningOfTotpWindow, oneTimePassword), @@ -2168,4 +2200,9 @@ class AccountsManagerTest { assertFalse(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP.getTimeStep()).plus(MAX_TOTP_VALIDATION_DELAY), oneTimePassword), "One-time password should not be valid after max delay past end of current TOTP window"); } + + private static Instant totpWindowStart(final Instant instant) { + return Instant.ofEpochMilli((instant.toEpochMilli() / AccountsManager.TOTP.getTimeStep().toMillis()) * + AccountsManager.TOTP.getTimeStep().toMillis()); + } }