Improve SignalServiceAddress handling

This commit is contained in:
AsamK 2019-12-30 16:06:16 +01:00
parent 4ca3fe87f4
commit 6cfddc0aff
8 changed files with 101 additions and 60 deletions

View file

@ -31,7 +31,7 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
conn.sendSignal(new Signal.ReceiptReceived(
objectPath,
envelope.getTimestamp(),
envelope.getSourceE164().get()
!envelope.isUnidentifiedSender() && envelope.hasSource() ? envelope.getSourceE164().get() : content.getSender().getNumber().get()
));
} catch (DBusException e) {
e.printStackTrace();
@ -55,7 +55,7 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
conn.sendSignal(new Signal.MessageReceived(
objectPath,
message.getTimestamp(),
envelope.isUnidentifiedSender() ? content.getSender().getNumber().get() : envelope.getSourceE164().get(),
envelope.isUnidentifiedSender() || !envelope.hasSource() ? content.getSender().getNumber().get() : envelope.getSourceE164().get(),
message.getGroupInfo().isPresent() ? message.getGroupInfo().get().getGroupId() : new byte[0],
message.getBody().isPresent() ? message.getBody().get() : "",
attachments));

View file

@ -17,10 +17,12 @@ class JsonMessageEnvelope {
JsonReceiptMessage receiptMessage;
public JsonMessageEnvelope(SignalServiceEnvelope envelope, SignalServiceContent content) {
SignalServiceAddress source = envelope.getSourceAddress();
this.source = source.getNumber().get();
if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
SignalServiceAddress source = envelope.getSourceAddress();
this.source = source.getNumber().get();
this.relay = source.getRelay().isPresent() ? source.getRelay().get() : null;
}
this.sourceDevice = envelope.getSourceDevice();
this.relay = source.getRelay().isPresent() ? source.getRelay().get() : null;
this.timestamp = envelope.getTimestamp();
this.isReceipt = envelope.isReceipt();
if (content != null) {

View file

@ -42,11 +42,15 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
@Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
SignalServiceAddress source = envelope.getSourceAddress();
ContactInfo sourceContact = m.getContact(source.getNumber().get());
System.out.println(String.format("Envelope from: %s (device: %d)", (sourceContact == null ? "" : "" + sourceContact.name + "") + source.getNumber(), envelope.getSourceDevice()));
if (source.getRelay().isPresent()) {
System.out.println("Relayed by: " + source.getRelay().get());
if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
SignalServiceAddress source = envelope.getSourceAddress();
ContactInfo sourceContact = m.getContact(source.getNumber().get());
System.out.println(String.format("Envelope from: %s (device: %d)", (sourceContact == null ? "" : "" + sourceContact.name + "") + source.getNumber().get(), envelope.getSourceDevice()));
if (source.getRelay().isPresent()) {
System.out.println("Relayed by: " + source.getRelay().get());
}
} else {
System.out.println("Envelope from: unknown source");
}
System.out.println("Timestamp: " + DateUtils.formatTimestamp(envelope.getTimestamp()));
if (envelope.isUnidentifiedSender()) {
@ -69,7 +73,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
if (content == null) {
System.out.println("Failed to decrypt message.");
} else {
System.out.println(String.format("Sender: %s (device: %d)", content.getSender(), content.getSenderDevice()));
System.out.println(String.format("Sender: %s (device: %d)", content.getSender().getNumber().get(), content.getSenderDevice()));
if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();
handleSignalServiceDataMessage(message);

View file

@ -17,7 +17,11 @@
package org.asamk.signal.manager;
import org.asamk.Signal;
import org.asamk.signal.*;
import org.asamk.signal.AttachmentInvalidException;
import org.asamk.signal.GroupNotFoundException;
import org.asamk.signal.NotAGroupMemberException;
import org.asamk.signal.TrustLevel;
import org.asamk.signal.UserAlreadyExists;
import org.asamk.signal.storage.SignalAccount;
import org.asamk.signal.storage.contacts.ContactInfo;
import org.asamk.signal.storage.groups.GroupInfo;
@ -145,6 +149,14 @@ public class Manager implements Signal {
return username;
}
private SignalServiceAddress getSelfAddress() {
return new SignalServiceAddress(null, username);
}
private SignalServiceAccountManager getSignalServiceAccountManager() {
return new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, account.getUsername(), account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer);
}
private IdentityKey getIdentity() {
return account.getSignalProtocolStore().getIdentityKeyPair().getPublicKey();
}
@ -179,7 +191,7 @@ public class Manager implements Signal {
migrateLegacyConfigs();
accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, username, account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer);
accountManager = getSignalServiceAccountManager();
try {
if (account.isRegistered() && accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) {
refreshPreKeys();
@ -238,7 +250,7 @@ public class Manager implements Signal {
createNewIdentity();
}
account.setPassword(KeyUtils.createPassword());
accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, account.getUsername(), account.getPassword(), BaseConfig.USER_AGENT, timer);
accountManager = getSignalServiceAccountManager();
if (voiceVerification) {
accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.<String>absent(), Optional.<String>absent());
@ -283,7 +295,7 @@ public class Manager implements Signal {
createNewIdentity();
}
account.setPassword(KeyUtils.createPassword());
accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, username, account.getPassword(), BaseConfig.USER_AGENT, timer);
accountManager = getSignalServiceAccountManager();
String uuid = accountManager.getNewDeviceUuid();
return Utils.createDeviceLinkUri(new Utils.DeviceLinkInfo(uuid, getIdentity().getPublicKey()));
@ -414,6 +426,11 @@ public class Manager implements Signal {
return new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, null, username, account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer);
}
private SignalServiceMessageSender getMessageSender() {
return new SignalServiceMessageSender(BaseConfig.serviceConfiguration, null, username, account.getPassword(),
account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
}
private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException {
File file = getGroupAvatarFile(groupId);
if (!file.exists()) {
@ -569,15 +586,10 @@ public class Manager implements Signal {
}
private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) {
ArrayList<SignalServiceAddress> members = new ArrayList<>(g.members.size());
for (String member : g.members) {
members.add(new SignalServiceAddress(null, member));
}
SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
.withId(g.groupId)
.withName(g.name)
.withMembers(members);
.withMembers(new ArrayList<>(g.getMembers()));
File aFile = getGroupAvatarFile(g.groupId);
if (aFile.exists()) {
@ -802,8 +814,7 @@ public class Manager implements Signal {
private void sendSyncMessage(SignalServiceSyncMessage message)
throws IOException, UntrustedIdentityException {
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(BaseConfig.serviceConfiguration, null, username, account.getPassword(),
account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
SignalServiceMessageSender messageSender = getMessageSender();
try {
messageSender.sendMessage(message, getAccessForSync());
} catch (UntrustedIdentityException e) {
@ -847,8 +858,7 @@ public class Manager implements Signal {
SignalServiceDataMessage message = null;
try {
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(BaseConfig.serviceConfiguration, null, username, account.getPassword(),
account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
SignalServiceMessageSender messageSender = getMessageSender();
message = messageBuilder.build();
if (message.getGroupInfo().isPresent()) {
@ -865,14 +875,14 @@ public class Manager implements Signal {
account.getSignalProtocolStore().saveIdentity(e.getIdentifier(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
return Collections.emptyList();
}
} else if (recipientsTS.size() == 1 && recipientsTS.contains(new SignalServiceAddress(null, username))) {
SignalServiceAddress recipient = new SignalServiceAddress(null, username);
} else if (recipientsTS.size() == 1 && recipientsTS.contains(getSelfAddress())) {
SignalServiceAddress recipient = getSelfAddress();
final Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);
SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(new SignalServiceAddress(null, recipient.getNumber().get())),
SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient),
message.getTimestamp(),
message,
message.getExpiresInSeconds(),
Collections.singletonMap(new SignalServiceAddress(null, recipient.getNumber()), unidentifiedAccess.isPresent()),
Collections.singletonMap(recipient, unidentifiedAccess.isPresent()),
false);
SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
@ -916,7 +926,7 @@ public class Manager implements Signal {
}
private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, ProtocolUntrustedIdentityException, SelfSendException, UnsupportedDataMessageException {
SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(null, username), account.getSignalProtocolStore(), Utils.getCertificateValidator());
SignalServiceCipher cipher = new SignalServiceCipher(getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator());
try {
return cipher.decrypt(envelope);
} catch (ProtocolUntrustedIdentityException e) {
@ -930,7 +940,7 @@ public class Manager implements Signal {
account.getSignalProtocolStore().deleteAllSessions(source);
}
private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination, boolean ignoreAttachments) {
private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, SignalServiceAddress destination, boolean ignoreAttachments) {
String threadId;
if (message.getGroupInfo().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupInfo().get();
@ -958,11 +968,7 @@ public class Manager implements Signal {
}
if (groupInfo.getMembers().isPresent()) {
List<String> members = new ArrayList<>(groupInfo.getMembers().get().size());
for (SignalServiceAddress address : groupInfo.getMembers().get()) {
members.add(address.getNumber().get());
}
group.members.addAll(members);
group.addMembers(groupInfo.getMembers().get());
}
account.getGroupStore().updateGroup(group);
@ -1002,13 +1008,13 @@ public class Manager implements Signal {
}
} else {
if (isSync) {
threadId = destination;
threadId = destination.getNumber().get();
} else {
threadId = source;
}
}
if (message.isEndSession()) {
handleEndSession(isSync ? destination : source);
handleEndSession(isSync ? destination.getNumber().get() : source);
}
if (message.isExpirationUpdate() || message.getBody().isPresent()) {
ThreadInfo thread = account.getThreadStore().getThread(threadId);
@ -1158,16 +1164,22 @@ public class Manager implements Signal {
private void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) {
if (content != null) {
SignalServiceAddress sender;
if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
sender = envelope.getSourceAddress();
} else {
sender = content.getSender();
}
if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();
handleSignalServiceDataMessage(message, false, envelope.getSourceE164().get(), username, ignoreAttachments);
handleSignalServiceDataMessage(message, false, sender.getNumber().get(), getSelfAddress(), ignoreAttachments);
}
if (content.getSyncMessage().isPresent()) {
account.setMultiDevice(true);
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
if (syncMessage.getSent().isPresent()) {
SignalServiceDataMessage message = syncMessage.getSent().get().getMessage();
handleSignalServiceDataMessage(message, true, envelope.getSourceE164().get(), syncMessage.getSent().get().getDestination().get().getNumber().get(), ignoreAttachments);
SentTranscriptMessage message = syncMessage.getSent().get();
handleSignalServiceDataMessage(message.getMessage(), true, sender.getNumber().get(), message.getDestination().orNull(), ignoreAttachments);
}
if (syncMessage.getRequest().isPresent()) {
RequestMessage rm = syncMessage.getRequest().get();
@ -1202,11 +1214,7 @@ public class Manager implements Signal {
if (g.getName().isPresent()) {
syncGroup.name = g.getName().get();
}
List<String> members = new ArrayList<>(g.getMembers().size());
for (SignalServiceAddress member : g.getMembers()) {
members.add(member.getNumber().get());
}
syncGroup.members.addAll(members);
syncGroup.addMembers(g.getMembers());
syncGroup.active = g.isActive();
if (g.getColor().isPresent()) {
syncGroup.color = g.getColor().get();
@ -1245,7 +1253,7 @@ public class Manager implements Signal {
}
DeviceContact c;
while ((c = s.read()) != null) {
if (c.getAddress().getNumber().get().equals(account.getUsername()) && c.getProfileKey().isPresent()) {
if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
account.setProfileKey(c.getProfileKey().get());
}
ContactInfo contact = account.getContactStore().getContact(c.getAddress().getNumber().get());
@ -1402,12 +1410,8 @@ public class Manager implements Signal {
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
for (GroupInfo record : account.getGroupStore().getGroups()) {
ThreadInfo info = account.getThreadStore().getThread(Base64.encodeBytes(record.groupId));
List<SignalServiceAddress> members = new ArrayList<>(record.members.size());
for (String member : record.members) {
members.add(new SignalServiceAddress(null, member));
}
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
members, createGroupAvatarAttachment(record.groupId),
new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId),
record.active, Optional.fromNullable(info != null ? info.messageExpirationTime : null),
Optional.fromNullable(record.color), false));
}
@ -1450,21 +1454,21 @@ public class Manager implements Signal {
}
}
if (currentIdentity != null) {
verifiedMessage = new VerifiedMessage(new SignalServiceAddress(null, record.number), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
}
}
byte[] profileKey = record.profileKey == null ? null : Base64.decode(record.profileKey);
// TODO store list of blocked numbers
boolean blocked = false;
out.write(new DeviceContact(new SignalServiceAddress(null, record.number), Optional.fromNullable(record.name),
out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name),
createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), blocked, Optional.fromNullable(info != null ? info.messageExpirationTime : null)));
}
if (account.getProfileKey() != null) {
// Send our own profile key as well
out.write(new DeviceContact(new SignalServiceAddress(null, account.getUsername()),
out.write(new DeviceContact(account.getSelfAddress(),
Optional.<String>absent(), Optional.<SignalServiceAttachmentStream>absent(),
Optional.<String>absent(), Optional.<VerifiedMessage>absent(),
Optional.of(account.getProfileKey()),
@ -1492,8 +1496,8 @@ public class Manager implements Signal {
}
}
private void sendVerifiedMessage(String destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
VerifiedMessage verifiedMessage = new VerifiedMessage(new SignalServiceAddress(null, destination), identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis());
private void sendVerifiedMessage(SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis());
sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
}
@ -1535,7 +1539,7 @@ public class Manager implements Signal {
account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
@ -1563,7 +1567,7 @@ public class Manager implements Signal {
account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
@ -1587,7 +1591,7 @@ public class Manager implements Signal {
if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}

View file

@ -257,6 +257,8 @@ class Utils {
}
static String computeSafetyNumber(String ownUsername, IdentityKey ownIdentityKey, String theirUsername, IdentityKey theirIdentityKey) {
// Version 1: E164 user
// Version 2: UUID user
Fingerprint fingerprint = new NumericFingerprintGenerator(5200).createFor(1, ownUsername.getBytes(), ownIdentityKey, theirUsername.getBytes(), theirIdentityKey);
return fingerprint.getDisplayableFingerprint().getDisplayText();
}

View file

@ -271,6 +271,10 @@ public class SignalAccount {
return username;
}
public SignalServiceAddress getSelfAddress() {
return new SignalServiceAddress(null, username);
}
public int getDeviceId() {
return deviceId;
}

View file

@ -1,7 +1,10 @@
package org.asamk.signal.storage.contacts;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
public class ContactInfo {
@JsonProperty
@ -15,4 +18,9 @@ public class ContactInfo {
@JsonProperty
public String profileKey;
@JsonIgnore
public SignalServiceAddress getAddress() {
return new SignalServiceAddress(null, number);
}
}

View file

@ -3,6 +3,8 @@ package org.asamk.signal.storage.groups;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@ -40,4 +42,19 @@ public class GroupInfo {
public long getAvatarId() {
return avatarId;
}
@JsonIgnore
public Set<SignalServiceAddress> getMembers() {
Set<SignalServiceAddress> addresses = new HashSet<>(members.size());
for (String member : members) {
addresses.add(new SignalServiceAddress(null, member));
}
return addresses;
}
public void addMembers(Collection<SignalServiceAddress> members) {
for (SignalServiceAddress member : members) {
this.members.add(member.getNumber().get());
}
}
}