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
.CdsiStore
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore2
;
39 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
40 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
41 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientIdCreator
;
42 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
43 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
44 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientTrustedResolver
;
45 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
46 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeyRecordStore
;
47 import org
.asamk
.signal
.manager
.storage
.senderKeys
.LegacySenderKeySharedStore
;
48 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
49 import org
.asamk
.signal
.manager
.storage
.sessions
.LegacySessionStore
;
50 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
51 import org
.asamk
.signal
.manager
.storage
.stickers
.LegacyStickerStore
;
52 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
53 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
54 import org
.asamk
.signal
.manager
.util
.IOUtils
;
55 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
56 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
57 import org
.signal
.libsignal
.protocol
.InvalidMessageException
;
58 import org
.signal
.libsignal
.protocol
.SignalProtocolAddress
;
59 import org
.signal
.libsignal
.protocol
.state
.KyberPreKeyRecord
;
60 import org
.signal
.libsignal
.protocol
.state
.PreKeyRecord
;
61 import org
.signal
.libsignal
.protocol
.state
.SessionRecord
;
62 import org
.signal
.libsignal
.protocol
.state
.SignedPreKeyRecord
;
63 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
64 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
65 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
66 import org
.slf4j
.Logger
;
67 import org
.slf4j
.LoggerFactory
;
68 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
69 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
70 import org
.whispersystems
.signalservice
.api
.account
.AccountAttributes
;
71 import org
.whispersystems
.signalservice
.api
.account
.PreKeyCollection
;
72 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
73 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
74 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
75 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.ACI
;
76 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.PNI
;
77 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
78 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
79 import org
.whispersystems
.signalservice
.api
.push
.UsernameLinkComponents
;
80 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
81 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
82 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
83 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
85 import java
.io
.ByteArrayInputStream
;
86 import java
.io
.ByteArrayOutputStream
;
87 import java
.io
.Closeable
;
89 import java
.io
.FileInputStream
;
90 import java
.io
.FileOutputStream
;
91 import java
.io
.IOException
;
92 import java
.io
.RandomAccessFile
;
93 import java
.nio
.channels
.Channels
;
94 import java
.nio
.channels
.ClosedChannelException
;
95 import java
.nio
.channels
.FileChannel
;
96 import java
.nio
.channels
.FileLock
;
97 import java
.nio
.file
.Files
;
98 import java
.sql
.Connection
;
99 import java
.sql
.SQLException
;
100 import java
.util
.Base64
;
101 import java
.util
.Comparator
;
102 import java
.util
.HashSet
;
103 import java
.util
.List
;
104 import java
.util
.Optional
;
105 import java
.util
.function
.Function
;
106 import java
.util
.function
.Supplier
;
108 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.PREKEY_MAXIMUM_ID
;
109 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.getCapabilities
;
111 public class SignalAccount
implements Closeable
{
113 private static final Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
115 private static final int MINIMUM_STORAGE_VERSION
= 1;
116 private static final int CURRENT_STORAGE_VERSION
= 8;
118 private final Object LOCK
= new Object();
120 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
122 private final FileChannel fileChannel
;
123 private final FileLock lock
;
125 private int previousStorageVersion
;
127 private File dataPath
;
128 private String accountPath
;
130 private ServiceEnvironment serviceEnvironment
;
131 private String number
;
132 private String username
;
133 private UsernameLinkComponents usernameLink
;
134 private String encryptedDeviceName
;
135 private int deviceId
= 0;
136 private String password
;
137 private String registrationLockPin
;
138 private MasterKey pinMasterKey
;
139 private StorageKey storageKey
;
140 private ProfileKey profileKey
;
142 private Settings settings
;
144 private final KeyValueEntry
<String
> verificationSessionId
= new KeyValueEntry
<>("verification-session-id",
146 private final KeyValueEntry
<String
> verificationSessionNumber
= new KeyValueEntry
<>("verification-session-number",
148 private final KeyValueEntry
<Long
> lastReceiveTimestamp
= new KeyValueEntry
<>("last-receive-timestamp",
151 private final KeyValueEntry
<byte[]> cdsiToken
= new KeyValueEntry
<>("cdsi-token", byte[].class);
152 private final KeyValueEntry
<Long
> lastRecipientsRefresh
= new KeyValueEntry
<>("last-recipients-refresh",
154 private final KeyValueEntry
<Long
> storageManifestVersion
= new KeyValueEntry
<>("storage-manifest-version",
157 private boolean isMultiDevice
= false;
158 private boolean registered
= false;
160 private final AccountData
<ACI
> aciAccountData
= new AccountData
<>(ServiceIdType
.ACI
);
161 private final AccountData
<PNI
> pniAccountData
= new AccountData
<>(ServiceIdType
.PNI
);
162 private IdentityKeyStore identityKeyStore
;
163 private SenderKeyStore senderKeyStore
;
164 private GroupStore groupStore
;
165 private RecipientStore recipientStore
;
166 private StickerStore stickerStore
;
167 private ConfigurationStore configurationStore
;
168 private KeyValueStore keyValueStore
;
169 private CdsiStore cdsiStore
;
171 private MessageCache messageCache
;
172 private MessageSendLogStore messageSendLogStore
;
174 private AccountDatabase accountDatabase
;
176 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
177 this.fileChannel
= fileChannel
;
181 public static SignalAccount
load(
182 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
183 ) throws IOException
{
184 logger
.trace("Opening account file");
185 final var fileName
= getFileName(dataPath
, accountPath
);
186 final var pair
= openFileChannel(fileName
, waitForLock
);
188 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
189 logger
.trace("Loading account file");
190 signalAccount
.load(dataPath
, accountPath
, settings
);
191 logger
.trace("Migrating legacy parts of account file");
192 signalAccount
.migrateLegacyConfigs();
194 return signalAccount
;
195 } catch (Throwable e
) {
196 pair
.second().close();
197 pair
.first().close();
202 public static SignalAccount
create(
206 ServiceEnvironment serviceEnvironment
,
207 IdentityKeyPair aciIdentityKey
,
208 IdentityKeyPair pniIdentityKey
,
209 ProfileKey profileKey
,
210 final Settings settings
211 ) throws IOException
{
212 IOUtils
.createPrivateDirectories(dataPath
);
213 var fileName
= getFileName(dataPath
, accountPath
);
214 if (!fileName
.exists()) {
215 IOUtils
.createPrivateFile(fileName
);
218 final var pair
= openFileChannel(fileName
, true);
219 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
221 signalAccount
.accountPath
= accountPath
;
222 signalAccount
.number
= number
;
223 signalAccount
.serviceEnvironment
= serviceEnvironment
;
224 signalAccount
.profileKey
= profileKey
;
225 signalAccount
.password
= KeyUtils
.createPassword();
226 signalAccount
.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
228 signalAccount
.dataPath
= dataPath
;
229 signalAccount
.aciAccountData
.setIdentityKeyPair(aciIdentityKey
);
230 signalAccount
.pniAccountData
.setIdentityKeyPair(pniIdentityKey
);
231 signalAccount
.aciAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
232 signalAccount
.pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
233 signalAccount
.initAllPreKeyIds();
234 signalAccount
.settings
= settings
;
236 signalAccount
.registered
= false;
238 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
239 signalAccount
.migrateLegacyConfigs();
240 signalAccount
.save();
242 return signalAccount
;
245 public static SignalAccount
createLinkedAccount(
247 final String accountPath
,
248 final ServiceEnvironment serviceEnvironment
,
249 final Settings settings
250 ) throws IOException
{
251 IOUtils
.createPrivateDirectories(dataPath
);
252 var fileName
= getFileName(dataPath
, accountPath
);
253 IOUtils
.createPrivateFile(fileName
);
255 final var pair
= openFileChannel(fileName
, true);
256 final var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
258 signalAccount
.dataPath
= dataPath
;
259 signalAccount
.accountPath
= accountPath
;
260 signalAccount
.serviceEnvironment
= serviceEnvironment
;
261 signalAccount
.aciAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
262 signalAccount
.pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
263 signalAccount
.settings
= settings
;
265 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
267 return signalAccount
;
270 public void setProvisioningData(
274 final String password
,
275 final String encryptedDeviceName
,
276 final IdentityKeyPair aciIdentity
,
277 final IdentityKeyPair pniIdentity
,
278 final ProfileKey profileKey
,
279 final MasterKey masterKey
282 this.number
= number
;
283 this.aciAccountData
.setServiceId(aci
);
284 this.pniAccountData
.setServiceId(pni
);
285 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
286 this.password
= password
;
287 this.profileKey
= profileKey
;
288 this.encryptedDeviceName
= encryptedDeviceName
;
289 this.aciAccountData
.setIdentityKeyPair(aciIdentity
);
290 this.pniAccountData
.setIdentityKeyPair(pniIdentity
);
291 this.registered
= false;
292 this.isMultiDevice
= true;
293 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
294 this.pinMasterKey
= masterKey
;
295 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
296 this.setStorageManifest(null);
297 this.storageKey
= null;
298 getSenderKeyStore().deleteAll();
299 trustSelfIdentity(ServiceIdType
.ACI
);
300 trustSelfIdentity(ServiceIdType
.PNI
);
301 aciAccountData
.getSessionStore().archiveAllSessions();
302 pniAccountData
.getSessionStore().archiveAllSessions();
304 getKeyValueStore().storeEntry(lastRecipientsRefresh
, null);
308 public void finishLinking(
309 final int deviceId
, final PreKeyCollection aciPreKeys
, final PreKeyCollection pniPreKeys
311 this.registered
= true;
312 this.deviceId
= deviceId
;
313 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
314 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
318 public void finishRegistration(
321 final MasterKey masterKey
,
323 final PreKeyCollection aciPreKeys
,
324 final PreKeyCollection pniPreKeys
326 this.pinMasterKey
= masterKey
;
327 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
328 this.setStorageManifest(null);
329 this.storageKey
= null;
330 this.encryptedDeviceName
= null;
331 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
332 this.isMultiDevice
= false;
333 this.registered
= true;
334 this.aciAccountData
.setServiceId(aci
);
335 this.pniAccountData
.setServiceId(pni
);
336 this.registrationLockPin
= pin
;
337 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
340 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
341 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
342 aciAccountData
.getSessionStore().archiveAllSessions();
343 pniAccountData
.getSessionStore().archiveAllSessions();
344 getSenderKeyStore().deleteAll();
345 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
346 trustSelfIdentity(ServiceIdType
.ACI
);
347 trustSelfIdentity(ServiceIdType
.PNI
);
348 getKeyValueStore().storeEntry(lastRecipientsRefresh
, null);
351 public void initDatabase() {
352 getAccountDatabase();
355 private void migrateLegacyConfigs() {
356 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
357 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
361 private void mergeRecipients(
362 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
363 ) throws SQLException
{
364 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
365 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
368 public void removeRecipient(final RecipientId recipientId
) {
369 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
370 if (recipientAddress
.matches(getSelfRecipientAddress())) {
371 throw new RuntimeException("Can't delete self recipient");
373 getRecipientStore().deleteRecipientData(recipientId
);
374 getMessageCache().deleteMessages(recipientId
);
375 if (recipientAddress
.serviceId().isPresent()) {
376 final var serviceId
= recipientAddress
.serviceId().get();
377 aciAccountData
.getSessionStore().deleteAllSessions(serviceId
);
378 pniAccountData
.getSessionStore().deleteAllSessions(serviceId
);
379 getIdentityKeyStore().deleteIdentity(serviceId
);
380 getSenderKeyStore().deleteAll(serviceId
);
384 public static File
getFileName(File dataPath
, String account
) {
385 return new File(dataPath
, account
);
388 private static File
getUserPath(final File dataPath
, final String account
) {
389 final var path
= new File(dataPath
, account
+ ".d");
391 IOUtils
.createPrivateDirectories(path
);
392 } catch (IOException e
) {
393 throw new AssertionError("Failed to create user path", e
);
398 private static File
getMessageCachePath(File dataPath
, String account
) {
399 return new File(getUserPath(dataPath
, account
), "msg-cache");
402 private static File
getStorageManifestFile(File dataPath
, String account
) {
403 return new File(getUserPath(dataPath
, account
), "storage-manifest");
406 private static File
getDatabaseFile(File dataPath
, String account
) {
407 return new File(getUserPath(dataPath
, account
), "account.db");
410 public static boolean accountFileExists(File dataPath
, String account
) {
411 if (account
== null) {
414 var f
= getFileName(dataPath
, account
);
415 return f
.exists() && !f
.isDirectory() && f
.length() > 0L;
419 File dataPath
, String accountPath
, final Settings settings
420 ) throws IOException
{
421 this.dataPath
= dataPath
;
422 this.accountPath
= accountPath
;
423 this.settings
= settings
;
424 final JsonNode rootNode
;
425 synchronized (fileChannel
) {
426 fileChannel
.position(0);
427 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
430 var migratedLegacyConfig
= false;
432 if (rootNode
.hasNonNull("version")) {
433 var accountVersion
= rootNode
.get("version").asInt(1);
434 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
435 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
436 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
437 throw new IOException("Config file was created by a no longer supported older version: "
440 previousStorageVersion
= accountVersion
;
441 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
442 migratedLegacyConfig
= true;
446 if (previousStorageVersion
< 8) {
447 final var userPath
= getUserPath(dataPath
, accountPath
);
448 loadLegacyFile(userPath
, rootNode
);
449 migratedLegacyConfig
= true;
451 final var storage
= jsonProcessor
.convertValue(rootNode
, Storage
.class);
452 serviceEnvironment
= ServiceEnvironment
.valueOf(storage
.serviceEnvironment
);
453 registered
= storage
.registered
;
454 number
= storage
.number
;
455 username
= storage
.username
;
456 encryptedDeviceName
= storage
.encryptedDeviceName
;
457 deviceId
= storage
.deviceId
;
458 isMultiDevice
= storage
.isMultiDevice
;
459 password
= storage
.password
;
460 setAccountData(aciAccountData
, storage
.aciAccountData
, ACI
::parseOrThrow
);
461 setAccountData(pniAccountData
, storage
.pniAccountData
, PNI
::parseOrThrow
);
462 registrationLockPin
= storage
.registrationLockPin
;
463 final var base64
= Base64
.getDecoder();
464 if (storage
.pinMasterKey
!= null) {
465 pinMasterKey
= new MasterKey(base64
.decode(storage
.pinMasterKey
));
467 if (storage
.storageKey
!= null) {
468 storageKey
= new StorageKey(base64
.decode(storage
.storageKey
));
470 if (storage
.profileKey
!= null) {
472 profileKey
= new ProfileKey(base64
.decode(storage
.profileKey
));
473 } catch (InvalidInputException e
) {
474 throw new IOException(
475 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
482 if (migratedLegacyConfig
) {
487 private <SERVICE_ID
extends ServiceId
> void setAccountData(
488 AccountData
<SERVICE_ID
> accountData
,
489 Storage
.AccountData storage
,
490 Function
<String
, SERVICE_ID
> serviceIdParser
491 ) throws IOException
{
492 if (storage
.serviceId
!= null) {
494 accountData
.setServiceId(serviceIdParser
.apply(storage
.serviceId
));
495 } catch (IllegalArgumentException e
) {
496 throw new IOException("Config file contains an invalid serviceId, needs to be a valid UUID", e
);
499 accountData
.setLocalRegistrationId(storage
.registrationId
);
500 if (storage
.identityPrivateKey
!= null && storage
.identityPublicKey
!= null) {
501 final var base64
= Base64
.getDecoder();
502 final var publicKeyBytes
= base64
.decode(storage
.identityPublicKey
);
503 final var privateKeyBytes
= base64
.decode(storage
.identityPrivateKey
);
504 final var keyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
505 accountData
.setIdentityKeyPair(keyPair
);
507 accountData
.preKeyMetadata
.nextPreKeyId
= storage
.nextPreKeyId
;
508 accountData
.preKeyMetadata
.nextSignedPreKeyId
= storage
.nextSignedPreKeyId
;
509 accountData
.preKeyMetadata
.activeSignedPreKeyId
= storage
.activeSignedPreKeyId
;
510 accountData
.preKeyMetadata
.nextKyberPreKeyId
= storage
.nextKyberPreKeyId
;
511 accountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= storage
.activeLastResortKyberPreKeyId
;
514 private void loadLegacyFile(final File userPath
, final JsonNode rootNode
) throws IOException
{
515 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
516 if (rootNode
.hasNonNull("password")) {
517 password
= rootNode
.get("password").asText();
519 if (password
== null) {
520 password
= KeyUtils
.createPassword();
523 if (rootNode
.hasNonNull("serviceEnvironment")) {
524 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
526 if (serviceEnvironment
== null) {
527 serviceEnvironment
= ServiceEnvironment
.LIVE
;
529 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
530 if (rootNode
.hasNonNull("usernameIdentifier")) {
531 username
= rootNode
.get("usernameIdentifier").asText();
533 if (rootNode
.hasNonNull("uuid")) {
535 aciAccountData
.setServiceId(ACI
.parseOrThrow(rootNode
.get("uuid").asText()));
536 } catch (IllegalArgumentException e
) {
537 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
540 if (rootNode
.hasNonNull("pni")) {
542 pniAccountData
.setServiceId(PNI
.parseOrThrow(rootNode
.get("pni").asText()));
543 } catch (IllegalArgumentException e
) {
544 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
547 if (rootNode
.hasNonNull("sessionId")) {
548 getKeyValueStore().storeEntry(verificationSessionId
, rootNode
.get("sessionId").asText());
550 if (rootNode
.hasNonNull("sessionNumber")) {
551 getKeyValueStore().storeEntry(verificationSessionNumber
, rootNode
.get("sessionNumber").asText());
553 if (rootNode
.hasNonNull("deviceName")) {
554 encryptedDeviceName
= rootNode
.get("deviceName").asText();
556 if (rootNode
.hasNonNull("deviceId")) {
557 deviceId
= rootNode
.get("deviceId").asInt();
559 if (rootNode
.hasNonNull("isMultiDevice")) {
560 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
562 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
563 getKeyValueStore().storeEntry(lastReceiveTimestamp
, rootNode
.get("lastReceiveTimestamp").asLong());
565 int registrationId
= 0;
566 if (rootNode
.hasNonNull("registrationId")) {
567 registrationId
= rootNode
.get("registrationId").asInt();
569 if (rootNode
.hasNonNull("pniRegistrationId")) {
570 pniAccountData
.setLocalRegistrationId(rootNode
.get("pniRegistrationId").asInt());
572 pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
574 IdentityKeyPair aciIdentityKeyPair
= null;
575 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
576 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
577 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
578 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
580 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
581 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
582 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
583 pniAccountData
.setIdentityKeyPair(KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
));
586 if (rootNode
.hasNonNull("registrationLockPin")) {
587 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
589 if (rootNode
.hasNonNull("pinMasterKey")) {
590 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
592 if (rootNode
.hasNonNull("storageKey")) {
593 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
595 if (rootNode
.hasNonNull("storageManifestVersion")) {
596 getKeyValueStore().storeEntry(storageManifestVersion
, rootNode
.get("storageManifestVersion").asLong());
598 if (rootNode
.hasNonNull("preKeyIdOffset")) {
599 aciAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("preKeyIdOffset").asInt(1);
601 aciAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
603 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
604 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
606 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
608 if (rootNode
.hasNonNull("activeSignedPreKeyId")) {
609 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("activeSignedPreKeyId").asInt(-1);
611 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
613 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
614 pniAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
616 pniAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
618 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
619 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
621 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
623 if (rootNode
.hasNonNull("pniActiveSignedPreKeyId")) {
624 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("pniActiveSignedPreKeyId").asInt(-1);
626 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
628 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
629 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
631 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
633 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
634 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId")
637 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
639 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
640 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
642 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
644 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
645 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get(
646 "pniActiveLastResortKyberPreKeyId").asInt(-1);
648 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
650 if (rootNode
.hasNonNull("profileKey")) {
652 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
653 } catch (InvalidInputException e
) {
654 throw new IOException(
655 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
659 if (profileKey
== null) {
660 // Old config file, creating new profile key
661 setProfileKey(KeyUtils
.createProfileKey());
664 if (previousStorageVersion
< 5) {
665 final var legacyRecipientsStoreFile
= new File(userPath
, "recipients-store");
666 if (legacyRecipientsStoreFile
.exists()) {
667 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
670 if (previousStorageVersion
< 6) {
671 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
673 final var legacyAciPreKeysPath
= new File(userPath
, "pre-keys");
674 if (legacyAciPreKeysPath
.exists()) {
675 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, aciAccountData
.getPreKeyStore());
677 final var legacyPniPreKeysPath
= new File(userPath
, "pre-keys-pni");
678 if (legacyPniPreKeysPath
.exists()) {
679 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, pniAccountData
.getPreKeyStore());
681 final var legacyAciSignedPreKeysPath
= new File(userPath
, "signed-pre-keys");
682 if (legacyAciSignedPreKeysPath
.exists()) {
683 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, aciAccountData
.getSignedPreKeyStore());
685 final var legacyPniSignedPreKeysPath
= new File(userPath
, "signed-pre-keys-pni");
686 if (legacyPniSignedPreKeysPath
.exists()) {
687 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, pniAccountData
.getSignedPreKeyStore());
689 final var legacySessionsPath
= new File(userPath
, "sessions");
690 if (legacySessionsPath
.exists()) {
691 LegacySessionStore
.migrate(legacySessionsPath
,
692 getRecipientResolver(),
693 getRecipientAddressResolver(),
694 aciAccountData
.getSessionStore());
696 final var legacyIdentitiesPath
= new File(userPath
, "identities");
697 if (legacyIdentitiesPath
.exists()) {
698 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
699 getRecipientResolver(),
700 getRecipientAddressResolver(),
701 getIdentityKeyStore());
703 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
704 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
705 LegacyJsonSignalProtocolStore
.class)
707 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
708 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
709 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
712 this.aciAccountData
.setIdentityKeyPair(aciIdentityKeyPair
);
713 this.aciAccountData
.setLocalRegistrationId(registrationId
);
715 loadLegacyStores(rootNode
, legacySignalProtocolStore
);
717 final var legacySenderKeysPath
= new File(userPath
, "sender-keys");
718 if (legacySenderKeysPath
.exists()) {
719 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
720 getRecipientResolver(),
721 getRecipientAddressResolver(),
722 getSenderKeyStore());
724 final var legacySenderKeysSharedPath
= new File(userPath
, "shared-sender-keys-store");
725 if (legacySenderKeysSharedPath
.exists()) {
726 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
727 getRecipientResolver(),
728 getRecipientAddressResolver(),
729 getSenderKeyStore());
731 if (rootNode
.hasNonNull("groupStore")) {
732 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
733 LegacyGroupStore
.Storage
.class);
734 LegacyGroupStore
.migrate(groupStoreStorage
,
735 new File(userPath
, "group-cache"),
736 getRecipientResolver(),
740 if (rootNode
.hasNonNull("stickerStore")) {
741 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
742 LegacyStickerStore
.Storage
.class);
743 LegacyStickerStore
.migrate(storage
, getStickerStore());
746 if (rootNode
.hasNonNull("configurationStore")) {
747 final var configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
748 LegacyConfigurationStore
.Storage
.class);
749 LegacyConfigurationStore
.migrate(configurationStoreStorage
, getConfigurationStore());
752 loadLegacyThreadStore(rootNode
);
755 private void loadLegacyStores(
756 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
758 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
759 if (legacyRecipientStoreNode
!= null) {
760 logger
.debug("Migrating legacy recipient store.");
761 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
762 if (legacyRecipientStore
!= null) {
763 legacyRecipientStore
.getAddresses()
764 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
766 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
769 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
770 logger
.debug("Migrating legacy pre key store.");
771 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
773 aciAccountData
.getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
774 } catch (InvalidMessageException e
) {
775 logger
.warn("Failed to migrate pre key, ignoring", e
);
780 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
781 logger
.debug("Migrating legacy signed pre key store.");
782 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
784 aciAccountData
.getSignedPreKeyStore()
785 .storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
786 } catch (InvalidMessageException e
) {
787 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
792 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
793 logger
.debug("Migrating legacy session store.");
794 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
796 aciAccountData
.getSessionStore()
797 .storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(), session
.deviceId
),
798 new SessionRecord(session
.sessionRecord
));
799 } catch (Exception e
) {
800 logger
.warn("Failed to migrate session, ignoring", e
);
805 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
806 logger
.debug("Migrating legacy identity session store.");
807 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
808 if (identity
.getAddress().serviceId().isEmpty()) {
811 final var serviceId
= identity
.getAddress().serviceId().get();
812 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
813 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
814 identity
.getIdentityKey(),
815 identity
.getTrustLevel());
819 if (rootNode
.hasNonNull("contactStore")) {
820 logger
.debug("Migrating legacy contact store.");
821 final var contactStoreNode
= rootNode
.get("contactStore");
822 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
823 for (var contact
: contactStore
.getContacts()) {
824 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
825 getContactStore().storeContact(recipientId
,
826 new Contact(contact
.name
,
829 contact
.messageExpirationTime
,
835 // Store profile keys only in profile store
836 var profileKeyString
= contact
.profileKey
;
837 if (profileKeyString
!= null) {
838 final ProfileKey profileKey
;
840 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
841 getProfileStore().storeProfileKey(recipientId
, profileKey
);
842 } catch (InvalidInputException e
) {
843 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
849 if (rootNode
.hasNonNull("profileStore")) {
850 logger
.debug("Migrating legacy profile store.");
851 var profileStoreNode
= rootNode
.get("profileStore");
852 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
853 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
854 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.address());
855 // Not migrating profile key credential here, it was changed to expiring profile key credentials
856 getProfileStore().storeProfileKey(recipientId
, profileEntry
.profileKey());
857 final var profile
= profileEntry
.profile();
858 if (profile
!= null) {
859 final var capabilities
= new HashSet
<Profile
.Capability
>();
860 if (profile
.getCapabilities() != null) {
861 if (profile
.getCapabilities().gv1Migration
) {
862 capabilities
.add(Profile
.Capability
.gv1Migration
);
864 if (profile
.getCapabilities().storage
) {
865 capabilities
.add(Profile
.Capability
.storage
);
868 final var newProfile
= new Profile(profileEntry
.lastUpdateTimestamp(),
869 profile
.getGivenName(),
870 profile
.getFamilyName(),
872 profile
.getAboutEmoji(),
875 profile
.isUnrestrictedUnidentifiedAccess()
876 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
877 : profile
.getUnidentifiedAccess() != null
878 ? Profile
.UnidentifiedAccessMode
.ENABLED
879 : Profile
.UnidentifiedAccessMode
.DISABLED
,
881 getProfileStore().storeProfile(recipientId
, newProfile
);
887 private void loadLegacyThreadStore(final JsonNode rootNode
) {
888 var threadStoreNode
= rootNode
.get("threadStore");
889 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
890 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
891 // Migrate thread info to group and contact store
892 for (var thread
: threadStore
.getThreads()) {
893 if (thread
.id
== null || thread
.id
.isEmpty()) {
897 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
898 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
899 var contact
= getContactStore().getContact(recipientId
);
900 if (contact
!= null) {
901 getContactStore().storeContact(recipientId
,
902 Contact
.newBuilder(contact
)
903 .withMessageExpirationTime(thread
.messageExpirationTime
)
907 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
908 if (groupInfo
instanceof GroupInfoV1
) {
909 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
910 getGroupStore().updateGroup(groupInfo
);
913 } catch (Exception e
) {
914 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
920 private void save() {
921 synchronized (fileChannel
) {
922 final var base64
= Base64
.getEncoder();
923 final var storage
= new Storage(CURRENT_STORAGE_VERSION
,
924 serviceEnvironment
.name(),
932 Storage
.AccountData
.from(aciAccountData
),
933 Storage
.AccountData
.from(pniAccountData
),
935 pinMasterKey
== null ?
null : base64
.encodeToString(pinMasterKey
.serialize()),
936 storageKey
== null ?
null : base64
.encodeToString(storageKey
.serialize()),
937 profileKey
== null ?
null : base64
.encodeToString(profileKey
.serialize()));
939 try (var output
= new ByteArrayOutputStream()) {
940 // Write to memory first to prevent corrupting the file in case of serialization errors
941 jsonProcessor
.writeValue(output
, storage
);
942 var input
= new ByteArrayInputStream(output
.toByteArray());
943 fileChannel
.position(0);
944 input
.transferTo(Channels
.newOutputStream(fileChannel
));
945 fileChannel
.truncate(fileChannel
.position());
946 fileChannel
.force(false);
948 } catch (Exception e
) {
949 logger
.error("Error saving file: {}", e
.getMessage(), e
);
954 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
955 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
957 var lock
= fileChannel
.tryLock();
960 logger
.debug("Config file is in use by another instance.");
961 throw new IOException("Config file is in use by another instance.");
963 logger
.info("Config file is in use by another instance, waiting…");
964 lock
= fileChannel
.lock();
965 logger
.info("Config file lock acquired.");
967 final var result
= new Pair
<>(fileChannel
, lock
);
971 if (fileChannel
!= null) {
977 private void clearAllPreKeys() {
978 clearAllPreKeys(ServiceIdType
.ACI
);
979 clearAllPreKeys(ServiceIdType
.PNI
);
982 private void initAllPreKeyIds() {
983 resetPreKeyOffsets(ServiceIdType
.ACI
);
984 resetPreKeyOffsets(ServiceIdType
.PNI
);
985 resetKyberPreKeyOffsets(ServiceIdType
.ACI
);
986 resetKyberPreKeyOffsets(ServiceIdType
.PNI
);
989 private void clearAllPreKeys(ServiceIdType serviceIdType
) {
990 final var accountData
= getAccountData(serviceIdType
);
991 resetPreKeyOffsets(serviceIdType
);
992 resetKyberPreKeyOffsets(serviceIdType
);
993 accountData
.getPreKeyStore().removeAllPreKeys();
994 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
995 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
999 private void setPreKeys(ServiceIdType serviceIdType
, PreKeyCollection preKeyCollection
) {
1000 final var accountData
= getAccountData(serviceIdType
);
1001 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1002 preKeyMetadata
.nextSignedPreKeyId
= preKeyCollection
.getSignedPreKey().getId();
1003 preKeyMetadata
.nextKyberPreKeyId
= preKeyCollection
.getLastResortKyberPreKey().getId();
1005 accountData
.getPreKeyStore().removeAllPreKeys();
1006 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1007 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1009 addSignedPreKey(serviceIdType
, preKeyCollection
.getSignedPreKey());
1010 addLastResortKyberPreKey(serviceIdType
, preKeyCollection
.getLastResortKyberPreKey());
1015 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1016 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1017 preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
1018 preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
1019 preKeyMetadata
.activeSignedPreKeyId
= -1;
1023 private static int getRandomPreKeyIdOffset() {
1024 return KeyUtils
.getRandomInt(PREKEY_MAXIMUM_ID
);
1027 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1028 final var accountData
= getAccountData(serviceIdType
);
1029 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1030 logger
.debug("Adding {} {} pre keys with offset {}",
1033 preKeyMetadata
.nextPreKeyId
);
1034 accountData
.getSignalServiceAccountDataStore()
1035 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1036 for (var record : records
) {
1037 if (preKeyMetadata
.nextPreKeyId
!= record.getId()) {
1038 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata
.nextPreKeyId
);
1039 throw new AssertionError("Invalid pre key id");
1041 accountData
.getPreKeyStore().storePreKey(record.getId(), record);
1042 preKeyMetadata
.nextPreKeyId
= (preKeyMetadata
.nextPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1047 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1048 final var accountData
= getAccountData(serviceIdType
);
1049 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1050 logger
.debug("Adding {} signed pre key with offset {}", serviceIdType
, preKeyMetadata
.nextSignedPreKeyId
);
1051 if (preKeyMetadata
.nextSignedPreKeyId
!= record.getId()) {
1052 logger
.error("Invalid signed pre key id {}, expected {}",
1054 preKeyMetadata
.nextSignedPreKeyId
);
1055 throw new AssertionError("Invalid signed pre key id");
1057 accountData
.getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1058 preKeyMetadata
.nextSignedPreKeyId
= (preKeyMetadata
.nextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1059 preKeyMetadata
.activeSignedPreKeyId
= record.getId();
1063 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1064 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1065 preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
1066 preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
1070 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1071 final var accountData
= getAccountData(serviceIdType
);
1072 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1073 logger
.debug("Adding {} {} kyber pre keys with offset {}",
1076 preKeyMetadata
.nextKyberPreKeyId
);
1077 accountData
.getSignalServiceAccountDataStore()
1078 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1079 for (var record : records
) {
1080 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1081 logger
.error("Invalid kyber pre key id {}, expected {}",
1083 preKeyMetadata
.nextKyberPreKeyId
);
1084 throw new AssertionError("Invalid kyber pre key id");
1086 accountData
.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1087 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1092 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1093 final var accountData
= getAccountData(serviceIdType
);
1094 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1095 logger
.debug("Adding {} last resort kyber pre key with offset {}",
1097 preKeyMetadata
.nextKyberPreKeyId
);
1098 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1099 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1101 preKeyMetadata
.nextKyberPreKeyId
);
1102 throw new AssertionError("Invalid last resort kyber pre key id");
1104 accountData
.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1105 preKeyMetadata
.activeLastResortKyberPreKeyId
= record.getId();
1106 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1110 public int getPreviousStorageVersion() {
1111 return previousStorageVersion
;
1114 public AccountData
<?
extends ServiceId
> getAccountData(ServiceIdType serviceIdType
) {
1115 return switch (serviceIdType
) {
1116 case ACI
-> aciAccountData
;
1117 case PNI
-> pniAccountData
;
1121 public AccountData
<?
extends ServiceId
> getAccountData(ServiceId accountIdentifier
) {
1122 if (accountIdentifier
.equals(aciAccountData
.getServiceId())) {
1123 return aciAccountData
;
1124 } else if (accountIdentifier
.equals(pniAccountData
.getServiceId())) {
1125 return pniAccountData
;
1127 throw new IllegalArgumentException("No matching account data found for " + accountIdentifier
);
1131 public SignalServiceDataStore
getSignalServiceDataStore() {
1132 return new SignalServiceDataStore() {
1134 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1135 return getAccountData(accountIdentifier
).getSignalServiceAccountDataStore();
1139 public SignalServiceAccountDataStore
aci() {
1140 return aciAccountData
.getSignalServiceAccountDataStore();
1144 public SignalServiceAccountDataStore
pni() {
1145 return pniAccountData
.getSignalServiceAccountDataStore();
1149 public boolean isMultiDevice() {
1150 return SignalAccount
.this.isMultiDevice();
1155 public IdentityKeyStore
getIdentityKeyStore() {
1156 return getOrCreate(() -> identityKeyStore
,
1157 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(), settings
.trustNewIdentity()));
1160 public GroupStore
getGroupStore() {
1161 return getOrCreate(() -> groupStore
,
1162 () -> groupStore
= new GroupStore(getAccountDatabase(),
1163 getRecipientResolver(),
1164 getRecipientIdCreator()));
1167 public ContactsStore
getContactStore() {
1168 return getRecipientStore();
1171 public CdsiStore
getCdsiStore() {
1172 return getOrCreate(() -> cdsiStore
, () -> cdsiStore
= new CdsiStore(getAccountDatabase()));
1175 private RecipientIdCreator
getRecipientIdCreator() {
1176 return recipientId
-> getRecipientStore().create(recipientId
);
1179 public RecipientResolver
getRecipientResolver() {
1180 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1183 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1184 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1187 public RecipientAddressResolver
getRecipientAddressResolver() {
1188 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1191 public RecipientStore
getRecipientStore() {
1192 return getOrCreate(() -> recipientStore
,
1193 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1194 this::getSelfRecipientAddress
,
1195 this::getProfileKey
,
1196 getAccountDatabase()));
1199 public ProfileStore
getProfileStore() {
1200 return getRecipientStore();
1203 public StickerStore
getStickerStore() {
1204 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1207 public SenderKeyStore
getSenderKeyStore() {
1208 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1211 private KeyValueStore
getKeyValueStore() {
1212 return getOrCreate(() -> keyValueStore
, () -> keyValueStore
= new KeyValueStore(getAccountDatabase()));
1215 public ConfigurationStore
getConfigurationStore() {
1216 return getOrCreate(() -> configurationStore
,
1217 () -> configurationStore
= new ConfigurationStore(getKeyValueStore()));
1220 public MessageCache
getMessageCache() {
1221 return getOrCreate(() -> messageCache
,
1222 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1225 public AccountDatabase
getAccountDatabase() {
1226 return getOrCreate(() -> accountDatabase
, () -> {
1228 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1229 } catch (SQLException e
) {
1230 throw new RuntimeException(e
);
1235 public MessageSendLogStore
getMessageSendLogStore() {
1236 return getOrCreate(() -> messageSendLogStore
,
1237 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1238 settings
.disableMessageSendLog()));
1241 public CredentialsProvider
getCredentialsProvider() {
1242 return new CredentialsProvider() {
1244 public ACI
getAci() {
1245 return aciAccountData
.getServiceId();
1249 public PNI
getPni() {
1250 return pniAccountData
.getServiceId();
1254 public String
getE164() {
1259 public String
getPassword() {
1264 public int getDeviceId() {
1270 public String
getNumber() {
1274 public void setNumber(final String number
) {
1275 this.number
= number
;
1279 public String
getUsername() {
1283 public void setUsername(final String username
) {
1284 this.username
= username
;
1288 public UsernameLinkComponents
getUsernameLink() {
1289 return usernameLink
;
1292 public void setUsernameLink(final UsernameLinkComponents usernameLink
) {
1293 this.usernameLink
= usernameLink
;
1296 public ServiceEnvironment
getServiceEnvironment() {
1297 return serviceEnvironment
;
1300 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1301 this.serviceEnvironment
= serviceEnvironment
;
1305 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1306 return new AccountAttributes(null,
1307 aciAccountData
.getLocalRegistrationId(),
1311 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1312 getSelfUnidentifiedAccessKey(),
1313 isUnrestrictedUnidentifiedAccess(),
1314 isDiscoverableByPhoneNumber(),
1315 getAccountCapabilities(),
1316 encryptedDeviceName
,
1317 pniAccountData
.getLocalRegistrationId(),
1318 null); // TODO recoveryPassword?
1321 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1322 return getCapabilities(isPrimaryDevice());
1325 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1326 return getAccountData(serviceIdType
).getServiceId();
1329 public ACI
getAci() {
1330 return aciAccountData
.getServiceId();
1333 public void setAci(final ACI aci
) {
1334 this.aciAccountData
.setServiceId(aci
);
1338 public PNI
getPni() {
1339 return pniAccountData
.getServiceId();
1342 public void setPni(final PNI updatedPni
) {
1343 final var oldPni
= pniAccountData
.getServiceId();
1344 if (oldPni
!= null && !oldPni
.equals(updatedPni
)) {
1345 // Clear data for old PNI
1346 identityKeyStore
.deleteIdentity(oldPni
);
1349 this.pniAccountData
.setServiceId(updatedPni
);
1350 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1351 trustSelfIdentity(ServiceIdType
.PNI
);
1355 public void setNewPniIdentity(
1356 final IdentityKeyPair pniIdentityKeyPair
,
1357 final SignedPreKeyRecord pniSignedPreKey
,
1358 final KyberPreKeyRecord lastResortKyberPreKey
,
1359 final int localPniRegistrationId
1361 setPniIdentityKeyPair(pniIdentityKeyPair
);
1362 pniAccountData
.setLocalRegistrationId(localPniRegistrationId
);
1364 final AccountData
<?
extends ServiceId
> accountData
= getAccountData(ServiceIdType
.PNI
);
1365 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1366 preKeyMetadata
.nextSignedPreKeyId
= pniSignedPreKey
.getId();
1367 accountData
.getSignedPreKeyStore().removeSignedPreKey(pniSignedPreKey
.getId());
1368 addSignedPreKey(ServiceIdType
.PNI
, pniSignedPreKey
);
1369 if (lastResortKyberPreKey
!= null) {
1370 preKeyMetadata
.nextKyberPreKeyId
= lastResortKyberPreKey
.getId();
1371 accountData
.getKyberPreKeyStore().removeKyberPreKey(lastResortKyberPreKey
.getId());
1372 addLastResortKyberPreKey(ServiceIdType
.PNI
, lastResortKyberPreKey
);
1377 public SignalServiceAddress
getSelfAddress() {
1378 return new SignalServiceAddress(getAci(), number
);
1381 public RecipientAddress
getSelfRecipientAddress() {
1382 return new RecipientAddress(getAci(), getPni(), number
, username
);
1385 public RecipientId
getSelfRecipientId() {
1386 return getRecipientResolver().resolveRecipient(getSelfRecipientAddress());
1389 public String
getSessionId(final String forNumber
) {
1390 final var keyValueStore
= getKeyValueStore();
1391 final var sessionNumber
= keyValueStore
.getEntry(verificationSessionNumber
);
1392 if (!forNumber
.equals(sessionNumber
)) {
1395 return keyValueStore
.getEntry(verificationSessionId
);
1398 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1399 final var keyValueStore
= getKeyValueStore();
1400 keyValueStore
.storeEntry(verificationSessionNumber
, sessionNumber
);
1401 keyValueStore
.storeEntry(verificationSessionId
, sessionId
);
1404 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1405 this.encryptedDeviceName
= encryptedDeviceName
;
1409 public int getDeviceId() {
1413 public boolean isPrimaryDevice() {
1414 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1417 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1418 return getAccountData(serviceIdType
).getIdentityKeyPair();
1421 public IdentityKeyPair
getAciIdentityKeyPair() {
1422 return aciAccountData
.getIdentityKeyPair();
1425 public IdentityKeyPair
getPniIdentityKeyPair() {
1426 return pniAccountData
.getIdentityKeyPair();
1429 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1430 pniAccountData
.setIdentityKeyPair(identityKeyPair
);
1431 trustSelfIdentity(ServiceIdType
.PNI
);
1435 public String
getPassword() {
1439 public void setRegistrationLockPin(final String registrationLockPin
) {
1440 this.registrationLockPin
= registrationLockPin
;
1444 public String
getRegistrationLockPin() {
1445 return registrationLockPin
;
1448 public String
getRegistrationLock() {
1449 final var masterKey
= getPinBackedMasterKey();
1450 if (masterKey
== null) {
1453 return masterKey
.deriveRegistrationLock();
1456 public MasterKey
getPinBackedMasterKey() {
1457 if (registrationLockPin
== null) {
1460 return pinMasterKey
;
1463 public MasterKey
getOrCreatePinMasterKey() {
1464 if (pinMasterKey
== null) {
1465 pinMasterKey
= KeyUtils
.createMasterKey();
1468 return pinMasterKey
;
1471 public StorageKey
getStorageKey() {
1472 if (pinMasterKey
!= null) {
1473 return pinMasterKey
.deriveStorageServiceKey();
1478 public StorageKey
getOrCreateStorageKey() {
1479 if (isPrimaryDevice()) {
1480 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1485 public void setStorageKey(final StorageKey storageKey
) {
1486 if (storageKey
.equals(this.storageKey
)) {
1489 this.storageKey
= storageKey
;
1493 public long getStorageManifestVersion() {
1494 return getKeyValueStore().getEntry(storageManifestVersion
);
1497 public void setStorageManifestVersion(final long value
) {
1498 getKeyValueStore().storeEntry(storageManifestVersion
, value
);
1501 public Optional
<SignalStorageManifest
> getStorageManifest() {
1502 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1503 if (!storageManifestFile
.exists()) {
1504 return Optional
.empty();
1506 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1507 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1508 } catch (IOException e
) {
1509 logger
.warn("Failed to read local storage manifest.", e
);
1510 return Optional
.empty();
1514 public void setStorageManifest(SignalStorageManifest manifest
) {
1515 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1516 if (manifest
== null) {
1517 if (storageManifestFile
.exists()) {
1519 Files
.delete(storageManifestFile
.toPath());
1520 } catch (IOException e
) {
1521 logger
.error("Failed to delete local storage manifest.", e
);
1527 final var manifestBytes
= manifest
.serialize();
1528 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1529 outputStream
.write(manifestBytes
);
1530 } catch (IOException e
) {
1531 logger
.error("Failed to store local storage manifest.", e
);
1535 public byte[] getCdsiToken() {
1536 return getKeyValueStore().getEntry(cdsiToken
);
1539 public void setCdsiToken(final byte[] value
) {
1540 getKeyValueStore().storeEntry(cdsiToken
, value
);
1543 public Long
getLastRecipientsRefresh() {
1544 return getKeyValueStore().getEntry(lastRecipientsRefresh
);
1547 public void setLastRecipientsRefresh(final Long value
) {
1548 getKeyValueStore().storeEntry(lastRecipientsRefresh
, value
);
1551 public ProfileKey
getProfileKey() {
1555 public void setProfileKey(final ProfileKey profileKey
) {
1556 if (profileKey
.equals(this.profileKey
)) {
1559 this.profileKey
= profileKey
;
1563 public byte[] getSelfUnidentifiedAccessKey() {
1564 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1567 public boolean isRegistered() {
1571 public void setRegistered(final boolean registered
) {
1572 this.registered
= registered
;
1576 public boolean isMultiDevice() {
1577 return isMultiDevice
;
1580 public void setMultiDevice(final boolean multiDevice
) {
1581 if (isMultiDevice
== multiDevice
) {
1584 isMultiDevice
= multiDevice
;
1588 public long getLastReceiveTimestamp() {
1589 return getKeyValueStore().getEntry(lastReceiveTimestamp
);
1592 public void setLastReceiveTimestamp(final long value
) {
1593 getKeyValueStore().storeEntry(lastReceiveTimestamp
, value
);
1596 public boolean isUnrestrictedUnidentifiedAccess() {
1597 final var profile
= getProfileStore().getProfile(getSelfRecipientId());
1598 return profile
!= null && profile
.getUnidentifiedAccessMode() == Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
1601 public boolean isDiscoverableByPhoneNumber() {
1602 final var phoneNumberUnlisted
= getConfigurationStore().getPhoneNumberUnlisted();
1603 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1606 private void trustSelfIdentity(ServiceIdType serviceIdType
) {
1607 final var accountData
= getAccountData(serviceIdType
);
1608 final var serviceId
= accountData
.getServiceId();
1609 final var identityKeyPair
= accountData
.getIdentityKeyPair();
1610 if (serviceId
== null || identityKeyPair
== null) {
1613 final var publicKey
= identityKeyPair
.getPublicKey();
1614 getIdentityKeyStore().saveIdentity(serviceId
, publicKey
);
1615 getIdentityKeyStore().setIdentityTrustLevel(serviceId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1618 public void deleteAccountData() throws IOException
{
1620 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1621 .sorted(Comparator
.reverseOrder())) {
1622 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1623 Files
.delete(file
.next());
1626 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1630 public void close() {
1631 synchronized (fileChannel
) {
1632 if (accountDatabase
!= null) {
1633 accountDatabase
.close();
1635 if (messageSendLogStore
!= null) {
1636 messageSendLogStore
.close();
1641 } catch (ClosedChannelException ignored
) {
1643 fileChannel
.close();
1644 } catch (IOException e
) {
1645 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1650 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1651 var value
= supplier
.get();
1652 if (value
!= null) {
1656 synchronized (LOCK
) {
1657 value
= supplier
.get();
1658 if (value
!= null) {
1662 return supplier
.get();
1666 private interface Callable
{
1671 public static class PreKeyMetadata
{
1673 private int nextPreKeyId
= 1;
1674 private int nextSignedPreKeyId
= 1;
1675 private int activeSignedPreKeyId
= -1;
1676 private int nextKyberPreKeyId
= 1;
1677 private int activeLastResortKyberPreKeyId
= -1;
1679 public int getNextPreKeyId() {
1680 return nextPreKeyId
;
1683 public int getNextSignedPreKeyId() {
1684 return nextSignedPreKeyId
;
1687 public int getActiveSignedPreKeyId() {
1688 return activeSignedPreKeyId
;
1691 public int getNextKyberPreKeyId() {
1692 return nextKyberPreKeyId
;
1695 public int getActiveLastResortKyberPreKeyId() {
1696 return activeLastResortKyberPreKeyId
;
1700 public class AccountData
<SERVICE_ID
extends ServiceId
> {
1702 private final ServiceIdType serviceIdType
;
1703 private SERVICE_ID serviceId
;
1704 private IdentityKeyPair identityKeyPair
;
1705 private int localRegistrationId
;
1706 private final PreKeyMetadata preKeyMetadata
= new PreKeyMetadata();
1708 private SignalProtocolStore signalProtocolStore
;
1709 private PreKeyStore preKeyStore
;
1710 private SignedPreKeyStore signedPreKeyStore
;
1711 private KyberPreKeyStore kyberPreKeyStore
;
1712 private SessionStore sessionStore
;
1713 private SignalIdentityKeyStore identityKeyStore
;
1715 private AccountData(final ServiceIdType serviceIdType
) {
1716 this.serviceIdType
= serviceIdType
;
1719 public SERVICE_ID
getServiceId() {
1723 private void setServiceId(final SERVICE_ID serviceId
) {
1724 this.serviceId
= serviceId
;
1727 public IdentityKeyPair
getIdentityKeyPair() {
1728 return identityKeyPair
;
1731 private void setIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1732 this.identityKeyPair
= identityKeyPair
;
1735 public int getLocalRegistrationId() {
1736 return localRegistrationId
;
1739 private void setLocalRegistrationId(final int localRegistrationId
) {
1740 this.localRegistrationId
= localRegistrationId
;
1741 this.identityKeyStore
= null;
1744 public PreKeyMetadata
getPreKeyMetadata() {
1745 return preKeyMetadata
;
1748 private SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1749 return getOrCreate(() -> signalProtocolStore
,
1750 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
1751 getSignedPreKeyStore(),
1752 getKyberPreKeyStore(),
1754 getIdentityKeyStore(),
1755 getSenderKeyStore(),
1756 SignalAccount
.this::isMultiDevice
));
1759 public PreKeyStore
getPreKeyStore() {
1760 return getOrCreate(() -> preKeyStore
,
1761 () -> preKeyStore
= new PreKeyStore(getAccountDatabase(), serviceIdType
));
1764 public SignedPreKeyStore
getSignedPreKeyStore() {
1765 return getOrCreate(() -> signedPreKeyStore
,
1766 () -> signedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), serviceIdType
));
1769 public KyberPreKeyStore
getKyberPreKeyStore() {
1770 return getOrCreate(() -> kyberPreKeyStore
,
1771 () -> kyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), serviceIdType
));
1774 public SessionStore
getSessionStore() {
1775 return getOrCreate(() -> sessionStore
,
1776 () -> sessionStore
= new SessionStore(getAccountDatabase(), serviceIdType
));
1779 public SignalIdentityKeyStore
getIdentityKeyStore() {
1780 return getOrCreate(() -> identityKeyStore
,
1781 () -> identityKeyStore
= new SignalIdentityKeyStore(() -> identityKeyPair
,
1782 localRegistrationId
,
1783 SignalAccount
.this.getIdentityKeyStore()));
1787 public record Storage(
1789 String serviceEnvironment
,
1793 String encryptedDeviceName
,
1795 boolean isMultiDevice
,
1797 AccountData aciAccountData
,
1798 AccountData pniAccountData
,
1799 String registrationLockPin
,
1800 String pinMasterKey
,
1805 public record AccountData(
1808 String identityPrivateKey
,
1809 String identityPublicKey
,
1812 int nextSignedPreKeyId
,
1813 int activeSignedPreKeyId
,
1814 int nextKyberPreKeyId
,
1815 int activeLastResortKyberPreKeyId
1818 private static AccountData
from(final SignalAccount
.AccountData
<?
> accountData
) {
1819 final var base64
= Base64
.getEncoder();
1820 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1821 return new AccountData(accountData
.getServiceId() == null
1823 : accountData
.getServiceId().toString(),
1824 accountData
.getLocalRegistrationId(),
1825 accountData
.getIdentityKeyPair() == null
1827 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPrivateKey().serialize()),
1828 accountData
.getIdentityKeyPair() == null
1830 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPublicKey().serialize()),
1831 preKeyMetadata
.getNextPreKeyId(),
1832 preKeyMetadata
.getNextSignedPreKeyId(),
1833 preKeyMetadata
.getActiveSignedPreKeyId(),
1834 preKeyMetadata
.getNextKyberPreKeyId(),
1835 preKeyMetadata
.getActiveLastResortKyberPreKeyId());