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
.JsonContactsStore
;
14 import org
.asamk
.signal
.storage
.groups
.JsonGroupStore
;
15 import org
.asamk
.signal
.storage
.protocol
.JsonSignalProtocolStore
;
16 import org
.asamk
.signal
.storage
.threads
.JsonThreadStore
;
17 import org
.asamk
.signal
.util
.IOUtils
;
18 import org
.asamk
.signal
.util
.Util
;
19 import org
.signal
.zkgroup
.InvalidInputException
;
20 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
21 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
22 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
23 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
24 import org
.whispersystems
.libsignal
.util
.Medium
;
25 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
26 import org
.whispersystems
.util
.Base64
;
29 import java
.io
.IOException
;
30 import java
.io
.RandomAccessFile
;
31 import java
.nio
.channels
.Channels
;
32 import java
.nio
.channels
.FileChannel
;
33 import java
.nio
.channels
.FileLock
;
34 import java
.util
.Collection
;
36 public class SignalAccount
{
38 private final ObjectMapper jsonProcessor
= new ObjectMapper();
39 private FileChannel fileChannel
;
40 private FileLock lock
;
41 private String username
;
42 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
43 private boolean isMultiDevice
= false;
44 private String password
;
45 private String registrationLockPin
;
46 private String signalingKey
;
47 private ProfileKey profileKey
;
48 private int preKeyIdOffset
;
49 private int nextSignedPreKeyId
;
51 private boolean registered
= false;
53 private JsonSignalProtocolStore signalProtocolStore
;
54 private JsonGroupStore groupStore
;
55 private JsonContactsStore contactStore
;
56 private JsonThreadStore threadStore
;
58 private SignalAccount() {
59 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
60 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
61 jsonProcessor
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
62 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
63 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
64 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
67 public static SignalAccount
load(String dataPath
, String username
) throws IOException
{
68 SignalAccount account
= new SignalAccount();
69 IOUtils
.createPrivateDirectories(dataPath
);
70 account
.openFileChannel(getFileName(dataPath
, username
));
75 public static SignalAccount
create(String dataPath
, String username
, IdentityKeyPair identityKey
, int registrationId
, ProfileKey profileKey
) throws IOException
{
76 IOUtils
.createPrivateDirectories(dataPath
);
78 SignalAccount account
= new SignalAccount();
79 account
.openFileChannel(getFileName(dataPath
, username
));
81 account
.username
= username
;
82 account
.profileKey
= profileKey
;
83 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
84 account
.groupStore
= new JsonGroupStore();
85 account
.threadStore
= new JsonThreadStore();
86 account
.contactStore
= new JsonContactsStore();
87 account
.registered
= false;
92 public static SignalAccount
createLinkedAccount(String dataPath
, String username
, String password
, int deviceId
, IdentityKeyPair identityKey
, int registrationId
, String signalingKey
, ProfileKey profileKey
) throws IOException
{
93 IOUtils
.createPrivateDirectories(dataPath
);
95 SignalAccount account
= new SignalAccount();
96 account
.openFileChannel(getFileName(dataPath
, username
));
98 account
.username
= username
;
99 account
.password
= password
;
100 account
.profileKey
= profileKey
;
101 account
.deviceId
= deviceId
;
102 account
.signalingKey
= signalingKey
;
103 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
104 account
.groupStore
= new JsonGroupStore();
105 account
.threadStore
= new JsonThreadStore();
106 account
.contactStore
= new JsonContactsStore();
107 account
.registered
= true;
108 account
.isMultiDevice
= true;
113 public static SignalAccount
createTemporaryAccount(IdentityKeyPair identityKey
, int registrationId
) {
114 SignalAccount account
= new SignalAccount();
116 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
117 account
.registered
= false;
122 public static String
getFileName(String dataPath
, String username
) {
123 return dataPath
+ "/" + username
;
126 public static boolean userExists(String dataPath
, String username
) {
127 if (username
== null) {
130 File f
= new File(getFileName(dataPath
, username
));
131 return !(!f
.exists() || f
.isDirectory());
134 private void load() throws IOException
{
136 synchronized (fileChannel
) {
137 fileChannel
.position(0);
138 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
141 JsonNode node
= rootNode
.get("deviceId");
143 deviceId
= node
.asInt();
145 if (rootNode
.has("isMultiDevice")) {
146 isMultiDevice
= Util
.getNotNullNode(rootNode
, "isMultiDevice").asBoolean();
148 username
= Util
.getNotNullNode(rootNode
, "username").asText();
149 password
= Util
.getNotNullNode(rootNode
, "password").asText();
150 JsonNode pinNode
= rootNode
.get("registrationLockPin");
151 registrationLockPin
= pinNode
== null || pinNode
.isNull() ?
null : pinNode
.asText();
152 if (rootNode
.has("signalingKey")) {
153 signalingKey
= Util
.getNotNullNode(rootNode
, "signalingKey").asText();
155 if (rootNode
.has("preKeyIdOffset")) {
156 preKeyIdOffset
= Util
.getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
160 if (rootNode
.has("nextSignedPreKeyId")) {
161 nextSignedPreKeyId
= Util
.getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
163 nextSignedPreKeyId
= 0;
165 if (rootNode
.has("profileKey")) {
167 profileKey
= new ProfileKey(Base64
.decode(Util
.getNotNullNode(rootNode
, "profileKey").asText()));
168 } catch (InvalidInputException e
) {
169 throw new IOException("Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes", e
);
173 signalProtocolStore
= jsonProcessor
.convertValue(Util
.getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
174 registered
= Util
.getNotNullNode(rootNode
, "registered").asBoolean();
175 JsonNode groupStoreNode
= rootNode
.get("groupStore");
176 if (groupStoreNode
!= null) {
177 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
179 if (groupStore
== null) {
180 groupStore
= new JsonGroupStore();
183 JsonNode contactStoreNode
= rootNode
.get("contactStore");
184 if (contactStoreNode
!= null) {
185 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
187 if (contactStore
== null) {
188 contactStore
= new JsonContactsStore();
190 JsonNode threadStoreNode
= rootNode
.get("threadStore");
191 if (threadStoreNode
!= null) {
192 threadStore
= jsonProcessor
.convertValue(threadStoreNode
, JsonThreadStore
.class);
194 if (threadStore
== null) {
195 threadStore
= new JsonThreadStore();
200 if (fileChannel
== null) {
203 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
204 rootNode
.put("username", username
)
205 .put("deviceId", deviceId
)
206 .put("isMultiDevice", isMultiDevice
)
207 .put("password", password
)
208 .put("registrationLockPin", registrationLockPin
)
209 .put("signalingKey", signalingKey
)
210 .put("preKeyIdOffset", preKeyIdOffset
)
211 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
212 .put("profileKey", Base64
.encodeBytes(profileKey
.serialize()))
213 .put("registered", registered
)
214 .putPOJO("axolotlStore", signalProtocolStore
)
215 .putPOJO("groupStore", groupStore
)
216 .putPOJO("contactStore", contactStore
)
217 .putPOJO("threadStore", threadStore
)
220 synchronized (fileChannel
) {
221 fileChannel
.position(0);
222 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
223 fileChannel
.truncate(fileChannel
.position());
224 fileChannel
.force(false);
226 } catch (Exception e
) {
227 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
231 private void openFileChannel(String fileName
) throws IOException
{
232 if (fileChannel
!= null) {
236 if (!new File(fileName
).exists()) {
237 IOUtils
.createPrivateFile(fileName
);
239 fileChannel
= new RandomAccessFile(new File(fileName
), "rw").getChannel();
240 lock
= fileChannel
.tryLock();
242 System
.err
.println("Config file is in use by another instance, waiting…");
243 lock
= fileChannel
.lock();
244 System
.err
.println("Config file lock acquired.");
248 public void addPreKeys(Collection
<PreKeyRecord
> records
) {
249 for (PreKeyRecord
record : records
) {
250 signalProtocolStore
.storePreKey(record.getId(), record);
252 preKeyIdOffset
= (preKeyIdOffset
+ records
.size()) % Medium
.MAX_VALUE
;
255 public void addSignedPreKey(SignedPreKeyRecord
record) {
256 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
257 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
260 public JsonSignalProtocolStore
getSignalProtocolStore() {
261 return signalProtocolStore
;
264 public JsonGroupStore
getGroupStore() {
268 public JsonContactsStore
getContactStore() {
272 public JsonThreadStore
getThreadStore() {
276 public String
getUsername() {
280 public SignalServiceAddress
getSelfAddress() {
281 return new SignalServiceAddress(null, username
);
284 public int getDeviceId() {
288 public String
getPassword() {
292 public void setPassword(final String password
) {
293 this.password
= password
;
296 public String
getRegistrationLockPin() {
297 return registrationLockPin
;
300 public String
getRegistrationLock() {
301 return null; // TODO implement KBS
304 public void setRegistrationLockPin(final String registrationLockPin
) {
305 this.registrationLockPin
= registrationLockPin
;
308 public String
getSignalingKey() {
312 public void setSignalingKey(final String signalingKey
) {
313 this.signalingKey
= signalingKey
;
316 public ProfileKey
getProfileKey() {
320 public void setProfileKey(final ProfileKey profileKey
) {
321 this.profileKey
= profileKey
;
324 public int getPreKeyIdOffset() {
325 return preKeyIdOffset
;
328 public int getNextSignedPreKeyId() {
329 return nextSignedPreKeyId
;
332 public boolean isRegistered() {
336 public void setRegistered(final boolean registered
) {
337 this.registered
= registered
;
340 public boolean isMultiDevice() {
341 return isMultiDevice
;
344 public void setMultiDevice(final boolean multiDevice
) {
345 isMultiDevice
= multiDevice
;