mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:46:53 +00:00
Add delay to TOTP confirmation validation
This commit is contained in:
1 parent
3a75f705c2
commit
8c62a7f819
2 files changed
+58
-14
No files matched your search
+17
-10
@@ -2086,7 +2086,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> 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<String, String> 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<String, String> 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;
|
||||
}
|
||||
}
|
||||
+41
-4
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user