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
.sql
.Connection
;
93 import java
.sql
.SQLException
;
94 import java
.util
.Base64
;
95 import java
.util
.Comparator
;
96 import java
.util
.HashSet
;
97 import java
.util
.List
;
98 import java
.util
.Optional
;
99 import java
.util
.function
.Supplier
;
101 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.PREKEY_MAXIMUM_ID
;
102 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.getCapabilities
;
104 public class SignalAccount
implements Closeable
{
106 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
108 private static final int MINIMUM_STORAGE_VERSION
= 1;
109 private static final int CURRENT_STORAGE_VERSION
= 7;
111 private final Object LOCK
= new Object();
113 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
115 private final FileChannel fileChannel
;
116 private final FileLock lock
;
118 private int previousStorageVersion
;
120 private File dataPath
;
121 private String accountPath
;
122 private ServiceEnvironment serviceEnvironment
;
123 private String number
;
124 private String username
;
127 private String sessionId
;
128 private String sessionNumber
;
129 private String encryptedDeviceName
;
130 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
131 private boolean isMultiDevice
= false;
132 private String password
;
133 private String registrationLockPin
;
134 private MasterKey pinMasterKey
;
135 private StorageKey storageKey
;
136 private long storageManifestVersion
= -1;
137 private ProfileKey profileKey
;
138 private Settings settings
;
139 private long lastReceiveTimestamp
= 0;
141 private boolean registered
= false;
143 private final AccountData aciAccountData
= new AccountData(ServiceIdType
.ACI
);
144 private final AccountData pniAccountData
= new AccountData(ServiceIdType
.PNI
);
145 private IdentityKeyStore identityKeyStore
;
146 private SenderKeyStore senderKeyStore
;
147 private GroupStore groupStore
;
148 private RecipientStore recipientStore
;
149 private StickerStore stickerStore
;
150 private ConfigurationStore configurationStore
;
151 private ConfigurationStore
.Storage configurationStoreStorage
;
153 private MessageCache messageCache
;
154 private MessageSendLogStore messageSendLogStore
;
156 private AccountDatabase accountDatabase
;
158 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
159 this.fileChannel
= fileChannel
;
163 public static SignalAccount
load(
164 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
165 ) throws IOException
{
166 logger
.trace("Opening account file");
167 final var fileName
= getFileName(dataPath
, accountPath
);
168 final var pair
= openFileChannel(fileName
, waitForLock
);
170 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
171 logger
.trace("Loading account file");
172 signalAccount
.load(dataPath
, accountPath
, settings
);
173 logger
.trace("Migrating legacy parts of account file");
174 signalAccount
.migrateLegacyConfigs();
176 return signalAccount
;
177 } catch (Throwable e
) {
178 pair
.second().close();
179 pair
.first().close();
184 public static SignalAccount
create(
188 ServiceEnvironment serviceEnvironment
,
189 IdentityKeyPair aciIdentityKey
,
190 IdentityKeyPair pniIdentityKey
,
192 int pniRegistrationId
,
193 ProfileKey profileKey
,
194 final Settings settings
195 ) throws IOException
{
196 IOUtils
.createPrivateDirectories(dataPath
);
197 var fileName
= getFileName(dataPath
, accountPath
);
198 if (!fileName
.exists()) {
199 IOUtils
.createPrivateFile(fileName
);
202 final var pair
= openFileChannel(fileName
, true);
203 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
205 signalAccount
.accountPath
= accountPath
;
206 signalAccount
.number
= number
;
207 signalAccount
.serviceEnvironment
= serviceEnvironment
;
208 signalAccount
.profileKey
= profileKey
;
210 signalAccount
.dataPath
= dataPath
;
211 signalAccount
.aciAccountData
.setIdentityKeyPair(aciIdentityKey
);
212 signalAccount
.pniAccountData
.setIdentityKeyPair(pniIdentityKey
);
213 signalAccount
.aciAccountData
.setLocalRegistrationId(registrationId
);
214 signalAccount
.pniAccountData
.setLocalRegistrationId(pniRegistrationId
);
215 signalAccount
.settings
= settings
;
216 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
218 signalAccount
.registered
= false;
220 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
221 signalAccount
.migrateLegacyConfigs();
222 signalAccount
.save();
224 return signalAccount
;
227 public static SignalAccount
createOrUpdateLinkedAccount(
231 ServiceEnvironment serviceEnvironment
,
235 String encryptedDeviceName
,
237 IdentityKeyPair aciIdentityKey
,
238 IdentityKeyPair pniIdentityKey
,
240 int pniRegistrationId
,
241 ProfileKey profileKey
,
242 final Settings settings
243 ) throws IOException
{
244 IOUtils
.createPrivateDirectories(dataPath
);
245 var fileName
= getFileName(dataPath
, accountPath
);
246 if (!fileName
.exists()) {
247 return createLinkedAccount(dataPath
,
264 final var signalAccount
= load(dataPath
, accountPath
, true, settings
);
265 signalAccount
.setProvisioningData(number
,
274 signalAccount
.getRecipientTrustedResolver()
275 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
276 signalAccount
.aciAccountData
.getSessionStore().archiveAllSessions();
277 signalAccount
.pniAccountData
.getSessionStore().archiveAllSessions();
278 signalAccount
.getSenderKeyStore().deleteAll();
279 signalAccount
.clearAllPreKeys();
280 return signalAccount
;
283 public void initDatabase() {
284 getAccountDatabase();
287 private void clearAllPreKeys() {
288 clearAllPreKeys(ServiceIdType
.ACI
);
289 clearAllPreKeys(ServiceIdType
.PNI
);
292 private void clearAllPreKeys(ServiceIdType serviceIdType
) {
293 final var accountData
= getAccountData(serviceIdType
);
294 resetPreKeyOffsets(serviceIdType
);
295 resetKyberPreKeyOffsets(serviceIdType
);
296 accountData
.getPreKeyStore().removeAllPreKeys();
297 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
298 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
302 private static SignalAccount
createLinkedAccount(
306 ServiceEnvironment serviceEnvironment
,
310 String encryptedDeviceName
,
312 IdentityKeyPair aciIdentityKey
,
313 IdentityKeyPair pniIdentityKey
,
315 int pniRegistrationId
,
316 ProfileKey profileKey
,
317 final Settings settings
318 ) throws IOException
{
319 var fileName
= getFileName(dataPath
, accountPath
);
320 IOUtils
.createPrivateFile(fileName
);
322 final var pair
= openFileChannel(fileName
, true);
323 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
325 signalAccount
.dataPath
= dataPath
;
326 signalAccount
.accountPath
= accountPath
;
327 signalAccount
.serviceEnvironment
= serviceEnvironment
;
328 signalAccount
.aciAccountData
.setLocalRegistrationId(registrationId
);
329 signalAccount
.pniAccountData
.setLocalRegistrationId(pniRegistrationId
);
330 signalAccount
.settings
= settings
;
331 signalAccount
.setProvisioningData(number
,
341 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
343 signalAccount
.getRecipientTrustedResolver()
344 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
345 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
346 signalAccount
.migrateLegacyConfigs();
347 signalAccount
.clearAllPreKeys();
348 signalAccount
.save();
350 return signalAccount
;
353 private void setProvisioningData(
357 final String password
,
358 final String encryptedDeviceName
,
360 final IdentityKeyPair aciIdentity
,
361 final IdentityKeyPair pniIdentity
,
362 final ProfileKey profileKey
364 this.number
= number
;
367 this.password
= password
;
368 this.profileKey
= profileKey
;
369 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
370 this.encryptedDeviceName
= encryptedDeviceName
;
371 this.deviceId
= deviceId
;
372 this.aciAccountData
.setIdentityKeyPair(aciIdentity
);
373 this.pniAccountData
.setIdentityKeyPair(pniIdentity
);
374 this.registered
= true;
375 this.isMultiDevice
= true;
376 this.lastReceiveTimestamp
= 0;
377 this.pinMasterKey
= null;
378 this.storageManifestVersion
= -1;
379 this.setStorageManifest(null);
380 this.storageKey
= null;
381 trustSelfIdentity(ServiceIdType
.ACI
);
382 if (getPniIdentityKeyPair() != null) {
383 trustSelfIdentity(ServiceIdType
.PNI
);
387 private void migrateLegacyConfigs() {
388 if (getPassword() == null) {
389 setPassword(KeyUtils
.createPassword());
392 if (getProfileKey() == null) {
393 // Old config file, creating new profile key
394 setProfileKey(KeyUtils
.createProfileKey());
396 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
397 if (isPrimaryDevice() && getPniIdentityKeyPair() == null && getPni() != null) {
398 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
402 private void mergeRecipients(
403 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
404 ) throws SQLException
{
405 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
406 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
409 public void removeRecipient(final RecipientId recipientId
) {
410 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
411 getRecipientStore().deleteRecipientData(recipientId
);
412 getMessageCache().deleteMessages(recipientId
);
413 if (recipientAddress
.serviceId().isPresent()) {
414 final var serviceId
= recipientAddress
.serviceId().get();
415 aciAccountData
.getSessionStore().deleteAllSessions(serviceId
);
416 pniAccountData
.getSessionStore().deleteAllSessions(serviceId
);
417 getIdentityKeyStore().deleteIdentity(serviceId
);
418 getSenderKeyStore().deleteAll(serviceId
);
422 public static File
getFileName(File dataPath
, String account
) {
423 return new File(dataPath
, account
);
426 private static File
getUserPath(final File dataPath
, final String account
) {
427 final var path
= new File(dataPath
, account
+ ".d");
429 IOUtils
.createPrivateDirectories(path
);
430 } catch (IOException e
) {
431 throw new AssertionError("Failed to create user path", e
);
436 private static File
getMessageCachePath(File dataPath
, String account
) {
437 return new File(getUserPath(dataPath
, account
), "msg-cache");
440 private static File
getGroupCachePath(File dataPath
, String account
) {
441 return new File(getUserPath(dataPath
, account
), "group-cache");
444 private static File
getAciPreKeysPath(File dataPath
, String account
) {
445 return new File(getUserPath(dataPath
, account
), "pre-keys");
448 private static File
getAciSignedPreKeysPath(File dataPath
, String account
) {
449 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
452 private static File
getPniPreKeysPath(File dataPath
, String account
) {
453 return new File(getUserPath(dataPath
, account
), "pre-keys-pni");
456 private static File
getPniSignedPreKeysPath(File dataPath
, String account
) {
457 return new File(getUserPath(dataPath
, account
), "signed-pre-keys-pni");
460 private static File
getIdentitiesPath(File dataPath
, String account
) {
461 return new File(getUserPath(dataPath
, account
), "identities");
464 private static File
getSessionsPath(File dataPath
, String account
) {
465 return new File(getUserPath(dataPath
, account
), "sessions");
468 private static File
getSenderKeysPath(File dataPath
, String account
) {
469 return new File(getUserPath(dataPath
, account
), "sender-keys");
472 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
473 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
476 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
477 return new File(getUserPath(dataPath
, account
), "recipients-store");
480 private static File
getStorageManifestFile(File dataPath
, String account
) {
481 return new File(getUserPath(dataPath
, account
), "storage-manifest");
484 private static File
getDatabaseFile(File dataPath
, String account
) {
485 return new File(getUserPath(dataPath
, account
), "account.db");
488 public static boolean accountFileExists(File dataPath
, String account
) {
489 if (account
== null) {
492 var f
= getFileName(dataPath
, account
);
493 return !(!f
.exists() || f
.isDirectory());
497 File dataPath
, String accountPath
, final Settings settings
498 ) throws IOException
{
499 this.dataPath
= dataPath
;
500 this.accountPath
= accountPath
;
501 this.settings
= settings
;
502 final JsonNode rootNode
;
503 synchronized (fileChannel
) {
504 fileChannel
.position(0);
505 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
508 var migratedLegacyConfig
= false;
510 if (rootNode
.hasNonNull("version")) {
511 var accountVersion
= rootNode
.get("version").asInt(1);
512 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
513 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
514 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
515 throw new IOException("Config file was created by a no longer supported older version: "
518 previousStorageVersion
= accountVersion
;
519 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
520 migratedLegacyConfig
= true;
524 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
525 if (rootNode
.hasNonNull("password")) {
526 password
= rootNode
.get("password").asText();
528 if (rootNode
.hasNonNull("serviceEnvironment")) {
529 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
531 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
532 if (rootNode
.hasNonNull("usernameIdentifier")) {
533 username
= rootNode
.get("usernameIdentifier").asText();
535 if (rootNode
.hasNonNull("uuid")) {
537 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
538 } catch (IllegalArgumentException e
) {
539 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
542 if (rootNode
.hasNonNull("pni")) {
544 pni
= PNI
.parseOrThrow(rootNode
.get("pni").asText());
545 } catch (IllegalArgumentException e
) {
546 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
549 if (rootNode
.hasNonNull("sessionId")) {
550 sessionId
= rootNode
.get("sessionId").asText();
552 if (rootNode
.hasNonNull("sessionNumber")) {
553 sessionNumber
= rootNode
.get("sessionNumber").asText();
555 if (rootNode
.hasNonNull("deviceName")) {
556 encryptedDeviceName
= rootNode
.get("deviceName").asText();
558 if (rootNode
.hasNonNull("deviceId")) {
559 deviceId
= rootNode
.get("deviceId").asInt();
561 if (rootNode
.hasNonNull("isMultiDevice")) {
562 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
564 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
565 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
567 int registrationId
= 0;
568 if (rootNode
.hasNonNull("registrationId")) {
569 registrationId
= rootNode
.get("registrationId").asInt();
571 if (rootNode
.hasNonNull("pniRegistrationId")) {
572 pniAccountData
.setLocalRegistrationId(rootNode
.get("pniRegistrationId").asInt());
574 pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
576 IdentityKeyPair aciIdentityKeyPair
= null;
577 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
578 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
579 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
580 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
582 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
583 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
584 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
585 pniAccountData
.setIdentityKeyPair(KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
));
588 if (rootNode
.hasNonNull("registrationLockPin")) {
589 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
591 if (rootNode
.hasNonNull("pinMasterKey")) {
592 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
594 if (rootNode
.hasNonNull("storageKey")) {
595 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
597 if (rootNode
.hasNonNull("storageManifestVersion")) {
598 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
600 if (rootNode
.hasNonNull("preKeyIdOffset")) {
601 aciAccountData
.preKeyMetadata
.preKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
603 aciAccountData
.preKeyMetadata
.preKeyIdOffset
= getRandomPreKeyIdOffset();
605 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
606 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
608 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
610 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
611 pniAccountData
.preKeyMetadata
.preKeyIdOffset
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
613 pniAccountData
.preKeyMetadata
.preKeyIdOffset
= getRandomPreKeyIdOffset();
615 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
616 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
618 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
620 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
621 aciAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
623 aciAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
625 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
626 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId")
629 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
631 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
632 pniAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
634 pniAccountData
.preKeyMetadata
.kyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
636 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
637 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get(
638 "pniActiveLastResortKyberPreKeyId").asInt(-1);
640 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
642 if (rootNode
.hasNonNull("profileKey")) {
644 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
645 } catch (InvalidInputException e
) {
646 throw new IOException(
647 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
652 if (previousStorageVersion
< 5) {
653 final var legacyRecipientsStoreFile
= getRecipientsStoreFile(dataPath
, accountPath
);
654 if (legacyRecipientsStoreFile
.exists()) {
655 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
656 // Ensure our profile key is stored in profile store
657 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
658 migratedLegacyConfig
= true;
661 if (previousStorageVersion
< 6) {
662 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
664 final var legacyAciPreKeysPath
= getAciPreKeysPath(dataPath
, accountPath
);
665 if (legacyAciPreKeysPath
.exists()) {
666 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, aciAccountData
.getPreKeyStore());
667 migratedLegacyConfig
= true;
669 final var legacyPniPreKeysPath
= getPniPreKeysPath(dataPath
, accountPath
);
670 if (legacyPniPreKeysPath
.exists()) {
671 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, pniAccountData
.getPreKeyStore());
672 migratedLegacyConfig
= true;
674 final var legacyAciSignedPreKeysPath
= getAciSignedPreKeysPath(dataPath
, accountPath
);
675 if (legacyAciSignedPreKeysPath
.exists()) {
676 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, aciAccountData
.getSignedPreKeyStore());
677 migratedLegacyConfig
= true;
679 final var legacyPniSignedPreKeysPath
= getPniSignedPreKeysPath(dataPath
, accountPath
);
680 if (legacyPniSignedPreKeysPath
.exists()) {
681 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, pniAccountData
.getSignedPreKeyStore());
682 migratedLegacyConfig
= true;
684 final var legacySessionsPath
= getSessionsPath(dataPath
, accountPath
);
685 if (legacySessionsPath
.exists()) {
686 LegacySessionStore
.migrate(legacySessionsPath
,
687 getRecipientResolver(),
688 getRecipientAddressResolver(),
689 aciAccountData
.getSessionStore());
690 migratedLegacyConfig
= true;
692 final var legacyIdentitiesPath
= getIdentitiesPath(dataPath
, accountPath
);
693 if (legacyIdentitiesPath
.exists()) {
694 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
695 getRecipientResolver(),
696 getRecipientAddressResolver(),
697 getIdentityKeyStore());
698 migratedLegacyConfig
= true;
700 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
701 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
702 LegacyJsonSignalProtocolStore
.class)
704 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
705 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
706 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
707 migratedLegacyConfig
= true;
710 this.aciAccountData
.setIdentityKeyPair(aciIdentityKeyPair
);
711 this.aciAccountData
.setLocalRegistrationId(registrationId
);
713 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
715 final var legacySenderKeysPath
= getSenderKeysPath(dataPath
, accountPath
);
716 if (legacySenderKeysPath
.exists()) {
717 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
718 getRecipientResolver(),
719 getRecipientAddressResolver(),
720 getSenderKeyStore());
721 migratedLegacyConfig
= true;
723 final var legacySenderKeysSharedPath
= getSharedSenderKeysFile(dataPath
, accountPath
);
724 if (legacySenderKeysSharedPath
.exists()) {
725 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
726 getRecipientResolver(),
727 getRecipientAddressResolver(),
728 getSenderKeyStore());
729 migratedLegacyConfig
= true;
731 if (rootNode
.hasNonNull("groupStore")) {
732 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
733 LegacyGroupStore
.Storage
.class);
734 LegacyGroupStore
.migrate(groupStoreStorage
,
735 getGroupCachePath(dataPath
, accountPath
),
736 getRecipientResolver(),
738 migratedLegacyConfig
= true;
741 if (rootNode
.hasNonNull("stickerStore")) {
742 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
743 LegacyStickerStore
.Storage
.class);
744 LegacyStickerStore
.migrate(storage
, getStickerStore());
745 migratedLegacyConfig
= true;
748 if (rootNode
.hasNonNull("configurationStore")) {
749 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
750 ConfigurationStore
.Storage
.class);
751 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
752 this::saveConfigurationStore
);
754 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
757 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
759 if (migratedLegacyConfig
) {
764 private boolean loadLegacyStores(
765 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
767 var migrated
= false;
768 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
769 if (legacyRecipientStoreNode
!= null) {
770 logger
.debug("Migrating legacy recipient store.");
771 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
772 if (legacyRecipientStore
!= null) {
773 legacyRecipientStore
.getAddresses()
774 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
776 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
780 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
781 logger
.debug("Migrating legacy pre key store.");
782 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
784 aciAccountData
.getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
785 } catch (InvalidMessageException e
) {
786 logger
.warn("Failed to migrate pre key, ignoring", e
);
792 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
793 logger
.debug("Migrating legacy signed pre key store.");
794 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
796 aciAccountData
.getSignedPreKeyStore()
797 .storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
798 } catch (InvalidMessageException e
) {
799 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
805 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
806 logger
.debug("Migrating legacy session store.");
807 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
809 aciAccountData
.getSessionStore()
810 .storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(), session
.deviceId
),
811 new SessionRecord(session
.sessionRecord
));
812 } catch (Exception e
) {
813 logger
.warn("Failed to migrate session, ignoring", e
);
819 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
820 logger
.debug("Migrating legacy identity session store.");
821 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
822 if (identity
.getAddress().serviceId().isEmpty()) {
825 final var serviceId
= identity
.getAddress().serviceId().get();
826 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
827 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
828 identity
.getIdentityKey(),
829 identity
.getTrustLevel());
834 if (rootNode
.hasNonNull("contactStore")) {
835 logger
.debug("Migrating legacy contact store.");
836 final var contactStoreNode
= rootNode
.get("contactStore");
837 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
838 for (var contact
: contactStore
.getContacts()) {
839 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
840 getContactStore().storeContact(recipientId
,
841 new Contact(contact
.name
,
844 contact
.messageExpirationTime
,
849 // Store profile keys only in profile store
850 var profileKeyString
= contact
.profileKey
;
851 if (profileKeyString
!= null) {
852 final ProfileKey profileKey
;
854 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
855 getProfileStore().storeProfileKey(recipientId
, profileKey
);
856 } catch (InvalidInputException e
) {
857 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
864 if (rootNode
.hasNonNull("profileStore")) {
865 logger
.debug("Migrating legacy profile store.");
866 var profileStoreNode
= rootNode
.get("profileStore");
867 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
868 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
869 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.getAddress());
870 // Not migrating profile key credential here, it was changed to expiring profile key credentials
871 getProfileStore().storeProfileKey(recipientId
, profileEntry
.getProfileKey());
872 final var profile
= profileEntry
.getProfile();
873 if (profile
!= null) {
874 final var capabilities
= new HashSet
<Profile
.Capability
>();
875 if (profile
.getCapabilities() != null) {
876 if (profile
.getCapabilities().gv1Migration
) {
877 capabilities
.add(Profile
.Capability
.gv1Migration
);
879 if (profile
.getCapabilities().storage
) {
880 capabilities
.add(Profile
.Capability
.storage
);
883 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
884 profile
.getGivenName(),
885 profile
.getFamilyName(),
887 profile
.getAboutEmoji(),
890 profile
.isUnrestrictedUnidentifiedAccess()
891 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
892 : profile
.getUnidentifiedAccess() != null
893 ? Profile
.UnidentifiedAccessMode
.ENABLED
894 : Profile
.UnidentifiedAccessMode
.DISABLED
,
896 getProfileStore().storeProfile(recipientId
, newProfile
);
904 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
905 var threadStoreNode
= rootNode
.get("threadStore");
906 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
907 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
908 // Migrate thread info to group and contact store
909 for (var thread
: threadStore
.getThreads()) {
910 if (thread
.id
== null || thread
.id
.isEmpty()) {
914 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
915 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
916 var contact
= getContactStore().getContact(recipientId
);
917 if (contact
!= null) {
918 getContactStore().storeContact(recipientId
,
919 Contact
.newBuilder(contact
)
920 .withMessageExpirationTime(thread
.messageExpirationTime
)
924 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
925 if (groupInfo
instanceof GroupInfoV1
) {
926 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
927 getGroupStore().updateGroup(groupInfo
);
930 } catch (Exception e
) {
931 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
940 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
941 this.configurationStoreStorage
= storage
;
945 private void save() {
946 synchronized (fileChannel
) {
947 var rootNode
= jsonProcessor
.createObjectNode();
948 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
949 .put("username", number
)
950 .put("serviceEnvironment", serviceEnvironment
== null ?
null : serviceEnvironment
.name())
951 .put("usernameIdentifier", username
)
952 .put("uuid", aci
== null ?
null : aci
.toString())
953 .put("pni", pni
== null ?
null : pni
.toString())
954 .put("sessionId", sessionId
)
955 .put("sessionNumber", sessionNumber
)
956 .put("deviceName", encryptedDeviceName
)
957 .put("deviceId", deviceId
)
958 .put("isMultiDevice", isMultiDevice
)
959 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
960 .put("password", password
)
961 .put("registrationId", aciAccountData
.getLocalRegistrationId())
962 .put("pniRegistrationId", pniAccountData
.getLocalRegistrationId())
963 .put("identityPrivateKey",
965 .encodeToString(aciAccountData
.getIdentityKeyPair().getPrivateKey().serialize()))
968 .encodeToString(aciAccountData
.getIdentityKeyPair().getPublicKey().serialize()))
969 .put("pniIdentityPrivateKey",
970 pniAccountData
.getIdentityKeyPair() == null
972 : Base64
.getEncoder()
973 .encodeToString(pniAccountData
.getIdentityKeyPair()
976 .put("pniIdentityKey",
977 pniAccountData
.getIdentityKeyPair() == null
979 : Base64
.getEncoder()
980 .encodeToString(pniAccountData
.getIdentityKeyPair()
983 .put("registrationLockPin", registrationLockPin
)
985 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
987 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
988 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
989 .put("preKeyIdOffset", aciAccountData
.getPreKeyMetadata().preKeyIdOffset
)
990 .put("nextSignedPreKeyId", aciAccountData
.getPreKeyMetadata().nextSignedPreKeyId
)
991 .put("pniPreKeyIdOffset", pniAccountData
.getPreKeyMetadata().preKeyIdOffset
)
992 .put("pniNextSignedPreKeyId", pniAccountData
.getPreKeyMetadata().nextSignedPreKeyId
)
993 .put("kyberPreKeyIdOffset", aciAccountData
.getPreKeyMetadata().kyberPreKeyIdOffset
)
994 .put("activeLastResortKyberPreKeyId",
995 aciAccountData
.getPreKeyMetadata().activeLastResortKyberPreKeyId
)
996 .put("pniKyberPreKeyIdOffset", pniAccountData
.getPreKeyMetadata().kyberPreKeyIdOffset
)
997 .put("pniActiveLastResortKyberPreKeyId",
998 pniAccountData
.getPreKeyMetadata().activeLastResortKyberPreKeyId
)
1000 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
1001 .put("registered", registered
)
1002 .putPOJO("configurationStore", configurationStoreStorage
);
1004 try (var output
= new ByteArrayOutputStream()) {
1005 // Write to memory first to prevent corrupting the file in case of serialization errors
1006 jsonProcessor
.writeValue(output
, rootNode
);
1007 var input
= new ByteArrayInputStream(output
.toByteArray());
1008 fileChannel
.position(0);
1009 input
.transferTo(Channels
.newOutputStream(fileChannel
));
1010 fileChannel
.truncate(fileChannel
.position());
1011 fileChannel
.force(false);
1013 } catch (Exception e
) {
1014 logger
.error("Error saving file: {}", e
.getMessage(), e
);
1019 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
1020 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
1022 var lock
= fileChannel
.tryLock();
1025 logger
.debug("Config file is in use by another instance.");
1026 throw new IOException("Config file is in use by another instance.");
1028 logger
.info("Config file is in use by another instance, waiting…");
1029 lock
= fileChannel
.lock();
1030 logger
.info("Config file lock acquired.");
1032 final var result
= new Pair
<>(fileChannel
, lock
);
1036 if (fileChannel
!= null) {
1037 fileChannel
.close();
1042 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1043 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1044 preKeyMetadata
.preKeyIdOffset
= getRandomPreKeyIdOffset();
1045 preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
1049 private static int getRandomPreKeyIdOffset() {
1050 return KeyUtils
.getRandomInt(PREKEY_MAXIMUM_ID
);
1053 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1054 final var accountData
= getAccountData(serviceIdType
);
1055 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1056 for (var record : records
) {
1057 if (preKeyMetadata
.preKeyIdOffset
!= record.getId()) {
1058 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata
.preKeyIdOffset
);
1059 throw new AssertionError("Invalid pre key id");
1061 accountData
.getPreKeyStore().storePreKey(record.getId(), record);
1062 preKeyMetadata
.preKeyIdOffset
= (preKeyMetadata
.preKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1067 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1068 final var accountData
= getAccountData(serviceIdType
);
1069 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1070 if (preKeyMetadata
.nextSignedPreKeyId
!= record.getId()) {
1071 logger
.error("Invalid signed pre key id {}, expected {}",
1073 preKeyMetadata
.nextSignedPreKeyId
);
1074 throw new AssertionError("Invalid signed pre key id");
1076 accountData
.getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1077 preKeyMetadata
.nextSignedPreKeyId
= (preKeyMetadata
.nextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1081 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1082 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1083 preKeyMetadata
.kyberPreKeyIdOffset
= getRandomPreKeyIdOffset();
1084 preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
1088 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1089 final var accountData
= getAccountData(serviceIdType
);
1090 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1091 for (var record : records
) {
1092 if (preKeyMetadata
.kyberPreKeyIdOffset
!= record.getId()) {
1093 logger
.error("Invalid kyber pre key id {}, expected {}",
1095 preKeyMetadata
.kyberPreKeyIdOffset
);
1096 throw new AssertionError("Invalid kyber pre key id");
1098 accountData
.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1099 preKeyMetadata
.kyberPreKeyIdOffset
= (preKeyMetadata
.kyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1104 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1105 final var accountData
= getAccountData(serviceIdType
);
1106 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1107 if (preKeyMetadata
.kyberPreKeyIdOffset
!= record.getId()) {
1108 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1110 preKeyMetadata
.kyberPreKeyIdOffset
);
1111 throw new AssertionError("Invalid last resort kyber pre key id");
1113 accountData
.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1114 preKeyMetadata
.activeLastResortKyberPreKeyId
= record.getId();
1115 preKeyMetadata
.kyberPreKeyIdOffset
= (preKeyMetadata
.kyberPreKeyIdOffset
+ 1) % PREKEY_MAXIMUM_ID
;
1119 public int getPreviousStorageVersion() {
1120 return previousStorageVersion
;
1123 public AccountData
getAccountData(ServiceIdType serviceIdType
) {
1124 return switch (serviceIdType
) {
1125 case ACI
-> aciAccountData
;
1126 case PNI
-> pniAccountData
;
1130 public AccountData
getAccountData(ServiceId accountIdentifier
) {
1131 if (accountIdentifier
.equals(aci
)) {
1132 return aciAccountData
;
1133 } else if (accountIdentifier
.equals(pni
)) {
1134 return pniAccountData
;
1136 throw new IllegalArgumentException("No matching account data found for " + accountIdentifier
);
1140 public SignalServiceDataStore
getSignalServiceDataStore() {
1141 return new SignalServiceDataStore() {
1143 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1144 if (accountIdentifier
.equals(aci
)) {
1146 } else if (accountIdentifier
.equals(pni
)) {
1149 throw new IllegalArgumentException("No matching store found for " + accountIdentifier
);
1154 public SignalServiceAccountDataStore
aci() {
1155 return aciAccountData
.getSignalServiceAccountDataStore();
1159 public SignalServiceAccountDataStore
pni() {
1160 return pniAccountData
.getSignalServiceAccountDataStore();
1164 public boolean isMultiDevice() {
1165 return SignalAccount
.this.isMultiDevice();
1170 public IdentityKeyStore
getIdentityKeyStore() {
1171 return getOrCreate(() -> identityKeyStore
,
1172 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(), settings
.trustNewIdentity()));
1175 public GroupStore
getGroupStore() {
1176 return getOrCreate(() -> groupStore
,
1177 () -> groupStore
= new GroupStore(getAccountDatabase(),
1178 getRecipientResolver(),
1179 getRecipientIdCreator()));
1182 public ContactsStore
getContactStore() {
1183 return getRecipientStore();
1186 private RecipientIdCreator
getRecipientIdCreator() {
1187 return recipientId
-> getRecipientStore().create(recipientId
);
1190 public RecipientResolver
getRecipientResolver() {
1191 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1194 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1195 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1198 public RecipientAddressResolver
getRecipientAddressResolver() {
1199 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1202 public RecipientStore
getRecipientStore() {
1203 return getOrCreate(() -> recipientStore
,
1204 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1205 this::getSelfRecipientAddress
,
1206 getAccountDatabase()));
1209 public ProfileStore
getProfileStore() {
1210 return getRecipientStore();
1213 public StickerStore
getStickerStore() {
1214 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1217 public SenderKeyStore
getSenderKeyStore() {
1218 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1221 public ConfigurationStore
getConfigurationStore() {
1222 return configurationStore
;
1225 public MessageCache
getMessageCache() {
1226 return getOrCreate(() -> messageCache
,
1227 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1230 public AccountDatabase
getAccountDatabase() {
1231 return getOrCreate(() -> accountDatabase
, () -> {
1233 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1234 } catch (SQLException e
) {
1235 throw new RuntimeException(e
);
1240 public MessageSendLogStore
getMessageSendLogStore() {
1241 return getOrCreate(() -> messageSendLogStore
,
1242 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1243 settings
.disableMessageSendLog()));
1246 public CredentialsProvider
getCredentialsProvider() {
1247 return new CredentialsProvider() {
1249 public ACI
getAci() {
1254 public PNI
getPni() {
1259 public String
getE164() {
1264 public String
getPassword() {
1269 public int getDeviceId() {
1275 public String
getNumber() {
1279 public void setNumber(final String number
) {
1280 this.number
= number
;
1284 public String
getUsername() {
1288 public void setUsername(final String username
) {
1289 this.username
= username
;
1293 public ServiceEnvironment
getServiceEnvironment() {
1294 return serviceEnvironment
;
1297 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1298 this.serviceEnvironment
= serviceEnvironment
;
1302 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1303 return new AccountAttributes(null,
1304 getLocalRegistrationId(),
1307 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1308 getSelfUnidentifiedAccessKey(),
1309 isUnrestrictedUnidentifiedAccess(),
1310 getAccountCapabilities(),
1311 isDiscoverableByPhoneNumber(),
1312 encryptedDeviceName
,
1313 getLocalPniRegistrationId(),
1314 null); // TODO recoveryPassword?
1317 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1318 return getCapabilities(isPrimaryDevice());
1321 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1322 return serviceIdType
.equals(ServiceIdType
.ACI
) ? aci
: pni
;
1325 public ACI
getAci() {
1329 public void setAci(final ACI aci
) {
1334 public PNI
getPni() {
1338 public void setPni(final PNI updatedPni
) {
1339 if (this.pni
!= null && !this.pni
.equals(updatedPni
)) {
1340 // Clear data for old PNI
1341 identityKeyStore
.deleteIdentity(this.pni
);
1342 clearAllPreKeys(ServiceIdType
.PNI
);
1345 this.pni
= updatedPni
;
1350 final PNI updatedPni
,
1351 final IdentityKeyPair pniIdentityKeyPair
,
1352 final SignedPreKeyRecord pniSignedPreKey
,
1353 final int localPniRegistrationId
1357 setPniIdentityKeyPair(pniIdentityKeyPair
);
1358 addSignedPreKey(ServiceIdType
.PNI
, pniSignedPreKey
);
1359 setLocalPniRegistrationId(localPniRegistrationId
);
1362 public SignalServiceAddress
getSelfAddress() {
1363 return new SignalServiceAddress(aci
, number
);
1366 public RecipientAddress
getSelfRecipientAddress() {
1367 return new RecipientAddress(aci
, pni
, number
, username
);
1370 public RecipientId
getSelfRecipientId() {
1371 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1374 public String
getSessionId(final String forNumber
) {
1375 if (!forNumber
.equals(sessionNumber
)) {
1381 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1382 this.sessionNumber
= sessionNumber
;
1383 this.sessionId
= sessionId
;
1387 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1388 this.encryptedDeviceName
= encryptedDeviceName
;
1392 public int getDeviceId() {
1396 public boolean isPrimaryDevice() {
1397 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1400 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1401 return getAccountData(serviceIdType
).getIdentityKeyPair();
1404 public IdentityKeyPair
getAciIdentityKeyPair() {
1405 return aciAccountData
.getIdentityKeyPair();
1408 public IdentityKeyPair
getPniIdentityKeyPair() {
1409 return pniAccountData
.getIdentityKeyPair();
1412 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1413 pniAccountData
.setIdentityKeyPair(identityKeyPair
);
1414 trustSelfIdentity(ServiceIdType
.PNI
);
1418 public int getLocalRegistrationId() {
1419 return aciAccountData
.getLocalRegistrationId();
1422 public int getLocalPniRegistrationId() {
1423 return pniAccountData
.getLocalRegistrationId();
1426 public void setLocalPniRegistrationId(final int localPniRegistrationId
) {
1427 pniAccountData
.setLocalRegistrationId(localPniRegistrationId
);
1431 public String
getPassword() {
1435 private void setPassword(final String password
) {
1436 this.password
= password
;
1440 public void setRegistrationLockPin(final String registrationLockPin
) {
1441 this.registrationLockPin
= registrationLockPin
;
1445 public String
getRegistrationLockPin() {
1446 return registrationLockPin
;
1449 public String
getRegistrationLock() {
1450 final var masterKey
= getPinBackedMasterKey();
1451 if (masterKey
== null) {
1454 return masterKey
.deriveRegistrationLock();
1457 public MasterKey
getPinBackedMasterKey() {
1458 if (registrationLockPin
== null) {
1461 return pinMasterKey
;
1464 public MasterKey
getOrCreatePinMasterKey() {
1465 if (pinMasterKey
== null) {
1466 pinMasterKey
= KeyUtils
.createMasterKey();
1469 return pinMasterKey
;
1472 public StorageKey
getStorageKey() {
1473 if (pinMasterKey
!= null) {
1474 return pinMasterKey
.deriveStorageServiceKey();
1479 public StorageKey
getOrCreateStorageKey() {
1480 if (isPrimaryDevice()) {
1481 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1486 public void setStorageKey(final StorageKey storageKey
) {
1487 if (storageKey
.equals(this.storageKey
)) {
1490 this.storageKey
= storageKey
;
1494 public long getStorageManifestVersion() {
1495 return this.storageManifestVersion
;
1498 public void setStorageManifestVersion(final long storageManifestVersion
) {
1499 if (storageManifestVersion
== this.storageManifestVersion
) {
1502 this.storageManifestVersion
= storageManifestVersion
;
1506 public Optional
<SignalStorageManifest
> getStorageManifest() {
1507 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1508 if (!storageManifestFile
.exists()) {
1509 return Optional
.empty();
1511 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1512 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1513 } catch (IOException e
) {
1514 logger
.warn("Failed to read local storage manifest.", e
);
1515 return Optional
.empty();
1519 public void setStorageManifest(SignalStorageManifest manifest
) {
1520 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1521 if (manifest
== null) {
1522 if (storageManifestFile
.exists()) {
1524 Files
.delete(storageManifestFile
.toPath());
1525 } catch (IOException e
) {
1526 logger
.error("Failed to delete local storage manifest.", e
);
1532 final var manifestBytes
= manifest
.serialize();
1533 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1534 outputStream
.write(manifestBytes
);
1535 } catch (IOException e
) {
1536 logger
.error("Failed to store local storage manifest.", e
);
1540 public ProfileKey
getProfileKey() {
1544 public void setProfileKey(final ProfileKey profileKey
) {
1545 if (profileKey
.equals(this.profileKey
)) {
1548 this.profileKey
= profileKey
;
1550 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1553 public byte[] getSelfUnidentifiedAccessKey() {
1554 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1557 public int getPreKeyIdOffset(ServiceIdType serviceIdType
) {
1558 return getAccountData(serviceIdType
).getPreKeyMetadata().preKeyIdOffset
;
1561 public int getNextSignedPreKeyId(ServiceIdType serviceIdType
) {
1562 return getAccountData(serviceIdType
).getPreKeyMetadata().nextSignedPreKeyId
;
1565 public int getKyberPreKeyIdOffset(ServiceIdType serviceIdType
) {
1566 return getAccountData(serviceIdType
).getPreKeyMetadata().kyberPreKeyIdOffset
;
1569 public boolean isRegistered() {
1573 public void setRegistered(final boolean registered
) {
1574 this.registered
= registered
;
1578 public boolean isMultiDevice() {
1579 return isMultiDevice
;
1582 public void setMultiDevice(final boolean multiDevice
) {
1583 if (isMultiDevice
== multiDevice
) {
1586 isMultiDevice
= multiDevice
;
1590 public long getLastReceiveTimestamp() {
1591 return lastReceiveTimestamp
;
1594 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1595 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1599 public boolean isUnrestrictedUnidentifiedAccess() {
1600 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1601 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1604 public boolean isDiscoverableByPhoneNumber() {
1605 final var phoneNumberUnlisted
= configurationStore
.getPhoneNumberUnlisted();
1606 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1609 public void finishRegistration(final ACI aci
, final PNI pni
, final MasterKey masterKey
, final String pin
) {
1610 this.pinMasterKey
= masterKey
;
1611 this.storageManifestVersion
= -1;
1612 this.setStorageManifest(null);
1613 this.storageKey
= null;
1614 this.encryptedDeviceName
= null;
1615 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1616 this.isMultiDevice
= false;
1617 this.registered
= true;
1620 this.registrationLockPin
= pin
;
1621 this.lastReceiveTimestamp
= 0;
1625 aciAccountData
.getSessionStore().archiveAllSessions();
1626 pniAccountData
.getSessionStore().archiveAllSessions();
1627 getSenderKeyStore().deleteAll();
1628 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1629 trustSelfIdentity(ServiceIdType
.ACI
);
1630 if (getPniIdentityKeyPair() == null) {
1631 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
1633 trustSelfIdentity(ServiceIdType
.PNI
);
1637 private void trustSelfIdentity(ServiceIdType serviceIdType
) {
1638 final var accountData
= getAccountData(serviceIdType
);
1639 final var serviceId
= accountData
.getServiceId();
1640 final var publicKey
= accountData
.getIdentityKeyPair().getPublicKey();
1641 getIdentityKeyStore().saveIdentity(serviceId
, publicKey
);
1642 getIdentityKeyStore().setIdentityTrustLevel(serviceId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1645 public void deleteAccountData() throws IOException
{
1647 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1648 .sorted(Comparator
.reverseOrder())) {
1649 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1650 Files
.delete(file
.next());
1653 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1657 public void close() {
1658 synchronized (fileChannel
) {
1659 if (accountDatabase
!= null) {
1661 accountDatabase
.close();
1662 } catch (SQLException e
) {
1663 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1666 if (messageSendLogStore
!= null) {
1667 messageSendLogStore
.close();
1672 } catch (ClosedChannelException ignored
) {
1674 fileChannel
.close();
1675 } catch (IOException e
) {
1676 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1681 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1682 var value
= supplier
.get();
1683 if (value
!= null) {
1687 synchronized (LOCK
) {
1688 value
= supplier
.get();
1689 if (value
!= null) {
1693 return supplier
.get();
1697 private interface Callable
{
1702 private static class PreKeyMetadata
{
1704 private int preKeyIdOffset
= 1;
1705 private int nextSignedPreKeyId
= 1;
1706 private int kyberPreKeyIdOffset
= 1;
1707 private int activeLastResortKyberPreKeyId
= -1;
1710 public class AccountData
{
1712 private final ServiceIdType serviceIdType
;
1713 private IdentityKeyPair identityKeyPair
;
1714 private int localRegistrationId
;
1715 private final PreKeyMetadata preKeyMetadata
= new PreKeyMetadata();
1717 private SignalProtocolStore signalProtocolStore
;
1718 private PreKeyStore preKeyStore
;
1719 private SignedPreKeyStore signedPreKeyStore
;
1720 private KyberPreKeyStore kyberPreKeyStore
;
1721 private SessionStore sessionStore
;
1722 private SignalIdentityKeyStore identityKeyStore
;
1724 public AccountData(final ServiceIdType serviceIdType
) {
1725 this.serviceIdType
= serviceIdType
;
1728 public ServiceId
getServiceId() {
1729 return getAccountId(serviceIdType
);
1732 public IdentityKeyPair
getIdentityKeyPair() {
1733 return identityKeyPair
;
1736 private void setIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1737 this.identityKeyPair
= identityKeyPair
;
1740 public int getLocalRegistrationId() {
1741 return localRegistrationId
;
1744 private void setLocalRegistrationId(final int localRegistrationId
) {
1745 this.localRegistrationId
= localRegistrationId
;
1746 this.identityKeyStore
= null;
1749 public PreKeyMetadata
getPreKeyMetadata() {
1750 return preKeyMetadata
;
1753 private SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1754 return getOrCreate(() -> signalProtocolStore
,
1755 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
1756 getSignedPreKeyStore(),
1757 getKyberPreKeyStore(),
1759 getIdentityKeyStore(),
1760 getSenderKeyStore(),
1761 SignalAccount
.this::isMultiDevice
));
1764 private PreKeyStore
getPreKeyStore() {
1765 return getOrCreate(() -> preKeyStore
,
1766 () -> preKeyStore
= new PreKeyStore(getAccountDatabase(), serviceIdType
));
1769 private SignedPreKeyStore
getSignedPreKeyStore() {
1770 return getOrCreate(() -> signedPreKeyStore
,
1771 () -> signedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), serviceIdType
));
1774 private KyberPreKeyStore
getKyberPreKeyStore() {
1775 return getOrCreate(() -> kyberPreKeyStore
,
1776 () -> kyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), serviceIdType
));
1779 public SessionStore
getSessionStore() {
1780 return getOrCreate(() -> sessionStore
,
1781 () -> sessionStore
= new SessionStore(getAccountDatabase(), serviceIdType
));
1784 public SignalIdentityKeyStore
getIdentityKeyStore() {
1785 return getOrCreate(() -> identityKeyStore
,
1786 () -> identityKeyStore
= new SignalIdentityKeyStore(getRecipientResolver(),
1787 () -> identityKeyPair
,
1788 localRegistrationId
,
1789 SignalAccount
.this.getIdentityKeyStore()));