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
.identities
.TrustNewIdentity
;
14 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
15 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
16 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
17 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
18 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
19 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
20 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
21 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
22 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
23 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
24 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
25 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
26 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
27 import org
.asamk
.signal
.manager
.storage
.senderKeys
.SenderKeyStore
;
28 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
29 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
30 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
31 import org
.asamk
.signal
.manager
.util
.IOUtils
;
32 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
33 import org
.signal
.zkgroup
.InvalidInputException
;
34 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
35 import org
.slf4j
.Logger
;
36 import org
.slf4j
.LoggerFactory
;
37 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
38 import org
.whispersystems
.libsignal
.SignalProtocolAddress
;
39 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
40 import org
.whispersystems
.libsignal
.state
.SessionRecord
;
41 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
42 import org
.whispersystems
.libsignal
.util
.Medium
;
43 import org
.whispersystems
.libsignal
.util
.Pair
;
44 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
45 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
46 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
47 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
48 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
50 import java
.io
.ByteArrayInputStream
;
51 import java
.io
.ByteArrayOutputStream
;
52 import java
.io
.Closeable
;
54 import java
.io
.IOException
;
55 import java
.io
.RandomAccessFile
;
56 import java
.nio
.channels
.Channels
;
57 import java
.nio
.channels
.ClosedChannelException
;
58 import java
.nio
.channels
.FileChannel
;
59 import java
.nio
.channels
.FileLock
;
60 import java
.util
.Base64
;
61 import java
.util
.Date
;
62 import java
.util
.HashSet
;
63 import java
.util
.List
;
64 import java
.util
.UUID
;
66 public class SignalAccount
implements Closeable
{
68 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
70 private static final int MINIMUM_STORAGE_VERSION
= 1;
71 private static final int CURRENT_STORAGE_VERSION
= 2;
73 private final ObjectMapper jsonProcessor
= Utils
.createStorageObjectMapper();
75 private final FileChannel fileChannel
;
76 private final FileLock lock
;
78 private String username
;
80 private String encryptedDeviceName
;
81 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
82 private boolean isMultiDevice
= false;
83 private String password
;
84 private String registrationLockPin
;
85 private MasterKey pinMasterKey
;
86 private StorageKey storageKey
;
87 private long storageManifestVersion
= -1;
88 private ProfileKey profileKey
;
89 private int preKeyIdOffset
;
90 private int nextSignedPreKeyId
;
91 private long lastReceiveTimestamp
= 0;
93 private boolean registered
= false;
95 private SignalProtocolStore signalProtocolStore
;
96 private PreKeyStore preKeyStore
;
97 private SignedPreKeyStore signedPreKeyStore
;
98 private SessionStore sessionStore
;
99 private IdentityKeyStore identityKeyStore
;
100 private SenderKeyStore senderKeyStore
;
101 private GroupStore groupStore
;
102 private GroupStore
.Storage groupStoreStorage
;
103 private RecipientStore recipientStore
;
104 private StickerStore stickerStore
;
105 private StickerStore
.Storage stickerStoreStorage
;
107 private MessageCache messageCache
;
109 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
110 this.fileChannel
= fileChannel
;
114 public static SignalAccount
load(
115 File dataPath
, String username
, boolean waitForLock
, final TrustNewIdentity trustNewIdentity
116 ) throws IOException
{
117 final var fileName
= getFileName(dataPath
, username
);
118 final var pair
= openFileChannel(fileName
, waitForLock
);
120 var account
= new SignalAccount(pair
.first(), pair
.second());
121 account
.load(dataPath
, trustNewIdentity
);
122 account
.migrateLegacyConfigs();
124 if (!username
.equals(account
.getUsername())) {
125 throw new IOException("Username in account file doesn't match expected number: "
126 + account
.getUsername());
130 } catch (Throwable e
) {
131 pair
.second().close();
132 pair
.first().close();
137 public static SignalAccount
create(
140 IdentityKeyPair identityKey
,
142 ProfileKey profileKey
,
143 final TrustNewIdentity trustNewIdentity
144 ) throws IOException
{
145 IOUtils
.createPrivateDirectories(dataPath
);
146 var fileName
= getFileName(dataPath
, username
);
147 if (!fileName
.exists()) {
148 IOUtils
.createPrivateFile(fileName
);
151 final var pair
= openFileChannel(fileName
, true);
152 var account
= new SignalAccount(pair
.first(), pair
.second());
154 account
.username
= username
;
155 account
.profileKey
= profileKey
;
157 account
.initStores(dataPath
, identityKey
, registrationId
, trustNewIdentity
);
158 account
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, username
),
159 account
.recipientStore
,
160 account
::saveGroupStore
);
161 account
.stickerStore
= new StickerStore(account
::saveStickerStore
);
163 account
.registered
= false;
165 account
.migrateLegacyConfigs();
171 private void initStores(
173 final IdentityKeyPair identityKey
,
174 final int registrationId
,
175 final TrustNewIdentity trustNewIdentity
176 ) throws IOException
{
177 recipientStore
= RecipientStore
.load(getRecipientsStoreFile(dataPath
, username
), this::mergeRecipients
);
179 preKeyStore
= new PreKeyStore(getPreKeysPath(dataPath
, username
));
180 signedPreKeyStore
= new SignedPreKeyStore(getSignedPreKeysPath(dataPath
, username
));
181 sessionStore
= new SessionStore(getSessionsPath(dataPath
, username
), recipientStore
);
182 identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, username
),
187 senderKeyStore
= new SenderKeyStore(getSharedSenderKeysFile(dataPath
, username
),
188 getSenderKeysPath(dataPath
, username
),
189 recipientStore
::resolveRecipientAddress
,
191 signalProtocolStore
= new SignalProtocolStore(preKeyStore
,
196 this::isMultiDevice
);
198 messageCache
= new MessageCache(getMessageCachePath(dataPath
, username
));
201 public static SignalAccount
createOrUpdateLinkedAccount(
206 String encryptedDeviceName
,
208 IdentityKeyPair identityKey
,
210 ProfileKey profileKey
,
211 final TrustNewIdentity trustNewIdentity
212 ) throws IOException
{
213 IOUtils
.createPrivateDirectories(dataPath
);
214 var fileName
= getFileName(dataPath
, username
);
215 if (!fileName
.exists()) {
216 return createLinkedAccount(dataPath
,
228 final var account
= load(dataPath
, username
, true, trustNewIdentity
);
229 account
.setProvisioningData(username
, uuid
, password
, encryptedDeviceName
, deviceId
, profileKey
);
230 account
.recipientStore
.resolveRecipientTrusted(account
.getSelfAddress());
231 account
.sessionStore
.archiveAllSessions();
232 account
.senderKeyStore
.deleteAll();
233 account
.clearAllPreKeys();
237 private void clearAllPreKeys() {
238 this.preKeyIdOffset
= 0;
239 this.nextSignedPreKeyId
= 0;
240 this.preKeyStore
.removeAllPreKeys();
241 this.signedPreKeyStore
.removeAllSignedPreKeys();
245 private static SignalAccount
createLinkedAccount(
250 String encryptedDeviceName
,
252 IdentityKeyPair identityKey
,
254 ProfileKey profileKey
,
255 final TrustNewIdentity trustNewIdentity
256 ) throws IOException
{
257 var fileName
= getFileName(dataPath
, username
);
258 IOUtils
.createPrivateFile(fileName
);
260 final var pair
= openFileChannel(fileName
, true);
261 var account
= new SignalAccount(pair
.first(), pair
.second());
263 account
.setProvisioningData(username
, uuid
, password
, encryptedDeviceName
, deviceId
, profileKey
);
265 account
.initStores(dataPath
, identityKey
, registrationId
, trustNewIdentity
);
266 account
.groupStore
= new GroupStore(getGroupCachePath(dataPath
, username
),
267 account
.recipientStore
,
268 account
::saveGroupStore
);
269 account
.stickerStore
= new StickerStore(account
::saveStickerStore
);
271 account
.recipientStore
.resolveRecipientTrusted(account
.getSelfAddress());
272 account
.migrateLegacyConfigs();
278 private void setProvisioningData(
279 final String username
,
281 final String password
,
282 final String encryptedDeviceName
,
284 final ProfileKey profileKey
286 this.username
= username
;
288 this.password
= password
;
289 this.profileKey
= profileKey
;
290 this.encryptedDeviceName
= encryptedDeviceName
;
291 this.deviceId
= deviceId
;
292 this.registered
= true;
293 this.isMultiDevice
= true;
294 this.lastReceiveTimestamp
= 0;
295 this.pinMasterKey
= null;
296 this.storageManifestVersion
= -1;
297 this.storageKey
= null;
300 private void migrateLegacyConfigs() {
301 if (getPassword() == null) {
302 setPassword(KeyUtils
.createPassword());
305 if (getProfileKey() == null && isRegistered()) {
306 // Old config file, creating new profile key
307 setProfileKey(KeyUtils
.createProfileKey());
309 // Ensure our profile key is stored in profile store
310 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
313 private void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
314 sessionStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
315 identityKeyStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
316 messageCache
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
317 groupStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
318 senderKeyStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
321 public static File
getFileName(File dataPath
, String username
) {
322 return new File(dataPath
, username
);
325 private static File
getUserPath(final File dataPath
, final String username
) {
326 final var path
= new File(dataPath
, username
+ ".d");
328 IOUtils
.createPrivateDirectories(path
);
329 } catch (IOException e
) {
330 throw new AssertionError("Failed to create user path", e
);
335 private static File
getMessageCachePath(File dataPath
, String username
) {
336 return new File(getUserPath(dataPath
, username
), "msg-cache");
339 private static File
getGroupCachePath(File dataPath
, String username
) {
340 return new File(getUserPath(dataPath
, username
), "group-cache");
343 private static File
getPreKeysPath(File dataPath
, String username
) {
344 return new File(getUserPath(dataPath
, username
), "pre-keys");
347 private static File
getSignedPreKeysPath(File dataPath
, String username
) {
348 return new File(getUserPath(dataPath
, username
), "signed-pre-keys");
351 private static File
getIdentitiesPath(File dataPath
, String username
) {
352 return new File(getUserPath(dataPath
, username
), "identities");
355 private static File
getSessionsPath(File dataPath
, String username
) {
356 return new File(getUserPath(dataPath
, username
), "sessions");
359 private static File
getSenderKeysPath(File dataPath
, String username
) {
360 return new File(getUserPath(dataPath
, username
), "sender-keys");
363 private static File
getSharedSenderKeysFile(File dataPath
, String username
) {
364 return new File(getUserPath(dataPath
, username
), "shared-sender-keys-store");
367 private static File
getRecipientsStoreFile(File dataPath
, String username
) {
368 return new File(getUserPath(dataPath
, username
), "recipients-store");
371 public static boolean userExists(File dataPath
, String username
) {
372 if (username
== null) {
375 var f
= getFileName(dataPath
, username
);
376 return !(!f
.exists() || f
.isDirectory());
380 File dataPath
, final TrustNewIdentity trustNewIdentity
381 ) throws IOException
{
383 synchronized (fileChannel
) {
384 fileChannel
.position(0);
385 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
388 if (rootNode
.hasNonNull("version")) {
389 var accountVersion
= rootNode
.get("version").asInt(1);
390 if (accountVersion
> CURRENT_STORAGE_VERSION
) {
391 throw new IOException("Config file was created by a more recent version!");
392 } else if (accountVersion
< MINIMUM_STORAGE_VERSION
) {
393 throw new IOException("Config file was created by a no longer supported older version!");
397 username
= Utils
.getNotNullNode(rootNode
, "username").asText();
398 password
= Utils
.getNotNullNode(rootNode
, "password").asText();
399 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
400 if (rootNode
.hasNonNull("uuid")) {
402 uuid
= UUID
.fromString(rootNode
.get("uuid").asText());
403 } catch (IllegalArgumentException e
) {
404 throw new IOException("Config file contains an invalid uuid, needs to be a valid UUID", e
);
407 if (rootNode
.hasNonNull("deviceName")) {
408 encryptedDeviceName
= rootNode
.get("deviceName").asText();
410 if (rootNode
.hasNonNull("deviceId")) {
411 deviceId
= rootNode
.get("deviceId").asInt();
413 if (rootNode
.hasNonNull("isMultiDevice")) {
414 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
416 if (rootNode
.hasNonNull("lastReceiveTimestamp")) {
417 lastReceiveTimestamp
= rootNode
.get("lastReceiveTimestamp").asLong();
419 int registrationId
= 0;
420 if (rootNode
.hasNonNull("registrationId")) {
421 registrationId
= rootNode
.get("registrationId").asInt();
423 IdentityKeyPair identityKeyPair
= null;
424 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
425 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
426 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
427 identityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
430 if (rootNode
.hasNonNull("registrationLockPin")) {
431 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
433 if (rootNode
.hasNonNull("pinMasterKey")) {
434 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
436 if (rootNode
.hasNonNull("storageKey")) {
437 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
439 if (rootNode
.hasNonNull("storageManifestVersion")) {
440 storageManifestVersion
= rootNode
.get("storageManifestVersion").asLong();
442 if (rootNode
.hasNonNull("preKeyIdOffset")) {
443 preKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(0);
447 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
448 nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt();
450 nextSignedPreKeyId
= 0;
452 if (rootNode
.hasNonNull("profileKey")) {
454 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
455 } catch (InvalidInputException e
) {
456 throw new IOException(
457 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
462 var migratedLegacyConfig
= false;
463 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
464 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
465 LegacyJsonSignalProtocolStore
.class)
467 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
468 identityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
469 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
470 migratedLegacyConfig
= true;
473 initStores(dataPath
, identityKeyPair
, registrationId
, trustNewIdentity
);
475 migratedLegacyConfig
= loadLegacyStores(rootNode
, legacySignalProtocolStore
) || migratedLegacyConfig
;
477 if (rootNode
.hasNonNull("groupStore")) {
478 groupStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("groupStore"), GroupStore
.Storage
.class);
479 groupStore
= GroupStore
.fromStorage(groupStoreStorage
,
480 getGroupCachePath(dataPath
, username
),
482 this::saveGroupStore
);
484 groupStore
= new GroupStore(getGroupCachePath(dataPath
, username
), recipientStore
, this::saveGroupStore
);
487 if (rootNode
.hasNonNull("stickerStore")) {
488 stickerStoreStorage
= jsonProcessor
.convertValue(rootNode
.get("stickerStore"), StickerStore
.Storage
.class);
489 stickerStore
= StickerStore
.fromStorage(stickerStoreStorage
, this::saveStickerStore
);
491 stickerStore
= new StickerStore(this::saveStickerStore
);
494 migratedLegacyConfig
= loadLegacyThreadStore(rootNode
) || migratedLegacyConfig
;
496 if (migratedLegacyConfig
) {
501 private boolean loadLegacyStores(
502 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
504 var migrated
= false;
505 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
506 if (legacyRecipientStoreNode
!= null) {
507 logger
.debug("Migrating legacy recipient store.");
508 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
509 if (legacyRecipientStore
!= null) {
510 recipientStore
.resolveRecipientsTrusted(legacyRecipientStore
.getAddresses());
512 recipientStore
.resolveRecipientTrusted(getSelfAddress());
516 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
517 logger
.debug("Migrating legacy pre key store.");
518 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
520 preKeyStore
.storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
521 } catch (IOException e
) {
522 logger
.warn("Failed to migrate pre key, ignoring", e
);
528 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
529 logger
.debug("Migrating legacy signed pre key store.");
530 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
532 signedPreKeyStore
.storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
533 } catch (IOException e
) {
534 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
540 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
541 logger
.debug("Migrating legacy session store.");
542 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
544 sessionStore
.storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
545 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
546 } catch (IOException e
) {
547 logger
.warn("Failed to migrate session, ignoring", e
);
553 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
554 logger
.debug("Migrating legacy identity session store.");
555 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
556 RecipientId recipientId
= recipientStore
.resolveRecipientTrusted(identity
.getAddress());
557 identityKeyStore
.saveIdentity(recipientId
, identity
.getIdentityKey(), identity
.getDateAdded());
558 identityKeyStore
.setIdentityTrustLevel(recipientId
,
559 identity
.getIdentityKey(),
560 identity
.getTrustLevel());
565 if (rootNode
.hasNonNull("contactStore")) {
566 logger
.debug("Migrating legacy contact store.");
567 final var contactStoreNode
= rootNode
.get("contactStore");
568 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
569 for (var contact
: contactStore
.getContacts()) {
570 final var recipientId
= recipientStore
.resolveRecipientTrusted(contact
.getAddress());
571 recipientStore
.storeContact(recipientId
,
572 new Contact(contact
.name
,
574 contact
.messageExpirationTime
,
578 // Store profile keys only in profile store
579 var profileKeyString
= contact
.profileKey
;
580 if (profileKeyString
!= null) {
581 final ProfileKey profileKey
;
583 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
584 getProfileStore().storeProfileKey(recipientId
, profileKey
);
585 } catch (InvalidInputException e
) {
586 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
593 if (rootNode
.hasNonNull("profileStore")) {
594 logger
.debug("Migrating legacy profile store.");
595 var profileStoreNode
= rootNode
.get("profileStore");
596 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
597 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
598 var recipientId
= recipientStore
.resolveRecipient(profileEntry
.getAddress());
599 recipientStore
.storeProfileKeyCredential(recipientId
, profileEntry
.getProfileKeyCredential());
600 recipientStore
.storeProfileKey(recipientId
, profileEntry
.getProfileKey());
601 final var profile
= profileEntry
.getProfile();
602 if (profile
!= null) {
603 final var capabilities
= new HashSet
<Profile
.Capability
>();
604 if (profile
.getCapabilities() != null) {
605 if (profile
.getCapabilities().gv1Migration
) {
606 capabilities
.add(Profile
.Capability
.gv1Migration
);
608 if (profile
.getCapabilities().gv2
) {
609 capabilities
.add(Profile
.Capability
.gv2
);
611 if (profile
.getCapabilities().storage
) {
612 capabilities
.add(Profile
.Capability
.storage
);
615 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
616 profile
.getGivenName(),
617 profile
.getFamilyName(),
619 profile
.getAboutEmoji(),
620 profile
.isUnrestrictedUnidentifiedAccess()
621 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
622 : profile
.getUnidentifiedAccess() != null
623 ? Profile
.UnidentifiedAccessMode
.ENABLED
624 : Profile
.UnidentifiedAccessMode
.DISABLED
,
626 recipientStore
.storeProfile(recipientId
, newProfile
);
634 private boolean loadLegacyThreadStore(final JsonNode rootNode
) {
635 var threadStoreNode
= rootNode
.get("threadStore");
636 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
637 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
638 // Migrate thread info to group and contact store
639 for (var thread
: threadStore
.getThreads()) {
640 if (thread
.id
== null || thread
.id
.isEmpty()) {
644 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
645 final var recipientId
= recipientStore
.resolveRecipient(thread
.id
);
646 var contact
= recipientStore
.getContact(recipientId
);
647 if (contact
!= null) {
648 recipientStore
.storeContact(recipientId
,
649 Contact
.newBuilder(contact
)
650 .withMessageExpirationTime(thread
.messageExpirationTime
)
654 var groupInfo
= groupStore
.getGroup(GroupId
.fromBase64(thread
.id
));
655 if (groupInfo
instanceof GroupInfoV1
) {
656 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
657 groupStore
.updateGroup(groupInfo
);
660 } catch (Exception e
) {
661 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
670 private void saveStickerStore(StickerStore
.Storage storage
) {
671 this.stickerStoreStorage
= storage
;
675 private void saveGroupStore(GroupStore
.Storage storage
) {
676 this.groupStoreStorage
= storage
;
680 private void save() {
681 synchronized (fileChannel
) {
682 var rootNode
= jsonProcessor
.createObjectNode();
683 rootNode
.put("version", CURRENT_STORAGE_VERSION
)
684 .put("username", username
)
685 .put("uuid", uuid
== null ?
null : uuid
.toString())
686 .put("deviceName", encryptedDeviceName
)
687 .put("deviceId", deviceId
)
688 .put("isMultiDevice", isMultiDevice
)
689 .put("lastReceiveTimestamp", lastReceiveTimestamp
)
690 .put("password", password
)
691 .put("registrationId", identityKeyStore
.getLocalRegistrationId())
692 .put("identityPrivateKey",
694 .encodeToString(identityKeyStore
.getIdentityKeyPair().getPrivateKey().serialize()))
697 .encodeToString(identityKeyStore
.getIdentityKeyPair().getPublicKey().serialize()))
698 .put("registrationLockPin", registrationLockPin
)
700 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
702 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
703 .put("storageManifestVersion", storageManifestVersion
== -1 ?
null : storageManifestVersion
)
704 .put("preKeyIdOffset", preKeyIdOffset
)
705 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
707 profileKey
== null ?
null : Base64
.getEncoder().encodeToString(profileKey
.serialize()))
708 .put("registered", registered
)
709 .putPOJO("groupStore", groupStoreStorage
)
710 .putPOJO("stickerStore", stickerStoreStorage
);
712 try (var output
= new ByteArrayOutputStream()) {
713 // Write to memory first to prevent corrupting the file in case of serialization errors
714 jsonProcessor
.writeValue(output
, rootNode
);
715 var input
= new ByteArrayInputStream(output
.toByteArray());
716 fileChannel
.position(0);
717 input
.transferTo(Channels
.newOutputStream(fileChannel
));
718 fileChannel
.truncate(fileChannel
.position());
719 fileChannel
.force(false);
721 } catch (Exception e
) {
722 logger
.error("Error saving file: {}", e
.getMessage());
727 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
, boolean waitForLock
) throws IOException
{
728 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
729 var lock
= fileChannel
.tryLock();
732 logger
.debug("Config file is in use by another instance.");
733 throw new IOException("Config file is in use by another instance.");
735 logger
.info("Config file is in use by another instance, waiting…");
736 lock
= fileChannel
.lock();
737 logger
.info("Config file lock acquired.");
739 return new Pair
<>(fileChannel
, lock
);
742 public void addPreKeys(List
<PreKeyRecord
> records
) {
743 for (var record : records
) {
744 if (preKeyIdOffset
!= record.getId()) {
745 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyIdOffset
);
746 throw new AssertionError("Invalid pre key id");
748 preKeyStore
.storePreKey(record.getId(), record);
749 preKeyIdOffset
= (preKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
754 public void addSignedPreKey(SignedPreKeyRecord
record) {
755 if (nextSignedPreKeyId
!= record.getId()) {
756 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), nextSignedPreKeyId
);
757 throw new AssertionError("Invalid signed pre key id");
759 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
760 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
764 public SignalProtocolStore
getSignalProtocolStore() {
765 return signalProtocolStore
;
768 public SessionStore
getSessionStore() {
772 public IdentityKeyStore
getIdentityKeyStore() {
773 return identityKeyStore
;
776 public GroupStore
getGroupStore() {
780 public ContactsStore
getContactStore() {
781 return recipientStore
;
784 public RecipientStore
getRecipientStore() {
785 return recipientStore
;
788 public ProfileStore
getProfileStore() {
789 return recipientStore
;
792 public StickerStore
getStickerStore() {
796 public SenderKeyStore
getSenderKeyStore() {
797 return senderKeyStore
;
800 public MessageCache
getMessageCache() {
804 public String
getUsername() {
808 public UUID
getUuid() {
812 public void setUuid(final UUID uuid
) {
817 public SignalServiceAddress
getSelfAddress() {
818 return new SignalServiceAddress(uuid
, username
);
821 public RecipientId
getSelfRecipientId() {
822 return recipientStore
.resolveRecipientTrusted(new RecipientAddress(uuid
, username
));
825 public String
getEncryptedDeviceName() {
826 return encryptedDeviceName
;
829 public void setEncryptedDeviceName(final String encryptedDeviceName
) {
830 this.encryptedDeviceName
= encryptedDeviceName
;
834 public int getDeviceId() {
838 public boolean isMasterDevice() {
839 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
842 public IdentityKeyPair
getIdentityKeyPair() {
843 return signalProtocolStore
.getIdentityKeyPair();
846 public int getLocalRegistrationId() {
847 return signalProtocolStore
.getLocalRegistrationId();
850 public String
getPassword() {
854 private void setPassword(final String password
) {
855 this.password
= password
;
859 public String
getRegistrationLockPin() {
860 return registrationLockPin
;
863 public void setRegistrationLockPin(final String registrationLockPin
, final MasterKey pinMasterKey
) {
864 this.registrationLockPin
= registrationLockPin
;
865 this.pinMasterKey
= pinMasterKey
;
869 public MasterKey
getPinMasterKey() {
873 public StorageKey
getStorageKey() {
874 if (pinMasterKey
!= null) {
875 return pinMasterKey
.deriveStorageServiceKey();
880 public void setStorageKey(final StorageKey storageKey
) {
881 if (storageKey
.equals(this.storageKey
)) {
884 this.storageKey
= storageKey
;
888 public long getStorageManifestVersion() {
889 return this.storageManifestVersion
;
892 public void setStorageManifestVersion(final long storageManifestVersion
) {
893 if (storageManifestVersion
== this.storageManifestVersion
) {
896 this.storageManifestVersion
= storageManifestVersion
;
900 public ProfileKey
getProfileKey() {
904 public void setProfileKey(final ProfileKey profileKey
) {
905 if (profileKey
.equals(this.profileKey
)) {
908 this.profileKey
= profileKey
;
912 public byte[] getSelfUnidentifiedAccessKey() {
913 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
916 public int getPreKeyIdOffset() {
917 return preKeyIdOffset
;
920 public int getNextSignedPreKeyId() {
921 return nextSignedPreKeyId
;
924 public boolean isRegistered() {
928 public void setRegistered(final boolean registered
) {
929 this.registered
= registered
;
933 public boolean isMultiDevice() {
934 return isMultiDevice
;
937 public void setMultiDevice(final boolean multiDevice
) {
938 if (isMultiDevice
== multiDevice
) {
941 isMultiDevice
= multiDevice
;
945 public long getLastReceiveTimestamp() {
946 return lastReceiveTimestamp
;
949 public void setLastReceiveTimestamp(final long lastReceiveTimestamp
) {
950 this.lastReceiveTimestamp
= lastReceiveTimestamp
;
954 public boolean isUnrestrictedUnidentifiedAccess() {
955 // TODO make configurable
959 public boolean isDiscoverableByPhoneNumber() {
960 // TODO make configurable
964 public boolean isPhoneNumberShared() {
965 // TODO make configurable
969 public void finishRegistration(final UUID uuid
, final MasterKey masterKey
, final String pin
) {
970 this.pinMasterKey
= masterKey
;
971 this.storageManifestVersion
= -1;
972 this.storageKey
= null;
973 this.encryptedDeviceName
= null;
974 this.deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
975 this.isMultiDevice
= false;
976 this.registered
= true;
978 this.registrationLockPin
= pin
;
979 this.lastReceiveTimestamp
= 0;
982 getSessionStore().archiveAllSessions();
983 senderKeyStore
.deleteAll();
984 final var recipientId
= getRecipientStore().resolveRecipientTrusted(getSelfAddress());
985 final var publicKey
= getIdentityKeyPair().getPublicKey();
986 getIdentityKeyStore().saveIdentity(recipientId
, publicKey
, new Date());
987 getIdentityKeyStore().setIdentityTrustLevel(recipientId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
991 public void close() throws IOException
{
992 synchronized (fileChannel
) {
995 } catch (ClosedChannelException ignored
) {