1 package org
.asamk
.signal
.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
;
11 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
13 import org
.asamk
.signal
.storage
.contacts
.ContactInfo
;
14 import org
.asamk
.signal
.storage
.contacts
.JsonContactsStore
;
15 import org
.asamk
.signal
.storage
.groups
.GroupInfo
;
16 import org
.asamk
.signal
.storage
.groups
.JsonGroupStore
;
17 import org
.asamk
.signal
.storage
.protocol
.JsonSignalProtocolStore
;
18 import org
.asamk
.signal
.storage
.threads
.LegacyJsonThreadStore
;
19 import org
.asamk
.signal
.storage
.threads
.ThreadInfo
;
20 import org
.asamk
.signal
.util
.IOUtils
;
21 import org
.asamk
.signal
.util
.Util
;
22 import org
.signal
.zkgroup
.InvalidInputException
;
23 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
24 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
25 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
26 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
27 import org
.whispersystems
.libsignal
.util
.Medium
;
28 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
29 import org
.whispersystems
.util
.Base64
;
32 import java
.io
.IOException
;
33 import java
.io
.RandomAccessFile
;
34 import java
.nio
.channels
.Channels
;
35 import java
.nio
.channels
.FileChannel
;
36 import java
.nio
.channels
.FileLock
;
37 import java
.util
.Collection
;
38 import java
.util
.UUID
;
40 public class SignalAccount
{
42 private final ObjectMapper jsonProcessor
= new ObjectMapper();
43 private FileChannel fileChannel
;
44 private FileLock lock
;
45 private String username
;
47 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
48 private boolean isMultiDevice
= false;
49 private String password
;
50 private String registrationLockPin
;
51 private String signalingKey
;
52 private ProfileKey profileKey
;
53 private int preKeyIdOffset
;
54 private int nextSignedPreKeyId
;
56 private boolean registered
= false;
58 private JsonSignalProtocolStore signalProtocolStore
;
59 private JsonGroupStore groupStore
;
60 private JsonContactsStore contactStore
;
62 private SignalAccount() {
63 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
64 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
65 jsonProcessor
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
66 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
67 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
68 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
71 public static SignalAccount
load(String dataPath
, String username
) throws IOException
{
72 SignalAccount account
= new SignalAccount();
73 IOUtils
.createPrivateDirectories(dataPath
);
74 account
.openFileChannel(getFileName(dataPath
, username
));
79 public static SignalAccount
create(String dataPath
, String username
, IdentityKeyPair identityKey
, int registrationId
, ProfileKey profileKey
) throws IOException
{
80 IOUtils
.createPrivateDirectories(dataPath
);
82 SignalAccount account
= new SignalAccount();
83 account
.openFileChannel(getFileName(dataPath
, username
));
85 account
.username
= username
;
86 account
.profileKey
= profileKey
;
87 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
88 account
.groupStore
= new JsonGroupStore();
89 account
.contactStore
= new JsonContactsStore();
90 account
.registered
= false;
95 public static SignalAccount
createLinkedAccount(String dataPath
, String username
, String password
, int deviceId
, IdentityKeyPair identityKey
, int registrationId
, String signalingKey
, ProfileKey profileKey
) throws IOException
{
96 IOUtils
.createPrivateDirectories(dataPath
);
98 SignalAccount account
= new SignalAccount();
99 account
.openFileChannel(getFileName(dataPath
, username
));
101 account
.username
= username
;
102 account
.password
= password
;
103 account
.profileKey
= profileKey
;
104 account
.deviceId
= deviceId
;
105 account
.signalingKey
= signalingKey
;
106 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
107 account
.groupStore
= new JsonGroupStore();
108 account
.contactStore
= new JsonContactsStore();
109 account
.registered
= true;
110 account
.isMultiDevice
= true;
115 public static SignalAccount
createTemporaryAccount(IdentityKeyPair identityKey
, int registrationId
) {
116 SignalAccount account
= new SignalAccount();
118 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
119 account
.registered
= false;
124 public static String
getFileName(String dataPath
, String username
) {
125 return dataPath
+ "/" + username
;
128 public static boolean userExists(String dataPath
, String username
) {
129 if (username
== null) {
132 File f
= new File(getFileName(dataPath
, username
));
133 return !(!f
.exists() || f
.isDirectory());
136 private void load() throws IOException
{
138 synchronized (fileChannel
) {
139 fileChannel
.position(0);
140 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
143 JsonNode node
= rootNode
.get("deviceId");
145 deviceId
= node
.asInt();
147 if (rootNode
.has("isMultiDevice")) {
148 isMultiDevice
= Util
.getNotNullNode(rootNode
, "isMultiDevice").asBoolean();
150 username
= Util
.getNotNullNode(rootNode
, "username").asText();
151 password
= Util
.getNotNullNode(rootNode
, "password").asText();
152 JsonNode pinNode
= rootNode
.get("registrationLockPin");
153 registrationLockPin
= pinNode
== null || pinNode
.isNull() ?
null : pinNode
.asText();
154 if (rootNode
.has("signalingKey")) {
155 signalingKey
= Util
.getNotNullNode(rootNode
, "signalingKey").asText();
157 if (rootNode
.has("preKeyIdOffset")) {
158 preKeyIdOffset
= Util
.getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
162 if (rootNode
.has("nextSignedPreKeyId")) {
163 nextSignedPreKeyId
= Util
.getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
165 nextSignedPreKeyId
= 0;
167 if (rootNode
.has("profileKey")) {
169 profileKey
= new ProfileKey(Base64
.decode(Util
.getNotNullNode(rootNode
, "profileKey").asText()));
170 } catch (InvalidInputException e
) {
171 throw new IOException("Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes", e
);
175 signalProtocolStore
= jsonProcessor
.convertValue(Util
.getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
176 registered
= Util
.getNotNullNode(rootNode
, "registered").asBoolean();
177 JsonNode groupStoreNode
= rootNode
.get("groupStore");
178 if (groupStoreNode
!= null) {
179 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
181 if (groupStore
== null) {
182 groupStore
= new JsonGroupStore();
185 JsonNode contactStoreNode
= rootNode
.get("contactStore");
186 if (contactStoreNode
!= null) {
187 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
189 if (contactStore
== null) {
190 contactStore
= new JsonContactsStore();
192 JsonNode threadStoreNode
= rootNode
.get("threadStore");
193 if (threadStoreNode
!= null) {
194 LegacyJsonThreadStore threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
195 // Migrate thread info to group and contact store
196 for (ThreadInfo thread
: threadStore
.getThreads()) {
198 ContactInfo contactInfo
= contactStore
.getContact(new SignalServiceAddress(null, thread
.id
));
199 if (contactInfo
!= null) {
200 contactInfo
.messageExpirationTime
= thread
.messageExpirationTime
;
201 contactStore
.updateContact(contactInfo
);
203 GroupInfo groupInfo
= groupStore
.getGroup(Base64
.decode(thread
.id
));
204 if (groupInfo
!= null) {
205 groupInfo
.messageExpirationTime
= thread
.messageExpirationTime
;
206 groupStore
.updateGroup(groupInfo
);
209 } catch (Exception ignored
) {
216 if (fileChannel
== null) {
219 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
220 rootNode
.put("username", username
)
221 .put("deviceId", deviceId
)
222 .put("isMultiDevice", isMultiDevice
)
223 .put("password", password
)
224 .put("registrationLockPin", registrationLockPin
)
225 .put("signalingKey", signalingKey
)
226 .put("preKeyIdOffset", preKeyIdOffset
)
227 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
228 .put("profileKey", Base64
.encodeBytes(profileKey
.serialize()))
229 .put("registered", registered
)
230 .putPOJO("axolotlStore", signalProtocolStore
)
231 .putPOJO("groupStore", groupStore
)
232 .putPOJO("contactStore", contactStore
)
235 synchronized (fileChannel
) {
236 fileChannel
.position(0);
237 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
238 fileChannel
.truncate(fileChannel
.position());
239 fileChannel
.force(false);
241 } catch (Exception e
) {
242 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
246 private void openFileChannel(String fileName
) throws IOException
{
247 if (fileChannel
!= null) {
251 if (!new File(fileName
).exists()) {
252 IOUtils
.createPrivateFile(fileName
);
254 fileChannel
= new RandomAccessFile(new File(fileName
), "rw").getChannel();
255 lock
= fileChannel
.tryLock();
257 System
.err
.println("Config file is in use by another instance, waiting…");
258 lock
= fileChannel
.lock();
259 System
.err
.println("Config file lock acquired.");
263 public void addPreKeys(Collection
<PreKeyRecord
> records
) {
264 for (PreKeyRecord
record : records
) {
265 signalProtocolStore
.storePreKey(record.getId(), record);
267 preKeyIdOffset
= (preKeyIdOffset
+ records
.size()) % Medium
.MAX_VALUE
;
270 public void addSignedPreKey(SignedPreKeyRecord
record) {
271 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
272 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
275 public JsonSignalProtocolStore
getSignalProtocolStore() {
276 return signalProtocolStore
;
279 public JsonGroupStore
getGroupStore() {
283 public JsonContactsStore
getContactStore() {
287 public String
getUsername() {
291 public UUID
getUuid() {
295 public SignalServiceAddress
getSelfAddress() {
296 return new SignalServiceAddress(uuid
, username
);
299 public int getDeviceId() {
303 public String
getPassword() {
307 public void setPassword(final String password
) {
308 this.password
= password
;
311 public String
getRegistrationLockPin() {
312 return registrationLockPin
;
315 public String
getRegistrationLock() {
316 return null; // TODO implement KBS
319 public void setRegistrationLockPin(final String registrationLockPin
) {
320 this.registrationLockPin
= registrationLockPin
;
323 public String
getSignalingKey() {
327 public void setSignalingKey(final String signalingKey
) {
328 this.signalingKey
= signalingKey
;
331 public ProfileKey
getProfileKey() {
335 public void setProfileKey(final ProfileKey profileKey
) {
336 this.profileKey
= profileKey
;
339 public int getPreKeyIdOffset() {
340 return preKeyIdOffset
;
343 public int getNextSignedPreKeyId() {
344 return nextSignedPreKeyId
;
347 public boolean isRegistered() {
351 public void setRegistered(final boolean registered
) {
352 this.registered
= registered
;
355 public boolean isMultiDevice() {
356 return isMultiDevice
;
359 public void setMultiDevice(final boolean multiDevice
) {
360 isMultiDevice
= multiDevice
;