Don't store self profile key in recipient store

This commit is contained in:
AsamK 2023-11-12 13:13:58 +01:00
parent 5cac7feabe
commit 26cef99cdf
4 changed files with 33 additions and 23 deletions

View file

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

View file

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

View file

@ -42,6 +42,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
private final RecipientMergeHandler recipientMergeHandler; private final RecipientMergeHandler recipientMergeHandler;
private final SelfAddressProvider selfAddressProvider; private final SelfAddressProvider selfAddressProvider;
private final SelfProfileKeyProvider selfProfileKeyProvider;
private final Database database; private final Database database;
private final Object recipientsLock = new Object(); private final Object recipientsLock = new Object();
@ -89,10 +90,12 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
public RecipientStore( public RecipientStore(
final RecipientMergeHandler recipientMergeHandler, final RecipientMergeHandler recipientMergeHandler,
final SelfAddressProvider selfAddressProvider, final SelfAddressProvider selfAddressProvider,
final SelfProfileKeyProvider selfProfileKeyProvider,
final Database database final Database database
) { ) {
this.recipientMergeHandler = recipientMergeHandler; this.recipientMergeHandler = recipientMergeHandler;
this.selfAddressProvider = selfAddressProvider; this.selfAddressProvider = selfAddressProvider;
this.selfProfileKeyProvider = selfProfileKeyProvider;
this.database = database; this.database = database;
} }
@ -366,6 +369,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
WHERE (r.number IS NOT NULL OR r.uuid IS NOT NULL) AND %s WHERE (r.number IS NOT NULL OR r.uuid IS NOT NULL) AND %s
""" """
).formatted(TABLE_RECIPIENT, sqlWhere.isEmpty() ? "TRUE" : String.join(" AND ", sqlWhere)); ).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 connection = database.getConnection()) {
try (final var statement = connection.prepareStatement(sql)) { try (final var statement = connection.prepareStatement(sql)) {
if (blocked.isPresent()) { if (blocked.isPresent()) {
@ -374,7 +378,14 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
try (var result = Utils.executeQueryForStream(statement, this::getRecipientFromResultSet)) { try (var result = Utils.executeQueryForStream(statement, this::getRecipientFromResultSet)) {
return result.filter(r -> name.isEmpty() || ( return result.filter(r -> name.isEmpty() || (
r.getContact() != null && name.get().equals(r.getContact().getName()) 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) { } catch (SQLException e) {
@ -419,10 +430,14 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
WHERE r.uuid IS NOT NULL AND r.profile_key IS NOT NULL WHERE r.uuid IS NOT NULL AND r.profile_key IS NOT NULL
""" """
).formatted(TABLE_RECIPIENT); ).formatted(TABLE_RECIPIENT);
final var selfServiceId = selfAddressProvider.getSelfAddress().serviceId().orElse(null);
try (final var connection = database.getConnection()) { try (final var connection = database.getConnection()) {
try (final var statement = connection.prepareStatement(sql)) { try (final var statement = connection.prepareStatement(sql)) {
return Utils.executeQueryForStream(statement, resultSet -> { return Utils.executeQueryForStream(statement, resultSet -> {
final var serviceId = ServiceId.parseOrThrow(resultSet.getBytes("uuid")); final var serviceId = ServiceId.parseOrThrow(resultSet.getBytes("uuid"));
if (serviceId.equals(selfServiceId)) {
return new Pair<>(serviceId, selfProfileKeyProvider.getSelfProfileKey());
}
final var profileKey = getProfileKeyFromResultSet(resultSet); final var profileKey = getProfileKeyFromResultSet(resultSet);
return new Pair<>(serviceId, profileKey); return new Pair<>(serviceId, profileKey);
}).filter(Objects::nonNull).collect(Collectors.toMap(Pair::first, Pair::second)); }).filter(Objects::nonNull).collect(Collectors.toMap(Pair::first, Pair::second));
@ -466,6 +481,10 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
@Override @Override
public ProfileKey getProfileKey(final RecipientId recipientId) { 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()) { try (final var connection = database.getConnection()) {
return getProfileKey(connection, recipientId); return getProfileKey(connection, recipientId);
} catch (SQLException e) { } catch (SQLException e) {
@ -491,15 +510,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 @Override
public void storeProfileKey(RecipientId recipientId, final ProfileKey profileKey) { public void storeProfileKey(RecipientId recipientId, final ProfileKey profileKey) {
try (final var connection = database.getConnection()) { 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();
}