From: AsamK Date: Sun, 12 Nov 2023 12:13:58 +0000 (+0100) Subject: Don't store self profile key in recipient store X-Git-Tag: v0.12.5~7 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/1bbfa74b3f9807404d10df18ca8265e216fb0f14?hp=7b15e2a39d17f32b186d1aded71f520123ac616c Don't store self profile key in recipient store --- diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java b/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java index 077ff732..60b53b24 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java @@ -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, - this::getSelfRecipientAddress, - getAccountDatabase()); - getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey()); - }); + return getOrCreate(() -> recipientStore, + () -> recipientStore = new RecipientStore(this::mergeRecipients, + this::getSelfRecipientAddress, + 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() { diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/profiles/ProfileStore.java b/lib/src/main/java/org/asamk/signal/manager/storage/profiles/ProfileStore.java index 8128435d..5d3a95a8 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/profiles/ProfileStore.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/profiles/ProfileStore.java @@ -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( diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java index e87dd6bb..9a016136 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java @@ -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()) { diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/SelfProfileKeyProvider.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/SelfProfileKeyProvider.java new file mode 100644 index 00000000..c3f7f000 --- /dev/null +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/SelfProfileKeyProvider.java @@ -0,0 +1,8 @@ +package org.asamk.signal.manager.storage.recipients; + +import org.signal.libsignal.zkgroup.profiles.ProfileKey; + +public interface SelfProfileKeyProvider { + + ProfileKey getSelfProfileKey(); +}