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
;
35 import java
.util
.UUID
;
37 public class SignalAccount
{
39 private final ObjectMapper jsonProcessor
= new ObjectMapper();
40 private FileChannel fileChannel
;
41 private FileLock lock
;
42 private String username
;
44 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
45 private boolean isMultiDevice
= false;
46 private String password
;
47 private String registrationLockPin
;
48 private String signalingKey
;
49 private ProfileKey profileKey
;
50 private int preKeyIdOffset
;
51 private int nextSignedPreKeyId
;
53 private boolean registered
= false;
55 private JsonSignalProtocolStore signalProtocolStore
;
56 private JsonGroupStore groupStore
;
57 private JsonContactsStore contactStore
;
58 private JsonThreadStore threadStore
;
60 private SignalAccount() {
61 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
62 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
63 jsonProcessor
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
64 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
65 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
66 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
69 public static SignalAccount
load(String dataPath
, String username
) throws IOException
{
70 SignalAccount account
= new SignalAccount();
71 IOUtils
.createPrivateDirectories(dataPath
);
72 account
.openFileChannel(getFileName(dataPath
, username
));
77 public static SignalAccount
create(String dataPath
, String username
, IdentityKeyPair identityKey
, int registrationId
, ProfileKey profileKey
) throws IOException
{
78 IOUtils
.createPrivateDirectories(dataPath
);
80 SignalAccount account
= new SignalAccount();
81 account
.openFileChannel(getFileName(dataPath
, username
));
83 account
.username
= username
;
84 account
.profileKey
= profileKey
;
85 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
86 account
.groupStore
= new JsonGroupStore();
87 account
.threadStore
= new JsonThreadStore();
88 account
.contactStore
= new JsonContactsStore();
89 account
.registered
= false;
94 public static SignalAccount
createLinkedAccount(String dataPath
, String username
, String password
, int deviceId
, IdentityKeyPair identityKey
, int registrationId
, String signalingKey
, ProfileKey profileKey
) throws IOException
{
95 IOUtils
.createPrivateDirectories(dataPath
);
97 SignalAccount account
= new SignalAccount();
98 account
.openFileChannel(getFileName(dataPath
, username
));
100 account
.username
= username
;
101 account
.password
= password
;
102 account
.profileKey
= profileKey
;
103 account
.deviceId
= deviceId
;
104 account
.signalingKey
= signalingKey
;
105 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
106 account
.groupStore
= new JsonGroupStore();
107 account
.threadStore
= new JsonThreadStore();
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 threadStore
= jsonProcessor
.convertValue(threadStoreNode
, JsonThreadStore
.class);
196 if (threadStore
== null) {
197 threadStore
= new JsonThreadStore();
202 if (fileChannel
== null) {
205 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
206 rootNode
.put("username", username
)
207 .put("deviceId", deviceId
)
208 .put("isMultiDevice", isMultiDevice
)
209 .put("password", password
)
210 .put("registrationLockPin", registrationLockPin
)
211 .put("signalingKey", signalingKey
)
212 .put("preKeyIdOffset", preKeyIdOffset
)
213 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
214 .put("profileKey", Base64
.encodeBytes(profileKey
.serialize()))
215 .put("registered", registered
)
216 .putPOJO("axolotlStore", signalProtocolStore
)
217 .putPOJO("groupStore", groupStore
)
218 .putPOJO("contactStore", contactStore
)
219 .putPOJO("threadStore", threadStore
)
222 synchronized (fileChannel
) {
223 fileChannel
.position(0);
224 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
225 fileChannel
.truncate(fileChannel
.position());
226 fileChannel
.force(false);
228 } catch (Exception e
) {
229 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
233 private void openFileChannel(String fileName
) throws IOException
{
234 if (fileChannel
!= null) {
238 if (!new File(fileName
).exists()) {
239 IOUtils
.createPrivateFile(fileName
);
241 fileChannel
= new RandomAccessFile(new File(fileName
), "rw").getChannel();
242 lock
= fileChannel
.tryLock();
244 System
.err
.println("Config file is in use by another instance, waiting…");
245 lock
= fileChannel
.lock();
246 System
.err
.println("Config file lock acquired.");
250 public void addPreKeys(Collection
<PreKeyRecord
> records
) {
251 for (PreKeyRecord
record : records
) {
252 signalProtocolStore
.storePreKey(record.getId(), record);
254 preKeyIdOffset
= (preKeyIdOffset
+ records
.size()) % Medium
.MAX_VALUE
;
257 public void addSignedPreKey(SignedPreKeyRecord
record) {
258 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
259 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
262 public JsonSignalProtocolStore
getSignalProtocolStore() {
263 return signalProtocolStore
;
266 public JsonGroupStore
getGroupStore() {
270 public JsonContactsStore
getContactStore() {
274 public JsonThreadStore
getThreadStore() {
278 public String
getUsername() {
282 public UUID
getUuid() {
286 public SignalServiceAddress
getSelfAddress() {
287 return new SignalServiceAddress(uuid
, username
);
290 public int getDeviceId() {
294 public String
getPassword() {
298 public void setPassword(final String password
) {
299 this.password
= password
;
302 public String
getRegistrationLockPin() {
303 return registrationLockPin
;
306 public String
getRegistrationLock() {
307 return null; // TODO implement KBS
310 public void setRegistrationLockPin(final String registrationLockPin
) {
311 this.registrationLockPin
= registrationLockPin
;
314 public String
getSignalingKey() {
318 public void setSignalingKey(final String signalingKey
) {
319 this.signalingKey
= signalingKey
;
322 public ProfileKey
getProfileKey() {
326 public void setProfileKey(final ProfileKey profileKey
) {
327 this.profileKey
= profileKey
;
330 public int getPreKeyIdOffset() {
331 return preKeyIdOffset
;
334 public int getNextSignedPreKeyId() {
335 return nextSignedPreKeyId
;
338 public boolean isRegistered() {
342 public void setRegistered(final boolean registered
) {
343 this.registered
= registered
;
346 public boolean isMultiDevice() {
347 return isMultiDevice
;
350 public void setMultiDevice(final boolean multiDevice
) {
351 isMultiDevice
= multiDevice
;