diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 111397894..f36c48500 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -1108,7 +1108,7 @@ public class WhisperServerService extends Application unauthenticatedServices = Stream.of( - new AccountsAnonymousGrpcService(accountsManager, rateLimiters), + new AccountsAnonymousGrpcService(accountsManager, rateLimiters, groupSendTokenUtil), new CallQualitySurveyGrpcService(callQualitySurveyManager, rateLimiters), new KeysAnonymousGrpcService(accountsManager, keysManager, groupZkSecretParams, Clock.systemUTC()), new ProfileAnonymousGrpcService(accountsManager, profilesManager, profileBadgeConverter, profileCdnPolicyGenerator, chatGenericZkSecretParams, groupZkSecretParams, rateLimiters, clock), diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java index c8f6fc93f..85fe8742a 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java @@ -559,7 +559,7 @@ public class ProfileController { private static Map getAccountCapabilities(final Account account) { return Arrays.stream(DeviceCapability.values()) - .filter(DeviceCapability::includeInProfile) + .filter(DeviceCapability::includeInLegacyProfile) .collect(Collectors.toMap(DeviceCapability::getName, account::hasCapability)); } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcService.java index b730f750d..322652a71 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcService.java @@ -6,31 +6,43 @@ package org.whispersystems.textsecuregcm.grpc; import com.google.protobuf.ByteString; +import java.util.Optional; import java.util.UUID; +import org.signal.chat.account.Capabilities; import org.signal.chat.account.CheckAccountExistenceRequest; import org.signal.chat.account.CheckAccountExistenceResponse; +import org.signal.chat.account.GetCapabilitiesAnonymousRequest; +import org.signal.chat.account.GetCapabilitiesAnonymousResponse; import org.signal.chat.account.LookupUsernameHashRequest; import org.signal.chat.account.LookupUsernameHashResponse; import org.signal.chat.account.LookupUsernameLinkRequest; import org.signal.chat.account.LookupUsernameLinkResponse; import org.signal.chat.account.SimpleAccountsAnonymousGrpc; +import org.signal.chat.errors.FailedUnidentifiedAuthorization; import org.signal.chat.errors.NotFound; +import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil; import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; import org.whispersystems.textsecuregcm.identity.ServiceIdentifier; import org.whispersystems.textsecuregcm.limits.RateLimiters; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; +import org.whispersystems.textsecuregcm.storage.DeviceCapability; import org.whispersystems.textsecuregcm.util.UUIDUtil; public class AccountsAnonymousGrpcService extends SimpleAccountsAnonymousGrpc.AccountsAnonymousImplBase { private final AccountsManager accountsManager; private final RateLimiters rateLimiters; + private final GroupSendTokenUtil groupSendTokenUtil; - public AccountsAnonymousGrpcService(final AccountsManager accountsManager, final RateLimiters rateLimiters) { + public AccountsAnonymousGrpcService( + final AccountsManager accountsManager, + final RateLimiters rateLimiters, + final GroupSendTokenUtil groupSendTokenUtil) { this.accountsManager = accountsManager; this.rateLimiters = rateLimiters; + this.groupSendTokenUtil = groupSendTokenUtil; } @Override @@ -80,4 +92,40 @@ public class AccountsAnonymousGrpcService extends SimpleAccountsAnonymousGrpc.Ac .build()) .orElseGet(() -> LookupUsernameLinkResponse.newBuilder().setNotFound(NotFound.getDefaultInstance()).build()); } + + + @Override + public GetCapabilitiesAnonymousResponse getCapabilities(GetCapabilitiesAnonymousRequest request) { + final ServiceIdentifier targetIdentifier = + GrpcServiceIdentifierUtil.fromGrpcServiceIdentifier(request.getAccountIdentifier()); + final Optional targetAccount = accountsManager.getByServiceIdentifier(targetIdentifier); + + final boolean authorized = switch (request.getAuthenticationCase()) { + case GROUP_SEND_TOKEN -> groupSendTokenUtil.checkGroupSendToken(request.getGroupSendToken(), targetIdentifier); + case UNIDENTIFIED_ACCESS_KEY -> targetAccount + .map(a -> UnidentifiedAccessUtil.checkUnidentifiedAccess(a, request.getUnidentifiedAccessKey().toByteArray())) + .orElse(false); + default -> throw GrpcExceptions.invalidArguments("invalid authentication"); + }; + + if (!authorized) { + return GetCapabilitiesAnonymousResponse.newBuilder() + .setFailedUnidentifiedAuthorization(FailedUnidentifiedAuthorization.getDefaultInstance()) + .build(); + } + + return targetAccount.map(target -> { + final Capabilities.Builder builder = Capabilities.newBuilder(); + for (DeviceCapability capability : DeviceCapability.PUBLIC_VISIBLE_CAPABILITIES) { + if (target.hasCapability(capability)) { + builder.addCapabilities(DeviceCapabilityUtil.toGrpcDeviceCapability(capability)); + } + } + return GetCapabilitiesAnonymousResponse.newBuilder().setCapabilities(builder.build()).build(); + }) + .orElseGet(() -> GetCapabilitiesAnonymousResponse.newBuilder() + .setNotFound(NotFound.getDefaultInstance()) + .build()); + } + } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java index a360c8e24..699963ab8 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java @@ -6,6 +6,7 @@ package org.whispersystems.textsecuregcm.grpc; import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; import java.io.IOException; import java.security.SecureRandom; import java.time.Clock; @@ -15,10 +16,11 @@ import java.util.HexFormat; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; -import io.grpc.StatusRuntimeException; +import org.signal.chat.account.Capabilities; import org.signal.chat.account.ChangeNumberRequest; import org.signal.chat.account.ChangeNumberResponse; import org.signal.chat.account.ClearRegistrationLockRequest; @@ -38,6 +40,8 @@ import org.signal.chat.account.GetAccountDataReportRequest; import org.signal.chat.account.GetAccountDataReportResponse; import org.signal.chat.account.GetAccountIdentityRequest; import org.signal.chat.account.GetAccountIdentityResponse; +import org.signal.chat.account.GetCapabilitiesRequest; +import org.signal.chat.account.GetCapabilitiesResponse; import org.signal.chat.account.GetEntitlementsRequest; import org.signal.chat.account.GetEntitlementsResponse; import org.signal.chat.account.RegistrationLockFailure; @@ -58,6 +62,7 @@ import org.signal.chat.account.StaleDevices; import org.signal.chat.account.UsernameNotAvailable; import org.signal.chat.common.AccountIdentifiers; import org.signal.chat.errors.FailedPrecondition; +import org.signal.chat.errors.NotFound; import org.signal.chat.messages.SendMessageType; import org.signal.libsignal.protocol.IdentityKey; import org.signal.libsignal.protocol.InvalidKeyException; @@ -88,6 +93,7 @@ import org.whispersystems.textsecuregcm.push.MessageTooLargeException; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.ChangeNumberManager; +import org.whispersystems.textsecuregcm.storage.DeviceCapability; import org.whispersystems.textsecuregcm.storage.RegistrationRecoveryPasswordsManager; import org.whispersystems.textsecuregcm.storage.UsernameHashNotAvailableException; import org.whispersystems.textsecuregcm.storage.UsernameReservationNotFoundException; @@ -500,6 +506,18 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase { .build(); } + @Override + public GetCapabilitiesResponse getCapabilities(GetCapabilitiesRequest request) { + final Account account = getAuthenticatedAccount(); + final Capabilities.Builder builder = Capabilities.newBuilder(); + for (DeviceCapability capability : DeviceCapability.SELF_VISIBLE_CAPABILITIES) { + if (account.hasCapability(capability)) { + builder.addCapabilities(DeviceCapabilityUtil.toGrpcDeviceCapability(capability)); + } + } + return GetCapabilitiesResponse.newBuilder().setCapabilities(builder.build()).build(); + } + private static AccountIdentifiers buildAccountIdentifiers(final Account account) { final AccountIdentifiers.Builder accountIdentifiersBuilder = AccountIdentifiers.newBuilder() .addServiceIdentifiers(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new AciServiceIdentifier(account.getUuid()))) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceCapability.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceCapability.java index 28450a17b..7e8e9c376 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceCapability.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceCapability.java @@ -7,38 +7,54 @@ package org.whispersystems.textsecuregcm.storage; import java.util.Arrays; import java.util.EnumSet; +import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; public enum DeviceCapability { - STORAGE("storage", AccountCapabilityMode.ANY_DEVICE, false, false, false), - TRANSFER("transfer", AccountCapabilityMode.PRIMARY_DEVICE, false, false, false), - ATTACHMENT_BACKFILL("attachmentBackfill", AccountCapabilityMode.PRIMARY_DEVICE, false, true, false), - SPARSE_POST_QUANTUM_RATCHET("spqr", AccountCapabilityMode.ALL_DEVICES, true, true, true), - PROFILES_V2("profiles_v2", AccountCapabilityMode.ALL_DEVICES, false, true, false), - USERNAME_CHANGE_SYNC_MESSAGE("usernameChangeSyncMessage", AccountCapabilityMode.ALL_DEVICES, true, true, false); + STORAGE("storage", AccountCapabilityMode.ANY_DEVICE, AccountCapabilityVisibility.SERVER, false, false), + TRANSFER("transfer", AccountCapabilityMode.PRIMARY_DEVICE, AccountCapabilityVisibility.SERVER, false, false), + ATTACHMENT_BACKFILL("attachmentBackfill", AccountCapabilityMode.PRIMARY_DEVICE, AccountCapabilityVisibility.SELF, false, false), + SPARSE_POST_QUANTUM_RATCHET("spqr", AccountCapabilityMode.ALL_DEVICES, AccountCapabilityVisibility.PUBLIC, true, true), + PROFILES_V2("profiles_v2", AccountCapabilityMode.ALL_DEVICES, AccountCapabilityVisibility.SELF, false, false), + USERNAME_CHANGE_SYNC_MESSAGE("usernameChangeSyncMessage", AccountCapabilityMode.ALL_DEVICES, AccountCapabilityVisibility.SELF, true, false); + + public static final List PUBLIC_VISIBLE_CAPABILITIES = Arrays.stream(DeviceCapability.values()) + .filter(c -> c.accountCapabilityVisibility == AccountCapabilityVisibility.PUBLIC) + .toList(); + + public static final List SELF_VISIBLE_CAPABILITIES = Arrays.stream(DeviceCapability.values()) + .filter(c -> c.accountCapabilityVisibility == AccountCapabilityVisibility.PUBLIC + || c.accountCapabilityVisibility == AccountCapabilityVisibility.SELF) + .toList(); public enum AccountCapabilityMode { - /** - * The account will have the capability iff the primary device has the capability - */ + /// The account will have the capability iff the primary device has the capability PRIMARY_DEVICE, - /** - * The account will have the capability iff any device on the account has the capability - */ + + /// The account will have the capability iff any device on the account has the capability ANY_DEVICE, - /** - * The account will have the capability iff all devices on the account have the capability - */ + + /// The account will have the capability iff all devices on the account have the capability ALL_DEVICES, - /** - * The account always has this capability, regardless of the constituent devices' capabilities. - * This supports retiring capabilities where older clients still need the field provided. - */ + + /// The account always has this capability, regardless of the constituent devices' capabilities. + /// This supports retiring capabilities where older clients still need the field provided. ALWAYS_CAPABLE, } + public enum AccountCapabilityVisibility { + /// The capability is only visible to the server. + SERVER, + + /// The capability is visible to all devices on the account. + SELF, + + /// The capability is publicly visible. + PUBLIC + } + public static final Set CAPABILITIES_REQUIRED_FOR_NEW_DEVICES = EnumSet.copyOf(Arrays.stream(DeviceCapability.values()) .filter(DeviceCapability::requireForNewDevices) @@ -46,34 +62,32 @@ public enum DeviceCapability { private final String name; private final AccountCapabilityMode accountCapabilityMode; + private final AccountCapabilityVisibility accountCapabilityVisibility; private final boolean preventDowngrade; - private final boolean includeInProfile; private final boolean requireForNewDevices; - /** - * Create a DeviceCapability - * - * @param name The name of the device capability that clients will see - * @param accountCapabilityMode How to combine the constituent device's capabilities in the account to an overall - * account capability - * @param preventDowngrade If true, don't let linked devices join that don't have a device capability if the - * overall account has the capability. Most of the time this should only be used in - * conjunction with AccountCapabilityMode.ALL_DEVICES. - * @param includeInProfile Whether to return this capability on the account's profile. If false, the capability - * is only visible to the server. - * @param requireForNewDevices If true, prevent device creation if the new device does not have this capability - */ + /// Create a DeviceCapability + /// + /// @param name The name of the device capability that clients will see + /// @param accountCapabilityMode How to combine the constituent device's capabilities in the account to an + /// overall account capability + /// @param accountCapabilityVisibility Who should be able to view this capability + /// @param preventDowngrade If true, don't let linked devices join that don't have a device capability if + /// the overall account has the capability. Most of the time this should only be + /// used in conjunction with AccountCapabilityMode.ALL_DEVICES. + /// @param requireForNewDevices If true, prevent device creation if the new device does not have this + /// capability DeviceCapability(final String name, final AccountCapabilityMode accountCapabilityMode, + final AccountCapabilityVisibility accountCapabilityVisibility, final boolean preventDowngrade, - final boolean includeInProfile, final boolean requireForNewDevices) { this.name = name; this.accountCapabilityMode = accountCapabilityMode; this.preventDowngrade = preventDowngrade; - this.includeInProfile = includeInProfile; this.requireForNewDevices = requireForNewDevices; + this.accountCapabilityVisibility = accountCapabilityVisibility; } public String getName() { @@ -84,12 +98,17 @@ public enum DeviceCapability { return accountCapabilityMode; } + public AccountCapabilityVisibility getAccountCapabilityVisibility() { + return accountCapabilityVisibility; + } + public boolean preventDowngrade() { return preventDowngrade; } - public boolean includeInProfile() { - return includeInProfile; + public boolean includeInLegacyProfile() { + // Profiles v1 does not distinguish between self/public capabilities + return accountCapabilityVisibility != AccountCapabilityVisibility.SERVER; } public boolean requireForNewDevices() { diff --git a/service/src/main/proto/org/signal/chat/account.proto b/service/src/main/proto/org/signal/chat/account.proto index 255b2efb0..1389bf8a1 100644 --- a/service/src/main/proto/org/signal/chat/account.proto +++ b/service/src/main/proto/org/signal/chat/account.proto @@ -72,6 +72,9 @@ service Accounts { // Produces a report of non-ephemeral account data stored by the service rpc GetAccountDataReport(GetAccountDataReportRequest) returns (GetAccountDataReportResponse) {} + + // Gets the capabilities for the authenticated account. + rpc GetCapabilities(GetCapabilitiesRequest) returns (GetCapabilitiesResponse) {} } // Provides methods for looking up Signal accounts. Callers must not provide @@ -88,6 +91,10 @@ service AccountsAnonymous { // Finds the encrypted username identified by a given username link handle. rpc LookupUsernameLink(LookupUsernameLinkRequest) returns (LookupUsernameLinkResponse) {} + + // Gets the publicly-visible capabilities enabled on the account identified by + // the provided ACI. + rpc GetCapabilities(GetCapabilitiesAnonymousRequest) returns (GetCapabilitiesAnonymousResponse) {} } message GetAccountIdentityRequest { @@ -434,3 +441,38 @@ message GetAccountDataReportResponse { // A plaintext representation of the data report string text = 4; } + +message GetCapabilitiesRequest { +} + +message Capabilities { + // A list of capabilities enabled on the account. + repeated common.DeviceCapability capabilities = 1; +} + +message GetCapabilitiesResponse { + // A list of capabilities enabled on the account. + Capabilities capabilities = 1; +} + +message GetCapabilitiesAnonymousRequest { + // The ACI of the account for which to get capabilities. + common.ServiceIdentifier account_identifier = 1 [(require.present) = true, (require.identityType) = IDENTITY_TYPE_ACI]; + + oneof authentication { + // The unidentified access key for the targeted account. + bytes unidentified_access_key = 2 [(require.exactlySize) = 16]; + + // A group send endorsement token for the targeted account. + bytes group_send_token = 3 [(require.nonEmpty) = true]; + } +} + +message GetCapabilitiesAnonymousResponse { + oneof response { + // A list of capabilities enabled on the account. + Capabilities capabilities = 1; + errors.NotFound not_found = 2 [(tag.reason) = "not_found"]; + errors.FailedUnidentifiedAuthorization failed_unidentified_authorization = 3 [(tag.reason) = "failed_unidentified_authorization"]; + } +} diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcServiceTest.java index 3c69ee015..0ef8522e2 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsAnonymousGrpcServiceTest.java @@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -26,16 +27,22 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.signal.chat.account.AccountsAnonymousGrpc; +import org.signal.chat.account.Capabilities; import org.signal.chat.account.CheckAccountExistenceRequest; +import org.signal.chat.account.GetCapabilitiesAnonymousRequest; +import org.signal.chat.account.GetCapabilitiesAnonymousResponse; import org.signal.chat.account.LookupUsernameHashRequest; import org.signal.chat.account.LookupUsernameHashResponse; import org.signal.chat.account.LookupUsernameLinkRequest; import org.signal.chat.account.LookupUsernameLinkResponse; import org.signal.chat.common.IdentityType; -import org.signal.chat.errors.NotFound; import org.signal.chat.common.ServiceIdentifier; +import org.signal.chat.errors.FailedUnidentifiedAuthorization; +import org.signal.chat.errors.NotFound; +import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil; import org.whispersystems.textsecuregcm.controllers.AccountController; import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; @@ -43,6 +50,7 @@ import org.whispersystems.textsecuregcm.limits.RateLimiter; import org.whispersystems.textsecuregcm.limits.RateLimiters; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; +import org.whispersystems.textsecuregcm.storage.DeviceCapability; import org.whispersystems.textsecuregcm.util.TestRandomUtil; import org.whispersystems.textsecuregcm.util.UUIDUtil; import reactor.core.publisher.Mono; @@ -59,6 +67,9 @@ class AccountsAnonymousGrpcServiceTest extends @Mock private RateLimiter rateLimiter; + @Mock + private GroupSendTokenUtil groupSendTokenUtil; + @Override protected AccountsAnonymousGrpcService createServiceBeforeEachTest() { when(accountsManager.getByServiceIdentifier(any())) @@ -79,7 +90,7 @@ class AccountsAnonymousGrpcServiceTest extends getMockRequestAttributesInterceptor().setRequestAttributes( new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, null)); - return new AccountsAnonymousGrpcService(accountsManager, rateLimiters); + return new AccountsAnonymousGrpcService(accountsManager, rateLimiters, groupSendTokenUtil); } @Test @@ -268,4 +279,111 @@ class AccountsAnonymousGrpcServiceTest extends .build()), accountsManager); } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void getCapabilities(final boolean useGroupSendToken) { + final AciServiceIdentifier serviceIdentifier = new AciServiceIdentifier(UUID.randomUUID()); + + final Account account = mock(Account.class); + when(accountsManager.getByServiceIdentifier(serviceIdentifier)).thenReturn(Optional.of(account)); + + // should be returned since it's public + when(account.hasCapability(DeviceCapability.SPARSE_POST_QUANTUM_RATCHET)).thenReturn(true); + // should not be returned since it's self-only + when(account.hasCapability(DeviceCapability.USERNAME_CHANGE_SYNC_MESSAGE)).thenReturn(true); + + final GetCapabilitiesAnonymousRequest.Builder builder = GetCapabilitiesAnonymousRequest.newBuilder() + .setAccountIdentifier(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(serviceIdentifier)); + if (useGroupSendToken) { + when(groupSendTokenUtil.checkGroupSendToken(any(), eq(serviceIdentifier))).thenReturn(true); + builder.setGroupSendToken(ByteString.copyFrom(TestRandomUtil.nextBytes(32))); + } else { + final byte[] unidentifiedAccessKey = TestRandomUtil.nextBytes(UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH); + when(account.getUnidentifiedAccessKey()).thenReturn(Optional.of(unidentifiedAccessKey)); + builder.setUnidentifiedAccessKey(ByteString.copyFrom(unidentifiedAccessKey)); + } + final GetCapabilitiesAnonymousResponse response = unauthenticatedServiceStub().getCapabilities(builder.build()); + + assertEquals(Capabilities.newBuilder() + .addCapabilities(org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_SPARSE_POST_QUANTUM_RATCHET) + .build(), + response.getCapabilities()); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void getCapabilitiesUnauthorized(final boolean useGroupSendToken) { + final AciServiceIdentifier serviceIdentifier = new AciServiceIdentifier(UUID.randomUUID()); + + final Account account = mock(Account.class); + when(accountsManager.getByServiceIdentifier(serviceIdentifier)).thenReturn(Optional.of(account)); + + final GetCapabilitiesAnonymousRequest.Builder builder = GetCapabilitiesAnonymousRequest.newBuilder() + .setAccountIdentifier(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(serviceIdentifier)); + if (useGroupSendToken) { + when(groupSendTokenUtil.checkGroupSendToken(any(), eq(serviceIdentifier))).thenReturn(false); + builder.setGroupSendToken(ByteString.copyFrom(TestRandomUtil.nextBytes(32))); + } else { + when(account.getUnidentifiedAccessKey()) + .thenReturn(Optional.of(TestRandomUtil.nextBytes(UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH))); + builder.setUnidentifiedAccessKey(ByteString.copyFrom(TestRandomUtil.nextBytes(UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH))); + } + final GetCapabilitiesAnonymousResponse response = unauthenticatedServiceStub().getCapabilities(builder.build()); + + assertEquals(FailedUnidentifiedAuthorization.getDefaultInstance(), response.getFailedUnidentifiedAuthorization()); + } + + @Test + void getCapabilitiesNotFound() { + final AciServiceIdentifier serviceIdentifier = new AciServiceIdentifier(UUID.randomUUID()); + final byte[] groupSendToken = TestRandomUtil.nextBytes(32); + + when(accountsManager.getByServiceIdentifier(serviceIdentifier)) + .thenReturn(Optional.empty()); + when(groupSendTokenUtil.checkGroupSendToken(ByteString.copyFrom(groupSendToken), serviceIdentifier)) + .thenReturn(true); + + final GetCapabilitiesAnonymousResponse response = + unauthenticatedServiceStub().getCapabilities(GetCapabilitiesAnonymousRequest.newBuilder() + .setAccountIdentifier(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(serviceIdentifier)) + .setGroupSendToken(ByteString.copyFrom(groupSendToken)) + .build()); + + assertEquals(NotFound.getDefaultInstance(), response.getNotFound()); + } + + private static Stream getCapabilitiesIllegalRequest() { + final ServiceIdentifier aci = + GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new AciServiceIdentifier(UUID.randomUUID())); + + return Stream.of( + Arguments.argumentSet("no aci", GetCapabilitiesAnonymousRequest.newBuilder() + .setUnidentifiedAccessKey( + ByteString.copyFrom(new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH])) + .build()), + + Arguments.argumentSet("wrong uak length", GetCapabilitiesAnonymousRequest.newBuilder() + .setAccountIdentifier(aci) + .setUnidentifiedAccessKey(ByteString.copyFrom(new byte[15])) + .build()), + + Arguments.argumentSet("invalid gse", GetCapabilitiesAnonymousRequest.newBuilder() + .setAccountIdentifier(aci) + .setGroupSendToken(ByteString.EMPTY) + .build()), + + Arguments.argumentSet("no auth", GetCapabilitiesAnonymousRequest.newBuilder() + .setAccountIdentifier( + GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new AciServiceIdentifier(UUID.randomUUID()))) + .build()) + ); + } + + @ParameterizedTest + @MethodSource + void getCapabilitiesIllegalRequest(final GetCapabilitiesAnonymousRequest request) { + GrpcTestUtils.assertStatusException(Status.INVALID_ARGUMENT, + () -> unauthenticatedServiceStub().getCapabilities(request)); + } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java index be86c686c..b0113b059 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java @@ -40,6 +40,7 @@ import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.signal.chat.account.AccountsGrpc; +import org.signal.chat.account.Capabilities; import org.signal.chat.account.ChangeNumberRequest; import org.signal.chat.account.ChangeNumberResponse; import org.signal.chat.account.ClearRegistrationLockRequest; @@ -56,6 +57,8 @@ import org.signal.chat.account.GetAccountDataReportRequest; import org.signal.chat.account.GetAccountDataReportResponse; import org.signal.chat.account.GetAccountIdentityRequest; import org.signal.chat.account.GetAccountIdentityResponse; +import org.signal.chat.account.GetCapabilitiesRequest; +import org.signal.chat.account.GetCapabilitiesResponse; import org.signal.chat.account.GetEntitlementsRequest; import org.signal.chat.account.GetEntitlementsResponse; import org.signal.chat.account.RegistrationLockFailure; @@ -105,14 +108,15 @@ import org.whispersystems.textsecuregcm.storage.AccountBadge; import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.ChangeNumberManager; import org.whispersystems.textsecuregcm.storage.Device; +import org.whispersystems.textsecuregcm.storage.DeviceCapability; import org.whispersystems.textsecuregcm.storage.KeyIdUtil; import org.whispersystems.textsecuregcm.storage.RegistrationRecoveryPasswordsManager; import org.whispersystems.textsecuregcm.storage.UsernameHashNotAvailableException; import org.whispersystems.textsecuregcm.storage.UsernameReservationNotFoundException; import org.whispersystems.textsecuregcm.tests.util.AccountsHelper; -import org.whispersystems.textsecuregcm.util.TestClock; import org.whispersystems.textsecuregcm.tests.util.AccountsTestHelper; import org.whispersystems.textsecuregcm.tests.util.KeysHelper; +import org.whispersystems.textsecuregcm.util.TestClock; import org.whispersystems.textsecuregcm.util.TestRandomUtil; import org.whispersystems.textsecuregcm.util.UUIDUtil; import org.whispersystems.textsecuregcm.util.UsernameHashZkProofVerifier; @@ -1039,4 +1043,25 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest