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
.Contact
;
8 import org
.asamk
.signal
.manager
.api
.GroupId
;
9 import org
.asamk
.signal
.manager
.api
.Pair
;
10 import org
.asamk
.signal
.manager
.api
.Profile
;
11 import org
.asamk
.signal
.manager
.api
.ServiceEnvironment
;
12 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
13 import org
.asamk
.signal
.manager
.helper
.RecipientAddressResolver
;
14 import org
.asamk
.signal
.manager
.storage
.configuration
.ConfigurationStore
;
15 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
16 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
17 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
18 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
19 import org
.asamk
.signal
.manager
.storage
.groups
.LegacyGroupStore
;
20 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
21 import org
.asamk
.signal
.manager
.storage
.identities
.LegacyIdentityKeyStore
;
22 import org
.asamk
.signal
.manager
.storage
.identities
.SignalIdentityKeyStore
;
23 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
24 import org
.asamk
.signal
.manager
.storage
.prekeys
.KyberPreKeyStore
;
25 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacyPreKeyStore
;
26 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacySignedPreKeyStore
;
27 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
28 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
29 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
30 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
31 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
32 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
33 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
34 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore2
;
35 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
36 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientIdCreator
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
39 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
40 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientTrustedResolver
;
41 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
42 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeyRecordStore
;
43 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeySharedStore
;
44 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
45 import org
.asamk
.signal
.manager
.storage
.sessions
.LegacySessionStore
;
46 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
47 import org
.asamk
.signal
.manager
.storage
.stickers
.LegacyStickerStore
;
48 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
49 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
50 import org
.asamk
.signal
.manager
.util
.IOUtils
;
51 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
52 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
53 import org
.signal
.libsignal
.protocol
.InvalidMessageException
;
54 import org
.signal
.libsignal
.protocol
.SignalProtocolAddress
;
55 import org
.signal
.libsignal
.protocol
.state
.KyberPreKeyRecord
;
56 import org
.signal
.libsignal
.protocol
.state
.PreKeyRecord
;
57 import org
.signal
.libsignal
.protocol
.state
.SessionRecord
;
58 import org
.signal
.libsignal
.protocol
.state
.SignedPreKeyRecord
;
59 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
60 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
61 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
62 import org
.slf4j
.Logger
;
63 import org
.slf4j
.LoggerFactory
;
64 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
65 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
66 import org
.whispersystems
.signalservice
.api
.account
.AccountAttributes
;
67 import org
.whispersystems
.signalservice
.api
.account
.PreKeyCollection
;
68 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
69 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
70 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
71 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.ACI
;
72 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.PNI
;
73 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
74 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
75 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
76 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
77 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
78 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
80 import java
.io
.ByteArrayInputStream
;
81 import java
.io
.ByteArrayOutputStream
;
82 import java
.io
.Closeable
;
84 import java
.io
.FileInputStream
;
85 import java
.io
.FileOutputStream
;
86 import java
.io
.IOException
;
87 import java
.io
.RandomAccessFile
;
88 import java
.nio
.channels
.Channels
;
89 import java
.nio
.channels
.ClosedChannelException
;
90 import java
.nio
.channels
.FileChannel
;
91 import java
.nio
.channels
.FileLock
;
92 import java
.nio
.file
.Files
;
93 import java
.sql
.Connection
;
94 import java
.sql
.SQLException
;
95 import java
.util
.Base64
;
96 import java
.util
.Comparator
;
97 import java
.util
.HashSet
;
98 import java
.util
.List
;
99 import java
.util
.Optional
;
100 import java
.util
.function
.Supplier
;
102 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.PREKEY_MAXIMUM_ID
;
103 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.getCapabilities
;
105 public class SignalAccount
implements Closeable
{
107 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
109 private static final int MINIMUM_STORAGE_VERSION
= 1;
110 private static final int CURRENT_STORAGE_VERSION
= 7;
112 private final Object LOCK
= new Object();
114 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
116 private final FileChannel fileChannel
;
117 private final FileLock lock
;
119 private int previousStorageVersion
;
121 private File dataPath
;
122 private String accountPath
;
123 private ServiceEnvironment serviceEnvironment
;
124 private String number
;
125 private String username
;
128 private String sessionId
;
129 private String sessionNumber
;
130 private String encryptedDeviceName
;
131 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
132 private boolean isMultiDevice
= false;
133 private String password
;
134 private String registrationLockPin
;
135 private MasterKey pinMasterKey
;
136 private StorageKey storageKey
;
137 private long storageManifestVersion
= -1;
138 private ProfileKey profileKey
;
139 private Settings settings
;
140 private long lastReceiveTimestamp
= 0;
142 private boolean registered
= false;
144 private final AccountData aciAccountData
= new AccountData(ServiceIdType
.ACI
);
145 private final AccountData pniAccountData
= new AccountData(ServiceIdType
.PNI
);
146 private IdentityKeyStore identityKeyStore
;
147 private SenderKeyStore senderKeyStore
;
148 private GroupStore groupStore
;
149 private RecipientStore recipientStore
;
150 private StickerStore stickerStore
;
151 private ConfigurationStore configurationStore
;
152 private ConfigurationStore
.Storage configurationStoreStorage
;
154 private MessageCache messageCache
;
155 private MessageSendLogStore messageSendLogStore
;
157 private AccountDatabase accountDatabase
;
159 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
160 this.fileChannel
= fileChannel
;
164 public static SignalAccount
load(
165 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
166 ) throws IOException
{
167 logger
.trace("Opening account file");
168 final var fileName
= getFileName(dataPath
, accountPath
);
169 final var pair
= openFileChannel(fileName
, waitForLock
);
171 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
172 logger
.trace("Loading account file");
173 signalAccount
.load(dataPath
, accountPath
, settings
);
174 logger
.trace("Migrating legacy parts of account file");
175 signalAccount
.migrateLegacyConfigs();
177 return signalAccount
;
178 } catch (Throwable e
) {
179 pair
.second().close();
180 pair
.first().close();
185 public static SignalAccount
create(
189 ServiceEnvironment serviceEnvironment
,
190 IdentityKeyPair aciIdentityKey
,
191 IdentityKeyPair pniIdentityKey
,
193 int pniRegistrationId
,
194 ProfileKey profileKey
,
195 final Settings settings
196 ) throws IOException
{
197 IOUtils
.createPrivateDirectories(dataPath
);
198 var fileName
= getFileName(dataPath
, accountPath
);
199 if (!fileName
.exists()) {
200 IOUtils
.createPrivateFile(fileName
);
203 final var pair
= openFileChannel(fileName
, true);
204 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
206 signalAccount
.accountPath
= accountPath
;
207 signalAccount
.number
= number
;
208 signalAccount
.serviceEnvironment
= serviceEnvironment
;
209 signalAccount
.profileKey
= profileKey
;
211 signalAccount
.dataPath
= dataPath
;
212 signalAccount
.aciAccountData
.setIdentityKeyPair(aciIdentityKey
);
213 signalAccount
.pniAccountData
.setIdentityKeyPair(pniIdentityKey
);
214 signalAccount
.aciAccountData
.setLocalRegistrationId(registrationId
);
215 signalAccount
.pniAccountData
.setLocalRegistrationId(pniRegistrationId
);
216 signalAccount
.settings
= settings
;
217 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
219 signalAccount
.registered
= false;
221 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
222 signalAccount
.migrateLegacyConfigs();
223 signalAccount
.save();
225 return signalAccount
;
228 public static SignalAccount
createOrUpdateLinkedAccount(
232 ServiceEnvironment serviceEnvironment
,
236 String encryptedDeviceName
,
238 IdentityKeyPair aciIdentityKey
,
239 IdentityKeyPair pniIdentityKey
,
241 int pniRegistrationId
,
242 ProfileKey profileKey
,
243 final Settings settings
244 ) throws IOException
{
245 IOUtils
.createPrivateDirectories(dataPath
);
246 var fileName
= getFileName(dataPath
, accountPath
);
247 if (!fileName
.exists()) {
248 return createLinkedAccount(dataPath
,
265 final var signalAccount
= load(dataPath
, accountPath
, true, settings
);
266 signalAccount
.setProvisioningData(number
,
275 signalAccount
.getRecipientTrustedResolver()
276 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
277 signalAccount
.aciAccountData
.getSessionStore().archiveAllSessions();
278 signalAccount
.pniAccountData
.getSessionStore().archiveAllSessions();
279 signalAccount
.getSenderKeyStore().deleteAll();
280 signalAccount
.clearAllPreKeys();
281 return signalAccount
;
284 public void initDatabase() {
285 getAccountDatabase();
288 private void clearAllPreKeys() {
289 clearAllPreKeys(ServiceIdType
.ACI
);
290 clearAllPreKeys(ServiceIdType
.PNI
);
293 private void clearAllPreKeys(ServiceIdType serviceIdType
) {
294 final var accountData
= getAccountData(serviceIdType
);
295 resetPreKeyOffsets(serviceIdType
);
296 resetKyberPreKeyOffsets(serviceIdType
);
297 accountData
.getPreKeyStore().removeAllPreKeys();
298 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
299 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
303 private void setPreKeys(ServiceIdType serviceIdType
, PreKeyCollection preKeyCollection
) {
304 final var accountData
= getAccountData(serviceIdType
);
305 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
306 preKeyMetadata
.nextSignedPreKeyId
= preKeyCollection
.getSignedPreKey().getId();
307 preKeyMetadata
.kyberPreKeyIdOffset
= preKeyCollection
.getLastResortKyberPreKey().getId();
309 accountData
.getPreKeyStore().removeAllPreKeys();
310 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
311 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
313 addSignedPreKey(serviceIdType
, preKeyCollection
.getSignedPreKey());
314 addLastResortKyberPreKey(serviceIdType
, preKeyCollection
.getLastResortKyberPreKey());
319 private static SignalAccount
createLinkedAccount(
323 ServiceEnvironment serviceEnvironment
,
327 String encryptedDeviceName
,
329 IdentityKeyPair aciIdentityKey
,
330 IdentityKeyPair pniIdentityKey
,
332 int pniRegistrationId
,
333 ProfileKey profileKey
,
334 final Settings settings
335 ) throws IOException
{
336 var fileName
= getFileName(dataPath
, accountPath
);
337 IOUtils
.createPrivateFile(fileName
);
339 final var pair
= openFileChannel(fileName
, true);
340 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
342 signalAccount
.dataPath
= dataPath
;
343 signalAccount
.accountPath
= accountPath
;
344 signalAccount
.serviceEnvironment
= serviceEnvironment
;
345 signalAccount
.aciAccountData
.setLocalRegistrationId(registrationId
);
346 signalAccount
.pniAccountData
.setLocalRegistrationId(pniRegistrationId
);
347 signalAccount
.settings
= settings
;
348 signalAccount
.setProvisioningData(number
,
358 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
360 signalAccount
.getRecipientTrustedResolver()
361 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
362 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
363 signalAccount
.migrateLegacyConfigs();
364 signalAccount
.clearAllPreKeys();
365 signalAccount
.save();
367 return signalAccount
;
370 private void setProvisioningData(
374 final String password
,
375 final String encryptedDeviceName
,
377 final IdentityKeyPair aciIdentity
,
378 final IdentityKeyPair pniIdentity
,
379 final ProfileKey profileKey
381 this.number
= number
;
384 this.password
= password
;
385 this.profileKey
= profileKey
;
386 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
387 this.encryptedDeviceName
= encryptedDeviceName
;
388 this.deviceId
= deviceId
;
389 this.aciAccountData
.setIdentityKeyPair(aciIdentity
);
390 this.pniAccountData
.setIdentityKeyPair(pniIdentity
);
391 this.registered
= true;
392 this.isMultiDevice
= true;
393 this.lastReceiveTimestamp
= 0;
394 this.pinMasterKey
= null;
395 this.storageManifestVersion
= -1;
396 this.setStorageManifest(null);
397 this.storageKey
= null;
398 trustSelfIdentity(ServiceIdType
.ACI
);
399 trustSelfIdentity(ServiceIdType
.PNI
);
402 private void migrateLegacyConfigs() {
403 if (getPassword() == null) {
404 setPassword(KeyUtils
.createPassword());
407 if (getProfileKey() == null) {
408 // Old config file, creating new profile key
409 setProfileKey(KeyUtils
.createProfileKey());
411 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
412 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
413 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
417 private void mergeRecipients(
418 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
419 ) throws SQLException
{
420 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
421 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
424 public void removeRecipient(final RecipientId recipientId
) {
425 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
426 getRecipientStore().deleteRecipientData(recipientId
);
427 getMessageCache().deleteMessages(recipientId
);
428 if (recipientAddress
.serviceId().isPresent()) {
429 final var serviceId
= recipientAddress
.serviceId().get();
430 aciAccountData
.getSessionStore().deleteAllSessions(serviceId
);
431 pniAccountData
.getSessionStore().deleteAllSessions(serviceId
);
432 getIdentityKeyStore().deleteIdentity(serviceId
);
433 getSenderKeyStore().deleteAll(serviceId
);
437 public static File
getFileName(File dataPath
, String account
) {
438 return new File(dataPath
, account
);
441 private static File
getUserPath(final File dataPath
, final String account
) {
442 final var path
= new File(dataPath
, account
+ ".d");
444 IOUtils
.createPrivateDirectories(path
);
445 } catch (IOException e
) {
446 throw new AssertionError("Failed to create user path", e
);
451 private static File
getMessageCachePath(File dataPath
, String account
) {
452 return new File(getUserPath(dataPath
, account
), "msg-cache");
455 private static File
getGroupCachePath(File dataPath
, String account
) {
456 return new File(getUserPath(dataPath
, account
), "group-cache");
459 private static File
getAciPreKeysPath(File dataPath
, String account
) {
460 return new File(getUserPath(dataPath
, account
), "pre-keys");
463 private static File
getAciSignedPreKeysPath(File dataPath
, String account
) {
464 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
467 private static File
getPniPreKeysPath(File dataPath
, String account
) {
468 return new File(getUserPath(dataPath
, account
), "pre-keys-pni");
471 private static File
getPniSignedPreKeysPath(File dataPath
, String account
) {
472 return new File(getUserPath(dataPath
, account
), "signed-pre-keys-pni");
475 private static File
getIdentitiesPath(File dataPath
, String account
) {
476 return new File(getUserPath(dataPath
, account
), "identities");
479 private static File
getSessionsPath(File dataPath
, String account
) {
480 return new File(getUserPath(dataPath
, account
), "sessions");
483 private static File
getSenderKeysPath(File dataPath
, String account
) {
484 return new File(getUserPath(dataPath
, account
), "sender-keys");
487 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
488 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
491 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
492 return new File(getUserPath(dataPath
, account
), "recipients-store");
495 private static File
getStorageManifestFile(File dataPath
, String account
) {
496 return new File(getUserPath(dataPath
, account
), "storage-manifest");
499 private static File
getDatabaseFile(File dataPath
, String account
) {
500 return new File(getUserPath(dataPath
, account
), "account.db");
503 public static boolean accountFileExists(File dataPath
, String account
) {
504 if (account
== null) {
507 var f
= getFileName(dataPath
, account
);
508 return !(!f
.exists() || f
.isDirectory());
512 File dataPath
, String accountPath
, final Settings settings
513 ) throws IOException
{
514 this.dataPath
= dataPath
;
515 this.accountPath
= accountPath
;
516 this.settings
= settings
;
517 final JsonNode rootNode
;
518 synchronized (fileChannel
) {
519 fileChannel
.position(0);
520 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
523 var migratedLegacyConfig
= false;
525 if (rootNode
.hasNonNull("version")) {
526 var accountVersion
= rootNode
.get("version").asInt(1);
527 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
528 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
529 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
530 throw new IOException("Config file was created by a no longer supported older version: "
533 previousStorageVersion
= accountVersion
;
534 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
535 migratedLegacyConfig
= true;
539 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
540 if (rootNode
.hasNonNull("password")) {
541 password
= rootNode
.get("password").asText();
543 if (rootNode
.hasNonNull("serviceEnvironment")) {
544 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
546 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
547 if (rootNode
.hasNonNull("usernameIdentifier")) {
548 username
= rootNode
.get("usernameIdentifier").asText();
550 if (rootNode
.hasNonNull("uuid")) {
552 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
553 } catch (IllegalArgumentException e
) {
554 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
557 if (rootNode
.hasNonNull("pni")) {
559 pni
= PNI
.parseOrThrow(rootNode
.get("pni").asText());
560 } catch (IllegalArgumentException e
) {
561 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
564 if (rootNode
.hasNonNull("sessionId")) {
565 sessionId
= rootNode
.get("sessionId").asText();
567 if (rootNode
.hasNonNull("sessionNumber")) {
568 sessionNumber
= rootNode
.get("sessionNumber").asText();
570 if (rootNode
.hasNonNull("deviceName")) {
571 encryptedDeviceName
= rootNode
.get("deviceName").asText();
573 if (rootNode
.hasNonNull("deviceId")) {
574 deviceId
= rootNode
.get("deviceId").asInt();
576 if (rootNode
.hasNonNull("isMultiDevice")) {
577 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
579 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
580 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
582 int registrationId
= 0;
583 if (rootNode
.hasNonNull("registrationId")) {
584 registrationId
= rootNode
.get("registrationId").asInt();
586 if (rootNode
.hasNonNull("pniRegistrationId")) {
587 pniAccountData
.setLocalRegistrationId(rootNode
.get("pniRegistrationId").asInt());
589 pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
591 IdentityKeyPair aciIdentityKeyPair
= null;
592 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
593 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
594 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
595 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
597 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
598 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
599 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
600 pniAccountData
.setIdentityKeyPair(KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
));
603 if (rootNode
.hasNonNull("registrationLockPin")) {
604 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
606 if (rootNode
.hasNonNull("pinMasterKey")) {
607 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
609 if (rootNode
.hasNonNull("storageKey")) {
610 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
612 if (rootNode
.hasNonNull("storageManifestVersion")) {
613 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
615 if (rootNode
.hasNonNull("preKeyIdOffset")) {
616 aciAccountData
.preKeyMetadata
.preKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
618 aciAccountData
.preKeyMetadata
.preKeyIdOffset
= getRandomPreKeyIdOffset();
620 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
621 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
623 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
625 if (rootNode
.hasNonNull("activeSignedPreKeyId")) {
626 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("activeSignedPreKeyId").asInt(-1);
628 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
630 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
631 pniAccountData
.preKeyMetadata
.preKeyIdOffset
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
633 pniAccountData
.preKeyMetadata
.preKeyIdOffset
= getRandomPreKeyIdOffset();
635 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
636 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
638 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
640 if (rootNode
.hasNonNull("pniActiveSignedPreKeyId")) {
641 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("pniActiveSignedPreKeyId").asInt(-1);
643 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
645 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
646 aciAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
648 aciAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
650 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
651 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId")
654 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
656 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
657 pniAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
659 pniAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
661 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
662 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get(
663 "pniActiveLastResortKyberPreKeyId").asInt(-1);
665 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
667 if (rootNode
.hasNonNull("profileKey")) {
669 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
670 } catch (InvalidInputException e
) {
671 throw new IOException(
672 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
677 if (previousStorageVersion
< 5) {
678 final var legacyRecipientsStoreFile
= getRecipientsStoreFile(dataPath
, accountPath
);
679 if (legacyRecipientsStoreFile
.exists()) {
680 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
681 // Ensure our profile key is stored in profile store
682 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
683 migratedLegacyConfig
= true;
686 if (previousStorageVersion
< 6) {
687 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
689 final var legacyAciPreKeysPath
= getAciPreKeysPath(dataPath
, accountPath
);
690 if (legacyAciPreKeysPath
.exists()) {
691 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, aciAccountData
.getPreKeyStore());
692 migratedLegacyConfig
= true;
694 final var legacyPniPreKeysPath
= getPniPreKeysPath(dataPath
, accountPath
);
695 if (legacyPniPreKeysPath
.exists()) {
696 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, pniAccountData
.getPreKeyStore());
697 migratedLegacyConfig
= true;
699 final var legacyAciSignedPreKeysPath
= getAciSignedPreKeysPath(dataPath
, accountPath
);
700 if (legacyAciSignedPreKeysPath
.exists()) {
701 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, aciAccountData
.getSignedPreKeyStore());
702 migratedLegacyConfig
= true;
704 final var legacyPniSignedPreKeysPath
= getPniSignedPreKeysPath(dataPath
, accountPath
);
705 if (legacyPniSignedPreKeysPath
.exists()) {
706 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, pniAccountData
.getSignedPreKeyStore());
707 migratedLegacyConfig
= true;
709 final var legacySessionsPath
= getSessionsPath(dataPath
, accountPath
);
710 if (legacySessionsPath
.exists()) {
711 LegacySessionStore
.migrate(legacySessionsPath
,
712 getRecipientResolver(),
713 getRecipientAddressResolver(),
714 aciAccountData
.getSessionStore());
715 migratedLegacyConfig
= true;
717 final var legacyIdentitiesPath
= getIdentitiesPath(dataPath
, accountPath
);
718 if (legacyIdentitiesPath
.exists()) {
719 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
720 getRecipientResolver(),
721 getRecipientAddressResolver(),
722 getIdentityKeyStore());
723 migratedLegacyConfig
= true;
725 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
726 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
727 LegacyJsonSignalProtocolStore
.class)
729 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
730 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
731 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
732 migratedLegacyConfig
= true;
735 this.aciAccountData
.setIdentityKeyPair(aciIdentityKeyPair
);
736 this.aciAccountData
.setLocalRegistrationId(registrationId
);
738 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
740 final var legacySenderKeysPath
= getSenderKeysPath(dataPath
, accountPath
);
741 if (legacySenderKeysPath
.exists()) {
742 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
743 getRecipientResolver(),
744 getRecipientAddressResolver(),
745 getSenderKeyStore());
746 migratedLegacyConfig
= true;
748 final var legacySenderKeysSharedPath
= getSharedSenderKeysFile(dataPath
, accountPath
);
749 if (legacySenderKeysSharedPath
.exists()) {
750 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
751 getRecipientResolver(),
752 getRecipientAddressResolver(),
753 getSenderKeyStore());
754 migratedLegacyConfig
= true;
756 if (rootNode
.hasNonNull("groupStore")) {
757 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
758 LegacyGroupStore
.Storage
.class);
759 LegacyGroupStore
.migrate(groupStoreStorage
,
760 getGroupCachePath(dataPath
, accountPath
),
761 getRecipientResolver(),
763 migratedLegacyConfig
= true;
766 if (rootNode
.hasNonNull("stickerStore")) {
767 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
768 LegacyStickerStore
.Storage
.class);
769 LegacyStickerStore
.migrate(storage
, getStickerStore());
770 migratedLegacyConfig
= true;
773 if (rootNode
.hasNonNull("configurationStore")) {
774 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
775 ConfigurationStore
.Storage
.class);
776 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
777 this::saveConfigurationStore
);
779 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
782 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
784 if (migratedLegacyConfig
) {
789 private boolean loadLegacyStores(
790 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
792 var migrated
= false;
793 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
794 if (legacyRecipientStoreNode
!= null) {
795 logger
.debug("Migrating legacy recipient store.");
796 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
797 if (legacyRecipientStore
!= null) {
798 legacyRecipientStore
.getAddresses()
799 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
801 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
805 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
806 logger
.debug("Migrating legacy pre key store.");
807 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
809 aciAccountData
.getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
810 } catch (InvalidMessageException e
) {
811 logger
.warn("Failed to migrate pre key, ignoring", e
);
817 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
818 logger
.debug("Migrating legacy signed pre key store.");
819 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
821 aciAccountData
.getSignedPreKeyStore()
822 .storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
823 } catch (InvalidMessageException e
) {
824 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
830 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
831 logger
.debug("Migrating legacy session store.");
832 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
834 aciAccountData
.getSessionStore()
835 .storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(), session
.deviceId
),
836 new SessionRecord(session
.sessionRecord
));
837 } catch (Exception e
) {
838 logger
.warn("Failed to migrate session, ignoring", e
);
844 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
845 logger
.debug("Migrating legacy identity session store.");
846 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
847 if (identity
.getAddress().serviceId().isEmpty()) {
850 final var serviceId
= identity
.getAddress().serviceId().get();
851 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
852 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
853 identity
.getIdentityKey(),
854 identity
.getTrustLevel());
859 if (rootNode
.hasNonNull("contactStore")) {
860 logger
.debug("Migrating legacy contact store.");
861 final var contactStoreNode
= rootNode
.get("contactStore");
862 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
863 for (var contact
: contactStore
.getContacts()) {
864 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
865 getContactStore().storeContact(recipientId
,
866 new Contact(contact
.name
,
869 contact
.messageExpirationTime
,
874 // Store profile keys only in profile store
875 var profileKeyString
= contact
.profileKey
;
876 if (profileKeyString
!= null) {
877 final ProfileKey profileKey
;
879 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
880 getProfileStore().storeProfileKey(recipientId
, profileKey
);
881 } catch (InvalidInputException e
) {
882 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
889 if (rootNode
.hasNonNull("profileStore")) {
890 logger
.debug("Migrating legacy profile store.");
891 var profileStoreNode
= rootNode
.get("profileStore");
892 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
893 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
894 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.address());
895 // Not migrating profile key credential here, it was changed to expiring profile key credentials
896 getProfileStore().storeProfileKey(recipientId
, profileEntry
.profileKey());
897 final var profile
= profileEntry
.profile();
898 if (profile
!= null) {
899 final var capabilities
= new HashSet
<Profile
.Capability
>();
900 if (profile
.getCapabilities() != null) {
901 if (profile
.getCapabilities().gv1Migration
) {
902 capabilities
.add(Profile
.Capability
.gv1Migration
);
904 if (profile
.getCapabilities().storage
) {
905 capabilities
.add(Profile
.Capability
.storage
);
908 final var newProfile
= new Profile(profileEntry
.lastUpdateTimestamp(),
909 profile
.getGivenName(),
910 profile
.getFamilyName(),
912 profile
.getAboutEmoji(),
915 profile
.isUnrestrictedUnidentifiedAccess()
916 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
917 : profile
.getUnidentifiedAccess() != null
918 ? Profile
.UnidentifiedAccessMode
.ENABLED
919 : Profile
.UnidentifiedAccessMode
.DISABLED
,
921 getProfileStore().storeProfile(recipientId
, newProfile
);
929 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
930 var threadStoreNode
= rootNode
.get("threadStore");
931 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
932 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
933 // Migrate thread info to group and contact store
934 for (var thread
: threadStore
.getThreads()) {
935 if (thread
.id
== null || thread
.id
.isEmpty()) {
939 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
940 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
941 var contact
= getContactStore().getContact(recipientId
);
942 if (contact
!= null) {
943 getContactStore().storeContact(recipientId
,
944 Contact
.newBuilder(contact
)
945 .withMessageExpirationTime(thread
.messageExpirationTime
)
949 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
950 if (groupInfo
instanceof GroupInfoV1
) {
951 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
952 getGroupStore().updateGroup(groupInfo
);
955 } catch (Exception e
) {
956 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
965 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
966 this.configurationStoreStorage
= storage
;
970 private void save() {
971 synchronized (fileChannel
) {
972 var rootNode
= jsonProcessor
.createObjectNode();
973 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
974 .put("username", number
)
975 .put("serviceEnvironment", serviceEnvironment
== null ?
null : serviceEnvironment
.name())
976 .put("usernameIdentifier", username
)
977 .put("uuid", aci
== null ?
null : aci
.toString())
978 .put("pni", pni
== null ?
null : pni
.toStringWithoutPrefix())
979 .put("sessionId", sessionId
)
980 .put("sessionNumber", sessionNumber
)
981 .put("deviceName", encryptedDeviceName
)
982 .put("deviceId", deviceId
)
983 .put("isMultiDevice", isMultiDevice
)
984 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
985 .put("password", password
)
986 .put("registrationId", aciAccountData
.getLocalRegistrationId())
987 .put("pniRegistrationId", pniAccountData
.getLocalRegistrationId())
988 .put("identityPrivateKey",
990 .encodeToString(aciAccountData
.getIdentityKeyPair().getPrivateKey().serialize()))
993 .encodeToString(aciAccountData
.getIdentityKeyPair().getPublicKey().serialize()))
994 .put("pniIdentityPrivateKey",
995 pniAccountData
.getIdentityKeyPair() == null
997 : Base64
.getEncoder()
998 .encodeToString(pniAccountData
.getIdentityKeyPair()
1001 .put("pniIdentityKey",
1002 pniAccountData
.getIdentityKeyPair() == null
1004 : Base64
.getEncoder()
1005 .encodeToString(pniAccountData
.getIdentityKeyPair()
1008 .put("registrationLockPin", registrationLockPin
)
1009 .put("pinMasterKey",
1010 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
1012 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
1013 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
1014 .put("preKeyIdOffset", aciAccountData
.getPreKeyMetadata().preKeyIdOffset
)
1015 .put("nextSignedPreKeyId", aciAccountData
.getPreKeyMetadata().nextSignedPreKeyId
)
1016 .put("activeSignedPreKeyId", aciAccountData
.getPreKeyMetadata().activeSignedPreKeyId
)
1017 .put("pniPreKeyIdOffset", pniAccountData
.getPreKeyMetadata().preKeyIdOffset
)
1018 .put("pniNextSignedPreKeyId", pniAccountData
.getPreKeyMetadata().nextSignedPreKeyId
)
1019 .put("pniActiveSignedPreKeyId", pniAccountData
.getPreKeyMetadata().activeSignedPreKeyId
)
1020 .put("kyberPreKeyIdOffset", aciAccountData
.getPreKeyMetadata().kyberPreKeyIdOffset
)
1021 .put("activeLastResortKyberPreKeyId",
1022 aciAccountData
.getPreKeyMetadata().activeLastResortKyberPreKeyId
)
1023 .put("pniKyberPreKeyIdOffset", pniAccountData
.getPreKeyMetadata().kyberPreKeyIdOffset
)
1024 .put("pniActiveLastResortKyberPreKeyId",
1025 pniAccountData
.getPreKeyMetadata().activeLastResortKyberPreKeyId
)
1027 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
1028 .put("registered", registered
)
1029 .putPOJO("configurationStore", configurationStoreStorage
);
1031 try (var output
= new ByteArrayOutputStream()) {
1032 // Write to memory first to prevent corrupting the file in case of serialization errors
1033 jsonProcessor
.writeValue(output
, rootNode
);
1034 var input
= new ByteArrayInputStream(output
.toByteArray());
1035 fileChannel
.position(0);
1036 input
.transferTo(Channels
.newOutputStream(fileChannel
));
1037 fileChannel
.truncate(fileChannel
.position());
1038 fileChannel
.force(false);
1040 } catch (Exception e
) {
1041 logger
.error("Error saving file: {}", e
.getMessage(), e
);
1046 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
1047 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
1049 var lock
= fileChannel
.tryLock();
1052 logger
.debug("Config file is in use by another instance.");
1053 throw new IOException("Config file is in use by another instance.");
1055 logger
.info("Config file is in use by another instance, waiting…");
1056 lock
= fileChannel
.lock();
1057 logger
.info("Config file lock acquired.");
1059 final var result
= new Pair
<>(fileChannel
, lock
);
1063 if (fileChannel
!= null) {
1064 fileChannel
.close();
1069 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1070 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1071 preKeyMetadata
.preKeyIdOffset
= getRandomPreKeyIdOffset();
1072 preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
1073 preKeyMetadata
.activeSignedPreKeyId
= -1;
1077 private static int getRandomPreKeyIdOffset() {
1078 return KeyUtils
.getRandomInt(PREKEY_MAXIMUM_ID
);
1081 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1082 final var accountData
= getAccountData(serviceIdType
);
1083 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1084 logger
.debug("Adding {} {} pre keys with offset {}",
1087 preKeyMetadata
.preKeyIdOffset
);
1088 accountData
.signalProtocolStore
.markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1089 for (var record : records
) {
1090 if (preKeyMetadata
.preKeyIdOffset
!= record.getId()) {
1091 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata
.preKeyIdOffset
);
1092 throw new AssertionError("Invalid pre key id");
1094 accountData
.getPreKeyStore().storePreKey(record.getId(), record);
1095 preKeyMetadata
.preKeyIdOffset
= (preKeyMetadata
.preKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1100 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1101 final var accountData
= getAccountData(serviceIdType
);
1102 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1103 logger
.debug("Adding {} signed pre key with offset {}", serviceIdType
, preKeyMetadata
.nextSignedPreKeyId
);
1104 if (preKeyMetadata
.nextSignedPreKeyId
!= record.getId()) {
1105 logger
.error("Invalid signed pre key id {}, expected {}",
1107 preKeyMetadata
.nextSignedPreKeyId
);
1108 throw new AssertionError("Invalid signed pre key id");
1110 accountData
.getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1111 preKeyMetadata
.nextSignedPreKeyId
= (preKeyMetadata
.nextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1112 preKeyMetadata
.activeSignedPreKeyId
= record.getId();
1116 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1117 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1118 preKeyMetadata
.kyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
1119 preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
1123 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1124 final var accountData
= getAccountData(serviceIdType
);
1125 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1126 logger
.debug("Adding {} {} kyber pre keys with offset {}",
1129 preKeyMetadata
.kyberPreKeyIdOffset
);
1130 accountData
.signalProtocolStore
.markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1131 for (var record : records
) {
1132 if (preKeyMetadata
.kyberPreKeyIdOffset
!= record.getId()) {
1133 logger
.error("Invalid kyber pre key id {}, expected {}",
1135 preKeyMetadata
.kyberPreKeyIdOffset
);
1136 throw new AssertionError("Invalid kyber pre key id");
1138 accountData
.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1139 preKeyMetadata
.kyberPreKeyIdOffset
= (preKeyMetadata
.kyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1144 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1145 final var accountData
= getAccountData(serviceIdType
);
1146 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1147 logger
.debug("Adding {} last resort kyber pre key with offset {}",
1149 preKeyMetadata
.kyberPreKeyIdOffset
);
1150 if (preKeyMetadata
.kyberPreKeyIdOffset
!= record.getId()) {
1151 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1153 preKeyMetadata
.kyberPreKeyIdOffset
);
1154 throw new AssertionError("Invalid last resort kyber pre key id");
1156 accountData
.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1157 preKeyMetadata
.activeLastResortKyberPreKeyId
= record.getId();
1158 preKeyMetadata
.kyberPreKeyIdOffset
= (preKeyMetadata
.kyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1162 public int getPreviousStorageVersion() {
1163 return previousStorageVersion
;
1166 public AccountData
getAccountData(ServiceIdType serviceIdType
) {
1167 return switch (serviceIdType
) {
1168 case ACI
-> aciAccountData
;
1169 case PNI
-> pniAccountData
;
1173 public AccountData
getAccountData(ServiceId accountIdentifier
) {
1174 if (accountIdentifier
.equals(aci
)) {
1175 return aciAccountData
;
1176 } else if (accountIdentifier
.equals(pni
)) {
1177 return pniAccountData
;
1179 throw new IllegalArgumentException("No matching account data found for " + accountIdentifier
);
1183 public SignalServiceDataStore
getSignalServiceDataStore() {
1184 return new SignalServiceDataStore() {
1186 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1187 if (accountIdentifier
.equals(aci
)) {
1189 } else if (accountIdentifier
.equals(pni
)) {
1192 throw new IllegalArgumentException("No matching store found for " + accountIdentifier
);
1197 public SignalServiceAccountDataStore
aci() {
1198 return aciAccountData
.getSignalServiceAccountDataStore();
1202 public SignalServiceAccountDataStore
pni() {
1203 return pniAccountData
.getSignalServiceAccountDataStore();
1207 public boolean isMultiDevice() {
1208 return SignalAccount
.this.isMultiDevice();
1213 public IdentityKeyStore
getIdentityKeyStore() {
1214 return getOrCreate(() -> identityKeyStore
,
1215 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(), settings
.trustNewIdentity()));
1218 public GroupStore
getGroupStore() {
1219 return getOrCreate(() -> groupStore
,
1220 () -> groupStore
= new GroupStore(getAccountDatabase(),
1221 getRecipientResolver(),
1222 getRecipientIdCreator()));
1225 public ContactsStore
getContactStore() {
1226 return getRecipientStore();
1229 private RecipientIdCreator
getRecipientIdCreator() {
1230 return recipientId
-> getRecipientStore().create(recipientId
);
1233 public RecipientResolver
getRecipientResolver() {
1234 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1237 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1238 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1241 public RecipientAddressResolver
getRecipientAddressResolver() {
1242 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1245 public RecipientStore
getRecipientStore() {
1246 return getOrCreate(() -> recipientStore
,
1247 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1248 this::getSelfRecipientAddress
,
1249 getAccountDatabase()));
1252 public ProfileStore
getProfileStore() {
1253 return getRecipientStore();
1256 public StickerStore
getStickerStore() {
1257 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1260 public SenderKeyStore
getSenderKeyStore() {
1261 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1264 public ConfigurationStore
getConfigurationStore() {
1265 return configurationStore
;
1268 public MessageCache
getMessageCache() {
1269 return getOrCreate(() -> messageCache
,
1270 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1273 public AccountDatabase
getAccountDatabase() {
1274 return getOrCreate(() -> accountDatabase
, () -> {
1276 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1277 } catch (SQLException e
) {
1278 throw new RuntimeException(e
);
1283 public MessageSendLogStore
getMessageSendLogStore() {
1284 return getOrCreate(() -> messageSendLogStore
,
1285 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1286 settings
.disableMessageSendLog()));
1289 public CredentialsProvider
getCredentialsProvider() {
1290 return new CredentialsProvider() {
1292 public ACI
getAci() {
1297 public PNI
getPni() {
1302 public String
getE164() {
1307 public String
getPassword() {
1312 public int getDeviceId() {
1318 public String
getNumber() {
1322 public void setNumber(final String number
) {
1323 this.number
= number
;
1327 public String
getUsername() {
1331 public void setUsername(final String username
) {
1332 this.username
= username
;
1336 public ServiceEnvironment
getServiceEnvironment() {
1337 return serviceEnvironment
;
1340 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1341 this.serviceEnvironment
= serviceEnvironment
;
1345 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1346 return new AccountAttributes(null,
1347 getLocalRegistrationId(),
1351 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1352 getSelfUnidentifiedAccessKey(),
1353 isUnrestrictedUnidentifiedAccess(),
1354 isDiscoverableByPhoneNumber(),
1355 getAccountCapabilities(),
1356 encryptedDeviceName
,
1357 getLocalPniRegistrationId(),
1358 null); // TODO recoveryPassword?
1361 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1362 return getCapabilities(isPrimaryDevice());
1365 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1366 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aci
: pni
;
1369 public ACI
getAci() {
1373 public void setAci(final ACI aci
) {
1378 public PNI
getPni() {
1382 public void setPni(final PNI updatedPni
) {
1383 if (this.pni
!= null && !this.pni
.equals(updatedPni
)) {
1384 // Clear data for old PNI
1385 identityKeyStore
.deleteIdentity(this.pni
);
1386 clearAllPreKeys(ServiceIdType
.PNI
);
1389 this.pni
= updatedPni
;
1390 trustSelfIdentity(ServiceIdType
.PNI
);
1394 public void setNewPniIdentity(
1395 final IdentityKeyPair pniIdentityKeyPair
,
1396 final SignedPreKeyRecord pniSignedPreKey
,
1397 final KyberPreKeyRecord lastResortKyberPreKey
,
1398 final int localPniRegistrationId
1400 setPniIdentityKeyPair(pniIdentityKeyPair
);
1401 setLocalPniRegistrationId(localPniRegistrationId
);
1403 final var preKeyMetadata
= getAccountData(ServiceIdType
.PNI
).getPreKeyMetadata();
1404 preKeyMetadata
.nextSignedPreKeyId
= pniSignedPreKey
.getId();
1405 addSignedPreKey(ServiceIdType
.PNI
, pniSignedPreKey
);
1406 if (lastResortKyberPreKey
!= null) {
1407 preKeyMetadata
.kyberPreKeyIdOffset
= lastResortKyberPreKey
.getId();
1408 addLastResortKyberPreKey(ServiceIdType
.PNI
, lastResortKyberPreKey
);
1412 public SignalServiceAddress
getSelfAddress() {
1413 return new SignalServiceAddress(aci
, number
);
1416 public RecipientAddress
getSelfRecipientAddress() {
1417 return new RecipientAddress(aci
, pni
, number
, username
);
1420 public RecipientId
getSelfRecipientId() {
1421 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1424 public String
getSessionId(final String forNumber
) {
1425 if (!forNumber
.equals(sessionNumber
)) {
1431 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1432 this.sessionNumber
= sessionNumber
;
1433 this.sessionId
= sessionId
;
1437 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1438 this.encryptedDeviceName
= encryptedDeviceName
;
1442 public int getDeviceId() {
1446 public boolean isPrimaryDevice() {
1447 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1450 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1451 return getAccountData(serviceIdType
).getIdentityKeyPair();
1454 public IdentityKeyPair
getAciIdentityKeyPair() {
1455 return aciAccountData
.getIdentityKeyPair();
1458 public IdentityKeyPair
getPniIdentityKeyPair() {
1459 return pniAccountData
.getIdentityKeyPair();
1462 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1463 pniAccountData
.setIdentityKeyPair(identityKeyPair
);
1464 trustSelfIdentity(ServiceIdType
.PNI
);
1468 public int getLocalRegistrationId() {
1469 return aciAccountData
.getLocalRegistrationId();
1472 public int getLocalPniRegistrationId() {
1473 return pniAccountData
.getLocalRegistrationId();
1476 public void setLocalPniRegistrationId(final int localPniRegistrationId
) {
1477 pniAccountData
.setLocalRegistrationId(localPniRegistrationId
);
1481 public String
getPassword() {
1485 private void setPassword(final String password
) {
1486 this.password
= password
;
1490 public void setRegistrationLockPin(final String registrationLockPin
) {
1491 this.registrationLockPin
= registrationLockPin
;
1495 public String
getRegistrationLockPin() {
1496 return registrationLockPin
;
1499 public String
getRegistrationLock() {
1500 final var masterKey
= getPinBackedMasterKey();
1501 if (masterKey
== null) {
1504 return masterKey
.deriveRegistrationLock();
1507 public MasterKey
getPinBackedMasterKey() {
1508 if (registrationLockPin
== null) {
1511 return pinMasterKey
;
1514 public MasterKey
getOrCreatePinMasterKey() {
1515 if (pinMasterKey
== null) {
1516 pinMasterKey
= KeyUtils
.createMasterKey();
1519 return pinMasterKey
;
1522 public StorageKey
getStorageKey() {
1523 if (pinMasterKey
!= null) {
1524 return pinMasterKey
.deriveStorageServiceKey();
1529 public StorageKey
getOrCreateStorageKey() {
1530 if (isPrimaryDevice()) {
1531 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1536 public void setStorageKey(final StorageKey storageKey
) {
1537 if (storageKey
.equals(this.storageKey
)) {
1540 this.storageKey
= storageKey
;
1544 public long getStorageManifestVersion() {
1545 return this.storageManifestVersion
;
1548 public void setStorageManifestVersion(final long storageManifestVersion
) {
1549 if (storageManifestVersion
== this.storageManifestVersion
) {
1552 this.storageManifestVersion
= storageManifestVersion
;
1556 public Optional
<SignalStorageManifest
> getStorageManifest() {
1557 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1558 if (!storageManifestFile
.exists()) {
1559 return Optional
.empty();
1561 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1562 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1563 } catch (IOException e
) {
1564 logger
.warn("Failed to read local storage manifest.", e
);
1565 return Optional
.empty();
1569 public void setStorageManifest(SignalStorageManifest manifest
) {
1570 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1571 if (manifest
== null) {
1572 if (storageManifestFile
.exists()) {
1574 Files
.delete(storageManifestFile
.toPath());
1575 } catch (IOException e
) {
1576 logger
.error("Failed to delete local storage manifest.", e
);
1582 final var manifestBytes
= manifest
.serialize();
1583 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1584 outputStream
.write(manifestBytes
);
1585 } catch (IOException e
) {
1586 logger
.error("Failed to store local storage manifest.", e
);
1590 public ProfileKey
getProfileKey() {
1594 public void setProfileKey(final ProfileKey profileKey
) {
1595 if (profileKey
.equals(this.profileKey
)) {
1598 this.profileKey
= profileKey
;
1600 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1603 public byte[] getSelfUnidentifiedAccessKey() {
1604 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1607 public int getPreKeyIdOffset(ServiceIdType serviceIdType
) {
1608 return getAccountData(serviceIdType
).getPreKeyMetadata().preKeyIdOffset
;
1611 public int getNextSignedPreKeyId(ServiceIdType serviceIdType
) {
1612 return getAccountData(serviceIdType
).getPreKeyMetadata().nextSignedPreKeyId
;
1615 public int getKyberPreKeyIdOffset(ServiceIdType serviceIdType
) {
1616 return getAccountData(serviceIdType
).getPreKeyMetadata().kyberPreKeyIdOffset
;
1619 public boolean isRegistered() {
1623 public void setRegistered(final boolean registered
) {
1624 this.registered
= registered
;
1628 public boolean isMultiDevice() {
1629 return isMultiDevice
;
1632 public void setMultiDevice(final boolean multiDevice
) {
1633 if (isMultiDevice
== multiDevice
) {
1636 isMultiDevice
= multiDevice
;
1640 public long getLastReceiveTimestamp() {
1641 return lastReceiveTimestamp
;
1644 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1645 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1649 public boolean isUnrestrictedUnidentifiedAccess() {
1650 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1651 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1654 public boolean isDiscoverableByPhoneNumber() {
1655 final var phoneNumberUnlisted
= configurationStore
.getPhoneNumberUnlisted();
1656 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1659 public void finishRegistration(
1662 final MasterKey masterKey
,
1664 final PreKeyCollection aciPreKeys
,
1665 final PreKeyCollection pniPreKeys
1667 this.pinMasterKey
= masterKey
;
1668 this.storageManifestVersion
= -1;
1669 this.setStorageManifest(null);
1670 this.storageKey
= null;
1671 this.encryptedDeviceName
= null;
1672 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1673 this.isMultiDevice
= false;
1674 this.registered
= true;
1677 this.registrationLockPin
= pin
;
1678 this.lastReceiveTimestamp
= 0;
1681 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
1682 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
1683 aciAccountData
.getSessionStore().archiveAllSessions();
1684 pniAccountData
.getSessionStore().archiveAllSessions();
1685 getSenderKeyStore().deleteAll();
1686 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1687 trustSelfIdentity(ServiceIdType
.ACI
);
1688 trustSelfIdentity(ServiceIdType
.PNI
);
1691 private void trustSelfIdentity(ServiceIdType serviceIdType
) {
1692 final var accountData
= getAccountData(serviceIdType
);
1693 final var serviceId
= accountData
.getServiceId();
1694 final var identityKeyPair
= accountData
.getIdentityKeyPair();
1695 if (serviceId
== null || identityKeyPair
== null) {
1698 final var publicKey
= identityKeyPair
.getPublicKey();
1699 getIdentityKeyStore().saveIdentity(serviceId
, publicKey
);
1700 getIdentityKeyStore().setIdentityTrustLevel(serviceId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1703 public void deleteAccountData() throws IOException
{
1705 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1706 .sorted(Comparator
.reverseOrder())) {
1707 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1708 Files
.delete(file
.next());
1711 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1715 public void close() {
1716 synchronized (fileChannel
) {
1717 if (accountDatabase
!= null) {
1719 accountDatabase
.close();
1720 } catch (SQLException e
) {
1721 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1724 if (messageSendLogStore
!= null) {
1725 messageSendLogStore
.close();
1730 } catch (ClosedChannelException ignored
) {
1732 fileChannel
.close();
1733 } catch (IOException e
) {
1734 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1739 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1740 var value
= supplier
.get();
1741 if (value
!= null) {
1745 synchronized (LOCK
) {
1746 value
= supplier
.get();
1747 if (value
!= null) {
1751 return supplier
.get();
1755 private interface Callable
{
1760 public static class PreKeyMetadata
{
1762 private int preKeyIdOffset
= 1;
1763 private int nextSignedPreKeyId
= 1;
1764 private int activeSignedPreKeyId
= -1;
1765 private int kyberPreKeyIdOffset
= 1;
1766 private int activeLastResortKyberPreKeyId
= -1;
1768 public int getPreKeyIdOffset() {
1769 return preKeyIdOffset
;
1772 public int getNextSignedPreKeyId() {
1773 return nextSignedPreKeyId
;
1776 public int getActiveSignedPreKeyId() {
1777 return activeSignedPreKeyId
;
1780 public int getKyberPreKeyIdOffset() {
1781 return kyberPreKeyIdOffset
;
1784 public int getActiveLastResortKyberPreKeyId() {
1785 return activeLastResortKyberPreKeyId
;
1789 public class AccountData
{
1791 private final ServiceIdType serviceIdType
;
1792 private IdentityKeyPair identityKeyPair
;
1793 private int localRegistrationId
;
1794 private final PreKeyMetadata preKeyMetadata
= new PreKeyMetadata();
1796 private SignalProtocolStore signalProtocolStore
;
1797 private PreKeyStore preKeyStore
;
1798 private SignedPreKeyStore signedPreKeyStore
;
1799 private KyberPreKeyStore kyberPreKeyStore
;
1800 private SessionStore sessionStore
;
1801 private SignalIdentityKeyStore identityKeyStore
;
1803 public AccountData(final ServiceIdType serviceIdType
) {
1804 this.serviceIdType
= serviceIdType
;
1807 public ServiceId
getServiceId() {
1808 return getAccountId(serviceIdType
);
1811 public IdentityKeyPair
getIdentityKeyPair() {
1812 return identityKeyPair
;
1815 private void setIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1816 this.identityKeyPair
= identityKeyPair
;
1819 public int getLocalRegistrationId() {
1820 return localRegistrationId
;
1823 private void setLocalRegistrationId(final int localRegistrationId
) {
1824 this.localRegistrationId
= localRegistrationId
;
1825 this.identityKeyStore
= null;
1828 public PreKeyMetadata
getPreKeyMetadata() {
1829 return preKeyMetadata
;
1832 private SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1833 return getOrCreate(() -> signalProtocolStore
,
1834 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
1835 getSignedPreKeyStore(),
1836 getKyberPreKeyStore(),
1838 getIdentityKeyStore(),
1839 getSenderKeyStore(),
1840 SignalAccount
.this::isMultiDevice
));
1843 public PreKeyStore
getPreKeyStore() {
1844 return getOrCreate(() -> preKeyStore
,
1845 () -> preKeyStore
= new PreKeyStore(getAccountDatabase(), serviceIdType
));
1848 public SignedPreKeyStore
getSignedPreKeyStore() {
1849 return getOrCreate(() -> signedPreKeyStore
,
1850 () -> signedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), serviceIdType
));
1853 public KyberPreKeyStore
getKyberPreKeyStore() {
1854 return getOrCreate(() -> kyberPreKeyStore
,
1855 () -> kyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), serviceIdType
));
1858 public SessionStore
getSessionStore() {
1859 return getOrCreate(() -> sessionStore
,
1860 () -> sessionStore
= new SessionStore(getAccountDatabase(), serviceIdType
));
1863 public SignalIdentityKeyStore
getIdentityKeyStore() {
1864 return getOrCreate(() -> identityKeyStore
,
1865 () -> identityKeyStore
= new SignalIdentityKeyStore(() -> identityKeyPair
,
1866 localRegistrationId
,
1867 SignalAccount
.this.getIdentityKeyStore()));