1 package org
.asamk
.signal
.manager
.storage
;
3 import com
.fasterxml
.jackson
.databind
.JsonNode
;
4 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
6 import org
.asamk
.signal
.manager
.api
.Pair
;
7 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
8 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
9 import org
.asamk
.signal
.manager
.groups
.GroupId
;
10 import org
.asamk
.signal
.manager
.helper
.RecipientAddressResolver
;
11 import org
.asamk
.signal
.manager
.storage
.configuration
.ConfigurationStore
;
12 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
13 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
14 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
15 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
16 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
17 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
18 import org
.asamk
.signal
.manager
.storage
.identities
.SignalIdentityKeyStore
;
19 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
20 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
21 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
22 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
23 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
24 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
25 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
26 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
27 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
28 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
29 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore2
;
30 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
31 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
32 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
33 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
34 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
35 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientTrustedResolver
;
36 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
37 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
38 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
39 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
40 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
41 import org
.asamk
.signal
.manager
.util
.IOUtils
;
42 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
43 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
44 import org
.signal
.libsignal
.protocol
.InvalidMessageException
;
45 import org
.signal
.libsignal
.protocol
.SignalProtocolAddress
;
46 import org
.signal
.libsignal
.protocol
.state
.PreKeyRecord
;
47 import org
.signal
.libsignal
.protocol
.state
.SessionRecord
;
48 import org
.signal
.libsignal
.protocol
.state
.SignedPreKeyRecord
;
49 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
50 import org
.signal
.libsignal
.protocol
.util
.Medium
;
51 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
52 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
53 import org
.slf4j
.Logger
;
54 import org
.slf4j
.LoggerFactory
;
55 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
56 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
57 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
58 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
59 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
60 import org
.whispersystems
.signalservice
.api
.push
.DistributionId
;
61 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
62 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
63 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
64 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
65 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
66 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
67 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
68 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
70 import java
.io
.ByteArrayInputStream
;
71 import java
.io
.ByteArrayOutputStream
;
72 import java
.io
.Closeable
;
74 import java
.io
.FileInputStream
;
75 import java
.io
.FileOutputStream
;
76 import java
.io
.IOException
;
77 import java
.io
.RandomAccessFile
;
78 import java
.nio
.channels
.Channels
;
79 import java
.nio
.channels
.ClosedChannelException
;
80 import java
.nio
.channels
.FileChannel
;
81 import java
.nio
.channels
.FileLock
;
82 import java
.nio
.file
.Files
;
83 import java
.security
.SecureRandom
;
84 import java
.sql
.SQLException
;
85 import java
.util
.Base64
;
86 import java
.util
.Comparator
;
87 import java
.util
.HashSet
;
88 import java
.util
.List
;
89 import java
.util
.Optional
;
90 import java
.util
.function
.Supplier
;
92 public class SignalAccount
implements Closeable
{
94 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
96 private static final int MINIMUM_STORAGE_VERSION
= 1;
97 private static final int CURRENT_STORAGE_VERSION
= 5;
99 private final Object LOCK
= new Object();
101 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
103 private final FileChannel fileChannel
;
104 private final FileLock lock
;
106 private int previousStorageVersion
;
108 private File dataPath
;
109 private String accountPath
;
110 private ServiceEnvironment serviceEnvironment
;
111 private String number
;
114 private String encryptedDeviceName
;
115 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
116 private boolean isMultiDevice
= false;
117 private String password
;
118 private String registrationLockPin
;
119 private MasterKey pinMasterKey
;
120 private StorageKey storageKey
;
121 private long storageManifestVersion
= -1;
122 private ProfileKey profileKey
;
123 private int aciPreKeyIdOffset
= 1;
124 private int aciNextSignedPreKeyId
= 1;
125 private int pniPreKeyIdOffset
= 1;
126 private int pniNextSignedPreKeyId
= 1;
127 private IdentityKeyPair aciIdentityKeyPair
;
128 private IdentityKeyPair pniIdentityKeyPair
;
129 private int localRegistrationId
;
130 private int localPniRegistrationId
;
131 private TrustNewIdentity trustNewIdentity
;
132 private long lastReceiveTimestamp
= 0;
134 private boolean registered
= false;
136 private SignalProtocolStore signalProtocolStore
;
137 private PreKeyStore aciPreKeyStore
;
138 private SignedPreKeyStore aciSignedPreKeyStore
;
139 private PreKeyStore pniPreKeyStore
;
140 private SignedPreKeyStore pniSignedPreKeyStore
;
141 private SessionStore sessionStore
;
142 private IdentityKeyStore identityKeyStore
;
143 private SignalIdentityKeyStore aciIdentityKeyStore
;
144 private SenderKeyStore senderKeyStore
;
145 private GroupStore groupStore
;
146 private GroupStore
.Storage groupStoreStorage
;
147 private RecipientStore recipientStore
;
148 private StickerStore stickerStore
;
149 private StickerStore
.Storage stickerStoreStorage
;
150 private ConfigurationStore configurationStore
;
151 private ConfigurationStore
.Storage configurationStoreStorage
;
153 private MessageCache messageCache
;
154 private MessageSendLogStore messageSendLogStore
;
156 private AccountDatabase accountDatabase
;
158 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
159 this.fileChannel
= fileChannel
;
163 public static SignalAccount
load(
164 File dataPath
, String accountPath
, boolean waitForLock
, final TrustNewIdentity trustNewIdentity
165 ) throws IOException
{
166 logger
.trace("Opening account file");
167 final var fileName
= getFileName(dataPath
, accountPath
);
168 final var pair
= openFileChannel(fileName
, waitForLock
);
170 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
171 logger
.trace("Loading account file");
172 signalAccount
.load(dataPath
, accountPath
, trustNewIdentity
);
173 logger
.trace("Migrating legacy parts of account file");
174 signalAccount
.migrateLegacyConfigs();
176 return signalAccount
;
177 } catch (Throwable e
) {
178 pair
.second().close();
179 pair
.first().close();
184 public static SignalAccount
create(
188 ServiceEnvironment serviceEnvironment
,
189 IdentityKeyPair aciIdentityKey
,
190 IdentityKeyPair pniIdentityKey
,
192 int pniRegistrationId
,
193 ProfileKey profileKey
,
194 final TrustNewIdentity trustNewIdentity
195 ) throws IOException
{
196 IOUtils
.createPrivateDirectories(dataPath
);
197 var fileName
= getFileName(dataPath
, accountPath
);
198 if (!fileName
.exists()) {
199 IOUtils
.createPrivateFile(fileName
);
202 final var pair
= openFileChannel(fileName
, true);
203 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
205 signalAccount
.accountPath
= accountPath
;
206 signalAccount
.number
= number
;
207 signalAccount
.serviceEnvironment
= serviceEnvironment
;
208 signalAccount
.profileKey
= profileKey
;
210 signalAccount
.dataPath
= dataPath
;
211 signalAccount
.aciIdentityKeyPair
= aciIdentityKey
;
212 signalAccount
.pniIdentityKeyPair
= pniIdentityKey
;
213 signalAccount
.localRegistrationId
= registrationId
;
214 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
215 signalAccount
.trustNewIdentity
= trustNewIdentity
;
216 signalAccount
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, accountPath
),
217 signalAccount
.getRecipientResolver(),
218 signalAccount
::saveGroupStore
);
219 signalAccount
.stickerStore
= new StickerStore(signalAccount
::saveStickerStore
);
220 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
222 signalAccount
.registered
= false;
224 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
225 signalAccount
.migrateLegacyConfigs();
226 signalAccount
.save();
228 return signalAccount
;
231 public static SignalAccount
createOrUpdateLinkedAccount(
235 ServiceEnvironment serviceEnvironment
,
239 String encryptedDeviceName
,
241 IdentityKeyPair aciIdentityKey
,
242 IdentityKeyPair pniIdentityKey
,
244 int pniRegistrationId
,
245 ProfileKey profileKey
,
246 final TrustNewIdentity trustNewIdentity
247 ) throws IOException
{
248 IOUtils
.createPrivateDirectories(dataPath
);
249 var fileName
= getFileName(dataPath
, accountPath
);
250 if (!fileName
.exists()) {
251 return createLinkedAccount(dataPath
,
268 final var signalAccount
= load(dataPath
, accountPath
, true, trustNewIdentity
);
269 signalAccount
.setProvisioningData(number
,
278 signalAccount
.getRecipientTrustedResolver()
279 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
280 signalAccount
.getSessionStore().archiveAllSessions();
281 signalAccount
.getSenderKeyStore().deleteAll();
282 signalAccount
.clearAllPreKeys();
283 return signalAccount
;
286 public void initDatabase() {
287 getAccountDatabase();
290 private void clearAllPreKeys() {
291 this.aciPreKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
292 this.aciNextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
293 this.pniPreKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
294 this.pniNextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
295 this.getAciPreKeyStore().removeAllPreKeys();
296 this.getAciSignedPreKeyStore().removeAllSignedPreKeys();
297 this.getPniPreKeyStore().removeAllPreKeys();
298 this.getPniSignedPreKeyStore().removeAllSignedPreKeys();
302 private static SignalAccount
createLinkedAccount(
306 ServiceEnvironment serviceEnvironment
,
310 String encryptedDeviceName
,
312 IdentityKeyPair aciIdentityKey
,
313 IdentityKeyPair pniIdentityKey
,
315 int pniRegistrationId
,
316 ProfileKey profileKey
,
317 final TrustNewIdentity trustNewIdentity
318 ) throws IOException
{
319 var fileName
= getFileName(dataPath
, accountPath
);
320 IOUtils
.createPrivateFile(fileName
);
322 final var pair
= openFileChannel(fileName
, true);
323 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
325 signalAccount
.dataPath
= dataPath
;
326 signalAccount
.accountPath
= accountPath
;
327 signalAccount
.serviceEnvironment
= serviceEnvironment
;
328 signalAccount
.localRegistrationId
= registrationId
;
329 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
330 signalAccount
.trustNewIdentity
= trustNewIdentity
;
331 signalAccount
.setProvisioningData(number
,
341 signalAccount
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, accountPath
),
342 signalAccount
.getRecipientResolver(),
343 signalAccount
::saveGroupStore
);
344 signalAccount
.stickerStore
= new StickerStore(signalAccount
::saveStickerStore
);
345 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
347 signalAccount
.getRecipientTrustedResolver()
348 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
349 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
350 signalAccount
.migrateLegacyConfigs();
351 signalAccount
.clearAllPreKeys();
352 signalAccount
.save();
354 return signalAccount
;
357 private void setProvisioningData(
361 final String password
,
362 final String encryptedDeviceName
,
364 final IdentityKeyPair aciIdentity
,
365 final IdentityKeyPair pniIdentity
,
366 final ProfileKey profileKey
368 this.number
= number
;
371 this.password
= password
;
372 this.profileKey
= profileKey
;
373 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
374 this.encryptedDeviceName
= encryptedDeviceName
;
375 this.deviceId
= deviceId
;
376 this.aciIdentityKeyPair
= aciIdentity
;
377 this.pniIdentityKeyPair
= pniIdentity
;
378 this.registered
= true;
379 this.isMultiDevice
= true;
380 this.lastReceiveTimestamp
= 0;
381 this.pinMasterKey
= null;
382 this.storageManifestVersion
= -1;
383 this.setStorageManifest(null);
384 this.storageKey
= null;
387 private void migrateLegacyConfigs() {
388 if (getPassword() == null) {
389 setPassword(KeyUtils
.createPassword());
392 if (getProfileKey() == null) {
393 // Old config file, creating new profile key
394 setProfileKey(KeyUtils
.createProfileKey());
396 if (previousStorageVersion
< 3) {
397 for (final var group
: groupStore
.getGroups()) {
398 if (group
instanceof GroupInfoV2
&& group
.getDistributionId() == null) {
399 ((GroupInfoV2
) group
).setDistributionId(DistributionId
.create());
400 groupStore
.updateGroup(group
);
405 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
406 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
410 private void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
411 getSessionStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
412 getIdentityKeyStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
413 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
414 getGroupStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
415 getSenderKeyStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
418 public void removeRecipient(final RecipientId recipientId
) {
419 getSessionStore().deleteAllSessions(recipientId
);
420 getIdentityKeyStore().deleteIdentity(recipientId
);
421 getMessageCache().deleteMessages(recipientId
);
422 getSenderKeyStore().deleteAll(recipientId
);
423 getRecipientStore().deleteRecipientData(recipientId
);
426 public static File
getFileName(File dataPath
, String account
) {
427 return new File(dataPath
, account
);
430 private static File
getUserPath(final File dataPath
, final String account
) {
431 final var path
= new File(dataPath
, account
+ ".d");
433 IOUtils
.createPrivateDirectories(path
);
434 } catch (IOException e
) {
435 throw new AssertionError("Failed to create user path", e
);
440 private static File
getMessageCachePath(File dataPath
, String account
) {
441 return new File(getUserPath(dataPath
, account
), "msg-cache");
444 private static File
getGroupCachePath(File dataPath
, String account
) {
445 return new File(getUserPath(dataPath
, account
), "group-cache");
448 private static File
getAciPreKeysPath(File dataPath
, String account
) {
449 return new File(getUserPath(dataPath
, account
), "pre-keys");
452 private static File
getAciSignedPreKeysPath(File dataPath
, String account
) {
453 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
456 private static File
getPniPreKeysPath(File dataPath
, String account
) {
457 return new File(getUserPath(dataPath
, account
), "pre-keys-pni");
460 private static File
getPniSignedPreKeysPath(File dataPath
, String account
) {
461 return new File(getUserPath(dataPath
, account
), "signed-pre-keys-pni");
464 private static File
getIdentitiesPath(File dataPath
, String account
) {
465 return new File(getUserPath(dataPath
, account
), "identities");
468 private static File
getSessionsPath(File dataPath
, String account
) {
469 return new File(getUserPath(dataPath
, account
), "sessions");
472 private static File
getSenderKeysPath(File dataPath
, String account
) {
473 return new File(getUserPath(dataPath
, account
), "sender-keys");
476 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
477 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
480 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
481 return new File(getUserPath(dataPath
, account
), "recipients-store");
484 private static File
getStorageManifestFile(File dataPath
, String account
) {
485 return new File(getUserPath(dataPath
, account
), "storage-manifest");
488 private static File
getDatabaseFile(File dataPath
, String account
) {
489 return new File(getUserPath(dataPath
, account
), "account.db");
492 public static boolean accountFileExists(File dataPath
, String account
) {
493 if (account
== null) {
496 var f
= getFileName(dataPath
, account
);
497 return !(!f
.exists() || f
.isDirectory());
501 File dataPath
, String accountPath
, final TrustNewIdentity trustNewIdentity
502 ) throws IOException
{
503 this.dataPath
= dataPath
;
504 this.accountPath
= accountPath
;
505 final JsonNode rootNode
;
506 synchronized (fileChannel
) {
507 fileChannel
.position(0);
508 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
511 var migratedLegacyConfig
= false;
513 if (rootNode
.hasNonNull("version")) {
514 var accountVersion
= rootNode
.get("version").asInt(1);
515 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
516 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
517 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
518 throw new IOException("Config file was created by a no longer supported older version: "
521 previousStorageVersion
= accountVersion
;
522 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
523 migratedLegacyConfig
= true;
527 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
528 if (rootNode
.hasNonNull("password")) {
529 password
= rootNode
.get("password").asText();
531 if (rootNode
.hasNonNull("serviceEnvironment")) {
532 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
534 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
535 if (rootNode
.hasNonNull("uuid")) {
537 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
538 } catch (IllegalArgumentException e
) {
539 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
542 if (rootNode
.hasNonNull("pni")) {
544 pni
= PNI
.parseOrThrow(rootNode
.get("pni").asText());
545 } catch (IllegalArgumentException e
) {
546 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
549 if (rootNode
.hasNonNull("deviceName")) {
550 encryptedDeviceName
= rootNode
.get("deviceName").asText();
552 if (rootNode
.hasNonNull("deviceId")) {
553 deviceId
= rootNode
.get("deviceId").asInt();
555 if (rootNode
.hasNonNull("isMultiDevice")) {
556 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
558 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
559 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
561 int registrationId
= 0;
562 if (rootNode
.hasNonNull("registrationId")) {
563 registrationId
= rootNode
.get("registrationId").asInt();
565 if (rootNode
.hasNonNull("pniRegistrationId")) {
566 localPniRegistrationId
= rootNode
.get("pniRegistrationId").asInt();
568 localPniRegistrationId
= KeyHelper
.generateRegistrationId(false);
570 IdentityKeyPair aciIdentityKeyPair
= null;
571 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
572 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
573 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
574 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
576 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
577 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
578 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
579 pniIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
582 if (rootNode
.hasNonNull("registrationLockPin")) {
583 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
585 if (rootNode
.hasNonNull("pinMasterKey")) {
586 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
588 if (rootNode
.hasNonNull("storageKey")) {
589 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
591 if (rootNode
.hasNonNull("storageManifestVersion")) {
592 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
594 if (rootNode
.hasNonNull("preKeyIdOffset")) {
595 aciPreKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
597 aciPreKeyIdOffset
= 1;
599 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
600 aciNextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
602 aciNextSignedPreKeyId
= 1;
604 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
605 pniPreKeyIdOffset
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
607 pniPreKeyIdOffset
= 1;
609 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
610 pniNextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
612 pniNextSignedPreKeyId
= 1;
614 if (rootNode
.hasNonNull("profileKey")) {
616 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
617 } catch (InvalidInputException e
) {
618 throw new IOException(
619 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
624 if (previousStorageVersion
< 5) {
625 final var legacyRecipientsStoreFile
= getRecipientsStoreFile(dataPath
, accountPath
);
626 if (legacyRecipientsStoreFile
.exists()) {
627 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
628 // Ensure our profile key is stored in profile store
629 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
630 migratedLegacyConfig
= true;
633 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
634 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
635 LegacyJsonSignalProtocolStore
.class)
637 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
638 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
639 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
640 migratedLegacyConfig
= true;
643 this.aciIdentityKeyPair
= aciIdentityKeyPair
;
644 this.localRegistrationId
= registrationId
;
645 this.trustNewIdentity
= trustNewIdentity
;
647 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
649 if (rootNode
.hasNonNull("groupStore")) {
650 groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"), GroupStore
.Storage
.class);
651 groupStore
= GroupStore
.fromStorage(groupStoreStorage
,
652 getGroupCachePath(dataPath
, accountPath
),
653 getRecipientResolver(),
654 this::saveGroupStore
);
656 groupStore
= new GroupStore(getGroupCachePath(dataPath
, accountPath
),
657 getRecipientResolver(),
658 this::saveGroupStore
);
661 if (rootNode
.hasNonNull("stickerStore")) {
662 stickerStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"), StickerStore
.Storage
.class);
663 stickerStore
= StickerStore
.fromStorage(stickerStoreStorage
, this::saveStickerStore
);
665 stickerStore
= new StickerStore(this::saveStickerStore
);
668 if (rootNode
.hasNonNull("configurationStore")) {
669 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
670 ConfigurationStore
.Storage
.class);
671 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
672 this::saveConfigurationStore
);
674 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
677 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
679 if (migratedLegacyConfig
) {
684 private boolean loadLegacyStores(
685 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
687 var migrated
= false;
688 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
689 if (legacyRecipientStoreNode
!= null) {
690 logger
.debug("Migrating legacy recipient store.");
691 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
692 if (legacyRecipientStore
!= null) {
693 legacyRecipientStore
.getAddresses()
694 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
696 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
700 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
701 logger
.debug("Migrating legacy pre key store.");
702 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
704 getAciPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
705 } catch (InvalidMessageException e
) {
706 logger
.warn("Failed to migrate pre key, ignoring", e
);
712 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
713 logger
.debug("Migrating legacy signed pre key store.");
714 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
716 getAciSignedPreKeyStore().storeSignedPreKey(entry
.getKey(),
717 new SignedPreKeyRecord(entry
.getValue()));
718 } catch (InvalidMessageException e
) {
719 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
725 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
726 logger
.debug("Migrating legacy session store.");
727 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
729 getSessionStore().storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
730 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
731 } catch (Exception e
) {
732 logger
.warn("Failed to migrate session, ignoring", e
);
738 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
739 logger
.debug("Migrating legacy identity session store.");
740 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
741 RecipientId recipientId
= getRecipientStore().resolveRecipientTrusted(identity
.getAddress());
742 getIdentityKeyStore().saveIdentity(recipientId
, identity
.getIdentityKey(), identity
.getDateAdded());
743 getIdentityKeyStore().setIdentityTrustLevel(recipientId
,
744 identity
.getIdentityKey(),
745 identity
.getTrustLevel());
750 if (rootNode
.hasNonNull("contactStore")) {
751 logger
.debug("Migrating legacy contact store.");
752 final var contactStoreNode
= rootNode
.get("contactStore");
753 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
754 for (var contact
: contactStore
.getContacts()) {
755 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
756 getContactStore().storeContact(recipientId
,
757 new Contact(contact
.name
,
760 contact
.messageExpirationTime
,
765 // Store profile keys only in profile store
766 var profileKeyString
= contact
.profileKey
;
767 if (profileKeyString
!= null) {
768 final ProfileKey profileKey
;
770 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
771 getProfileStore().storeProfileKey(recipientId
, profileKey
);
772 } catch (InvalidInputException e
) {
773 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
780 if (rootNode
.hasNonNull("profileStore")) {
781 logger
.debug("Migrating legacy profile store.");
782 var profileStoreNode
= rootNode
.get("profileStore");
783 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
784 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
785 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.getAddress());
786 // Not migrating profile key credential here, it was changed to expiring profile key credentials
787 getProfileStore().storeProfileKey(recipientId
, profileEntry
.getProfileKey());
788 final var profile
= profileEntry
.getProfile();
789 if (profile
!= null) {
790 final var capabilities
= new HashSet
<Profile
.Capability
>();
791 if (profile
.getCapabilities() != null) {
792 if (profile
.getCapabilities().gv1Migration
) {
793 capabilities
.add(Profile
.Capability
.gv1Migration
);
795 if (profile
.getCapabilities().storage
) {
796 capabilities
.add(Profile
.Capability
.storage
);
799 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
800 profile
.getGivenName(),
801 profile
.getFamilyName(),
803 profile
.getAboutEmoji(),
806 profile
.isUnrestrictedUnidentifiedAccess()
807 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
808 : profile
.getUnidentifiedAccess() != null
809 ? Profile
.UnidentifiedAccessMode
.ENABLED
810 : Profile
.UnidentifiedAccessMode
.DISABLED
,
812 getProfileStore().storeProfile(recipientId
, newProfile
);
820 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
821 var threadStoreNode
= rootNode
.get("threadStore");
822 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
823 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
824 // Migrate thread info to group and contact store
825 for (var thread
: threadStore
.getThreads()) {
826 if (thread
.id
== null || thread
.id
.isEmpty()) {
830 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
831 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
832 var contact
= getContactStore().getContact(recipientId
);
833 if (contact
!= null) {
834 getContactStore().storeContact(recipientId
,
835 Contact
.newBuilder(contact
)
836 .withMessageExpirationTime(thread
.messageExpirationTime
)
840 var groupInfo
= groupStore
.getGroup(GroupId
.fromBase64(thread
.id
));
841 if (groupInfo
instanceof GroupInfoV1
) {
842 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
843 groupStore
.updateGroup(groupInfo
);
846 } catch (Exception e
) {
847 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
856 private void saveStickerStore(StickerStore
.Storage storage
) {
857 this.stickerStoreStorage
= storage
;
861 private void saveGroupStore(GroupStore
.Storage storage
) {
862 this.groupStoreStorage
= storage
;
866 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
867 this.configurationStoreStorage
= storage
;
871 private void save() {
872 synchronized (fileChannel
) {
873 var rootNode
= jsonProcessor
.createObjectNode();
874 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
875 .put("username", number
)
876 .put("serviceEnvironment", serviceEnvironment
== null ?
null : serviceEnvironment
.name())
877 .put("uuid", aci
== null ?
null : aci
.toString())
878 .put("pni", pni
== null ?
null : pni
.toString())
879 .put("deviceName", encryptedDeviceName
)
880 .put("deviceId", deviceId
)
881 .put("isMultiDevice", isMultiDevice
)
882 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
883 .put("password", password
)
884 .put("registrationId", localRegistrationId
)
885 .put("pniRegistrationId", localPniRegistrationId
)
886 .put("identityPrivateKey",
887 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPrivateKey().serialize()))
889 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPublicKey().serialize()))
890 .put("pniIdentityPrivateKey",
891 pniIdentityKeyPair
== null
893 : Base64
.getEncoder()
894 .encodeToString(pniIdentityKeyPair
.getPrivateKey().serialize()))
895 .put("pniIdentityKey",
896 pniIdentityKeyPair
== null
898 : Base64
.getEncoder().encodeToString(pniIdentityKeyPair
.getPublicKey().serialize()))
899 .put("registrationLockPin", registrationLockPin
)
901 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
903 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
904 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
905 .put("preKeyIdOffset", aciPreKeyIdOffset
)
906 .put("nextSignedPreKeyId", aciNextSignedPreKeyId
)
907 .put("pniPreKeyIdOffset", pniPreKeyIdOffset
)
908 .put("pniNextSignedPreKeyId", pniNextSignedPreKeyId
)
910 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
911 .put("registered", registered
)
912 .putPOJO("groupStore", groupStoreStorage
)
913 .putPOJO("stickerStore", stickerStoreStorage
)
914 .putPOJO("configurationStore", configurationStoreStorage
);
916 try (var output
= new ByteArrayOutputStream()) {
917 // Write to memory first to prevent corrupting the file in case of serialization errors
918 jsonProcessor
.writeValue(output
, rootNode
);
919 var input
= new ByteArrayInputStream(output
.toByteArray());
920 fileChannel
.position(0);
921 input
.transferTo(Channels
.newOutputStream(fileChannel
));
922 fileChannel
.truncate(fileChannel
.position());
923 fileChannel
.force(false);
925 } catch (Exception e
) {
926 logger
.error("Error saving file: {}", e
.getMessage(), e
);
931 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
932 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
934 var lock
= fileChannel
.tryLock();
937 logger
.debug("Config file is in use by another instance.");
938 throw new IOException("Config file is in use by another instance.");
940 logger
.info("Config file is in use by another instance, waiting…");
941 lock
= fileChannel
.lock();
942 logger
.info("Config file lock acquired.");
944 final var result
= new Pair
<>(fileChannel
, lock
);
948 if (fileChannel
!= null) {
954 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
955 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
956 addAciPreKeys(records
);
958 addPniPreKeys(records
);
962 public void addAciPreKeys(List
<PreKeyRecord
> records
) {
963 for (var record : records
) {
964 if (aciPreKeyIdOffset
!= record.getId()) {
965 logger
.error("Invalid pre key id {}, expected {}", record.getId(), aciPreKeyIdOffset
);
966 throw new AssertionError("Invalid pre key id");
968 getAciPreKeyStore().storePreKey(record.getId(), record);
969 aciPreKeyIdOffset
= (aciPreKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
974 public void addPniPreKeys(List
<PreKeyRecord
> records
) {
975 for (var record : records
) {
976 if (pniPreKeyIdOffset
!= record.getId()) {
977 logger
.error("Invalid pre key id {}, expected {}", record.getId(), pniPreKeyIdOffset
);
978 throw new AssertionError("Invalid pre key id");
980 getPniPreKeyStore().storePreKey(record.getId(), record);
981 pniPreKeyIdOffset
= (pniPreKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
986 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
987 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
988 addAciSignedPreKey(record);
990 addPniSignedPreKey(record);
994 public void addAciSignedPreKey(SignedPreKeyRecord
record) {
995 if (aciNextSignedPreKeyId
!= record.getId()) {
996 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), aciNextSignedPreKeyId
);
997 throw new AssertionError("Invalid signed pre key id");
999 getAciSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1000 aciNextSignedPreKeyId
= (aciNextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
1004 public void addPniSignedPreKey(SignedPreKeyRecord
record) {
1005 if (pniNextSignedPreKeyId
!= record.getId()) {
1006 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), pniNextSignedPreKeyId
);
1007 throw new AssertionError("Invalid signed pre key id");
1009 getPniSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1010 pniNextSignedPreKeyId
= (pniNextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
1014 public int getPreviousStorageVersion() {
1015 return previousStorageVersion
;
1018 public SignalServiceDataStore
getSignalServiceDataStore() {
1019 return new SignalServiceDataStore() {
1021 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1022 if (accountIdentifier
.equals(aci
)) {
1023 return getSignalServiceAccountDataStore();
1024 } else if (accountIdentifier
.equals(pni
)) {
1025 throw new AssertionError("PNI not to be used yet!");
1027 throw new IllegalArgumentException("No matching store found for " + accountIdentifier
);
1032 public SignalServiceAccountDataStore
aci() {
1033 return getSignalServiceAccountDataStore();
1037 public SignalServiceAccountDataStore
pni() {
1038 throw new AssertionError("PNI not to be used yet!");
1042 public boolean isMultiDevice() {
1043 return SignalAccount
.this.isMultiDevice();
1048 public SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1049 return getOrCreate(() -> signalProtocolStore
,
1050 () -> signalProtocolStore
= new SignalProtocolStore(getAciPreKeyStore(),
1051 getAciSignedPreKeyStore(),
1053 getAciIdentityKeyStore(),
1054 getSenderKeyStore(),
1055 this::isMultiDevice
));
1058 private PreKeyStore
getAciPreKeyStore() {
1059 return getOrCreate(() -> aciPreKeyStore
,
1060 () -> aciPreKeyStore
= new PreKeyStore(getAciPreKeysPath(dataPath
, accountPath
)));
1063 private SignedPreKeyStore
getAciSignedPreKeyStore() {
1064 return getOrCreate(() -> aciSignedPreKeyStore
,
1065 () -> aciSignedPreKeyStore
= new SignedPreKeyStore(getAciSignedPreKeysPath(dataPath
, accountPath
)));
1068 private PreKeyStore
getPniPreKeyStore() {
1069 return getOrCreate(() -> pniPreKeyStore
,
1070 () -> pniPreKeyStore
= new PreKeyStore(getPniPreKeysPath(dataPath
, accountPath
)));
1073 private SignedPreKeyStore
getPniSignedPreKeyStore() {
1074 return getOrCreate(() -> pniSignedPreKeyStore
,
1075 () -> pniSignedPreKeyStore
= new SignedPreKeyStore(getPniSignedPreKeysPath(dataPath
, accountPath
)));
1078 public SessionStore
getSessionStore() {
1079 return getOrCreate(() -> sessionStore
,
1080 () -> sessionStore
= new SessionStore(getSessionsPath(dataPath
, accountPath
), getRecipientResolver()));
1083 public IdentityKeyStore
getIdentityKeyStore() {
1084 return getOrCreate(() -> identityKeyStore
,
1085 () -> identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, accountPath
),
1086 getRecipientResolver(),
1090 public SignalIdentityKeyStore
getAciIdentityKeyStore() {
1091 return getOrCreate(() -> aciIdentityKeyStore
,
1092 () -> aciIdentityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1093 () -> aciIdentityKeyPair
,
1094 localRegistrationId
,
1095 getIdentityKeyStore()));
1098 public GroupStore
getGroupStore() {
1102 public ContactsStore
getContactStore() {
1103 return getRecipientStore();
1106 public RecipientResolver
getRecipientResolver() {
1107 return new RecipientResolver() {
1109 public RecipientId
resolveRecipient(final RecipientAddress address
) {
1110 return getRecipientStore().resolveRecipient(address
);
1114 public RecipientId
resolveRecipient(final long recipientId
) {
1115 return getRecipientStore().resolveRecipient(recipientId
);
1120 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1121 return new RecipientTrustedResolver() {
1123 public RecipientId
resolveSelfRecipientTrusted(final RecipientAddress address
) {
1124 return getRecipientStore().resolveSelfRecipientTrusted(address
);
1128 public RecipientId
resolveRecipientTrusted(final SignalServiceAddress address
) {
1129 return getRecipientStore().resolveRecipientTrusted(address
);
1134 public RecipientAddressResolver
getRecipientAddressResolver() {
1135 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1138 public RecipientStore
getRecipientStore() {
1139 return getOrCreate(() -> recipientStore
,
1140 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1141 this::getSelfRecipientAddress
,
1142 getAccountDatabase()));
1145 public ProfileStore
getProfileStore() {
1146 return getRecipientStore();
1149 public StickerStore
getStickerStore() {
1150 return stickerStore
;
1153 public SenderKeyStore
getSenderKeyStore() {
1154 return getOrCreate(() -> senderKeyStore
,
1155 () -> senderKeyStore
= new SenderKeyStore(getSharedSenderKeysFile(dataPath
, accountPath
),
1156 getSenderKeysPath(dataPath
, accountPath
),
1157 getRecipientAddressResolver(),
1158 getRecipientResolver()));
1161 public ConfigurationStore
getConfigurationStore() {
1162 return configurationStore
;
1165 public MessageCache
getMessageCache() {
1166 return getOrCreate(() -> messageCache
,
1167 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1170 public AccountDatabase
getAccountDatabase() {
1171 return getOrCreate(() -> accountDatabase
, () -> {
1173 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1174 } catch (SQLException e
) {
1175 throw new RuntimeException(e
);
1180 public MessageSendLogStore
getMessageSendLogStore() {
1181 return getOrCreate(() -> messageSendLogStore
,
1182 () -> messageSendLogStore
= new MessageSendLogStore(getRecipientResolver(), getAccountDatabase()));
1185 public CredentialsProvider
getCredentialsProvider() {
1186 return new CredentialsProvider() {
1188 public ACI
getAci() {
1193 public PNI
getPni() {
1198 public String
getE164() {
1203 public String
getPassword() {
1208 public int getDeviceId() {
1214 public String
getNumber() {
1218 public void setNumber(final String number
) {
1219 this.number
= number
;
1223 public ServiceEnvironment
getServiceEnvironment() {
1224 return serviceEnvironment
;
1227 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1228 this.serviceEnvironment
= serviceEnvironment
;
1232 public ACI
getAci() {
1236 public void setAci(final ACI aci
) {
1241 public PNI
getPni() {
1245 public void setPni(final PNI pni
) {
1250 public SignalServiceAddress
getSelfAddress() {
1251 return new SignalServiceAddress(aci
, number
);
1254 public RecipientAddress
getSelfRecipientAddress() {
1255 return new RecipientAddress(aci
== null ?
null : aci
.uuid(), number
);
1258 public RecipientId
getSelfRecipientId() {
1259 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1262 public byte[] getEncryptedDeviceName() {
1263 return encryptedDeviceName
== null ?
null : Base64
.getDecoder().decode(encryptedDeviceName
);
1266 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1267 this.encryptedDeviceName
= encryptedDeviceName
;
1271 public int getDeviceId() {
1275 public boolean isPrimaryDevice() {
1276 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1279 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1280 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciIdentityKeyPair
: pniIdentityKeyPair
;
1283 public IdentityKeyPair
getAciIdentityKeyPair() {
1284 return aciIdentityKeyPair
;
1287 public IdentityKeyPair
getPniIdentityKeyPair() {
1288 return pniIdentityKeyPair
;
1291 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1292 pniIdentityKeyPair
= identityKeyPair
;
1296 public int getLocalRegistrationId() {
1297 return localRegistrationId
;
1300 public int getLocalPniRegistrationId() {
1301 return localPniRegistrationId
;
1304 public String
getPassword() {
1308 private void setPassword(final String password
) {
1309 this.password
= password
;
1313 public void setRegistrationLockPin(final String registrationLockPin
) {
1314 this.registrationLockPin
= registrationLockPin
;
1318 public String
getRegistrationLockPin() {
1319 return registrationLockPin
;
1322 public String
getRegistrationLock() {
1323 final var masterKey
= getPinBackedMasterKey();
1324 if (masterKey
== null) {
1327 return masterKey
.deriveRegistrationLock();
1330 public MasterKey
getPinBackedMasterKey() {
1331 if (registrationLockPin
== null) {
1334 return pinMasterKey
;
1337 public MasterKey
getOrCreatePinMasterKey() {
1338 if (pinMasterKey
== null) {
1339 pinMasterKey
= KeyUtils
.createMasterKey();
1342 return pinMasterKey
;
1345 public StorageKey
getStorageKey() {
1346 if (pinMasterKey
!= null) {
1347 return pinMasterKey
.deriveStorageServiceKey();
1352 public StorageKey
getOrCreateStorageKey() {
1353 if (isPrimaryDevice()) {
1354 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1359 public void setStorageKey(final StorageKey storageKey
) {
1360 if (storageKey
.equals(this.storageKey
)) {
1363 this.storageKey
= storageKey
;
1367 public long getStorageManifestVersion() {
1368 return this.storageManifestVersion
;
1371 public void setStorageManifestVersion(final long storageManifestVersion
) {
1372 if (storageManifestVersion
== this.storageManifestVersion
) {
1375 this.storageManifestVersion
= storageManifestVersion
;
1379 public Optional
<SignalStorageManifest
> getStorageManifest() {
1380 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1381 if (!storageManifestFile
.exists()) {
1382 return Optional
.empty();
1384 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1385 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1386 } catch (IOException e
) {
1387 logger
.warn("Failed to read local storage manifest.", e
);
1388 return Optional
.empty();
1392 public void setStorageManifest(SignalStorageManifest manifest
) {
1393 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1394 if (manifest
== null) {
1395 if (storageManifestFile
.exists()) {
1397 Files
.delete(storageManifestFile
.toPath());
1398 } catch (IOException e
) {
1399 logger
.error("Failed to delete local storage manifest.", e
);
1405 final var manifestBytes
= manifest
.serialize();
1406 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1407 outputStream
.write(manifestBytes
);
1408 } catch (IOException e
) {
1409 logger
.error("Failed to store local storage manifest.", e
);
1413 public ProfileKey
getProfileKey() {
1417 public void setProfileKey(final ProfileKey profileKey
) {
1418 if (profileKey
.equals(this.profileKey
)) {
1421 this.profileKey
= profileKey
;
1423 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1426 public byte[] getSelfUnidentifiedAccessKey() {
1427 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1430 public int getPreKeyIdOffset(ServiceIdType serviceIdType
) {
1431 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciPreKeyIdOffset
: pniPreKeyIdOffset
;
1434 public int getNextSignedPreKeyId(ServiceIdType serviceIdType
) {
1435 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciNextSignedPreKeyId
: pniNextSignedPreKeyId
;
1438 public boolean isRegistered() {
1442 public void setRegistered(final boolean registered
) {
1443 this.registered
= registered
;
1447 public boolean isMultiDevice() {
1448 return isMultiDevice
;
1451 public void setMultiDevice(final boolean multiDevice
) {
1452 if (isMultiDevice
== multiDevice
) {
1455 isMultiDevice
= multiDevice
;
1459 public long getLastReceiveTimestamp() {
1460 return lastReceiveTimestamp
;
1463 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1464 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1468 public boolean isUnrestrictedUnidentifiedAccess() {
1469 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1470 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1473 public boolean isDiscoverableByPhoneNumber() {
1474 return configurationStore
.getPhoneNumberUnlisted() == null || !configurationStore
.getPhoneNumberUnlisted();
1477 public void finishRegistration(final ACI aci
, final PNI pni
, final MasterKey masterKey
, final String pin
) {
1478 this.pinMasterKey
= masterKey
;
1479 this.storageManifestVersion
= -1;
1480 this.setStorageManifest(null);
1481 this.storageKey
= null;
1482 this.encryptedDeviceName
= null;
1483 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1484 this.isMultiDevice
= false;
1485 this.registered
= true;
1488 this.registrationLockPin
= pin
;
1489 this.lastReceiveTimestamp
= 0;
1493 getSessionStore().archiveAllSessions();
1494 getSenderKeyStore().deleteAll();
1495 final var recipientId
= getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1496 final var publicKey
= getAciIdentityKeyPair().getPublicKey();
1497 getIdentityKeyStore().saveIdentity(recipientId
, publicKey
);
1498 getIdentityKeyStore().setIdentityTrustLevel(recipientId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1501 public void deleteAccountData() throws IOException
{
1503 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1504 .sorted(Comparator
.reverseOrder())) {
1505 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1506 Files
.delete(file
.next());
1509 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1513 public void close() {
1514 synchronized (fileChannel
) {
1515 if (accountDatabase
!= null) {
1517 accountDatabase
.close();
1518 } catch (SQLException e
) {
1519 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1522 if (messageSendLogStore
!= null) {
1523 messageSendLogStore
.close();
1528 } catch (ClosedChannelException ignored
) {
1530 fileChannel
.close();
1531 } catch (IOException e
) {
1532 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1537 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1538 var value
= supplier
.get();
1539 if (value
!= null) {
1543 synchronized (LOCK
) {
1544 value
= supplier
.get();
1545 if (value
!= null) {
1549 return supplier
.get();
1553 private interface Callable
{