diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcService.java index 47ad5972c..44c72c9ef 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcService.java @@ -22,6 +22,7 @@ import org.signal.chat.keys.SimpleKeysAnonymousGrpc; import org.signal.libsignal.protocol.IdentityKey; import org.signal.libsignal.zkgroup.ServerSecretParams; import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil; +import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.identity.ServiceIdentifier; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; @@ -48,6 +49,11 @@ public class KeysAnonymousGrpcService extends SimpleKeysAnonymousGrpc.KeysAnonym final ServiceIdentifier serviceIdentifier = GrpcServiceIdentifierUtil.fromGrpcServiceIdentifier(request.getRequest().getTargetIdentifier()); + if (serviceIdentifier.identityType() == IdentityType.PNI + && request.getAuthorizationCase() != GetPreKeysAnonymousRequest.AuthorizationCase.GROUP_SEND_TOKEN) { + throw GrpcExceptions.fieldViolation("authorization", "group send token is required for PNI targets"); + } + final byte deviceId = request.getRequest().hasDeviceId() ? DeviceIdUtil.validate(request.getRequest().getDeviceId()) : KeysGrpcHelper.ALL_DEVICES; diff --git a/service/src/main/proto/org/signal/chat/keys.proto b/service/src/main/proto/org/signal/chat/keys.proto index ca79c7c28..603d5d89b 100644 --- a/service/src/main/proto/org/signal/chat/keys.proto +++ b/service/src/main/proto/org/signal/chat/keys.proto @@ -110,6 +110,7 @@ message GetPreKeysRequest { message GetPreKeysAnonymousRequest { // The request to retrieve pre-keys for a specific account/device(s). + // If `request.target_identifier` is a PNI, `authorization` must be a group send token. GetPreKeysRequest request = 1; // A means to authorize the request. diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcServiceTest.java index 2482a2234..b8dedae73 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcServiceTest.java @@ -37,6 +37,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.junitpioneer.jupiter.cartesian.CartesianTest; import org.mockito.Mock; import org.signal.chat.common.EcPreKey; @@ -61,6 +63,7 @@ import org.whispersystems.textsecuregcm.entities.ECPreKey; import org.whispersystems.textsecuregcm.entities.ECSignedPreKey; import org.whispersystems.textsecuregcm.entities.KEMSignedPreKey; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; +import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.identity.PniServiceIdentifier; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; @@ -141,25 +144,33 @@ class KeysAnonymousGrpcServiceTest extends SimpleBaseGrpcTest new AciServiceIdentifier(aci); + case PNI -> new PniServiceIdentifier(UUID.randomUUID()); + }; final byte[] unidentifiedAccessKey = TestRandomUtil.nextBytes(UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH); when(targetAccount.getUnidentifiedAccessKey()).thenReturn(Optional.of(unidentifiedAccessKey)); - when(targetAccount.getAccountIdentifier()).thenReturn(uuid); - when(targetAccount.getAccountIdentityKey()).thenReturn(identityKey); - when(accountsManager.getByServiceIdentifier(identifier)) - .thenReturn(Optional.of(targetAccount)); + when(targetAccount.getAccountIdentifier()).thenReturn(aci); + when(targetAccount.getAccountIdentityKey()).thenReturn(new IdentityKey(aciIdentityKeyPair.getPublicKey())); + when(targetAccount.getPhoneNumberIdentityKey()).thenReturn(Optional.of(new IdentityKey(pniIdentityKeyPair.getPublicKey()))); + when(accountsManager.getByServiceIdentifier(identifier)).thenReturn(Optional.of(targetAccount)); + final ECKeyPair identityKeyPair = identityType == IdentityType.ACI ? aciIdentityKeyPair : pniIdentityKeyPair; final ECPreKey ecPreKey = new ECPreKey(1, ECKeyPair.generate().getPublicKey()); final ECSignedPreKey ecSignedPreKey = KeysHelper.signedECPreKey(2, identityKeyPair); final KEMSignedPreKey kemSignedPreKey = KeysHelper.signedKEMPreKey(3, identityKeyPair); @@ -181,18 +192,22 @@ class KeysAnonymousGrpcServiceTest extends SimpleBaseGrpcTest requestBuilder.setUnidentifiedAccessKey(ByteString.copyFrom(unidentifiedAccessKey)); + case UNRESTRICTED_ACCESS -> requestBuilder.setUnrestrictedAccess(Empty.getDefaultInstance()); + default -> throw new AssertionError("unexpected authorization case: " + authorizationCase); + } + + GrpcTestUtils.assertStatusInvalidArgument(() -> unauthenticatedServiceStub().getPreKeys(requestBuilder.build())); + verifyNoInteractions(keysManager); + } + @Test void checkIdentityKeys() throws InterruptedException { final KeysAnonymousGrpc.KeysAnonymousStub keysAnonymousStub =