Fix issues with receiving message to PNI address

This commit is contained in:
AsamK 2024-02-18 19:25:40 +01:00
parent 6cd57312a1
commit fd671576a4
2 changed files with 21 additions and 20 deletions

View file

@ -64,6 +64,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSy
import org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage; import org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage;
import org.whispersystems.signalservice.api.push.ServiceId; import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.push.ServiceId.ACI; import org.whispersystems.signalservice.api.push.ServiceId.ACI;
import org.whispersystems.signalservice.api.push.ServiceIdType;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.internal.push.Envelope; import org.whispersystems.signalservice.internal.push.Envelope;
import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException; import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
@ -100,8 +101,10 @@ public final class IncomingMessageHandler {
SignalServiceContent content = null; SignalServiceContent content = null;
if (!envelope.isReceipt()) { if (!envelope.isReceipt()) {
account.getIdentityKeyStore().setRetryingDecryption(true); account.getIdentityKeyStore().setRetryingDecryption(true);
final var destination = getDestination(envelope).serviceId();
try { try {
final var cipherResult = dependencies.getCipher() final var cipherResult = dependencies.getCipher(destination == null
|| destination.equals(account.getAci()) ? ServiceIdType.ACI : ServiceIdType.PNI)
.decrypt(envelope.getProto(), envelope.getServerDeliveredTimestamp()); .decrypt(envelope.getProto(), envelope.getServerDeliveredTimestamp());
content = validate(envelope.getProto(), cipherResult, envelope.getServerDeliveredTimestamp()); content = validate(envelope.getProto(), cipherResult, envelope.getServerDeliveredTimestamp());
if (content == null) { if (content == null) {
@ -136,8 +139,10 @@ public final class IncomingMessageHandler {
// uuid in envelope is sent by server // uuid in envelope is sent by server
.ifPresent(serviceId -> account.getRecipientResolver().resolveRecipient(serviceId)); .ifPresent(serviceId -> account.getRecipientResolver().resolveRecipient(serviceId));
if (!envelope.isReceipt()) { if (!envelope.isReceipt()) {
final var destination = getDestination(envelope).serviceId();
try { try {
final var cipherResult = dependencies.getCipher() final var cipherResult = dependencies.getCipher(destination == null
|| destination.equals(account.getAci()) ? ServiceIdType.ACI : ServiceIdType.PNI)
.decrypt(envelope.getProto(), envelope.getServerDeliveredTimestamp()); .decrypt(envelope.getProto(), envelope.getServerDeliveredTimestamp());
content = validate(envelope.getProto(), cipherResult, envelope.getServerDeliveredTimestamp()); content = validate(envelope.getProto(), cipherResult, envelope.getServerDeliveredTimestamp());
if (content == null) { if (content == null) {
@ -173,7 +178,6 @@ public final class IncomingMessageHandler {
.contains(Profile.Capability.senderKey); .contains(Profile.Capability.senderKey);
final var isSelfSenderKeyCapable = selfProfile != null && selfProfile.getCapabilities() final var isSelfSenderKeyCapable = selfProfile != null && selfProfile.getCapabilities()
.contains(Profile.Capability.senderKey); .contains(Profile.Capability.senderKey);
final var destination = getDestination(envelope).serviceId();
if (!isSelf && isSenderSenderKeyCapable && isSelfSenderKeyCapable) { if (!isSelf && isSenderSenderKeyCapable && isSelfSenderKeyCapable) {
logger.debug("Received invalid message, requesting message resend."); logger.debug("Received invalid message, requesting message resend.");
actions.add(new SendRetryMessageRequestAction(sender, serviceId, e, envelope, destination)); actions.add(new SendRetryMessageRequestAction(sender, serviceId, e, envelope, destination));
@ -953,16 +957,12 @@ public final class IncomingMessageHandler {
} }
private DeviceAddress getDestination(SignalServiceEnvelope envelope) { private DeviceAddress getDestination(SignalServiceEnvelope envelope) {
if (!envelope.hasDestinationUuid()) { final var destination = envelope.getDestinationServiceId();
if (destination == null) {
return new DeviceAddress(account.getSelfRecipientId(), account.getAci(), account.getDeviceId()); return new DeviceAddress(account.getSelfRecipientId(), account.getAci(), account.getDeviceId());
} }
final var addressOptional = SignalServiceAddress.fromRaw(envelope.getDestinationServiceId(), null); return new DeviceAddress(account.getRecipientResolver().resolveRecipient(destination),
if (addressOptional.isEmpty()) { destination,
return new DeviceAddress(account.getSelfRecipientId(), account.getAci(), account.getDeviceId());
}
final var address = addressOptional.get();
return new DeviceAddress(account.getRecipientResolver().resolveRecipient(address),
address.getServiceId(),
account.getDeviceId()); account.getDeviceId());
} }

View file

@ -14,6 +14,7 @@ import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations; import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api; import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations; import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
import org.whispersystems.signalservice.api.push.ServiceIdType;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.services.ProfileService; import org.whispersystems.signalservice.api.services.ProfileService;
import org.whispersystems.signalservice.api.svr.SecureValueRecovery; import org.whispersystems.signalservice.api.svr.SecureValueRecovery;
@ -53,7 +54,6 @@ public class SignalDependencies {
private List<SecureValueRecovery> secureValueRecoveryV2; private List<SecureValueRecovery> secureValueRecoveryV2;
private ProfileService profileService; private ProfileService profileService;
private SignalServiceCipher cipher;
SignalDependencies( SignalDependencies(
final ServiceEnvironmentConfig serviceEnvironmentConfig, final ServiceEnvironmentConfig serviceEnvironmentConfig,
@ -77,7 +77,6 @@ public class SignalDependencies {
this.pushServiceSocket = null; this.pushServiceSocket = null;
} }
this.messageSender = null; this.messageSender = null;
this.cipher = null;
getSignalWebSocket().forceNewWebSockets(); getSignalWebSocket().forceNewWebSockets();
} }
@ -208,13 +207,15 @@ public class SignalDependencies {
getSignalWebSocket())); getSignalWebSocket()));
} }
public SignalServiceCipher getCipher() { public SignalServiceCipher getCipher(ServiceIdType serviceIdType) {
return getOrCreate(() -> cipher, () -> { final var certificateValidator = new CertificateValidator(serviceEnvironmentConfig.unidentifiedSenderTrustRoot());
final var certificateValidator = new CertificateValidator(serviceEnvironmentConfig.unidentifiedSenderTrustRoot()); final var address = new SignalServiceAddress(credentialsProvider.getAci(), credentialsProvider.getE164());
final var address = new SignalServiceAddress(credentialsProvider.getAci(), credentialsProvider.getE164()); final var deviceId = credentialsProvider.getDeviceId();
final var deviceId = credentialsProvider.getDeviceId(); return new SignalServiceCipher(address,
cipher = new SignalServiceCipher(address, deviceId, dataStore.aci(), sessionLock, certificateValidator); deviceId,
}); serviceIdType == ServiceIdType.ACI ? dataStore.aci() : dataStore.pni(),
sessionLock,
certificateValidator);
} }
private <T> T getOrCreate(Supplier<T> supplier, Callable creator) { private <T> T getOrCreate(Supplier<T> supplier, Callable creator) {