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 44c72c9ef..97b64f77c 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcService.java @@ -6,6 +6,7 @@ package org.whispersystems.textsecuregcm.grpc; import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.Clock; @@ -33,6 +34,8 @@ import reactor.util.function.Tuples; public class KeysAnonymousGrpcService extends SimpleKeysAnonymousGrpc.KeysAnonymousImplBase { + private static final int CHECK_KEYS_MAX_CONCURRENCY = 16; + private final AccountsManager accountsManager; private final KeysManager keysManager; private final GroupSendTokenUtil groupSendTokenUtil; @@ -99,20 +102,22 @@ public class KeysAnonymousGrpcService extends SimpleKeysAnonymousGrpc.KeysAnonym return JdkFlowAdapter.publisherToFlowPublisher(JdkFlowAdapter.flowPublisherToFlux(requests) .map(request -> Tuples.of(GrpcServiceIdentifierUtil.fromGrpcServiceIdentifier(request.getTargetIdentifier()), request.getFingerprint().toByteArray())) - .flatMap(serviceIdentifierAndFingerprint -> Mono.fromFuture( + .flatMapSequential(serviceIdentifierAndFingerprint -> Mono.fromFuture( () -> accountsManager.getByServiceIdentifierAsync(serviceIdentifierAndFingerprint.getT1())) .map(maybeAccount -> maybeAccount.flatMap( account -> switch (serviceIdentifierAndFingerprint.getT1().identityType()) { case ACI -> Optional.of(account.getAccountIdentityKey()); case PNI -> account.getPhoneNumberIdentityKey(); })) - .flatMap(Mono::justOrEmpty) - .filter(identityKey -> !fingerprintMatches(identityKey, serviceIdentifierAndFingerprint.getT2())) - .map(identityKey -> CheckIdentityKeyResponse.newBuilder() - .setTargetIdentifier( - GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(serviceIdentifierAndFingerprint.getT1())) - .setIdentityKey(ByteString.copyFrom(identityKey.serialize())) - .build()))); + .map(maybeIdentityKey -> { + final CheckIdentityKeyResponse.Builder builder = CheckIdentityKeyResponse.newBuilder(); + + return maybeIdentityKey.map(identityKey -> + fingerprintMatches(identityKey, serviceIdentifierAndFingerprint.getT2()) + ? builder.setMatched(Empty.getDefaultInstance()).build() + : builder.setIdentityKey(ByteString.copyFrom(identityKey.serialize())).build()) + .orElseGet(() -> builder.setTargetNotFound(NotFound.getDefaultInstance()).build()); + }), CHECK_KEYS_MAX_CONCURRENCY)); } private static boolean fingerprintMatches(final IdentityKey identityKey, final byte[] fingerprint) { diff --git a/service/src/main/proto/org/signal/chat/keys.proto b/service/src/main/proto/org/signal/chat/keys.proto index 9bdd05c42..069e9d6bc 100644 --- a/service/src/main/proto/org/signal/chat/keys.proto +++ b/service/src/main/proto/org/signal/chat/keys.proto @@ -230,8 +230,16 @@ message CheckIdentityKeyRequest { } message CheckIdentityKeyResponse { - // The service identifier of the account for which there is a mismatch between the client and server identity key fingerprints. - common.ServiceIdentifier target_identifier = 1; - // The identity key that is stored by the server for the target account/identity type. - bytes identity_key = 2; + + oneof response { + // The key for the target account/identity type matched. + google.protobuf.Empty matched = 1; + + // The identity key that is stored by the server for the target account/identity type. + bytes identity_key = 2; + + // Either the target account was not found, or it has no identity key for + // the requested identity type. + errors.NotFound target_not_found = 3 [(tag.reason) = "not_found"]; + } } 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 7aaed1be3..e3cb1c4e1 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/KeysAnonymousGrpcServiceTest.java @@ -26,13 +26,12 @@ import java.security.NoSuchAlgorithmException; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -45,6 +44,7 @@ import org.signal.chat.common.EcPreKey; import org.signal.chat.common.EcSignedPreKey; import org.signal.chat.common.KemSignedPreKey; import org.signal.chat.common.ServiceIdentifier; +import org.signal.chat.errors.NotFound; import org.signal.chat.keys.AccountPreKeyBundles; import org.signal.chat.keys.CheckIdentityKeyRequest; import org.signal.chat.keys.CheckIdentityKeyResponse; @@ -54,7 +54,6 @@ import org.signal.chat.keys.GetPreKeysAnonymousResponse; import org.signal.chat.keys.GetPreKeysRequest; import org.signal.chat.keys.KeysAnonymousGrpc; import org.signal.libsignal.protocol.IdentityKey; -import org.signal.libsignal.protocol.InvalidKeyException; import org.signal.libsignal.protocol.ecc.ECKeyPair; import org.signal.libsignal.zkgroup.ServerSecretParams; import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessChecksum; @@ -463,7 +462,6 @@ class KeysAnonymousGrpcServiceTest extends SimpleBaseGrpcTest expectedResponses = Map.of( - mismatchedAciFingerprintAccountIdentifier, mismatchedAciFingerprintAccountIdentityKey, - mismatchedPniFingerprintAccountIdentifier, mismatchedPniFingerpringAccountIdentityKey); + final List expectedResponses = List.of( + CheckIdentityKeyResponse.newBuilder() + .setIdentityKey(ByteString.copyFrom(mismatchedAciFingerprintAccountIdentityKey.serialize())) + .build(), + CheckIdentityKeyResponse.newBuilder() + .setMatched(Empty.getDefaultInstance()) + .build(), + CheckIdentityKeyResponse.newBuilder() + .setTargetNotFound(NotFound.getDefaultInstance()) + .build(), + CheckIdentityKeyResponse.newBuilder() + .setIdentityKey(ByteString.copyFrom(mismatchedPniFingerpringAccountIdentityKey.serialize())) + .build()); - final Map responses = new ConcurrentHashMap<>(); + final List responses = Collections.synchronizedList(new ArrayList<>()); final CountDownLatch completedLatch = new CountDownLatch(1); final AtomicReference error = new AtomicReference<>(); @@ -488,13 +496,7 @@ class KeysAnonymousGrpcServiceTest extends SimpleBaseGrpcTest() { @Override public void onNext(final CheckIdentityKeyResponse checkIdentityKeyResponse) { - try { - responses.put( - GrpcServiceIdentifierUtil.fromGrpcServiceIdentifier(checkIdentityKeyResponse.getTargetIdentifier()).uuid(), - new IdentityKey(checkIdentityKeyResponse.getIdentityKey().toByteArray())); - } catch (final InvalidKeyException e) { - throw new RuntimeException(e); - } + responses.add(checkIdentityKeyResponse); } @Override