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
.Settings
;
7 import org
.asamk
.signal
.manager
.api
.Pair
;
8 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
9 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
10 import org
.asamk
.signal
.manager
.groups
.GroupId
;
11 import org
.asamk
.signal
.manager
.helper
.RecipientAddressResolver
;
12 import org
.asamk
.signal
.manager
.storage
.configuration
.ConfigurationStore
;
13 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
14 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
15 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
16 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
17 import org
.asamk
.signal
.manager
.storage
.groups
.LegacyGroupStore
;
18 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
19 import org
.asamk
.signal
.manager
.storage
.identities
.LegacyIdentityKeyStore
;
20 import org
.asamk
.signal
.manager
.storage
.identities
.SignalIdentityKeyStore
;
21 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
22 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacyPreKeyStore
;
23 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacySignedPreKeyStore
;
24 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
25 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
26 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
27 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
28 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
29 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
30 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
31 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
32 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore2
;
33 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
34 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
35 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
36 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientIdCreator
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
39 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientTrustedResolver
;
40 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
41 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeyRecordStore
;
42 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeySharedStore
;
43 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
44 import org
.asamk
.signal
.manager
.storage
.sessions
.LegacySessionStore
;
45 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
46 import org
.asamk
.signal
.manager
.storage
.stickers
.LegacyStickerStore
;
47 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
48 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
49 import org
.asamk
.signal
.manager
.util
.IOUtils
;
50 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
51 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
52 import org
.signal
.libsignal
.protocol
.InvalidMessageException
;
53 import org
.signal
.libsignal
.protocol
.SignalProtocolAddress
;
54 import org
.signal
.libsignal
.protocol
.state
.PreKeyRecord
;
55 import org
.signal
.libsignal
.protocol
.state
.SessionRecord
;
56 import org
.signal
.libsignal
.protocol
.state
.SignedPreKeyRecord
;
57 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
58 import org
.signal
.libsignal
.protocol
.util
.Medium
;
59 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
60 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
61 import org
.slf4j
.Logger
;
62 import org
.slf4j
.LoggerFactory
;
63 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
64 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
65 import org
.whispersystems
.signalservice
.api
.account
.AccountAttributes
;
66 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
67 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
68 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
69 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
70 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
71 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
72 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
73 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
74 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
75 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
76 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
78 import java
.io
.ByteArrayInputStream
;
79 import java
.io
.ByteArrayOutputStream
;
80 import java
.io
.Closeable
;
82 import java
.io
.FileInputStream
;
83 import java
.io
.FileOutputStream
;
84 import java
.io
.IOException
;
85 import java
.io
.RandomAccessFile
;
86 import java
.nio
.channels
.Channels
;
87 import java
.nio
.channels
.ClosedChannelException
;
88 import java
.nio
.channels
.FileChannel
;
89 import java
.nio
.channels
.FileLock
;
90 import java
.nio
.file
.Files
;
91 import java
.security
.SecureRandom
;
92 import java
.sql
.Connection
;
93 import java
.sql
.SQLException
;
94 import java
.util
.Base64
;
95 import java
.util
.Comparator
;
96 import java
.util
.HashSet
;
97 import java
.util
.List
;
98 import java
.util
.Optional
;
99 import java
.util
.function
.Supplier
;
101 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
103 public class SignalAccount
implements Closeable
{
105 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
107 private static final int MINIMUM_STORAGE_VERSION
= 1;
108 private static final int CURRENT_STORAGE_VERSION
= 6;
110 private final Object LOCK
= new Object();
112 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
114 private final FileChannel fileChannel
;
115 private final FileLock lock
;
117 private int previousStorageVersion
;
119 private File dataPath
;
120 private String accountPath
;
121 private ServiceEnvironment serviceEnvironment
;
122 private String number
;
123 private String username
;
126 private String sessionId
;
127 private String sessionNumber
;
128 private String encryptedDeviceName
;
129 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
130 private boolean isMultiDevice
= false;
131 private String password
;
132 private String registrationLockPin
;
133 private MasterKey pinMasterKey
;
134 private StorageKey storageKey
;
135 private long storageManifestVersion
= -1;
136 private ProfileKey profileKey
;
137 private int aciPreKeyIdOffset
= 1;
138 private int aciNextSignedPreKeyId
= 1;
139 private int pniPreKeyIdOffset
= 1;
140 private int pniNextSignedPreKeyId
= 1;
141 private IdentityKeyPair aciIdentityKeyPair
;
142 private IdentityKeyPair pniIdentityKeyPair
;
143 private int localRegistrationId
;
144 private int localPniRegistrationId
;
145 private Settings settings
;
146 private long lastReceiveTimestamp
= 0;
148 private boolean registered
= false;
150 private SignalProtocolStore aciSignalProtocolStore
;
151 private SignalProtocolStore pniSignalProtocolStore
;
152 private PreKeyStore aciPreKeyStore
;
153 private SignedPreKeyStore aciSignedPreKeyStore
;
154 private PreKeyStore pniPreKeyStore
;
155 private SignedPreKeyStore pniSignedPreKeyStore
;
156 private SessionStore aciSessionStore
;
157 private SessionStore pniSessionStore
;
158 private IdentityKeyStore identityKeyStore
;
159 private SignalIdentityKeyStore aciIdentityKeyStore
;
160 private SignalIdentityKeyStore pniIdentityKeyStore
;
161 private SenderKeyStore senderKeyStore
;
162 private GroupStore groupStore
;
163 private RecipientStore recipientStore
;
164 private StickerStore stickerStore
;
165 private ConfigurationStore configurationStore
;
166 private ConfigurationStore
.Storage configurationStoreStorage
;
168 private MessageCache messageCache
;
169 private MessageSendLogStore messageSendLogStore
;
171 private AccountDatabase accountDatabase
;
173 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
174 this.fileChannel
= fileChannel
;
178 public static SignalAccount
load(
179 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
180 ) throws IOException
{
181 logger
.trace("Opening account file");
182 final var fileName
= getFileName(dataPath
, accountPath
);
183 final var pair
= openFileChannel(fileName
, waitForLock
);
185 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
186 logger
.trace("Loading account file");
187 signalAccount
.load(dataPath
, accountPath
, settings
);
188 logger
.trace("Migrating legacy parts of account file");
189 signalAccount
.migrateLegacyConfigs();
191 return signalAccount
;
192 } catch (Throwable e
) {
193 pair
.second().close();
194 pair
.first().close();
199 public static SignalAccount
create(
203 ServiceEnvironment serviceEnvironment
,
204 IdentityKeyPair aciIdentityKey
,
205 IdentityKeyPair pniIdentityKey
,
207 int pniRegistrationId
,
208 ProfileKey profileKey
,
209 final Settings settings
210 ) throws IOException
{
211 IOUtils
.createPrivateDirectories(dataPath
);
212 var fileName
= getFileName(dataPath
, accountPath
);
213 if (!fileName
.exists()) {
214 IOUtils
.createPrivateFile(fileName
);
217 final var pair
= openFileChannel(fileName
, true);
218 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
220 signalAccount
.accountPath
= accountPath
;
221 signalAccount
.number
= number
;
222 signalAccount
.serviceEnvironment
= serviceEnvironment
;
223 signalAccount
.profileKey
= profileKey
;
225 signalAccount
.dataPath
= dataPath
;
226 signalAccount
.aciIdentityKeyPair
= aciIdentityKey
;
227 signalAccount
.pniIdentityKeyPair
= pniIdentityKey
;
228 signalAccount
.localRegistrationId
= registrationId
;
229 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
230 signalAccount
.settings
= settings
;
231 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
233 signalAccount
.registered
= false;
235 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
236 signalAccount
.migrateLegacyConfigs();
237 signalAccount
.save();
239 return signalAccount
;
242 public static SignalAccount
createOrUpdateLinkedAccount(
246 ServiceEnvironment serviceEnvironment
,
250 String encryptedDeviceName
,
252 IdentityKeyPair aciIdentityKey
,
253 IdentityKeyPair pniIdentityKey
,
255 int pniRegistrationId
,
256 ProfileKey profileKey
,
257 final Settings settings
258 ) throws IOException
{
259 IOUtils
.createPrivateDirectories(dataPath
);
260 var fileName
= getFileName(dataPath
, accountPath
);
261 if (!fileName
.exists()) {
262 return createLinkedAccount(dataPath
,
279 final var signalAccount
= load(dataPath
, accountPath
, true, settings
);
280 signalAccount
.setProvisioningData(number
,
289 signalAccount
.getRecipientTrustedResolver()
290 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
291 signalAccount
.getAciSessionStore().archiveAllSessions();
292 signalAccount
.getPniSessionStore().archiveAllSessions();
293 signalAccount
.getSenderKeyStore().deleteAll();
294 signalAccount
.clearAllPreKeys();
295 return signalAccount
;
298 public void initDatabase() {
299 getAccountDatabase();
302 private void clearAllPreKeys() {
303 resetPreKeyOffsets(ServiceIdType
.ACI
);
304 resetPreKeyOffsets(ServiceIdType
.PNI
);
305 this.getAciPreKeyStore().removeAllPreKeys();
306 this.getAciSignedPreKeyStore().removeAllSignedPreKeys();
307 this.getPniPreKeyStore().removeAllPreKeys();
308 this.getPniSignedPreKeyStore().removeAllSignedPreKeys();
312 private static SignalAccount
createLinkedAccount(
316 ServiceEnvironment serviceEnvironment
,
320 String encryptedDeviceName
,
322 IdentityKeyPair aciIdentityKey
,
323 IdentityKeyPair pniIdentityKey
,
325 int pniRegistrationId
,
326 ProfileKey profileKey
,
327 final Settings settings
328 ) throws IOException
{
329 var fileName
= getFileName(dataPath
, accountPath
);
330 IOUtils
.createPrivateFile(fileName
);
332 final var pair
= openFileChannel(fileName
, true);
333 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
335 signalAccount
.dataPath
= dataPath
;
336 signalAccount
.accountPath
= accountPath
;
337 signalAccount
.serviceEnvironment
= serviceEnvironment
;
338 signalAccount
.localRegistrationId
= registrationId
;
339 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
340 signalAccount
.settings
= settings
;
341 signalAccount
.setProvisioningData(number
,
351 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
353 signalAccount
.getRecipientTrustedResolver()
354 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
355 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
356 signalAccount
.migrateLegacyConfigs();
357 signalAccount
.clearAllPreKeys();
358 signalAccount
.save();
360 return signalAccount
;
363 private void setProvisioningData(
367 final String password
,
368 final String encryptedDeviceName
,
370 final IdentityKeyPair aciIdentity
,
371 final IdentityKeyPair pniIdentity
,
372 final ProfileKey profileKey
374 this.number
= number
;
377 this.password
= password
;
378 this.profileKey
= profileKey
;
379 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
380 this.encryptedDeviceName
= encryptedDeviceName
;
381 this.deviceId
= deviceId
;
382 this.aciIdentityKeyPair
= aciIdentity
;
383 this.pniIdentityKeyPair
= pniIdentity
;
384 this.registered
= true;
385 this.isMultiDevice
= true;
386 this.lastReceiveTimestamp
= 0;
387 this.pinMasterKey
= null;
388 this.storageManifestVersion
= -1;
389 this.setStorageManifest(null);
390 this.storageKey
= null;
391 final var aciPublicKey
= getAciIdentityKeyPair().getPublicKey();
392 getIdentityKeyStore().saveIdentity(getAci(), aciPublicKey
);
393 getIdentityKeyStore().setIdentityTrustLevel(getAci(), aciPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
394 if (getPniIdentityKeyPair() != null) {
395 final var pniPublicKey
= getPniIdentityKeyPair().getPublicKey();
396 getIdentityKeyStore().saveIdentity(getPni(), pniPublicKey
);
397 getIdentityKeyStore().setIdentityTrustLevel(getPni(), pniPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
401 private void migrateLegacyConfigs() {
402 if (getPassword() == null) {
403 setPassword(KeyUtils
.createPassword());
406 if (getProfileKey() == null) {
407 // Old config file, creating new profile key
408 setProfileKey(KeyUtils
.createProfileKey());
410 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
411 if (isPrimaryDevice() && getPniIdentityKeyPair() == null && getPni() != null) {
412 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
416 private void mergeRecipients(
417 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
418 ) throws SQLException
{
419 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
420 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
423 public void removeRecipient(final RecipientId recipientId
) {
424 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
425 getRecipientStore().deleteRecipientData(recipientId
);
426 getMessageCache().deleteMessages(recipientId
);
427 if (recipientAddress
.serviceId().isPresent()) {
428 final var serviceId
= recipientAddress
.serviceId().get();
429 getAciSessionStore().deleteAllSessions(serviceId
);
430 getPniSessionStore().deleteAllSessions(serviceId
);
431 getIdentityKeyStore().deleteIdentity(serviceId
);
432 getSenderKeyStore().deleteAll(serviceId
);
436 public static File
getFileName(File dataPath
, String account
) {
437 return new File(dataPath
, account
);
440 private static File
getUserPath(final File dataPath
, final String account
) {
441 final var path
= new File(dataPath
, account
+ ".d");
443 IOUtils
.createPrivateDirectories(path
);
444 } catch (IOException e
) {
445 throw new AssertionError("Failed to create user path", e
);
450 private static File
getMessageCachePath(File dataPath
, String account
) {
451 return new File(getUserPath(dataPath
, account
), "msg-cache");
454 private static File
getGroupCachePath(File dataPath
, String account
) {
455 return new File(getUserPath(dataPath
, account
), "group-cache");
458 private static File
getAciPreKeysPath(File dataPath
, String account
) {
459 return new File(getUserPath(dataPath
, account
), "pre-keys");
462 private static File
getAciSignedPreKeysPath(File dataPath
, String account
) {
463 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
466 private static File
getPniPreKeysPath(File dataPath
, String account
) {
467 return new File(getUserPath(dataPath
, account
), "pre-keys-pni");
470 private static File
getPniSignedPreKeysPath(File dataPath
, String account
) {
471 return new File(getUserPath(dataPath
, account
), "signed-pre-keys-pni");
474 private static File
getIdentitiesPath(File dataPath
, String account
) {
475 return new File(getUserPath(dataPath
, account
), "identities");
478 private static File
getSessionsPath(File dataPath
, String account
) {
479 return new File(getUserPath(dataPath
, account
), "sessions");
482 private static File
getSenderKeysPath(File dataPath
, String account
) {
483 return new File(getUserPath(dataPath
, account
), "sender-keys");
486 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
487 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
490 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
491 return new File(getUserPath(dataPath
, account
), "recipients-store");
494 private static File
getStorageManifestFile(File dataPath
, String account
) {
495 return new File(getUserPath(dataPath
, account
), "storage-manifest");
498 private static File
getDatabaseFile(File dataPath
, String account
) {
499 return new File(getUserPath(dataPath
, account
), "account.db");
502 public static boolean accountFileExists(File dataPath
, String account
) {
503 if (account
== null) {
506 var f
= getFileName(dataPath
, account
);
507 return !(!f
.exists() || f
.isDirectory());
511 File dataPath
, String accountPath
, final Settings settings
512 ) throws IOException
{
513 this.dataPath
= dataPath
;
514 this.accountPath
= accountPath
;
515 this.settings
= settings
;
516 final JsonNode rootNode
;
517 synchronized (fileChannel
) {
518 fileChannel
.position(0);
519 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
522 var migratedLegacyConfig
= false;
524 if (rootNode
.hasNonNull("version")) {
525 var accountVersion
= rootNode
.get("version").asInt(1);
526 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
527 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
528 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
529 throw new IOException("Config file was created by a no longer supported older version: "
532 previousStorageVersion
= accountVersion
;
533 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
534 migratedLegacyConfig
= true;
538 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
539 if (rootNode
.hasNonNull("password")) {
540 password
= rootNode
.get("password").asText();
542 if (rootNode
.hasNonNull("serviceEnvironment")) {
543 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
545 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
546 if (rootNode
.hasNonNull("usernameIdentifier")) {
547 username
= rootNode
.get("usernameIdentifier").asText();
549 if (rootNode
.hasNonNull("uuid")) {
551 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
552 } catch (IllegalArgumentException e
) {
553 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
556 if (rootNode
.hasNonNull("pni")) {
558 pni
= PNI
.parseOrThrow(rootNode
.get("pni").asText());
559 } catch (IllegalArgumentException e
) {
560 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
563 if (rootNode
.hasNonNull("sessionId")) {
564 sessionId
= rootNode
.get("sessionId").asText();
566 if (rootNode
.hasNonNull("sessionNumber")) {
567 sessionNumber
= rootNode
.get("sessionNumber").asText();
569 if (rootNode
.hasNonNull("deviceName")) {
570 encryptedDeviceName
= rootNode
.get("deviceName").asText();
572 if (rootNode
.hasNonNull("deviceId")) {
573 deviceId
= rootNode
.get("deviceId").asInt();
575 if (rootNode
.hasNonNull("isMultiDevice")) {
576 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
578 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
579 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
581 int registrationId
= 0;
582 if (rootNode
.hasNonNull("registrationId")) {
583 registrationId
= rootNode
.get("registrationId").asInt();
585 if (rootNode
.hasNonNull("pniRegistrationId")) {
586 localPniRegistrationId
= rootNode
.get("pniRegistrationId").asInt();
588 localPniRegistrationId
= KeyHelper
.generateRegistrationId(false);
590 IdentityKeyPair aciIdentityKeyPair
= null;
591 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
592 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
593 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
594 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
596 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
597 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
598 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
599 pniIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
602 if (rootNode
.hasNonNull("registrationLockPin")) {
603 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
605 if (rootNode
.hasNonNull("pinMasterKey")) {
606 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
608 if (rootNode
.hasNonNull("storageKey")) {
609 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
611 if (rootNode
.hasNonNull("storageManifestVersion")) {
612 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
614 if (rootNode
.hasNonNull("preKeyIdOffset")) {
615 aciPreKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
617 aciPreKeyIdOffset
= 1;
619 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
620 aciNextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
622 aciNextSignedPreKeyId
= 1;
624 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
625 pniPreKeyIdOffset
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
627 pniPreKeyIdOffset
= 1;
629 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
630 pniNextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
632 pniNextSignedPreKeyId
= 1;
634 if (rootNode
.hasNonNull("profileKey")) {
636 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
637 } catch (InvalidInputException e
) {
638 throw new IOException(
639 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
644 if (previousStorageVersion
< 5) {
645 final var legacyRecipientsStoreFile
= getRecipientsStoreFile(dataPath
, accountPath
);
646 if (legacyRecipientsStoreFile
.exists()) {
647 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
648 // Ensure our profile key is stored in profile store
649 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
650 migratedLegacyConfig
= true;
653 if (previousStorageVersion
< 6) {
654 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
656 final var legacyAciPreKeysPath
= getAciPreKeysPath(dataPath
, accountPath
);
657 if (legacyAciPreKeysPath
.exists()) {
658 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, getAciPreKeyStore());
659 migratedLegacyConfig
= true;
661 final var legacyPniPreKeysPath
= getPniPreKeysPath(dataPath
, accountPath
);
662 if (legacyPniPreKeysPath
.exists()) {
663 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, getPniPreKeyStore());
664 migratedLegacyConfig
= true;
666 final var legacyAciSignedPreKeysPath
= getAciSignedPreKeysPath(dataPath
, accountPath
);
667 if (legacyAciSignedPreKeysPath
.exists()) {
668 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, getAciSignedPreKeyStore());
669 migratedLegacyConfig
= true;
671 final var legacyPniSignedPreKeysPath
= getPniSignedPreKeysPath(dataPath
, accountPath
);
672 if (legacyPniSignedPreKeysPath
.exists()) {
673 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, getPniSignedPreKeyStore());
674 migratedLegacyConfig
= true;
676 final var legacySessionsPath
= getSessionsPath(dataPath
, accountPath
);
677 if (legacySessionsPath
.exists()) {
678 LegacySessionStore
.migrate(legacySessionsPath
,
679 getRecipientResolver(),
680 getRecipientAddressResolver(),
681 getAciSessionStore());
682 migratedLegacyConfig
= true;
684 final var legacyIdentitiesPath
= getIdentitiesPath(dataPath
, accountPath
);
685 if (legacyIdentitiesPath
.exists()) {
686 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
687 getRecipientResolver(),
688 getRecipientAddressResolver(),
689 getIdentityKeyStore());
690 migratedLegacyConfig
= true;
692 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
693 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
694 LegacyJsonSignalProtocolStore
.class)
696 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
697 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
698 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
699 migratedLegacyConfig
= true;
702 this.aciIdentityKeyPair
= aciIdentityKeyPair
;
703 this.localRegistrationId
= registrationId
;
705 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
707 final var legacySenderKeysPath
= getSenderKeysPath(dataPath
, accountPath
);
708 if (legacySenderKeysPath
.exists()) {
709 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
710 getRecipientResolver(),
711 getRecipientAddressResolver(),
712 getSenderKeyStore());
713 migratedLegacyConfig
= true;
715 final var legacySenderKeysSharedPath
= getSharedSenderKeysFile(dataPath
, accountPath
);
716 if (legacySenderKeysSharedPath
.exists()) {
717 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
718 getRecipientResolver(),
719 getRecipientAddressResolver(),
720 getSenderKeyStore());
721 migratedLegacyConfig
= true;
723 if (rootNode
.hasNonNull("groupStore")) {
724 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
725 LegacyGroupStore
.Storage
.class);
726 LegacyGroupStore
.migrate(groupStoreStorage
,
727 getGroupCachePath(dataPath
, accountPath
),
728 getRecipientResolver(),
730 migratedLegacyConfig
= true;
733 if (rootNode
.hasNonNull("stickerStore")) {
734 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
735 LegacyStickerStore
.Storage
.class);
736 LegacyStickerStore
.migrate(storage
, getStickerStore());
737 migratedLegacyConfig
= true;
740 if (rootNode
.hasNonNull("configurationStore")) {
741 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
742 ConfigurationStore
.Storage
.class);
743 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
744 this::saveConfigurationStore
);
746 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
749 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
751 if (migratedLegacyConfig
) {
756 private boolean loadLegacyStores(
757 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
759 var migrated
= false;
760 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
761 if (legacyRecipientStoreNode
!= null) {
762 logger
.debug("Migrating legacy recipient store.");
763 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
764 if (legacyRecipientStore
!= null) {
765 legacyRecipientStore
.getAddresses()
766 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
768 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
772 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
773 logger
.debug("Migrating legacy pre key store.");
774 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
776 getAciPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
777 } catch (InvalidMessageException e
) {
778 logger
.warn("Failed to migrate pre key, ignoring", e
);
784 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
785 logger
.debug("Migrating legacy signed pre key store.");
786 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
788 getAciSignedPreKeyStore().storeSignedPreKey(entry
.getKey(),
789 new SignedPreKeyRecord(entry
.getValue()));
790 } catch (InvalidMessageException e
) {
791 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
797 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
798 logger
.debug("Migrating legacy session store.");
799 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
801 getAciSessionStore().storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
802 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
803 } catch (Exception e
) {
804 logger
.warn("Failed to migrate session, ignoring", e
);
810 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
811 logger
.debug("Migrating legacy identity session store.");
812 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
813 if (identity
.getAddress().serviceId().isEmpty()) {
816 final var serviceId
= identity
.getAddress().getServiceId();
817 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
818 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
819 identity
.getIdentityKey(),
820 identity
.getTrustLevel());
825 if (rootNode
.hasNonNull("contactStore")) {
826 logger
.debug("Migrating legacy contact store.");
827 final var contactStoreNode
= rootNode
.get("contactStore");
828 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
829 for (var contact
: contactStore
.getContacts()) {
830 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
831 getContactStore().storeContact(recipientId
,
832 new Contact(contact
.name
,
835 contact
.messageExpirationTime
,
840 // Store profile keys only in profile store
841 var profileKeyString
= contact
.profileKey
;
842 if (profileKeyString
!= null) {
843 final ProfileKey profileKey
;
845 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
846 getProfileStore().storeProfileKey(recipientId
, profileKey
);
847 } catch (InvalidInputException e
) {
848 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
855 if (rootNode
.hasNonNull("profileStore")) {
856 logger
.debug("Migrating legacy profile store.");
857 var profileStoreNode
= rootNode
.get("profileStore");
858 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
859 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
860 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.getAddress());
861 // Not migrating profile key credential here, it was changed to expiring profile key credentials
862 getProfileStore().storeProfileKey(recipientId
, profileEntry
.getProfileKey());
863 final var profile
= profileEntry
.getProfile();
864 if (profile
!= null) {
865 final var capabilities
= new HashSet
<Profile
.Capability
>();
866 if (profile
.getCapabilities() != null) {
867 if (profile
.getCapabilities().gv1Migration
) {
868 capabilities
.add(Profile
.Capability
.gv1Migration
);
870 if (profile
.getCapabilities().storage
) {
871 capabilities
.add(Profile
.Capability
.storage
);
874 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
875 profile
.getGivenName(),
876 profile
.getFamilyName(),
878 profile
.getAboutEmoji(),
881 profile
.isUnrestrictedUnidentifiedAccess()
882 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
883 : profile
.getUnidentifiedAccess() != null
884 ? Profile
.UnidentifiedAccessMode
.ENABLED
885 : Profile
.UnidentifiedAccessMode
.DISABLED
,
887 getProfileStore().storeProfile(recipientId
, newProfile
);
895 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
896 var threadStoreNode
= rootNode
.get("threadStore");
897 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
898 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
899 // Migrate thread info to group and contact store
900 for (var thread
: threadStore
.getThreads()) {
901 if (thread
.id
== null || thread
.id
.isEmpty()) {
905 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
906 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
907 var contact
= getContactStore().getContact(recipientId
);
908 if (contact
!= null) {
909 getContactStore().storeContact(recipientId
,
910 Contact
.newBuilder(contact
)
911 .withMessageExpirationTime(thread
.messageExpirationTime
)
915 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
916 if (groupInfo
instanceof GroupInfoV1
) {
917 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
918 getGroupStore().updateGroup(groupInfo
);
921 } catch (Exception e
) {
922 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
931 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
932 this.configurationStoreStorage
= storage
;
936 private void save() {
937 synchronized (fileChannel
) {
938 var rootNode
= jsonProcessor
.createObjectNode();
939 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
940 .put("username", number
)
941 .put("serviceEnvironment", serviceEnvironment
== null ?
null : serviceEnvironment
.name())
942 .put("usernameIdentifier", username
)
943 .put("uuid", aci
== null ?
null : aci
.toString())
944 .put("pni", pni
== null ?
null : pni
.toString())
945 .put("sessionId", sessionId
)
946 .put("sessionNumber", sessionNumber
)
947 .put("deviceName", encryptedDeviceName
)
948 .put("deviceId", deviceId
)
949 .put("isMultiDevice", isMultiDevice
)
950 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
951 .put("password", password
)
952 .put("registrationId", localRegistrationId
)
953 .put("pniRegistrationId", localPniRegistrationId
)
954 .put("identityPrivateKey",
955 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPrivateKey().serialize()))
957 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPublicKey().serialize()))
958 .put("pniIdentityPrivateKey",
959 pniIdentityKeyPair
== null
961 : Base64
.getEncoder()
962 .encodeToString(pniIdentityKeyPair
.getPrivateKey().serialize()))
963 .put("pniIdentityKey",
964 pniIdentityKeyPair
== null
966 : Base64
.getEncoder().encodeToString(pniIdentityKeyPair
.getPublicKey().serialize()))
967 .put("registrationLockPin", registrationLockPin
)
969 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
971 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
972 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
973 .put("preKeyIdOffset", aciPreKeyIdOffset
)
974 .put("nextSignedPreKeyId", aciNextSignedPreKeyId
)
975 .put("pniPreKeyIdOffset", pniPreKeyIdOffset
)
976 .put("pniNextSignedPreKeyId", pniNextSignedPreKeyId
)
978 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
979 .put("registered", registered
)
980 .putPOJO("configurationStore", configurationStoreStorage
);
982 try (var output
= new ByteArrayOutputStream()) {
983 // Write to memory first to prevent corrupting the file in case of serialization errors
984 jsonProcessor
.writeValue(output
, rootNode
);
985 var input
= new ByteArrayInputStream(output
.toByteArray());
986 fileChannel
.position(0);
987 input
.transferTo(Channels
.newOutputStream(fileChannel
));
988 fileChannel
.truncate(fileChannel
.position());
989 fileChannel
.force(false);
991 } catch (Exception e
) {
992 logger
.error("Error saving file: {}", e
.getMessage(), e
);
997 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
998 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
1000 var lock
= fileChannel
.tryLock();
1003 logger
.debug("Config file is in use by another instance.");
1004 throw new IOException("Config file is in use by another instance.");
1006 logger
.info("Config file is in use by another instance, waiting…");
1007 lock
= fileChannel
.lock();
1008 logger
.info("Config file lock acquired.");
1010 final var result
= new Pair
<>(fileChannel
, lock
);
1014 if (fileChannel
!= null) {
1015 fileChannel
.close();
1020 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1021 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1022 this.aciPreKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
1023 this.aciNextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
1025 this.pniPreKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
1026 this.pniNextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
1031 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1032 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1033 addAciPreKeys(records
);
1035 addPniPreKeys(records
);
1039 public void addAciPreKeys(List
<PreKeyRecord
> records
) {
1040 for (var record : records
) {
1041 if (aciPreKeyIdOffset
!= record.getId()) {
1042 logger
.error("Invalid pre key id {}, expected {}", record.getId(), aciPreKeyIdOffset
);
1043 throw new AssertionError("Invalid pre key id");
1045 getAciPreKeyStore().storePreKey(record.getId(), record);
1046 aciPreKeyIdOffset
= (aciPreKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
1051 public void addPniPreKeys(List
<PreKeyRecord
> records
) {
1052 for (var record : records
) {
1053 if (pniPreKeyIdOffset
!= record.getId()) {
1054 logger
.error("Invalid pre key id {}, expected {}", record.getId(), pniPreKeyIdOffset
);
1055 throw new AssertionError("Invalid pre key id");
1057 getPniPreKeyStore().storePreKey(record.getId(), record);
1058 pniPreKeyIdOffset
= (pniPreKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
1063 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1064 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1065 addAciSignedPreKey(record);
1067 addPniSignedPreKey(record);
1071 public void addAciSignedPreKey(SignedPreKeyRecord
record) {
1072 if (aciNextSignedPreKeyId
!= record.getId()) {
1073 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), aciNextSignedPreKeyId
);
1074 throw new AssertionError("Invalid signed pre key id");
1076 getAciSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1077 aciNextSignedPreKeyId
= (aciNextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
1081 public void addPniSignedPreKey(SignedPreKeyRecord
record) {
1082 if (pniNextSignedPreKeyId
!= record.getId()) {
1083 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), pniNextSignedPreKeyId
);
1084 throw new AssertionError("Invalid signed pre key id");
1086 getPniSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1087 pniNextSignedPreKeyId
= (pniNextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
1091 public int getPreviousStorageVersion() {
1092 return previousStorageVersion
;
1095 public SignalServiceDataStore
getSignalServiceDataStore() {
1096 return new SignalServiceDataStore() {
1098 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1099 if (accountIdentifier
.equals(aci
)) {
1101 } else if (accountIdentifier
.equals(pni
)) {
1104 throw new IllegalArgumentException("No matching store found for " + accountIdentifier
);
1109 public SignalServiceAccountDataStore
aci() {
1110 return getAciSignalServiceAccountDataStore();
1114 public SignalServiceAccountDataStore
pni() {
1115 return getPniSignalServiceAccountDataStore();
1119 public boolean isMultiDevice() {
1120 return SignalAccount
.this.isMultiDevice();
1125 private SignalServiceAccountDataStore
getAciSignalServiceAccountDataStore() {
1126 return getOrCreate(() -> aciSignalProtocolStore
,
1127 () -> aciSignalProtocolStore
= new SignalProtocolStore(getAciPreKeyStore(),
1128 getAciSignedPreKeyStore(),
1129 getAciSessionStore(),
1130 getAciIdentityKeyStore(),
1131 getSenderKeyStore(),
1132 this::isMultiDevice
));
1135 private SignalServiceAccountDataStore
getPniSignalServiceAccountDataStore() {
1136 return getOrCreate(() -> pniSignalProtocolStore
,
1137 () -> pniSignalProtocolStore
= new SignalProtocolStore(getPniPreKeyStore(),
1138 getPniSignedPreKeyStore(),
1139 getPniSessionStore(),
1140 getPniIdentityKeyStore(),
1141 getSenderKeyStore(),
1142 this::isMultiDevice
));
1145 private PreKeyStore
getAciPreKeyStore() {
1146 return getOrCreate(() -> aciPreKeyStore
,
1147 () -> aciPreKeyStore
= new PreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1150 private SignedPreKeyStore
getAciSignedPreKeyStore() {
1151 return getOrCreate(() -> aciSignedPreKeyStore
,
1152 () -> aciSignedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1155 private PreKeyStore
getPniPreKeyStore() {
1156 return getOrCreate(() -> pniPreKeyStore
,
1157 () -> pniPreKeyStore
= new PreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1160 private SignedPreKeyStore
getPniSignedPreKeyStore() {
1161 return getOrCreate(() -> pniSignedPreKeyStore
,
1162 () -> pniSignedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1165 public SessionStore
getAciSessionStore() {
1166 return getOrCreate(() -> aciSessionStore
,
1167 () -> aciSessionStore
= new SessionStore(getAccountDatabase(), ServiceIdType
.ACI
));
1170 public SessionStore
getPniSessionStore() {
1171 return getOrCreate(() -> pniSessionStore
,
1172 () -> pniSessionStore
= new SessionStore(getAccountDatabase(), ServiceIdType
.PNI
));
1175 public IdentityKeyStore
getIdentityKeyStore() {
1176 return getOrCreate(() -> identityKeyStore
,
1177 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(), settings
.trustNewIdentity()));
1180 public SignalIdentityKeyStore
getAciIdentityKeyStore() {
1181 return getOrCreate(() -> aciIdentityKeyStore
,
1182 () -> aciIdentityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1183 () -> aciIdentityKeyPair
,
1184 localRegistrationId
,
1185 getIdentityKeyStore()));
1188 public SignalIdentityKeyStore
getPniIdentityKeyStore() {
1189 return getOrCreate(() -> pniIdentityKeyStore
,
1190 () -> pniIdentityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1191 () -> pniIdentityKeyPair
,
1192 localRegistrationId
,
1193 getIdentityKeyStore()));
1196 public GroupStore
getGroupStore() {
1197 return getOrCreate(() -> groupStore
,
1198 () -> groupStore
= new GroupStore(getAccountDatabase(),
1199 getRecipientResolver(),
1200 getRecipientIdCreator()));
1203 public ContactsStore
getContactStore() {
1204 return getRecipientStore();
1207 private RecipientIdCreator
getRecipientIdCreator() {
1208 return recipientId
-> getRecipientStore().create(recipientId
);
1211 public RecipientResolver
getRecipientResolver() {
1212 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1215 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1216 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1219 public RecipientAddressResolver
getRecipientAddressResolver() {
1220 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1223 public RecipientStore
getRecipientStore() {
1224 return getOrCreate(() -> recipientStore
,
1225 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1226 this::getSelfRecipientAddress
,
1227 getAccountDatabase()));
1230 public ProfileStore
getProfileStore() {
1231 return getRecipientStore();
1234 public StickerStore
getStickerStore() {
1235 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1238 public SenderKeyStore
getSenderKeyStore() {
1239 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1242 public ConfigurationStore
getConfigurationStore() {
1243 return configurationStore
;
1246 public MessageCache
getMessageCache() {
1247 return getOrCreate(() -> messageCache
,
1248 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1251 public AccountDatabase
getAccountDatabase() {
1252 return getOrCreate(() -> accountDatabase
, () -> {
1254 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1255 } catch (SQLException e
) {
1256 throw new RuntimeException(e
);
1261 public MessageSendLogStore
getMessageSendLogStore() {
1262 return getOrCreate(() -> messageSendLogStore
,
1263 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1264 settings
.disableMessageSendLog()));
1267 public CredentialsProvider
getCredentialsProvider() {
1268 return new CredentialsProvider() {
1270 public ACI
getAci() {
1275 public PNI
getPni() {
1280 public String
getE164() {
1285 public String
getPassword() {
1290 public int getDeviceId() {
1296 public String
getNumber() {
1300 public void setNumber(final String number
) {
1301 this.number
= number
;
1305 public String
getUsername() {
1309 public void setUsername(final String username
) {
1310 this.username
= username
;
1314 public ServiceEnvironment
getServiceEnvironment() {
1315 return serviceEnvironment
;
1318 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1319 this.serviceEnvironment
= serviceEnvironment
;
1323 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1324 return new AccountAttributes(null,
1325 getLocalRegistrationId(),
1328 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1329 getSelfUnidentifiedAccessKey(),
1330 isUnrestrictedUnidentifiedAccess(),
1332 isDiscoverableByPhoneNumber(),
1333 encryptedDeviceName
,
1334 getLocalPniRegistrationId(),
1335 null); // TODO recoveryPassword?
1338 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1339 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aci
: pni
;
1342 public ACI
getAci() {
1346 public void setAci(final ACI aci
) {
1351 public PNI
getPni() {
1355 public void setPni(final PNI updatedPni
) {
1356 if (this.pni
!= null && !this.pni
.equals(updatedPni
)) {
1357 // Clear data for old PNI
1358 identityKeyStore
.deleteIdentity(this.pni
);
1359 getPniPreKeyStore().removeAllPreKeys();
1360 getPniSignedPreKeyStore().removeAllSignedPreKeys();
1363 this.pni
= updatedPni
;
1368 final PNI updatedPni
,
1369 final IdentityKeyPair pniIdentityKeyPair
,
1370 final SignedPreKeyRecord pniSignedPreKey
,
1371 final int localPniRegistrationId
1375 setPniIdentityKeyPair(pniIdentityKeyPair
);
1376 addPniSignedPreKey(pniSignedPreKey
);
1377 setLocalPniRegistrationId(localPniRegistrationId
);
1380 public SignalServiceAddress
getSelfAddress() {
1381 return new SignalServiceAddress(aci
, number
);
1384 public RecipientAddress
getSelfRecipientAddress() {
1385 return new RecipientAddress(aci
, pni
, number
, username
);
1388 public RecipientId
getSelfRecipientId() {
1389 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1392 public String
getSessionId(final String forNumber
) {
1393 if (!forNumber
.equals(sessionNumber
)) {
1399 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1400 this.sessionNumber
= sessionNumber
;
1401 this.sessionId
= sessionId
;
1405 public byte[] getEncryptedDeviceName() {
1406 return encryptedDeviceName
== null ?
null : Base64
.getDecoder().decode(encryptedDeviceName
);
1409 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1410 this.encryptedDeviceName
= encryptedDeviceName
;
1414 public int getDeviceId() {
1418 public boolean isPrimaryDevice() {
1419 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1422 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1423 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciIdentityKeyPair
: pniIdentityKeyPair
;
1426 public IdentityKeyPair
getAciIdentityKeyPair() {
1427 return aciIdentityKeyPair
;
1430 public IdentityKeyPair
getPniIdentityKeyPair() {
1431 return pniIdentityKeyPair
;
1434 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1435 pniIdentityKeyPair
= identityKeyPair
;
1436 final var pniPublicKey
= identityKeyPair
.getPublicKey();
1437 getIdentityKeyStore().saveIdentity(getPni(), pniPublicKey
);
1438 getIdentityKeyStore().setIdentityTrustLevel(getPni(), pniPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1442 public int getLocalRegistrationId() {
1443 return localRegistrationId
;
1446 public int getLocalPniRegistrationId() {
1447 return localPniRegistrationId
;
1450 public void setLocalPniRegistrationId(final int localPniRegistrationId
) {
1451 this.localPniRegistrationId
= localPniRegistrationId
;
1455 public String
getPassword() {
1459 private void setPassword(final String password
) {
1460 this.password
= password
;
1464 public void setRegistrationLockPin(final String registrationLockPin
) {
1465 this.registrationLockPin
= registrationLockPin
;
1469 public String
getRegistrationLockPin() {
1470 return registrationLockPin
;
1473 public String
getRegistrationLock() {
1474 final var masterKey
= getPinBackedMasterKey();
1475 if (masterKey
== null) {
1478 return masterKey
.deriveRegistrationLock();
1481 public MasterKey
getPinBackedMasterKey() {
1482 if (registrationLockPin
== null) {
1485 return pinMasterKey
;
1488 public MasterKey
getOrCreatePinMasterKey() {
1489 if (pinMasterKey
== null) {
1490 pinMasterKey
= KeyUtils
.createMasterKey();
1493 return pinMasterKey
;
1496 public StorageKey
getStorageKey() {
1497 if (pinMasterKey
!= null) {
1498 return pinMasterKey
.deriveStorageServiceKey();
1503 public StorageKey
getOrCreateStorageKey() {
1504 if (isPrimaryDevice()) {
1505 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1510 public void setStorageKey(final StorageKey storageKey
) {
1511 if (storageKey
.equals(this.storageKey
)) {
1514 this.storageKey
= storageKey
;
1518 public long getStorageManifestVersion() {
1519 return this.storageManifestVersion
;
1522 public void setStorageManifestVersion(final long storageManifestVersion
) {
1523 if (storageManifestVersion
== this.storageManifestVersion
) {
1526 this.storageManifestVersion
= storageManifestVersion
;
1530 public Optional
<SignalStorageManifest
> getStorageManifest() {
1531 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1532 if (!storageManifestFile
.exists()) {
1533 return Optional
.empty();
1535 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1536 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1537 } catch (IOException e
) {
1538 logger
.warn("Failed to read local storage manifest.", e
);
1539 return Optional
.empty();
1543 public void setStorageManifest(SignalStorageManifest manifest
) {
1544 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1545 if (manifest
== null) {
1546 if (storageManifestFile
.exists()) {
1548 Files
.delete(storageManifestFile
.toPath());
1549 } catch (IOException e
) {
1550 logger
.error("Failed to delete local storage manifest.", e
);
1556 final var manifestBytes
= manifest
.serialize();
1557 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1558 outputStream
.write(manifestBytes
);
1559 } catch (IOException e
) {
1560 logger
.error("Failed to store local storage manifest.", e
);
1564 public ProfileKey
getProfileKey() {
1568 public void setProfileKey(final ProfileKey profileKey
) {
1569 if (profileKey
.equals(this.profileKey
)) {
1572 this.profileKey
= profileKey
;
1574 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1577 public byte[] getSelfUnidentifiedAccessKey() {
1578 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1581 public int getPreKeyIdOffset(ServiceIdType serviceIdType
) {
1582 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciPreKeyIdOffset
: pniPreKeyIdOffset
;
1585 public int getNextSignedPreKeyId(ServiceIdType serviceIdType
) {
1586 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciNextSignedPreKeyId
: pniNextSignedPreKeyId
;
1589 public boolean isRegistered() {
1593 public void setRegistered(final boolean registered
) {
1594 this.registered
= registered
;
1598 public boolean isMultiDevice() {
1599 return isMultiDevice
;
1602 public void setMultiDevice(final boolean multiDevice
) {
1603 if (isMultiDevice
== multiDevice
) {
1606 isMultiDevice
= multiDevice
;
1610 public long getLastReceiveTimestamp() {
1611 return lastReceiveTimestamp
;
1614 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1615 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1619 public boolean isUnrestrictedUnidentifiedAccess() {
1620 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1621 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1624 public boolean isDiscoverableByPhoneNumber() {
1625 final var phoneNumberUnlisted
= configurationStore
.getPhoneNumberUnlisted();
1626 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1629 public void finishRegistration(final ACI aci
, final PNI pni
, final MasterKey masterKey
, final String pin
) {
1630 this.pinMasterKey
= masterKey
;
1631 this.storageManifestVersion
= -1;
1632 this.setStorageManifest(null);
1633 this.storageKey
= null;
1634 this.encryptedDeviceName
= null;
1635 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1636 this.isMultiDevice
= false;
1637 this.registered
= true;
1640 this.registrationLockPin
= pin
;
1641 this.lastReceiveTimestamp
= 0;
1645 getAciSessionStore().archiveAllSessions();
1646 getPniSessionStore().archiveAllSessions();
1647 getSenderKeyStore().deleteAll();
1648 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1649 final var aciPublicKey
= getAciIdentityKeyPair().getPublicKey();
1650 getIdentityKeyStore().saveIdentity(getAci(), aciPublicKey
);
1651 getIdentityKeyStore().setIdentityTrustLevel(getAci(), aciPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1652 if (getPniIdentityKeyPair() == null) {
1653 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
1655 final var pniPublicKey
= getPniIdentityKeyPair().getPublicKey();
1656 getIdentityKeyStore().saveIdentity(getPni(), pniPublicKey
);
1657 getIdentityKeyStore().setIdentityTrustLevel(getPni(), pniPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1661 public void deleteAccountData() throws IOException
{
1663 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1664 .sorted(Comparator
.reverseOrder())) {
1665 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1666 Files
.delete(file
.next());
1669 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1673 public void close() {
1674 synchronized (fileChannel
) {
1675 if (accountDatabase
!= null) {
1677 accountDatabase
.close();
1678 } catch (SQLException e
) {
1679 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1682 if (messageSendLogStore
!= null) {
1683 messageSendLogStore
.close();
1688 } catch (ClosedChannelException ignored
) {
1690 fileChannel
.close();
1691 } catch (IOException e
) {
1692 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1697 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1698 var value
= supplier
.get();
1699 if (value
!= null) {
1703 synchronized (LOCK
) {
1704 value
= supplier
.get();
1705 if (value
!= null) {
1709 return supplier
.get();
1713 private interface Callable
{