From: AsamK Date: Sat, 21 Nov 2020 19:11:46 +0000 (+0100) Subject: Store profile keys only in profile store X-Git-Tag: v0.6.12~7 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/8a86f250ec7be2a37d40558c5179759b7643bf40 Store profile keys only in profile store Fixes #328 --- diff --git a/src/main/java/org/asamk/signal/manager/Manager.java b/src/main/java/org/asamk/signal/manager/Manager.java index d80b522e..6b2e7c96 100644 --- a/src/main/java/org/asamk/signal/manager/Manager.java +++ b/src/main/java/org/asamk/signal/manager/Manager.java @@ -267,6 +267,21 @@ public class Manager implements Closeable { account.setProfileKey(KeyUtils.createProfileKey()); account.save(); } + // Store profile keys only in profile store + for (ContactInfo contact : account.getContactStore().getContacts()) { + String profileKeyString = contact.profileKey; + if (profileKeyString == null) { + continue; + } + final ProfileKey profileKey; + try { + profileKey = new ProfileKey(Base64.decode(profileKeyString)); + } catch (InvalidInputException | IOException e) { + continue; + } + contact.profileKey = null; + account.getProfileStore().storeProfileKey(contact.getAddress(), profileKey); + } } public void checkAccountState() throws IOException { @@ -993,16 +1008,10 @@ public class Manager implements Closeable { } private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) { - ContactInfo contact = account.getContactStore().getContact(recipient); - if (contact == null || contact.profileKey == null) { + ProfileKey theirProfileKey = account.getProfileStore().getProfileKey(recipient); + if (theirProfileKey == null) { return null; } - ProfileKey theirProfileKey; - try { - theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey)); - } catch (InvalidInputException | IOException e) { - throw new AssertionError(e); - } SignalProfile targetProfile; try { targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey); @@ -1326,24 +1335,16 @@ public class Manager implements Closeable { } } if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) { + final ProfileKey profileKey; + try { + profileKey = new ProfileKey(message.getProfileKey().get()); + } catch (InvalidInputException e) { + throw new AssertionError(e); + } if (source.matches(account.getSelfAddress())) { - try { - this.account.setProfileKey(new ProfileKey(message.getProfileKey().get())); - } catch (InvalidInputException ignored) { - } - ContactInfo contact = account.getContactStore().getContact(source); - if (contact != null) { - contact.profileKey = Base64.encodeBytes(message.getProfileKey().get()); - account.getContactStore().updateContact(contact); - } - } else { - ContactInfo contact = account.getContactStore().getContact(source); - if (contact == null) { - contact = new ContactInfo(source); - } - contact.profileKey = Base64.encodeBytes(message.getProfileKey().get()); - account.getContactStore().updateContact(contact); + this.account.setProfileKey(profileKey); } + this.account.getProfileStore().storeProfileKey(source, profileKey); } if (message.getPreviews().isPresent()) { final List previews = message.getPreviews().get(); @@ -1690,7 +1691,7 @@ public class Manager implements Closeable { contact.color = c.getColor().get(); } if (c.getProfileKey().isPresent()) { - contact.profileKey = Base64.encodeBytes(c.getProfileKey().get().serialize()); + account.getProfileStore().storeProfileKey(address, c.getProfileKey().get()); } if (c.getVerified().isPresent()) { final VerifiedMessage verifiedMessage = c.getVerified().get(); @@ -1874,11 +1875,7 @@ public class Manager implements Closeable { verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime()); } - ProfileKey profileKey = null; - try { - profileKey = record.profileKey == null ? null : new ProfileKey(Base64.decode(record.profileKey)); - } catch (InvalidInputException ignored) { - } + ProfileKey profileKey = account.getProfileStore().getProfileKey(record.getAddress()); out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name), createContactAvatarAttachment(record.number), Optional.fromNullable(record.color), Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked, diff --git a/src/main/java/org/asamk/signal/storage/contacts/ContactInfo.java b/src/main/java/org/asamk/signal/storage/contacts/ContactInfo.java index 4d3a5e95..3b155210 100644 --- a/src/main/java/org/asamk/signal/storage/contacts/ContactInfo.java +++ b/src/main/java/org/asamk/signal/storage/contacts/ContactInfo.java @@ -7,6 +7,8 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress; import java.util.UUID; +import static com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY; + public class ContactInfo { @JsonProperty @@ -24,7 +26,7 @@ public class ContactInfo { @JsonProperty(defaultValue = "0") public int messageExpirationTime; - @JsonProperty + @JsonProperty(access = WRITE_ONLY) public String profileKey; @JsonProperty(defaultValue = "false") diff --git a/src/main/java/org/asamk/signal/storage/profiles/ProfileStore.java b/src/main/java/org/asamk/signal/storage/profiles/ProfileStore.java index 24b08968..662d0161 100644 --- a/src/main/java/org/asamk/signal/storage/profiles/ProfileStore.java +++ b/src/main/java/org/asamk/signal/storage/profiles/ProfileStore.java @@ -20,7 +20,6 @@ import org.whispersystems.util.Base64; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -42,6 +41,15 @@ public class ProfileStore { return null; } + public ProfileKey getProfileKey(SignalServiceAddress serviceAddress) { + for (SignalProfileEntry entry : profiles) { + if (entry.getServiceAddress().matches(serviceAddress)) { + return entry.getProfileKey(); + } + } + return null; + } + public void updateProfile(SignalServiceAddress serviceAddress, ProfileKey profileKey, long now, SignalProfile profile) { SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, now, profile); for (int i = 0; i < profiles.size(); i++) { @@ -54,6 +62,20 @@ public class ProfileStore { profiles.add(newEntry); } + public void storeProfileKey(SignalServiceAddress serviceAddress, ProfileKey profileKey) { + SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, 0, null); + for (int i = 0; i < profiles.size(); i++) { + if (profiles.get(i).getServiceAddress().matches(serviceAddress)) { + if (!profiles.get(i).getProfileKey().equals(profileKey)) { + profiles.set(i, newEntry); + } + return; + } + } + + profiles.add(newEntry); + } + public static class ProfileStoreDeserializer extends JsonDeserializer> { @Override