1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.SignalDependencies
;
4 import org
.asamk
.signal
.manager
.api
.PhoneNumberSharingMode
;
5 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
6 import org
.asamk
.signal
.manager
.groups
.GroupId
;
7 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
8 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
9 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
10 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
11 import org
.signal
.libsignal
.protocol
.IdentityKey
;
12 import org
.signal
.libsignal
.protocol
.InvalidKeyException
;
13 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
14 import org
.signal
.libsignal
.zkgroup
.groups
.GroupMasterKey
;
15 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
16 import org
.slf4j
.Logger
;
17 import org
.slf4j
.LoggerFactory
;
18 import org
.whispersystems
.signalservice
.api
.storage
.SignalAccountRecord
;
19 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
20 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageRecord
;
21 import org
.whispersystems
.signalservice
.api
.storage
.StorageId
;
22 import org
.whispersystems
.signalservice
.internal
.storage
.protos
.AccountRecord
;
23 import org
.whispersystems
.signalservice
.internal
.storage
.protos
.ManifestRecord
;
25 import java
.io
.IOException
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Collection
;
28 import java
.util
.Collections
;
29 import java
.util
.List
;
30 import java
.util
.Optional
;
31 import java
.util
.stream
.Collectors
;
33 public class StorageHelper
{
35 private final static Logger logger
= LoggerFactory
.getLogger(StorageHelper
.class);
37 private final SignalAccount account
;
38 private final SignalDependencies dependencies
;
39 private final Context context
;
41 public StorageHelper(final Context context
) {
42 this.account
= context
.getAccount();
43 this.dependencies
= context
.getDependencies();
44 this.context
= context
;
47 public void readDataFromStorage() throws IOException
{
48 final var storageKey
= account
.getOrCreateStorageKey();
49 if (storageKey
== null) {
50 logger
.debug("Storage key unknown, requesting from primary device.");
51 context
.getSyncHelper().requestSyncKeys();
55 logger
.debug("Reading data from remote storage");
56 Optional
<SignalStorageManifest
> manifest
;
58 manifest
= dependencies
.getAccountManager()
59 .getStorageManifestIfDifferentVersion(storageKey
, account
.getStorageManifestVersion());
60 } catch (InvalidKeyException e
) {
61 logger
.warn("Manifest couldn't be decrypted, ignoring.");
65 if (manifest
.isEmpty()) {
66 logger
.debug("Manifest is up to date, does not exist or couldn't be decrypted, ignoring.");
70 logger
.trace("Remote storage manifest has {} records", manifest
.get().getStorageIds().size());
71 final var storageIds
= manifest
.get()
74 .filter(id
-> !id
.isUnknown())
75 .collect(Collectors
.toSet());
77 Optional
<SignalStorageManifest
> localManifest
= account
.getStorageManifest();
78 localManifest
.ifPresent(m
-> m
.getStorageIds().forEach(storageIds
::remove
));
80 logger
.trace("Reading {} new records", manifest
.get().getStorageIds().size());
81 for (final var record : getSignalStorageRecords(storageIds
)) {
82 logger
.debug("Reading record of type {}", record.getType());
83 if (record.getType() == ManifestRecord
.Identifier
.Type
.ACCOUNT_VALUE
) {
84 readAccountRecord(record);
85 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV2_VALUE
) {
86 readGroupV2Record(record);
87 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV1_VALUE
) {
88 readGroupV1Record(record);
89 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.CONTACT_VALUE
) {
90 readContactRecord(record);
93 account
.setStorageManifestVersion(manifest
.get().getVersion());
94 account
.setStorageManifest(manifest
.get());
95 logger
.debug("Done reading data from remote storage");
98 private void readContactRecord(final SignalStorageRecord
record) {
99 if (record == null || record.getContact().isEmpty()) {
103 final var contactRecord
= record.getContact().get();
104 final var address
= new RecipientAddress(contactRecord
.getServiceId(), contactRecord
.getNumber().orElse(null));
105 var recipientId
= account
.getRecipientResolver().resolveRecipient(address
);
106 if (contactRecord
.getUsername().isPresent()) {
107 recipientId
= account
.getRecipientTrustedResolver()
108 .resolveRecipientTrusted(contactRecord
.getServiceId(), contactRecord
.getUsername().get());
111 final var contact
= account
.getContactStore().getContact(recipientId
);
112 final var blocked
= contact
!= null && contact
.isBlocked();
113 final var profileShared
= contact
!= null && contact
.isProfileSharingEnabled();
114 final var archived
= contact
!= null && contact
.isArchived();
115 final var contactGivenName
= contact
== null ?
null : contact
.getGivenName();
116 final var contactFamilyName
= contact
== null ?
null : contact
.getFamilyName();
117 if (blocked
!= contactRecord
.isBlocked()
118 || profileShared
!= contactRecord
.isProfileSharingEnabled()
119 || archived
!= contactRecord
.isArchived()
121 contactRecord
.getSystemGivenName().isPresent() && !contactRecord
.getSystemGivenName()
123 .equals(contactGivenName
)
126 contactRecord
.getSystemFamilyName().isPresent() && !contactRecord
.getSystemFamilyName()
128 .equals(contactFamilyName
)
130 logger
.debug("Storing new or updated contact {}", recipientId
);
131 final var contactBuilder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
132 final var newContact
= contactBuilder
.withBlocked(contactRecord
.isBlocked())
133 .withProfileSharingEnabled(contactRecord
.isProfileSharingEnabled())
134 .withArchived(contactRecord
.isArchived());
135 if (contactRecord
.getSystemGivenName().isPresent() || contactRecord
.getSystemFamilyName().isPresent()) {
136 newContact
.withGivenName(contactRecord
.getSystemGivenName().orElse(null))
137 .withFamilyName(contactRecord
.getSystemFamilyName().orElse(null));
139 account
.getContactStore().storeContact(recipientId
, newContact
.build());
142 final var profile
= account
.getProfileStore().getProfile(recipientId
);
143 final var profileGivenName
= profile
== null ?
null : profile
.getGivenName();
144 final var profileFamilyName
= profile
== null ?
null : profile
.getFamilyName();
146 contactRecord
.getProfileGivenName().isPresent() && !contactRecord
.getProfileGivenName()
148 .equals(profileGivenName
)
150 contactRecord
.getProfileFamilyName().isPresent() && !contactRecord
.getProfileFamilyName()
152 .equals(profileFamilyName
)
154 final var profileBuilder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
155 final var newProfile
= profileBuilder
.withGivenName(contactRecord
.getProfileGivenName().orElse(null))
156 .withFamilyName(contactRecord
.getProfileFamilyName().orElse(null))
158 account
.getProfileStore().storeProfile(recipientId
, newProfile
);
160 if (contactRecord
.getProfileKey().isPresent()) {
162 logger
.trace("Storing profile key {}", recipientId
);
163 final var profileKey
= new ProfileKey(contactRecord
.getProfileKey().get());
164 account
.getProfileStore().storeProfileKey(recipientId
, profileKey
);
165 } catch (InvalidInputException e
) {
166 logger
.warn("Received invalid contact profile key from storage");
169 if (contactRecord
.getIdentityKey().isPresent()) {
171 logger
.trace("Storing identity key {}", recipientId
);
172 final var identityKey
= new IdentityKey(contactRecord
.getIdentityKey().get());
173 account
.getIdentityKeyStore().saveIdentity(address
.getServiceId(), identityKey
);
175 final var trustLevel
= TrustLevel
.fromIdentityState(contactRecord
.getIdentityState());
176 if (trustLevel
!= null) {
177 account
.getIdentityKeyStore()
178 .setIdentityTrustLevel(address
.getServiceId(), identityKey
, trustLevel
);
180 } catch (InvalidKeyException e
) {
181 logger
.warn("Received invalid contact identity key from storage");
186 private void readGroupV1Record(final SignalStorageRecord
record) {
187 if (record == null || record.getGroupV1().isEmpty()) {
191 final var groupV1Record
= record.getGroupV1().get();
192 final var groupIdV1
= GroupId
.v1(groupV1Record
.getGroupId());
194 var group
= account
.getGroupStore().getGroup(groupIdV1
);
197 context
.getGroupHelper().sendGroupInfoRequest(groupIdV1
, account
.getSelfRecipientId());
198 } catch (Throwable e
) {
199 logger
.warn("Failed to send group request", e
);
201 group
= account
.getGroupStore().getOrCreateGroupV1(groupIdV1
);
203 if (group
!= null && group
.isBlocked() != groupV1Record
.isBlocked()) {
204 group
.setBlocked(groupV1Record
.isBlocked());
205 account
.getGroupStore().updateGroup(group
);
209 private void readGroupV2Record(final SignalStorageRecord
record) {
210 if (record == null || record.getGroupV2().isEmpty()) {
214 final var groupV2Record
= record.getGroupV2().get();
215 if (groupV2Record
.isArchived()) {
219 final GroupMasterKey groupMasterKey
;
221 groupMasterKey
= new GroupMasterKey(groupV2Record
.getMasterKeyBytes());
222 } catch (InvalidInputException e
) {
223 logger
.warn("Received invalid group master key from storage");
227 final var group
= context
.getGroupHelper().getOrMigrateGroup(groupMasterKey
, 0, null);
228 if (group
.isBlocked() != groupV2Record
.isBlocked()) {
229 group
.setBlocked(groupV2Record
.isBlocked());
230 account
.getGroupStore().updateGroup(group
);
234 private void readAccountRecord(final SignalStorageRecord
record) throws IOException
{
235 if (record == null) {
236 logger
.warn("Could not find account record, even though we had an ID, ignoring.");
240 SignalAccountRecord accountRecord
= record.getAccount().orElse(null);
241 if (accountRecord
== null) {
242 logger
.warn("The storage record didn't actually have an account, ignoring.");
246 if (!accountRecord
.getE164().equals(account
.getNumber())) {
247 context
.getAccountHelper().checkWhoAmiI();
250 account
.getConfigurationStore().setReadReceipts(accountRecord
.isReadReceiptsEnabled());
251 account
.getConfigurationStore().setTypingIndicators(accountRecord
.isTypingIndicatorsEnabled());
252 account
.getConfigurationStore()
253 .setUnidentifiedDeliveryIndicators(accountRecord
.isSealedSenderIndicatorsEnabled());
254 account
.getConfigurationStore().setLinkPreviews(accountRecord
.isLinkPreviewsEnabled());
255 if (accountRecord
.getPhoneNumberSharingMode() != AccountRecord
.PhoneNumberSharingMode
.UNRECOGNIZED
) {
256 account
.getConfigurationStore()
257 .setPhoneNumberSharingMode(switch (accountRecord
.getPhoneNumberSharingMode()) {
258 case EVERYBODY
-> PhoneNumberSharingMode
.EVERYBODY
;
259 case NOBODY
-> PhoneNumberSharingMode
.NOBODY
;
260 default -> PhoneNumberSharingMode
.CONTACTS
;
263 account
.getConfigurationStore().setPhoneNumberUnlisted(accountRecord
.isPhoneNumberUnlisted());
264 account
.setUsername(accountRecord
.getUsername());
266 if (accountRecord
.getProfileKey().isPresent()) {
267 ProfileKey profileKey
;
269 profileKey
= new ProfileKey(accountRecord
.getProfileKey().get());
270 } catch (InvalidInputException e
) {
271 logger
.warn("Received invalid profile key from storage");
274 if (profileKey
!= null) {
275 account
.setProfileKey(profileKey
);
276 final var avatarPath
= accountRecord
.getAvatarUrlPath().orElse(null);
277 context
.getProfileHelper().downloadProfileAvatar(account
.getSelfRecipientId(), avatarPath
, profileKey
);
281 context
.getProfileHelper()
284 accountRecord
.getGivenName().orElse(null),
285 accountRecord
.getFamilyName().orElse(null),
292 private SignalStorageRecord
getSignalStorageRecord(final StorageId accountId
) throws IOException
{
293 List
<SignalStorageRecord
> records
;
295 records
= dependencies
.getAccountManager()
296 .readStorageRecords(account
.getStorageKey(), Collections
.singletonList(accountId
));
297 } catch (InvalidKeyException e
) {
298 logger
.warn("Failed to read storage records, ignoring.");
301 return records
.size() > 0 ? records
.get(0) : null;
304 private List
<SignalStorageRecord
> getSignalStorageRecords(final Collection
<StorageId
> storageIds
) throws IOException
{
305 List
<SignalStorageRecord
> records
;
307 records
= dependencies
.getAccountManager()
308 .readStorageRecords(account
.getStorageKey(), new ArrayList
<>(storageIds
));
309 } catch (InvalidKeyException e
) {
310 logger
.warn("Failed to read storage records, ignoring.");