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
.UUID
;
106 import java
.util
.function
.Function
;
107 import java
.util
.function
.Supplier
;
109 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.PREKEY_MAXIMUM_ID
;
110 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.getCapabilities
;
112 public class SignalAccount
implements Closeable
{
114 private static final Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
116 private static final int MINIMUM_STORAGE_VERSION
= 1;
117 private static final int CURRENT_STORAGE_VERSION
= 8;
119 private final Object LOCK
= new Object();
121 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
123 private final FileChannel fileChannel
;
124 private final FileLock lock
;
126 private int previousStorageVersion
;
128 private File dataPath
;
129 private String accountPath
;
131 private ServiceEnvironment serviceEnvironment
;
132 private String number
;
133 private String username
;
134 private UsernameLinkComponents usernameLink
;
135 private String encryptedDeviceName
;
136 private int deviceId
= 0;
137 private String password
;
138 private String registrationLockPin
;
139 private MasterKey pinMasterKey
;
140 private StorageKey storageKey
;
141 private ProfileKey profileKey
;
143 private Settings settings
;
145 private final KeyValueEntry
<String
> verificationSessionId
= new KeyValueEntry
<>("verification-session-id",
147 private final KeyValueEntry
<String
> verificationSessionNumber
= new KeyValueEntry
<>("verification-session-number",
149 private final KeyValueEntry
<Long
> lastReceiveTimestamp
= new KeyValueEntry
<>("last-receive-timestamp",
152 private final KeyValueEntry
<byte[]> cdsiToken
= new KeyValueEntry
<>("cdsi-token", byte[].class);
153 private final KeyValueEntry
<Long
> lastRecipientsRefresh
= new KeyValueEntry
<>("last-recipients-refresh",
155 private final KeyValueEntry
<Long
> storageManifestVersion
= new KeyValueEntry
<>("storage-manifest-version",
158 private final KeyValueEntry
<Boolean
> unrestrictedUnidentifiedAccess
= new KeyValueEntry
<>(
159 "unrestricted-unidentified-access",
162 private boolean isMultiDevice
= false;
163 private boolean registered
= false;
165 private final AccountData
<ACI
> aciAccountData
= new AccountData
<>(ServiceIdType
.ACI
);
166 private final AccountData
<PNI
> pniAccountData
= new AccountData
<>(ServiceIdType
.PNI
);
167 private IdentityKeyStore identityKeyStore
;
168 private SenderKeyStore senderKeyStore
;
169 private GroupStore groupStore
;
170 private RecipientStore recipientStore
;
171 private StickerStore stickerStore
;
172 private UnknownStorageIdStore unknownStorageIdStore
;
173 private ConfigurationStore configurationStore
;
174 private KeyValueStore keyValueStore
;
175 private CdsiStore cdsiStore
;
177 private MessageCache messageCache
;
178 private MessageSendLogStore messageSendLogStore
;
180 private AccountDatabase accountDatabase
;
181 private RecipientId selfRecipientId
;
183 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
184 this.fileChannel
= fileChannel
;
188 public static SignalAccount
load(
189 File dataPath
, String accountPath
, boolean waitForLock
, final Settings settings
190 ) throws IOException
{
191 logger
.trace("Opening account file");
192 final var fileName
= getFileName(dataPath
, accountPath
);
193 final var pair
= openFileChannel(fileName
, waitForLock
);
195 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
196 signalAccount
.load(dataPath
, accountPath
, settings
);
197 signalAccount
.migrateLegacyConfigs();
198 signalAccount
.init();
200 return signalAccount
;
201 } catch (Throwable e
) {
202 pair
.second().close();
203 pair
.first().close();
208 public static SignalAccount
create(
212 ServiceEnvironment serviceEnvironment
,
213 IdentityKeyPair aciIdentityKey
,
214 IdentityKeyPair pniIdentityKey
,
215 ProfileKey profileKey
,
216 final Settings settings
217 ) throws IOException
{
218 IOUtils
.createPrivateDirectories(dataPath
);
219 var fileName
= getFileName(dataPath
, accountPath
);
220 if (!fileName
.exists()) {
221 IOUtils
.createPrivateFile(fileName
);
224 final var pair
= openFileChannel(fileName
, true);
225 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
227 signalAccount
.accountPath
= accountPath
;
228 signalAccount
.number
= number
;
229 signalAccount
.serviceEnvironment
= serviceEnvironment
;
230 signalAccount
.profileKey
= profileKey
;
231 signalAccount
.password
= KeyUtils
.createPassword();
232 signalAccount
.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
234 signalAccount
.dataPath
= dataPath
;
235 signalAccount
.aciAccountData
.setIdentityKeyPair(aciIdentityKey
);
236 signalAccount
.pniAccountData
.setIdentityKeyPair(pniIdentityKey
);
237 signalAccount
.aciAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
238 signalAccount
.pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
239 signalAccount
.initAllPreKeyIds();
240 signalAccount
.settings
= settings
;
242 signalAccount
.registered
= false;
244 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
245 signalAccount
.init();
246 signalAccount
.save();
248 return signalAccount
;
251 public static SignalAccount
createLinkedAccount(
253 final String accountPath
,
254 final ServiceEnvironment serviceEnvironment
,
255 final Settings settings
256 ) throws IOException
{
257 IOUtils
.createPrivateDirectories(dataPath
);
258 var fileName
= getFileName(dataPath
, accountPath
);
259 IOUtils
.createPrivateFile(fileName
);
261 final var pair
= openFileChannel(fileName
, true);
262 final var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
264 signalAccount
.dataPath
= dataPath
;
265 signalAccount
.accountPath
= accountPath
;
266 signalAccount
.serviceEnvironment
= serviceEnvironment
;
267 signalAccount
.aciAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
268 signalAccount
.pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
269 signalAccount
.settings
= settings
;
271 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
273 return signalAccount
;
276 public void setProvisioningData(
280 final String password
,
281 final String encryptedDeviceName
,
282 final IdentityKeyPair aciIdentity
,
283 final IdentityKeyPair pniIdentity
,
284 final ProfileKey profileKey
,
285 final MasterKey masterKey
288 this.number
= number
;
289 this.aciAccountData
.setServiceId(aci
);
290 this.pniAccountData
.setServiceId(pni
);
292 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
293 this.password
= password
;
294 this.profileKey
= profileKey
;
295 this.encryptedDeviceName
= encryptedDeviceName
;
296 this.aciAccountData
.setIdentityKeyPair(aciIdentity
);
297 this.pniAccountData
.setIdentityKeyPair(pniIdentity
);
298 this.registered
= false;
299 this.isMultiDevice
= true;
300 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
301 this.pinMasterKey
= masterKey
;
302 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
303 this.setStorageManifest(null);
304 this.storageKey
= null;
305 getSenderKeyStore().deleteAll();
306 trustSelfIdentity(ServiceIdType
.ACI
);
307 trustSelfIdentity(ServiceIdType
.PNI
);
308 aciAccountData
.getSessionStore().archiveAllSessions();
309 pniAccountData
.getSessionStore().archiveAllSessions();
311 getKeyValueStore().storeEntry(lastRecipientsRefresh
, null);
315 public void finishLinking(
316 final int deviceId
, final PreKeyCollection aciPreKeys
, final PreKeyCollection pniPreKeys
318 this.registered
= true;
319 this.deviceId
= deviceId
;
320 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
321 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
325 public void finishRegistration(
328 final MasterKey masterKey
,
330 final PreKeyCollection aciPreKeys
,
331 final PreKeyCollection pniPreKeys
333 this.pinMasterKey
= masterKey
;
334 getKeyValueStore().storeEntry(storageManifestVersion
, -1L);
335 this.setStorageManifest(null);
336 this.storageKey
= null;
337 this.encryptedDeviceName
= null;
338 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
339 this.isMultiDevice
= false;
340 this.registered
= true;
341 this.aciAccountData
.setServiceId(aci
);
342 this.pniAccountData
.setServiceId(pni
);
344 this.registrationLockPin
= pin
;
345 getKeyValueStore().storeEntry(lastReceiveTimestamp
, 0L);
348 setPreKeys(ServiceIdType
.ACI
, aciPreKeys
);
349 setPreKeys(ServiceIdType
.PNI
, pniPreKeys
);
350 aciAccountData
.getSessionStore().archiveAllSessions();
351 pniAccountData
.getSessionStore().archiveAllSessions();
352 getSenderKeyStore().deleteAll();
353 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
354 trustSelfIdentity(ServiceIdType
.ACI
);
355 trustSelfIdentity(ServiceIdType
.PNI
);
356 getKeyValueStore().storeEntry(lastRecipientsRefresh
, null);
359 public void initDatabase() {
360 getAccountDatabase();
363 private void init() {
364 this.selfRecipientId
= getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
367 private void migrateLegacyConfigs() {
368 if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
369 logger
.trace("Migrating legacy parts of account file");
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 logger
.trace("Loading account file {}", accountPath
);
442 this.dataPath
= dataPath
;
443 this.accountPath
= accountPath
;
444 this.settings
= settings
;
445 final JsonNode rootNode
;
446 synchronized (fileChannel
) {
447 fileChannel
.position(0);
448 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
451 var migratedLegacyConfig
= false;
453 if (rootNode
.hasNonNull("version")) {
454 var accountVersion
= rootNode
.get("version").asInt(1);
455 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
456 throw new IOException("Config file was created by a more recent version: " + accountVersion
);
457 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
458 throw new IOException("Config file was created by a no longer supported older version: "
461 previousStorageVersion
= accountVersion
;
462 if (accountVersion
< CURRENT_STORAGE_VERSION
) {
463 migratedLegacyConfig
= true;
467 if (previousStorageVersion
< 8) {
468 final var userPath
= getUserPath(dataPath
, accountPath
);
469 loadLegacyFile(userPath
, rootNode
);
470 migratedLegacyConfig
= true;
472 final var storage
= jsonProcessor
.convertValue(rootNode
, Storage
.class);
473 serviceEnvironment
= ServiceEnvironment
.valueOf(storage
.serviceEnvironment
);
474 registered
= storage
.registered
;
475 number
= storage
.number
;
476 username
= storage
.username
;
477 if ("".equals(username
)) {
480 encryptedDeviceName
= storage
.encryptedDeviceName
;
481 deviceId
= storage
.deviceId
;
482 isMultiDevice
= storage
.isMultiDevice
;
483 password
= storage
.password
;
484 setAccountData(aciAccountData
, storage
.aciAccountData
, ACI
::parseOrThrow
);
485 setAccountData(pniAccountData
, storage
.pniAccountData
, PNI
::parseOrThrow
);
486 registrationLockPin
= storage
.registrationLockPin
;
487 final var base64
= Base64
.getDecoder();
488 if (storage
.pinMasterKey
!= null) {
489 pinMasterKey
= new MasterKey(base64
.decode(storage
.pinMasterKey
));
491 if (storage
.storageKey
!= null) {
492 storageKey
= new StorageKey(base64
.decode(storage
.storageKey
));
494 if (storage
.profileKey
!= null) {
496 profileKey
= new ProfileKey(base64
.decode(storage
.profileKey
));
497 } catch (InvalidInputException e
) {
498 throw new IOException(
499 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
503 if (storage
.usernameLinkEntropy
!= null && storage
.usernameLinkServerId
!= null) {
504 usernameLink
= new UsernameLinkComponents(base64
.decode(storage
.usernameLinkEntropy
),
505 UUID
.fromString(storage
.usernameLinkServerId
));
509 if (migratedLegacyConfig
) {
514 private <SERVICE_ID
extends ServiceId
> void setAccountData(
515 AccountData
<SERVICE_ID
> accountData
,
516 Storage
.AccountData storage
,
517 Function
<String
, SERVICE_ID
> serviceIdParser
518 ) throws IOException
{
519 if (storage
.serviceId
!= null) {
521 accountData
.setServiceId(serviceIdParser
.apply(storage
.serviceId
));
522 } catch (IllegalArgumentException e
) {
523 throw new IOException("Config file contains an invalid serviceId, needs to be a valid UUID", e
);
526 accountData
.setLocalRegistrationId(storage
.registrationId
);
527 if (storage
.identityPrivateKey
!= null && storage
.identityPublicKey
!= null) {
528 final var base64
= Base64
.getDecoder();
529 final var publicKeyBytes
= base64
.decode(storage
.identityPublicKey
);
530 final var privateKeyBytes
= base64
.decode(storage
.identityPrivateKey
);
531 final var keyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
532 accountData
.setIdentityKeyPair(keyPair
);
534 accountData
.preKeyMetadata
.nextPreKeyId
= storage
.nextPreKeyId
;
535 accountData
.preKeyMetadata
.nextSignedPreKeyId
= storage
.nextSignedPreKeyId
;
536 accountData
.preKeyMetadata
.activeSignedPreKeyId
= storage
.activeSignedPreKeyId
;
537 accountData
.preKeyMetadata
.nextKyberPreKeyId
= storage
.nextKyberPreKeyId
;
538 accountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= storage
.activeLastResortKyberPreKeyId
;
541 private void loadLegacyFile(final File userPath
, final JsonNode rootNode
) throws IOException
{
542 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
543 if (rootNode
.hasNonNull("password")) {
544 password
= rootNode
.get("password").asText();
546 if (password
== null) {
547 password
= KeyUtils
.createPassword();
550 if (rootNode
.hasNonNull("serviceEnvironment")) {
551 serviceEnvironment
= ServiceEnvironment
.valueOf(rootNode
.get("serviceEnvironment").asText());
553 if (serviceEnvironment
== null) {
554 serviceEnvironment
= ServiceEnvironment
.LIVE
;
556 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
557 if (rootNode
.hasNonNull("usernameIdentifier")) {
558 username
= rootNode
.get("usernameIdentifier").asText();
559 if ("".equals(username
)) {
563 if (rootNode
.hasNonNull("uuid")) {
565 aciAccountData
.setServiceId(ACI
.parseOrThrow(rootNode
.get("uuid").asText()));
566 } catch (IllegalArgumentException e
) {
567 throw new IOException("Config file contains an invalid aci/uuid, needs to be a valid UUID", e
);
570 if (rootNode
.hasNonNull("pni")) {
572 pniAccountData
.setServiceId(PNI
.parseOrThrow(rootNode
.get("pni").asText()));
573 } catch (IllegalArgumentException e
) {
574 throw new IOException("Config file contains an invalid pni, needs to be a valid UUID", e
);
577 if (rootNode
.hasNonNull("sessionId")) {
578 getKeyValueStore().storeEntry(verificationSessionId
, rootNode
.get("sessionId").asText());
580 if (rootNode
.hasNonNull("sessionNumber")) {
581 getKeyValueStore().storeEntry(verificationSessionNumber
, rootNode
.get("sessionNumber").asText());
583 if (rootNode
.hasNonNull("deviceName")) {
584 encryptedDeviceName
= rootNode
.get("deviceName").asText();
586 if (rootNode
.hasNonNull("deviceId")) {
587 deviceId
= rootNode
.get("deviceId").asInt();
589 if (rootNode
.hasNonNull("isMultiDevice")) {
590 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
592 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
593 getKeyValueStore().storeEntry(lastReceiveTimestamp
, rootNode
.get("lastReceiveTimestamp").asLong());
595 int registrationId
= 0;
596 if (rootNode
.hasNonNull("registrationId")) {
597 registrationId
= rootNode
.get("registrationId").asInt();
599 if (rootNode
.hasNonNull("pniRegistrationId")) {
600 pniAccountData
.setLocalRegistrationId(rootNode
.get("pniRegistrationId").asInt());
602 pniAccountData
.setLocalRegistrationId(KeyHelper
.generateRegistrationId(false));
604 IdentityKeyPair aciIdentityKeyPair
= null;
605 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
606 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
607 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
608 aciIdentityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
610 if (rootNode
.hasNonNull("pniIdentityPrivateKey") && rootNode
.hasNonNull("pniIdentityKey")) {
611 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityKey").asText());
612 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("pniIdentityPrivateKey").asText());
613 pniAccountData
.setIdentityKeyPair(KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
));
616 if (rootNode
.hasNonNull("registrationLockPin")) {
617 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
619 if (rootNode
.hasNonNull("pinMasterKey")) {
620 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
622 if (rootNode
.hasNonNull("storageKey")) {
623 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
625 if (rootNode
.hasNonNull("storageManifestVersion")) {
626 getKeyValueStore().storeEntry(storageManifestVersion
, rootNode
.get("storageManifestVersion").asLong());
628 if (rootNode
.hasNonNull("preKeyIdOffset")) {
629 aciAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("preKeyIdOffset").asInt(1);
631 aciAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
633 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
634 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
636 aciAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
638 if (rootNode
.hasNonNull("activeSignedPreKeyId")) {
639 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("activeSignedPreKeyId").asInt(-1);
641 aciAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
643 if (rootNode
.hasNonNull("pniPreKeyIdOffset")) {
644 pniAccountData
.preKeyMetadata
.nextPreKeyId
= rootNode
.get("pniPreKeyIdOffset").asInt(1);
646 pniAccountData
.preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
648 if (rootNode
.hasNonNull("pniNextSignedPreKeyId")) {
649 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= rootNode
.get("pniNextSignedPreKeyId").asInt(1);
651 pniAccountData
.preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
653 if (rootNode
.hasNonNull("pniActiveSignedPreKeyId")) {
654 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= rootNode
.get("pniActiveSignedPreKeyId").asInt(-1);
656 pniAccountData
.preKeyMetadata
.activeSignedPreKeyId
= -1;
658 if (rootNode
.hasNonNull("kyberPreKeyIdOffset")) {
659 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("kyberPreKeyIdOffset").asInt(1);
661 aciAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
663 if (rootNode
.hasNonNull("activeLastResortKyberPreKeyId")) {
664 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get("activeLastResortKyberPreKeyId")
667 aciAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
669 if (rootNode
.hasNonNull("pniKyberPreKeyIdOffset")) {
670 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= rootNode
.get("pniKyberPreKeyIdOffset").asInt(1);
672 pniAccountData
.preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
674 if (rootNode
.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
675 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= rootNode
.get(
676 "pniActiveLastResortKyberPreKeyId").asInt(-1);
678 pniAccountData
.preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
680 if (rootNode
.hasNonNull("profileKey")) {
682 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
683 } catch (InvalidInputException e
) {
684 throw new IOException(
685 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
689 if (profileKey
== null) {
690 // Old config file, creating new profile key
691 setProfileKey(KeyUtils
.createProfileKey());
694 if (previousStorageVersion
< 5) {
695 final var legacyRecipientsStoreFile
= new File(userPath
, "recipients-store");
696 if (legacyRecipientsStoreFile
.exists()) {
697 LegacyRecipientStore2
.migrate(legacyRecipientsStoreFile
, getRecipientStore());
700 if (previousStorageVersion
< 6) {
701 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
703 final var legacyAciPreKeysPath
= new File(userPath
, "pre-keys");
704 if (legacyAciPreKeysPath
.exists()) {
705 LegacyPreKeyStore
.migrate(legacyAciPreKeysPath
, aciAccountData
.getPreKeyStore());
707 final var legacyPniPreKeysPath
= new File(userPath
, "pre-keys-pni");
708 if (legacyPniPreKeysPath
.exists()) {
709 LegacyPreKeyStore
.migrate(legacyPniPreKeysPath
, pniAccountData
.getPreKeyStore());
711 final var legacyAciSignedPreKeysPath
= new File(userPath
, "signed-pre-keys");
712 if (legacyAciSignedPreKeysPath
.exists()) {
713 LegacySignedPreKeyStore
.migrate(legacyAciSignedPreKeysPath
, aciAccountData
.getSignedPreKeyStore());
715 final var legacyPniSignedPreKeysPath
= new File(userPath
, "signed-pre-keys-pni");
716 if (legacyPniSignedPreKeysPath
.exists()) {
717 LegacySignedPreKeyStore
.migrate(legacyPniSignedPreKeysPath
, pniAccountData
.getSignedPreKeyStore());
719 final var legacySessionsPath
= new File(userPath
, "sessions");
720 if (legacySessionsPath
.exists()) {
721 LegacySessionStore
.migrate(legacySessionsPath
,
722 getRecipientResolver(),
723 getRecipientAddressResolver(),
724 aciAccountData
.getSessionStore());
726 final var legacyIdentitiesPath
= new File(userPath
, "identities");
727 if (legacyIdentitiesPath
.exists()) {
728 LegacyIdentityKeyStore
.migrate(legacyIdentitiesPath
,
729 getRecipientResolver(),
730 getRecipientAddressResolver(),
731 getIdentityKeyStore());
733 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
734 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
735 LegacyJsonSignalProtocolStore
.class)
737 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
738 aciIdentityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
739 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
742 this.aciAccountData
.setIdentityKeyPair(aciIdentityKeyPair
);
743 this.aciAccountData
.setLocalRegistrationId(registrationId
);
745 loadLegacyStores(rootNode
, legacySignalProtocolStore
);
747 final var legacySenderKeysPath
= new File(userPath
, "sender-keys");
748 if (legacySenderKeysPath
.exists()) {
749 LegacySenderKeyRecordStore
.migrate(legacySenderKeysPath
,
750 getRecipientResolver(),
751 getRecipientAddressResolver(),
752 getSenderKeyStore());
754 final var legacySenderKeysSharedPath
= new File(userPath
, "shared-sender-keys-store");
755 if (legacySenderKeysSharedPath
.exists()) {
756 LegacySenderKeySharedStore
.migrate(legacySenderKeysSharedPath
,
757 getRecipientResolver(),
758 getRecipientAddressResolver(),
759 getSenderKeyStore());
761 if (rootNode
.hasNonNull("groupStore")) {
762 final var groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"),
763 LegacyGroupStore
.Storage
.class);
764 LegacyGroupStore
.migrate(groupStoreStorage
,
765 new File(userPath
, "group-cache"),
766 getRecipientResolver(),
770 if (rootNode
.hasNonNull("stickerStore")) {
771 final var storage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"),
772 LegacyStickerStore
.Storage
.class);
773 LegacyStickerStore
.migrate(storage
, getStickerStore());
776 if (rootNode
.hasNonNull("configurationStore")) {
777 final var configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
778 LegacyConfigurationStore
.Storage
.class);
779 LegacyConfigurationStore
.migrate(configurationStoreStorage
, getConfigurationStore());
782 loadLegacyThreadStore(rootNode
);
785 private void loadLegacyStores(
786 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
788 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
789 if (legacyRecipientStoreNode
!= null) {
790 logger
.debug("Migrating legacy recipient store.");
791 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
792 if (legacyRecipientStore
!= null) {
793 legacyRecipientStore
.getAddresses()
794 .forEach(recipient
-> getRecipientStore().resolveRecipientTrusted(recipient
));
796 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
799 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
800 logger
.debug("Migrating legacy pre key store.");
801 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
803 aciAccountData
.getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
804 } catch (InvalidMessageException e
) {
805 logger
.warn("Failed to migrate pre key, ignoring", e
);
810 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
811 logger
.debug("Migrating legacy signed pre key store.");
812 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
814 aciAccountData
.getSignedPreKeyStore()
815 .storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
816 } catch (InvalidMessageException e
) {
817 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
822 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
823 logger
.debug("Migrating legacy session store.");
824 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
826 aciAccountData
.getSessionStore()
827 .storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(), session
.deviceId
),
828 new SessionRecord(session
.sessionRecord
));
829 } catch (Exception e
) {
830 logger
.warn("Failed to migrate session, ignoring", e
);
835 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
836 logger
.debug("Migrating legacy identity session store.");
837 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
838 if (identity
.getAddress().serviceId().isEmpty()) {
841 final var serviceId
= identity
.getAddress().serviceId().get();
842 getIdentityKeyStore().saveIdentity(serviceId
, identity
.getIdentityKey());
843 getIdentityKeyStore().setIdentityTrustLevel(serviceId
,
844 identity
.getIdentityKey(),
845 identity
.getTrustLevel());
849 if (rootNode
.hasNonNull("contactStore")) {
850 logger
.debug("Migrating legacy contact store.");
851 final var contactStoreNode
= rootNode
.get("contactStore");
852 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
853 for (var contact
: contactStore
.getContacts()) {
854 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
855 getContactStore().storeContact(recipientId
,
856 new Contact(contact
.name
,
860 contact
.messageExpirationTime
,
869 // Store profile keys only in profile store
870 var profileKeyString
= contact
.profileKey
;
871 if (profileKeyString
!= null) {
872 final ProfileKey profileKey
;
874 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
875 getProfileStore().storeProfileKey(recipientId
, profileKey
);
876 } catch (InvalidInputException e
) {
877 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
883 if (rootNode
.hasNonNull("profileStore")) {
884 logger
.debug("Migrating legacy profile store.");
885 var profileStoreNode
= rootNode
.get("profileStore");
886 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
887 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
888 var recipientId
= getRecipientResolver().resolveRecipient(profileEntry
.address());
889 // Not migrating profile key credential here, it was changed to expiring profile key credentials
890 getProfileStore().storeProfileKey(recipientId
, profileEntry
.profileKey());
891 final var profile
= profileEntry
.profile();
892 if (profile
!= null) {
893 final var capabilities
= new HashSet
<Profile
.Capability
>();
894 if (profile
.getCapabilities() != null) {
895 if (profile
.getCapabilities().gv1Migration
) {
896 capabilities
.add(Profile
.Capability
.gv1Migration
);
898 if (profile
.getCapabilities().storage
) {
899 capabilities
.add(Profile
.Capability
.storage
);
902 final var newProfile
= new Profile(profileEntry
.lastUpdateTimestamp(),
903 profile
.getGivenName(),
904 profile
.getFamilyName(),
906 profile
.getAboutEmoji(),
909 profile
.isUnrestrictedUnidentifiedAccess()
910 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
911 : profile
.getUnidentifiedAccess() != null
912 ? Profile
.UnidentifiedAccessMode
.ENABLED
913 : Profile
.UnidentifiedAccessMode
.DISABLED
,
915 getProfileStore().storeProfile(recipientId
, newProfile
);
921 private void loadLegacyThreadStore(final JsonNode rootNode
) {
922 var threadStoreNode
= rootNode
.get("threadStore");
923 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
924 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
925 // Migrate thread info to group and contact store
926 for (var thread
: threadStore
.getThreads()) {
927 if (thread
.id
== null || thread
.id
.isEmpty()) {
931 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
932 final var recipientId
= getRecipientResolver().resolveRecipient(thread
.id
);
933 var contact
= getContactStore().getContact(recipientId
);
934 if (contact
!= null) {
935 getContactStore().storeContact(recipientId
,
936 Contact
.newBuilder(contact
)
937 .withMessageExpirationTime(thread
.messageExpirationTime
)
941 var groupInfo
= getGroupStore().getGroup(GroupId
.fromBase64(thread
.id
));
942 if (groupInfo
instanceof GroupInfoV1
) {
943 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
944 getGroupStore().updateGroup(groupInfo
);
947 } catch (Exception e
) {
948 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
954 private void save() {
955 synchronized (fileChannel
) {
956 final var base64
= Base64
.getEncoder();
957 final var storage
= new Storage(CURRENT_STORAGE_VERSION
,
958 serviceEnvironment
.name(),
966 Storage
.AccountData
.from(aciAccountData
),
967 Storage
.AccountData
.from(pniAccountData
),
969 pinMasterKey
== null ?
null : base64
.encodeToString(pinMasterKey
.serialize()),
970 storageKey
== null ?
null : base64
.encodeToString(storageKey
.serialize()),
971 profileKey
== null ?
null : base64
.encodeToString(profileKey
.serialize()),
972 usernameLink
== null ?
null : base64
.encodeToString(usernameLink
.getEntropy()),
973 usernameLink
== null ?
null : usernameLink
.getServerId().toString());
975 try (var output
= new ByteArrayOutputStream()) {
976 // Write to memory first to prevent corrupting the file in case of serialization errors
977 jsonProcessor
.writeValue(output
, storage
);
978 var input
= new ByteArrayInputStream(output
.toByteArray());
979 fileChannel
.position(0);
980 input
.transferTo(Channels
.newOutputStream(fileChannel
));
981 fileChannel
.truncate(fileChannel
.position());
982 fileChannel
.force(false);
984 } catch (Exception e
) {
985 logger
.error("Error saving file: {}", e
.getMessage(), e
);
990 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
991 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
993 var lock
= fileChannel
.tryLock();
996 logger
.debug("Config file is in use by another instance.");
997 throw new IOException("Config file is in use by another instance.");
999 logger
.info("Config file is in use by another instance, waiting…");
1000 lock
= fileChannel
.lock();
1001 logger
.info("Config file lock acquired.");
1003 final var result
= new Pair
<>(fileChannel
, lock
);
1007 if (fileChannel
!= null) {
1008 fileChannel
.close();
1013 private void clearAllPreKeys() {
1014 clearAllPreKeys(ServiceIdType
.ACI
);
1015 clearAllPreKeys(ServiceIdType
.PNI
);
1018 private void initAllPreKeyIds() {
1019 resetPreKeyOffsets(ServiceIdType
.ACI
);
1020 resetPreKeyOffsets(ServiceIdType
.PNI
);
1021 resetKyberPreKeyOffsets(ServiceIdType
.ACI
);
1022 resetKyberPreKeyOffsets(ServiceIdType
.PNI
);
1025 private void clearAllPreKeys(ServiceIdType serviceIdType
) {
1026 final var accountData
= getAccountData(serviceIdType
);
1027 resetPreKeyOffsets(serviceIdType
);
1028 resetKyberPreKeyOffsets(serviceIdType
);
1029 accountData
.getPreKeyStore().removeAllPreKeys();
1030 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1031 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1035 private void setPreKeys(ServiceIdType serviceIdType
, PreKeyCollection preKeyCollection
) {
1036 final var accountData
= getAccountData(serviceIdType
);
1037 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1038 preKeyMetadata
.nextSignedPreKeyId
= preKeyCollection
.getSignedPreKey().getId();
1039 preKeyMetadata
.nextKyberPreKeyId
= preKeyCollection
.getLastResortKyberPreKey().getId();
1041 accountData
.getPreKeyStore().removeAllPreKeys();
1042 accountData
.getSignedPreKeyStore().removeAllSignedPreKeys();
1043 accountData
.getKyberPreKeyStore().removeAllKyberPreKeys();
1045 addSignedPreKey(serviceIdType
, preKeyCollection
.getSignedPreKey());
1046 addLastResortKyberPreKey(serviceIdType
, preKeyCollection
.getLastResortKyberPreKey());
1051 public void resetPreKeyOffsets(final ServiceIdType serviceIdType
) {
1052 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1053 preKeyMetadata
.nextPreKeyId
= getRandomPreKeyIdOffset();
1054 preKeyMetadata
.nextSignedPreKeyId
= getRandomPreKeyIdOffset();
1055 preKeyMetadata
.activeSignedPreKeyId
= -1;
1059 private static int getRandomPreKeyIdOffset() {
1060 return KeyUtils
.getRandomInt(PREKEY_MAXIMUM_ID
);
1063 public void addPreKeys(ServiceIdType serviceIdType
, List
<PreKeyRecord
> records
) {
1064 final var accountData
= getAccountData(serviceIdType
);
1065 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1066 logger
.debug("Adding {} {} pre keys with offset {}",
1069 preKeyMetadata
.nextPreKeyId
);
1070 accountData
.getSignalServiceAccountDataStore()
1071 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1072 for (var record : records
) {
1073 if (preKeyMetadata
.nextPreKeyId
!= record.getId()) {
1074 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata
.nextPreKeyId
);
1075 throw new AssertionError("Invalid pre key id");
1077 accountData
.getPreKeyStore().storePreKey(record.getId(), record);
1078 preKeyMetadata
.nextPreKeyId
= (preKeyMetadata
.nextPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1083 public void addSignedPreKey(ServiceIdType serviceIdType
, SignedPreKeyRecord
record) {
1084 final var accountData
= getAccountData(serviceIdType
);
1085 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1086 logger
.debug("Adding {} signed pre key with offset {}", serviceIdType
, preKeyMetadata
.nextSignedPreKeyId
);
1087 if (preKeyMetadata
.nextSignedPreKeyId
!= record.getId()) {
1088 logger
.error("Invalid signed pre key id {}, expected {}",
1090 preKeyMetadata
.nextSignedPreKeyId
);
1091 throw new AssertionError("Invalid signed pre key id");
1093 accountData
.getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
1094 preKeyMetadata
.nextSignedPreKeyId
= (preKeyMetadata
.nextSignedPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1095 preKeyMetadata
.activeSignedPreKeyId
= record.getId();
1099 public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType
) {
1100 final var preKeyMetadata
= getAccountData(serviceIdType
).getPreKeyMetadata();
1101 preKeyMetadata
.nextKyberPreKeyId
= getRandomPreKeyIdOffset();
1102 preKeyMetadata
.activeLastResortKyberPreKeyId
= -1;
1106 public void addKyberPreKeys(ServiceIdType serviceIdType
, List
<KyberPreKeyRecord
> records
) {
1107 final var accountData
= getAccountData(serviceIdType
);
1108 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1109 logger
.debug("Adding {} {} kyber pre keys with offset {}",
1112 preKeyMetadata
.nextKyberPreKeyId
);
1113 accountData
.getSignalServiceAccountDataStore()
1114 .markAllOneTimeEcPreKeysStaleIfNecessary(System
.currentTimeMillis());
1115 for (var record : records
) {
1116 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1117 logger
.error("Invalid kyber pre key id {}, expected {}",
1119 preKeyMetadata
.nextKyberPreKeyId
);
1120 throw new AssertionError("Invalid kyber pre key id");
1122 accountData
.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
1123 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1128 public void addLastResortKyberPreKey(ServiceIdType serviceIdType
, KyberPreKeyRecord
record) {
1129 final var accountData
= getAccountData(serviceIdType
);
1130 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1131 logger
.debug("Adding {} last resort kyber pre key with offset {}",
1133 preKeyMetadata
.nextKyberPreKeyId
);
1134 if (preKeyMetadata
.nextKyberPreKeyId
!= record.getId()) {
1135 logger
.error("Invalid last resort kyber pre key id {}, expected {}",
1137 preKeyMetadata
.nextKyberPreKeyId
);
1138 throw new AssertionError("Invalid last resort kyber pre key id");
1140 accountData
.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
1141 preKeyMetadata
.activeLastResortKyberPreKeyId
= record.getId();
1142 preKeyMetadata
.nextKyberPreKeyId
= (preKeyMetadata
.nextKyberPreKeyId
+ 1) % PREKEY_MAXIMUM_ID
;
1146 public int getPreviousStorageVersion() {
1147 return previousStorageVersion
;
1150 public AccountData
<?
extends ServiceId
> getAccountData(ServiceIdType serviceIdType
) {
1151 return switch (serviceIdType
) {
1152 case ACI
-> aciAccountData
;
1153 case PNI
-> pniAccountData
;
1157 public AccountData
<?
extends ServiceId
> getAccountData(ServiceId accountIdentifier
) {
1158 if (accountIdentifier
.equals(aciAccountData
.getServiceId())) {
1159 return aciAccountData
;
1160 } else if (accountIdentifier
.equals(pniAccountData
.getServiceId())) {
1161 return pniAccountData
;
1163 throw new IllegalArgumentException("No matching account data found for " + accountIdentifier
);
1167 public SignalServiceDataStore
getSignalServiceDataStore() {
1168 return new SignalServiceDataStore() {
1170 public SignalServiceAccountDataStore
get(final ServiceId accountIdentifier
) {
1171 return getAccountData(accountIdentifier
).getSignalServiceAccountDataStore();
1175 public SignalServiceAccountDataStore
aci() {
1176 return aciAccountData
.getSignalServiceAccountDataStore();
1180 public SignalServiceAccountDataStore
pni() {
1181 return pniAccountData
.getSignalServiceAccountDataStore();
1185 public boolean isMultiDevice() {
1186 return SignalAccount
.this.isMultiDevice();
1191 public IdentityKeyStore
getIdentityKeyStore() {
1192 return getOrCreate(() -> identityKeyStore
,
1193 () -> identityKeyStore
= new IdentityKeyStore(getAccountDatabase(),
1194 settings
.trustNewIdentity(),
1195 getRecipientStore()));
1198 public GroupStore
getGroupStore() {
1199 return getOrCreate(() -> groupStore
,
1200 () -> groupStore
= new GroupStore(getAccountDatabase(),
1201 getRecipientResolver(),
1202 getRecipientIdCreator()));
1205 public ContactsStore
getContactStore() {
1206 return getRecipientStore();
1209 public CdsiStore
getCdsiStore() {
1210 return getOrCreate(() -> cdsiStore
, () -> cdsiStore
= new CdsiStore(getAccountDatabase()));
1213 private RecipientIdCreator
getRecipientIdCreator() {
1214 return recipientId
-> getRecipientStore().create(recipientId
);
1217 public RecipientResolver
getRecipientResolver() {
1218 return new RecipientResolver
.RecipientResolverWrapper(this::getRecipientStore
);
1221 public RecipientTrustedResolver
getRecipientTrustedResolver() {
1222 return new RecipientTrustedResolver
.RecipientTrustedResolverWrapper(this::getRecipientStore
);
1225 public RecipientAddressResolver
getRecipientAddressResolver() {
1226 return recipientId
-> getRecipientStore().resolveRecipientAddress(recipientId
);
1229 public RecipientStore
getRecipientStore() {
1230 return getOrCreate(() -> recipientStore
,
1231 () -> recipientStore
= new RecipientStore(this::mergeRecipients
,
1232 this::getSelfRecipientAddress
,
1233 this::getProfileKey
,
1234 getAccountDatabase()));
1237 public ProfileStore
getProfileStore() {
1238 return getRecipientStore();
1241 public StickerStore
getStickerStore() {
1242 return getOrCreate(() -> stickerStore
, () -> stickerStore
= new StickerStore(getAccountDatabase()));
1245 public SenderKeyStore
getSenderKeyStore() {
1246 return getOrCreate(() -> senderKeyStore
, () -> senderKeyStore
= new SenderKeyStore(getAccountDatabase()));
1249 private KeyValueStore
getKeyValueStore() {
1250 return getOrCreate(() -> keyValueStore
, () -> keyValueStore
= new KeyValueStore(getAccountDatabase()));
1253 public UnknownStorageIdStore
getUnknownStorageIdStore() {
1254 return getOrCreate(() -> unknownStorageIdStore
, () -> unknownStorageIdStore
= new UnknownStorageIdStore());
1257 public ConfigurationStore
getConfigurationStore() {
1258 return getOrCreate(() -> configurationStore
,
1259 () -> configurationStore
= new ConfigurationStore(getKeyValueStore(), getRecipientStore()));
1262 public MessageCache
getMessageCache() {
1263 return getOrCreate(() -> messageCache
,
1264 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
1267 public AccountDatabase
getAccountDatabase() {
1268 return getOrCreate(() -> accountDatabase
, () -> {
1270 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
1271 } catch (SQLException e
) {
1272 throw new RuntimeException(e
);
1277 public MessageSendLogStore
getMessageSendLogStore() {
1278 return getOrCreate(() -> messageSendLogStore
,
1279 () -> messageSendLogStore
= new MessageSendLogStore(getAccountDatabase(),
1280 settings
.disableMessageSendLog()));
1283 public CredentialsProvider
getCredentialsProvider() {
1284 return new CredentialsProvider() {
1286 public ACI
getAci() {
1287 return aciAccountData
.getServiceId();
1291 public PNI
getPni() {
1292 return pniAccountData
.getServiceId();
1296 public String
getE164() {
1301 public String
getPassword() {
1306 public int getDeviceId() {
1312 public String
getNumber() {
1316 public void setNumber(final String number
) {
1317 this.number
= number
;
1321 public String
getUsername() {
1325 public void setUsername(final String username
) {
1326 this.username
= username
;
1330 public UsernameLinkComponents
getUsernameLink() {
1331 return usernameLink
;
1334 public void setUsernameLink(final UsernameLinkComponents usernameLink
) {
1335 this.usernameLink
= usernameLink
;
1338 public ServiceEnvironment
getServiceEnvironment() {
1339 return serviceEnvironment
;
1342 public void setServiceEnvironment(final ServiceEnvironment serviceEnvironment
) {
1343 this.serviceEnvironment
= serviceEnvironment
;
1347 public AccountAttributes
getAccountAttributes(String registrationLock
) {
1348 return new AccountAttributes(null,
1349 aciAccountData
.getLocalRegistrationId(),
1353 registrationLock
!= null ? registrationLock
: getRegistrationLock(),
1354 getSelfUnidentifiedAccessKey(),
1355 isUnrestrictedUnidentifiedAccess(),
1356 isDiscoverableByPhoneNumber(),
1357 getAccountCapabilities(),
1358 encryptedDeviceName
,
1359 pniAccountData
.getLocalRegistrationId(),
1360 getRecoveryPassword());
1363 public AccountAttributes
.Capabilities
getAccountCapabilities() {
1364 return getCapabilities(isPrimaryDevice());
1367 public ServiceId
getAccountId(ServiceIdType serviceIdType
) {
1368 return getAccountData(serviceIdType
).getServiceId();
1371 public ACI
getAci() {
1372 return aciAccountData
.getServiceId();
1375 public void setAci(final ACI aci
) {
1376 this.aciAccountData
.setServiceId(aci
);
1380 public PNI
getPni() {
1381 return pniAccountData
.getServiceId();
1384 public void setPni(final PNI updatedPni
) {
1385 final var oldPni
= pniAccountData
.getServiceId();
1386 if (oldPni
!= null && !oldPni
.equals(updatedPni
)) {
1387 // Clear data for old PNI
1388 identityKeyStore
.deleteIdentity(oldPni
);
1391 this.pniAccountData
.setServiceId(updatedPni
);
1392 getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1393 trustSelfIdentity(ServiceIdType
.PNI
);
1397 public void setNewPniIdentity(
1398 final IdentityKeyPair pniIdentityKeyPair
,
1399 final SignedPreKeyRecord pniSignedPreKey
,
1400 final KyberPreKeyRecord lastResortKyberPreKey
,
1401 final int localPniRegistrationId
1403 setPniIdentityKeyPair(pniIdentityKeyPair
);
1404 pniAccountData
.setLocalRegistrationId(localPniRegistrationId
);
1406 final AccountData
<?
extends ServiceId
> accountData
= getAccountData(ServiceIdType
.PNI
);
1407 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1408 preKeyMetadata
.nextSignedPreKeyId
= pniSignedPreKey
.getId();
1409 accountData
.getSignedPreKeyStore().removeSignedPreKey(pniSignedPreKey
.getId());
1410 addSignedPreKey(ServiceIdType
.PNI
, pniSignedPreKey
);
1411 if (lastResortKyberPreKey
!= null) {
1412 preKeyMetadata
.nextKyberPreKeyId
= lastResortKyberPreKey
.getId();
1413 accountData
.getKyberPreKeyStore().removeKyberPreKey(lastResortKyberPreKey
.getId());
1414 addLastResortKyberPreKey(ServiceIdType
.PNI
, lastResortKyberPreKey
);
1419 public SignalServiceAddress
getSelfAddress() {
1420 return new SignalServiceAddress(getAci(), number
);
1423 public RecipientAddress
getSelfRecipientAddress() {
1424 return new RecipientAddress(getAci(), getPni(), number
, username
);
1427 public RecipientId
getSelfRecipientId() {
1428 return selfRecipientId
;
1431 public String
getSessionId(final String forNumber
) {
1432 final var keyValueStore
= getKeyValueStore();
1433 final var sessionNumber
= keyValueStore
.getEntry(verificationSessionNumber
);
1434 if (!forNumber
.equals(sessionNumber
)) {
1437 return keyValueStore
.getEntry(verificationSessionId
);
1440 public void setSessionId(final String sessionNumber
, final String sessionId
) {
1441 final var keyValueStore
= getKeyValueStore();
1442 keyValueStore
.storeEntry(verificationSessionNumber
, sessionNumber
);
1443 keyValueStore
.storeEntry(verificationSessionId
, sessionId
);
1446 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
1447 this.encryptedDeviceName
= encryptedDeviceName
;
1451 public int getDeviceId() {
1455 public boolean isPrimaryDevice() {
1456 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1459 public IdentityKeyPair
getIdentityKeyPair(ServiceIdType serviceIdType
) {
1460 return getAccountData(serviceIdType
).getIdentityKeyPair();
1463 public IdentityKeyPair
getAciIdentityKeyPair() {
1464 return aciAccountData
.getIdentityKeyPair();
1467 public IdentityKeyPair
getPniIdentityKeyPair() {
1468 return pniAccountData
.getIdentityKeyPair();
1471 public void setPniIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1472 pniAccountData
.setIdentityKeyPair(identityKeyPair
);
1473 trustSelfIdentity(ServiceIdType
.PNI
);
1477 public String
getPassword() {
1481 public void setRegistrationLockPin(final String registrationLockPin
) {
1482 this.registrationLockPin
= registrationLockPin
;
1486 public String
getRegistrationLockPin() {
1487 return registrationLockPin
;
1490 public String
getRegistrationLock() {
1491 final var masterKey
= getPinBackedMasterKey();
1492 if (masterKey
== null) {
1495 return masterKey
.deriveRegistrationLock();
1498 public MasterKey
getPinBackedMasterKey() {
1499 if (registrationLockPin
== null) {
1502 return pinMasterKey
;
1505 public MasterKey
getOrCreatePinMasterKey() {
1506 if (pinMasterKey
== null) {
1507 pinMasterKey
= KeyUtils
.createMasterKey();
1510 return pinMasterKey
;
1513 public void setMasterKey(MasterKey masterKey
) {
1514 if (isPrimaryDevice()) {
1517 this.pinMasterKey
= masterKey
;
1521 public StorageKey
getOrCreateStorageKey() {
1522 if (pinMasterKey
!= null) {
1523 return pinMasterKey
.deriveStorageServiceKey();
1524 } else if (storageKey
!= null) {
1526 } else if (!isPrimaryDevice() || !isMultiDevice()) {
1527 // Only upload storage, if a pin master key already exists or linked devices exist
1531 return getOrCreatePinMasterKey().deriveStorageServiceKey();
1534 public void setStorageKey(final StorageKey storageKey
) {
1535 if (isPrimaryDevice() || storageKey
.equals(this.storageKey
)) {
1538 this.storageKey
= storageKey
;
1542 public String
getRecoveryPassword() {
1543 final var masterKey
= getPinBackedMasterKey();
1544 if (masterKey
== null) {
1547 return masterKey
.deriveRegistrationRecoveryPassword();
1550 public long getStorageManifestVersion() {
1551 return getKeyValueStore().getEntry(storageManifestVersion
);
1554 public void setStorageManifestVersion(final long value
) {
1555 getKeyValueStore().storeEntry(storageManifestVersion
, value
);
1558 public Optional
<SignalStorageManifest
> getStorageManifest() {
1559 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1560 if (!storageManifestFile
.exists()) {
1561 return Optional
.empty();
1563 try (var inputStream
= new FileInputStream(storageManifestFile
)) {
1564 return Optional
.of(SignalStorageManifest
.deserialize(inputStream
.readAllBytes()));
1565 } catch (IOException e
) {
1566 logger
.warn("Failed to read local storage manifest.", e
);
1567 return Optional
.empty();
1571 public void setStorageManifest(SignalStorageManifest manifest
) {
1572 final var storageManifestFile
= getStorageManifestFile(dataPath
, accountPath
);
1573 if (manifest
== null) {
1574 if (storageManifestFile
.exists()) {
1576 Files
.delete(storageManifestFile
.toPath());
1577 } catch (IOException e
) {
1578 logger
.error("Failed to delete local storage manifest.", e
);
1584 final var manifestBytes
= manifest
.serialize();
1585 try (var outputStream
= new FileOutputStream(storageManifestFile
)) {
1586 outputStream
.write(manifestBytes
);
1587 } catch (IOException e
) {
1588 logger
.error("Failed to store local storage manifest.", e
);
1592 public byte[] getCdsiToken() {
1593 return getKeyValueStore().getEntry(cdsiToken
);
1596 public void setCdsiToken(final byte[] value
) {
1597 getKeyValueStore().storeEntry(cdsiToken
, value
);
1600 public Long
getLastRecipientsRefresh() {
1601 return getKeyValueStore().getEntry(lastRecipientsRefresh
);
1604 public void setLastRecipientsRefresh(final Long value
) {
1605 getKeyValueStore().storeEntry(lastRecipientsRefresh
, value
);
1608 public ProfileKey
getProfileKey() {
1612 public void setProfileKey(final ProfileKey profileKey
) {
1613 if (profileKey
.equals(this.profileKey
)) {
1616 this.profileKey
= profileKey
;
1620 public byte[] getSelfUnidentifiedAccessKey() {
1621 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1624 public boolean isRegistered() {
1628 public void setRegistered(final boolean registered
) {
1629 this.registered
= registered
;
1633 public boolean isMultiDevice() {
1634 return isMultiDevice
;
1637 public void setMultiDevice(final boolean multiDevice
) {
1638 if (isMultiDevice
== multiDevice
) {
1641 isMultiDevice
= multiDevice
;
1645 public long getLastReceiveTimestamp() {
1646 return getKeyValueStore().getEntry(lastReceiveTimestamp
);
1649 public void setLastReceiveTimestamp(final long value
) {
1650 getKeyValueStore().storeEntry(lastReceiveTimestamp
, value
);
1653 public boolean isUnrestrictedUnidentifiedAccess() {
1654 return Boolean
.TRUE
.equals(getKeyValueStore().getEntry(unrestrictedUnidentifiedAccess
));
1657 public void setUnrestrictedUnidentifiedAccess(boolean value
) {
1658 getKeyValueStore().storeEntry(unrestrictedUnidentifiedAccess
, value
);
1661 public boolean isDiscoverableByPhoneNumber() {
1662 final var phoneNumberUnlisted
= getConfigurationStore().getPhoneNumberUnlisted();
1663 return phoneNumberUnlisted
== null || !phoneNumberUnlisted
;
1666 private void trustSelfIdentity(ServiceIdType serviceIdType
) {
1667 final var accountData
= getAccountData(serviceIdType
);
1668 final var serviceId
= accountData
.getServiceId();
1669 final var identityKeyPair
= accountData
.getIdentityKeyPair();
1670 if (serviceId
== null || identityKeyPair
== null) {
1673 final var publicKey
= identityKeyPair
.getPublicKey();
1674 getIdentityKeyStore().saveIdentity(serviceId
, publicKey
);
1675 getIdentityKeyStore().setIdentityTrustLevel(serviceId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1678 public void deleteAccountData() throws IOException
{
1680 try (final var files
= Files
.walk(getUserPath(dataPath
, accountPath
).toPath())
1681 .sorted(Comparator
.reverseOrder())) {
1682 for (final var file
= files
.iterator(); file
.hasNext(); ) {
1683 Files
.delete(file
.next());
1686 Files
.delete(getFileName(dataPath
, accountPath
).toPath());
1690 public void close() {
1691 synchronized (fileChannel
) {
1692 if (accountDatabase
!= null) {
1693 accountDatabase
.close();
1695 if (messageSendLogStore
!= null) {
1696 messageSendLogStore
.close();
1701 } catch (ClosedChannelException ignored
) {
1703 fileChannel
.close();
1704 } catch (IOException e
) {
1705 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1710 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1711 var value
= supplier
.get();
1712 if (value
!= null) {
1716 synchronized (LOCK
) {
1717 value
= supplier
.get();
1718 if (value
!= null) {
1722 return supplier
.get();
1726 private interface Callable
{
1731 public static class PreKeyMetadata
{
1733 private int nextPreKeyId
= 1;
1734 private int nextSignedPreKeyId
= 1;
1735 private int activeSignedPreKeyId
= -1;
1736 private int nextKyberPreKeyId
= 1;
1737 private int activeLastResortKyberPreKeyId
= -1;
1739 public int getNextPreKeyId() {
1740 return nextPreKeyId
;
1743 public int getNextSignedPreKeyId() {
1744 return nextSignedPreKeyId
;
1747 public int getActiveSignedPreKeyId() {
1748 return activeSignedPreKeyId
;
1751 public int getNextKyberPreKeyId() {
1752 return nextKyberPreKeyId
;
1755 public int getActiveLastResortKyberPreKeyId() {
1756 return activeLastResortKyberPreKeyId
;
1760 public class AccountData
<SERVICE_ID
extends ServiceId
> {
1762 private final ServiceIdType serviceIdType
;
1763 private SERVICE_ID serviceId
;
1764 private IdentityKeyPair identityKeyPair
;
1765 private int localRegistrationId
;
1766 private final PreKeyMetadata preKeyMetadata
= new PreKeyMetadata();
1768 private SignalProtocolStore signalProtocolStore
;
1769 private PreKeyStore preKeyStore
;
1770 private SignedPreKeyStore signedPreKeyStore
;
1771 private KyberPreKeyStore kyberPreKeyStore
;
1772 private SessionStore sessionStore
;
1773 private SignalIdentityKeyStore identityKeyStore
;
1775 private AccountData(final ServiceIdType serviceIdType
) {
1776 this.serviceIdType
= serviceIdType
;
1779 public SERVICE_ID
getServiceId() {
1783 private void setServiceId(final SERVICE_ID serviceId
) {
1784 this.serviceId
= serviceId
;
1787 public IdentityKeyPair
getIdentityKeyPair() {
1788 return identityKeyPair
;
1791 private void setIdentityKeyPair(final IdentityKeyPair identityKeyPair
) {
1792 this.identityKeyPair
= identityKeyPair
;
1795 public int getLocalRegistrationId() {
1796 return localRegistrationId
;
1799 private void setLocalRegistrationId(final int localRegistrationId
) {
1800 this.localRegistrationId
= localRegistrationId
;
1801 this.identityKeyStore
= null;
1804 public PreKeyMetadata
getPreKeyMetadata() {
1805 return preKeyMetadata
;
1808 private SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
1809 return getOrCreate(() -> signalProtocolStore
,
1810 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
1811 getSignedPreKeyStore(),
1812 getKyberPreKeyStore(),
1814 getIdentityKeyStore(),
1815 getSenderKeyStore(),
1816 SignalAccount
.this::isMultiDevice
));
1819 public PreKeyStore
getPreKeyStore() {
1820 return getOrCreate(() -> preKeyStore
,
1821 () -> preKeyStore
= new PreKeyStore(getAccountDatabase(), serviceIdType
));
1824 public SignedPreKeyStore
getSignedPreKeyStore() {
1825 return getOrCreate(() -> signedPreKeyStore
,
1826 () -> signedPreKeyStore
= new SignedPreKeyStore(getAccountDatabase(), serviceIdType
));
1829 public KyberPreKeyStore
getKyberPreKeyStore() {
1830 return getOrCreate(() -> kyberPreKeyStore
,
1831 () -> kyberPreKeyStore
= new KyberPreKeyStore(getAccountDatabase(), serviceIdType
));
1834 public SessionStore
getSessionStore() {
1835 return getOrCreate(() -> sessionStore
,
1836 () -> sessionStore
= new SessionStore(getAccountDatabase(), serviceIdType
));
1839 public SignalIdentityKeyStore
getIdentityKeyStore() {
1840 return getOrCreate(() -> identityKeyStore
,
1841 () -> identityKeyStore
= new SignalIdentityKeyStore(() -> identityKeyPair
,
1842 localRegistrationId
,
1843 SignalAccount
.this.getIdentityKeyStore()));
1847 public record Storage(
1849 String serviceEnvironment
,
1853 String encryptedDeviceName
,
1855 boolean isMultiDevice
,
1857 AccountData aciAccountData
,
1858 AccountData pniAccountData
,
1859 String registrationLockPin
,
1860 String pinMasterKey
,
1863 String usernameLinkEntropy
,
1864 String usernameLinkServerId
1867 public record AccountData(
1870 String identityPrivateKey
,
1871 String identityPublicKey
,
1874 int nextSignedPreKeyId
,
1875 int activeSignedPreKeyId
,
1876 int nextKyberPreKeyId
,
1877 int activeLastResortKyberPreKeyId
1880 private static AccountData
from(final SignalAccount
.AccountData
<?
> accountData
) {
1881 final var base64
= Base64
.getEncoder();
1882 final var preKeyMetadata
= accountData
.getPreKeyMetadata();
1883 return new AccountData(accountData
.getServiceId() == null
1885 : accountData
.getServiceId().toString(),
1886 accountData
.getLocalRegistrationId(),
1887 accountData
.getIdentityKeyPair() == null
1889 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPrivateKey().serialize()),
1890 accountData
.getIdentityKeyPair() == null
1892 : base64
.encodeToString(accountData
.getIdentityKeyPair().getPublicKey().serialize()),
1893 preKeyMetadata
.getNextPreKeyId(),
1894 preKeyMetadata
.getNextSignedPreKeyId(),
1895 preKeyMetadata
.getActiveSignedPreKeyId(),
1896 preKeyMetadata
.getNextKyberPreKeyId(),
1897 preKeyMetadata
.getActiveLastResortKyberPreKeyId());