From 674d03ef0c927d43f3624c51ac721401a3f9f759 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Tue, 4 Aug 2026 16:41:49 -0400 Subject: [PATCH] Add issueAuthCredentialZkcWithoutPni to GroupCredentials issuance --- .../auth/CertificateGenerator.java | 3 +- .../controllers/CertificateController.java | 15 +- .../grpc/CredentialsGrpcService.java | 22 ++- .../textsecuregcm/storage/Account.java | 14 ++ .../proto/org/signal/chat/credentials.proto | 2 +- .../auth/CertificateGeneratorTest.java | 5 +- .../CertificateControllerTest.java | 140 ++++++++---------- .../grpc/CredentialsGrpcServiceTest.java | 64 ++++++-- .../textsecuregcm/storage/AccountTest.java | 13 ++ .../textsecuregcm/tests/util/AuthHelper.java | 6 + 10 files changed, 174 insertions(+), 110 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/auth/CertificateGenerator.java b/service/src/main/java/org/whispersystems/textsecuregcm/auth/CertificateGenerator.java index caa1cd0a2..103e31874 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/auth/CertificateGenerator.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/auth/CertificateGenerator.java @@ -11,7 +11,6 @@ import java.util.concurrent.TimeUnit; import org.signal.libsignal.protocol.ecc.ECPrivateKey; import org.whispersystems.textsecuregcm.entities.MessageProtos.SenderCertificate; import org.whispersystems.textsecuregcm.entities.MessageProtos.ServerCertificate; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.util.UUIDUtil; @@ -42,7 +41,7 @@ public class CertificateGenerator { SenderCertificate.Certificate.Builder builder = SenderCertificate.Certificate.newBuilder() .setSenderDevice(Math.toIntExact(deviceId)) .setExpires(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(expiresDays)) - .setIdentityKey(ByteString.copyFrom(account.getIdentityKey(IdentityType.ACI).serialize())) + .setIdentityKey(ByteString.copyFrom(account.getAccountIdentityKey().serialize())) .setSenderUuid(UUIDUtil.toByteString(account.getAccountIdentifier())); if (includeE164) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/CertificateController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/CertificateController.java index 9a9234a7b..522c4c201 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/CertificateController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/CertificateController.java @@ -26,6 +26,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.Optional; import javax.annotation.Nonnull; import org.signal.libsignal.protocol.ServiceId; import org.signal.libsignal.zkgroup.GenericServerSecretParams; @@ -37,7 +38,6 @@ import org.whispersystems.textsecuregcm.auth.CertificateGenerator; import org.whispersystems.textsecuregcm.auth.RedemptionRange; import org.whispersystems.textsecuregcm.entities.DeliveryCertificate; import org.whispersystems.textsecuregcm.entities.GroupCredentials; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; @@ -112,11 +112,16 @@ public class CertificateController { final List credentials = new ArrayList<>(); final List callLinkAuthCredentials = new ArrayList<>(); - final ServiceId.Aci aci = new ServiceId.Aci(account.getIdentifier(IdentityType.ACI)); - final ServiceId.Pni pni = new ServiceId.Pni(account.getIdentifier(IdentityType.PNI)); + final ServiceId.Aci aci = new ServiceId.Aci(account.getAccountIdentifier()); + final Optional maybePni = account.getPhoneNumberIdentifierOptional().map(ServiceId.Pni::new); for (Instant redemption : redemptionRange) { - AuthCredentialWithPniResponse authCredentialWithPni = serverZkAuthOperations.issueAuthCredentialWithPniZkc(aci, pni, redemption); + final AuthCredentialWithPniResponse authCredentialWithPni = + maybePni.map(pni -> serverZkAuthOperations.issueAuthCredentialWithPniZkc(aci, pni, redemption)) + .orElseGet(() -> serverZkAuthOperations.issueAuthCredentialZkcWithoutPni(aci, + account.getAuthCredentialSalt() + .orElseThrow(() -> new IllegalStateException("account without PNI must have auth credential salt")), + redemption)); credentials.add(new GroupCredentials.GroupCredential( authCredentialWithPni.serialize(), (int) redemption.getEpochSecond())); @@ -126,6 +131,6 @@ public class CertificateController { redemption.getEpochSecond())); } - return new GroupCredentials(credentials, callLinkAuthCredentials, pni.getRawUUID()); + return new GroupCredentials(credentials, callLinkAuthCredentials, maybePni.map(ServiceId.Pni::getRawUUID).orElse(null)); } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcService.java index e3db95581..768833935 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcService.java @@ -10,6 +10,7 @@ import java.time.Clock; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Map; +import java.util.Optional; import java.util.UUID; import org.signal.chat.credentials.ExternalServiceType; import org.signal.chat.credentials.GetCreateCallLinkCredentialsRequest; @@ -24,6 +25,7 @@ import org.signal.chat.credentials.SimpleCredentialsGrpc; import org.signal.libsignal.protocol.ServiceId; import org.signal.libsignal.zkgroup.GenericServerSecretParams; import org.signal.libsignal.zkgroup.InvalidInputException; +import org.signal.libsignal.zkgroup.auth.AuthCredentialWithPniResponse; import org.signal.libsignal.zkgroup.auth.ServerZkAuthOperations; import org.signal.libsignal.zkgroup.calllinks.CallLinkAuthCredentialResponse; import org.signal.libsignal.zkgroup.calllinks.CreateCallLinkCredentialRequest; @@ -34,7 +36,6 @@ import org.whispersystems.textsecuregcm.auth.RedemptionRange; import org.whispersystems.textsecuregcm.auth.grpc.AuthenticatedDevice; import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil; import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.limits.RateLimiters; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; @@ -121,17 +122,26 @@ public class CredentialsGrpcService extends SimpleCredentialsGrpc.CredentialsImp accountsManager.getByAccountIdentifier(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier()) .orElseThrow(() -> GrpcExceptions.invalidCredentials("invalid credentials")); - final ServiceId.Aci aci = new ServiceId.Aci(account.getIdentifier(IdentityType.ACI)); - final ServiceId.Pni pni = new ServiceId.Pni(account.getIdentifier(IdentityType.PNI)); + final ServiceId.Aci aci = new ServiceId.Aci(account.getAccountIdentifier()); + final Optional maybePni = account.getPhoneNumberIdentifierOptional().map(ServiceId.Pni::new); - final GetGroupCredentialsResponse.Builder responseBuilder = GetGroupCredentialsResponse.newBuilder() - .setPni(UUIDUtil.toByteString(pni.getRawUUID())); + final GetGroupCredentialsResponse.Builder responseBuilder = GetGroupCredentialsResponse.newBuilder(); + + maybePni.ifPresent(pni -> responseBuilder.setPni(UUIDUtil.toByteString(pni.getRawUUID()))); for (final Instant redemption : redemptionRange) { + final AuthCredentialWithPniResponse authCredentialWithPniResponse = + maybePni.map(pni -> + serverZkAuthOperations.issueAuthCredentialWithPniZkc(aci, pni, redemption)) + .orElseGet(() -> serverZkAuthOperations.issueAuthCredentialZkcWithoutPni(aci, + account.getAuthCredentialSalt() + .orElseThrow( + () -> new IllegalStateException("account without PNI must have auth credential salt")), + redemption)); responseBuilder.addGroupCredentials(GetGroupCredentialsResponse.CredentialAndRedemptionTime.newBuilder() .setRedemptionTimeSeconds(redemption.getEpochSecond()) .setCredential(ByteString.copyFrom( - serverZkAuthOperations.issueAuthCredentialWithPniZkc(aci, pni, redemption).serialize())) + authCredentialWithPniResponse.serialize())) .build()); responseBuilder.addCallLinkAuthCredentials(GetGroupCredentialsResponse.CredentialAndRedemptionTime.newBuilder() diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java index f78da0a09..8eb3d89e7 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java @@ -153,6 +153,10 @@ public class Account { @Nullable private String accountRecoveryPasswordHash; + @JsonProperty("acs") + @Nullable + private byte[] authCredentialSalt; + @JsonIgnore private boolean stale; @@ -707,6 +711,16 @@ public class Account { this.accountRecoveryPasswordHash = saltedAccountRecoveryPasswordHash.hash(); } + public Optional getAuthCredentialSalt() { + requireNotStale(); + return Optional.ofNullable(authCredentialSalt); + } + + public void setAuthCredentialSalt(final byte[] authCredentialSalt) { + requireNotStale(); + this.authCredentialSalt = authCredentialSalt; + } + public void markStale() { stale = true; } diff --git a/service/src/main/proto/org/signal/chat/credentials.proto b/service/src/main/proto/org/signal/chat/credentials.proto index a1568c8b8..fa9dce7de 100644 --- a/service/src/main/proto/org/signal/chat/credentials.proto +++ b/service/src/main/proto/org/signal/chat/credentials.proto @@ -146,7 +146,7 @@ message GetGroupCredentialsResponse { repeated CredentialAndRedemptionTime call_link_auth_credentials = 2; // The phone number identifier for which the included credentials were - // generated + // generated. Empty if the account does not have a phone number. bytes pni = 3; } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/CertificateGeneratorTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/CertificateGeneratorTest.java index e496665d5..1dc98c0f5 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/CertificateGeneratorTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/CertificateGeneratorTest.java @@ -27,7 +27,6 @@ import org.signal.libsignal.protocol.ecc.ECPrivateKey; import org.signal.libsignal.protocol.ecc.ECPublicKey; import org.whispersystems.textsecuregcm.entities.MessageProtos.SenderCertificate; import org.whispersystems.textsecuregcm.entities.MessageProtos.ServerCertificate; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.util.UUIDUtil; @@ -61,7 +60,7 @@ class CertificateGeneratorTest { final CertificateGenerator certificateGenerator = new CertificateGenerator( SIGNING_CERTIFICATE_DATA, SIGNING_KEY, 1, embedSigner); - when(account.getIdentityKey(IdentityType.ACI)).thenReturn(IDENTITY_KEY); + when(account.getAccountIdentityKey()).thenReturn(IDENTITY_KEY); when(account.getAccountIdentifier()).thenReturn(ACI); when(account.getNumberOptional()).thenReturn(Optional.of(E164)); @@ -101,7 +100,7 @@ class CertificateGeneratorTest { final CertificateGenerator certificateGenerator = new CertificateGenerator( SIGNING_CERTIFICATE_DATA, SIGNING_KEY, 1, embedSigner); - when(account.getIdentityKey(IdentityType.ACI)).thenReturn(IDENTITY_KEY); + when(account.getAccountIdentityKey()).thenReturn(IDENTITY_KEY); when(account.getAccountIdentifier()).thenReturn(ACI); when(account.getNumberOptional()).thenReturn(Optional.empty()); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/CertificateControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/CertificateControllerTest.java index b7a674add..1c1dfb8f9 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/CertificateControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/CertificateControllerTest.java @@ -10,7 +10,6 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import io.dropwizard.auth.AuthValueFactoryProvider; import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; @@ -26,10 +25,10 @@ import java.util.Arrays; import java.util.Base64; import java.util.Collection; import java.util.List; -import java.util.Optional; +import java.util.UUID; +import javax.annotation.Nullable; import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -52,7 +51,6 @@ import org.whispersystems.textsecuregcm.entities.DeliveryCertificate; import org.whispersystems.textsecuregcm.entities.GroupCredentials; import org.whispersystems.textsecuregcm.entities.MessageProtos.SenderCertificate; import org.whispersystems.textsecuregcm.entities.MessageProtos.ServerCertificate; -import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.tests.util.AuthHelper; import org.whispersystems.textsecuregcm.util.HeaderUtils; @@ -98,14 +96,9 @@ class CertificateControllerTest { .addProvider(new AuthValueFactoryProvider.Binder<>(AuthenticatedDevice.class)) .setMapper(SystemMapper.jsonMapper()) .setTestContainerFactory(new GrizzlyWebTestContainerFactory()) - .addResource(new CertificateController(ACCOUNTS_MANAGER, CERTIFICATE_GENERATOR, SERVER_ZK_AUTH_OPERATIONS, genericServerSecretParams, clock)) + .addResource(new CertificateController(AuthHelper.ACCOUNTS_MANAGER, CERTIFICATE_GENERATOR, SERVER_ZK_AUTH_OPERATIONS, genericServerSecretParams, clock)) .build(); - @BeforeEach - void setUp() { - when(ACCOUNTS_MANAGER.getByAccountIdentifier(AuthHelper.VALID_UUID)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT)); - } - @Test void testSigningCertificate() throws Exception { final ServerCertificate fullCertificate = ServerCertificate.parseFrom(SIGNING_CERTIFICATE_DATA); @@ -227,62 +220,25 @@ class CertificateControllerTest { @Test void testValidCertificateAccountHasNoNumber() { - final Account numberlessAccount = mock(Account.class); - when(numberlessAccount.getNumberOptional()).thenReturn(Optional.empty()); - - when(ACCOUNTS_MANAGER.getByAccountIdentifier(AuthHelper.VALID_UUID)) - .thenReturn(Optional.of(numberlessAccount)); - final Response response = resources.getJerseyTest() .target("/v1/certificate/delivery") .queryParam("includeUuid", "true") .queryParam("includeE164", "true") .request() - .header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)) + .header("Authorization", AuthHelper.getAuthHeader(AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD)) .get(); assertEquals(400, response.getStatus()); } - @Test - void testGetSingleGroupCredentialWithPniAsServiceId() { - final Instant startOfDay = clock.instant().truncatedTo(ChronoUnit.DAYS); + @ParameterizedTest + @MethodSource("getGroupCredentials") + void testGetSingleGroupCredential(final boolean accountHasNumber, + final UUID aci, + final String password, + @Nullable UUID pni, + @Nullable byte[] authCredentialSalt) { - final GroupCredentials credentials = resources.getJerseyTest() - .target("/v1/certificate/auth/group") - .queryParam("redemptionStartSeconds", startOfDay.getEpochSecond()) - .queryParam("redemptionEndSeconds", startOfDay.getEpochSecond()) - .queryParam("pniAsServiceId", true) - .request() - .header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)) - .get(GroupCredentials.class); - - assertEquals(1, credentials.credentials().size()); - assertEquals(1, credentials.callLinkAuthCredentials().size()); - - assertEquals(AuthHelper.VALID_PNI, credentials.pni()); - assertEquals(startOfDay.getEpochSecond(), credentials.credentials().getFirst().redemptionTime()); - assertEquals(startOfDay.getEpochSecond(), credentials.callLinkAuthCredentials().getFirst().redemptionTime()); - - final ClientZkAuthOperations clientZkAuthOperations = - new ClientZkAuthOperations(SERVER_SECRET_PARAMS.getPublicParams()); - - assertDoesNotThrow(() -> { - clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( - new ServiceId.Aci(AuthHelper.VALID_UUID), - new ServiceId.Pni(AuthHelper.VALID_PNI), - (int) startOfDay.getEpochSecond(), - new AuthCredentialWithPniResponse(credentials.credentials().getFirst().credential())); - }); - - assertDoesNotThrow(() -> { - new CallLinkAuthCredentialResponse(credentials.callLinkAuthCredentials().getFirst().credential()) - .receive(new ServiceId.Aci(AuthHelper.VALID_UUID), startOfDay, genericServerSecretParams.getPublicParams()); - }); - } - - @Test - void testGetSingleGroupCredential() { final Instant startOfDay = clock.instant().truncatedTo(ChronoUnit.DAYS); final GroupCredentials credentials = resources.getJerseyTest() @@ -290,47 +246,67 @@ class CertificateControllerTest { .queryParam("redemptionStartSeconds", startOfDay.getEpochSecond()) .queryParam("redemptionEndSeconds", startOfDay.getEpochSecond()) .request() - .header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)) + .header("Authorization", AuthHelper.getAuthHeader(aci, password)) .get(GroupCredentials.class); assertEquals(1, credentials.credentials().size()); assertEquals(1, credentials.callLinkAuthCredentials().size()); - assertEquals(AuthHelper.VALID_PNI, credentials.pni()); + assertEquals(pni, credentials.pni()); assertEquals(startOfDay.getEpochSecond(), credentials.credentials().getFirst().redemptionTime()); assertEquals(startOfDay.getEpochSecond(), credentials.callLinkAuthCredentials().getFirst().redemptionTime()); final ClientZkAuthOperations clientZkAuthOperations = new ClientZkAuthOperations(SERVER_SECRET_PARAMS.getPublicParams()); - assertDoesNotThrow(() -> { - clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( - new ServiceId.Aci(AuthHelper.VALID_UUID), - new ServiceId.Pni(AuthHelper.VALID_PNI), - (int) startOfDay.getEpochSecond(), - new AuthCredentialWithPniResponse(credentials.credentials().getFirst().credential())); - }); + if (accountHasNumber) { + assertDoesNotThrow(() -> { + clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( + new ServiceId.Aci(aci), + new ServiceId.Pni(pni), + (int) startOfDay.getEpochSecond(), + new AuthCredentialWithPniResponse(credentials.credentials().getFirst().credential())); + }); + } else { + assertDoesNotThrow(() -> clientZkAuthOperations.receiveAuthCredentialWithoutPni( + new ServiceId.Aci(aci), + authCredentialSalt, + startOfDay.getEpochSecond(), + new AuthCredentialWithPniResponse(credentials.credentials().getFirst().credential()))); + + } assertDoesNotThrow(() -> { new CallLinkAuthCredentialResponse(credentials.callLinkAuthCredentials().getFirst().credential()) - .receive(new ServiceId.Aci(AuthHelper.VALID_UUID), startOfDay, genericServerSecretParams.getPublicParams()); + .receive(new ServiceId.Aci(aci), startOfDay, genericServerSecretParams.getPublicParams()); }); } - @Test - void testGetWeekLongGroupCredentials() { + static Collection getGroupCredentials() { + return List.of( + Arguments.argumentSet("account with phone number", true, AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD, AuthHelper.VALID_PNI, null), + Arguments.argumentSet("account without phone number", false, AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD, null, AuthHelper.NUMBERLESS_AUTH_CREDENTIAL_SALT) + ); + } + + @ParameterizedTest + @MethodSource("getGroupCredentials") + void testGetWeekLongGroupCredentials(final boolean accountHasNumber, + final UUID aci, + final String password, + @Nullable UUID pni, + @Nullable byte[] authCredentialSalt) { final Instant startOfDay = clock.instant().truncatedTo(ChronoUnit.DAYS); final GroupCredentials credentials = resources.getJerseyTest() .target("/v1/certificate/auth/group") .queryParam("redemptionStartSeconds", startOfDay.getEpochSecond()) .queryParam("redemptionEndSeconds", startOfDay.plus(Duration.ofDays(7)).getEpochSecond()) - .queryParam("pniAsServiceId", true) .request() - .header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)) + .header("Authorization", AuthHelper.getAuthHeader(aci, password)) .get(GroupCredentials.class); - assertEquals(AuthHelper.VALID_PNI, credentials.pni()); + assertEquals(pni, credentials.pni()); assertEquals(8, credentials.credentials().size()); assertEquals(8, credentials.callLinkAuthCredentials().size()); @@ -344,17 +320,25 @@ class CertificateControllerTest { final int index = i; - assertDoesNotThrow(() -> { - clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( - new ServiceId.Aci(AuthHelper.VALID_UUID), - new ServiceId.Pni(AuthHelper.VALID_PNI), - redemptionTime.getEpochSecond(), - new AuthCredentialWithPniResponse(credentials.credentials().get(index).credential())); - }); + if (accountHasNumber) { + assertDoesNotThrow(() -> { + clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( + new ServiceId.Aci(aci), + new ServiceId.Pni(pni), + redemptionTime.getEpochSecond(), + new AuthCredentialWithPniResponse(credentials.credentials().get(index).credential())); + }); + } else { + assertDoesNotThrow(() -> clientZkAuthOperations.receiveAuthCredentialWithoutPni( + new ServiceId.Aci(aci), + authCredentialSalt, + startOfDay.getEpochSecond(), + new AuthCredentialWithPniResponse(credentials.credentials().getFirst().credential()))); + } assertDoesNotThrow(() -> { new CallLinkAuthCredentialResponse(credentials.callLinkAuthCredentials().get(index).credential()) - .receive(new ServiceId.Aci(AuthHelper.VALID_UUID), redemptionTime, genericServerSecretParams.getPublicParams()); + .receive(new ServiceId.Aci(aci), redemptionTime, genericServerSecretParams.getPublicParams()); }); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcServiceTest.java index eee4799a6..345ba5980 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/CredentialsGrpcServiceTest.java @@ -43,6 +43,7 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.EnumSource; 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.mockito.Mockito; import org.signal.chat.credentials.CredentialsGrpc; @@ -73,7 +74,6 @@ import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsGenerator import org.whispersystems.textsecuregcm.controllers.CertificateController; import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.entities.MessageProtos; -import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.limits.RateLimiter; import org.whispersystems.textsecuregcm.limits.RateLimiters; import org.whispersystems.textsecuregcm.storage.Account; @@ -151,9 +151,8 @@ public class CredentialsGrpcServiceTest void setUp() { when(authenticatedAccount.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI); when(authenticatedAccount.getNumberOptional()).thenReturn(Optional.of(PHONE_NUMBER)); - when(authenticatedAccount.getIdentifier(IdentityType.ACI)).thenReturn(AUTHENTICATED_ACI); - when(authenticatedAccount.getIdentifier(IdentityType.PNI)).thenReturn(AUTHENTICATED_PNI); - when(authenticatedAccount.getIdentityKey(IdentityType.ACI)) + when(authenticatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(AUTHENTICATED_PNI)); + when(authenticatedAccount.getAccountIdentityKey()) .thenReturn(new IdentityKey(IDENTITY_KEY_PAIR.getPublicKey())); when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI)).thenReturn(Optional.of(authenticatedAccount)); @@ -208,7 +207,7 @@ public class CredentialsGrpcServiceTest @ParameterizedTest @ValueSource(ints = { -1, 0, 1000 }) - public void testUnrecognizedService(final int externalServiceTypeValue) throws Exception { + public void testUnrecognizedService(final int externalServiceTypeValue) { assertStatusException(Status.INVALID_ARGUMENT, () -> authenticatedServiceStub().getExternalServiceCredentials( GetExternalServiceCredentialsRequest.newBuilder() .setExternalServiceValue(externalServiceTypeValue) @@ -216,7 +215,7 @@ public class CredentialsGrpcServiceTest } @Test - public void testInvalidRequest() throws Exception { + public void testInvalidRequest() { assertStatusException(Status.INVALID_ARGUMENT, () -> authenticatedServiceStub().getExternalServiceCredentials( GetExternalServiceCredentialsRequest.newBuilder() .build())); @@ -244,7 +243,7 @@ public class CredentialsGrpcServiceTest */ @ParameterizedTest @EnumSource(mode = EnumSource.Mode.EXCLUDE, names = { "UNRECOGNIZED", "EXTERNAL_SERVICE_TYPE_UNSPECIFIED" }) - public void testHaveExternalServiceDefinitionForServiceTypes(final ExternalServiceType externalServiceType) throws Exception { + public void testHaveExternalServiceDefinitionForServiceTypes(final ExternalServiceType externalServiceType) { assertTrue( Arrays.stream(ExternalServiceDefinitions.values()).anyMatch(v -> v.externalService() == externalServiceType), "`ExternalServiceDefinitions` enum entry is missing for the `%s` value of `ExternalServiceType`".formatted(externalServiceType) @@ -302,9 +301,31 @@ public class CredentialsGrpcServiceTest checkDeliveryCertificate(response.getCertificateWithoutE164().toByteArray(), false); } - @ParameterizedTest + @CartesianTest @ValueSource(ints = {0, 1, 2, 3, 4, 5, 6, 7}) - void getGroupCredentials(final int redemptionEndOffsetDays) { + void getGroupCredentials( + @CartesianTest.Values(ints = {0, 1, 2, 3, 4, 5, 6, 7}) final int redemptionEndOffsetDays, + @CartesianTest.Values(booleans = {true, false}) final boolean accountHasPhoneNumber + ) { + + final Account account = mock(Account.class); + when(account.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI); + when(account.getAccountIdentityKey()) + .thenReturn(new IdentityKey(IDENTITY_KEY_PAIR.getPublicKey())); + when(account.getPhoneNumberIdentifierOptional()).thenReturn( + Optional.ofNullable(accountHasPhoneNumber ? AUTHENTICATED_PNI : null)); + + final byte[] authCredentialSalt; + if (accountHasPhoneNumber) { + authCredentialSalt = null; + } else { + authCredentialSalt = TestRandomUtil.nextBytes(16); + } + when(account.getAuthCredentialSalt()).thenReturn(Optional.ofNullable(authCredentialSalt)); + + when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI)).thenReturn(Optional.of(account)); + + final Instant startOfDay = CLOCK.instant().truncatedTo(ChronoUnit.DAYS); final GetGroupCredentialsResponse response = @@ -313,10 +334,15 @@ public class CredentialsGrpcServiceTest .setRedemptionEndSeconds(startOfDay.plus(Duration.ofDays(redemptionEndOffsetDays)).getEpochSecond()) .build()); - assertEquals(AUTHENTICATED_PNI, UUIDUtil.fromByteString(response.getPni())); assertEquals(redemptionEndOffsetDays + 1, response.getGroupCredentialsCount()); assertEquals(redemptionEndOffsetDays + 1, response.getCallLinkAuthCredentialsCount()); + if (accountHasPhoneNumber) { + assertEquals(AUTHENTICATED_PNI, UUIDUtil.fromByteString(response.getPni())); + } else { + assertTrue(response.getPni().isEmpty()); + } + final ClientZkAuthOperations clientZkAuthOperations = new ClientZkAuthOperations(SERVER_SECRET_PARAMS.getPublicParams()); @@ -327,13 +353,21 @@ public class CredentialsGrpcServiceTest final int index = i; - assertDoesNotThrow(() -> { - clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( + if (accountHasPhoneNumber) { + assertDoesNotThrow(() -> { + clientZkAuthOperations.receiveAuthCredentialWithPniAsServiceId( + new ServiceId.Aci(AUTHENTICATED_ACI), + new ServiceId.Pni(AUTHENTICATED_PNI), + redemptionTime.getEpochSecond(), + new AuthCredentialWithPniResponse(response.getGroupCredentials(index).getCredential().toByteArray())); + }); + } else { + assertDoesNotThrow(() -> clientZkAuthOperations.receiveAuthCredentialWithoutPni( new ServiceId.Aci(AUTHENTICATED_ACI), - new ServiceId.Pni(AUTHENTICATED_PNI), + authCredentialSalt, redemptionTime.getEpochSecond(), - new AuthCredentialWithPniResponse(response.getGroupCredentials(index).getCredential().toByteArray())); - }); + new AuthCredentialWithPniResponse(response.getGroupCredentials(index).getCredential().toByteArray()))); + } assertDoesNotThrow(() -> { new CallLinkAuthCredentialResponse(response.getCallLinkAuthCredentials(index).getCredential().toByteArray()) diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java index 08d795a9f..8076b3daf 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java @@ -245,6 +245,19 @@ class AccountTest { assertThat(deserialized.getCurrentProfileVersion()).isPresent().hasValue(version); } + @Test + public void testAuthCredentialSaltRoundTrip() throws Exception { + final Account account = AccountsHelper.generateTestAccount("+12345678901", UUID.randomUUID(), UUID.randomUUID(), List.of(createDevice(Device.PRIMARY_ID)), new byte[0]); + assertThat(account.getAuthCredentialSalt()).isEmpty(); + + final byte[] authCredentialSalt = TestRandomUtil.nextBytes(16); + account.setAuthCredentialSalt(authCredentialSalt); + + final String json = SystemMapper.jsonMapper().writeValueAsString(account); + final Account deserialized = SystemMapper.jsonMapper().readValue(json, Account.class); + assertThat(deserialized.getAuthCredentialSalt()).hasValue(authCredentialSalt); + } + @Test public void testProfileVersionDeserialization() throws Exception { final byte[] version = TestRandomUtil.nextBytes(32); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/tests/util/AuthHelper.java b/service/src/test/java/org/whispersystems/textsecuregcm/tests/util/AuthHelper.java index ebfcfb630..2c2794399 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/tests/util/AuthHelper.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/tests/util/AuthHelper.java @@ -58,6 +58,7 @@ import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.Device; import org.whispersystems.textsecuregcm.util.HeaderUtils; +import org.whispersystems.textsecuregcm.util.TestRandomUtil; public class AuthHelper { // Static seed to ensure reproducible tests. @@ -91,6 +92,7 @@ public class AuthHelper { public static final UUID NUMBERLESS_UUID = UUID.randomUUID(); public static final String NUMBERLESS_PASSWORD = "numberless"; + public static final byte[] NUMBERLESS_AUTH_CREDENTIAL_SALT = TestRandomUtil.nextBytes(16); public static final ECKeyPair VALID_IDENTITY_KEY_PAIR = ECKeyPair.generate(); public static final IdentityKey VALID_IDENTITY = new IdentityKey(VALID_IDENTITY_KEY_PAIR.getPublicKey()); @@ -208,7 +210,9 @@ public class AuthHelper { when(VALID_ACCOUNT_3.isIdentifiedBy(new PniServiceIdentifier(VALID_PNI_3))).thenReturn(true); when(VALID_ACCOUNT.getIdentityKey(IdentityType.ACI)).thenReturn(VALID_IDENTITY); + when(VALID_ACCOUNT.getAccountIdentityKey()).thenReturn(VALID_IDENTITY); when(VALID_ACCOUNT.getIdentityKey(IdentityType.PNI)).thenReturn(VALID_PNI_IDENTITY); + when(VALID_ACCOUNT.getPhoneNumberIdentityKey()).thenReturn(Optional.of(VALID_PNI_IDENTITY)); when(NUMBERLESS_CREDENTIALS.verify(NUMBERLESS_PASSWORD)).thenReturn(true); when(NUMBERLESS_DEVICE.getAuthTokenHash()).thenReturn(NUMBERLESS_CREDENTIALS); @@ -221,8 +225,10 @@ public class AuthHelper { when(NUMBERLESS_ACCOUNT.isIdentifiedBy(new AciServiceIdentifier(NUMBERLESS_UUID))).thenReturn(true); when(NUMBERLESS_ACCOUNT.isDiscoverableByPhoneNumber()).thenReturn(false); when(NUMBERLESS_ACCOUNT.getIdentityKey(IdentityType.ACI)).thenReturn(VALID_IDENTITY); + when(NUMBERLESS_ACCOUNT.getAccountIdentityKey()).thenReturn(VALID_IDENTITY); when(NUMBERLESS_ACCOUNT.getNumberOptional()).thenReturn(Optional.empty()); when(NUMBERLESS_ACCOUNT.getPhoneNumberIdentifierOptional()).thenReturn(Optional.empty()); + when(NUMBERLESS_ACCOUNT.getAuthCredentialSalt()).thenReturn(Optional.of(NUMBERLESS_AUTH_CREDENTIAL_SALT)); doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getNumber(); doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getPhoneNumberIdentifier(); doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getIdentityKey(IdentityType.PNI);