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
.GroupStore
;
16 import org
.asamk
.signal
.manager
.storage
.groups
.LegacyGroupStore
;
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
.LegacyPreKeyStore
;
22 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacySignedPreKeyStore
;
23 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
24 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
25 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
26 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
27 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
28 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
29 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
30 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
31 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore2
;
32 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
33 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
34 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
35 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientIdCreator
;
36 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientTrustedResolver
;
39 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
40 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
41 import org
.asamk
.signal
.manager
.storage
.sessions
.LegacySessionStore
;
42 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
43 import org
.asamk
.signal
.manager
.storage
.stickers
.LegacyStickerStore
;
44 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
45 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
46 import org
.asamk
.signal
.manager
.util
.IOUtils
;
47 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
48 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
49 import org
.signal
.libsignal
.protocol
.InvalidMessageException
;
50 import org
.signal
.libsignal
.protocol
.SignalProtocolAddress
;
51 import org
.signal
.libsignal
.protocol
.state
.PreKeyRecord
;
52 import org
.signal
.libsignal
.protocol
.state
.SessionRecord
;
53 import org
.signal
.libsignal
.protocol
.state
.SignedPreKeyRecord
;
54 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
55 import org
.signal
.libsignal
.protocol
.util
.Medium
;
56 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
57 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
58 import org
.slf4j
.Logger
;
59 import org
.slf4j
.LoggerFactory
;
60 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
61 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
62 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
63 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
64 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
65 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
66 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
67 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
68 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
69 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
70 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
71 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
72 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
74 import java
.io
.ByteArrayInputStream
;
75 import java
.io
.ByteArrayOutputStream
;
76 import java
.io
.Closeable
;
78 import java
.io
.FileInputStream
;
79 import java
.io
.FileOutputStream
;
80 import java
.io
.IOException
;
81 import java
.io
.RandomAccessFile
;
82 import java
.nio
.channels
.Channels
;
83 import java
.nio
.channels
.ClosedChannelException
;
84 import java
.nio
.channels
.FileChannel
;
85 import java
.nio
.channels
.FileLock
;
86 import java
.nio
.file
.Files
;
87 import java
.security
.SecureRandom
;
88 import java
.sql
.SQLException
;
89 import java
.util
.Base64
;
90 import java
.util
.Comparator
;
91 import java
.util
.HashSet
;
92 import java
.util
.List
;
93 import java
.util
.Optional
;
94 import java
.util
.function
.Supplier
;
96 public class SignalAccount
implements Closeable
{
98 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
100 private static final int MINIMUM_STORAGE_VERSION
= 1;
101 private static final int CURRENT_STORAGE_VERSION
= 5;
103 private final Object LOCK
= new Object();
105 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
107 private final FileChannel fileChannel
;
108 private final FileLock lock
;
110 private int previousStorageVersion
;
112 private File dataPath
;
113 private String accountPath
;
114 private ServiceEnvironment serviceEnvironment
;
115 private String number
;
118 private String encryptedDeviceName
;
119 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
120 private boolean isMultiDevice
= false;
121 private String password
;
122 private String registrationLockPin
;
123 private MasterKey pinMasterKey
;
124 private StorageKey storageKey
;
125 private long storageManifestVersion
= -1;
126 private ProfileKey profileKey
;
127 private int aciPreKeyIdOffset
= 1;
128 private int aciNextSignedPreKeyId
= 1;
129 private int pniPreKeyIdOffset
= 1;
130 private int pniNextSignedPreKeyId
= 1;
131 private IdentityKeyPair aciIdentityKeyPair
;
132 private IdentityKeyPair pniIdentityKeyPair
;
133 private int localRegistrationId
;
134 private int localPniRegistrationId
;
135 private TrustNewIdentity trustNewIdentity
;
136 private long lastReceiveTimestamp
= 0;
138 private boolean registered
= false;
140 private SignalProtocolStore signalProtocolStore
;
141 private PreKeyStore aciPreKeyStore
;
142 private SignedPreKeyStore aciSignedPreKeyStore
;
143 private PreKeyStore pniPreKeyStore
;
144 private SignedPreKeyStore pniSignedPreKeyStore
;
145 private SessionStore sessionStore
;
146 private IdentityKeyStore identityKeyStore
;
147 private SignalIdentityKeyStore aciIdentityKeyStore
;
148 private SenderKeyStore senderKeyStore
;
149 private GroupStore groupStore
;
150 private RecipientStore recipientStore
;
151 private StickerStore stickerStore
;
152 private ConfigurationStore configurationStore
;
153 private ConfigurationStore
.Storage configurationStoreStorage
;
155 private MessageCache messageCache
;
156 private MessageSendLogStore messageSendLogStore
;
158 private AccountDatabase accountDatabase
;
160 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
161 this.fileChannel
= fileChannel
;
165 public static SignalAccount
load(
166 File dataPath
, String accountPath
, boolean waitForLock
, final TrustNewIdentity trustNewIdentity
167 ) throws IOException
{
168 logger
.trace("Opening account file");
169 final var fileName
= getFileName(dataPath
, accountPath
);
170 final var pair
= openFileChannel(fileName
, waitForLock
);
172 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
173 logger
.trace("Loading account file");
174 signalAccount
.load(dataPath
, accountPath
, trustNewIdentity
);
175 logger
.trace("Migrating legacy parts of account file");
176 signalAccount
.migrateLegacyConfigs();
178 return signalAccount
;
179 } catch (Throwable e
) {
180 pair
.second().close();
181 pair
.first().close();
186 public static SignalAccount
create(
190 ServiceEnvironment serviceEnvironment
,
191 IdentityKeyPair aciIdentityKey
,
192 IdentityKeyPair pniIdentityKey
,
194 int pniRegistrationId
,
195 ProfileKey profileKey
,
196 final TrustNewIdentity trustNewIdentity
197 ) throws IOException
{
198 IOUtils
.createPrivateDirectories(dataPath
);
199 var fileName
= getFileName(dataPath
, accountPath
);
200 if (!fileName
.exists()) {
201 IOUtils
.createPrivateFile(fileName
);
204 final var pair
= openFileChannel(fileName
, true);
205 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
207 signalAccount
.accountPath
= accountPath
;
208 signalAccount
.number
= number
;
209 signalAccount
.serviceEnvironment
= serviceEnvironment
;
210 signalAccount
.profileKey
= profileKey
;
212 signalAccount
.dataPath
= dataPath
;
213 signalAccount
.aciIdentityKeyPair
= aciIdentityKey
;
214 signalAccount
.pniIdentityKeyPair
= pniIdentityKey
;
215 signalAccount
.localRegistrationId
= registrationId
;
216 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
217 signalAccount
.trustNewIdentity
= trustNewIdentity
;
218 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
220 signalAccount
.registered
= false;
222 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
223 signalAccount
.migrateLegacyConfigs();
224 signalAccount
.save();
226 return signalAccount
;
229 public static SignalAccount
createOrUpdateLinkedAccount(
233 ServiceEnvironment serviceEnvironment
,
237 String encryptedDeviceName
,
239 IdentityKeyPair aciIdentityKey
,
240 IdentityKeyPair pniIdentityKey
,
242 int pniRegistrationId
,
243 ProfileKey profileKey
,
244 final TrustNewIdentity trustNewIdentity
245 ) throws IOException
{
246 IOUtils
.createPrivateDirectories(dataPath
);
247 var fileName
= getFileName(dataPath
, accountPath
);
248 if (!fileName
.exists()) {
249 return createLinkedAccount(dataPath
,
266 final var signalAccount
= load(dataPath
, accountPath
, true, trustNewIdentity
);
267 signalAccount
.setProvisioningData(number
,
276 signalAccount
.getRecipientTrustedResolver()
277 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
278 signalAccount
.getSessionStore().archiveAllSessions();
279 signalAccount
.getSenderKeyStore().deleteAll();
280 signalAccount
.clearAllPreKeys();
281 return signalAccount
;
284 public void initDatabase() {
285 getAccountDatabase();
288 private void clearAllPreKeys() {
289 this.aciPreKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
290 this.aciNextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
291 this.pniPreKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
292 this.pniNextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
293 this.getAciPreKeyStore().removeAllPreKeys();
294 this.getAciSignedPreKeyStore().removeAllSignedPreKeys();
295 this.getPniPreKeyStore().removeAllPreKeys();
296 this.getPniSignedPreKeyStore().removeAllSignedPreKeys();
300 private static SignalAccount
createLinkedAccount(
304 ServiceEnvironment serviceEnvironment
,
308 String encryptedDeviceName
,
310 IdentityKeyPair aciIdentityKey
,
311 IdentityKeyPair pniIdentityKey
,
313 int pniRegistrationId
,
314 ProfileKey profileKey
,
315 final TrustNewIdentity trustNewIdentity
316 ) throws IOException
{
317 var fileName
= getFileName(dataPath
, accountPath
);
318 IOUtils
.createPrivateFile(fileName
);
320 final var pair
= openFileChannel(fileName
, true);
321 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
323 signalAccount
.dataPath
= dataPath
;
324 signalAccount
.accountPath
= accountPath
;
325 signalAccount
.serviceEnvironment
= serviceEnvironment
;
326 signalAccount
.localRegistrationId
= registrationId
;
327 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
328 signalAccount
.trustNewIdentity
= trustNewIdentity
;
329 signalAccount
.setProvisioningData(number
,
339 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
341 signalAccount
.getRecipientTrustedResolver()
342 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
343 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
344 signalAccount
.migrateLegacyConfigs();
345 signalAccount
.clearAllPreKeys();
346 signalAccount
.save();
348 return signalAccount
;
351 private void setProvisioningData(
355 final String password
,
356 final String encryptedDeviceName
,
358 final IdentityKeyPair aciIdentity
,
359 final IdentityKeyPair pniIdentity
,
360 final ProfileKey profileKey
362 this.number
= number
;
365 this.password
= password
;
366 this.profileKey
= profileKey
;
367 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
368 this.encryptedDeviceName
= encryptedDeviceName
;
369 this.deviceId
= deviceId
;
370 this.aciIdentityKeyPair
= aciIdentity
;
371 this.pniIdentityKeyPair
= pniIdentity
;
372 this.registered
= true;
373 this.isMultiDevice
= true;
374 this.lastReceiveTimestamp
= 0;
375 this.pinMasterKey
= null;
376 this.storageManifestVersion
= -1;
377 this.setStorageManifest(null);
378 this.storageKey
= null;
381 private void migrateLegacyConfigs() {
382 if (getPassword() == null) {
383 setPassword(KeyUtils
.createPassword());
386 if (getProfileKey() == null) {
387 // Old config file, creating new profile key
388 setProfileKey(KeyUtils
.createProfileKey());
390 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
391 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
395 private void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
396 getSessionStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
397 getIdentityKeyStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
398 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
399 getGroupStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
400 getSenderKeyStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
403 public void removeRecipient(final RecipientId recipientId
) {
404 getSessionStore().deleteAllSessions(recipientId
);
405 getIdentityKeyStore().deleteIdentity(recipientId
);
406 getMessageCache().deleteMessages(recipientId
);
407 getSenderKeyStore().deleteAll(recipientId
);
408 getRecipientStore().deleteRecipientData(recipientId
);
411 public static File
getFileName(File dataPath
, String account
) {
412 return new File(dataPath
, account
);
415 private static File
getUserPath(final File dataPath
, final String account
) {
416 final var path
= new File(dataPath
, account
+ ".d");
418 IOUtils
.createPrivateDirectories(path
);
419 } catch (IOException e
) {
420 throw new AssertionError("Failed to create user path", e
);
425 private static File
getMessageCachePath(File dataPath
, String account
) {
426 return new File(getUserPath(dataPath
, account
), "msg-cache");
429 private static File
getGroupCachePath(File dataPath
, String account
) {
430 return new File(getUserPath(dataPath
, account
), "group-cache");
433 private static File
getAciPreKeysPath(File dataPath
, String account
) {
434 return new File(getUserPath(dataPath
, account
), "pre-keys");
437 private static File
getAciSignedPreKeysPath(File dataPath
, String account
) {
438 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
441 private static File
getPniPreKeysPath(File dataPath
, String account
) {
442 return new File(getUserPath(dataPath
, account
), "pre-keys-pni");
445 private static File
getPniSignedPreKeysPath(File dataPath
, String account
) {
446 return new File(getUserPath(dataPath
, account
), "signed-pre-keys-pni");
449 private static File
getIdentitiesPath(File dataPath
, String account
) {
450 return new File(getUserPath(dataPath
, account
), "identities");
453 private static File
getSessionsPath(File dataPath
, String account
) {
454 return new File(getUserPath(dataPath
, account
), "sessions");
457 private static File
getSenderKeysPath(File dataPath
, String account
) {
458 return new File(getUserPath(dataPath
, account
), "sender-keys");
461 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
462 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
465 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
466 return new File(getUserPath(dataPath
, account
), "recipients-store");
469 private static File
getStorageManifestFile(File dataPath
, String account
) {
470 return new File(getUserPath(dataPath
, account
), "storage-manifest");
473 private static File
getDatabaseFile(File dataPath
, String account
) {
474 return new File(getUserPath(dataPath
, account
), "account.db");
477 public static boolean accountFileExists(File dataPath
, String account
) {
478 if (account
== null) {
481 var f
= getFileName(dataPath
, account
);
482 return !(!f
.exists() || f
.isDirectory());
486 File dataPath
, String accountPath
, final TrustNewIdentity trustNewIdentity
487 ) throws IOException
{
488 this.dataPath
= dataPath
;
489 this.accountPath
= accountPath
;
490 final JsonNode rootNode
;
491 synchronized (fileChannel
) {
492 fileChannel
.position(0);
493 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
496 var migratedLegacyConfig
= false;
498 if (rootNode
.hasNonNull("version")) {
499 var accountVersion
= rootNode
.get("version").asInt(1);
500 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
501 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
502 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
503 throw new IOException("Config file was created by a no longer supported older version: "
506 previousStorageVersion
= accountVersion
;
507 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
508 migratedLegacyConfig
= true;
512 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
513 if (rootNode
.hasNonNull("password")) {
514 password
= rootNode
.get("password").asText();
516 if (rootNode
.hasNonNull("serviceEnvironment")) {
517 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
519 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
520 if (rootNode
.hasNonNull("uuid")) {
522 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
523 } catch (IllegalArgumentException e
) {
524 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
527 if (rootNode
.hasNonNull("pni")) {
529 pni
= PNI
.parseOrThrow(rootNode
.get("pni").asText());
530 } catch (IllegalArgumentException e
) {
531 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
534 if (rootNode
.hasNonNull("deviceName")) {
535 encryptedDeviceName
= rootNode
.get("deviceName").asText();
537 if (rootNode
.hasNonNull("deviceId")) {
538 deviceId
= rootNode
.get("deviceId").asInt();
540 if (rootNode
.hasNonNull("isMultiDevice")) {
541 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
543 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
544 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
546 int registrationId
= 0;
547 if (rootNode
.hasNonNull("registrationId")) {
548 registrationId
= rootNode
.get("registrationId").asInt();
550 if (rootNode
.hasNonNull("pniRegistrationId")) {
551 localPniRegistrationId
= rootNode
.get("pniRegistrationId").asInt();
553 localPniRegistrationId
= KeyHelper
.generateRegistrationId(false);
555 IdentityKeyPair aciIdentityKeyPair
= null;
556 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
557 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
558 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
559 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
561 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
562 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
563 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
564 pniIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
567 if (rootNode
.hasNonNull("registrationLockPin")) {
568 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
570 if (rootNode
.hasNonNull("pinMasterKey")) {
571 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
573 if (rootNode
.hasNonNull("storageKey")) {
574 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
576 if (rootNode
.hasNonNull("storageManifestVersion")) {
577 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
579 if (rootNode
.hasNonNull("preKeyIdOffset")) {
580 aciPreKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
582 aciPreKeyIdOffset
= 1;
584 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
585 aciNextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
587 aciNextSignedPreKeyId
= 1;
589 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
590 pniPreKeyIdOffset
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
592 pniPreKeyIdOffset
= 1;
594 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
595 pniNextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
597 pniNextSignedPreKeyId
= 1;
599 if (rootNode
.hasNonNull("profileKey")) {
601 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
602 } catch (InvalidInputException e
) {
603 throw new IOException(
604 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
609 if (previousStorageVersion
< 5) {
610 final var legacyRecipientsStoreFile
= getRecipientsStoreFile(dataPath
, accountPath
);
611 if (legacyRecipientsStoreFile
.exists()) {
612 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
613 // Ensure our profile key is stored in profile store
614 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
615 migratedLegacyConfig
= true;
618 final var legacyAciPreKeysPath
= getAciPreKeysPath(dataPath
, accountPath
);
619 if (legacyAciPreKeysPath
.exists()) {
620 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, getAciPreKeyStore());
621 migratedLegacyConfig
= true;
623 final var legacyPniPreKeysPath
= getPniPreKeysPath(dataPath
, accountPath
);
624 if (legacyPniPreKeysPath
.exists()) {
625 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, getPniPreKeyStore());
626 migratedLegacyConfig
= true;
628 final var legacyAciSignedPreKeysPath
= getAciSignedPreKeysPath(dataPath
, accountPath
);
629 if (legacyAciSignedPreKeysPath
.exists()) {
630 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, getAciSignedPreKeyStore());
631 migratedLegacyConfig
= true;
633 final var legacyPniSignedPreKeysPath
= getPniSignedPreKeysPath(dataPath
, accountPath
);
634 if (legacyPniSignedPreKeysPath
.exists()) {
635 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, getPniSignedPreKeyStore());
636 migratedLegacyConfig
= true;
638 final var legacySessionsPath
= getSessionsPath(dataPath
, accountPath
);
639 if (legacySessionsPath
.exists()) {
640 LegacySessionStore
.migrate(legacySessionsPath
, getRecipientResolver(), getSessionStore());
641 migratedLegacyConfig
= true;
643 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
644 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
645 LegacyJsonSignalProtocolStore
.class)
647 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
648 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
649 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
650 migratedLegacyConfig
= true;
653 this.aciIdentityKeyPair
= aciIdentityKeyPair
;
654 this.localRegistrationId
= registrationId
;
655 this.trustNewIdentity
= trustNewIdentity
;
657 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
659 if (rootNode
.hasNonNull("groupStore")) {
660 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
661 LegacyGroupStore
.Storage
.class);
662 LegacyGroupStore
.migrate(groupStoreStorage
,
663 getGroupCachePath(dataPath
, accountPath
),
664 getRecipientResolver(),
666 migratedLegacyConfig
= true;
669 if (rootNode
.hasNonNull("stickerStore")) {
670 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
671 LegacyStickerStore
.Storage
.class);
672 LegacyStickerStore
.migrate(storage
, getStickerStore());
673 migratedLegacyConfig
= true;
676 if (rootNode
.hasNonNull("configurationStore")) {
677 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
678 ConfigurationStore
.Storage
.class);
679 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
680 this::saveConfigurationStore
);
682 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
685 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
687 if (migratedLegacyConfig
) {
692 private boolean loadLegacyStores(
693 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
695 var migrated
= false;
696 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
697 if (legacyRecipientStoreNode
!= null) {
698 logger
.debug("Migrating legacy recipient store.");
699 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
700 if (legacyRecipientStore
!= null) {
701 legacyRecipientStore
.getAddresses()
702 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
704 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
708 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
709 logger
.debug("Migrating legacy pre key store.");
710 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
712 getAciPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
713 } catch (InvalidMessageException e
) {
714 logger
.warn("Failed to migrate pre key, ignoring", e
);
720 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
721 logger
.debug("Migrating legacy signed pre key store.");
722 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
724 getAciSignedPreKeyStore().storeSignedPreKey(entry
.getKey(),
725 new SignedPreKeyRecord(entry
.getValue()));
726 } catch (InvalidMessageException e
) {
727 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
733 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
734 logger
.debug("Migrating legacy session store.");
735 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
737 getSessionStore().storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
738 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
739 } catch (Exception e
) {
740 logger
.warn("Failed to migrate session, ignoring", e
);
746 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
747 logger
.debug("Migrating legacy identity session store.");
748 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
749 RecipientId recipientId
= getRecipientStore().resolveRecipientTrusted(identity
.getAddress());
750 getIdentityKeyStore().saveIdentity(recipientId
, identity
.getIdentityKey(), identity
.getDateAdded());
751 getIdentityKeyStore().setIdentityTrustLevel(recipientId
,
752 identity
.getIdentityKey(),
753 identity
.getTrustLevel());
758 if (rootNode
.hasNonNull("contactStore")) {
759 logger
.debug("Migrating legacy contact store.");
760 final var contactStoreNode
= rootNode
.get("contactStore");
761 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
762 for (var contact
: contactStore
.getContacts()) {
763 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
764 getContactStore().storeContact(recipientId
,
765 new Contact(contact
.name
,
768 contact
.messageExpirationTime
,
773 // Store profile keys only in profile store
774 var profileKeyString
= contact
.profileKey
;
775 if (profileKeyString
!= null) {
776 final ProfileKey profileKey
;
778 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
779 getProfileStore().storeProfileKey(recipientId
, profileKey
);
780 } catch (InvalidInputException e
) {
781 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
788 if (rootNode
.hasNonNull("profileStore")) {
789 logger
.debug("Migrating legacy profile store.");
790 var profileStoreNode
= rootNode
.get("profileStore");
791 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
792 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
793 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.getAddress());
794 // Not migrating profile key credential here, it was changed to expiring profile key credentials
795 getProfileStore().storeProfileKey(recipientId
, profileEntry
.getProfileKey());
796 final var profile
= profileEntry
.getProfile();
797 if (profile
!= null) {
798 final var capabilities
= new HashSet
<Profile
.Capability
>();
799 if (profile
.getCapabilities() != null) {
800 if (profile
.getCapabilities().gv1Migration
) {
801 capabilities
.add(Profile
.Capability
.gv1Migration
);
803 if (profile
.getCapabilities().storage
) {
804 capabilities
.add(Profile
.Capability
.storage
);
807 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
808 profile
.getGivenName(),
809 profile
.getFamilyName(),
811 profile
.getAboutEmoji(),
814 profile
.isUnrestrictedUnidentifiedAccess()
815 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
816 : profile
.getUnidentifiedAccess() != null
817 ? Profile
.UnidentifiedAccessMode
.ENABLED
818 : Profile
.UnidentifiedAccessMode
.DISABLED
,
820 getProfileStore().storeProfile(recipientId
, newProfile
);
828 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
829 var threadStoreNode
= rootNode
.get("threadStore");
830 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
831 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
832 // Migrate thread info to group and contact store
833 for (var thread
: threadStore
.getThreads()) {
834 if (thread
.id
== null || thread
.id
.isEmpty()) {
838 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
839 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
840 var contact
= getContactStore().getContact(recipientId
);
841 if (contact
!= null) {
842 getContactStore().storeContact(recipientId
,
843 Contact
.newBuilder(contact
)
844 .withMessageExpirationTime(thread
.messageExpirationTime
)
848 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
849 if (groupInfo
instanceof GroupInfoV1
) {
850 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
851 getGroupStore().updateGroup(groupInfo
);
854 } catch (Exception e
) {
855 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
864 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
865 this.configurationStoreStorage
= storage
;
869 private void save() {
870 synchronized (fileChannel
) {
871 var rootNode
= jsonProcessor
.createObjectNode();
872 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
873 .put("username", number
)
874 .put("serviceEnvironment", serviceEnvironment
== null ?
null : serviceEnvironment
.name())
875 .put("uuid", aci
== null ?
null : aci
.toString())
876 .put("pni", pni
== null ?
null : pni
.toString())
877 .put("deviceName", encryptedDeviceName
)
878 .put("deviceId", deviceId
)
879 .put("isMultiDevice", isMultiDevice
)
880 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
881 .put("password", password
)
882 .put("registrationId", localRegistrationId
)
883 .put("pniRegistrationId", localPniRegistrationId
)
884 .put("identityPrivateKey",
885 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPrivateKey().serialize()))
887 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPublicKey().serialize()))
888 .put("pniIdentityPrivateKey",
889 pniIdentityKeyPair
== null
891 : Base64
.getEncoder()
892 .encodeToString(pniIdentityKeyPair
.getPrivateKey().serialize()))
893 .put("pniIdentityKey",
894 pniIdentityKeyPair
== null
896 : Base64
.getEncoder().encodeToString(pniIdentityKeyPair
.getPublicKey().serialize()))
897 .put("registrationLockPin", registrationLockPin
)
899 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
901 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
902 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
903 .put("preKeyIdOffset", aciPreKeyIdOffset
)
904 .put("nextSignedPreKeyId", aciNextSignedPreKeyId
)
905 .put("pniPreKeyIdOffset", pniPreKeyIdOffset
)
906 .put("pniNextSignedPreKeyId", pniNextSignedPreKeyId
)
908 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
909 .put("registered", registered
)
910 .putPOJO("configurationStore", configurationStoreStorage
);
912 try (var output
= new ByteArrayOutputStream()) {
913 // Write to memory first to prevent corrupting the file in case of serialization errors
914 jsonProcessor
.writeValue(output
, rootNode
);
915 var input
= new ByteArrayInputStream(output
.toByteArray());
916 fileChannel
.position(0);
917 input
.transferTo(Channels
.newOutputStream(fileChannel
));
918 fileChannel
.truncate(fileChannel
.position());
919 fileChannel
.force(false);
921 } catch (Exception e
) {
922 logger
.error("Error saving file: {}", e
.getMessage(), e
);
927 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
928 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
930 var lock
= fileChannel
.tryLock();
933 logger
.debug("Config file is in use by another instance.");
934 throw new IOException("Config file is in use by another instance.");
936 logger
.info("Config file is in use by another instance, waiting…");
937 lock
= fileChannel
.lock();
938 logger
.info("Config file lock acquired.");
940 final var result
= new Pair
<>(fileChannel
, lock
);
944 if (fileChannel
!= null) {
950 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
951 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
952 addAciPreKeys(records
);
954 addPniPreKeys(records
);
958 public void addAciPreKeys(List
<PreKeyRecord
> records
) {
959 for (var record : records
) {
960 if (aciPreKeyIdOffset
!= record.getId()) {
961 logger
.error("Invalid pre key id {}, expected {}", record.getId(), aciPreKeyIdOffset
);
962 throw new AssertionError("Invalid pre key id");
964 getAciPreKeyStore().storePreKey(record.getId(), record);
965 aciPreKeyIdOffset
= (aciPreKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
970 public void addPniPreKeys(List
<PreKeyRecord
> records
) {
971 for (var record : records
) {
972 if (pniPreKeyIdOffset
!= record.getId()) {
973 logger
.error("Invalid pre key id {}, expected {}", record.getId(), pniPreKeyIdOffset
);
974 throw new AssertionError("Invalid pre key id");
976 getPniPreKeyStore().storePreKey(record.getId(), record);
977 pniPreKeyIdOffset
= (pniPreKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
982 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
983 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
984 addAciSignedPreKey(record);
986 addPniSignedPreKey(record);
990 public void addAciSignedPreKey(SignedPreKeyRecord
record) {
991 if (aciNextSignedPreKeyId
!= record.getId()) {
992 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), aciNextSignedPreKeyId
);
993 throw new AssertionError("Invalid signed pre key id");
995 getAciSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
996 aciNextSignedPreKeyId
= (aciNextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
1000 public void addPniSignedPreKey(SignedPreKeyRecord
record) {
1001 if (pniNextSignedPreKeyId
!= record.getId()) {
1002 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), pniNextSignedPreKeyId
);
1003 throw new AssertionError("Invalid signed pre key id");
1005 getPniSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1006 pniNextSignedPreKeyId
= (pniNextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
1010 public int getPreviousStorageVersion() {
1011 return previousStorageVersion
;
1014 public SignalServiceDataStore
getSignalServiceDataStore() {
1015 return new SignalServiceDataStore() {
1017 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1018 if (accountIdentifier
.equals(aci
)) {
1019 return getAciSignalServiceAccountDataStore();
1020 } else if (accountIdentifier
.equals(pni
)) {
1021 throw new AssertionError("PNI not to be used yet!");
1023 throw new IllegalArgumentException("No matching store found for " + accountIdentifier
);
1028 public SignalServiceAccountDataStore
aci() {
1029 return getAciSignalServiceAccountDataStore();
1033 public SignalServiceAccountDataStore
pni() {
1034 throw new AssertionError("PNI not to be used yet!");
1038 public boolean isMultiDevice() {
1039 return SignalAccount
.this.isMultiDevice();
1044 private SignalServiceAccountDataStore
getAciSignalServiceAccountDataStore() {
1045 return getOrCreate(() -> signalProtocolStore
,
1046 () -> signalProtocolStore
= new SignalProtocolStore(getAciPreKeyStore(),
1047 getAciSignedPreKeyStore(),
1049 getAciIdentityKeyStore(),
1050 getSenderKeyStore(),
1051 this::isMultiDevice
));
1054 private PreKeyStore
getAciPreKeyStore() {
1055 return getOrCreate(() -> aciPreKeyStore
,
1056 () -> aciPreKeyStore
= new PreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1059 private SignedPreKeyStore
getAciSignedPreKeyStore() {
1060 return getOrCreate(() -> aciSignedPreKeyStore
,
1061 () -> aciSignedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1064 private PreKeyStore
getPniPreKeyStore() {
1065 return getOrCreate(() -> pniPreKeyStore
,
1066 () -> pniPreKeyStore
= new PreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1069 private SignedPreKeyStore
getPniSignedPreKeyStore() {
1070 return getOrCreate(() -> pniSignedPreKeyStore
,
1071 () -> pniSignedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1074 public SessionStore
getSessionStore() {
1075 return getOrCreate(() -> sessionStore
,
1076 () -> sessionStore
= new SessionStore(getAccountDatabase(),
1078 getRecipientResolver(),
1079 getRecipientIdCreator()));
1082 public IdentityKeyStore
getIdentityKeyStore() {
1083 return getOrCreate(() -> identityKeyStore
,
1084 () -> identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, accountPath
),
1085 getRecipientResolver(),
1089 public SignalIdentityKeyStore
getAciIdentityKeyStore() {
1090 return getOrCreate(() -> aciIdentityKeyStore
,
1091 () -> aciIdentityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1092 () -> aciIdentityKeyPair
,
1093 localRegistrationId
,
1094 getIdentityKeyStore()));
1097 public GroupStore
getGroupStore() {
1098 return getOrCreate(() -> groupStore
,
1099 () -> groupStore
= new GroupStore(getAccountDatabase(),
1100 getRecipientResolver(),
1101 getRecipientIdCreator()));
1104 public ContactsStore
getContactStore() {
1105 return getRecipientStore();
1108 private RecipientIdCreator
getRecipientIdCreator() {
1109 return recipientId
-> getRecipientStore().create(recipientId
);
1112 public RecipientResolver
getRecipientResolver() {
1113 return new RecipientResolver() {
1115 public RecipientId
resolveRecipient(final RecipientAddress address
) {
1116 return getRecipientStore().resolveRecipient(address
);
1120 public RecipientId
resolveRecipient(final long recipientId
) {
1121 return getRecipientStore().resolveRecipient(recipientId
);
1126 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1127 return new RecipientTrustedResolver() {
1129 public RecipientId
resolveSelfRecipientTrusted(final RecipientAddress address
) {
1130 return getRecipientStore().resolveSelfRecipientTrusted(address
);
1134 public RecipientId
resolveRecipientTrusted(final SignalServiceAddress address
) {
1135 return getRecipientStore().resolveRecipientTrusted(address
);
1140 public RecipientAddressResolver
getRecipientAddressResolver() {
1141 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1144 public RecipientStore
getRecipientStore() {
1145 return getOrCreate(() -> recipientStore
,
1146 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1147 this::getSelfRecipientAddress
,
1148 getAccountDatabase()));
1151 public ProfileStore
getProfileStore() {
1152 return getRecipientStore();
1155 public StickerStore
getStickerStore() {
1156 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1159 public SenderKeyStore
getSenderKeyStore() {
1160 return getOrCreate(() -> senderKeyStore
,
1161 () -> senderKeyStore
= new SenderKeyStore(getSharedSenderKeysFile(dataPath
, accountPath
),
1162 getSenderKeysPath(dataPath
, accountPath
),
1163 getRecipientAddressResolver(),
1164 getRecipientResolver()));
1167 public ConfigurationStore
getConfigurationStore() {
1168 return configurationStore
;
1171 public MessageCache
getMessageCache() {
1172 return getOrCreate(() -> messageCache
,
1173 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1176 public AccountDatabase
getAccountDatabase() {
1177 return getOrCreate(() -> accountDatabase
, () -> {
1179 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1180 } catch (SQLException e
) {
1181 throw new RuntimeException(e
);
1186 public MessageSendLogStore
getMessageSendLogStore() {
1187 return getOrCreate(() -> messageSendLogStore
,
1188 () -> messageSendLogStore
= new MessageSendLogStore(getRecipientResolver(), getAccountDatabase()));
1191 public CredentialsProvider
getCredentialsProvider() {
1192 return new CredentialsProvider() {
1194 public ACI
getAci() {
1199 public PNI
getPni() {
1204 public String
getE164() {
1209 public String
getPassword() {
1214 public int getDeviceId() {
1220 public String
getNumber() {
1224 public void setNumber(final String number
) {
1225 this.number
= number
;
1229 public ServiceEnvironment
getServiceEnvironment() {
1230 return serviceEnvironment
;
1233 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1234 this.serviceEnvironment
= serviceEnvironment
;
1238 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1239 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aci
: pni
;
1242 public ACI
getAci() {
1246 public void setAci(final ACI aci
) {
1251 public PNI
getPni() {
1255 public void setPni(final PNI pni
) {
1260 public SignalServiceAddress
getSelfAddress() {
1261 return new SignalServiceAddress(aci
, number
);
1264 public RecipientAddress
getSelfRecipientAddress() {
1265 return new RecipientAddress(aci
== null ?
null : aci
.uuid(), number
);
1268 public RecipientId
getSelfRecipientId() {
1269 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1272 public byte[] getEncryptedDeviceName() {
1273 return encryptedDeviceName
== null ?
null : Base64
.getDecoder().decode(encryptedDeviceName
);
1276 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1277 this.encryptedDeviceName
= encryptedDeviceName
;
1281 public int getDeviceId() {
1285 public boolean isPrimaryDevice() {
1286 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1289 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1290 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciIdentityKeyPair
: pniIdentityKeyPair
;
1293 public IdentityKeyPair
getAciIdentityKeyPair() {
1294 return aciIdentityKeyPair
;
1297 public IdentityKeyPair
getPniIdentityKeyPair() {
1298 return pniIdentityKeyPair
;
1301 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1302 pniIdentityKeyPair
= identityKeyPair
;
1306 public int getLocalRegistrationId() {
1307 return localRegistrationId
;
1310 public int getLocalPniRegistrationId() {
1311 return localPniRegistrationId
;
1314 public String
getPassword() {
1318 private void setPassword(final String password
) {
1319 this.password
= password
;
1323 public void setRegistrationLockPin(final String registrationLockPin
) {
1324 this.registrationLockPin
= registrationLockPin
;
1328 public String
getRegistrationLockPin() {
1329 return registrationLockPin
;
1332 public String
getRegistrationLock() {
1333 final var masterKey
= getPinBackedMasterKey();
1334 if (masterKey
== null) {
1337 return masterKey
.deriveRegistrationLock();
1340 public MasterKey
getPinBackedMasterKey() {
1341 if (registrationLockPin
== null) {
1344 return pinMasterKey
;
1347 public MasterKey
getOrCreatePinMasterKey() {
1348 if (pinMasterKey
== null) {
1349 pinMasterKey
= KeyUtils
.createMasterKey();
1352 return pinMasterKey
;
1355 public StorageKey
getStorageKey() {
1356 if (pinMasterKey
!= null) {
1357 return pinMasterKey
.deriveStorageServiceKey();
1362 public StorageKey
getOrCreateStorageKey() {
1363 if (isPrimaryDevice()) {
1364 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1369 public void setStorageKey(final StorageKey storageKey
) {
1370 if (storageKey
.equals(this.storageKey
)) {
1373 this.storageKey
= storageKey
;
1377 public long getStorageManifestVersion() {
1378 return this.storageManifestVersion
;
1381 public void setStorageManifestVersion(final long storageManifestVersion
) {
1382 if (storageManifestVersion
== this.storageManifestVersion
) {
1385 this.storageManifestVersion
= storageManifestVersion
;
1389 public Optional
<SignalStorageManifest
> getStorageManifest() {
1390 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1391 if (!storageManifestFile
.exists()) {
1392 return Optional
.empty();
1394 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1395 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1396 } catch (IOException e
) {
1397 logger
.warn("Failed to read local storage manifest.", e
);
1398 return Optional
.empty();
1402 public void setStorageManifest(SignalStorageManifest manifest
) {
1403 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1404 if (manifest
== null) {
1405 if (storageManifestFile
.exists()) {
1407 Files
.delete(storageManifestFile
.toPath());
1408 } catch (IOException e
) {
1409 logger
.error("Failed to delete local storage manifest.", e
);
1415 final var manifestBytes
= manifest
.serialize();
1416 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1417 outputStream
.write(manifestBytes
);
1418 } catch (IOException e
) {
1419 logger
.error("Failed to store local storage manifest.", e
);
1423 public ProfileKey
getProfileKey() {
1427 public void setProfileKey(final ProfileKey profileKey
) {
1428 if (profileKey
.equals(this.profileKey
)) {
1431 this.profileKey
= profileKey
;
1433 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1436 public byte[] getSelfUnidentifiedAccessKey() {
1437 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1440 public int getPreKeyIdOffset(ServiceIdType serviceIdType
) {
1441 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciPreKeyIdOffset
: pniPreKeyIdOffset
;
1444 public int getNextSignedPreKeyId(ServiceIdType serviceIdType
) {
1445 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciNextSignedPreKeyId
: pniNextSignedPreKeyId
;
1448 public boolean isRegistered() {
1452 public void setRegistered(final boolean registered
) {
1453 this.registered
= registered
;
1457 public boolean isMultiDevice() {
1458 return isMultiDevice
;
1461 public void setMultiDevice(final boolean multiDevice
) {
1462 if (isMultiDevice
== multiDevice
) {
1465 isMultiDevice
= multiDevice
;
1469 public long getLastReceiveTimestamp() {
1470 return lastReceiveTimestamp
;
1473 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1474 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1478 public boolean isUnrestrictedUnidentifiedAccess() {
1479 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1480 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1483 public boolean isDiscoverableByPhoneNumber() {
1484 return configurationStore
.getPhoneNumberUnlisted() == null || !configurationStore
.getPhoneNumberUnlisted();
1487 public void finishRegistration(final ACI aci
, final PNI pni
, final MasterKey masterKey
, final String pin
) {
1488 this.pinMasterKey
= masterKey
;
1489 this.storageManifestVersion
= -1;
1490 this.setStorageManifest(null);
1491 this.storageKey
= null;
1492 this.encryptedDeviceName
= null;
1493 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1494 this.isMultiDevice
= false;
1495 this.registered
= true;
1498 this.registrationLockPin
= pin
;
1499 this.lastReceiveTimestamp
= 0;
1503 getSessionStore().archiveAllSessions();
1504 getSenderKeyStore().deleteAll();
1505 final var recipientId
= getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1506 final var publicKey
= getAciIdentityKeyPair().getPublicKey();
1507 getIdentityKeyStore().saveIdentity(recipientId
, publicKey
);
1508 getIdentityKeyStore().setIdentityTrustLevel(recipientId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1511 public void deleteAccountData() throws IOException
{
1513 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1514 .sorted(Comparator
.reverseOrder())) {
1515 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1516 Files
.delete(file
.next());
1519 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1523 public void close() {
1524 synchronized (fileChannel
) {
1525 if (accountDatabase
!= null) {
1527 accountDatabase
.close();
1528 } catch (SQLException e
) {
1529 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1532 if (messageSendLogStore
!= null) {
1533 messageSendLogStore
.close();
1538 } catch (ClosedChannelException ignored
) {
1540 fileChannel
.close();
1541 } catch (IOException e
) {
1542 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1547 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1548 var value
= supplier
.get();
1549 if (value
!= null) {
1553 synchronized (LOCK
) {
1554 value
= supplier
.get();
1555 if (value
!= null) {
1559 return supplier
.get();
1563 private interface Callable
{