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
.configuration
.LegacyConfigurationStore
;
16 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
17 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
18 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
19 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
20 import org
.asamk
.signal
.manager
.storage
.groups
.LegacyGroupStore
;
21 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
22 import org
.asamk
.signal
.manager
.storage
.identities
.LegacyIdentityKeyStore
;
23 import org
.asamk
.signal
.manager
.storage
.identities
.SignalIdentityKeyStore
;
24 import org
.asamk
.signal
.manager
.storage
.keyValue
.KeyValueEntry
;
25 import org
.asamk
.signal
.manager
.storage
.keyValue
.KeyValueStore
;
26 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
27 import org
.asamk
.signal
.manager
.storage
.prekeys
.KyberPreKeyStore
;
28 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacyPreKeyStore
;
29 import org
.asamk
.signal
.manager
.storage
.prekeys
.LegacySignedPreKeyStore
;
30 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
31 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
32 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
33 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
34 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
35 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
36 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore2
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
39 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
40 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientIdCreator
;
41 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
42 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
43 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientTrustedResolver
;
44 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
45 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeyRecordStore
;
46 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeySharedStore
;
47 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
48 import org
.asamk
.signal
.manager
.storage
.sessions
.LegacySessionStore
;
49 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
50 import org
.asamk
.signal
.manager
.storage
.stickers
.LegacyStickerStore
;
51 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
52 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
53 import org
.asamk
.signal
.manager
.util
.IOUtils
;
54 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
55 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
56 import org
.signal
.libsignal
.protocol
.InvalidMessageException
;
57 import org
.signal
.libsignal
.protocol
.SignalProtocolAddress
;
58 import org
.signal
.libsignal
.protocol
.state
.KyberPreKeyRecord
;
59 import org
.signal
.libsignal
.protocol
.state
.PreKeyRecord
;
60 import org
.signal
.libsignal
.protocol
.state
.SessionRecord
;
61 import org
.signal
.libsignal
.protocol
.state
.SignedPreKeyRecord
;
62 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
63 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
64 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
65 import org
.slf4j
.Logger
;
66 import org
.slf4j
.LoggerFactory
;
67 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
68 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
69 import org
.whispersystems
.signalservice
.api
.account
.AccountAttributes
;
70 import org
.whispersystems
.signalservice
.api
.account
.PreKeyCollection
;
71 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
72 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
73 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
74 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.ACI
;
75 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.PNI
;
76 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
77 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
78 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
79 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
80 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
81 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
83 import java
.io
.ByteArrayInputStream
;
84 import java
.io
.ByteArrayOutputStream
;
85 import java
.io
.Closeable
;
87 import java
.io
.FileInputStream
;
88 import java
.io
.FileOutputStream
;
89 import java
.io
.IOException
;
90 import java
.io
.RandomAccessFile
;
91 import java
.nio
.channels
.Channels
;
92 import java
.nio
.channels
.ClosedChannelException
;
93 import java
.nio
.channels
.FileChannel
;
94 import java
.nio
.channels
.FileLock
;
95 import java
.nio
.file
.Files
;
96 import java
.sql
.Connection
;
97 import java
.sql
.SQLException
;
98 import java
.util
.Base64
;
99 import java
.util
.Comparator
;
100 import java
.util
.HashSet
;
101 import java
.util
.List
;
102 import java
.util
.Optional
;
103 import java
.util
.function
.Function
;
104 import java
.util
.function
.Supplier
;
106 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.PREKEY_MAXIMUM_ID
;
107 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.getCapabilities
;
109 public class SignalAccount
implements Closeable
{
111 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
113 private static final int MINIMUM_STORAGE_VERSION
= 1;
114 private static final int CURRENT_STORAGE_VERSION
= 8;
116 private final Object LOCK
= new Object();
118 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
120 private final FileChannel fileChannel
;
121 private final FileLock lock
;
123 private int previousStorageVersion
;
125 private File dataPath
;
126 private String accountPath
;
128 private ServiceEnvironment serviceEnvironment
;
129 private String number
;
130 private String username
;
131 private String encryptedDeviceName
;
132 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
133 private String password
;
134 private String registrationLockPin
;
135 private MasterKey pinMasterKey
;
136 private StorageKey storageKey
;
137 private ProfileKey profileKey
;
139 private Settings settings
;
141 private final KeyValueEntry
<String
> verificationSessionId
= new KeyValueEntry
<>("verification-session-id",
143 private final KeyValueEntry
<String
> verificationSessionNumber
= new KeyValueEntry
<>("verification-session-number",
145 private final KeyValueEntry
<Long
> lastReceiveTimestamp
= new KeyValueEntry
<>("last-receive-timestamp",
148 private final KeyValueEntry
<Long
> storageManifestVersion
= new KeyValueEntry
<>("storage-manifest-version",
151 private boolean isMultiDevice
= false;
152 private boolean registered
= false;
154 private final AccountData
<ACI
> aciAccountData
= new AccountData
<>(ServiceIdType
.ACI
);
155 private final AccountData
<PNI
> pniAccountData
= new AccountData
<>(ServiceIdType
.PNI
);
156 private IdentityKeyStore identityKeyStore
;
157 private SenderKeyStore senderKeyStore
;
158 private GroupStore groupStore
;
159 private RecipientStore recipientStore
;
160 private StickerStore stickerStore
;
161 private ConfigurationStore configurationStore
;
162 private KeyValueStore keyValueStore
;
164 private MessageCache messageCache
;
165 private MessageSendLogStore messageSendLogStore
;
167 private AccountDatabase accountDatabase
;
169 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
170 this.fileChannel
= fileChannel
;
174 public static SignalAccount
load(
175 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
176 ) throws IOException
{
177 logger
.trace("Opening account file");
178 final var fileName
= getFileName(dataPath
, accountPath
);
179 final var pair
= openFileChannel(fileName
, waitForLock
);
181 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
182 logger
.trace("Loading account file");
183 signalAccount
.load(dataPath
, accountPath
, settings
);
184 logger
.trace("Migrating legacy parts of account file");
185 signalAccount
.migrateLegacyConfigs();
187 return signalAccount
;
188 } catch (Throwable e
) {
189 pair
.second().close();
190 pair
.first().close();
195 public static SignalAccount
create(
199 ServiceEnvironment serviceEnvironment
,
200 IdentityKeyPair aciIdentityKey
,
201 IdentityKeyPair pniIdentityKey
,
203 int pniRegistrationId
,
204 ProfileKey profileKey
,
205 final Settings settings
206 ) throws IOException
{
207 IOUtils
.createPrivateDirectories(dataPath
);
208 var fileName
= getFileName(dataPath
, accountPath
);
209 if (!fileName
.exists()) {
210 IOUtils
.createPrivateFile(fileName
);
213 final var pair
= openFileChannel(fileName
, true);
214 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
216 signalAccount
.accountPath
= accountPath
;
217 signalAccount
.number
= number
;
218 signalAccount
.serviceEnvironment
= serviceEnvironment
;
219 signalAccount
.profileKey
= profileKey
;
220 signalAccount
.password
= KeyUtils
.createPassword();
222 signalAccount
.dataPath
= dataPath
;
223 signalAccount
.aciAccountData
.setIdentityKeyPair(aciIdentityKey
);
224 signalAccount
.pniAccountData
.setIdentityKeyPair(pniIdentityKey
);
225 signalAccount
.aciAccountData
.setLocalRegistrationId(registrationId
);
226 signalAccount
.pniAccountData
.setLocalRegistrationId(pniRegistrationId
);
227 signalAccount
.settings
= settings
;
229 signalAccount
.registered
= false;
231 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
232 signalAccount
.migrateLegacyConfigs();
233 signalAccount
.clearAllPreKeys();
234 signalAccount
.save();
236 return signalAccount
;
239 private static SignalAccount
createLinkedAccount(
243 ServiceEnvironment serviceEnvironment
,
247 String encryptedDeviceName
,
249 IdentityKeyPair aciIdentityKey
,
250 IdentityKeyPair pniIdentityKey
,
252 int pniRegistrationId
,
253 ProfileKey profileKey
,
254 final Settings settings
255 ) throws IOException
{
256 var fileName
= getFileName(dataPath
, accountPath
);
257 IOUtils
.createPrivateFile(fileName
);
259 final var pair
= openFileChannel(fileName
, true);
260 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
262 signalAccount
.dataPath
= dataPath
;
263 signalAccount
.accountPath
= accountPath
;
264 signalAccount
.serviceEnvironment
= serviceEnvironment
;
265 signalAccount
.aciAccountData
.setLocalRegistrationId(registrationId
);
266 signalAccount
.pniAccountData
.setLocalRegistrationId(pniRegistrationId
);
267 signalAccount
.settings
= settings
;
268 signalAccount
.setProvisioningData(number
,
278 signalAccount
.getRecipientTrustedResolver()
279 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
280 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
281 signalAccount
.migrateLegacyConfigs();
282 signalAccount
.clearAllPreKeys();
283 signalAccount
.save();
285 return signalAccount
;
288 public static SignalAccount
createOrUpdateLinkedAccount(
292 ServiceEnvironment serviceEnvironment
,
296 String encryptedDeviceName
,
298 IdentityKeyPair aciIdentityKey
,
299 IdentityKeyPair pniIdentityKey
,
301 int pniRegistrationId
,
302 ProfileKey profileKey
,
303 final Settings settings
304 ) throws IOException
{
305 IOUtils
.createPrivateDirectories(dataPath
);
306 var fileName
= getFileName(dataPath
, accountPath
);
307 if (!fileName
.exists()) {
308 return createLinkedAccount(dataPath
,
325 final var signalAccount
= load(dataPath
, accountPath
, true, settings
);
326 signalAccount
.setProvisioningData(number
,
335 signalAccount
.getRecipientTrustedResolver()
336 .resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
337 signalAccount
.aciAccountData
.getSessionStore().archiveAllSessions();
338 signalAccount
.pniAccountData
.getSessionStore().archiveAllSessions();
339 signalAccount
.getSenderKeyStore().deleteAll();
340 signalAccount
.clearAllPreKeys();
341 return signalAccount
;
344 private void setProvisioningData(
348 final String password
,
349 final String encryptedDeviceName
,
351 final IdentityKeyPair aciIdentity
,
352 final IdentityKeyPair pniIdentity
,
353 final ProfileKey profileKey
355 this.number
= number
;
356 this.aciAccountData
.setServiceId(aci
);
357 this.pniAccountData
.setServiceId(pni
);
358 this.password
= password
;
359 this.profileKey
= profileKey
;
360 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
361 this.encryptedDeviceName
= encryptedDeviceName
;
362 this.deviceId
= deviceId
;
363 this.aciAccountData
.setIdentityKeyPair(aciIdentity
);
364 this.pniAccountData
.setIdentityKeyPair(pniIdentity
);
365 this.registered
= true;
366 this.isMultiDevice
= true;
367 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
368 this.pinMasterKey
= null;
369 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
370 this.setStorageManifest(null);
371 this.storageKey
= null;
372 trustSelfIdentity(ServiceIdType
.ACI
);
373 trustSelfIdentity(ServiceIdType
.PNI
);
376 public void finishRegistration(
379 final MasterKey masterKey
,
381 final PreKeyCollection aciPreKeys
,
382 final PreKeyCollection pniPreKeys
384 this.pinMasterKey
= masterKey
;
385 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
386 this.setStorageManifest(null);
387 this.storageKey
= null;
388 this.encryptedDeviceName
= null;
389 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
390 this.isMultiDevice
= false;
391 this.registered
= true;
392 this.aciAccountData
.setServiceId(aci
);
393 this.pniAccountData
.setServiceId(pni
);
394 this.registrationLockPin
= pin
;
395 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
398 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
399 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
400 aciAccountData
.getSessionStore().archiveAllSessions();
401 pniAccountData
.getSessionStore().archiveAllSessions();
402 getSenderKeyStore().deleteAll();
403 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
404 trustSelfIdentity(ServiceIdType
.ACI
);
405 trustSelfIdentity(ServiceIdType
.PNI
);
408 public void initDatabase() {
409 getAccountDatabase();
412 private void migrateLegacyConfigs() {
413 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
414 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
418 private void mergeRecipients(
419 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
420 ) throws SQLException
{
421 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
422 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
425 public void removeRecipient(final RecipientId recipientId
) {
426 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
427 if (recipientAddress
.matches(getSelfRecipientAddress())) {
428 throw new RuntimeException("Can't delete self recipient");
430 getRecipientStore().deleteRecipientData(recipientId
);
431 getMessageCache().deleteMessages(recipientId
);
432 if (recipientAddress
.serviceId().isPresent()) {
433 final var serviceId
= recipientAddress
.serviceId().get();
434 aciAccountData
.getSessionStore().deleteAllSessions(serviceId
);
435 pniAccountData
.getSessionStore().deleteAllSessions(serviceId
);
436 getIdentityKeyStore().deleteIdentity(serviceId
);
437 getSenderKeyStore().deleteAll(serviceId
);
441 public static File
getFileName(File dataPath
, String account
) {
442 return new File(dataPath
, account
);
445 private static File
getUserPath(final File dataPath
, final String account
) {
446 final var path
= new File(dataPath
, account
+ ".d");
448 IOUtils
.createPrivateDirectories(path
);
449 } catch (IOException e
) {
450 throw new AssertionError("Failed to create user path", e
);
455 private static File
getMessageCachePath(File dataPath
, String account
) {
456 return new File(getUserPath(dataPath
, account
), "msg-cache");
459 private static File
getStorageManifestFile(File dataPath
, String account
) {
460 return new File(getUserPath(dataPath
, account
), "storage-manifest");
463 private static File
getDatabaseFile(File dataPath
, String account
) {
464 return new File(getUserPath(dataPath
, account
), "account.db");
467 public static boolean accountFileExists(File dataPath
, String account
) {
468 if (account
== null) {
471 var f
= getFileName(dataPath
, account
);
472 return !(!f
.exists() || f
.isDirectory());
476 File dataPath
, String accountPath
, final Settings settings
477 ) throws IOException
{
478 this.dataPath
= dataPath
;
479 this.accountPath
= accountPath
;
480 this.settings
= settings
;
481 final JsonNode rootNode
;
482 synchronized (fileChannel
) {
483 fileChannel
.position(0);
484 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
487 var migratedLegacyConfig
= false;
489 if (rootNode
.hasNonNull("version")) {
490 var accountVersion
= rootNode
.get("version").asInt(1);
491 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
492 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
493 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
494 throw new IOException("Config file was created by a no longer supported older version: "
497 previousStorageVersion
= accountVersion
;
498 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
499 migratedLegacyConfig
= true;
503 if (previousStorageVersion
< 8) {
504 final var userPath
= getUserPath(dataPath
, accountPath
);
505 loadLegacyFile(userPath
, rootNode
);
506 migratedLegacyConfig
= true;
508 final var storage
= jsonProcessor
.convertValue(rootNode
, Storage
.class);
509 serviceEnvironment
= ServiceEnvironment
.valueOf(storage
.serviceEnvironment
);
510 registered
= storage
.registered
;
511 number
= storage
.number
;
512 username
= storage
.username
;
513 encryptedDeviceName
= storage
.encryptedDeviceName
;
514 deviceId
= storage
.deviceId
;
515 isMultiDevice
= storage
.isMultiDevice
;
516 password
= storage
.password
;
517 setAccountData(aciAccountData
, storage
.aciAccountData
, ACI
::parseOrThrow
);
518 setAccountData(pniAccountData
, storage
.pniAccountData
, PNI
::parseOrThrow
);
519 registrationLockPin
= storage
.registrationLockPin
;
520 final var base64
= Base64
.getDecoder();
521 if (storage
.pinMasterKey
!= null) {
522 pinMasterKey
= new MasterKey(base64
.decode(storage
.pinMasterKey
));
524 if (storage
.storageKey
!= null) {
525 storageKey
= new StorageKey(base64
.decode(storage
.storageKey
));
527 if (storage
.profileKey
!= null) {
529 profileKey
= new ProfileKey(base64
.decode(storage
.profileKey
));
530 } catch (InvalidInputException e
) {
531 throw new IOException(
532 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
539 if (migratedLegacyConfig
) {
544 private <SERVICE_ID
extends ServiceId
> void setAccountData(
545 AccountData
<SERVICE_ID
> accountData
,
546 Storage
.AccountData storage
,
547 Function
<String
, SERVICE_ID
> serviceIdParser
548 ) throws IOException
{
549 if (storage
.serviceId
!= null) {
551 accountData
.setServiceId(serviceIdParser
.apply(storage
.serviceId
));
552 } catch (IllegalArgumentException e
) {
553 throw new IOException("Config file contains an invalid serviceId, needs to be a valid UUID", e
);
556 accountData
.setLocalRegistrationId(storage
.registrationId
);
557 if (storage
.identityPrivateKey
!= null && storage
.identityPublicKey
!= null) {
558 final var base64
= Base64
.getDecoder();
559 final var publicKeyBytes
= base64
.decode(storage
.identityPublicKey
);
560 final var privateKeyBytes
= base64
.decode(storage
.identityPrivateKey
);
561 final var keyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
562 accountData
.setIdentityKeyPair(keyPair
);
564 accountData
.preKeyMetadata
.nextPreKeyId
= storage
.nextPreKeyId
;
565 accountData
.preKeyMetadata
.nextSignedPreKeyId
= storage
.nextSignedPreKeyId
;
566 accountData
.preKeyMetadata
.activeSignedPreKeyId
= storage
.activeSignedPreKeyId
;
567 accountData
.preKeyMetadata
.nextKyberPreKeyId
= storage
.nextKyberPreKeyId
;
568 accountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= storage
.activeLastResortKyberPreKeyId
;
571 private void loadLegacyFile(final File userPath
, final JsonNode rootNode
) throws IOException
{
572 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
573 if (rootNode
.hasNonNull("password")) {
574 password
= rootNode
.get("password").asText();
576 if (password
== null) {
577 password
= KeyUtils
.createPassword();
580 if (rootNode
.hasNonNull("serviceEnvironment")) {
581 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
583 if (serviceEnvironment
== null) {
584 serviceEnvironment
= ServiceEnvironment
.LIVE
;
586 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
587 if (rootNode
.hasNonNull("usernameIdentifier")) {
588 username
= rootNode
.get("usernameIdentifier").asText();
590 if (rootNode
.hasNonNull("uuid")) {
592 aciAccountData
.setServiceId(ACI
.parseOrThrow(rootNode
.get("uuid").asText()));
593 } catch (IllegalArgumentException e
) {
594 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
597 if (rootNode
.hasNonNull("pni")) {
599 pniAccountData
.setServiceId(PNI
.parseOrThrow(rootNode
.get("pni").asText()));
600 } catch (IllegalArgumentException e
) {
601 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
604 if (rootNode
.hasNonNull("sessionId")) {
605 getKeyValueStore().storeEntry(verificationSessionId
, rootNode
.get("sessionId").asText());
607 if (rootNode
.hasNonNull("sessionNumber")) {
608 getKeyValueStore().storeEntry(verificationSessionNumber
, rootNode
.get("sessionNumber").asText());
610 if (rootNode
.hasNonNull("deviceName")) {
611 encryptedDeviceName
= rootNode
.get("deviceName").asText();
613 if (rootNode
.hasNonNull("deviceId")) {
614 deviceId
= rootNode
.get("deviceId").asInt();
616 if (rootNode
.hasNonNull("isMultiDevice")) {
617 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
619 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
620 getKeyValueStore().storeEntry(lastReceiveTimestamp
, rootNode
.get("lastReceiveTimestamp").asLong());
622 int registrationId
= 0;
623 if (rootNode
.hasNonNull("registrationId")) {
624 registrationId
= rootNode
.get("registrationId").asInt();
626 if (rootNode
.hasNonNull("pniRegistrationId")) {
627 pniAccountData
.setLocalRegistrationId(rootNode
.get("pniRegistrationId").asInt());
629 pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
631 IdentityKeyPair aciIdentityKeyPair
= null;
632 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
633 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
634 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
635 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
637 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
638 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
639 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
640 pniAccountData
.setIdentityKeyPair(KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
));
643 if (rootNode
.hasNonNull("registrationLockPin")) {
644 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
646 if (rootNode
.hasNonNull("pinMasterKey")) {
647 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
649 if (rootNode
.hasNonNull("storageKey")) {
650 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
652 if (rootNode
.hasNonNull("storageManifestVersion")) {
653 getKeyValueStore().storeEntry(storageManifestVersion
, rootNode
.get("storageManifestVersion").asLong());
655 if (rootNode
.hasNonNull("preKeyIdOffset")) {
656 aciAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("preKeyIdOffset").asInt(1);
658 aciAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
660 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
661 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
663 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
665 if (rootNode
.hasNonNull("activeSignedPreKeyId")) {
666 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("activeSignedPreKeyId").asInt(-1);
668 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
670 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
671 pniAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
673 pniAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
675 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
676 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
678 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
680 if (rootNode
.hasNonNull("pniActiveSignedPreKeyId")) {
681 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("pniActiveSignedPreKeyId").asInt(-1);
683 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
685 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
686 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
688 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
690 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
691 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId")
694 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
696 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
697 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
699 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
701 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
702 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get(
703 "pniActiveLastResortKyberPreKeyId").asInt(-1);
705 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
707 if (rootNode
.hasNonNull("profileKey")) {
709 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
710 } catch (InvalidInputException e
) {
711 throw new IOException(
712 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
716 if (profileKey
== null) {
717 // Old config file, creating new profile key
718 setProfileKey(KeyUtils
.createProfileKey());
720 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
722 if (previousStorageVersion
< 5) {
723 final var legacyRecipientsStoreFile
= new File(userPath
, "recipients-store");
724 if (legacyRecipientsStoreFile
.exists()) {
725 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
726 // Ensure our profile key is stored in profile store
727 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
730 if (previousStorageVersion
< 6) {
731 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
733 final var legacyAciPreKeysPath
= new File(userPath
, "pre-keys");
734 if (legacyAciPreKeysPath
.exists()) {
735 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, aciAccountData
.getPreKeyStore());
737 final var legacyPniPreKeysPath
= new File(userPath
, "pre-keys-pni");
738 if (legacyPniPreKeysPath
.exists()) {
739 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, pniAccountData
.getPreKeyStore());
741 final var legacyAciSignedPreKeysPath
= new File(userPath
, "signed-pre-keys");
742 if (legacyAciSignedPreKeysPath
.exists()) {
743 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, aciAccountData
.getSignedPreKeyStore());
745 final var legacyPniSignedPreKeysPath
= new File(userPath
, "signed-pre-keys-pni");
746 if (legacyPniSignedPreKeysPath
.exists()) {
747 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, pniAccountData
.getSignedPreKeyStore());
749 final var legacySessionsPath
= new File(userPath
, "sessions");
750 if (legacySessionsPath
.exists()) {
751 LegacySessionStore
.migrate(legacySessionsPath
,
752 getRecipientResolver(),
753 getRecipientAddressResolver(),
754 aciAccountData
.getSessionStore());
756 final var legacyIdentitiesPath
= new File(userPath
, "identities");
757 if (legacyIdentitiesPath
.exists()) {
758 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
759 getRecipientResolver(),
760 getRecipientAddressResolver(),
761 getIdentityKeyStore());
763 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
764 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
765 LegacyJsonSignalProtocolStore
.class)
767 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
768 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
769 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
772 this.aciAccountData
.setIdentityKeyPair(aciIdentityKeyPair
);
773 this.aciAccountData
.setLocalRegistrationId(registrationId
);
775 loadLegacyStores(rootNode
, legacySignalProtocolStore
);
777 final var legacySenderKeysPath
= new File(userPath
, "sender-keys");
778 if (legacySenderKeysPath
.exists()) {
779 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
780 getRecipientResolver(),
781 getRecipientAddressResolver(),
782 getSenderKeyStore());
784 final var legacySenderKeysSharedPath
= new File(userPath
, "shared-sender-keys-store");
785 if (legacySenderKeysSharedPath
.exists()) {
786 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
787 getRecipientResolver(),
788 getRecipientAddressResolver(),
789 getSenderKeyStore());
791 if (rootNode
.hasNonNull("groupStore")) {
792 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
793 LegacyGroupStore
.Storage
.class);
794 LegacyGroupStore
.migrate(groupStoreStorage
,
795 new File(userPath
, "group-cache"),
796 getRecipientResolver(),
800 if (rootNode
.hasNonNull("stickerStore")) {
801 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
802 LegacyStickerStore
.Storage
.class);
803 LegacyStickerStore
.migrate(storage
, getStickerStore());
806 if (rootNode
.hasNonNull("configurationStore")) {
807 final var configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
808 LegacyConfigurationStore
.Storage
.class);
809 LegacyConfigurationStore
.migrate(configurationStoreStorage
, getConfigurationStore());
812 loadLegacyThreadStore(rootNode
);
815 private void loadLegacyStores(
816 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
818 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
819 if (legacyRecipientStoreNode
!= null) {
820 logger
.debug("Migrating legacy recipient store.");
821 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
822 if (legacyRecipientStore
!= null) {
823 legacyRecipientStore
.getAddresses()
824 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
826 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
829 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
830 logger
.debug("Migrating legacy pre key store.");
831 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
833 aciAccountData
.getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
834 } catch (InvalidMessageException e
) {
835 logger
.warn("Failed to migrate pre key, ignoring", e
);
840 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
841 logger
.debug("Migrating legacy signed pre key store.");
842 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
844 aciAccountData
.getSignedPreKeyStore()
845 .storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
846 } catch (InvalidMessageException e
) {
847 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
852 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
853 logger
.debug("Migrating legacy session store.");
854 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
856 aciAccountData
.getSessionStore()
857 .storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(), session
.deviceId
),
858 new SessionRecord(session
.sessionRecord
));
859 } catch (Exception e
) {
860 logger
.warn("Failed to migrate session, ignoring", e
);
865 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
866 logger
.debug("Migrating legacy identity session store.");
867 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
868 if (identity
.getAddress().serviceId().isEmpty()) {
871 final var serviceId
= identity
.getAddress().serviceId().get();
872 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
873 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
874 identity
.getIdentityKey(),
875 identity
.getTrustLevel());
879 if (rootNode
.hasNonNull("contactStore")) {
880 logger
.debug("Migrating legacy contact store.");
881 final var contactStoreNode
= rootNode
.get("contactStore");
882 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
883 for (var contact
: contactStore
.getContacts()) {
884 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
885 getContactStore().storeContact(recipientId
,
886 new Contact(contact
.name
,
889 contact
.messageExpirationTime
,
894 // Store profile keys only in profile store
895 var profileKeyString
= contact
.profileKey
;
896 if (profileKeyString
!= null) {
897 final ProfileKey profileKey
;
899 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
900 getProfileStore().storeProfileKey(recipientId
, profileKey
);
901 } catch (InvalidInputException e
) {
902 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
908 if (rootNode
.hasNonNull("profileStore")) {
909 logger
.debug("Migrating legacy profile store.");
910 var profileStoreNode
= rootNode
.get("profileStore");
911 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
912 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
913 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.address());
914 // Not migrating profile key credential here, it was changed to expiring profile key credentials
915 getProfileStore().storeProfileKey(recipientId
, profileEntry
.profileKey());
916 final var profile
= profileEntry
.profile();
917 if (profile
!= null) {
918 final var capabilities
= new HashSet
<Profile
.Capability
>();
919 if (profile
.getCapabilities() != null) {
920 if (profile
.getCapabilities().gv1Migration
) {
921 capabilities
.add(Profile
.Capability
.gv1Migration
);
923 if (profile
.getCapabilities().storage
) {
924 capabilities
.add(Profile
.Capability
.storage
);
927 final var newProfile
= new Profile(profileEntry
.lastUpdateTimestamp(),
928 profile
.getGivenName(),
929 profile
.getFamilyName(),
931 profile
.getAboutEmoji(),
934 profile
.isUnrestrictedUnidentifiedAccess()
935 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
936 : profile
.getUnidentifiedAccess() != null
937 ? Profile
.UnidentifiedAccessMode
.ENABLED
938 : Profile
.UnidentifiedAccessMode
.DISABLED
,
940 getProfileStore().storeProfile(recipientId
, newProfile
);
946 private void loadLegacyThreadStore(final JsonNode rootNode
) {
947 var threadStoreNode
= rootNode
.get("threadStore");
948 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
949 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
950 // Migrate thread info to group and contact store
951 for (var thread
: threadStore
.getThreads()) {
952 if (thread
.id
== null || thread
.id
.isEmpty()) {
956 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
957 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
958 var contact
= getContactStore().getContact(recipientId
);
959 if (contact
!= null) {
960 getContactStore().storeContact(recipientId
,
961 Contact
.newBuilder(contact
)
962 .withMessageExpirationTime(thread
.messageExpirationTime
)
966 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
967 if (groupInfo
instanceof GroupInfoV1
) {
968 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
969 getGroupStore().updateGroup(groupInfo
);
972 } catch (Exception e
) {
973 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
979 private void save() {
980 synchronized (fileChannel
) {
981 final var base64
= Base64
.getEncoder();
982 final var storage
= new Storage(CURRENT_STORAGE_VERSION
,
983 serviceEnvironment
.name(),
991 Storage
.AccountData
.from(aciAccountData
),
992 Storage
.AccountData
.from(pniAccountData
),
994 pinMasterKey
== null ?
null : base64
.encodeToString(pinMasterKey
.serialize()),
995 storageKey
== null ?
null : base64
.encodeToString(storageKey
.serialize()),
996 profileKey
== null ?
null : base64
.encodeToString(profileKey
.serialize()));
998 try (var output
= new ByteArrayOutputStream()) {
999 // Write to memory first to prevent corrupting the file in case of serialization errors
1000 jsonProcessor
.writeValue(output
, storage
);
1001 var input
= new ByteArrayInputStream(output
.toByteArray());
1002 fileChannel
.position(0);
1003 input
.transferTo(Channels
.newOutputStream(fileChannel
));
1004 fileChannel
.truncate(fileChannel
.position());
1005 fileChannel
.force(false);
1007 } catch (Exception e
) {
1008 logger
.error("Error saving file: {}", e
.getMessage(), e
);
1013 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
1014 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
1016 var lock
= fileChannel
.tryLock();
1019 logger
.debug("Config file is in use by another instance.");
1020 throw new IOException("Config file is in use by another instance.");
1022 logger
.info("Config file is in use by another instance, waiting…");
1023 lock
= fileChannel
.lock();
1024 logger
.info("Config file lock acquired.");
1026 final var result
= new Pair
<>(fileChannel
, lock
);
1030 if (fileChannel
!= null) {
1031 fileChannel
.close();
1036 private void clearAllPreKeys() {
1037 clearAllPreKeys(ServiceIdType
.ACI
);
1038 clearAllPreKeys(ServiceIdType
.PNI
);
1041 private void clearAllPreKeys(ServiceIdType serviceIdType
) {
1042 final var accountData
= getAccountData(serviceIdType
);
1043 resetPreKeyOffsets(serviceIdType
);
1044 resetKyberPreKeyOffsets(serviceIdType
);
1045 accountData
.getPreKeyStore().removeAllPreKeys();
1046 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1047 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1051 private void setPreKeys(ServiceIdType serviceIdType
, PreKeyCollection preKeyCollection
) {
1052 final var accountData
= getAccountData(serviceIdType
);
1053 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1054 preKeyMetadata
.nextSignedPreKeyId
= preKeyCollection
.getSignedPreKey().getId();
1055 preKeyMetadata
.nextKyberPreKeyId
= preKeyCollection
.getLastResortKyberPreKey().getId();
1057 accountData
.getPreKeyStore().removeAllPreKeys();
1058 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1059 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1061 addSignedPreKey(serviceIdType
, preKeyCollection
.getSignedPreKey());
1062 addLastResortKyberPreKey(serviceIdType
, preKeyCollection
.getLastResortKyberPreKey());
1067 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1068 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1069 preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
1070 preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
1071 preKeyMetadata
.activeSignedPreKeyId
= -1;
1075 private static int getRandomPreKeyIdOffset() {
1076 return KeyUtils
.getRandomInt(PREKEY_MAXIMUM_ID
);
1079 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1080 final var accountData
= getAccountData(serviceIdType
);
1081 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1082 logger
.debug("Adding {} {} pre keys with offset {}",
1085 preKeyMetadata
.nextPreKeyId
);
1086 accountData
.getSignalServiceAccountDataStore()
1087 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1088 for (var record : records
) {
1089 if (preKeyMetadata
.nextPreKeyId
!= record.getId()) {
1090 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata
.nextPreKeyId
);
1091 throw new AssertionError("Invalid pre key id");
1093 accountData
.getPreKeyStore().storePreKey(record.getId(), record);
1094 preKeyMetadata
.nextPreKeyId
= (preKeyMetadata
.nextPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1099 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1100 final var accountData
= getAccountData(serviceIdType
);
1101 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1102 logger
.debug("Adding {} signed pre key with offset {}", serviceIdType
, preKeyMetadata
.nextSignedPreKeyId
);
1103 if (preKeyMetadata
.nextSignedPreKeyId
!= record.getId()) {
1104 logger
.error("Invalid signed pre key id {}, expected {}",
1106 preKeyMetadata
.nextSignedPreKeyId
);
1107 throw new AssertionError("Invalid signed pre key id");
1109 accountData
.getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1110 preKeyMetadata
.nextSignedPreKeyId
= (preKeyMetadata
.nextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1111 preKeyMetadata
.activeSignedPreKeyId
= record.getId();
1115 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1116 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1117 preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
1118 preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
1122 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1123 final var accountData
= getAccountData(serviceIdType
);
1124 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1125 logger
.debug("Adding {} {} kyber pre keys with offset {}",
1128 preKeyMetadata
.nextKyberPreKeyId
);
1129 accountData
.getSignalServiceAccountDataStore()
1130 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1131 for (var record : records
) {
1132 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1133 logger
.error("Invalid kyber pre key id {}, expected {}",
1135 preKeyMetadata
.nextKyberPreKeyId
);
1136 throw new AssertionError("Invalid kyber pre key id");
1138 accountData
.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1139 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1144 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1145 final var accountData
= getAccountData(serviceIdType
);
1146 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1147 logger
.debug("Adding {} last resort kyber pre key with offset {}",
1149 preKeyMetadata
.nextKyberPreKeyId
);
1150 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1151 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1153 preKeyMetadata
.nextKyberPreKeyId
);
1154 throw new AssertionError("Invalid last resort kyber pre key id");
1156 accountData
.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1157 preKeyMetadata
.activeLastResortKyberPreKeyId
= record.getId();
1158 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1162 public int getPreviousStorageVersion() {
1163 return previousStorageVersion
;
1166 public AccountData
<?
extends ServiceId
> getAccountData(ServiceIdType serviceIdType
) {
1167 return switch (serviceIdType
) {
1168 case ACI
-> aciAccountData
;
1169 case PNI
-> pniAccountData
;
1173 public AccountData
<?
extends ServiceId
> getAccountData(ServiceId accountIdentifier
) {
1174 if (accountIdentifier
.equals(aciAccountData
.getServiceId())) {
1175 return aciAccountData
;
1176 } else if (accountIdentifier
.equals(pniAccountData
.getServiceId())) {
1177 return pniAccountData
;
1179 throw new IllegalArgumentException("No matching account data found for " + accountIdentifier
);
1183 public SignalServiceDataStore
getSignalServiceDataStore() {
1184 return new SignalServiceDataStore() {
1186 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1187 return getAccountData(accountIdentifier
).getSignalServiceAccountDataStore();
1191 public SignalServiceAccountDataStore
aci() {
1192 return aciAccountData
.getSignalServiceAccountDataStore();
1196 public SignalServiceAccountDataStore
pni() {
1197 return pniAccountData
.getSignalServiceAccountDataStore();
1201 public boolean isMultiDevice() {
1202 return SignalAccount
.this.isMultiDevice();
1207 public IdentityKeyStore
getIdentityKeyStore() {
1208 return getOrCreate(() -> identityKeyStore
,
1209 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(), settings
.trustNewIdentity()));
1212 public GroupStore
getGroupStore() {
1213 return getOrCreate(() -> groupStore
,
1214 () -> groupStore
= new GroupStore(getAccountDatabase(),
1215 getRecipientResolver(),
1216 getRecipientIdCreator()));
1219 public ContactsStore
getContactStore() {
1220 return getRecipientStore();
1223 private RecipientIdCreator
getRecipientIdCreator() {
1224 return recipientId
-> getRecipientStore().create(recipientId
);
1227 public RecipientResolver
getRecipientResolver() {
1228 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1231 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1232 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1235 public RecipientAddressResolver
getRecipientAddressResolver() {
1236 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1239 public RecipientStore
getRecipientStore() {
1240 return getOrCreate(() -> recipientStore
,
1241 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1242 this::getSelfRecipientAddress
,
1243 getAccountDatabase()));
1246 public ProfileStore
getProfileStore() {
1247 return getRecipientStore();
1250 public StickerStore
getStickerStore() {
1251 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1254 public SenderKeyStore
getSenderKeyStore() {
1255 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1258 private KeyValueStore
getKeyValueStore() {
1259 return getOrCreate(() -> keyValueStore
, () -> keyValueStore
= new KeyValueStore(getAccountDatabase()));
1262 public ConfigurationStore
getConfigurationStore() {
1263 return getOrCreate(() -> configurationStore
,
1264 () -> configurationStore
= new ConfigurationStore(getKeyValueStore()));
1267 public MessageCache
getMessageCache() {
1268 return getOrCreate(() -> messageCache
,
1269 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1272 public AccountDatabase
getAccountDatabase() {
1273 return getOrCreate(() -> accountDatabase
, () -> {
1275 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1276 } catch (SQLException e
) {
1277 throw new RuntimeException(e
);
1282 public MessageSendLogStore
getMessageSendLogStore() {
1283 return getOrCreate(() -> messageSendLogStore
,
1284 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1285 settings
.disableMessageSendLog()));
1288 public CredentialsProvider
getCredentialsProvider() {
1289 return new CredentialsProvider() {
1291 public ACI
getAci() {
1292 return aciAccountData
.getServiceId();
1296 public PNI
getPni() {
1297 return pniAccountData
.getServiceId();
1301 public String
getE164() {
1306 public String
getPassword() {
1311 public int getDeviceId() {
1317 public String
getNumber() {
1321 public void setNumber(final String number
) {
1322 this.number
= number
;
1326 public String
getUsername() {
1330 public void setUsername(final String username
) {
1331 this.username
= username
;
1335 public ServiceEnvironment
getServiceEnvironment() {
1336 return serviceEnvironment
;
1339 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1340 this.serviceEnvironment
= serviceEnvironment
;
1344 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1345 return new AccountAttributes(null,
1346 aciAccountData
.getLocalRegistrationId(),
1350 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1351 getSelfUnidentifiedAccessKey(),
1352 isUnrestrictedUnidentifiedAccess(),
1353 isDiscoverableByPhoneNumber(),
1354 getAccountCapabilities(),
1355 encryptedDeviceName
,
1356 pniAccountData
.getLocalRegistrationId(),
1357 null); // TODO recoveryPassword?
1360 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1361 return getCapabilities(isPrimaryDevice());
1364 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1365 return getAccountData(serviceIdType
).getServiceId();
1368 public ACI
getAci() {
1369 return aciAccountData
.getServiceId();
1372 public void setAci(final ACI aci
) {
1373 this.aciAccountData
.setServiceId(aci
);
1377 public PNI
getPni() {
1378 return pniAccountData
.getServiceId();
1381 public void setPni(final PNI updatedPni
) {
1382 final var oldPni
= pniAccountData
.getServiceId();
1383 if (oldPni
!= null && !oldPni
.equals(updatedPni
)) {
1384 // Clear data for old PNI
1385 identityKeyStore
.deleteIdentity(oldPni
);
1388 this.pniAccountData
.setServiceId(updatedPni
);
1389 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1390 trustSelfIdentity(ServiceIdType
.PNI
);
1394 public void setNewPniIdentity(
1395 final IdentityKeyPair pniIdentityKeyPair
,
1396 final SignedPreKeyRecord pniSignedPreKey
,
1397 final KyberPreKeyRecord lastResortKyberPreKey
,
1398 final int localPniRegistrationId
1400 setPniIdentityKeyPair(pniIdentityKeyPair
);
1401 pniAccountData
.setLocalRegistrationId(localPniRegistrationId
);
1403 final AccountData
<?
extends ServiceId
> accountData
= getAccountData(ServiceIdType
.PNI
);
1404 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1405 preKeyMetadata
.nextSignedPreKeyId
= pniSignedPreKey
.getId();
1406 accountData
.getSignedPreKeyStore().removeSignedPreKey(pniSignedPreKey
.getId());
1407 addSignedPreKey(ServiceIdType
.PNI
, pniSignedPreKey
);
1408 if (lastResortKyberPreKey
!= null) {
1409 preKeyMetadata
.nextKyberPreKeyId
= lastResortKyberPreKey
.getId();
1410 accountData
.getKyberPreKeyStore().removeKyberPreKey(lastResortKyberPreKey
.getId());
1411 addLastResortKyberPreKey(ServiceIdType
.PNI
, lastResortKyberPreKey
);
1416 public SignalServiceAddress
getSelfAddress() {
1417 return new SignalServiceAddress(getAci(), number
);
1420 public RecipientAddress
getSelfRecipientAddress() {
1421 return new RecipientAddress(getAci(), getPni(), number
, username
);
1424 public RecipientId
getSelfRecipientId() {
1425 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1428 public String
getSessionId(final String forNumber
) {
1429 final var keyValueStore
= getKeyValueStore();
1430 final var sessionNumber
= keyValueStore
.getEntry(verificationSessionNumber
);
1431 if (!forNumber
.equals(sessionNumber
)) {
1434 return keyValueStore
.getEntry(verificationSessionId
);
1437 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1438 final var keyValueStore
= getKeyValueStore();
1439 keyValueStore
.storeEntry(verificationSessionNumber
, sessionNumber
);
1440 keyValueStore
.storeEntry(verificationSessionId
, sessionId
);
1443 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1444 this.encryptedDeviceName
= encryptedDeviceName
;
1448 public int getDeviceId() {
1452 public boolean isPrimaryDevice() {
1453 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1456 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1457 return getAccountData(serviceIdType
).getIdentityKeyPair();
1460 public IdentityKeyPair
getAciIdentityKeyPair() {
1461 return aciAccountData
.getIdentityKeyPair();
1464 public IdentityKeyPair
getPniIdentityKeyPair() {
1465 return pniAccountData
.getIdentityKeyPair();
1468 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1469 pniAccountData
.setIdentityKeyPair(identityKeyPair
);
1470 trustSelfIdentity(ServiceIdType
.PNI
);
1474 public String
getPassword() {
1478 public void setRegistrationLockPin(final String registrationLockPin
) {
1479 this.registrationLockPin
= registrationLockPin
;
1483 public String
getRegistrationLockPin() {
1484 return registrationLockPin
;
1487 public String
getRegistrationLock() {
1488 final var masterKey
= getPinBackedMasterKey();
1489 if (masterKey
== null) {
1492 return masterKey
.deriveRegistrationLock();
1495 public MasterKey
getPinBackedMasterKey() {
1496 if (registrationLockPin
== null) {
1499 return pinMasterKey
;
1502 public MasterKey
getOrCreatePinMasterKey() {
1503 if (pinMasterKey
== null) {
1504 pinMasterKey
= KeyUtils
.createMasterKey();
1507 return pinMasterKey
;
1510 public StorageKey
getStorageKey() {
1511 if (pinMasterKey
!= null) {
1512 return pinMasterKey
.deriveStorageServiceKey();
1517 public StorageKey
getOrCreateStorageKey() {
1518 if (isPrimaryDevice()) {
1519 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1524 public void setStorageKey(final StorageKey storageKey
) {
1525 if (storageKey
.equals(this.storageKey
)) {
1528 this.storageKey
= storageKey
;
1532 public long getStorageManifestVersion() {
1533 return getKeyValueStore().getEntry(storageManifestVersion
);
1536 public void setStorageManifestVersion(final long value
) {
1537 getKeyValueStore().storeEntry(storageManifestVersion
, value
);
1540 public Optional
<SignalStorageManifest
> getStorageManifest() {
1541 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1542 if (!storageManifestFile
.exists()) {
1543 return Optional
.empty();
1545 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1546 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1547 } catch (IOException e
) {
1548 logger
.warn("Failed to read local storage manifest.", e
);
1549 return Optional
.empty();
1553 public void setStorageManifest(SignalStorageManifest manifest
) {
1554 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1555 if (manifest
== null) {
1556 if (storageManifestFile
.exists()) {
1558 Files
.delete(storageManifestFile
.toPath());
1559 } catch (IOException e
) {
1560 logger
.error("Failed to delete local storage manifest.", e
);
1566 final var manifestBytes
= manifest
.serialize();
1567 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1568 outputStream
.write(manifestBytes
);
1569 } catch (IOException e
) {
1570 logger
.error("Failed to store local storage manifest.", e
);
1574 public ProfileKey
getProfileKey() {
1578 public void setProfileKey(final ProfileKey profileKey
) {
1579 if (profileKey
.equals(this.profileKey
)) {
1582 this.profileKey
= profileKey
;
1584 getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
1587 public byte[] getSelfUnidentifiedAccessKey() {
1588 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1591 public boolean isRegistered() {
1595 public void setRegistered(final boolean registered
) {
1596 this.registered
= registered
;
1600 public boolean isMultiDevice() {
1601 return isMultiDevice
;
1604 public void setMultiDevice(final boolean multiDevice
) {
1605 if (isMultiDevice
== multiDevice
) {
1608 isMultiDevice
= multiDevice
;
1612 public long getLastReceiveTimestamp() {
1613 return getKeyValueStore().getEntry(lastReceiveTimestamp
);
1616 public void setLastReceiveTimestamp(final long value
) {
1617 getKeyValueStore().storeEntry(lastReceiveTimestamp
, value
);
1620 public boolean isUnrestrictedUnidentifiedAccess() {
1621 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1622 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1625 public boolean isDiscoverableByPhoneNumber() {
1626 final var phoneNumberUnlisted
= getConfigurationStore().getPhoneNumberUnlisted();
1627 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1630 private void trustSelfIdentity(ServiceIdType serviceIdType
) {
1631 final var accountData
= getAccountData(serviceIdType
);
1632 final var serviceId
= accountData
.getServiceId();
1633 final var identityKeyPair
= accountData
.getIdentityKeyPair();
1634 if (serviceId
== null || identityKeyPair
== null) {
1637 final var publicKey
= identityKeyPair
.getPublicKey();
1638 getIdentityKeyStore().saveIdentity(serviceId
, publicKey
);
1639 getIdentityKeyStore().setIdentityTrustLevel(serviceId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1642 public void deleteAccountData() throws IOException
{
1644 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1645 .sorted(Comparator
.reverseOrder())) {
1646 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1647 Files
.delete(file
.next());
1650 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1654 public void close() {
1655 synchronized (fileChannel
) {
1656 if (accountDatabase
!= null) {
1658 accountDatabase
.close();
1659 } catch (SQLException e
) {
1660 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1663 if (messageSendLogStore
!= null) {
1664 messageSendLogStore
.close();
1669 } catch (ClosedChannelException ignored
) {
1671 fileChannel
.close();
1672 } catch (IOException e
) {
1673 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1678 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1679 var value
= supplier
.get();
1680 if (value
!= null) {
1684 synchronized (LOCK
) {
1685 value
= supplier
.get();
1686 if (value
!= null) {
1690 return supplier
.get();
1694 private interface Callable
{
1699 public static class PreKeyMetadata
{
1701 private int nextPreKeyId
= 1;
1702 private int nextSignedPreKeyId
= 1;
1703 private int activeSignedPreKeyId
= -1;
1704 private int nextKyberPreKeyId
= 1;
1705 private int activeLastResortKyberPreKeyId
= -1;
1707 public int getNextPreKeyId() {
1708 return nextPreKeyId
;
1711 public int getNextSignedPreKeyId() {
1712 return nextSignedPreKeyId
;
1715 public int getActiveSignedPreKeyId() {
1716 return activeSignedPreKeyId
;
1719 public int getNextKyberPreKeyId() {
1720 return nextKyberPreKeyId
;
1723 public int getActiveLastResortKyberPreKeyId() {
1724 return activeLastResortKyberPreKeyId
;
1728 public class AccountData
<SERVICE_ID
extends ServiceId
> {
1730 private final ServiceIdType serviceIdType
;
1731 private SERVICE_ID serviceId
;
1732 private IdentityKeyPair identityKeyPair
;
1733 private int localRegistrationId
;
1734 private final PreKeyMetadata preKeyMetadata
= new PreKeyMetadata();
1736 private SignalProtocolStore signalProtocolStore
;
1737 private PreKeyStore preKeyStore
;
1738 private SignedPreKeyStore signedPreKeyStore
;
1739 private KyberPreKeyStore kyberPreKeyStore
;
1740 private SessionStore sessionStore
;
1741 private SignalIdentityKeyStore identityKeyStore
;
1743 private AccountData(final ServiceIdType serviceIdType
) {
1744 this.serviceIdType
= serviceIdType
;
1747 public SERVICE_ID
getServiceId() {
1751 private void setServiceId(final SERVICE_ID serviceId
) {
1752 this.serviceId
= serviceId
;
1755 public IdentityKeyPair
getIdentityKeyPair() {
1756 return identityKeyPair
;
1759 private void setIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1760 this.identityKeyPair
= identityKeyPair
;
1763 public int getLocalRegistrationId() {
1764 return localRegistrationId
;
1767 private void setLocalRegistrationId(final int localRegistrationId
) {
1768 this.localRegistrationId
= localRegistrationId
;
1769 this.identityKeyStore
= null;
1772 public PreKeyMetadata
getPreKeyMetadata() {
1773 return preKeyMetadata
;
1776 private SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1777 return getOrCreate(() -> signalProtocolStore
,
1778 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
1779 getSignedPreKeyStore(),
1780 getKyberPreKeyStore(),
1782 getIdentityKeyStore(),
1783 getSenderKeyStore(),
1784 SignalAccount
.this::isMultiDevice
));
1787 public PreKeyStore
getPreKeyStore() {
1788 return getOrCreate(() -> preKeyStore
,
1789 () -> preKeyStore
= new PreKeyStore(getAccountDatabase(), serviceIdType
));
1792 public SignedPreKeyStore
getSignedPreKeyStore() {
1793 return getOrCreate(() -> signedPreKeyStore
,
1794 () -> signedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), serviceIdType
));
1797 public KyberPreKeyStore
getKyberPreKeyStore() {
1798 return getOrCreate(() -> kyberPreKeyStore
,
1799 () -> kyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), serviceIdType
));
1802 public SessionStore
getSessionStore() {
1803 return getOrCreate(() -> sessionStore
,
1804 () -> sessionStore
= new SessionStore(getAccountDatabase(), serviceIdType
));
1807 public SignalIdentityKeyStore
getIdentityKeyStore() {
1808 return getOrCreate(() -> identityKeyStore
,
1809 () -> identityKeyStore
= new SignalIdentityKeyStore(() -> identityKeyPair
,
1810 localRegistrationId
,
1811 SignalAccount
.this.getIdentityKeyStore()));
1815 public record Storage(
1817 String serviceEnvironment
,
1821 String encryptedDeviceName
,
1823 boolean isMultiDevice
,
1825 AccountData aciAccountData
,
1826 AccountData pniAccountData
,
1827 String registrationLockPin
,
1828 String pinMasterKey
,
1833 public record AccountData(
1836 String identityPrivateKey
,
1837 String identityPublicKey
,
1840 int nextSignedPreKeyId
,
1841 int activeSignedPreKeyId
,
1842 int nextKyberPreKeyId
,
1843 int activeLastResortKyberPreKeyId
1846 private static AccountData
from(final SignalAccount
.AccountData
<?
> accountData
) {
1847 final var base64
= Base64
.getEncoder();
1848 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1849 return new AccountData(accountData
.getServiceId() == null
1851 : accountData
.getServiceId().toString(),
1852 accountData
.getLocalRegistrationId(),
1853 accountData
.getIdentityKeyPair() == null
1855 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPrivateKey().serialize()),
1856 accountData
.getIdentityKeyPair() == null
1858 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPublicKey().serialize()),
1859 preKeyMetadata
.getNextPreKeyId(),
1860 preKeyMetadata
.getNextSignedPreKeyId(),
1861 preKeyMetadata
.getActiveSignedPreKeyId(),
1862 preKeyMetadata
.getNextKyberPreKeyId(),
1863 preKeyMetadata
.getActiveLastResortKyberPreKeyId());