1 package org
.asamk
.signal
.manager
.storage
;
3 import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
4 import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
5 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
6 import com
.fasterxml
.jackson
.core
.JsonParser
;
7 import com
.fasterxml
.jackson
.databind
.DeserializationFeature
;
8 import com
.fasterxml
.jackson
.databind
.JsonNode
;
9 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
10 import com
.fasterxml
.jackson
.databind
.SerializationFeature
;
12 import org
.asamk
.signal
.manager
.groups
.GroupId
;
13 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
14 import org
.asamk
.signal
.manager
.storage
.contacts
.LegacyJsonContactsStore
;
15 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
16 import org
.asamk
.signal
.manager
.storage
.groups
.JsonGroupStore
;
17 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
18 import org
.asamk
.signal
.manager
.storage
.messageCache
.MessageCache
;
19 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
20 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
21 import org
.asamk
.signal
.manager
.storage
.profiles
.LegacyProfileStore
;
22 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
23 import org
.asamk
.signal
.manager
.storage
.protocol
.LegacyJsonSignalProtocolStore
;
24 import org
.asamk
.signal
.manager
.storage
.protocol
.SignalProtocolStore
;
25 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
26 import org
.asamk
.signal
.manager
.storage
.recipients
.LegacyRecipientStore
;
27 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
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
.sessions
.SessionStore
;
31 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
32 import org
.asamk
.signal
.manager
.storage
.threads
.LegacyJsonThreadStore
;
33 import org
.asamk
.signal
.manager
.util
.IOUtils
;
34 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
35 import org
.asamk
.signal
.manager
.util
.Utils
;
36 import org
.signal
.zkgroup
.InvalidInputException
;
37 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
38 import org
.slf4j
.Logger
;
39 import org
.slf4j
.LoggerFactory
;
40 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
41 import org
.whispersystems
.libsignal
.SignalProtocolAddress
;
42 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
43 import org
.whispersystems
.libsignal
.state
.SessionRecord
;
44 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
45 import org
.whispersystems
.libsignal
.util
.Medium
;
46 import org
.whispersystems
.libsignal
.util
.Pair
;
47 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
48 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
49 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
50 import org
.whispersystems
.signalservice
.api
.storage
.StorageKey
;
51 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
53 import java
.io
.ByteArrayInputStream
;
54 import java
.io
.ByteArrayOutputStream
;
55 import java
.io
.Closeable
;
57 import java
.io
.IOException
;
58 import java
.io
.RandomAccessFile
;
59 import java
.nio
.channels
.Channels
;
60 import java
.nio
.channels
.ClosedChannelException
;
61 import java
.nio
.channels
.FileChannel
;
62 import java
.nio
.channels
.FileLock
;
63 import java
.util
.Base64
;
64 import java
.util
.HashSet
;
65 import java
.util
.List
;
66 import java
.util
.UUID
;
68 public class SignalAccount
implements Closeable
{
70 private final static Logger logger
= LoggerFactory
.getLogger(SignalAccount
.class);
72 private final ObjectMapper jsonProcessor
= new ObjectMapper();
73 private final FileChannel fileChannel
;
74 private final FileLock lock
;
75 private String username
;
77 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
78 private boolean isMultiDevice
= false;
79 private String password
;
80 private String registrationLockPin
;
81 private MasterKey pinMasterKey
;
82 private StorageKey storageKey
;
83 private ProfileKey profileKey
;
84 private int preKeyIdOffset
;
85 private int nextSignedPreKeyId
;
87 private boolean registered
= false;
89 private SignalProtocolStore signalProtocolStore
;
90 private PreKeyStore preKeyStore
;
91 private SignedPreKeyStore signedPreKeyStore
;
92 private SessionStore sessionStore
;
93 private IdentityKeyStore identityKeyStore
;
94 private JsonGroupStore groupStore
;
95 private RecipientStore recipientStore
;
96 private StickerStore stickerStore
;
98 private MessageCache messageCache
;
100 private SignalAccount(final FileChannel fileChannel
, final FileLock lock
) {
101 this.fileChannel
= fileChannel
;
103 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
104 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print
105 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
106 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
107 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
110 public static SignalAccount
load(File dataPath
, String username
) throws IOException
{
111 final var fileName
= getFileName(dataPath
, username
);
112 final var pair
= openFileChannel(fileName
);
114 var account
= new SignalAccount(pair
.first(), pair
.second());
115 account
.load(dataPath
);
116 account
.migrateLegacyConfigs();
119 } catch (Throwable e
) {
120 pair
.second().close();
121 pair
.first().close();
126 public static SignalAccount
create(
127 File dataPath
, String username
, IdentityKeyPair identityKey
, int registrationId
, ProfileKey profileKey
128 ) throws IOException
{
129 IOUtils
.createPrivateDirectories(dataPath
);
130 var fileName
= getFileName(dataPath
, username
);
131 if (!fileName
.exists()) {
132 IOUtils
.createPrivateFile(fileName
);
135 final var pair
= openFileChannel(fileName
);
136 var account
= new SignalAccount(pair
.first(), pair
.second());
138 account
.username
= username
;
139 account
.profileKey
= profileKey
;
140 account
.groupStore
= new JsonGroupStore(getGroupCachePath(dataPath
, username
));
141 account
.recipientStore
= RecipientStore
.load(getRecipientsStoreFile(dataPath
, username
),
142 account
::mergeRecipients
);
143 account
.preKeyStore
= new PreKeyStore(getPreKeysPath(dataPath
, username
));
144 account
.signedPreKeyStore
= new SignedPreKeyStore(getSignedPreKeysPath(dataPath
, username
));
145 account
.sessionStore
= new SessionStore(getSessionsPath(dataPath
, username
),
146 account
.recipientStore
::resolveRecipient
);
147 account
.identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, username
),
148 account
.recipientStore
::resolveRecipient
,
151 account
.signalProtocolStore
= new SignalProtocolStore(account
.preKeyStore
,
152 account
.signedPreKeyStore
,
153 account
.sessionStore
,
154 account
.identityKeyStore
);
155 account
.stickerStore
= new StickerStore();
157 account
.messageCache
= new MessageCache(getMessageCachePath(dataPath
, username
));
159 account
.registered
= false;
161 account
.migrateLegacyConfigs();
166 public static SignalAccount
createLinkedAccount(
172 IdentityKeyPair identityKey
,
174 ProfileKey profileKey
175 ) throws IOException
{
176 IOUtils
.createPrivateDirectories(dataPath
);
177 var fileName
= getFileName(dataPath
, username
);
178 if (!fileName
.exists()) {
179 IOUtils
.createPrivateFile(fileName
);
182 final var pair
= openFileChannel(fileName
);
183 var account
= new SignalAccount(pair
.first(), pair
.second());
185 account
.username
= username
;
187 account
.password
= password
;
188 account
.profileKey
= profileKey
;
189 account
.deviceId
= deviceId
;
190 account
.groupStore
= new JsonGroupStore(getGroupCachePath(dataPath
, username
));
191 account
.recipientStore
= RecipientStore
.load(getRecipientsStoreFile(dataPath
, username
),
192 account
::mergeRecipients
);
193 account
.recipientStore
.resolveRecipientTrusted(account
.getSelfAddress());
194 account
.preKeyStore
= new PreKeyStore(getPreKeysPath(dataPath
, username
));
195 account
.signedPreKeyStore
= new SignedPreKeyStore(getSignedPreKeysPath(dataPath
, username
));
196 account
.sessionStore
= new SessionStore(getSessionsPath(dataPath
, username
),
197 account
.recipientStore
::resolveRecipient
);
198 account
.identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, username
),
199 account
.recipientStore
::resolveRecipient
,
202 account
.signalProtocolStore
= new SignalProtocolStore(account
.preKeyStore
,
203 account
.signedPreKeyStore
,
204 account
.sessionStore
,
205 account
.identityKeyStore
);
206 account
.stickerStore
= new StickerStore();
208 account
.messageCache
= new MessageCache(getMessageCachePath(dataPath
, username
));
210 account
.registered
= true;
211 account
.isMultiDevice
= true;
213 account
.migrateLegacyConfigs();
218 public void migrateLegacyConfigs() {
219 if (getProfileKey() == null && isRegistered()) {
220 // Old config file, creating new profile key
221 setProfileKey(KeyUtils
.createProfileKey());
224 // Ensure our profile key is stored in profile store
225 getProfileStore().storeProfileKey(getSelfRecipientId(), getProfileKey());
228 private void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
229 sessionStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
230 identityKeyStore
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
231 messageCache
.mergeRecipients(recipientId
, toBeMergedRecipientId
);
234 public static File
getFileName(File dataPath
, String username
) {
235 return new File(dataPath
, username
);
238 private static File
getUserPath(final File dataPath
, final String username
) {
239 return new File(dataPath
, username
+ ".d");
242 public static File
getMessageCachePath(File dataPath
, String username
) {
243 return new File(getUserPath(dataPath
, username
), "msg-cache");
246 private static File
getGroupCachePath(File dataPath
, String username
) {
247 return new File(getUserPath(dataPath
, username
), "group-cache");
250 private static File
getPreKeysPath(File dataPath
, String username
) {
251 return new File(getUserPath(dataPath
, username
), "pre-keys");
254 private static File
getSignedPreKeysPath(File dataPath
, String username
) {
255 return new File(getUserPath(dataPath
, username
), "signed-pre-keys");
258 private static File
getIdentitiesPath(File dataPath
, String username
) {
259 return new File(getUserPath(dataPath
, username
), "identities");
262 private static File
getSessionsPath(File dataPath
, String username
) {
263 return new File(getUserPath(dataPath
, username
), "sessions");
266 private static File
getRecipientsStoreFile(File dataPath
, String username
) {
267 return new File(getUserPath(dataPath
, username
), "recipients-store");
270 public static boolean userExists(File dataPath
, String username
) {
271 if (username
== null) {
274 var f
= getFileName(dataPath
, username
);
275 return !(!f
.exists() || f
.isDirectory());
278 private void load(File dataPath
) throws IOException
{
280 synchronized (fileChannel
) {
281 fileChannel
.position(0);
282 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
285 username
= Utils
.getNotNullNode(rootNode
, "username").asText();
286 password
= Utils
.getNotNullNode(rootNode
, "password").asText();
287 registered
= Utils
.getNotNullNode(rootNode
, "registered").asBoolean();
288 if (rootNode
.hasNonNull("uuid")) {
290 uuid
= UUID
.fromString(rootNode
.get("uuid").asText());
291 } catch (IllegalArgumentException e
) {
292 throw new IOException("Config file contains an invalid uuid, needs to be a valid UUID", e
);
295 if (rootNode
.hasNonNull("deviceId")) {
296 deviceId
= rootNode
.get("deviceId").asInt();
298 if (rootNode
.hasNonNull("isMultiDevice")) {
299 isMultiDevice
= rootNode
.get("isMultiDevice").asBoolean();
301 int registrationId
= 0;
302 if (rootNode
.hasNonNull("registrationId")) {
303 registrationId
= rootNode
.get("registrationId").asInt();
305 IdentityKeyPair identityKeyPair
= null;
306 if (rootNode
.hasNonNull("identityPrivateKey") && rootNode
.hasNonNull("identityKey")) {
307 final var publicKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityKey").asText());
308 final var privateKeyBytes
= Base64
.getDecoder().decode(rootNode
.get("identityPrivateKey").asText());
309 identityKeyPair
= KeyUtils
.getIdentityKeyPair(publicKeyBytes
, privateKeyBytes
);
312 if (rootNode
.hasNonNull("registrationLockPin")) {
313 registrationLockPin
= rootNode
.get("registrationLockPin").asText();
315 if (rootNode
.hasNonNull("pinMasterKey")) {
316 pinMasterKey
= new MasterKey(Base64
.getDecoder().decode(rootNode
.get("pinMasterKey").asText()));
318 if (rootNode
.hasNonNull("storageKey")) {
319 storageKey
= new StorageKey(Base64
.getDecoder().decode(rootNode
.get("storageKey").asText()));
321 if (rootNode
.hasNonNull("preKeyIdOffset")) {
322 preKeyIdOffset
= rootNode
.get("preKeyIdOffset").asInt(0);
326 if (rootNode
.hasNonNull("nextSignedPreKeyId")) {
327 nextSignedPreKeyId
= rootNode
.get("nextSignedPreKeyId").asInt();
329 nextSignedPreKeyId
= 0;
331 if (rootNode
.hasNonNull("profileKey")) {
333 profileKey
= new ProfileKey(Base64
.getDecoder().decode(rootNode
.get("profileKey").asText()));
334 } catch (InvalidInputException e
) {
335 throw new IOException(
336 "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
341 final var legacySignalProtocolStore
= rootNode
.hasNonNull("axolotlStore")
342 ? jsonProcessor
.convertValue(Utils
.getNotNullNode(rootNode
, "axolotlStore"),
343 LegacyJsonSignalProtocolStore
.class)
345 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
346 identityKeyPair
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentityKeyPair();
347 registrationId
= legacySignalProtocolStore
.getLegacyIdentityKeyStore().getLocalRegistrationId();
350 recipientStore
= RecipientStore
.load(getRecipientsStoreFile(dataPath
, username
), this::mergeRecipients
);
352 preKeyStore
= new PreKeyStore(getPreKeysPath(dataPath
, username
));
353 signedPreKeyStore
= new SignedPreKeyStore(getSignedPreKeysPath(dataPath
, username
));
354 sessionStore
= new SessionStore(getSessionsPath(dataPath
, username
), recipientStore
::resolveRecipient
);
355 identityKeyStore
= new IdentityKeyStore(getIdentitiesPath(dataPath
, username
),
356 recipientStore
::resolveRecipient
,
360 loadLegacyStores(rootNode
, legacySignalProtocolStore
);
362 signalProtocolStore
= new SignalProtocolStore(preKeyStore
, signedPreKeyStore
, sessionStore
, identityKeyStore
);
364 var groupStoreNode
= rootNode
.get("groupStore");
365 if (groupStoreNode
!= null) {
366 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
367 groupStore
.groupCachePath
= getGroupCachePath(dataPath
, username
);
369 if (groupStore
== null) {
370 groupStore
= new JsonGroupStore(getGroupCachePath(dataPath
, username
));
373 var stickerStoreNode
= rootNode
.get("stickerStore");
374 if (stickerStoreNode
!= null) {
375 stickerStore
= jsonProcessor
.convertValue(stickerStoreNode
, StickerStore
.class);
377 if (stickerStore
== null) {
378 stickerStore
= new StickerStore();
381 messageCache
= new MessageCache(getMessageCachePath(dataPath
, username
));
383 loadLegacyThreadStore(rootNode
);
386 private void loadLegacyStores(
387 final JsonNode rootNode
, final LegacyJsonSignalProtocolStore legacySignalProtocolStore
389 var legacyRecipientStoreNode
= rootNode
.get("recipientStore");
390 if (legacyRecipientStoreNode
!= null) {
391 logger
.debug("Migrating legacy recipient store.");
392 var legacyRecipientStore
= jsonProcessor
.convertValue(legacyRecipientStoreNode
, LegacyRecipientStore
.class);
393 if (legacyRecipientStore
!= null) {
394 recipientStore
.resolveRecipientsTrusted(legacyRecipientStore
.getAddresses());
396 recipientStore
.resolveRecipientTrusted(getSelfAddress());
399 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyPreKeyStore() != null) {
400 logger
.debug("Migrating legacy pre key store.");
401 for (var entry
: legacySignalProtocolStore
.getLegacyPreKeyStore().getPreKeys().entrySet()) {
403 preKeyStore
.storePreKey(entry
.getKey(), new PreKeyRecord(entry
.getValue()));
404 } catch (IOException e
) {
405 logger
.warn("Failed to migrate pre key, ignoring", e
);
410 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySignedPreKeyStore() != null) {
411 logger
.debug("Migrating legacy signed pre key store.");
412 for (var entry
: legacySignalProtocolStore
.getLegacySignedPreKeyStore().getSignedPreKeys().entrySet()) {
414 signedPreKeyStore
.storeSignedPreKey(entry
.getKey(), new SignedPreKeyRecord(entry
.getValue()));
415 } catch (IOException e
) {
416 logger
.warn("Failed to migrate signed pre key, ignoring", e
);
421 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacySessionStore() != null) {
422 logger
.debug("Migrating legacy session store.");
423 for (var session
: legacySignalProtocolStore
.getLegacySessionStore().getSessions()) {
425 sessionStore
.storeSession(new SignalProtocolAddress(session
.address
.getIdentifier(),
426 session
.deviceId
), new SessionRecord(session
.sessionRecord
));
427 } catch (IOException e
) {
428 logger
.warn("Failed to migrate session, ignoring", e
);
433 if (legacySignalProtocolStore
!= null && legacySignalProtocolStore
.getLegacyIdentityKeyStore() != null) {
434 logger
.debug("Migrating legacy identity session store.");
435 for (var identity
: legacySignalProtocolStore
.getLegacyIdentityKeyStore().getIdentities()) {
436 RecipientId recipientId
= recipientStore
.resolveRecipientTrusted(identity
.getAddress());
437 identityKeyStore
.saveIdentity(recipientId
, identity
.getIdentityKey(), identity
.getDateAdded());
438 identityKeyStore
.setIdentityTrustLevel(recipientId
,
439 identity
.getIdentityKey(),
440 identity
.getTrustLevel());
444 if (rootNode
.hasNonNull("contactStore")) {
445 logger
.debug("Migrating legacy contact store.");
446 final var contactStoreNode
= rootNode
.get("contactStore");
447 final var contactStore
= jsonProcessor
.convertValue(contactStoreNode
, LegacyJsonContactsStore
.class);
448 for (var contact
: contactStore
.getContacts()) {
449 final var recipientId
= recipientStore
.resolveRecipientTrusted(contact
.getAddress());
450 recipientStore
.storeContact(recipientId
,
451 new Contact(contact
.name
,
453 contact
.messageExpirationTime
,
457 // Store profile keys only in profile store
458 var profileKeyString
= contact
.profileKey
;
459 if (profileKeyString
!= null) {
460 final ProfileKey profileKey
;
462 profileKey
= new ProfileKey(Base64
.getDecoder().decode(profileKeyString
));
463 getProfileStore().storeProfileKey(recipientId
, profileKey
);
464 } catch (InvalidInputException e
) {
465 logger
.warn("Failed to parse legacy contact profile key: {}", e
.getMessage());
471 if (rootNode
.hasNonNull("profileStore")) {
472 logger
.debug("Migrating legacy profile store.");
473 var profileStoreNode
= rootNode
.get("profileStore");
474 final var legacyProfileStore
= jsonProcessor
.convertValue(profileStoreNode
, LegacyProfileStore
.class);
475 for (var profileEntry
: legacyProfileStore
.getProfileEntries()) {
476 var recipientId
= recipientStore
.resolveRecipient(profileEntry
.getServiceAddress());
477 recipientStore
.storeProfileKey(recipientId
, profileEntry
.getProfileKey());
478 recipientStore
.storeProfileKeyCredential(recipientId
, profileEntry
.getProfileKeyCredential());
479 final var profile
= profileEntry
.getProfile();
480 if (profile
!= null) {
481 final var capabilities
= new HashSet
<Profile
.Capability
>();
482 if (profile
.getCapabilities().gv1Migration
) {
483 capabilities
.add(Profile
.Capability
.gv1Migration
);
485 if (profile
.getCapabilities().gv2
) {
486 capabilities
.add(Profile
.Capability
.gv2
);
488 if (profile
.getCapabilities().storage
) {
489 capabilities
.add(Profile
.Capability
.storage
);
491 final var newProfile
= new Profile(profileEntry
.getLastUpdateTimestamp(),
492 profile
.getGivenName(),
493 profile
.getFamilyName(),
495 profile
.getAboutEmoji(),
496 profile
.isUnrestrictedUnidentifiedAccess()
497 ? Profile
.UnidentifiedAccessMode
.UNRESTRICTED
498 : profile
.getUnidentifiedAccess() != null
499 ? Profile
.UnidentifiedAccessMode
.ENABLED
500 : Profile
.UnidentifiedAccessMode
.DISABLED
,
502 recipientStore
.storeProfile(recipientId
, newProfile
);
508 private void loadLegacyThreadStore(final JsonNode rootNode
) {
509 var threadStoreNode
= rootNode
.get("threadStore");
510 if (threadStoreNode
!= null && !threadStoreNode
.isNull()) {
511 var threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
512 // Migrate thread info to group and contact store
513 for (var thread
: threadStore
.getThreads()) {
514 if (thread
.id
== null || thread
.id
.isEmpty()) {
518 if (UuidUtil
.isUuid(thread
.id
) || thread
.id
.startsWith("+")) {
519 final var recipientId
= recipientStore
.resolveRecipient(thread
.id
);
520 var contact
= recipientStore
.getContact(recipientId
);
521 if (contact
!= null) {
522 recipientStore
.storeContact(recipientId
,
523 Contact
.newBuilder(contact
)
524 .withMessageExpirationTime(thread
.messageExpirationTime
)
528 var groupInfo
= groupStore
.getGroup(GroupId
.fromBase64(thread
.id
));
529 if (groupInfo
instanceof GroupInfoV1
) {
530 ((GroupInfoV1
) groupInfo
).messageExpirationTime
= thread
.messageExpirationTime
;
531 groupStore
.updateGroup(groupInfo
);
534 } catch (Exception e
) {
535 logger
.warn("Failed to read legacy thread info: {}", e
.getMessage());
542 if (fileChannel
== null) {
545 var rootNode
= jsonProcessor
.createObjectNode();
546 rootNode
.put("username", username
)
547 .put("uuid", uuid
== null ?
null : uuid
.toString())
548 .put("deviceId", deviceId
)
549 .put("isMultiDevice", isMultiDevice
)
550 .put("password", password
)
551 .put("registrationId", identityKeyStore
.getLocalRegistrationId())
552 .put("identityPrivateKey",
554 .encodeToString(identityKeyStore
.getIdentityKeyPair().getPrivateKey().serialize()))
557 .encodeToString(identityKeyStore
.getIdentityKeyPair().getPublicKey().serialize()))
558 .put("registrationLockPin", registrationLockPin
)
560 pinMasterKey
== null ?
null : Base64
.getEncoder().encodeToString(pinMasterKey
.serialize()))
562 storageKey
== null ?
null : Base64
.getEncoder().encodeToString(storageKey
.serialize()))
563 .put("preKeyIdOffset", preKeyIdOffset
)
564 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
565 .put("profileKey", Base64
.getEncoder().encodeToString(profileKey
.serialize()))
566 .put("registered", registered
)
567 .putPOJO("groupStore", groupStore
)
568 .putPOJO("stickerStore", stickerStore
);
570 try (var output
= new ByteArrayOutputStream()) {
571 // Write to memory first to prevent corrupting the file in case of serialization errors
572 jsonProcessor
.writeValue(output
, rootNode
);
573 var input
= new ByteArrayInputStream(output
.toByteArray());
574 synchronized (fileChannel
) {
575 fileChannel
.position(0);
576 input
.transferTo(Channels
.newOutputStream(fileChannel
));
577 fileChannel
.truncate(fileChannel
.position());
578 fileChannel
.force(false);
581 } catch (Exception e
) {
582 logger
.error("Error saving file: {}", e
.getMessage());
586 private static Pair
<FileChannel
, FileLock
> openFileChannel(File fileName
) throws IOException
{
587 var fileChannel
= new RandomAccessFile(fileName
, "rw").getChannel();
588 var lock
= fileChannel
.tryLock();
590 logger
.info("Config file is in use by another instance, waiting…");
591 lock
= fileChannel
.lock();
592 logger
.info("Config file lock acquired.");
594 return new Pair
<>(fileChannel
, lock
);
597 public void addPreKeys(List
<PreKeyRecord
> records
) {
598 for (var record : records
) {
599 if (preKeyIdOffset
!= record.getId()) {
600 logger
.error("Invalid pre key id {}, expected {}", record.getId(), preKeyIdOffset
);
601 throw new AssertionError("Invalid pre key id");
603 preKeyStore
.storePreKey(record.getId(), record);
604 preKeyIdOffset
= (preKeyIdOffset
+ 1) % Medium
.MAX_VALUE
;
609 public void addSignedPreKey(SignedPreKeyRecord
record) {
610 if (nextSignedPreKeyId
!= record.getId()) {
611 logger
.error("Invalid signed pre key id {}, expected {}", record.getId(), nextSignedPreKeyId
);
612 throw new AssertionError("Invalid signed pre key id");
614 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
615 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
619 public SignalProtocolStore
getSignalProtocolStore() {
620 return signalProtocolStore
;
623 public SessionStore
getSessionStore() {
627 public IdentityKeyStore
getIdentityKeyStore() {
628 return identityKeyStore
;
631 public JsonGroupStore
getGroupStore() {
635 public ContactsStore
getContactStore() {
636 return recipientStore
;
639 public RecipientStore
getRecipientStore() {
640 return recipientStore
;
643 public ProfileStore
getProfileStore() {
644 return recipientStore
;
647 public StickerStore
getStickerStore() {
651 public MessageCache
getMessageCache() {
655 public String
getUsername() {
659 public UUID
getUuid() {
663 public void setUuid(final UUID uuid
) {
667 public SignalServiceAddress
getSelfAddress() {
668 return new SignalServiceAddress(uuid
, username
);
671 public RecipientId
getSelfRecipientId() {
672 return recipientStore
.resolveRecipientTrusted(getSelfAddress());
675 public int getDeviceId() {
679 public void setDeviceId(final int deviceId
) {
680 this.deviceId
= deviceId
;
683 public boolean isMasterDevice() {
684 return deviceId
== SignalServiceAddress
.DEFAULT_DEVICE_ID
;
687 public IdentityKeyPair
getIdentityKeyPair() {
688 return signalProtocolStore
.getIdentityKeyPair();
691 public int getLocalRegistrationId() {
692 return signalProtocolStore
.getLocalRegistrationId();
695 public String
getPassword() {
699 public void setPassword(final String password
) {
700 this.password
= password
;
703 public String
getRegistrationLockPin() {
704 return registrationLockPin
;
707 public void setRegistrationLockPin(final String registrationLockPin
) {
708 this.registrationLockPin
= registrationLockPin
;
711 public MasterKey
getPinMasterKey() {
715 public void setPinMasterKey(final MasterKey pinMasterKey
) {
716 this.pinMasterKey
= pinMasterKey
;
719 public StorageKey
getStorageKey() {
720 if (pinMasterKey
!= null) {
721 return pinMasterKey
.deriveStorageServiceKey();
726 public void setStorageKey(final StorageKey storageKey
) {
727 this.storageKey
= storageKey
;
730 public ProfileKey
getProfileKey() {
734 public void setProfileKey(final ProfileKey profileKey
) {
735 this.profileKey
= profileKey
;
738 public byte[] getSelfUnidentifiedAccessKey() {
739 return UnidentifiedAccess
.deriveAccessKeyFrom(getProfileKey());
742 public int getPreKeyIdOffset() {
743 return preKeyIdOffset
;
746 public int getNextSignedPreKeyId() {
747 return nextSignedPreKeyId
;
750 public boolean isRegistered() {
754 public void setRegistered(final boolean registered
) {
755 this.registered
= registered
;
758 public boolean isMultiDevice() {
759 return isMultiDevice
;
762 public void setMultiDevice(final boolean multiDevice
) {
763 isMultiDevice
= multiDevice
;
766 public boolean isUnrestrictedUnidentifiedAccess() {
767 // TODO make configurable
771 public boolean isDiscoverableByPhoneNumber() {
772 // TODO make configurable
777 public void close() throws IOException
{
778 if (fileChannel
.isOpen()) {
781 synchronized (fileChannel
) {
784 } catch (ClosedChannelException ignored
) {