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
.api
.Pair
;
7 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
8 import org
.asamk
.signal
.manager
.groups
.GroupId
;
9 import org
.asamk
.signal
.manager
.storage
.configuration
.ConfigurationStore
;
10 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
11 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
12 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
13 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
14 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
15 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
16 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
17 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
18 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
19 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
20 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
21 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
22 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
23 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
24 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
25 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
26 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
27 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
28 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
29 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
30 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
31 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
32 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
33 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
34 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
35 import org
.asamk
.signal
.manager
.util
.IOUtils
;
36 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
37 import org
.signal
.zkgroup
.InvalidInputException
;
38 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
39 import org
.slf4j
.Logger
;
40 import org
.slf4j
.LoggerFactory
;
41 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
42 import org
.whispersystems
.libsignal
.SignalProtocolAddress
;
43 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
44 import org
.whispersystems
.libsignal
.state
.SessionRecord
;
45 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
46 import org
.whispersystems
.libsignal
.util
.Medium
;
47 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountDataStore
;
48 import org
.whispersystems
.signalservice
.api
.SignalServiceDataStore
;
49 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
50 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
51 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
52 import org
.whispersystems
.signalservice
.api
.push
.AccountIdentifier
;
53 import org
.whispersystems
.signalservice
.api
.push
.DistributionId
;
54 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
55 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
56 import org
.whispersystems
.signalservice
.api
.util
.CredentialsProvider
;
57 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
59 import java
.io
.ByteArrayInputStream
;
60 import java
.io
.ByteArrayOutputStream
;
61 import java
.io
.Closeable
;
63 import java
.io
.IOException
;
64 import java
.io
.RandomAccessFile
;
65 import java
.nio
.channels
.Channels
;
66 import java
.nio
.channels
.ClosedChannelException
;
67 import java
.nio
.channels
.FileChannel
;
68 import java
.nio
.channels
.FileLock
;
69 import java
.security
.SecureRandom
;
70 import java
.sql
.SQLException
;
71 import java
.util
.Base64
;
72 import java
.util
.Date
;
73 import java
.util
.HashSet
;
74 import java
.util
.List
;
75 import java
.util
.function
.Supplier
;
77 public class SignalAccount
implements Closeable
{
79 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
81 private static final int MINIMUM_STORAGE_VERSION
= 1;
82 private static final int CURRENT_STORAGE_VERSION
= 3;
84 private final Object LOCK
= new Object();
86 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
88 private final FileChannel fileChannel
;
89 private final FileLock lock
;
91 private int previousStorageVersion
;
93 private File dataPath
;
94 private String accountPath
;
95 private String number
;
97 private String encryptedDeviceName
;
98 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
99 private boolean isMultiDevice
= false;
100 private String password
;
101 private String registrationLockPin
;
102 private MasterKey pinMasterKey
;
103 private StorageKey storageKey
;
104 private long storageManifestVersion
= -1;
105 private ProfileKey profileKey
;
106 private int preKeyIdOffset
= 1;
107 private int nextSignedPreKeyId
= 1;
108 private IdentityKeyPair identityKeyPair
;
109 private int localRegistrationId
;
110 private TrustNewIdentity trustNewIdentity
;
111 private long lastReceiveTimestamp
= 0;
113 private boolean registered
= false;
115 private SignalProtocolStore signalProtocolStore
;
116 private PreKeyStore preKeyStore
;
117 private SignedPreKeyStore signedPreKeyStore
;
118 private SessionStore sessionStore
;
119 private IdentityKeyStore identityKeyStore
;
120 private SenderKeyStore senderKeyStore
;
121 private GroupStore groupStore
;
122 private GroupStore
.Storage groupStoreStorage
;
123 private RecipientStore recipientStore
;
124 private StickerStore stickerStore
;
125 private StickerStore
.Storage stickerStoreStorage
;
126 private ConfigurationStore configurationStore
;
127 private ConfigurationStore
.Storage configurationStoreStorage
;
129 private MessageCache messageCache
;
130 private MessageSendLogStore messageSendLogStore
;
132 private AccountDatabase accountDatabase
;
134 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
135 this.fileChannel
= fileChannel
;
139 public static SignalAccount
load(
140 File dataPath
, String accountPath
, boolean waitForLock
, final TrustNewIdentity trustNewIdentity
141 ) throws IOException
{
142 logger
.trace("Opening account file");
143 final var fileName
= getFileName(dataPath
, accountPath
);
144 final var pair
= openFileChannel(fileName
, waitForLock
);
146 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
147 logger
.trace("Loading account file");
148 signalAccount
.load(dataPath
, accountPath
, trustNewIdentity
);
149 logger
.trace("Migrating legacy parts of account file");
150 signalAccount
.migrateLegacyConfigs();
152 return signalAccount
;
153 } catch (Throwable e
) {
154 pair
.second().close();
155 pair
.first().close();
160 public static SignalAccount
create(
164 IdentityKeyPair identityKey
,
166 ProfileKey profileKey
,
167 final TrustNewIdentity trustNewIdentity
168 ) throws IOException
{
169 IOUtils
.createPrivateDirectories(dataPath
);
170 var fileName
= getFileName(dataPath
, accountPath
);
171 if (!fileName
.exists()) {
172 IOUtils
.createPrivateFile(fileName
);
175 final var pair
= openFileChannel(fileName
, true);
176 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
178 signalAccount
.accountPath
= accountPath
;
179 signalAccount
.number
= number
;
180 signalAccount
.profileKey
= profileKey
;
182 signalAccount
.dataPath
= dataPath
;
183 signalAccount
.identityKeyPair
= identityKey
;
184 signalAccount
.localRegistrationId
= registrationId
;
185 signalAccount
.trustNewIdentity
= trustNewIdentity
;
186 signalAccount
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, accountPath
),
187 signalAccount
.getRecipientStore(),
188 signalAccount
::saveGroupStore
);
189 signalAccount
.stickerStore
= new StickerStore(signalAccount
::saveStickerStore
);
190 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
192 signalAccount
.registered
= false;
194 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
195 signalAccount
.migrateLegacyConfigs();
196 signalAccount
.save();
198 return signalAccount
;
201 public static SignalAccount
createOrUpdateLinkedAccount(
207 String encryptedDeviceName
,
209 IdentityKeyPair identityKey
,
211 ProfileKey profileKey
,
212 final TrustNewIdentity trustNewIdentity
213 ) throws IOException
{
214 IOUtils
.createPrivateDirectories(dataPath
);
215 var fileName
= getFileName(dataPath
, accountPath
);
216 if (!fileName
.exists()) {
217 return createLinkedAccount(dataPath
,
230 final var signalAccount
= load(dataPath
, accountPath
, true, trustNewIdentity
);
231 signalAccount
.setProvisioningData(number
, aci
, password
, encryptedDeviceName
, deviceId
, profileKey
);
232 signalAccount
.getRecipientStore().resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
233 signalAccount
.getSessionStore().archiveAllSessions();
234 signalAccount
.getSenderKeyStore().deleteAll();
235 signalAccount
.clearAllPreKeys();
236 return signalAccount
;
239 public void initDatabase() {
240 getAccountDatabase();
243 private void clearAllPreKeys() {
244 this.preKeyIdOffset
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
245 this.nextSignedPreKeyId
= new SecureRandom().nextInt(Medium
.MAX_VALUE
);
246 this.getPreKeyStore().removeAllPreKeys();
247 this.getSignedPreKeyStore().removeAllSignedPreKeys();
251 private static SignalAccount
createLinkedAccount(
257 String encryptedDeviceName
,
259 IdentityKeyPair identityKey
,
261 ProfileKey profileKey
,
262 final TrustNewIdentity trustNewIdentity
263 ) throws IOException
{
264 var fileName
= getFileName(dataPath
, accountPath
);
265 IOUtils
.createPrivateFile(fileName
);
267 final var pair
= openFileChannel(fileName
, true);
268 var signalAccount
= new SignalAccount(pair
.first(), pair
.second());
270 signalAccount
.setProvisioningData(number
, aci
, password
, encryptedDeviceName
, deviceId
, profileKey
);
272 signalAccount
.dataPath
= dataPath
;
273 signalAccount
.accountPath
= accountPath
;
274 signalAccount
.identityKeyPair
= identityKey
;
275 signalAccount
.localRegistrationId
= registrationId
;
276 signalAccount
.trustNewIdentity
= trustNewIdentity
;
277 signalAccount
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, accountPath
),
278 signalAccount
.getRecipientStore(),
279 signalAccount
::saveGroupStore
);
280 signalAccount
.stickerStore
= new StickerStore(signalAccount
::saveStickerStore
);
281 signalAccount
.configurationStore
= new ConfigurationStore(signalAccount
::saveConfigurationStore
);
283 signalAccount
.getRecipientStore().resolveSelfRecipientTrusted(signalAccount
.getSelfRecipientAddress());
284 signalAccount
.previousStorageVersion
= CURRENT_STORAGE_VERSION
;
285 signalAccount
.migrateLegacyConfigs();
286 signalAccount
.save();
288 return signalAccount
;
291 private void setProvisioningData(
294 final String password
,
295 final String encryptedDeviceName
,
297 final ProfileKey profileKey
299 this.number
= number
;
301 this.password
= password
;
302 this.profileKey
= profileKey
;
303 this.encryptedDeviceName
= encryptedDeviceName
;
304 this.deviceId
= deviceId
;
305 this.registered
= true;
306 this.isMultiDevice
= true;
307 this.lastReceiveTimestamp
= 0;
308 this.pinMasterKey
= null;
309 this.storageManifestVersion
= -1;
310 this.storageKey
= null;
313 private void migrateLegacyConfigs() {
314 if (getPassword() == null) {
315 setPassword(KeyUtils
.createPassword());
318 if (getProfileKey() == null) {
319 // Old config file, creating new profile key
320 setProfileKey(KeyUtils
.createProfileKey());
322 // Ensure our profile key is stored in profile store
323 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
324 if (previousStorageVersion
< 3) {
325 for (final var group
: groupStore
.getGroups()) {
326 if (group
instanceof GroupInfoV2
&& group
.getDistributionId() == null) {
327 ((GroupInfoV2
) group
).setDistributionId(DistributionId
.create());
328 groupStore
.updateGroup(group
);
335 private void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
336 getSessionStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
337 getIdentityKeyStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
338 getMessageCache().mergeRecipients(recipientId
, toBeMergedRecipientId
);
339 getGroupStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
340 getSenderKeyStore().mergeRecipients(recipientId
, toBeMergedRecipientId
);
343 public void removeRecipient(final RecipientId recipientId
) {
344 getSessionStore().deleteAllSessions(recipientId
);
345 getIdentityKeyStore().deleteIdentity(recipientId
);
346 getMessageCache().deleteMessages(recipientId
);
347 getSenderKeyStore().deleteAll(recipientId
);
348 getRecipientStore().deleteRecipientData(recipientId
);
351 public static File
getFileName(File dataPath
, String account
) {
352 return new File(dataPath
, account
);
355 private static File
getUserPath(final File dataPath
, final String account
) {
356 final var path
= new File(dataPath
, account
+ ".d");
358 IOUtils
.createPrivateDirectories(path
);
359 } catch (IOException e
) {
360 throw new AssertionError("Failed to create user path", e
);
365 private static File
getMessageCachePath(File dataPath
, String account
) {
366 return new File(getUserPath(dataPath
, account
), "msg-cache");
369 private static File
getGroupCachePath(File dataPath
, String account
) {
370 return new File(getUserPath(dataPath
, account
), "group-cache");
373 private static File
getPreKeysPath(File dataPath
, String account
) {
374 return new File(getUserPath(dataPath
, account
), "pre-keys");
377 private static File
getSignedPreKeysPath(File dataPath
, String account
) {
378 return new File(getUserPath(dataPath
, account
), "signed-pre-keys");
381 private static File
getIdentitiesPath(File dataPath
, String account
) {
382 return new File(getUserPath(dataPath
, account
), "identities");
385 private static File
getSessionsPath(File dataPath
, String account
) {
386 return new File(getUserPath(dataPath
, account
), "sessions");
389 private static File
getSenderKeysPath(File dataPath
, String account
) {
390 return new File(getUserPath(dataPath
, account
), "sender-keys");
393 private static File
getSharedSenderKeysFile(File dataPath
, String account
) {
394 return new File(getUserPath(dataPath
, account
), "shared-sender-keys-store");
397 private static File
getRecipientsStoreFile(File dataPath
, String account
) {
398 return new File(getUserPath(dataPath
, account
), "recipients-store");
401 private static File
getDatabaseFile(File dataPath
, String account
) {
402 return new File(getUserPath(dataPath
, account
), "account.db");
405 public static boolean accountFileExists(File dataPath
, String account
) {
406 if (account
== null) {
409 var f
= getFileName(dataPath
, account
);
410 return !(!f
.exists() || f
.isDirectory());
414 File dataPath
, String accountPath
, final TrustNewIdentity trustNewIdentity
415 ) throws IOException
{
416 this.dataPath
= dataPath
;
417 this.accountPath
= accountPath
;
418 final JsonNode rootNode
;
419 synchronized (fileChannel
) {
420 fileChannel
.position(0);
421 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
424 if (rootNode
.hasNonNull("version")) {
425 var accountVersion
= rootNode
.get("version").asInt(1);
426 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
427 throw new IOException("Config file was created by a more recent version!");
428 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
429 throw new IOException("Config file was created by a no longer supported older version!");
431 previousStorageVersion
= accountVersion
;
434 number
= Utils
.getNotNullNode(rootNode
, "username").asText();
435 if (rootNode
.hasNonNull("password")) {
436 password
= rootNode
.get("password").asText();
438 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
439 if (rootNode
.hasNonNull("uuid")) {
441 aci
= ACI
.parseOrThrow(rootNode
.get("uuid").asText());
442 } catch (IllegalArgumentException e
) {
443 throw new IOException("Config file contains an invalid uuid, needs to be a valid UUID", e
);
446 if (rootNode
.hasNonNull("deviceName")) {
447 encryptedDeviceName
= rootNode
.get("deviceName").asText();
449 if (rootNode
.hasNonNull("deviceId")) {
450 deviceId
= rootNode
.get("deviceId").asInt();
452 if (rootNode
.hasNonNull("isMultiDevice")) {
453 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
455 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
456 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
458 int registrationId
= 0;
459 if (rootNode
.hasNonNull("registrationId")) {
460 registrationId
= rootNode
.get("registrationId").asInt();
462 IdentityKeyPair identityKeyPair
= null;
463 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
464 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
465 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
466 identityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
469 if (rootNode
.hasNonNull("registrationLockPin")) {
470 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
472 if (rootNode
.hasNonNull("pinMasterKey")) {
473 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
475 if (rootNode
.hasNonNull("storageKey")) {
476 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
478 if (rootNode
.hasNonNull("storageManifestVersion")) {
479 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
481 if (rootNode
.hasNonNull("preKeyIdOffset")) {
482 preKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(1);
486 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
487 nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt(1);
489 nextSignedPreKeyId
= 1;
491 if (rootNode
.hasNonNull("profileKey")) {
493 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
494 } catch (InvalidInputException e
) {
495 throw new IOException(
496 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
501 var migratedLegacyConfig
= false;
502 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
503 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
504 LegacyJsonSignalProtocolStore
.class)
506 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
507 identityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
508 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
509 migratedLegacyConfig
= true;
512 this.identityKeyPair
= identityKeyPair
;
513 this.localRegistrationId
= registrationId
;
514 this.trustNewIdentity
= trustNewIdentity
;
516 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
518 if (rootNode
.hasNonNull("groupStore")) {
519 groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"), GroupStore
.Storage
.class);
520 groupStore
= GroupStore
.fromStorage(groupStoreStorage
,
521 getGroupCachePath(dataPath
, accountPath
),
523 this::saveGroupStore
);
525 groupStore
= new GroupStore(getGroupCachePath(dataPath
, accountPath
),
527 this::saveGroupStore
);
530 if (rootNode
.hasNonNull("stickerStore")) {
531 stickerStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"), StickerStore
.Storage
.class);
532 stickerStore
= StickerStore
.fromStorage(stickerStoreStorage
, this::saveStickerStore
);
534 stickerStore
= new StickerStore(this::saveStickerStore
);
537 if (rootNode
.hasNonNull("configurationStore")) {
538 configurationStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("configurationStore"),
539 ConfigurationStore
.Storage
.class);
540 configurationStore
= ConfigurationStore
.fromStorage(configurationStoreStorage
,
541 this::saveConfigurationStore
);
543 configurationStore
= new ConfigurationStore(this::saveConfigurationStore
);
546 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
548 if (migratedLegacyConfig
) {
553 private boolean loadLegacyStores(
554 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
556 var migrated
= false;
557 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
558 if (legacyRecipientStoreNode
!= null) {
559 logger
.debug("Migrating legacy recipient store.");
560 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
561 if (legacyRecipientStore
!= null) {
562 getRecipientStore().resolveRecipientsTrusted(legacyRecipientStore
.getAddresses());
564 getRecipientStore().resolveSelfRecipientTrusted(getSelfRecipientAddress());
568 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
569 logger
.debug("Migrating legacy pre key store.");
570 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
572 getPreKeyStore().storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
573 } catch (IOException e
) {
574 logger
.warn("Failed to migrate pre key, ignoring", e
);
580 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
581 logger
.debug("Migrating legacy signed pre key store.");
582 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
584 getSignedPreKeyStore().storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
585 } catch (IOException e
) {
586 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
592 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
593 logger
.debug("Migrating legacy session store.");
594 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
596 getSessionStore().storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
597 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
598 } catch (Exception e
) {
599 logger
.warn("Failed to migrate session, ignoring", e
);
605 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
606 logger
.debug("Migrating legacy identity session store.");
607 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
608 RecipientId recipientId
= getRecipientStore().resolveRecipientTrusted(identity
.getAddress());
609 getIdentityKeyStore().saveIdentity(recipientId
, identity
.getIdentityKey(), identity
.getDateAdded());
610 getIdentityKeyStore().setIdentityTrustLevel(recipientId
,
611 identity
.getIdentityKey(),
612 identity
.getTrustLevel());
617 if (rootNode
.hasNonNull("contactStore")) {
618 logger
.debug("Migrating legacy contact store.");
619 final var contactStoreNode
= rootNode
.get("contactStore");
620 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
621 for (var contact
: contactStore
.getContacts()) {
622 final var recipientId
= getRecipientStore().resolveRecipientTrusted(contact
.getAddress());
623 getRecipientStore().storeContact(recipientId
,
624 new Contact(contact
.name
,
626 contact
.messageExpirationTime
,
630 // Store profile keys only in profile store
631 var profileKeyString
= contact
.profileKey
;
632 if (profileKeyString
!= null) {
633 final ProfileKey profileKey
;
635 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
636 getProfileStore().storeProfileKey(recipientId
, profileKey
);
637 } catch (InvalidInputException e
) {
638 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
645 if (rootNode
.hasNonNull("profileStore")) {
646 logger
.debug("Migrating legacy profile store.");
647 var profileStoreNode
= rootNode
.get("profileStore");
648 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
649 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
650 var recipientId
= getRecipientStore().resolveRecipient(profileEntry
.getAddress());
651 getRecipientStore().storeProfileKeyCredential(recipientId
, profileEntry
.getProfileKeyCredential());
652 getRecipientStore().storeProfileKey(recipientId
, profileEntry
.getProfileKey());
653 final var profile
= profileEntry
.getProfile();
654 if (profile
!= null) {
655 final var capabilities
= new HashSet
<Profile
.Capability
>();
656 if (profile
.getCapabilities() != null) {
657 if (profile
.getCapabilities().gv1Migration
) {
658 capabilities
.add(Profile
.Capability
.gv1Migration
);
660 if (profile
.getCapabilities().gv2
) {
661 capabilities
.add(Profile
.Capability
.gv2
);
663 if (profile
.getCapabilities().storage
) {
664 capabilities
.add(Profile
.Capability
.storage
);
667 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
668 profile
.getGivenName(),
669 profile
.getFamilyName(),
671 profile
.getAboutEmoji(),
673 profile
.isUnrestrictedUnidentifiedAccess()
674 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
675 : profile
.getUnidentifiedAccess() != null
676 ? Profile
.UnidentifiedAccessMode
.ENABLED
677 : Profile
.UnidentifiedAccessMode
.DISABLED
,
679 getRecipientStore().storeProfile(recipientId
, newProfile
);
687 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
688 var threadStoreNode
= rootNode
.get("threadStore");
689 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
690 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
691 // Migrate thread info to group and contact store
692 for (var thread
: threadStore
.getThreads()) {
693 if (thread
.id
== null || thread
.id
.isEmpty()) {
697 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
698 final var recipientId
= getRecipientStore().resolveRecipient(thread
.id
);
699 var contact
= getRecipientStore().getContact(recipientId
);
700 if (contact
!= null) {
701 getRecipientStore().storeContact(recipientId
,
702 Contact
.newBuilder(contact
)
703 .withMessageExpirationTime(thread
.messageExpirationTime
)
707 var groupInfo
= groupStore
.getGroup(GroupId
.fromBase64(thread
.id
));
708 if (groupInfo
instanceof GroupInfoV1
) {
709 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
710 groupStore
.updateGroup(groupInfo
);
713 } catch (Exception e
) {
714 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
723 private void saveStickerStore(StickerStore
.Storage storage
) {
724 this.stickerStoreStorage
= storage
;
728 private void saveGroupStore(GroupStore
.Storage storage
) {
729 this.groupStoreStorage
= storage
;
733 private void saveConfigurationStore(ConfigurationStore
.Storage storage
) {
734 this.configurationStoreStorage
= storage
;
738 private void save() {
739 synchronized (fileChannel
) {
740 var rootNode
= jsonProcessor
.createObjectNode();
741 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
742 .put("username", number
)
743 .put("uuid", aci
== null ?
null : aci
.toString())
744 .put("deviceName", encryptedDeviceName
)
745 .put("deviceId", deviceId
)
746 .put("isMultiDevice", isMultiDevice
)
747 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
748 .put("password", password
)
749 .put("registrationId", localRegistrationId
)
750 .put("identityPrivateKey",
751 Base64
.getEncoder().encodeToString(identityKeyPair
.getPrivateKey().serialize()))
752 .put("identityKey", Base64
.getEncoder().encodeToString(identityKeyPair
.getPublicKey().serialize()))
753 .put("registrationLockPin", registrationLockPin
)
755 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
757 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
758 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
759 .put("preKeyIdOffset", preKeyIdOffset
)
760 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
762 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
763 .put("registered", registered
)
764 .putPOJO("groupStore", groupStoreStorage
)
765 .putPOJO("stickerStore", stickerStoreStorage
)
766 .putPOJO("configurationStore", configurationStoreStorage
);
768 try (var output
= new ByteArrayOutputStream()) {
769 // Write to memory first to prevent corrupting the file in case of serialization errors
770 jsonProcessor
.writeValue(output
, rootNode
);
771 var input
= new ByteArrayInputStream(output
.toByteArray());
772 fileChannel
.position(0);
773 input
.transferTo(Channels
.newOutputStream(fileChannel
));
774 fileChannel
.truncate(fileChannel
.position());
775 fileChannel
.force(false);
777 } catch (Exception e
) {
778 logger
.error("Error saving file: {}", e
.getMessage(), e
);
783 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
784 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
785 var lock
= fileChannel
.tryLock();
788 logger
.debug("Config file is in use by another instance.");
789 throw new IOException("Config file is in use by another instance.");
791 logger
.info("Config file is in use by another instance, waiting…");
792 lock
= fileChannel
.lock();
793 logger
.info("Config file lock acquired.");
795 return new Pair
<>(fileChannel
, lock
);
798 public void addPreKeys(List
<PreKeyRecord
> records
) {
799 for (var record : records
) {
800 if (preKeyIdOffset
!= record.getId()) {
801 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyIdOffset
);
802 throw new AssertionError("Invalid pre key id");
804 getPreKeyStore().storePreKey(record.getId(), record);
805 preKeyIdOffset
= (preKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
810 public void addSignedPreKey(SignedPreKeyRecord
record) {
811 if (nextSignedPreKeyId
!= record.getId()) {
812 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), nextSignedPreKeyId
);
813 throw new AssertionError("Invalid signed pre key id");
815 getSignedPreKeyStore().storeSignedPreKey(record.getId(), record);
816 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
820 public SignalServiceDataStore
getSignalServiceDataStore() {
821 return new SignalServiceDataStore() {
823 public SignalServiceAccountDataStore
get(final AccountIdentifier accountIdentifier
) {
824 return getSignalServiceAccountDataStore();
828 public SignalServiceAccountDataStore
aci() {
829 return getSignalServiceAccountDataStore();
833 public SignalServiceAccountDataStore
pni() {
834 return getSignalServiceAccountDataStore();
838 public boolean isMultiDevice() {
839 return SignalAccount
.this.isMultiDevice();
844 public SignalServiceAccountDataStore
getSignalServiceAccountDataStore() {
845 return getOrCreate(() -> signalProtocolStore
,
846 () -> signalProtocolStore
= new SignalProtocolStore(getPreKeyStore(),
847 getSignedPreKeyStore(),
849 getIdentityKeyStore(),
851 this::isMultiDevice
));
854 private PreKeyStore
getPreKeyStore() {
855 return getOrCreate(() -> preKeyStore
,
856 () -> preKeyStore
= new PreKeyStore(getPreKeysPath(dataPath
, accountPath
)));
859 private SignedPreKeyStore
getSignedPreKeyStore() {
860 return getOrCreate(() -> signedPreKeyStore
,
861 () -> signedPreKeyStore
= new SignedPreKeyStore(getSignedPreKeysPath(dataPath
, accountPath
)));
864 public SessionStore
getSessionStore() {
865 return getOrCreate(() -> sessionStore
,
866 () -> sessionStore
= new SessionStore(getSessionsPath(dataPath
, accountPath
), getRecipientStore()));
869 public IdentityKeyStore
getIdentityKeyStore() {
870 return getOrCreate(() -> identityKeyStore
,
871 () -> identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, accountPath
),
878 public GroupStore
getGroupStore() {
882 public ContactsStore
getContactStore() {
883 return getRecipientStore();
886 public RecipientStore
getRecipientStore() {
887 return getOrCreate(() -> recipientStore
,
888 () -> recipientStore
= RecipientStore
.load(getRecipientsStoreFile(dataPath
, accountPath
),
889 this::mergeRecipients
,
890 this::getSelfRecipientAddress
));
893 public ProfileStore
getProfileStore() {
894 return getRecipientStore();
897 public StickerStore
getStickerStore() {
901 public SenderKeyStore
getSenderKeyStore() {
902 return getOrCreate(() -> senderKeyStore
,
903 () -> senderKeyStore
= new SenderKeyStore(getSharedSenderKeysFile(dataPath
, accountPath
),
904 getSenderKeysPath(dataPath
, accountPath
),
905 getRecipientStore()::resolveRecipientAddress
,
906 getRecipientStore()));
909 public ConfigurationStore
getConfigurationStore() {
910 return configurationStore
;
913 public MessageCache
getMessageCache() {
914 return getOrCreate(() -> messageCache
,
915 () -> messageCache
= new MessageCache(getMessageCachePath(dataPath
, accountPath
)));
918 public AccountDatabase
getAccountDatabase() {
919 return getOrCreate(() -> accountDatabase
, () -> {
921 accountDatabase
= AccountDatabase
.init(getDatabaseFile(dataPath
, accountPath
));
922 } catch (SQLException e
) {
923 throw new RuntimeException(e
);
928 public MessageSendLogStore
getMessageSendLogStore() {
929 return getOrCreate(() -> messageSendLogStore
,
930 () -> messageSendLogStore
= new MessageSendLogStore(getRecipientStore(), getAccountDatabase()));
933 public CredentialsProvider
getCredentialsProvider() {
934 return new CredentialsProvider() {
936 public ACI
getAci() {
941 public String
getE164() {
946 public String
getPassword() {
951 public int getDeviceId() {
957 public String
getNumber() {
961 public void setNumber(final String number
) {
962 this.number
= number
;
966 public ACI
getAci() {
970 public void setAci(final ACI aci
) {
975 public SignalServiceAddress
getSelfAddress() {
976 return new SignalServiceAddress(aci
, number
);
979 public RecipientAddress
getSelfRecipientAddress() {
980 return new RecipientAddress(aci
== null ?
null : aci
.uuid(), number
);
983 public RecipientId
getSelfRecipientId() {
984 return getRecipientStore().resolveRecipient(getSelfRecipientAddress());
987 public byte[] getEncryptedDeviceName() {
988 return encryptedDeviceName
== null ?
null : Base64
.getDecoder().decode(encryptedDeviceName
);
991 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
992 this.encryptedDeviceName
= encryptedDeviceName
;
996 public int getDeviceId() {
1000 public boolean isMasterDevice() {
1001 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1004 public IdentityKeyPair
getIdentityKeyPair() {
1005 return identityKeyPair
;
1008 public int getLocalRegistrationId() {
1009 return localRegistrationId
;
1012 public String
getPassword() {
1016 private void setPassword(final String password
) {
1017 this.password
= password
;
1021 public void setRegistrationLockPin(final String registrationLockPin
, final MasterKey pinMasterKey
) {
1022 this.registrationLockPin
= registrationLockPin
;
1023 this.pinMasterKey
= pinMasterKey
;
1027 public MasterKey
getPinMasterKey() {
1028 return pinMasterKey
;
1031 public StorageKey
getStorageKey() {
1032 if (pinMasterKey
!= null) {
1033 return pinMasterKey
.deriveStorageServiceKey();
1038 public void setStorageKey(final StorageKey storageKey
) {
1039 if (storageKey
.equals(this.storageKey
)) {
1042 this.storageKey
= storageKey
;
1046 public long getStorageManifestVersion() {
1047 return this.storageManifestVersion
;
1050 public void setStorageManifestVersion(final long storageManifestVersion
) {
1051 if (storageManifestVersion
== this.storageManifestVersion
) {
1054 this.storageManifestVersion
= storageManifestVersion
;
1058 public ProfileKey
getProfileKey() {
1062 public void setProfileKey(final ProfileKey profileKey
) {
1063 if (profileKey
.equals(this.profileKey
)) {
1066 this.profileKey
= profileKey
;
1070 public byte[] getSelfUnidentifiedAccessKey() {
1071 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
1074 public int getPreKeyIdOffset() {
1075 return preKeyIdOffset
;
1078 public int getNextSignedPreKeyId() {
1079 return nextSignedPreKeyId
;
1082 public boolean isRegistered() {
1086 public void setRegistered(final boolean registered
) {
1087 this.registered
= registered
;
1091 public boolean isMultiDevice() {
1092 return isMultiDevice
;
1095 public void setMultiDevice(final boolean multiDevice
) {
1096 if (isMultiDevice
== multiDevice
) {
1099 isMultiDevice
= multiDevice
;
1103 public long getLastReceiveTimestamp() {
1104 return lastReceiveTimestamp
;
1107 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
1108 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
1112 public boolean isUnrestrictedUnidentifiedAccess() {
1113 // TODO make configurable
1117 public boolean isDiscoverableByPhoneNumber() {
1118 return configurationStore
.getPhoneNumberUnlisted() == null || !configurationStore
.getPhoneNumberUnlisted();
1121 public void finishRegistration(final ACI aci
, final MasterKey masterKey
, final String pin
) {
1122 this.pinMasterKey
= masterKey
;
1123 this.storageManifestVersion
= -1;
1124 this.storageKey
= null;
1125 this.encryptedDeviceName
= null;
1126 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
1127 this.isMultiDevice
= false;
1128 this.registered
= true;
1130 this.registrationLockPin
= pin
;
1131 this.lastReceiveTimestamp
= 0;
1135 getSessionStore().archiveAllSessions();
1136 getSenderKeyStore().deleteAll();
1137 final var recipientId
= getRecipientStore().resolveSelfRecipientTrusted(getSelfRecipientAddress());
1138 final var publicKey
= getIdentityKeyPair().getPublicKey();
1139 getIdentityKeyStore().saveIdentity(recipientId
, publicKey
, new Date());
1140 getIdentityKeyStore().setIdentityTrustLevel(recipientId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
1144 public void close() {
1145 synchronized (fileChannel
) {
1146 if (accountDatabase
!= null) {
1148 accountDatabase
.close();
1149 } catch (SQLException e
) {
1150 logger
.warn("Failed to close account database: {}", e
.getMessage(), e
);
1153 if (messageSendLogStore
!= null) {
1154 messageSendLogStore
.close();
1159 } catch (ClosedChannelException ignored
) {
1161 fileChannel
.close();
1162 } catch (IOException e
) {
1163 logger
.warn("Failed to close account: {}", e
.getMessage(), e
);
1168 private <T
> T
getOrCreate(Supplier
<T
> supplier
, Callable creator
) {
1169 var value
= supplier
.get();
1170 if (value
!= null) {
1174 synchronized (LOCK
) {
1175 value
= supplier
.get();
1176 if (value
!= null) {
1180 return supplier
.get();
1184 private interface Callable
{