Store profile keys only in profile store

Fixes #328
This commit is contained in:
AsamK 2020-11-21 20:11:46 +01:00
parent 8c1f082c8a
commit 8a86f250ec
3 changed files with 54 additions and 33 deletions

View file

@ -267,6 +267,21 @@ public class Manager implements Closeable {
account.setProfileKey(KeyUtils.createProfileKey()); account.setProfileKey(KeyUtils.createProfileKey());
account.save(); 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 { public void checkAccountState() throws IOException {
@ -993,16 +1008,10 @@ public class Manager implements Closeable {
} }
private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) { private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
ContactInfo contact = account.getContactStore().getContact(recipient); ProfileKey theirProfileKey = account.getProfileStore().getProfileKey(recipient);
if (contact == null || contact.profileKey == null) { if (theirProfileKey == null) {
return null; return null;
} }
ProfileKey theirProfileKey;
try {
theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey));
} catch (InvalidInputException | IOException e) {
throw new AssertionError(e);
}
SignalProfile targetProfile; SignalProfile targetProfile;
try { try {
targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey); targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey);
@ -1326,24 +1335,16 @@ public class Manager implements Closeable {
} }
} }
if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) { if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
if (source.matches(account.getSelfAddress())) { final ProfileKey profileKey;
try { try {
this.account.setProfileKey(new ProfileKey(message.getProfileKey().get())); profileKey = new ProfileKey(message.getProfileKey().get());
} catch (InvalidInputException ignored) { } catch (InvalidInputException e) {
} throw new AssertionError(e);
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);
} }
if (source.matches(account.getSelfAddress())) {
this.account.setProfileKey(profileKey);
}
this.account.getProfileStore().storeProfileKey(source, profileKey);
} }
if (message.getPreviews().isPresent()) { if (message.getPreviews().isPresent()) {
final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get(); final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();
@ -1690,7 +1691,7 @@ public class Manager implements Closeable {
contact.color = c.getColor().get(); contact.color = c.getColor().get();
} }
if (c.getProfileKey().isPresent()) { if (c.getProfileKey().isPresent()) {
contact.profileKey = Base64.encodeBytes(c.getProfileKey().get().serialize()); account.getProfileStore().storeProfileKey(address, c.getProfileKey().get());
} }
if (c.getVerified().isPresent()) { if (c.getVerified().isPresent()) {
final VerifiedMessage verifiedMessage = c.getVerified().get(); 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()); verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
} }
ProfileKey profileKey = null; ProfileKey profileKey = account.getProfileStore().getProfileKey(record.getAddress());
try {
profileKey = record.profileKey == null ? null : new ProfileKey(Base64.decode(record.profileKey));
} catch (InvalidInputException ignored) {
}
out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name), out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name),
createContactAvatarAttachment(record.number), Optional.fromNullable(record.color), createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked, Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked,

View file

@ -7,6 +7,8 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.util.UUID; import java.util.UUID;
import static com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY;
public class ContactInfo { public class ContactInfo {
@JsonProperty @JsonProperty
@ -24,7 +26,7 @@ public class ContactInfo {
@JsonProperty(defaultValue = "0") @JsonProperty(defaultValue = "0")
public int messageExpirationTime; public int messageExpirationTime;
@JsonProperty @JsonProperty(access = WRITE_ONLY)
public String profileKey; public String profileKey;
@JsonProperty(defaultValue = "false") @JsonProperty(defaultValue = "false")

View file

@ -20,7 +20,6 @@ import org.whispersystems.util.Base64;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -42,6 +41,15 @@ public class ProfileStore {
return null; 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) { public void updateProfile(SignalServiceAddress serviceAddress, ProfileKey profileKey, long now, SignalProfile profile) {
SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, now, profile); SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, now, profile);
for (int i = 0; i < profiles.size(); i++) { for (int i = 0; i < profiles.size(); i++) {
@ -54,6 +62,20 @@ public class ProfileStore {
profiles.add(newEntry); 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<List<SignalProfileEntry>> { public static class ProfileStoreDeserializer extends JsonDeserializer<List<SignalProfileEntry>> {
@Override @Override