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
.TrustLevel
;
7 import org
.asamk
.signal
.manager
.groups
.GroupId
;
8 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
9 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
10 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
11 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
12 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
13 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
14 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
15 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
16 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
17 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
18 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
19 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
20 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
21 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
22 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
23 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
24 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
25 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
26 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
27 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
28 import org
.asamk
.signal
.manager
.util
.IOUtils
;
29 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
30 import org
.signal
.zkgroup
.InvalidInputException
;
31 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
32 import org
.slf4j
.Logger
;
33 import org
.slf4j
.LoggerFactory
;
34 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
35 import org
.whispersystems
.libsignal
.SignalProtocolAddress
;
36 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
37 import org
.whispersystems
.libsignal
.state
.SessionRecord
;
38 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
39 import org
.whispersystems
.libsignal
.util
.Medium
;
40 import org
.whispersystems
.libsignal
.util
.Pair
;
41 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
42 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
43 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
44 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
45 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
47 import java
.io
.ByteArrayInputStream
;
48 import java
.io
.ByteArrayOutputStream
;
49 import java
.io
.Closeable
;
51 import java
.io
.IOException
;
52 import java
.io
.RandomAccessFile
;
53 import java
.nio
.channels
.Channels
;
54 import java
.nio
.channels
.ClosedChannelException
;
55 import java
.nio
.channels
.FileChannel
;
56 import java
.nio
.channels
.FileLock
;
57 import java
.util
.Base64
;
58 import java
.util
.Date
;
59 import java
.util
.HashSet
;
60 import java
.util
.List
;
61 import java
.util
.UUID
;
63 public class SignalAccount
implements Closeable
{
65 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
67 private static final int MINIMUM_STORAGE_VERSION
= 1;
68 private static final int CURRENT_STORAGE_VERSION
= 2;
70 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
72 private final FileChannel fileChannel
;
73 private final FileLock lock
;
75 private String username
;
77 private String encryptedDeviceName
;
78 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
79 private boolean isMultiDevice
= false;
80 private String password
;
81 private String registrationLockPin
;
82 private MasterKey pinMasterKey
;
83 private StorageKey storageKey
;
84 private ProfileKey profileKey
;
85 private int preKeyIdOffset
;
86 private int nextSignedPreKeyId
;
88 private boolean registered
= false;
90 private SignalProtocolStore signalProtocolStore
;
91 private PreKeyStore preKeyStore
;
92 private SignedPreKeyStore signedPreKeyStore
;
93 private SessionStore sessionStore
;
94 private IdentityKeyStore identityKeyStore
;
95 private GroupStore groupStore
;
96 private GroupStore
.Storage groupStoreStorage
;
97 private RecipientStore recipientStore
;
98 private StickerStore stickerStore
;
99 private StickerStore
.Storage stickerStoreStorage
;
101 private MessageCache messageCache
;
103 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
104 this.fileChannel
= fileChannel
;
108 public static SignalAccount
load(File dataPath
, String username
, boolean waitForLock
) throws IOException
{
109 final var fileName
= getFileName(dataPath
, username
);
110 final var pair
= openFileChannel(fileName
, waitForLock
);
112 var account
= new SignalAccount(pair
.first(), pair
.second());
113 account
.load(dataPath
);
114 account
.migrateLegacyConfigs();
116 if (!username
.equals(account
.getUsername())) {
117 throw new IOException("Username in account file doesn't match expected number: "
118 + account
.getUsername());
122 } catch (Throwable e
) {
123 pair
.second().close();
124 pair
.first().close();
129 public static SignalAccount
create(
130 File dataPath
, String username
, IdentityKeyPair identityKey
, int registrationId
, ProfileKey profileKey
131 ) throws IOException
{
132 IOUtils
.createPrivateDirectories(dataPath
);
133 var fileName
= getFileName(dataPath
, username
);
134 if (!fileName
.exists()) {
135 IOUtils
.createPrivateFile(fileName
);
138 final var pair
= openFileChannel(fileName
, true);
139 var account
= new SignalAccount(pair
.first(), pair
.second());
141 account
.username
= username
;
142 account
.profileKey
= profileKey
;
144 account
.initStores(dataPath
, identityKey
, registrationId
);
145 account
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, username
),
146 account
.recipientStore
::resolveRecipient
,
147 account
::saveGroupStore
);
148 account
.stickerStore
= new StickerStore(account
::saveStickerStore
);
150 account
.registered
= false;
152 account
.migrateLegacyConfigs();
158 private void initStores(
159 final File dataPath
, final IdentityKeyPair identityKey
, final int registrationId
160 ) throws IOException
{
161 recipientStore
= RecipientStore
.load(getRecipientsStoreFile(dataPath
, username
), this::mergeRecipients
);
163 preKeyStore
= new PreKeyStore(getPreKeysPath(dataPath
, username
));
164 signedPreKeyStore
= new SignedPreKeyStore(getSignedPreKeysPath(dataPath
, username
));
165 sessionStore
= new SessionStore(getSessionsPath(dataPath
, username
), recipientStore
::resolveRecipient
);
166 identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, username
),
167 recipientStore
::resolveRecipient
,
170 signalProtocolStore
= new SignalProtocolStore(preKeyStore
, signedPreKeyStore
, sessionStore
, identityKeyStore
);
172 messageCache
= new MessageCache(getMessageCachePath(dataPath
, username
));
175 public static SignalAccount
createOrUpdateLinkedAccount(
180 String encryptedDeviceName
,
182 IdentityKeyPair identityKey
,
184 ProfileKey profileKey
185 ) throws IOException
{
186 IOUtils
.createPrivateDirectories(dataPath
);
187 var fileName
= getFileName(dataPath
, username
);
188 if (!fileName
.exists()) {
189 return createLinkedAccount(dataPath
,
200 final var account
= load(dataPath
, username
, true);
201 account
.setProvisioningData(username
, uuid
, password
, encryptedDeviceName
, deviceId
, profileKey
);
202 account
.recipientStore
.resolveRecipientTrusted(account
.getSelfAddress());
203 account
.sessionStore
.archiveAllSessions();
204 account
.clearAllPreKeys();
208 private void clearAllPreKeys() {
209 this.preKeyIdOffset
= 0;
210 this.nextSignedPreKeyId
= 0;
211 this.preKeyStore
.removeAllPreKeys();
212 this.signedPreKeyStore
.removeAllSignedPreKeys();
216 private static SignalAccount
createLinkedAccount(
221 String encryptedDeviceName
,
223 IdentityKeyPair identityKey
,
225 ProfileKey profileKey
226 ) throws IOException
{
227 var fileName
= getFileName(dataPath
, username
);
228 IOUtils
.createPrivateFile(fileName
);
230 final var pair
= openFileChannel(fileName
, true);
231 var account
= new SignalAccount(pair
.first(), pair
.second());
233 account
.setProvisioningData(username
, uuid
, password
, encryptedDeviceName
, deviceId
, profileKey
);
235 account
.initStores(dataPath
, identityKey
, registrationId
);
236 account
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, username
),
237 account
.recipientStore
::resolveRecipient
,
238 account
::saveGroupStore
);
239 account
.stickerStore
= new StickerStore(account
::saveStickerStore
);
241 account
.recipientStore
.resolveRecipientTrusted(account
.getSelfAddress());
242 account
.migrateLegacyConfigs();
248 private void setProvisioningData(
249 final String username
,
251 final String password
,
252 final String encryptedDeviceName
,
254 final ProfileKey profileKey
256 this.username
= username
;
258 this.password
= password
;
259 this.profileKey
= profileKey
;
260 this.encryptedDeviceName
= encryptedDeviceName
;
261 this.deviceId
= deviceId
;
262 this.registered
= true;
263 this.isMultiDevice
= true;
266 private void migrateLegacyConfigs() {
267 if (getPassword() == null) {
268 setPassword(KeyUtils
.createPassword());
271 if (getProfileKey() == null && isRegistered()) {
272 // Old config file, creating new profile key
273 setProfileKey(KeyUtils
.createProfileKey());
275 // Ensure our profile key is stored in profile store
276 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
279 private void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
280 sessionStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
281 identityKeyStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
282 messageCache
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
283 groupStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
286 public static File
getFileName(File dataPath
, String username
) {
287 return new File(dataPath
, username
);
290 private static File
getUserPath(final File dataPath
, final String username
) {
291 final var path
= new File(dataPath
, username
+ ".d");
293 IOUtils
.createPrivateDirectories(path
);
294 } catch (IOException e
) {
295 throw new AssertionError("Failed to create user path", e
);
300 private static File
getMessageCachePath(File dataPath
, String username
) {
301 return new File(getUserPath(dataPath
, username
), "msg-cache");
304 private static File
getGroupCachePath(File dataPath
, String username
) {
305 return new File(getUserPath(dataPath
, username
), "group-cache");
308 private static File
getPreKeysPath(File dataPath
, String username
) {
309 return new File(getUserPath(dataPath
, username
), "pre-keys");
312 private static File
getSignedPreKeysPath(File dataPath
, String username
) {
313 return new File(getUserPath(dataPath
, username
), "signed-pre-keys");
316 private static File
getIdentitiesPath(File dataPath
, String username
) {
317 return new File(getUserPath(dataPath
, username
), "identities");
320 private static File
getSessionsPath(File dataPath
, String username
) {
321 return new File(getUserPath(dataPath
, username
), "sessions");
324 private static File
getRecipientsStoreFile(File dataPath
, String username
) {
325 return new File(getUserPath(dataPath
, username
), "recipients-store");
328 public static boolean userExists(File dataPath
, String username
) {
329 if (username
== null) {
332 var f
= getFileName(dataPath
, username
);
333 return !(!f
.exists() || f
.isDirectory());
336 private void load(File dataPath
) throws IOException
{
338 synchronized (fileChannel
) {
339 fileChannel
.position(0);
340 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
343 if (rootNode
.hasNonNull("version")) {
344 var accountVersion
= rootNode
.get("version").asInt(1);
345 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
346 throw new IOException("Config file was created by a more recent version!");
347 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
348 throw new IOException("Config file was created by a no longer supported older version!");
352 username
= Utils
.getNotNullNode(rootNode
, "username").asText();
353 password
= Utils
.getNotNullNode(rootNode
, "password").asText();
354 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
355 if (rootNode
.hasNonNull("uuid")) {
357 uuid
= UUID
.fromString(rootNode
.get("uuid").asText());
358 } catch (IllegalArgumentException e
) {
359 throw new IOException("Config file contains an invalid uuid, needs to be a valid UUID", e
);
362 if (rootNode
.hasNonNull("deviceName")) {
363 encryptedDeviceName
= rootNode
.get("deviceName").asText();
365 if (rootNode
.hasNonNull("deviceId")) {
366 deviceId
= rootNode
.get("deviceId").asInt();
368 if (rootNode
.hasNonNull("isMultiDevice")) {
369 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
371 int registrationId
= 0;
372 if (rootNode
.hasNonNull("registrationId")) {
373 registrationId
= rootNode
.get("registrationId").asInt();
375 IdentityKeyPair identityKeyPair
= null;
376 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
377 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
378 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
379 identityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
382 if (rootNode
.hasNonNull("registrationLockPin")) {
383 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
385 if (rootNode
.hasNonNull("pinMasterKey")) {
386 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
388 if (rootNode
.hasNonNull("storageKey")) {
389 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
391 if (rootNode
.hasNonNull("preKeyIdOffset")) {
392 preKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(0);
396 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
397 nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt();
399 nextSignedPreKeyId
= 0;
401 if (rootNode
.hasNonNull("profileKey")) {
403 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
404 } catch (InvalidInputException e
) {
405 throw new IOException(
406 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
411 var migratedLegacyConfig
= false;
412 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
413 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
414 LegacyJsonSignalProtocolStore
.class)
416 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
417 identityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
418 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
419 migratedLegacyConfig
= true;
422 initStores(dataPath
, identityKeyPair
, registrationId
);
424 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
426 if (rootNode
.hasNonNull("groupStore")) {
427 groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"), GroupStore
.Storage
.class);
428 groupStore
= GroupStore
.fromStorage(groupStoreStorage
,
429 getGroupCachePath(dataPath
, username
),
430 recipientStore
::resolveRecipient
,
431 this::saveGroupStore
);
433 groupStore
= new GroupStore(getGroupCachePath(dataPath
, username
),
434 recipientStore
::resolveRecipient
,
435 this::saveGroupStore
);
438 if (rootNode
.hasNonNull("stickerStore")) {
439 stickerStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"), StickerStore
.Storage
.class);
440 stickerStore
= StickerStore
.fromStorage(stickerStoreStorage
, this::saveStickerStore
);
442 stickerStore
= new StickerStore(this::saveStickerStore
);
445 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
447 if (migratedLegacyConfig
) {
452 private boolean loadLegacyStores(
453 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
455 var migrated
= false;
456 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
457 if (legacyRecipientStoreNode
!= null) {
458 logger
.debug("Migrating legacy recipient store.");
459 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
460 if (legacyRecipientStore
!= null) {
461 recipientStore
.resolveRecipientsTrusted(legacyRecipientStore
.getAddresses());
463 recipientStore
.resolveRecipientTrusted(getSelfAddress());
467 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
468 logger
.debug("Migrating legacy pre key store.");
469 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
471 preKeyStore
.storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
472 } catch (IOException e
) {
473 logger
.warn("Failed to migrate pre key, ignoring", e
);
479 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
480 logger
.debug("Migrating legacy signed pre key store.");
481 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
483 signedPreKeyStore
.storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
484 } catch (IOException e
) {
485 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
491 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
492 logger
.debug("Migrating legacy session store.");
493 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
495 sessionStore
.storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
496 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
497 } catch (IOException e
) {
498 logger
.warn("Failed to migrate session, ignoring", e
);
504 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
505 logger
.debug("Migrating legacy identity session store.");
506 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
507 RecipientId recipientId
= recipientStore
.resolveRecipientTrusted(identity
.getAddress());
508 identityKeyStore
.saveIdentity(recipientId
, identity
.getIdentityKey(), identity
.getDateAdded());
509 identityKeyStore
.setIdentityTrustLevel(recipientId
,
510 identity
.getIdentityKey(),
511 identity
.getTrustLevel());
516 if (rootNode
.hasNonNull("contactStore")) {
517 logger
.debug("Migrating legacy contact store.");
518 final var contactStoreNode
= rootNode
.get("contactStore");
519 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
520 for (var contact
: contactStore
.getContacts()) {
521 final var recipientId
= recipientStore
.resolveRecipientTrusted(contact
.getAddress());
522 recipientStore
.storeContact(recipientId
,
523 new Contact(contact
.name
,
525 contact
.messageExpirationTime
,
529 // Store profile keys only in profile store
530 var profileKeyString
= contact
.profileKey
;
531 if (profileKeyString
!= null) {
532 final ProfileKey profileKey
;
534 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
535 getProfileStore().storeProfileKey(recipientId
, profileKey
);
536 } catch (InvalidInputException e
) {
537 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
544 if (rootNode
.hasNonNull("profileStore")) {
545 logger
.debug("Migrating legacy profile store.");
546 var profileStoreNode
= rootNode
.get("profileStore");
547 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
548 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
549 var recipientId
= recipientStore
.resolveRecipient(profileEntry
.getServiceAddress());
550 recipientStore
.storeProfileKeyCredential(recipientId
, profileEntry
.getProfileKeyCredential());
551 recipientStore
.storeProfileKey(recipientId
, profileEntry
.getProfileKey());
552 final var profile
= profileEntry
.getProfile();
553 if (profile
!= null) {
554 final var capabilities
= new HashSet
<Profile
.Capability
>();
555 if (profile
.getCapabilities() != null) {
556 if (profile
.getCapabilities().gv1Migration
) {
557 capabilities
.add(Profile
.Capability
.gv1Migration
);
559 if (profile
.getCapabilities().gv2
) {
560 capabilities
.add(Profile
.Capability
.gv2
);
562 if (profile
.getCapabilities().storage
) {
563 capabilities
.add(Profile
.Capability
.storage
);
566 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
567 profile
.getGivenName(),
568 profile
.getFamilyName(),
570 profile
.getAboutEmoji(),
571 profile
.isUnrestrictedUnidentifiedAccess()
572 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
573 : profile
.getUnidentifiedAccess() != null
574 ? Profile
.UnidentifiedAccessMode
.ENABLED
575 : Profile
.UnidentifiedAccessMode
.DISABLED
,
577 recipientStore
.storeProfile(recipientId
, newProfile
);
585 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
586 var threadStoreNode
= rootNode
.get("threadStore");
587 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
588 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
589 // Migrate thread info to group and contact store
590 for (var thread
: threadStore
.getThreads()) {
591 if (thread
.id
== null || thread
.id
.isEmpty()) {
595 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
596 final var recipientId
= recipientStore
.resolveRecipient(thread
.id
);
597 var contact
= recipientStore
.getContact(recipientId
);
598 if (contact
!= null) {
599 recipientStore
.storeContact(recipientId
,
600 Contact
.newBuilder(contact
)
601 .withMessageExpirationTime(thread
.messageExpirationTime
)
605 var groupInfo
= groupStore
.getGroup(GroupId
.fromBase64(thread
.id
));
606 if (groupInfo
instanceof GroupInfoV1
) {
607 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
608 groupStore
.updateGroup(groupInfo
);
611 } catch (Exception e
) {
612 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
621 private void saveStickerStore(StickerStore
.Storage storage
) {
622 this.stickerStoreStorage
= storage
;
626 private void saveGroupStore(GroupStore
.Storage storage
) {
627 this.groupStoreStorage
= storage
;
631 private void save() {
632 synchronized (fileChannel
) {
633 var rootNode
= jsonProcessor
.createObjectNode();
634 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
635 .put("username", username
)
636 .put("uuid", uuid
== null ?
null : uuid
.toString())
637 .put("deviceName", encryptedDeviceName
)
638 .put("deviceId", deviceId
)
639 .put("isMultiDevice", isMultiDevice
)
640 .put("password", password
)
641 .put("registrationId", identityKeyStore
.getLocalRegistrationId())
642 .put("identityPrivateKey",
644 .encodeToString(identityKeyStore
.getIdentityKeyPair().getPrivateKey().serialize()))
647 .encodeToString(identityKeyStore
.getIdentityKeyPair().getPublicKey().serialize()))
648 .put("registrationLockPin", registrationLockPin
)
650 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
652 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
653 .put("preKeyIdOffset", preKeyIdOffset
)
654 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
656 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
657 .put("registered", registered
)
658 .putPOJO("groupStore", groupStoreStorage
)
659 .putPOJO("stickerStore", stickerStoreStorage
);
661 try (var output
= new ByteArrayOutputStream()) {
662 // Write to memory first to prevent corrupting the file in case of serialization errors
663 jsonProcessor
.writeValue(output
, rootNode
);
664 var input
= new ByteArrayInputStream(output
.toByteArray());
665 fileChannel
.position(0);
666 input
.transferTo(Channels
.newOutputStream(fileChannel
));
667 fileChannel
.truncate(fileChannel
.position());
668 fileChannel
.force(false);
670 } catch (Exception e
) {
671 logger
.error("Error saving file: {}", e
.getMessage());
676 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
677 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
678 var lock
= fileChannel
.tryLock();
681 logger
.debug("Config file is in use by another instance.");
682 throw new IOException("Config file is in use by another instance.");
684 logger
.info("Config file is in use by another instance, waiting…");
685 lock
= fileChannel
.lock();
686 logger
.info("Config file lock acquired.");
688 return new Pair
<>(fileChannel
, lock
);
691 public void addPreKeys(List
<PreKeyRecord
> records
) {
692 for (var record : records
) {
693 if (preKeyIdOffset
!= record.getId()) {
694 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyIdOffset
);
695 throw new AssertionError("Invalid pre key id");
697 preKeyStore
.storePreKey(record.getId(), record);
698 preKeyIdOffset
= (preKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
703 public void addSignedPreKey(SignedPreKeyRecord
record) {
704 if (nextSignedPreKeyId
!= record.getId()) {
705 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), nextSignedPreKeyId
);
706 throw new AssertionError("Invalid signed pre key id");
708 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
709 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
713 public SignalProtocolStore
getSignalProtocolStore() {
714 return signalProtocolStore
;
717 public SessionStore
getSessionStore() {
721 public IdentityKeyStore
getIdentityKeyStore() {
722 return identityKeyStore
;
725 public GroupStore
getGroupStore() {
729 public ContactsStore
getContactStore() {
730 return recipientStore
;
733 public RecipientStore
getRecipientStore() {
734 return recipientStore
;
737 public ProfileStore
getProfileStore() {
738 return recipientStore
;
741 public StickerStore
getStickerStore() {
745 public MessageCache
getMessageCache() {
749 public String
getUsername() {
753 public UUID
getUuid() {
757 public void setUuid(final UUID uuid
) {
762 public SignalServiceAddress
getSelfAddress() {
763 return new SignalServiceAddress(uuid
, username
);
766 public RecipientId
getSelfRecipientId() {
767 return recipientStore
.resolveRecipientTrusted(getSelfAddress());
770 public String
getEncryptedDeviceName() {
771 return encryptedDeviceName
;
774 public int getDeviceId() {
778 public boolean isMasterDevice() {
779 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
782 public IdentityKeyPair
getIdentityKeyPair() {
783 return signalProtocolStore
.getIdentityKeyPair();
786 public int getLocalRegistrationId() {
787 return signalProtocolStore
.getLocalRegistrationId();
790 public String
getPassword() {
794 private void setPassword(final String password
) {
795 this.password
= password
;
799 public String
getRegistrationLockPin() {
800 return registrationLockPin
;
803 public void setRegistrationLockPin(final String registrationLockPin
, final MasterKey pinMasterKey
) {
804 this.registrationLockPin
= registrationLockPin
;
805 this.pinMasterKey
= pinMasterKey
;
809 public MasterKey
getPinMasterKey() {
813 public StorageKey
getStorageKey() {
814 if (pinMasterKey
!= null) {
815 return pinMasterKey
.deriveStorageServiceKey();
820 public void setStorageKey(final StorageKey storageKey
) {
821 if (storageKey
.equals(this.storageKey
)) {
824 this.storageKey
= storageKey
;
828 public ProfileKey
getProfileKey() {
832 public void setProfileKey(final ProfileKey profileKey
) {
833 if (profileKey
.equals(this.profileKey
)) {
836 this.profileKey
= profileKey
;
840 public byte[] getSelfUnidentifiedAccessKey() {
841 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
844 public int getPreKeyIdOffset() {
845 return preKeyIdOffset
;
848 public int getNextSignedPreKeyId() {
849 return nextSignedPreKeyId
;
852 public boolean isRegistered() {
856 public void setRegistered(final boolean registered
) {
857 this.registered
= registered
;
861 public boolean isMultiDevice() {
862 return isMultiDevice
;
865 public void setMultiDevice(final boolean multiDevice
) {
866 if (isMultiDevice
== multiDevice
) {
869 isMultiDevice
= multiDevice
;
873 public boolean isUnrestrictedUnidentifiedAccess() {
874 // TODO make configurable
878 public boolean isDiscoverableByPhoneNumber() {
879 // TODO make configurable
883 public boolean isPhoneNumberShared() {
884 // TODO make configurable
888 public void finishRegistration(final UUID uuid
, final MasterKey masterKey
, final String pin
) {
889 this.pinMasterKey
= masterKey
;
890 this.encryptedDeviceName
= null;
891 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
892 this.isMultiDevice
= false;
893 this.registered
= true;
895 this.registrationLockPin
= pin
;
898 getSessionStore().archiveAllSessions();
899 final var recipientId
= getRecipientStore().resolveRecipientTrusted(getSelfAddress());
900 final var publicKey
= getIdentityKeyPair().getPublicKey();
901 getIdentityKeyStore().saveIdentity(recipientId
, publicKey
, new Date());
902 getIdentityKeyStore().setIdentityTrustLevel(recipientId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
906 public void close() throws IOException
{
907 synchronized (fileChannel
) {
910 } catch (ClosedChannelException ignored
) {