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 final KeyValueEntry
<Boolean
> unrestrictedUnidentifiedAccess
= new KeyValueEntry
<>(
158 "unrestricted-unidentified-access",
161 private boolean isMultiDevice
= false;
162 private boolean registered
= false;
164 private final AccountData
<ACI
> aciAccountData
= new AccountData
<>(ServiceIdType
.ACI
);
165 private final AccountData
<PNI
> pniAccountData
= new AccountData
<>(ServiceIdType
.PNI
);
166 private IdentityKeyStore identityKeyStore
;
167 private SenderKeyStore senderKeyStore
;
168 private GroupStore groupStore
;
169 private RecipientStore recipientStore
;
170 private StickerStore stickerStore
;
171 private UnknownStorageIdStore unknownStorageIdStore
;
172 private ConfigurationStore configurationStore
;
173 private KeyValueStore keyValueStore
;
174 private CdsiStore cdsiStore
;
176 private MessageCache messageCache
;
177 private MessageSendLogStore messageSendLogStore
;
179 private AccountDatabase accountDatabase
;
180 private RecipientId selfRecipientId
;
182 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
183 this.fileChannel
= fileChannel
;
187 public static SignalAccount
load(
188 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
189 ) throws IOException
{
190 logger
.trace("Opening account file");
191 final var fileName
= getFileName(dataPath
, accountPath
);
192 final var pair
= openFileChannel(fileName
, waitForLock
);
194 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
195 logger
.trace("Loading account file");
196 signalAccount
.load(dataPath
, accountPath
, settings
);
197 logger
.trace("Migrating legacy parts of account file");
198 signalAccount
.migrateLegacyConfigs();
199 signalAccount
.init();
201 return signalAccount
;
202 } catch (Throwable e
) {
203 pair
.second().close();
204 pair
.first().close();
209 public static SignalAccount
create(
213 ServiceEnvironment serviceEnvironment
,
214 IdentityKeyPair aciIdentityKey
,
215 IdentityKeyPair pniIdentityKey
,
216 ProfileKey profileKey
,
217 final Settings settings
218 ) throws IOException
{
219 IOUtils
.createPrivateDirectories(dataPath
);
220 var fileName
= getFileName(dataPath
, accountPath
);
221 if (!fileName
.exists()) {
222 IOUtils
.createPrivateFile(fileName
);
225 final var pair
= openFileChannel(fileName
, true);
226 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
228 signalAccount
.accountPath
= accountPath
;
229 signalAccount
.number
= number
;
230 signalAccount
.serviceEnvironment
= serviceEnvironment
;
231 signalAccount
.profileKey
= profileKey
;
232 signalAccount
.password
= KeyUtils
.createPassword();
233 signalAccount
.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
235 signalAccount
.dataPath
= dataPath
;
236 signalAccount
.aciAccountData
.setIdentityKeyPair(aciIdentityKey
);
237 signalAccount
.pniAccountData
.setIdentityKeyPair(pniIdentityKey
);
238 signalAccount
.aciAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
239 signalAccount
.pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
240 signalAccount
.initAllPreKeyIds();
241 signalAccount
.settings
= settings
;
243 signalAccount
.registered
= false;
245 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
246 signalAccount
.init();
247 signalAccount
.save();
249 return signalAccount
;
252 public static SignalAccount
createLinkedAccount(
254 final String accountPath
,
255 final ServiceEnvironment serviceEnvironment
,
256 final Settings settings
257 ) throws IOException
{
258 IOUtils
.createPrivateDirectories(dataPath
);
259 var fileName
= getFileName(dataPath
, accountPath
);
260 IOUtils
.createPrivateFile(fileName
);
262 final var pair
= openFileChannel(fileName
, true);
263 final var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
265 signalAccount
.dataPath
= dataPath
;
266 signalAccount
.accountPath
= accountPath
;
267 signalAccount
.serviceEnvironment
= serviceEnvironment
;
268 signalAccount
.aciAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
269 signalAccount
.pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
270 signalAccount
.settings
= settings
;
272 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
274 return signalAccount
;
277 public void setProvisioningData(
281 final String password
,
282 final String encryptedDeviceName
,
283 final IdentityKeyPair aciIdentity
,
284 final IdentityKeyPair pniIdentity
,
285 final ProfileKey profileKey
,
286 final MasterKey masterKey
289 this.number
= number
;
290 this.aciAccountData
.setServiceId(aci
);
291 this.pniAccountData
.setServiceId(pni
);
293 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
294 this.password
= password
;
295 this.profileKey
= profileKey
;
296 this.encryptedDeviceName
= encryptedDeviceName
;
297 this.aciAccountData
.setIdentityKeyPair(aciIdentity
);
298 this.pniAccountData
.setIdentityKeyPair(pniIdentity
);
299 this.registered
= false;
300 this.isMultiDevice
= true;
301 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
302 this.pinMasterKey
= masterKey
;
303 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
304 this.setStorageManifest(null);
305 this.storageKey
= null;
306 getSenderKeyStore().deleteAll();
307 trustSelfIdentity(ServiceIdType
.ACI
);
308 trustSelfIdentity(ServiceIdType
.PNI
);
309 aciAccountData
.getSessionStore().archiveAllSessions();
310 pniAccountData
.getSessionStore().archiveAllSessions();
312 getKeyValueStore().storeEntry(lastRecipientsRefresh
, null);
316 public void finishLinking(
317 final int deviceId
, final PreKeyCollection aciPreKeys
, final PreKeyCollection pniPreKeys
319 this.registered
= true;
320 this.deviceId
= deviceId
;
321 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
322 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
326 public void finishRegistration(
329 final MasterKey masterKey
,
331 final PreKeyCollection aciPreKeys
,
332 final PreKeyCollection pniPreKeys
334 this.pinMasterKey
= masterKey
;
335 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
336 this.setStorageManifest(null);
337 this.storageKey
= null;
338 this.encryptedDeviceName
= null;
339 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
340 this.isMultiDevice
= false;
341 this.registered
= true;
342 this.aciAccountData
.setServiceId(aci
);
343 this.pniAccountData
.setServiceId(pni
);
345 this.registrationLockPin
= pin
;
346 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
349 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
350 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
351 aciAccountData
.getSessionStore().archiveAllSessions();
352 pniAccountData
.getSessionStore().archiveAllSessions();
353 getSenderKeyStore().deleteAll();
354 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
355 trustSelfIdentity(ServiceIdType
.ACI
);
356 trustSelfIdentity(ServiceIdType
.PNI
);
357 getKeyValueStore().storeEntry(lastRecipientsRefresh
, null);
360 public void initDatabase() {
361 getAccountDatabase();
364 private void init() {
365 this.selfRecipientId
= getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
368 private void migrateLegacyConfigs() {
369 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
370 setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
374 private void mergeRecipients(
375 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
376 ) throws SQLException
{
377 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
378 getGroupStore().mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
381 public void removeRecipient(final RecipientId recipientId
) {
382 final var recipientAddress
= getRecipientStore().resolveRecipientAddress(recipientId
);
383 if (recipientAddress
.matches(getSelfRecipientAddress())) {
384 throw new RuntimeException("Can't delete self recipient");
386 getRecipientStore().deleteRecipientData(recipientId
);
387 getMessageCache().deleteMessages(recipientId
);
388 if (recipientAddress
.aci().isPresent()) {
389 final var serviceId
= recipientAddress
.aci().get();
390 aciAccountData
.getSessionStore().deleteAllSessions(serviceId
);
391 pniAccountData
.getSessionStore().deleteAllSessions(serviceId
);
392 getIdentityKeyStore().deleteIdentity(serviceId
);
393 getSenderKeyStore().deleteAll(serviceId
);
395 if (recipientAddress
.pni().isPresent()) {
396 final var serviceId
= recipientAddress
.pni().get();
397 aciAccountData
.getSessionStore().deleteAllSessions(serviceId
);
398 pniAccountData
.getSessionStore().deleteAllSessions(serviceId
);
399 getIdentityKeyStore().deleteIdentity(serviceId
);
400 getSenderKeyStore().deleteAll(serviceId
);
404 public static File
getFileName(File dataPath
, String account
) {
405 return new File(dataPath
, account
);
408 private static File
getUserPath(final File dataPath
, final String account
) {
409 final var path
= new File(dataPath
, account
+ ".d");
411 IOUtils
.createPrivateDirectories(path
);
412 } catch (IOException e
) {
413 throw new AssertionError("Failed to create user path", e
);
418 private static File
getMessageCachePath(File dataPath
, String account
) {
419 return new File(getUserPath(dataPath
, account
), "msg-cache");
422 private static File
getStorageManifestFile(File dataPath
, String account
) {
423 return new File(getUserPath(dataPath
, account
), "storage-manifest");
426 private static File
getDatabaseFile(File dataPath
, String account
) {
427 return new File(getUserPath(dataPath
, account
), "account.db");
430 public static boolean accountFileExists(File dataPath
, String account
) {
431 if (account
== null) {
434 var f
= getFileName(dataPath
, account
);
435 return f
.exists() && !f
.isDirectory() && f
.length() > 0L;
439 File dataPath
, String accountPath
, final Settings settings
440 ) throws IOException
{
441 this.dataPath
= dataPath
;
442 this.accountPath
= accountPath
;
443 this.settings
= settings
;
444 final JsonNode rootNode
;
445 synchronized (fileChannel
) {
446 fileChannel
.position(0);
447 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
450 var migratedLegacyConfig
= false;
452 if (rootNode
.hasNonNull("version")) {
453 var accountVersion
= rootNode
.get("version").asInt(1);
454 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
455 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
456 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
457 throw new IOException("Config file was created by a no longer supported older version: "
460 previousStorageVersion
= accountVersion
;
461 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
462 migratedLegacyConfig
= true;
466 if (previousStorageVersion
< 8) {
467 final var userPath
= getUserPath(dataPath
, accountPath
);
468 loadLegacyFile(userPath
, rootNode
);
469 migratedLegacyConfig
= true;
471 final var storage
= jsonProcessor
.convertValue(rootNode
, Storage
.class);
472 serviceEnvironment
= ServiceEnvironment
.valueOf(storage
.serviceEnvironment
);
473 registered
= storage
.registered
;
474 number
= storage
.number
;
475 username
= storage
.username
;
476 encryptedDeviceName
= storage
.encryptedDeviceName
;
477 deviceId
= storage
.deviceId
;
478 isMultiDevice
= storage
.isMultiDevice
;
479 password
= storage
.password
;
480 setAccountData(aciAccountData
, storage
.aciAccountData
, ACI
::parseOrThrow
);
481 setAccountData(pniAccountData
, storage
.pniAccountData
, PNI
::parseOrThrow
);
482 registrationLockPin
= storage
.registrationLockPin
;
483 final var base64
= Base64
.getDecoder();
484 if (storage
.pinMasterKey
!= null) {
485 pinMasterKey
= new MasterKey(base64
.decode(storage
.pinMasterKey
));
487 if (storage
.storageKey
!= null) {
488 storageKey
= new StorageKey(base64
.decode(storage
.storageKey
));
490 if (storage
.profileKey
!= null) {
492 profileKey
= new ProfileKey(base64
.decode(storage
.profileKey
));
493 } catch (InvalidInputException e
) {
494 throw new IOException(
495 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
502 if (migratedLegacyConfig
) {
507 private <SERVICE_ID
extends ServiceId
> void setAccountData(
508 AccountData
<SERVICE_ID
> accountData
,
509 Storage
.AccountData storage
,
510 Function
<String
, SERVICE_ID
> serviceIdParser
511 ) throws IOException
{
512 if (storage
.serviceId
!= null) {
514 accountData
.setServiceId(serviceIdParser
.apply(storage
.serviceId
));
515 } catch (IllegalArgumentException e
) {
516 throw new IOException("Config file contains an invalid serviceId, needs to be a valid UUID", e
);
519 accountData
.setLocalRegistrationId(storage
.registrationId
);
520 if (storage
.identityPrivateKey
!= null && storage
.identityPublicKey
!= null) {
521 final var base64
= Base64
.getDecoder();
522 final var publicKeyBytes
= base64
.decode(storage
.identityPublicKey
);
523 final var privateKeyBytes
= base64
.decode(storage
.identityPrivateKey
);
524 final var keyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
525 accountData
.setIdentityKeyPair(keyPair
);
527 accountData
.preKeyMetadata
.nextPreKeyId
= storage
.nextPreKeyId
;
528 accountData
.preKeyMetadata
.nextSignedPreKeyId
= storage
.nextSignedPreKeyId
;
529 accountData
.preKeyMetadata
.activeSignedPreKeyId
= storage
.activeSignedPreKeyId
;
530 accountData
.preKeyMetadata
.nextKyberPreKeyId
= storage
.nextKyberPreKeyId
;
531 accountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= storage
.activeLastResortKyberPreKeyId
;
534 private void loadLegacyFile(final File userPath
, final JsonNode rootNode
) throws IOException
{
535 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
536 if (rootNode
.hasNonNull("password")) {
537 password
= rootNode
.get("password").asText();
539 if (password
== null) {
540 password
= KeyUtils
.createPassword();
543 if (rootNode
.hasNonNull("serviceEnvironment")) {
544 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
546 if (serviceEnvironment
== null) {
547 serviceEnvironment
= ServiceEnvironment
.LIVE
;
549 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
550 if (rootNode
.hasNonNull("usernameIdentifier")) {
551 username
= rootNode
.get("usernameIdentifier").asText();
553 if (rootNode
.hasNonNull("uuid")) {
555 aciAccountData
.setServiceId(ACI
.parseOrThrow(rootNode
.get("uuid").asText()));
556 } catch (IllegalArgumentException e
) {
557 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
560 if (rootNode
.hasNonNull("pni")) {
562 pniAccountData
.setServiceId(PNI
.parseOrThrow(rootNode
.get("pni").asText()));
563 } catch (IllegalArgumentException e
) {
564 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
567 if (rootNode
.hasNonNull("sessionId")) {
568 getKeyValueStore().storeEntry(verificationSessionId
, rootNode
.get("sessionId").asText());
570 if (rootNode
.hasNonNull("sessionNumber")) {
571 getKeyValueStore().storeEntry(verificationSessionNumber
, rootNode
.get("sessionNumber").asText());
573 if (rootNode
.hasNonNull("deviceName")) {
574 encryptedDeviceName
= rootNode
.get("deviceName").asText();
576 if (rootNode
.hasNonNull("deviceId")) {
577 deviceId
= rootNode
.get("deviceId").asInt();
579 if (rootNode
.hasNonNull("isMultiDevice")) {
580 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
582 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
583 getKeyValueStore().storeEntry(lastReceiveTimestamp
, rootNode
.get("lastReceiveTimestamp").asLong());
585 int registrationId
= 0;
586 if (rootNode
.hasNonNull("registrationId")) {
587 registrationId
= rootNode
.get("registrationId").asInt();
589 if (rootNode
.hasNonNull("pniRegistrationId")) {
590 pniAccountData
.setLocalRegistrationId(rootNode
.get("pniRegistrationId").asInt());
592 pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
594 IdentityKeyPair aciIdentityKeyPair
= null;
595 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
596 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
597 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
598 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
600 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
601 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
602 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
603 pniAccountData
.setIdentityKeyPair(KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
));
606 if (rootNode
.hasNonNull("registrationLockPin")) {
607 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
609 if (rootNode
.hasNonNull("pinMasterKey")) {
610 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
612 if (rootNode
.hasNonNull("storageKey")) {
613 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
615 if (rootNode
.hasNonNull("storageManifestVersion")) {
616 getKeyValueStore().storeEntry(storageManifestVersion
, rootNode
.get("storageManifestVersion").asLong());
618 if (rootNode
.hasNonNull("preKeyIdOffset")) {
619 aciAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("preKeyIdOffset").asInt(1);
621 aciAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
623 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
624 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
626 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
628 if (rootNode
.hasNonNull("activeSignedPreKeyId")) {
629 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("activeSignedPreKeyId").asInt(-1);
631 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
633 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
634 pniAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
636 pniAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
638 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
639 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
641 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
643 if (rootNode
.hasNonNull("pniActiveSignedPreKeyId")) {
644 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("pniActiveSignedPreKeyId").asInt(-1);
646 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
648 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
649 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
651 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
653 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
654 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId")
657 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
659 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
660 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
662 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
664 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
665 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get(
666 "pniActiveLastResortKyberPreKeyId").asInt(-1);
668 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
670 if (rootNode
.hasNonNull("profileKey")) {
672 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
673 } catch (InvalidInputException e
) {
674 throw new IOException(
675 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
679 if (profileKey
== null) {
680 // Old config file, creating new profile key
681 setProfileKey(KeyUtils
.createProfileKey());
684 if (previousStorageVersion
< 5) {
685 final var legacyRecipientsStoreFile
= new File(userPath
, "recipients-store");
686 if (legacyRecipientsStoreFile
.exists()) {
687 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
690 if (previousStorageVersion
< 6) {
691 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
693 final var legacyAciPreKeysPath
= new File(userPath
, "pre-keys");
694 if (legacyAciPreKeysPath
.exists()) {
695 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, aciAccountData
.getPreKeyStore());
697 final var legacyPniPreKeysPath
= new File(userPath
, "pre-keys-pni");
698 if (legacyPniPreKeysPath
.exists()) {
699 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, pniAccountData
.getPreKeyStore());
701 final var legacyAciSignedPreKeysPath
= new File(userPath
, "signed-pre-keys");
702 if (legacyAciSignedPreKeysPath
.exists()) {
703 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, aciAccountData
.getSignedPreKeyStore());
705 final var legacyPniSignedPreKeysPath
= new File(userPath
, "signed-pre-keys-pni");
706 if (legacyPniSignedPreKeysPath
.exists()) {
707 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, pniAccountData
.getSignedPreKeyStore());
709 final var legacySessionsPath
= new File(userPath
, "sessions");
710 if (legacySessionsPath
.exists()) {
711 LegacySessionStore
.migrate(legacySessionsPath
,
712 getRecipientResolver(),
713 getRecipientAddressResolver(),
714 aciAccountData
.getSessionStore());
716 final var legacyIdentitiesPath
= new File(userPath
, "identities");
717 if (legacyIdentitiesPath
.exists()) {
718 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
719 getRecipientResolver(),
720 getRecipientAddressResolver(),
721 getIdentityKeyStore());
723 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
724 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
725 LegacyJsonSignalProtocolStore
.class)
727 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
728 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
729 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
732 this.aciAccountData
.setIdentityKeyPair(aciIdentityKeyPair
);
733 this.aciAccountData
.setLocalRegistrationId(registrationId
);
735 loadLegacyStores(rootNode
, legacySignalProtocolStore
);
737 final var legacySenderKeysPath
= new File(userPath
, "sender-keys");
738 if (legacySenderKeysPath
.exists()) {
739 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
740 getRecipientResolver(),
741 getRecipientAddressResolver(),
742 getSenderKeyStore());
744 final var legacySenderKeysSharedPath
= new File(userPath
, "shared-sender-keys-store");
745 if (legacySenderKeysSharedPath
.exists()) {
746 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
747 getRecipientResolver(),
748 getRecipientAddressResolver(),
749 getSenderKeyStore());
751 if (rootNode
.hasNonNull("groupStore")) {
752 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
753 LegacyGroupStore
.Storage
.class);
754 LegacyGroupStore
.migrate(groupStoreStorage
,
755 new File(userPath
, "group-cache"),
756 getRecipientResolver(),
760 if (rootNode
.hasNonNull("stickerStore")) {
761 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
762 LegacyStickerStore
.Storage
.class);
763 LegacyStickerStore
.migrate(storage
, getStickerStore());
766 if (rootNode
.hasNonNull("configurationStore")) {
767 final var configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
768 LegacyConfigurationStore
.Storage
.class);
769 LegacyConfigurationStore
.migrate(configurationStoreStorage
, getConfigurationStore());
772 loadLegacyThreadStore(rootNode
);
775 private void loadLegacyStores(
776 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
778 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
779 if (legacyRecipientStoreNode
!= null) {
780 logger
.debug("Migrating legacy recipient store.");
781 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
782 if (legacyRecipientStore
!= null) {
783 legacyRecipientStore
.getAddresses()
784 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
786 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
789 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
790 logger
.debug("Migrating legacy pre key store.");
791 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
793 aciAccountData
.getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
794 } catch (InvalidMessageException e
) {
795 logger
.warn("Failed to migrate pre key, ignoring", e
);
800 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
801 logger
.debug("Migrating legacy signed pre key store.");
802 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
804 aciAccountData
.getSignedPreKeyStore()
805 .storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
806 } catch (InvalidMessageException e
) {
807 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
812 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
813 logger
.debug("Migrating legacy session store.");
814 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
816 aciAccountData
.getSessionStore()
817 .storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(), session
.deviceId
),
818 new SessionRecord(session
.sessionRecord
));
819 } catch (Exception e
) {
820 logger
.warn("Failed to migrate session, ignoring", e
);
825 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
826 logger
.debug("Migrating legacy identity session store.");
827 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
828 if (identity
.getAddress().serviceId().isEmpty()) {
831 final var serviceId
= identity
.getAddress().serviceId().get();
832 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
833 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
834 identity
.getIdentityKey(),
835 identity
.getTrustLevel());
839 if (rootNode
.hasNonNull("contactStore")) {
840 logger
.debug("Migrating legacy contact store.");
841 final var contactStoreNode
= rootNode
.get("contactStore");
842 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
843 for (var contact
: contactStore
.getContacts()) {
844 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
845 getContactStore().storeContact(recipientId
,
846 new Contact(contact
.name
,
850 contact
.messageExpirationTime
,
859 // Store profile keys only in profile store
860 var profileKeyString
= contact
.profileKey
;
861 if (profileKeyString
!= null) {
862 final ProfileKey profileKey
;
864 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
865 getProfileStore().storeProfileKey(recipientId
, profileKey
);
866 } catch (InvalidInputException e
) {
867 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
873 if (rootNode
.hasNonNull("profileStore")) {
874 logger
.debug("Migrating legacy profile store.");
875 var profileStoreNode
= rootNode
.get("profileStore");
876 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
877 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
878 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.address());
879 // Not migrating profile key credential here, it was changed to expiring profile key credentials
880 getProfileStore().storeProfileKey(recipientId
, profileEntry
.profileKey());
881 final var profile
= profileEntry
.profile();
882 if (profile
!= null) {
883 final var capabilities
= new HashSet
<Profile
.Capability
>();
884 if (profile
.getCapabilities() != null) {
885 if (profile
.getCapabilities().gv1Migration
) {
886 capabilities
.add(Profile
.Capability
.gv1Migration
);
888 if (profile
.getCapabilities().storage
) {
889 capabilities
.add(Profile
.Capability
.storage
);
892 final var newProfile
= new Profile(profileEntry
.lastUpdateTimestamp(),
893 profile
.getGivenName(),
894 profile
.getFamilyName(),
896 profile
.getAboutEmoji(),
899 profile
.isUnrestrictedUnidentifiedAccess()
900 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
901 : profile
.getUnidentifiedAccess() != null
902 ? Profile
.UnidentifiedAccessMode
.ENABLED
903 : Profile
.UnidentifiedAccessMode
.DISABLED
,
905 getProfileStore().storeProfile(recipientId
, newProfile
);
911 private void loadLegacyThreadStore(final JsonNode rootNode
) {
912 var threadStoreNode
= rootNode
.get("threadStore");
913 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
914 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
915 // Migrate thread info to group and contact store
916 for (var thread
: threadStore
.getThreads()) {
917 if (thread
.id
== null || thread
.id
.isEmpty()) {
921 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
922 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
923 var contact
= getContactStore().getContact(recipientId
);
924 if (contact
!= null) {
925 getContactStore().storeContact(recipientId
,
926 Contact
.newBuilder(contact
)
927 .withMessageExpirationTime(thread
.messageExpirationTime
)
931 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
932 if (groupInfo
instanceof GroupInfoV1
) {
933 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
934 getGroupStore().updateGroup(groupInfo
);
937 } catch (Exception e
) {
938 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
944 private void save() {
945 synchronized (fileChannel
) {
946 final var base64
= Base64
.getEncoder();
947 final var storage
= new Storage(CURRENT_STORAGE_VERSION
,
948 serviceEnvironment
.name(),
956 Storage
.AccountData
.from(aciAccountData
),
957 Storage
.AccountData
.from(pniAccountData
),
959 pinMasterKey
== null ?
null : base64
.encodeToString(pinMasterKey
.serialize()),
960 storageKey
== null ?
null : base64
.encodeToString(storageKey
.serialize()),
961 profileKey
== null ?
null : base64
.encodeToString(profileKey
.serialize()));
963 try (var output
= new ByteArrayOutputStream()) {
964 // Write to memory first to prevent corrupting the file in case of serialization errors
965 jsonProcessor
.writeValue(output
, storage
);
966 var input
= new ByteArrayInputStream(output
.toByteArray());
967 fileChannel
.position(0);
968 input
.transferTo(Channels
.newOutputStream(fileChannel
));
969 fileChannel
.truncate(fileChannel
.position());
970 fileChannel
.force(false);
972 } catch (Exception e
) {
973 logger
.error("Error saving file: {}", e
.getMessage(), e
);
978 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
979 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
981 var lock
= fileChannel
.tryLock();
984 logger
.debug("Config file is in use by another instance.");
985 throw new IOException("Config file is in use by another instance.");
987 logger
.info("Config file is in use by another instance, waiting…");
988 lock
= fileChannel
.lock();
989 logger
.info("Config file lock acquired.");
991 final var result
= new Pair
<>(fileChannel
, lock
);
995 if (fileChannel
!= null) {
1001 private void clearAllPreKeys() {
1002 clearAllPreKeys(ServiceIdType
.ACI
);
1003 clearAllPreKeys(ServiceIdType
.PNI
);
1006 private void initAllPreKeyIds() {
1007 resetPreKeyOffsets(ServiceIdType
.ACI
);
1008 resetPreKeyOffsets(ServiceIdType
.PNI
);
1009 resetKyberPreKeyOffsets(ServiceIdType
.ACI
);
1010 resetKyberPreKeyOffsets(ServiceIdType
.PNI
);
1013 private void clearAllPreKeys(ServiceIdType serviceIdType
) {
1014 final var accountData
= getAccountData(serviceIdType
);
1015 resetPreKeyOffsets(serviceIdType
);
1016 resetKyberPreKeyOffsets(serviceIdType
);
1017 accountData
.getPreKeyStore().removeAllPreKeys();
1018 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1019 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1023 private void setPreKeys(ServiceIdType serviceIdType
, PreKeyCollection preKeyCollection
) {
1024 final var accountData
= getAccountData(serviceIdType
);
1025 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1026 preKeyMetadata
.nextSignedPreKeyId
= preKeyCollection
.getSignedPreKey().getId();
1027 preKeyMetadata
.nextKyberPreKeyId
= preKeyCollection
.getLastResortKyberPreKey().getId();
1029 accountData
.getPreKeyStore().removeAllPreKeys();
1030 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1031 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1033 addSignedPreKey(serviceIdType
, preKeyCollection
.getSignedPreKey());
1034 addLastResortKyberPreKey(serviceIdType
, preKeyCollection
.getLastResortKyberPreKey());
1039 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1040 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1041 preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
1042 preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
1043 preKeyMetadata
.activeSignedPreKeyId
= -1;
1047 private static int getRandomPreKeyIdOffset() {
1048 return KeyUtils
.getRandomInt(PREKEY_MAXIMUM_ID
);
1051 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1052 final var accountData
= getAccountData(serviceIdType
);
1053 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1054 logger
.debug("Adding {} {} pre keys with offset {}",
1057 preKeyMetadata
.nextPreKeyId
);
1058 accountData
.getSignalServiceAccountDataStore()
1059 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1060 for (var record : records
) {
1061 if (preKeyMetadata
.nextPreKeyId
!= record.getId()) {
1062 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata
.nextPreKeyId
);
1063 throw new AssertionError("Invalid pre key id");
1065 accountData
.getPreKeyStore().storePreKey(record.getId(), record);
1066 preKeyMetadata
.nextPreKeyId
= (preKeyMetadata
.nextPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1071 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1072 final var accountData
= getAccountData(serviceIdType
);
1073 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1074 logger
.debug("Adding {} signed pre key with offset {}", serviceIdType
, preKeyMetadata
.nextSignedPreKeyId
);
1075 if (preKeyMetadata
.nextSignedPreKeyId
!= record.getId()) {
1076 logger
.error("Invalid signed pre key id {}, expected {}",
1078 preKeyMetadata
.nextSignedPreKeyId
);
1079 throw new AssertionError("Invalid signed pre key id");
1081 accountData
.getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1082 preKeyMetadata
.nextSignedPreKeyId
= (preKeyMetadata
.nextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1083 preKeyMetadata
.activeSignedPreKeyId
= record.getId();
1087 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1088 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1089 preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
1090 preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
1094 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1095 final var accountData
= getAccountData(serviceIdType
);
1096 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1097 logger
.debug("Adding {} {} kyber pre keys with offset {}",
1100 preKeyMetadata
.nextKyberPreKeyId
);
1101 accountData
.getSignalServiceAccountDataStore()
1102 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1103 for (var record : records
) {
1104 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1105 logger
.error("Invalid kyber pre key id {}, expected {}",
1107 preKeyMetadata
.nextKyberPreKeyId
);
1108 throw new AssertionError("Invalid kyber pre key id");
1110 accountData
.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1111 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1116 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1117 final var accountData
= getAccountData(serviceIdType
);
1118 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1119 logger
.debug("Adding {} last resort kyber pre key with offset {}",
1121 preKeyMetadata
.nextKyberPreKeyId
);
1122 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1123 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1125 preKeyMetadata
.nextKyberPreKeyId
);
1126 throw new AssertionError("Invalid last resort kyber pre key id");
1128 accountData
.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1129 preKeyMetadata
.activeLastResortKyberPreKeyId
= record.getId();
1130 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1134 public int getPreviousStorageVersion() {
1135 return previousStorageVersion
;
1138 public AccountData
<?
extends ServiceId
> getAccountData(ServiceIdType serviceIdType
) {
1139 return switch (serviceIdType
) {
1140 case ACI
-> aciAccountData
;
1141 case PNI
-> pniAccountData
;
1145 public AccountData
<?
extends ServiceId
> getAccountData(ServiceId accountIdentifier
) {
1146 if (accountIdentifier
.equals(aciAccountData
.getServiceId())) {
1147 return aciAccountData
;
1148 } else if (accountIdentifier
.equals(pniAccountData
.getServiceId())) {
1149 return pniAccountData
;
1151 throw new IllegalArgumentException("No matching account data found for " + accountIdentifier
);
1155 public SignalServiceDataStore
getSignalServiceDataStore() {
1156 return new SignalServiceDataStore() {
1158 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1159 return getAccountData(accountIdentifier
).getSignalServiceAccountDataStore();
1163 public SignalServiceAccountDataStore
aci() {
1164 return aciAccountData
.getSignalServiceAccountDataStore();
1168 public SignalServiceAccountDataStore
pni() {
1169 return pniAccountData
.getSignalServiceAccountDataStore();
1173 public boolean isMultiDevice() {
1174 return SignalAccount
.this.isMultiDevice();
1179 public IdentityKeyStore
getIdentityKeyStore() {
1180 return getOrCreate(() -> identityKeyStore
,
1181 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(),
1182 settings
.trustNewIdentity(),
1183 getRecipientStore()));
1186 public GroupStore
getGroupStore() {
1187 return getOrCreate(() -> groupStore
,
1188 () -> groupStore
= new GroupStore(getAccountDatabase(),
1189 getRecipientResolver(),
1190 getRecipientIdCreator()));
1193 public ContactsStore
getContactStore() {
1194 return getRecipientStore();
1197 public CdsiStore
getCdsiStore() {
1198 return getOrCreate(() -> cdsiStore
, () -> cdsiStore
= new CdsiStore(getAccountDatabase()));
1201 private RecipientIdCreator
getRecipientIdCreator() {
1202 return recipientId
-> getRecipientStore().create(recipientId
);
1205 public RecipientResolver
getRecipientResolver() {
1206 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1209 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1210 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1213 public RecipientAddressResolver
getRecipientAddressResolver() {
1214 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1217 public RecipientStore
getRecipientStore() {
1218 return getOrCreate(() -> recipientStore
,
1219 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1220 this::getSelfRecipientAddress
,
1221 this::getProfileKey
,
1222 getAccountDatabase()));
1225 public ProfileStore
getProfileStore() {
1226 return getRecipientStore();
1229 public StickerStore
getStickerStore() {
1230 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1233 public SenderKeyStore
getSenderKeyStore() {
1234 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1237 private KeyValueStore
getKeyValueStore() {
1238 return getOrCreate(() -> keyValueStore
, () -> keyValueStore
= new KeyValueStore(getAccountDatabase()));
1241 public UnknownStorageIdStore
getUnknownStorageIdStore() {
1242 return getOrCreate(() -> unknownStorageIdStore
, () -> unknownStorageIdStore
= new UnknownStorageIdStore());
1245 public ConfigurationStore
getConfigurationStore() {
1246 return getOrCreate(() -> configurationStore
,
1247 () -> configurationStore
= new ConfigurationStore(getKeyValueStore(), getRecipientStore()));
1250 public MessageCache
getMessageCache() {
1251 return getOrCreate(() -> messageCache
,
1252 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1255 public AccountDatabase
getAccountDatabase() {
1256 return getOrCreate(() -> accountDatabase
, () -> {
1258 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1259 } catch (SQLException e
) {
1260 throw new RuntimeException(e
);
1265 public MessageSendLogStore
getMessageSendLogStore() {
1266 return getOrCreate(() -> messageSendLogStore
,
1267 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1268 settings
.disableMessageSendLog()));
1271 public CredentialsProvider
getCredentialsProvider() {
1272 return new CredentialsProvider() {
1274 public ACI
getAci() {
1275 return aciAccountData
.getServiceId();
1279 public PNI
getPni() {
1280 return pniAccountData
.getServiceId();
1284 public String
getE164() {
1289 public String
getPassword() {
1294 public int getDeviceId() {
1300 public String
getNumber() {
1304 public void setNumber(final String number
) {
1305 this.number
= number
;
1309 public String
getUsername() {
1313 public void setUsername(final String username
) {
1314 this.username
= username
;
1318 public UsernameLinkComponents
getUsernameLink() {
1319 return usernameLink
;
1322 public void setUsernameLink(final UsernameLinkComponents usernameLink
) {
1323 this.usernameLink
= usernameLink
;
1326 public ServiceEnvironment
getServiceEnvironment() {
1327 return serviceEnvironment
;
1330 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1331 this.serviceEnvironment
= serviceEnvironment
;
1335 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1336 return new AccountAttributes(null,
1337 aciAccountData
.getLocalRegistrationId(),
1341 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1342 getSelfUnidentifiedAccessKey(),
1343 isUnrestrictedUnidentifiedAccess(),
1344 isDiscoverableByPhoneNumber(),
1345 getAccountCapabilities(),
1346 encryptedDeviceName
,
1347 pniAccountData
.getLocalRegistrationId(),
1348 null); // TODO recoveryPassword?
1351 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1352 return getCapabilities(isPrimaryDevice());
1355 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1356 return getAccountData(serviceIdType
).getServiceId();
1359 public ACI
getAci() {
1360 return aciAccountData
.getServiceId();
1363 public void setAci(final ACI aci
) {
1364 this.aciAccountData
.setServiceId(aci
);
1368 public PNI
getPni() {
1369 return pniAccountData
.getServiceId();
1372 public void setPni(final PNI updatedPni
) {
1373 final var oldPni
= pniAccountData
.getServiceId();
1374 if (oldPni
!= null && !oldPni
.equals(updatedPni
)) {
1375 // Clear data for old PNI
1376 identityKeyStore
.deleteIdentity(oldPni
);
1379 this.pniAccountData
.setServiceId(updatedPni
);
1380 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1381 trustSelfIdentity(ServiceIdType
.PNI
);
1385 public void setNewPniIdentity(
1386 final IdentityKeyPair pniIdentityKeyPair
,
1387 final SignedPreKeyRecord pniSignedPreKey
,
1388 final KyberPreKeyRecord lastResortKyberPreKey
,
1389 final int localPniRegistrationId
1391 setPniIdentityKeyPair(pniIdentityKeyPair
);
1392 pniAccountData
.setLocalRegistrationId(localPniRegistrationId
);
1394 final AccountData
<?
extends ServiceId
> accountData
= getAccountData(ServiceIdType
.PNI
);
1395 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1396 preKeyMetadata
.nextSignedPreKeyId
= pniSignedPreKey
.getId();
1397 accountData
.getSignedPreKeyStore().removeSignedPreKey(pniSignedPreKey
.getId());
1398 addSignedPreKey(ServiceIdType
.PNI
, pniSignedPreKey
);
1399 if (lastResortKyberPreKey
!= null) {
1400 preKeyMetadata
.nextKyberPreKeyId
= lastResortKyberPreKey
.getId();
1401 accountData
.getKyberPreKeyStore().removeKyberPreKey(lastResortKyberPreKey
.getId());
1402 addLastResortKyberPreKey(ServiceIdType
.PNI
, lastResortKyberPreKey
);
1407 public SignalServiceAddress
getSelfAddress() {
1408 return new SignalServiceAddress(getAci(), number
);
1411 public RecipientAddress
getSelfRecipientAddress() {
1412 return new RecipientAddress(getAci(), getPni(), number
, username
);
1415 public RecipientId
getSelfRecipientId() {
1416 return selfRecipientId
;
1419 public String
getSessionId(final String forNumber
) {
1420 final var keyValueStore
= getKeyValueStore();
1421 final var sessionNumber
= keyValueStore
.getEntry(verificationSessionNumber
);
1422 if (!forNumber
.equals(sessionNumber
)) {
1425 return keyValueStore
.getEntry(verificationSessionId
);
1428 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1429 final var keyValueStore
= getKeyValueStore();
1430 keyValueStore
.storeEntry(verificationSessionNumber
, sessionNumber
);
1431 keyValueStore
.storeEntry(verificationSessionId
, sessionId
);
1434 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1435 this.encryptedDeviceName
= encryptedDeviceName
;
1439 public int getDeviceId() {
1443 public boolean isPrimaryDevice() {
1444 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1447 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1448 return getAccountData(serviceIdType
).getIdentityKeyPair();
1451 public IdentityKeyPair
getAciIdentityKeyPair() {
1452 return aciAccountData
.getIdentityKeyPair();
1455 public IdentityKeyPair
getPniIdentityKeyPair() {
1456 return pniAccountData
.getIdentityKeyPair();
1459 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1460 pniAccountData
.setIdentityKeyPair(identityKeyPair
);
1461 trustSelfIdentity(ServiceIdType
.PNI
);
1465 public String
getPassword() {
1469 public void setRegistrationLockPin(final String registrationLockPin
) {
1470 this.registrationLockPin
= registrationLockPin
;
1474 public String
getRegistrationLockPin() {
1475 return registrationLockPin
;
1478 public String
getRegistrationLock() {
1479 final var masterKey
= getPinBackedMasterKey();
1480 if (masterKey
== null) {
1483 return masterKey
.deriveRegistrationLock();
1486 public MasterKey
getPinBackedMasterKey() {
1487 if (registrationLockPin
== null) {
1490 return pinMasterKey
;
1493 public MasterKey
getOrCreatePinMasterKey() {
1494 if (pinMasterKey
== null) {
1495 pinMasterKey
= KeyUtils
.createMasterKey();
1498 return pinMasterKey
;
1501 public void setMasterKey(MasterKey masterKey
) {
1502 if (isPrimaryDevice()) {
1505 this.pinMasterKey
= masterKey
;
1509 public StorageKey
getOrCreateStorageKey() {
1510 if (pinMasterKey
!= null) {
1511 return pinMasterKey
.deriveStorageServiceKey();
1512 } else if (storageKey
!= null) {
1514 } else if (!isPrimaryDevice() || !isMultiDevice()) {
1515 // Only upload storage, if a pin master key already exists or linked devices exist
1519 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1522 public void setStorageKey(final StorageKey storageKey
) {
1523 if (isPrimaryDevice() || storageKey
.equals(this.storageKey
)) {
1526 this.storageKey
= storageKey
;
1530 public long getStorageManifestVersion() {
1531 return getKeyValueStore().getEntry(storageManifestVersion
);
1534 public void setStorageManifestVersion(final long value
) {
1535 getKeyValueStore().storeEntry(storageManifestVersion
, value
);
1538 public Optional
<SignalStorageManifest
> getStorageManifest() {
1539 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1540 if (!storageManifestFile
.exists()) {
1541 return Optional
.empty();
1543 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1544 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1545 } catch (IOException e
) {
1546 logger
.warn("Failed to read local storage manifest.", e
);
1547 return Optional
.empty();
1551 public void setStorageManifest(SignalStorageManifest manifest
) {
1552 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1553 if (manifest
== null) {
1554 if (storageManifestFile
.exists()) {
1556 Files
.delete(storageManifestFile
.toPath());
1557 } catch (IOException e
) {
1558 logger
.error("Failed to delete local storage manifest.", e
);
1564 final var manifestBytes
= manifest
.serialize();
1565 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1566 outputStream
.write(manifestBytes
);
1567 } catch (IOException e
) {
1568 logger
.error("Failed to store local storage manifest.", e
);
1572 public byte[] getCdsiToken() {
1573 return getKeyValueStore().getEntry(cdsiToken
);
1576 public void setCdsiToken(final byte[] value
) {
1577 getKeyValueStore().storeEntry(cdsiToken
, value
);
1580 public Long
getLastRecipientsRefresh() {
1581 return getKeyValueStore().getEntry(lastRecipientsRefresh
);
1584 public void setLastRecipientsRefresh(final Long value
) {
1585 getKeyValueStore().storeEntry(lastRecipientsRefresh
, value
);
1588 public ProfileKey
getProfileKey() {
1592 public void setProfileKey(final ProfileKey profileKey
) {
1593 if (profileKey
.equals(this.profileKey
)) {
1596 this.profileKey
= profileKey
;
1600 public byte[] getSelfUnidentifiedAccessKey() {
1601 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1604 public boolean isRegistered() {
1608 public void setRegistered(final boolean registered
) {
1609 this.registered
= registered
;
1613 public boolean isMultiDevice() {
1614 return isMultiDevice
;
1617 public void setMultiDevice(final boolean multiDevice
) {
1618 if (isMultiDevice
== multiDevice
) {
1621 isMultiDevice
= multiDevice
;
1625 public long getLastReceiveTimestamp() {
1626 return getKeyValueStore().getEntry(lastReceiveTimestamp
);
1629 public void setLastReceiveTimestamp(final long value
) {
1630 getKeyValueStore().storeEntry(lastReceiveTimestamp
, value
);
1633 public boolean isUnrestrictedUnidentifiedAccess() {
1634 return Boolean
.TRUE
.equals(getKeyValueStore().getEntry(unrestrictedUnidentifiedAccess
));
1637 public void setUnrestrictedUnidentifiedAccess(boolean value
) {
1638 getKeyValueStore().storeEntry(unrestrictedUnidentifiedAccess
, value
);
1641 public boolean isDiscoverableByPhoneNumber() {
1642 final var phoneNumberUnlisted
= getConfigurationStore().getPhoneNumberUnlisted();
1643 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1646 private void trustSelfIdentity(ServiceIdType serviceIdType
) {
1647 final var accountData
= getAccountData(serviceIdType
);
1648 final var serviceId
= accountData
.getServiceId();
1649 final var identityKeyPair
= accountData
.getIdentityKeyPair();
1650 if (serviceId
== null || identityKeyPair
== null) {
1653 final var publicKey
= identityKeyPair
.getPublicKey();
1654 getIdentityKeyStore().saveIdentity(serviceId
, publicKey
);
1655 getIdentityKeyStore().setIdentityTrustLevel(serviceId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1658 public void deleteAccountData() throws IOException
{
1660 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1661 .sorted(Comparator
.reverseOrder())) {
1662 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1663 Files
.delete(file
.next());
1666 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1670 public void close() {
1671 synchronized (fileChannel
) {
1672 if (accountDatabase
!= null) {
1673 accountDatabase
.close();
1675 if (messageSendLogStore
!= null) {
1676 messageSendLogStore
.close();
1681 } catch (ClosedChannelException ignored
) {
1683 fileChannel
.close();
1684 } catch (IOException e
) {
1685 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1690 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1691 var value
= supplier
.get();
1692 if (value
!= null) {
1696 synchronized (LOCK
) {
1697 value
= supplier
.get();
1698 if (value
!= null) {
1702 return supplier
.get();
1706 private interface Callable
{
1711 public static class PreKeyMetadata
{
1713 private int nextPreKeyId
= 1;
1714 private int nextSignedPreKeyId
= 1;
1715 private int activeSignedPreKeyId
= -1;
1716 private int nextKyberPreKeyId
= 1;
1717 private int activeLastResortKyberPreKeyId
= -1;
1719 public int getNextPreKeyId() {
1720 return nextPreKeyId
;
1723 public int getNextSignedPreKeyId() {
1724 return nextSignedPreKeyId
;
1727 public int getActiveSignedPreKeyId() {
1728 return activeSignedPreKeyId
;
1731 public int getNextKyberPreKeyId() {
1732 return nextKyberPreKeyId
;
1735 public int getActiveLastResortKyberPreKeyId() {
1736 return activeLastResortKyberPreKeyId
;
1740 public class AccountData
<SERVICE_ID
extends ServiceId
> {
1742 private final ServiceIdType serviceIdType
;
1743 private SERVICE_ID serviceId
;
1744 private IdentityKeyPair identityKeyPair
;
1745 private int localRegistrationId
;
1746 private final PreKeyMetadata preKeyMetadata
= new PreKeyMetadata();
1748 private SignalProtocolStore signalProtocolStore
;
1749 private PreKeyStore preKeyStore
;
1750 private SignedPreKeyStore signedPreKeyStore
;
1751 private KyberPreKeyStore kyberPreKeyStore
;
1752 private SessionStore sessionStore
;
1753 private SignalIdentityKeyStore identityKeyStore
;
1755 private AccountData(final ServiceIdType serviceIdType
) {
1756 this.serviceIdType
= serviceIdType
;
1759 public SERVICE_ID
getServiceId() {
1763 private void setServiceId(final SERVICE_ID serviceId
) {
1764 this.serviceId
= serviceId
;
1767 public IdentityKeyPair
getIdentityKeyPair() {
1768 return identityKeyPair
;
1771 private void setIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1772 this.identityKeyPair
= identityKeyPair
;
1775 public int getLocalRegistrationId() {
1776 return localRegistrationId
;
1779 private void setLocalRegistrationId(final int localRegistrationId
) {
1780 this.localRegistrationId
= localRegistrationId
;
1781 this.identityKeyStore
= null;
1784 public PreKeyMetadata
getPreKeyMetadata() {
1785 return preKeyMetadata
;
1788 private SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1789 return getOrCreate(() -> signalProtocolStore
,
1790 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
1791 getSignedPreKeyStore(),
1792 getKyberPreKeyStore(),
1794 getIdentityKeyStore(),
1795 getSenderKeyStore(),
1796 SignalAccount
.this::isMultiDevice
));
1799 public PreKeyStore
getPreKeyStore() {
1800 return getOrCreate(() -> preKeyStore
,
1801 () -> preKeyStore
= new PreKeyStore(getAccountDatabase(), serviceIdType
));
1804 public SignedPreKeyStore
getSignedPreKeyStore() {
1805 return getOrCreate(() -> signedPreKeyStore
,
1806 () -> signedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), serviceIdType
));
1809 public KyberPreKeyStore
getKyberPreKeyStore() {
1810 return getOrCreate(() -> kyberPreKeyStore
,
1811 () -> kyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), serviceIdType
));
1814 public SessionStore
getSessionStore() {
1815 return getOrCreate(() -> sessionStore
,
1816 () -> sessionStore
= new SessionStore(getAccountDatabase(), serviceIdType
));
1819 public SignalIdentityKeyStore
getIdentityKeyStore() {
1820 return getOrCreate(() -> identityKeyStore
,
1821 () -> identityKeyStore
= new SignalIdentityKeyStore(() -> identityKeyPair
,
1822 localRegistrationId
,
1823 SignalAccount
.this.getIdentityKeyStore()));
1827 public record Storage(
1829 String serviceEnvironment
,
1833 String encryptedDeviceName
,
1835 boolean isMultiDevice
,
1837 AccountData aciAccountData
,
1838 AccountData pniAccountData
,
1839 String registrationLockPin
,
1840 String pinMasterKey
,
1845 public record AccountData(
1848 String identityPrivateKey
,
1849 String identityPublicKey
,
1852 int nextSignedPreKeyId
,
1853 int activeSignedPreKeyId
,
1854 int nextKyberPreKeyId
,
1855 int activeLastResortKyberPreKeyId
1858 private static AccountData
from(final SignalAccount
.AccountData
<?
> accountData
) {
1859 final var base64
= Base64
.getEncoder();
1860 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1861 return new AccountData(accountData
.getServiceId() == null
1863 : accountData
.getServiceId().toString(),
1864 accountData
.getLocalRegistrationId(),
1865 accountData
.getIdentityKeyPair() == null
1867 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPrivateKey().serialize()),
1868 accountData
.getIdentityKeyPair() == null
1870 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPublicKey().serialize()),
1871 preKeyMetadata
.getNextPreKeyId(),
1872 preKeyMetadata
.getNextSignedPreKeyId(),
1873 preKeyMetadata
.getActiveSignedPreKeyId(),
1874 preKeyMetadata
.getNextKyberPreKeyId(),
1875 preKeyMetadata
.getActiveLastResortKyberPreKeyId());