Don't store self profile key in recipient store

This commit is contained in:
AsamK 2023-11-12 13:13:58 +01:00
parent 7b15e2a39d
commit 1bbfa74b3f
4 changed files with 33 additions and 23 deletions

View file

@ -282,7 +282,6 @@ public class SignalAccount implements Closeable {
getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
this.password = password;
this.profileKey = profileKey;
getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
this.encryptedDeviceName = encryptedDeviceName;
this.aciAccountData.setIdentityKeyPair(aciIdentity);
this.pniAccountData.setIdentityKeyPair(pniIdentity);
@ -654,14 +653,11 @@ public class SignalAccount implements Closeable {
// Old config file, creating new profile key
setProfileKey(KeyUtils.createProfileKey());
}
getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
if (previousStorageVersion < 5) {
final var legacyRecipientsStoreFile = new File(userPath, "recipients-store");
if (legacyRecipientsStoreFile.exists()) {
LegacyRecipientStore2.migrate(legacyRecipientsStoreFile, getRecipientStore());
// Ensure our profile key is stored in profile store
getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
}
}
if (previousStorageVersion < 6) {
@ -1185,12 +1181,11 @@ public class SignalAccount implements Closeable {
}
public RecipientStore getRecipientStore() {
return getOrCreate(() -> recipientStore, () -> {
recipientStore = new RecipientStore(this::mergeRecipients,
return getOrCreate(() -> recipientStore,
() -> recipientStore = new RecipientStore(this::mergeRecipients,
this::getSelfRecipientAddress,
getAccountDatabase());
getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
});
this::getProfileKey,
getAccountDatabase()));
}
public ProfileStore getProfileStore() {
@ -1547,7 +1542,6 @@ public class SignalAccount implements Closeable {
}
this.profileKey = profileKey;
save();
getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
}
public byte[] getSelfUnidentifiedAccessKey() {

View file

@ -15,8 +15,6 @@ public interface ProfileStore {
void storeProfile(RecipientId recipientId, Profile profile);
void storeSelfProfileKey(RecipientId recipientId, ProfileKey profileKey);
void storeProfileKey(RecipientId recipientId, ProfileKey profileKey);
void storeExpiringProfileKeyCredential(

View file

@ -42,6 +42,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
private final RecipientMergeHandler recipientMergeHandler;
private final SelfAddressProvider selfAddressProvider;
private final SelfProfileKeyProvider selfProfileKeyProvider;
private final Database database;
private final Object recipientsLock = new Object();
@ -88,10 +89,12 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
public RecipientStore(
final RecipientMergeHandler recipientMergeHandler,
final SelfAddressProvider selfAddressProvider,
final SelfProfileKeyProvider selfProfileKeyProvider,
final Database database
) {
this.recipientMergeHandler = recipientMergeHandler;
this.selfAddressProvider = selfAddressProvider;
this.selfProfileKeyProvider = selfProfileKeyProvider;
this.database = database;
}
@ -363,6 +366,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
WHERE (r.number IS NOT NULL OR r.uuid IS NOT NULL) AND %s
"""
).formatted(TABLE_RECIPIENT, sqlWhere.isEmpty() ? "TRUE" : String.join(" AND ", sqlWhere));
final var selfServiceId = selfAddressProvider.getSelfAddress().serviceId();
try (final var connection = database.getConnection()) {
try (final var statement = connection.prepareStatement(sql)) {
if (blocked.isPresent()) {
@ -371,7 +375,14 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
try (var result = Utils.executeQueryForStream(statement, this::getRecipientFromResultSet)) {
return result.filter(r -> name.isEmpty() || (
r.getContact() != null && name.get().equals(r.getContact().getName())
) || (r.getProfile() != null && name.get().equals(r.getProfile().getDisplayName()))).toList();
) || (r.getProfile() != null && name.get().equals(r.getProfile().getDisplayName()))).map(r -> {
if (r.getAddress().serviceId().equals(selfServiceId)) {
return Recipient.newBuilder(r)
.withProfileKey(selfProfileKeyProvider.getSelfProfileKey())
.build();
}
return r;
}).toList();
}
}
} catch (SQLException e) {
@ -416,10 +427,14 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
WHERE r.uuid IS NOT NULL AND r.profile_key IS NOT NULL
"""
).formatted(TABLE_RECIPIENT);
final var selfServiceId = selfAddressProvider.getSelfAddress().serviceId().orElse(null);
try (final var connection = database.getConnection()) {
try (final var statement = connection.prepareStatement(sql)) {
return Utils.executeQueryForStream(statement, resultSet -> {
final var serviceId = ServiceId.parseOrThrow(resultSet.getBytes("uuid"));
if (serviceId.equals(selfServiceId)) {
return new Pair<>(serviceId, selfProfileKeyProvider.getSelfProfileKey());
}
final var profileKey = getProfileKeyFromResultSet(resultSet);
return new Pair<>(serviceId, profileKey);
}).filter(Objects::nonNull).collect(Collectors.toMap(Pair::first, Pair::second));
@ -463,6 +478,10 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
@Override
public ProfileKey getProfileKey(final RecipientId recipientId) {
final var selfRecipientId = resolveRecipient(selfAddressProvider.getSelfAddress());
if (recipientId.equals(selfRecipientId)) {
return selfProfileKeyProvider.getSelfProfileKey();
}
try (final var connection = database.getConnection()) {
return getProfileKey(connection, recipientId);
} catch (SQLException e) {
@ -488,15 +507,6 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
}
}
@Override
public void storeSelfProfileKey(final RecipientId recipientId, final ProfileKey profileKey) {
try (final var connection = database.getConnection()) {
storeProfileKey(connection, recipientId, profileKey, false);
} catch (SQLException e) {
throw new RuntimeException("Failed update recipient store", e);
}
}
@Override
public void storeProfileKey(RecipientId recipientId, final ProfileKey profileKey) {
try (final var connection = database.getConnection()) {

View file

@ -0,0 +1,8 @@
package org.asamk.signal.manager.storage.recipients;
import org.signal.libsignal.zkgroup.profiles.ProfileKey;
public interface SelfProfileKeyProvider {
ProfileKey getSelfProfileKey();
}