From 685026dd396796bc13c7b7a3843f93cceec64a73 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Mon, 26 Oct 2026 13:54:35 -0400 Subject: [PATCH] Add Devices.ChangeDevicePassword --- .../grpc/DevicesGrpcService.java | 20 ++++++- .../storage/AccountsManager.java | 38 +++++++++++++ .../main/proto/org/signal/chat/device.proto | 9 +++ .../grpc/DevicesGrpcServiceTest.java | 57 +++++++++++++++++++ .../storage/AccountsManagerTest.java | 48 ++++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java index 3dd5a0f1f..304b540ab 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java @@ -12,6 +12,8 @@ import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.commons.lang3.StringUtils; +import org.signal.chat.device.ChangeDevicePasswordRequest; +import org.signal.chat.device.ChangeDevicePasswordResponse; import org.signal.chat.device.ClearPushTokenRequest; import org.signal.chat.device.ClearPushTokenResponse; import org.signal.chat.device.GetDevicesRequest; @@ -28,7 +30,6 @@ import org.signal.chat.device.SimpleDevicesGrpc; import org.signal.chat.errors.NotFound; import org.whispersystems.textsecuregcm.auth.grpc.AuthenticatedDevice; import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.Device; @@ -184,6 +185,23 @@ public class DevicesGrpcService extends SimpleDevicesGrpc.DevicesImplBase { return SetCapabilitiesResponse.getDefaultInstance(); } + @Override + public ChangeDevicePasswordResponse changeDevicePassword(final ChangeDevicePasswordRequest request) { + final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice(); + + if (request.getNewPassword().isBlank()) { + throw GrpcExceptions.fieldViolation("new_password", "password must not be blank"); + } + + if (!accountsManager.updateDevicePassword(authenticatedDevice.accountIdentifier(), authenticatedDevice.deviceId(), + request.getNewPassword())) { + + throw GrpcExceptions.invalidCredentials("invalid credentials"); + } + + return ChangeDevicePasswordResponse.getDefaultInstance(); + } + private Account getAuthenticatedAccount() { return accountsManager.getByAccountIdentifier(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier()) .orElseThrow(() -> GrpcExceptions.invalidCredentials("invalid credentials")); 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 c99df8117..dbc51acac 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -71,6 +71,7 @@ import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialPresentation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.whispersystems.textsecuregcm.auth.DisconnectionRequestManager; +import org.whispersystems.textsecuregcm.auth.SaltedTokenHash; import org.whispersystems.textsecuregcm.auth.webauthn.AuthenticationCeremonyParameters; import org.whispersystems.textsecuregcm.auth.webauthn.RegistrationCeremonyParameters; import org.whispersystems.textsecuregcm.auth.webauthn.RegistrationCeremonyResult; @@ -1377,6 +1378,43 @@ public class AccountsManager extends RedisPubSubAdapter implemen }); } + /// Sets a new password for the given device and requests disconnection for all authenticated device connections. + /// + /// + /// @return `true` if the password was changed or `false` if the device no longer exists or its credentials + /// have been locked + public boolean updateDevicePassword(final UUID accountIdentifier, final byte deviceId, final String newPassword) { + + final SaltedTokenHash newCredentials = SaltedTokenHash.generateFor(newPassword); + + final Account updatedAccount = update(accountIdentifier, a -> { + + final Optional maybeDevice = a.getDevice(deviceId); + + return maybeDevice.map(d -> { + + if (d.hasLockedCredentials()) { + return false; + } + + d.setAuthTokenHash(newCredentials); + + return true; + + }).orElse(false); + }); + + final boolean passwordChanged = updatedAccount.getDevice(deviceId) + .map(d -> d.getAuthTokenHash().equals(newCredentials)) + .orElse(false); + + if (passwordChanged) { + disconnectionRequestManager.requestDisconnection(accountIdentifier, List.of(deviceId)); + } + + return passwordChanged; + } + /** * @param accountIdentifier identifier of account to update * @param updater must return {@code true} if the account was actually updated diff --git a/service/src/main/proto/org/signal/chat/device.proto b/service/src/main/proto/org/signal/chat/device.proto index ea4f9edff..48fa68f9d 100644 --- a/service/src/main/proto/org/signal/chat/device.proto +++ b/service/src/main/proto/org/signal/chat/device.proto @@ -46,6 +46,9 @@ service Devices { // Declares that the authenticated device supports certain features. rpc SetCapabilities(SetCapabilitiesRequest) returns (SetCapabilitiesResponse) {} + + // Changes the password for the currently authenticated device. + rpc ChangeDevicePassword(ChangeDevicePasswordRequest) returns (ChangeDevicePasswordResponse) {} } message GetDevicesRequest {} @@ -136,3 +139,9 @@ message SetCapabilitiesRequest { } message SetCapabilitiesResponse {} + +message ChangeDevicePasswordRequest { + string new_password = 1 [(require.size) = {min: 32; max: 255}]; +} + +message ChangeDevicePasswordResponse {} diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcServiceTest.java index 7d6d28e2f..9140d9c3f 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcServiceTest.java @@ -36,6 +36,7 @@ import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import org.junitpioneer.jupiter.cartesian.CartesianTest; import org.mockito.Mock; +import org.signal.chat.device.ChangeDevicePasswordRequest; import org.signal.chat.device.ClearPushTokenRequest; import org.signal.chat.device.ClearPushTokenResponse; import org.signal.chat.device.DevicesGrpc; @@ -504,4 +505,60 @@ class DevicesGrpcServiceTest extends SimpleBaseGrpcTest authenticatedServiceStub().changeDevicePassword(ChangeDevicePasswordRequest.newBuilder() + .setNewPassword(RandomStringUtils.insecure().nextAlphanumeric(32)) + .build())); + } + + @ParameterizedTest + @MethodSource + void changeDevicePasswordIllegalArgument(final ChangeDevicePasswordRequest request) { + assertStatusException(Status.INVALID_ARGUMENT, () -> authenticatedServiceStub().changeDevicePassword(request)); + verify(accountsManager, never()).updateDevicePassword(any(), anyByte(), any()); + } + + private static Stream changeDevicePasswordIllegalArgument() { + return Stream.of( + Arguments.argumentSet("No new password", + ChangeDevicePasswordRequest.newBuilder() + .build()), + + Arguments.argumentSet("New password too short", + ChangeDevicePasswordRequest.newBuilder() + .setNewPassword(RandomStringUtils.insecure().nextAlphanumeric(31)) + .build()), + + Arguments.argumentSet("New password blank", + ChangeDevicePasswordRequest.newBuilder() + .setNewPassword(" ".repeat(64)) + .build()), + + Arguments.argumentSet("New password too long", + ChangeDevicePasswordRequest.newBuilder() + .setNewPassword(RandomStringUtils.insecure().nextAlphanumeric(256)) + .build()) + ); + } } 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 be5c2a7b2..8635589e6 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java @@ -1414,6 +1414,54 @@ class AccountsManagerTest { ); } + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testUpdateDevicePassword(final boolean credentialsLocked) { + final Account account = AccountsHelper.generateTestAccount(null, UUID.randomUUID(), null, new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]); + addRetrievableAccount(account); + + final String newPassword = RandomStringUtils.insecure().nextAlphanumeric(32); + + final Device device = new Device(); + device.setId(Device.PRIMARY_ID); + device.setAuthTokenHash(SaltedTokenHash.generateFor("old-password")); + account.addDevice(device); + + if (credentialsLocked) { + device.lockAuthTokenHash(); + } + final SaltedTokenHash lockedCredentials = device.getAuthTokenHash(); + + assertEquals(!credentialsLocked, accountsManager.updateDevicePassword(account.getAccountIdentifier(), + Device.PRIMARY_ID, newPassword)); + + if (credentialsLocked) { + assertEquals(lockedCredentials, device.getAuthTokenHash()); + assertTrue(device.hasLockedCredentials()); + + verify(accounts, never()).update(any()); + verifyNoInteractions(disconnectionRequestManager); + } else { + assertTrue(device.getAuthTokenHash().verify(newPassword)); + + verify(accounts).update(account); + verify(disconnectionRequestManager) + .requestDisconnection(account.getAccountIdentifier(), List.of(Device.PRIMARY_ID)); + } + } + + @Test + void testUpdateDevicePasswordDeviceNotFound() { + final Account account = AccountsHelper.generateTestAccount(null, UUID.randomUUID(), null, new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]); + addRetrievableAccount(account); + final byte unknownDeviceId = 2; + + assertFalse(accountsManager.updateDevicePassword(account.getAccountIdentifier(), unknownDeviceId, "new-password")); + + verify(accounts, never()).update(any()); + verifyNoInteractions(disconnectionRequestManager); + } + @ParameterizedTest @CsvSource({ "+14152222222,+14153333333",