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
, UUID uuid
, 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
;
103 account
.password
= password
;
104 account
.profileKey
= profileKey
;
105 account
.deviceId
= deviceId
;
106 account
.signalingKey
= signalingKey
;
107 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
108 account
.groupStore
= new JsonGroupStore();
109 account
.contactStore
= new JsonContactsStore();
110 account
.registered
= true;
111 account
.isMultiDevice
= true;
116 public static SignalAccount
createTemporaryAccount(IdentityKeyPair identityKey
, int registrationId
) {
117 SignalAccount account
= new SignalAccount();
119 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
120 account
.registered
= false;
125 public static String
getFileName(String dataPath
, String username
) {
126 return dataPath
+ "/" + username
;
129 public static boolean userExists(String dataPath
, String username
) {
130 if (username
== null) {
133 File f
= new File(getFileName(dataPath
, username
));
134 return !(!f
.exists() || f
.isDirectory());
137 private void load() throws IOException
{
139 synchronized (fileChannel
) {
140 fileChannel
.position(0);
141 rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
144 JsonNode uuidNode
= rootNode
.get("uuid");
145 if (uuidNode
!= null && !uuidNode
.isNull()) {
147 uuid
= UUID
.fromString(uuidNode
.asText());
148 } catch (IllegalArgumentException e
) {
149 throw new IOException("Config file contains an invalid uuid, needs to be a valid UUID", e
);
152 JsonNode node
= rootNode
.get("deviceId");
154 deviceId
= node
.asInt();
156 if (rootNode
.has("isMultiDevice")) {
157 isMultiDevice
= Util
.getNotNullNode(rootNode
, "isMultiDevice").asBoolean();
159 username
= Util
.getNotNullNode(rootNode
, "username").asText();
160 password
= Util
.getNotNullNode(rootNode
, "password").asText();
161 JsonNode pinNode
= rootNode
.get("registrationLockPin");
162 registrationLockPin
= pinNode
== null || pinNode
.isNull() ?
null : pinNode
.asText();
163 if (rootNode
.has("signalingKey")) {
164 signalingKey
= Util
.getNotNullNode(rootNode
, "signalingKey").asText();
166 if (rootNode
.has("preKeyIdOffset")) {
167 preKeyIdOffset
= Util
.getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
171 if (rootNode
.has("nextSignedPreKeyId")) {
172 nextSignedPreKeyId
= Util
.getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
174 nextSignedPreKeyId
= 0;
176 if (rootNode
.has("profileKey")) {
178 profileKey
= new ProfileKey(Base64
.decode(Util
.getNotNullNode(rootNode
, "profileKey").asText()));
179 } catch (InvalidInputException e
) {
180 throw new IOException("Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes", e
);
184 signalProtocolStore
= jsonProcessor
.convertValue(Util
.getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
185 registered
= Util
.getNotNullNode(rootNode
, "registered").asBoolean();
186 JsonNode groupStoreNode
= rootNode
.get("groupStore");
187 if (groupStoreNode
!= null) {
188 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
190 if (groupStore
== null) {
191 groupStore
= new JsonGroupStore();
194 JsonNode contactStoreNode
= rootNode
.get("contactStore");
195 if (contactStoreNode
!= null) {
196 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
198 if (contactStore
== null) {
199 contactStore
= new JsonContactsStore();
201 JsonNode threadStoreNode
= rootNode
.get("threadStore");
202 if (threadStoreNode
!= null) {
203 LegacyJsonThreadStore threadStore
= jsonProcessor
.convertValue(threadStoreNode
, LegacyJsonThreadStore
.class);
204 // Migrate thread info to group and contact store
205 for (ThreadInfo thread
: threadStore
.getThreads()) {
206 if (thread
.id
== null || thread
.id
.isEmpty()) {
210 ContactInfo contactInfo
= contactStore
.getContact(new SignalServiceAddress(null, thread
.id
));
211 if (contactInfo
!= null) {
212 contactInfo
.messageExpirationTime
= thread
.messageExpirationTime
;
213 contactStore
.updateContact(contactInfo
);
215 GroupInfo groupInfo
= groupStore
.getGroup(Base64
.decode(thread
.id
));
216 if (groupInfo
!= null) {
217 groupInfo
.messageExpirationTime
= thread
.messageExpirationTime
;
218 groupStore
.updateGroup(groupInfo
);
221 } catch (Exception ignored
) {
228 if (fileChannel
== null) {
231 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
232 rootNode
.put("username", username
)
233 .put("uuid", uuid
== null ?
null : uuid
.toString())
234 .put("deviceId", deviceId
)
235 .put("isMultiDevice", isMultiDevice
)
236 .put("password", password
)
237 .put("registrationLockPin", registrationLockPin
)
238 .put("signalingKey", signalingKey
)
239 .put("preKeyIdOffset", preKeyIdOffset
)
240 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
241 .put("profileKey", Base64
.encodeBytes(profileKey
.serialize()))
242 .put("registered", registered
)
243 .putPOJO("axolotlStore", signalProtocolStore
)
244 .putPOJO("groupStore", groupStore
)
245 .putPOJO("contactStore", contactStore
)
248 synchronized (fileChannel
) {
249 fileChannel
.position(0);
250 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
251 fileChannel
.truncate(fileChannel
.position());
252 fileChannel
.force(false);
254 } catch (Exception e
) {
255 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
259 private void openFileChannel(String fileName
) throws IOException
{
260 if (fileChannel
!= null) {
264 if (!new File(fileName
).exists()) {
265 IOUtils
.createPrivateFile(fileName
);
267 fileChannel
= new RandomAccessFile(new File(fileName
), "rw").getChannel();
268 lock
= fileChannel
.tryLock();
270 System
.err
.println("Config file is in use by another instance, waiting…");
271 lock
= fileChannel
.lock();
272 System
.err
.println("Config file lock acquired.");
276 public void addPreKeys(Collection
<PreKeyRecord
> records
) {
277 for (PreKeyRecord
record : records
) {
278 signalProtocolStore
.storePreKey(record.getId(), record);
280 preKeyIdOffset
= (preKeyIdOffset
+ records
.size()) % Medium
.MAX_VALUE
;
283 public void addSignedPreKey(SignedPreKeyRecord
record) {
284 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
285 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
288 public JsonSignalProtocolStore
getSignalProtocolStore() {
289 return signalProtocolStore
;
292 public JsonGroupStore
getGroupStore() {
296 public JsonContactsStore
getContactStore() {
300 public String
getUsername() {
304 public UUID
getUuid() {
308 public void setUuid(final UUID uuid
) {
312 public SignalServiceAddress
getSelfAddress() {
313 return new SignalServiceAddress(uuid
, username
);
316 public int getDeviceId() {
320 public String
getPassword() {
324 public void setPassword(final String password
) {
325 this.password
= password
;
328 public String
getRegistrationLockPin() {
329 return registrationLockPin
;
332 public String
getRegistrationLock() {
333 return null; // TODO implement KBS
336 public void setRegistrationLockPin(final String registrationLockPin
) {
337 this.registrationLockPin
= registrationLockPin
;
340 public String
getSignalingKey() {
344 public void setSignalingKey(final String signalingKey
) {
345 this.signalingKey
= signalingKey
;
348 public ProfileKey
getProfileKey() {
352 public void setProfileKey(final ProfileKey profileKey
) {
353 this.profileKey
= profileKey
;
356 public int getPreKeyIdOffset() {
357 return preKeyIdOffset
;
360 public int getNextSignedPreKeyId() {
361 return nextSignedPreKeyId
;
364 public boolean isRegistered() {
368 public void setRegistered(final boolean registered
) {
369 this.registered
= registered
;
372 public boolean isMultiDevice() {
373 return isMultiDevice
;
376 public void setMultiDevice(final boolean multiDevice
) {
377 isMultiDevice
= multiDevice
;