Update CheckIdentityKeys response stream to be 1:1 with request

This commit is contained in:
Chris Eager authored and Jon Chambers committed 2026-11-18 15:15:02 -05:00
1 parent 2d6eb7951a
commit 99278dcd1f
3 files changed
+42 -27

No files matched your search

@@ -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) {
@@ -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"];
}
}
@@ -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<KeysAnonymousGrpcS
final IdentityKey mismatchedPniFingerpringAccountIdentityKey = new IdentityKey(ECKeyPair.generate().getPublicKey());
// Complete futures asynchronously to catch potential async/context-propagation issues
final Duration futureDelay = Duration.ofMillis(1);
when(mismatchedAciFingerprintAccount.getAccountIdentityKey()).thenReturn(mismatchedAciFingerprintAccountIdentityKey);
when(accountsManager.getByServiceIdentifierAsync(new AciServiceIdentifier(mismatchedAciFingerprintAccountIdentifier)))
.thenReturn(delayedAccount(mismatchedAciFingerprintAccount));
@@ -476,11 +474,21 @@ class KeysAnonymousGrpcServiceTest extends SimpleBaseGrpcTest<KeysAnonymousGrpcS
when(accountsManager.getByServiceIdentifierAsync(new PniServiceIdentifier(mismatchedPniFingerprintAccountIdentifier)))
.thenReturn(delayedAccount(mismatchedPniFingerprintAccount));
final Map<UUID, IdentityKey> expectedResponses = Map.of(
mismatchedAciFingerprintAccountIdentifier, mismatchedAciFingerprintAccountIdentityKey,
mismatchedPniFingerprintAccountIdentifier, mismatchedPniFingerpringAccountIdentityKey);
final List<CheckIdentityKeyResponse> 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<UUID, IdentityKey> responses = new ConcurrentHashMap<>();
final List<CheckIdentityKeyResponse> responses = Collections.synchronizedList(new ArrayList<>());
final CountDownLatch completedLatch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<>();
@@ -488,13 +496,7 @@ class KeysAnonymousGrpcServiceTest extends SimpleBaseGrpcTest<KeysAnonymousGrpcS
keysAnonymousStub.checkIdentityKeys(new StreamObserver<>() {
@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