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
.crypto
.UnidentifiedAccess
;
68 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
69 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
70 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
71 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
72 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
73 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
74 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
75 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
76 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
77 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
79 import java
.io
.ByteArrayInputStream
;
80 import java
.io
.ByteArrayOutputStream
;
81 import java
.io
.Closeable
;
83 import java
.io
.FileInputStream
;
84 import java
.io
.FileOutputStream
;
85 import java
.io
.IOException
;
86 import java
.io
.RandomAccessFile
;
87 import java
.nio
.channels
.Channels
;
88 import java
.nio
.channels
.ClosedChannelException
;
89 import java
.nio
.channels
.FileChannel
;
90 import java
.nio
.channels
.FileLock
;
91 import java
.nio
.file
.Files
;
92 import java
.security
.SecureRandom
;
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 int aciPreKeyIdOffset
= 1;
140 private int aciNextSignedPreKeyId
= 1;
141 private int pniPreKeyIdOffset
= 1;
142 private int pniNextSignedPreKeyId
= 1;
143 private int aciKyberPreKeyIdOffset
= 1;
144 private int aciActiveLastResortKyberPreKeyId
= -1;
145 private int pniKyberPreKeyIdOffset
= 1;
146 private int pniActiveLastResortKyberPreKeyId
= -1;
147 private IdentityKeyPair aciIdentityKeyPair
;
148 private IdentityKeyPair pniIdentityKeyPair
;
149 private int localRegistrationId
;
150 private int localPniRegistrationId
;
151 private Settings settings
;
152 private long lastReceiveTimestamp
= 0;
154 private boolean registered
= false;
156 private SignalProtocolStore aciSignalProtocolStore
;
157 private SignalProtocolStore pniSignalProtocolStore
;
158 private PreKeyStore aciPreKeyStore
;
159 private SignedPreKeyStore aciSignedPreKeyStore
;
160 private KyberPreKeyStore aciKyberPreKeyStore
;
161 private PreKeyStore pniPreKeyStore
;
162 private SignedPreKeyStore pniSignedPreKeyStore
;
163 private KyberPreKeyStore pniKyberPreKeyStore
;
164 private SessionStore aciSessionStore
;
165 private SessionStore pniSessionStore
;
166 private IdentityKeyStore identityKeyStore
;
167 private SignalIdentityKeyStore aciIdentityKeyStore
;
168 private SignalIdentityKeyStore pniIdentityKeyStore
;
169 private SenderKeyStore senderKeyStore
;
170 private GroupStore groupStore
;
171 private RecipientStore recipientStore
;
172 private StickerStore stickerStore
;
173 private ConfigurationStore configurationStore
;
174 private ConfigurationStore
.Storage configurationStoreStorage
;
176 private MessageCache messageCache
;
177 private MessageSendLogStore messageSendLogStore
;
179 private AccountDatabase accountDatabase
;
181 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
182 this.fileChannel
= fileChannel
;
186 public static SignalAccount
load(
187 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
188 ) throws IOException
{
189 logger
.trace("Opening account file");
190 final var fileName
= getFileName(dataPath
, accountPath
);
191 final var pair
= openFileChannel(fileName
, waitForLock
);
193 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
194 logger
.trace("Loading account file");
195 signalAccount
.load(dataPath
, accountPath
, settings
);
196 logger
.trace("Migrating legacy parts of account file");
197 signalAccount
.migrateLegacyConfigs();
199 return signalAccount
;
200 } catch (Throwable e
) {
201 pair
.second().close();
202 pair
.first().close();
207 public static SignalAccount
create(
211 ServiceEnvironment serviceEnvironment
,
212 IdentityKeyPair aciIdentityKey
,
213 IdentityKeyPair pniIdentityKey
,
215 int pniRegistrationId
,
216 ProfileKey profileKey
,
217 final Settings settings
218 ) throws IOException
{
219 IOUtils
.createPrivateDirectories(dataPath
);
220 var fileName
= getFileName(dataPath
, accountPath
);
221 if (!fileName
.exists()) {
222 IOUtils
.createPrivateFile(fileName
);
225 final var pair
= openFileChannel(fileName
, true);
226 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
228 signalAccount
.accountPath
= accountPath
;
229 signalAccount
.number
= number
;
230 signalAccount
.serviceEnvironment
= serviceEnvironment
;
231 signalAccount
.profileKey
= profileKey
;
233 signalAccount
.dataPath
= dataPath
;
234 signalAccount
.aciIdentityKeyPair
= aciIdentityKey
;
235 signalAccount
.pniIdentityKeyPair
= pniIdentityKey
;
236 signalAccount
.localRegistrationId
= registrationId
;
237 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
238 signalAccount
.settings
= settings
;
239 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
241 signalAccount
.registered
= false;
243 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
244 signalAccount
.migrateLegacyConfigs();
245 signalAccount
.save();
247 return signalAccount
;
250 public static SignalAccount
createOrUpdateLinkedAccount(
254 ServiceEnvironment serviceEnvironment
,
258 String encryptedDeviceName
,
260 IdentityKeyPair aciIdentityKey
,
261 IdentityKeyPair pniIdentityKey
,
263 int pniRegistrationId
,
264 ProfileKey profileKey
,
265 final Settings settings
266 ) throws IOException
{
267 IOUtils
.createPrivateDirectories(dataPath
);
268 var fileName
= getFileName(dataPath
, accountPath
);
269 if (!fileName
.exists()) {
270 return createLinkedAccount(dataPath
,
287 final var signalAccount
= load(dataPath
, accountPath
, true, settings
);
288 signalAccount
.setProvisioningData(number
,
297 signalAccount
.getRecipientTrustedResolver()
298 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
299 signalAccount
.getAciSessionStore().archiveAllSessions();
300 signalAccount
.getPniSessionStore().archiveAllSessions();
301 signalAccount
.getSenderKeyStore().deleteAll();
302 signalAccount
.clearAllPreKeys();
303 return signalAccount
;
306 public void initDatabase() {
307 getAccountDatabase();
310 private void clearAllPreKeys() {
311 resetPreKeyOffsets(ServiceIdType
.ACI
);
312 resetPreKeyOffsets(ServiceIdType
.PNI
);
313 resetKyberPreKeyOffsets(ServiceIdType
.ACI
);
314 resetKyberPreKeyOffsets(ServiceIdType
.PNI
);
315 this.getAciPreKeyStore().removeAllPreKeys();
316 this.getAciSignedPreKeyStore().removeAllSignedPreKeys();
317 this.getAciKyberPreKeyStore().removeAllKyberPreKeys();
318 this.getPniPreKeyStore().removeAllPreKeys();
319 this.getPniSignedPreKeyStore().removeAllSignedPreKeys();
320 this.getPniKyberPreKeyStore().removeAllKyberPreKeys();
324 private static SignalAccount
createLinkedAccount(
328 ServiceEnvironment serviceEnvironment
,
332 String encryptedDeviceName
,
334 IdentityKeyPair aciIdentityKey
,
335 IdentityKeyPair pniIdentityKey
,
337 int pniRegistrationId
,
338 ProfileKey profileKey
,
339 final Settings settings
340 ) throws IOException
{
341 var fileName
= getFileName(dataPath
, accountPath
);
342 IOUtils
.createPrivateFile(fileName
);
344 final var pair
= openFileChannel(fileName
, true);
345 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
347 signalAccount
.dataPath
= dataPath
;
348 signalAccount
.accountPath
= accountPath
;
349 signalAccount
.serviceEnvironment
= serviceEnvironment
;
350 signalAccount
.localRegistrationId
= registrationId
;
351 signalAccount
.localPniRegistrationId
= pniRegistrationId
;
352 signalAccount
.settings
= settings
;
353 signalAccount
.setProvisioningData(number
,
363 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
365 signalAccount
.getRecipientTrustedResolver()
366 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
367 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
368 signalAccount
.migrateLegacyConfigs();
369 signalAccount
.clearAllPreKeys();
370 signalAccount
.save();
372 return signalAccount
;
375 private void setProvisioningData(
379 final String password
,
380 final String encryptedDeviceName
,
382 final IdentityKeyPair aciIdentity
,
383 final IdentityKeyPair pniIdentity
,
384 final ProfileKey profileKey
386 this.number
= number
;
389 this.password
= password
;
390 this.profileKey
= profileKey
;
391 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
392 this.encryptedDeviceName
= encryptedDeviceName
;
393 this.deviceId
= deviceId
;
394 this.aciIdentityKeyPair
= aciIdentity
;
395 this.pniIdentityKeyPair
= pniIdentity
;
396 this.registered
= true;
397 this.isMultiDevice
= true;
398 this.lastReceiveTimestamp
= 0;
399 this.pinMasterKey
= null;
400 this.storageManifestVersion
= -1;
401 this.setStorageManifest(null);
402 this.storageKey
= null;
403 final var aciPublicKey
= getAciIdentityKeyPair().getPublicKey();
404 getIdentityKeyStore().saveIdentity(getAci(), aciPublicKey
);
405 getIdentityKeyStore().setIdentityTrustLevel(getAci(), aciPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
406 if (getPniIdentityKeyPair() != null) {
407 final var pniPublicKey
= getPniIdentityKeyPair().getPublicKey();
408 getIdentityKeyStore().saveIdentity(getPni(), pniPublicKey
);
409 getIdentityKeyStore().setIdentityTrustLevel(getPni(), pniPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
413 private void migrateLegacyConfigs() {
414 if (getPassword() == null) {
415 setPassword(KeyUtils
.createPassword());
418 if (getProfileKey() == null) {
419 // Old config file, creating new profile key
420 setProfileKey(KeyUtils
.createProfileKey());
422 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
423 if (isPrimaryDevice() && getPniIdentityKeyPair() == null && getPni() != null) {
424 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
428 private void mergeRecipients(
429 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
430 ) throws SQLException
{
431 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
432 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
435 public void removeRecipient(final RecipientId recipientId
) {
436 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
437 getRecipientStore().deleteRecipientData(recipientId
);
438 getMessageCache().deleteMessages(recipientId
);
439 if (recipientAddress
.serviceId().isPresent()) {
440 final var serviceId
= recipientAddress
.serviceId().get();
441 getAciSessionStore().deleteAllSessions(serviceId
);
442 getPniSessionStore().deleteAllSessions(serviceId
);
443 getIdentityKeyStore().deleteIdentity(serviceId
);
444 getSenderKeyStore().deleteAll(serviceId
);
448 public static File
getFileName(File dataPath
, String account
) {
449 return new File(dataPath
, account
);
452 private static File
getUserPath(final File dataPath
, final String account
) {
453 final var path
= new File(dataPath
, account
+ ".d");
455 IOUtils
.createPrivateDirectories(path
);
456 } catch (IOException e
) {
457 throw new AssertionError("Failed to create user path", e
);
462 private static File
getMessageCachePath(File dataPath
, String account
) {
463 return new File(getUserPath(dataPath
, account
), "msg-cache");
466 private static File
getGroupCachePath(File dataPath
, String account
) {
467 return new File(getUserPath(dataPath
, account
), "group-cache");
470 private static File
getAciPreKeysPath(File dataPath
, String account
) {
471 return new File(getUserPath(dataPath
, account
), "pre-keys");
474 private static File
getAciSignedPreKeysPath(File dataPath
, String account
) {
475 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
478 private static File
getPniPreKeysPath(File dataPath
, String account
) {
479 return new File(getUserPath(dataPath
, account
), "pre-keys-pni");
482 private static File
getPniSignedPreKeysPath(File dataPath
, String account
) {
483 return new File(getUserPath(dataPath
, account
), "signed-pre-keys-pni");
486 private static File
getIdentitiesPath(File dataPath
, String account
) {
487 return new File(getUserPath(dataPath
, account
), "identities");
490 private static File
getSessionsPath(File dataPath
, String account
) {
491 return new File(getUserPath(dataPath
, account
), "sessions");
494 private static File
getSenderKeysPath(File dataPath
, String account
) {
495 return new File(getUserPath(dataPath
, account
), "sender-keys");
498 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
499 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
502 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
503 return new File(getUserPath(dataPath
, account
), "recipients-store");
506 private static File
getStorageManifestFile(File dataPath
, String account
) {
507 return new File(getUserPath(dataPath
, account
), "storage-manifest");
510 private static File
getDatabaseFile(File dataPath
, String account
) {
511 return new File(getUserPath(dataPath
, account
), "account.db");
514 public static boolean accountFileExists(File dataPath
, String account
) {
515 if (account
== null) {
518 var f
= getFileName(dataPath
, account
);
519 return !(!f
.exists() || f
.isDirectory());
523 File dataPath
, String accountPath
, final Settings settings
524 ) throws IOException
{
525 this.dataPath
= dataPath
;
526 this.accountPath
= accountPath
;
527 this.settings
= settings
;
528 final JsonNode rootNode
;
529 synchronized (fileChannel
) {
530 fileChannel
.position(0);
531 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
534 var migratedLegacyConfig
= false;
536 if (rootNode
.hasNonNull("version")) {
537 var accountVersion
= rootNode
.get("version").asInt(1);
538 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
539 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
540 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
541 throw new IOException("Config file was created by a no longer supported older version: "
544 previousStorageVersion
= accountVersion
;
545 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
546 migratedLegacyConfig
= true;
550 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
551 if (rootNode
.hasNonNull("password")) {
552 password
= rootNode
.get("password").asText();
554 if (rootNode
.hasNonNull("serviceEnvironment")) {
555 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
557 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
558 if (rootNode
.hasNonNull("usernameIdentifier")) {
559 username
= rootNode
.get("usernameIdentifier").asText();
561 if (rootNode
.hasNonNull("uuid")) {
563 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
564 } catch (IllegalArgumentException e
) {
565 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
568 if (rootNode
.hasNonNull("pni")) {
570 pni
= PNI
.parseOrThrow(rootNode
.get("pni").asText());
571 } catch (IllegalArgumentException e
) {
572 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
575 if (rootNode
.hasNonNull("sessionId")) {
576 sessionId
= rootNode
.get("sessionId").asText();
578 if (rootNode
.hasNonNull("sessionNumber")) {
579 sessionNumber
= rootNode
.get("sessionNumber").asText();
581 if (rootNode
.hasNonNull("deviceName")) {
582 encryptedDeviceName
= rootNode
.get("deviceName").asText();
584 if (rootNode
.hasNonNull("deviceId")) {
585 deviceId
= rootNode
.get("deviceId").asInt();
587 if (rootNode
.hasNonNull("isMultiDevice")) {
588 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
590 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
591 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
593 int registrationId
= 0;
594 if (rootNode
.hasNonNull("registrationId")) {
595 registrationId
= rootNode
.get("registrationId").asInt();
597 if (rootNode
.hasNonNull("pniRegistrationId")) {
598 localPniRegistrationId
= rootNode
.get("pniRegistrationId").asInt();
600 localPniRegistrationId
= KeyHelper
.generateRegistrationId(false);
602 IdentityKeyPair aciIdentityKeyPair
= null;
603 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
604 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
605 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
606 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
608 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
609 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
610 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
611 pniIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
614 if (rootNode
.hasNonNull("registrationLockPin")) {
615 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
617 if (rootNode
.hasNonNull("pinMasterKey")) {
618 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
620 if (rootNode
.hasNonNull("storageKey")) {
621 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
623 if (rootNode
.hasNonNull("storageManifestVersion")) {
624 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
626 if (rootNode
.hasNonNull("preKeyIdOffset")) {
627 aciPreKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
629 aciPreKeyIdOffset
= getRandomPreKeyIdOffset();
631 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
632 aciNextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
634 aciNextSignedPreKeyId
= getRandomPreKeyIdOffset();
636 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
637 pniPreKeyIdOffset
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
639 pniPreKeyIdOffset
= getRandomPreKeyIdOffset();
641 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
642 pniNextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
644 pniNextSignedPreKeyId
= getRandomPreKeyIdOffset();
646 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
647 aciKyberPreKeyIdOffset
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
649 aciKyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
651 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
652 aciActiveLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId").asInt(-1);
654 aciActiveLastResortKyberPreKeyId
= -1;
656 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
657 pniKyberPreKeyIdOffset
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
659 pniKyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
661 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
662 pniActiveLastResortKyberPreKeyId
= rootNode
.get("pniActiveLastResortKyberPreKeyId").asInt(-1);
664 pniActiveLastResortKyberPreKeyId
= -1;
666 if (rootNode
.hasNonNull("profileKey")) {
668 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
669 } catch (InvalidInputException e
) {
670 throw new IOException(
671 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
676 if (previousStorageVersion
< 5) {
677 final var legacyRecipientsStoreFile
= getRecipientsStoreFile(dataPath
, accountPath
);
678 if (legacyRecipientsStoreFile
.exists()) {
679 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
680 // Ensure our profile key is stored in profile store
681 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
682 migratedLegacyConfig
= true;
685 if (previousStorageVersion
< 6) {
686 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
688 final var legacyAciPreKeysPath
= getAciPreKeysPath(dataPath
, accountPath
);
689 if (legacyAciPreKeysPath
.exists()) {
690 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, getAciPreKeyStore());
691 migratedLegacyConfig
= true;
693 final var legacyPniPreKeysPath
= getPniPreKeysPath(dataPath
, accountPath
);
694 if (legacyPniPreKeysPath
.exists()) {
695 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, getPniPreKeyStore());
696 migratedLegacyConfig
= true;
698 final var legacyAciSignedPreKeysPath
= getAciSignedPreKeysPath(dataPath
, accountPath
);
699 if (legacyAciSignedPreKeysPath
.exists()) {
700 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, getAciSignedPreKeyStore());
701 migratedLegacyConfig
= true;
703 final var legacyPniSignedPreKeysPath
= getPniSignedPreKeysPath(dataPath
, accountPath
);
704 if (legacyPniSignedPreKeysPath
.exists()) {
705 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, getPniSignedPreKeyStore());
706 migratedLegacyConfig
= true;
708 final var legacySessionsPath
= getSessionsPath(dataPath
, accountPath
);
709 if (legacySessionsPath
.exists()) {
710 LegacySessionStore
.migrate(legacySessionsPath
,
711 getRecipientResolver(),
712 getRecipientAddressResolver(),
713 getAciSessionStore());
714 migratedLegacyConfig
= true;
716 final var legacyIdentitiesPath
= getIdentitiesPath(dataPath
, accountPath
);
717 if (legacyIdentitiesPath
.exists()) {
718 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
719 getRecipientResolver(),
720 getRecipientAddressResolver(),
721 getIdentityKeyStore());
722 migratedLegacyConfig
= true;
724 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
725 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
726 LegacyJsonSignalProtocolStore
.class)
728 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
729 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
730 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
731 migratedLegacyConfig
= true;
734 this.aciIdentityKeyPair
= aciIdentityKeyPair
;
735 this.localRegistrationId
= registrationId
;
737 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
739 final var legacySenderKeysPath
= getSenderKeysPath(dataPath
, accountPath
);
740 if (legacySenderKeysPath
.exists()) {
741 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
742 getRecipientResolver(),
743 getRecipientAddressResolver(),
744 getSenderKeyStore());
745 migratedLegacyConfig
= true;
747 final var legacySenderKeysSharedPath
= getSharedSenderKeysFile(dataPath
, accountPath
);
748 if (legacySenderKeysSharedPath
.exists()) {
749 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
750 getRecipientResolver(),
751 getRecipientAddressResolver(),
752 getSenderKeyStore());
753 migratedLegacyConfig
= true;
755 if (rootNode
.hasNonNull("groupStore")) {
756 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
757 LegacyGroupStore
.Storage
.class);
758 LegacyGroupStore
.migrate(groupStoreStorage
,
759 getGroupCachePath(dataPath
, accountPath
),
760 getRecipientResolver(),
762 migratedLegacyConfig
= true;
765 if (rootNode
.hasNonNull("stickerStore")) {
766 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
767 LegacyStickerStore
.Storage
.class);
768 LegacyStickerStore
.migrate(storage
, getStickerStore());
769 migratedLegacyConfig
= true;
772 if (rootNode
.hasNonNull("configurationStore")) {
773 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
774 ConfigurationStore
.Storage
.class);
775 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
776 this::saveConfigurationStore
);
778 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
781 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
783 if (migratedLegacyConfig
) {
788 private boolean loadLegacyStores(
789 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
791 var migrated
= false;
792 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
793 if (legacyRecipientStoreNode
!= null) {
794 logger
.debug("Migrating legacy recipient store.");
795 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
796 if (legacyRecipientStore
!= null) {
797 legacyRecipientStore
.getAddresses()
798 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
800 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
804 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
805 logger
.debug("Migrating legacy pre key store.");
806 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
808 getAciPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
809 } catch (InvalidMessageException e
) {
810 logger
.warn("Failed to migrate pre key, ignoring", e
);
816 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
817 logger
.debug("Migrating legacy signed pre key store.");
818 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
820 getAciSignedPreKeyStore().storeSignedPreKey(entry
.getKey(),
821 new SignedPreKeyRecord(entry
.getValue()));
822 } catch (InvalidMessageException e
) {
823 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
829 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
830 logger
.debug("Migrating legacy session store.");
831 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
833 getAciSessionStore().storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
834 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
835 } catch (Exception e
) {
836 logger
.warn("Failed to migrate session, ignoring", e
);
842 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
843 logger
.debug("Migrating legacy identity session store.");
844 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
845 if (identity
.getAddress().serviceId().isEmpty()) {
848 final var serviceId
= identity
.getAddress().serviceId().get();
849 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
850 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
851 identity
.getIdentityKey(),
852 identity
.getTrustLevel());
857 if (rootNode
.hasNonNull("contactStore")) {
858 logger
.debug("Migrating legacy contact store.");
859 final var contactStoreNode
= rootNode
.get("contactStore");
860 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
861 for (var contact
: contactStore
.getContacts()) {
862 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
863 getContactStore().storeContact(recipientId
,
864 new Contact(contact
.name
,
867 contact
.messageExpirationTime
,
872 // Store profile keys only in profile store
873 var profileKeyString
= contact
.profileKey
;
874 if (profileKeyString
!= null) {
875 final ProfileKey profileKey
;
877 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
878 getProfileStore().storeProfileKey(recipientId
, profileKey
);
879 } catch (InvalidInputException e
) {
880 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
887 if (rootNode
.hasNonNull("profileStore")) {
888 logger
.debug("Migrating legacy profile store.");
889 var profileStoreNode
= rootNode
.get("profileStore");
890 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
891 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
892 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.getAddress());
893 // Not migrating profile key credential here, it was changed to expiring profile key credentials
894 getProfileStore().storeProfileKey(recipientId
, profileEntry
.getProfileKey());
895 final var profile
= profileEntry
.getProfile();
896 if (profile
!= null) {
897 final var capabilities
= new HashSet
<Profile
.Capability
>();
898 if (profile
.getCapabilities() != null) {
899 if (profile
.getCapabilities().gv1Migration
) {
900 capabilities
.add(Profile
.Capability
.gv1Migration
);
902 if (profile
.getCapabilities().storage
) {
903 capabilities
.add(Profile
.Capability
.storage
);
906 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
907 profile
.getGivenName(),
908 profile
.getFamilyName(),
910 profile
.getAboutEmoji(),
913 profile
.isUnrestrictedUnidentifiedAccess()
914 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
915 : profile
.getUnidentifiedAccess() != null
916 ? Profile
.UnidentifiedAccessMode
.ENABLED
917 : Profile
.UnidentifiedAccessMode
.DISABLED
,
919 getProfileStore().storeProfile(recipientId
, newProfile
);
927 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
928 var threadStoreNode
= rootNode
.get("threadStore");
929 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
930 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
931 // Migrate thread info to group and contact store
932 for (var thread
: threadStore
.getThreads()) {
933 if (thread
.id
== null || thread
.id
.isEmpty()) {
937 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
938 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
939 var contact
= getContactStore().getContact(recipientId
);
940 if (contact
!= null) {
941 getContactStore().storeContact(recipientId
,
942 Contact
.newBuilder(contact
)
943 .withMessageExpirationTime(thread
.messageExpirationTime
)
947 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
948 if (groupInfo
instanceof GroupInfoV1
) {
949 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
950 getGroupStore().updateGroup(groupInfo
);
953 } catch (Exception e
) {
954 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
963 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
964 this.configurationStoreStorage
= storage
;
968 private void save() {
969 synchronized (fileChannel
) {
970 var rootNode
= jsonProcessor
.createObjectNode();
971 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
972 .put("username", number
)
973 .put("serviceEnvironment", serviceEnvironment
== null ?
null : serviceEnvironment
.name())
974 .put("usernameIdentifier", username
)
975 .put("uuid", aci
== null ?
null : aci
.toString())
976 .put("pni", pni
== null ?
null : pni
.toString())
977 .put("sessionId", sessionId
)
978 .put("sessionNumber", sessionNumber
)
979 .put("deviceName", encryptedDeviceName
)
980 .put("deviceId", deviceId
)
981 .put("isMultiDevice", isMultiDevice
)
982 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
983 .put("password", password
)
984 .put("registrationId", localRegistrationId
)
985 .put("pniRegistrationId", localPniRegistrationId
)
986 .put("identityPrivateKey",
987 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPrivateKey().serialize()))
989 Base64
.getEncoder().encodeToString(aciIdentityKeyPair
.getPublicKey().serialize()))
990 .put("pniIdentityPrivateKey",
991 pniIdentityKeyPair
== null
993 : Base64
.getEncoder()
994 .encodeToString(pniIdentityKeyPair
.getPrivateKey().serialize()))
995 .put("pniIdentityKey",
996 pniIdentityKeyPair
== null
998 : Base64
.getEncoder().encodeToString(pniIdentityKeyPair
.getPublicKey().serialize()))
999 .put("registrationLockPin", registrationLockPin
)
1000 .put("pinMasterKey",
1001 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
1003 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
1004 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
1005 .put("preKeyIdOffset", aciPreKeyIdOffset
)
1006 .put("nextSignedPreKeyId", aciNextSignedPreKeyId
)
1007 .put("pniPreKeyIdOffset", pniPreKeyIdOffset
)
1008 .put("pniNextSignedPreKeyId", pniNextSignedPreKeyId
)
1009 .put("kyberPreKeyIdOffset", aciKyberPreKeyIdOffset
)
1010 .put("activeLastResortKyberPreKeyId", aciActiveLastResortKyberPreKeyId
)
1011 .put("pniKyberPreKeyIdOffset", pniKyberPreKeyIdOffset
)
1012 .put("pniActiveLastResortKyberPreKeyId", pniActiveLastResortKyberPreKeyId
)
1014 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
1015 .put("registered", registered
)
1016 .putPOJO("configurationStore", configurationStoreStorage
);
1018 try (var output
= new ByteArrayOutputStream()) {
1019 // Write to memory first to prevent corrupting the file in case of serialization errors
1020 jsonProcessor
.writeValue(output
, rootNode
);
1021 var input
= new ByteArrayInputStream(output
.toByteArray());
1022 fileChannel
.position(0);
1023 input
.transferTo(Channels
.newOutputStream(fileChannel
));
1024 fileChannel
.truncate(fileChannel
.position());
1025 fileChannel
.force(false);
1027 } catch (Exception e
) {
1028 logger
.error("Error saving file: {}", e
.getMessage(), e
);
1033 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
1034 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
1036 var lock
= fileChannel
.tryLock();
1039 logger
.debug("Config file is in use by another instance.");
1040 throw new IOException("Config file is in use by another instance.");
1042 logger
.info("Config file is in use by another instance, waiting…");
1043 lock
= fileChannel
.lock();
1044 logger
.info("Config file lock acquired.");
1046 final var result
= new Pair
<>(fileChannel
, lock
);
1050 if (fileChannel
!= null) {
1051 fileChannel
.close();
1056 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1057 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1058 this.aciPreKeyIdOffset
= getRandomPreKeyIdOffset();
1059 this.aciNextSignedPreKeyId
= getRandomPreKeyIdOffset();
1061 this.pniPreKeyIdOffset
= getRandomPreKeyIdOffset();
1062 this.pniNextSignedPreKeyId
= getRandomPreKeyIdOffset();
1067 private static int getRandomPreKeyIdOffset() {
1068 return new SecureRandom().nextInt(PREKEY_MAXIMUM_ID
);
1071 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1072 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1073 addAciPreKeys(records
);
1075 addPniPreKeys(records
);
1079 private void addAciPreKeys(List
<PreKeyRecord
> records
) {
1080 for (var record : records
) {
1081 if (aciPreKeyIdOffset
!= record.getId()) {
1082 logger
.error("Invalid pre key id {}, expected {}", record.getId(), aciPreKeyIdOffset
);
1083 throw new AssertionError("Invalid pre key id");
1085 getAciPreKeyStore().storePreKey(record.getId(), record);
1086 aciPreKeyIdOffset
= (aciPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1091 private void addPniPreKeys(List
<PreKeyRecord
> records
) {
1092 for (var record : records
) {
1093 if (pniPreKeyIdOffset
!= record.getId()) {
1094 logger
.error("Invalid pre key id {}, expected {}", record.getId(), pniPreKeyIdOffset
);
1095 throw new AssertionError("Invalid pre key id");
1097 getPniPreKeyStore().storePreKey(record.getId(), record);
1098 pniPreKeyIdOffset
= (pniPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1103 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1104 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1105 addAciSignedPreKey(record);
1107 addPniSignedPreKey(record);
1111 public void addAciSignedPreKey(SignedPreKeyRecord
record) {
1112 if (aciNextSignedPreKeyId
!= record.getId()) {
1113 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), aciNextSignedPreKeyId
);
1114 throw new AssertionError("Invalid signed pre key id");
1116 getAciSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1117 aciNextSignedPreKeyId
= (aciNextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1121 public void addPniSignedPreKey(SignedPreKeyRecord
record) {
1122 if (pniNextSignedPreKeyId
!= record.getId()) {
1123 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), pniNextSignedPreKeyId
);
1124 throw new AssertionError("Invalid signed pre key id");
1126 getPniSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1127 pniNextSignedPreKeyId
= (pniNextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1131 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1132 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1133 this.aciKyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
1134 this.aciActiveLastResortKyberPreKeyId
= -1;
1136 this.pniKyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
1137 this.pniActiveLastResortKyberPreKeyId
= -1;
1142 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1143 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1144 addAciKyberPreKeys(records
);
1146 addPniKyberPreKeys(records
);
1150 private void addAciKyberPreKeys(List
<KyberPreKeyRecord
> records
) {
1151 for (var record : records
) {
1152 if (aciKyberPreKeyIdOffset
!= record.getId()) {
1153 logger
.error("Invalid kyber pre key id {}, expected {}", record.getId(), aciKyberPreKeyIdOffset
);
1154 throw new AssertionError("Invalid kyber pre key id");
1156 getAciKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1157 aciKyberPreKeyIdOffset
= (aciKyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1162 private void addPniKyberPreKeys(List
<KyberPreKeyRecord
> records
) {
1163 for (var record : records
) {
1164 if (pniKyberPreKeyIdOffset
!= record.getId()) {
1165 logger
.error("Invalid kyber pre key id {}, expected {}", record.getId(), pniKyberPreKeyIdOffset
);
1166 throw new AssertionError("Invalid kyber pre key id");
1168 getPniKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1169 pniKyberPreKeyIdOffset
= (pniKyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1174 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1175 if (serviceIdType
.equals(ServiceIdType
.ACI
)) {
1176 addAciLastResortKyberPreKey(record);
1178 addPniLastResortKyberPreKey(record);
1182 public void addAciLastResortKyberPreKey(KyberPreKeyRecord
record) {
1183 if (aciKyberPreKeyIdOffset
!= record.getId()) {
1184 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1186 aciKyberPreKeyIdOffset
);
1187 throw new AssertionError("Invalid last resort kyber pre key id");
1189 getAciKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1190 aciActiveLastResortKyberPreKeyId
= record.getId();
1191 aciKyberPreKeyIdOffset
= (aciKyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1195 public void addPniLastResortKyberPreKey(KyberPreKeyRecord
record) {
1196 if (pniKyberPreKeyIdOffset
!= record.getId()) {
1197 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1199 pniKyberPreKeyIdOffset
);
1200 throw new AssertionError("Invalid last resort kyber pre key id");
1202 getPniKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1203 pniActiveLastResortKyberPreKeyId
= record.getId();
1204 pniKyberPreKeyIdOffset
= (pniKyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1208 public int getPreviousStorageVersion() {
1209 return previousStorageVersion
;
1212 public SignalServiceDataStore
getSignalServiceDataStore() {
1213 return new SignalServiceDataStore() {
1215 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1216 if (accountIdentifier
.equals(aci
)) {
1218 } else if (accountIdentifier
.equals(pni
)) {
1221 throw new IllegalArgumentException("No matching store found for " + accountIdentifier
);
1226 public SignalServiceAccountDataStore
aci() {
1227 return getAciSignalServiceAccountDataStore();
1231 public SignalServiceAccountDataStore
pni() {
1232 return getPniSignalServiceAccountDataStore();
1236 public boolean isMultiDevice() {
1237 return SignalAccount
.this.isMultiDevice();
1242 private SignalServiceAccountDataStore
getAciSignalServiceAccountDataStore() {
1243 return getOrCreate(() -> aciSignalProtocolStore
,
1244 () -> aciSignalProtocolStore
= new SignalProtocolStore(getAciPreKeyStore(),
1245 getAciSignedPreKeyStore(),
1246 getAciKyberPreKeyStore(),
1247 getAciSessionStore(),
1248 getAciIdentityKeyStore(),
1249 getSenderKeyStore(),
1250 this::isMultiDevice
));
1253 private SignalServiceAccountDataStore
getPniSignalServiceAccountDataStore() {
1254 return getOrCreate(() -> pniSignalProtocolStore
,
1255 () -> pniSignalProtocolStore
= new SignalProtocolStore(getPniPreKeyStore(),
1256 getPniSignedPreKeyStore(),
1257 getPniKyberPreKeyStore(),
1258 getPniSessionStore(),
1259 getPniIdentityKeyStore(),
1260 getSenderKeyStore(),
1261 this::isMultiDevice
));
1264 private PreKeyStore
getAciPreKeyStore() {
1265 return getOrCreate(() -> aciPreKeyStore
,
1266 () -> aciPreKeyStore
= new PreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1269 private SignedPreKeyStore
getAciSignedPreKeyStore() {
1270 return getOrCreate(() -> aciSignedPreKeyStore
,
1271 () -> aciSignedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1274 private KyberPreKeyStore
getAciKyberPreKeyStore() {
1275 return getOrCreate(() -> aciKyberPreKeyStore
,
1276 () -> aciKyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), ServiceIdType
.ACI
));
1279 private PreKeyStore
getPniPreKeyStore() {
1280 return getOrCreate(() -> pniPreKeyStore
,
1281 () -> pniPreKeyStore
= new PreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1284 private SignedPreKeyStore
getPniSignedPreKeyStore() {
1285 return getOrCreate(() -> pniSignedPreKeyStore
,
1286 () -> pniSignedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1289 private KyberPreKeyStore
getPniKyberPreKeyStore() {
1290 return getOrCreate(() -> pniKyberPreKeyStore
,
1291 () -> pniKyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), ServiceIdType
.PNI
));
1294 public SessionStore
getAciSessionStore() {
1295 return getOrCreate(() -> aciSessionStore
,
1296 () -> aciSessionStore
= new SessionStore(getAccountDatabase(), ServiceIdType
.ACI
));
1299 public SessionStore
getPniSessionStore() {
1300 return getOrCreate(() -> pniSessionStore
,
1301 () -> pniSessionStore
= new SessionStore(getAccountDatabase(), ServiceIdType
.PNI
));
1304 public IdentityKeyStore
getIdentityKeyStore() {
1305 return getOrCreate(() -> identityKeyStore
,
1306 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(), settings
.trustNewIdentity()));
1309 public SignalIdentityKeyStore
getAciIdentityKeyStore() {
1310 return getOrCreate(() -> aciIdentityKeyStore
,
1311 () -> aciIdentityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1312 () -> aciIdentityKeyPair
,
1313 localRegistrationId
,
1314 getIdentityKeyStore()));
1317 public SignalIdentityKeyStore
getPniIdentityKeyStore() {
1318 return getOrCreate(() -> pniIdentityKeyStore
,
1319 () -> pniIdentityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1320 () -> pniIdentityKeyPair
,
1321 localRegistrationId
,
1322 getIdentityKeyStore()));
1325 public GroupStore
getGroupStore() {
1326 return getOrCreate(() -> groupStore
,
1327 () -> groupStore
= new GroupStore(getAccountDatabase(),
1328 getRecipientResolver(),
1329 getRecipientIdCreator()));
1332 public ContactsStore
getContactStore() {
1333 return getRecipientStore();
1336 private RecipientIdCreator
getRecipientIdCreator() {
1337 return recipientId
-> getRecipientStore().create(recipientId
);
1340 public RecipientResolver
getRecipientResolver() {
1341 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1344 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1345 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1348 public RecipientAddressResolver
getRecipientAddressResolver() {
1349 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1352 public RecipientStore
getRecipientStore() {
1353 return getOrCreate(() -> recipientStore
,
1354 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1355 this::getSelfRecipientAddress
,
1356 getAccountDatabase()));
1359 public ProfileStore
getProfileStore() {
1360 return getRecipientStore();
1363 public StickerStore
getStickerStore() {
1364 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1367 public SenderKeyStore
getSenderKeyStore() {
1368 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1371 public ConfigurationStore
getConfigurationStore() {
1372 return configurationStore
;
1375 public MessageCache
getMessageCache() {
1376 return getOrCreate(() -> messageCache
,
1377 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1380 public AccountDatabase
getAccountDatabase() {
1381 return getOrCreate(() -> accountDatabase
, () -> {
1383 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1384 } catch (SQLException e
) {
1385 throw new RuntimeException(e
);
1390 public MessageSendLogStore
getMessageSendLogStore() {
1391 return getOrCreate(() -> messageSendLogStore
,
1392 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1393 settings
.disableMessageSendLog()));
1396 public CredentialsProvider
getCredentialsProvider() {
1397 return new CredentialsProvider() {
1399 public ACI
getAci() {
1404 public PNI
getPni() {
1409 public String
getE164() {
1414 public String
getPassword() {
1419 public int getDeviceId() {
1425 public String
getNumber() {
1429 public void setNumber(final String number
) {
1430 this.number
= number
;
1434 public String
getUsername() {
1438 public void setUsername(final String username
) {
1439 this.username
= username
;
1443 public ServiceEnvironment
getServiceEnvironment() {
1444 return serviceEnvironment
;
1447 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1448 this.serviceEnvironment
= serviceEnvironment
;
1452 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1453 return new AccountAttributes(null,
1454 getLocalRegistrationId(),
1457 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1458 getSelfUnidentifiedAccessKey(),
1459 isUnrestrictedUnidentifiedAccess(),
1460 getAccountCapabilities(),
1461 isDiscoverableByPhoneNumber(),
1462 encryptedDeviceName
,
1463 getLocalPniRegistrationId(),
1464 null); // TODO recoveryPassword?
1467 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1468 return getCapabilities(isPrimaryDevice());
1471 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1472 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aci
: pni
;
1475 public ACI
getAci() {
1479 public void setAci(final ACI aci
) {
1484 public PNI
getPni() {
1488 public void setPni(final PNI updatedPni
) {
1489 if (this.pni
!= null && !this.pni
.equals(updatedPni
)) {
1490 // Clear data for old PNI
1491 identityKeyStore
.deleteIdentity(this.pni
);
1492 getPniPreKeyStore().removeAllPreKeys();
1493 getPniSignedPreKeyStore().removeAllSignedPreKeys();
1494 getPniKyberPreKeyStore().removeAllKyberPreKeys();
1495 aciActiveLastResortKyberPreKeyId
= -1;
1496 pniActiveLastResortKyberPreKeyId
= -1;
1499 this.pni
= updatedPni
;
1504 final PNI updatedPni
,
1505 final IdentityKeyPair pniIdentityKeyPair
,
1506 final SignedPreKeyRecord pniSignedPreKey
,
1507 final int localPniRegistrationId
1511 setPniIdentityKeyPair(pniIdentityKeyPair
);
1512 addPniSignedPreKey(pniSignedPreKey
);
1513 setLocalPniRegistrationId(localPniRegistrationId
);
1516 public SignalServiceAddress
getSelfAddress() {
1517 return new SignalServiceAddress(aci
, number
);
1520 public RecipientAddress
getSelfRecipientAddress() {
1521 return new RecipientAddress(aci
, pni
, number
, username
);
1524 public RecipientId
getSelfRecipientId() {
1525 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1528 public String
getSessionId(final String forNumber
) {
1529 if (!forNumber
.equals(sessionNumber
)) {
1535 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1536 this.sessionNumber
= sessionNumber
;
1537 this.sessionId
= sessionId
;
1541 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1542 this.encryptedDeviceName
= encryptedDeviceName
;
1546 public int getDeviceId() {
1550 public boolean isPrimaryDevice() {
1551 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1554 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1555 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciIdentityKeyPair
: pniIdentityKeyPair
;
1558 public IdentityKeyPair
getAciIdentityKeyPair() {
1559 return aciIdentityKeyPair
;
1562 public IdentityKeyPair
getPniIdentityKeyPair() {
1563 return pniIdentityKeyPair
;
1566 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1567 pniIdentityKeyPair
= identityKeyPair
;
1568 final var pniPublicKey
= identityKeyPair
.getPublicKey();
1569 getIdentityKeyStore().saveIdentity(getPni(), pniPublicKey
);
1570 getIdentityKeyStore().setIdentityTrustLevel(getPni(), pniPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1574 public int getLocalRegistrationId() {
1575 return localRegistrationId
;
1578 public int getLocalPniRegistrationId() {
1579 return localPniRegistrationId
;
1582 public void setLocalPniRegistrationId(final int localPniRegistrationId
) {
1583 this.localPniRegistrationId
= localPniRegistrationId
;
1587 public String
getPassword() {
1591 private void setPassword(final String password
) {
1592 this.password
= password
;
1596 public void setRegistrationLockPin(final String registrationLockPin
) {
1597 this.registrationLockPin
= registrationLockPin
;
1601 public String
getRegistrationLockPin() {
1602 return registrationLockPin
;
1605 public String
getRegistrationLock() {
1606 final var masterKey
= getPinBackedMasterKey();
1607 if (masterKey
== null) {
1610 return masterKey
.deriveRegistrationLock();
1613 public MasterKey
getPinBackedMasterKey() {
1614 if (registrationLockPin
== null) {
1617 return pinMasterKey
;
1620 public MasterKey
getOrCreatePinMasterKey() {
1621 if (pinMasterKey
== null) {
1622 pinMasterKey
= KeyUtils
.createMasterKey();
1625 return pinMasterKey
;
1628 public StorageKey
getStorageKey() {
1629 if (pinMasterKey
!= null) {
1630 return pinMasterKey
.deriveStorageServiceKey();
1635 public StorageKey
getOrCreateStorageKey() {
1636 if (isPrimaryDevice()) {
1637 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1642 public void setStorageKey(final StorageKey storageKey
) {
1643 if (storageKey
.equals(this.storageKey
)) {
1646 this.storageKey
= storageKey
;
1650 public long getStorageManifestVersion() {
1651 return this.storageManifestVersion
;
1654 public void setStorageManifestVersion(final long storageManifestVersion
) {
1655 if (storageManifestVersion
== this.storageManifestVersion
) {
1658 this.storageManifestVersion
= storageManifestVersion
;
1662 public Optional
<SignalStorageManifest
> getStorageManifest() {
1663 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1664 if (!storageManifestFile
.exists()) {
1665 return Optional
.empty();
1667 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1668 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1669 } catch (IOException e
) {
1670 logger
.warn("Failed to read local storage manifest.", e
);
1671 return Optional
.empty();
1675 public void setStorageManifest(SignalStorageManifest manifest
) {
1676 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1677 if (manifest
== null) {
1678 if (storageManifestFile
.exists()) {
1680 Files
.delete(storageManifestFile
.toPath());
1681 } catch (IOException e
) {
1682 logger
.error("Failed to delete local storage manifest.", e
);
1688 final var manifestBytes
= manifest
.serialize();
1689 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1690 outputStream
.write(manifestBytes
);
1691 } catch (IOException e
) {
1692 logger
.error("Failed to store local storage manifest.", e
);
1696 public ProfileKey
getProfileKey() {
1700 public void setProfileKey(final ProfileKey profileKey
) {
1701 if (profileKey
.equals(this.profileKey
)) {
1704 this.profileKey
= profileKey
;
1706 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1709 public byte[] getSelfUnidentifiedAccessKey() {
1710 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1713 public int getPreKeyIdOffset(ServiceIdType serviceIdType
) {
1714 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciPreKeyIdOffset
: pniPreKeyIdOffset
;
1717 public int getNextSignedPreKeyId(ServiceIdType serviceIdType
) {
1718 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciNextSignedPreKeyId
: pniNextSignedPreKeyId
;
1721 public int getKyberPreKeyIdOffset(ServiceIdType serviceIdType
) {
1722 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aciKyberPreKeyIdOffset
: pniKyberPreKeyIdOffset
;
1725 public boolean isRegistered() {
1729 public void setRegistered(final boolean registered
) {
1730 this.registered
= registered
;
1734 public boolean isMultiDevice() {
1735 return isMultiDevice
;
1738 public void setMultiDevice(final boolean multiDevice
) {
1739 if (isMultiDevice
== multiDevice
) {
1742 isMultiDevice
= multiDevice
;
1746 public long getLastReceiveTimestamp() {
1747 return lastReceiveTimestamp
;
1750 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1751 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1755 public boolean isUnrestrictedUnidentifiedAccess() {
1756 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1757 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1760 public boolean isDiscoverableByPhoneNumber() {
1761 final var phoneNumberUnlisted
= configurationStore
.getPhoneNumberUnlisted();
1762 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1765 public void finishRegistration(final ACI aci
, final PNI pni
, final MasterKey masterKey
, final String pin
) {
1766 this.pinMasterKey
= masterKey
;
1767 this.storageManifestVersion
= -1;
1768 this.setStorageManifest(null);
1769 this.storageKey
= null;
1770 this.encryptedDeviceName
= null;
1771 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1772 this.isMultiDevice
= false;
1773 this.registered
= true;
1776 this.registrationLockPin
= pin
;
1777 this.lastReceiveTimestamp
= 0;
1781 getAciSessionStore().archiveAllSessions();
1782 getPniSessionStore().archiveAllSessions();
1783 getSenderKeyStore().deleteAll();
1784 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1785 final var aciPublicKey
= getAciIdentityKeyPair().getPublicKey();
1786 getIdentityKeyStore().saveIdentity(getAci(), aciPublicKey
);
1787 getIdentityKeyStore().setIdentityTrustLevel(getAci(), aciPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1788 if (getPniIdentityKeyPair() == null) {
1789 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
1791 final var pniPublicKey
= getPniIdentityKeyPair().getPublicKey();
1792 getIdentityKeyStore().saveIdentity(getPni(), pniPublicKey
);
1793 getIdentityKeyStore().setIdentityTrustLevel(getPni(), pniPublicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1797 public void deleteAccountData() throws IOException
{
1799 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1800 .sorted(Comparator
.reverseOrder())) {
1801 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1802 Files
.delete(file
.next());
1805 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1809 public void close() {
1810 synchronized (fileChannel
) {
1811 if (accountDatabase
!= null) {
1813 accountDatabase
.close();
1814 } catch (SQLException e
) {
1815 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1818 if (messageSendLogStore
!= null) {
1819 messageSendLogStore
.close();
1824 } catch (ClosedChannelException ignored
) {
1826 fileChannel
.close();
1827 } catch (IOException e
) {
1828 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1833 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1834 var value
= supplier
.get();
1835 if (value
!= null) {
1839 synchronized (LOCK
) {
1840 value
= supplier
.get();
1841 if (value
!= null) {
1845 return supplier
.get();
1849 private interface Callable
{