2 * Copyright (C) 2015 AsamK
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
;
19 import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
20 import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
21 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
22 import com
.fasterxml
.jackson
.core
.JsonParser
;
23 import com
.fasterxml
.jackson
.databind
.DeserializationFeature
;
24 import com
.fasterxml
.jackson
.databind
.JsonNode
;
25 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
26 import com
.fasterxml
.jackson
.databind
.SerializationFeature
;
27 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
28 import org
.apache
.http
.util
.TextUtils
;
29 import org
.asamk
.Signal
;
30 import org
.whispersystems
.libsignal
.*;
31 import org
.whispersystems
.libsignal
.ecc
.Curve
;
32 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
33 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
34 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
35 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
36 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
37 import org
.whispersystems
.libsignal
.util
.Medium
;
38 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
39 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
40 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
41 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
42 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
43 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
44 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
45 import org
.whispersystems
.signalservice
.api
.messages
.*;
46 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.*;
47 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
48 import org
.whispersystems
.signalservice
.api
.push
.TrustStore
;
49 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.*;
50 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
51 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
52 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
56 import java
.net
.URISyntaxException
;
57 import java
.net
.URLDecoder
;
58 import java
.net
.URLEncoder
;
59 import java
.nio
.channels
.Channels
;
60 import java
.nio
.channels
.FileChannel
;
61 import java
.nio
.channels
.FileLock
;
62 import java
.nio
.file
.Files
;
63 import java
.nio
.file
.Path
;
64 import java
.nio
.file
.Paths
;
65 import java
.nio
.file
.StandardCopyOption
;
66 import java
.nio
.file
.attribute
.PosixFilePermission
;
67 import java
.nio
.file
.attribute
.PosixFilePermissions
;
69 import java
.util
.concurrent
.TimeUnit
;
70 import java
.util
.concurrent
.TimeoutException
;
72 import static java
.nio
.file
.attribute
.PosixFilePermission
.*;
74 class Manager
implements Signal
{
75 private final static String URL
= "https://textsecure-service.whispersystems.org";
76 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
78 public final static String PROJECT_NAME
= Manager
.class.getPackage().getImplementationTitle();
79 public final static String PROJECT_VERSION
= Manager
.class.getPackage().getImplementationVersion();
80 private final static String USER_AGENT
= PROJECT_NAME
== null ?
null : PROJECT_NAME
+ " " + PROJECT_VERSION
;
82 private final static int PREKEY_MINIMUM_COUNT
= 20;
83 private static final int PREKEY_BATCH_SIZE
= 100;
85 private final String settingsPath
;
86 private final String dataPath
;
87 private final String attachmentsPath
;
88 private final String avatarsPath
;
90 private FileChannel fileChannel
;
91 private FileLock lock
;
93 private final ObjectMapper jsonProcessot
= new ObjectMapper();
94 private String username
;
95 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
96 private String password
;
97 private String signalingKey
;
98 private int preKeyIdOffset
;
99 private int nextSignedPreKeyId
;
101 private boolean registered
= false;
103 private JsonSignalProtocolStore signalProtocolStore
;
104 private SignalServiceAccountManager accountManager
;
105 private JsonGroupStore groupStore
;
106 private JsonContactsStore contactStore
;
108 public Manager(String username
, String settingsPath
) {
109 this.username
= username
;
110 this.settingsPath
= settingsPath
;
111 this.dataPath
= this.settingsPath
+ "/data";
112 this.attachmentsPath
= this.settingsPath
+ "/attachments";
113 this.avatarsPath
= this.settingsPath
+ "/avatars";
115 jsonProcessot
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
116 jsonProcessot
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
117 jsonProcessot
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
118 jsonProcessot
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
119 jsonProcessot
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
120 jsonProcessot
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
123 public String
getUsername() {
127 public int getDeviceId() {
131 public String
getFileName() {
132 return dataPath
+ "/" + username
;
135 private static void createPrivateDirectories(String path
) throws IOException
{
136 final Path file
= new File(path
).toPath();
138 Set
<PosixFilePermission
> perms
= EnumSet
.of(OWNER_READ
, OWNER_WRITE
, OWNER_EXECUTE
);
139 Files
.createDirectories(file
, PosixFilePermissions
.asFileAttribute(perms
));
140 } catch (UnsupportedOperationException e
) {
141 Files
.createDirectories(file
);
145 private static void createPrivateFile(String path
) throws IOException
{
146 final Path file
= new File(path
).toPath();
148 Set
<PosixFilePermission
> perms
= EnumSet
.of(OWNER_READ
, OWNER_WRITE
);
149 Files
.createFile(file
, PosixFilePermissions
.asFileAttribute(perms
));
150 } catch (UnsupportedOperationException e
) {
151 Files
.createFile(file
);
155 public boolean userExists() {
156 if (username
== null) {
159 File f
= new File(getFileName());
160 return !(!f
.exists() || f
.isDirectory());
163 public boolean userHasKeys() {
164 return signalProtocolStore
!= null;
167 private JsonNode
getNotNullNode(JsonNode parent
, String name
) throws InvalidObjectException
{
168 JsonNode node
= parent
.get(name
);
170 throw new InvalidObjectException(String
.format("Incorrect file format: expected parameter %s not found ", name
));
176 private void openFileChannel() throws IOException
{
177 if (fileChannel
!= null)
180 createPrivateDirectories(dataPath
);
181 if (!new File(getFileName()).exists()) {
182 createPrivateFile(getFileName());
184 fileChannel
= new RandomAccessFile(new File(getFileName()), "rw").getChannel();
185 lock
= fileChannel
.tryLock();
187 System
.err
.println("Config file is in use by another instance, waiting…");
188 lock
= fileChannel
.lock();
189 System
.err
.println("Config file lock acquired.");
193 public void load() throws IOException
, InvalidKeyException
{
195 JsonNode rootNode
= jsonProcessot
.readTree(Channels
.newInputStream(fileChannel
));
197 JsonNode node
= rootNode
.get("deviceId");
199 deviceId
= node
.asInt();
201 username
= getNotNullNode(rootNode
, "username").asText();
202 password
= getNotNullNode(rootNode
, "password").asText();
203 if (rootNode
.has("signalingKey")) {
204 signalingKey
= getNotNullNode(rootNode
, "signalingKey").asText();
206 if (rootNode
.has("preKeyIdOffset")) {
207 preKeyIdOffset
= getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
211 if (rootNode
.has("nextSignedPreKeyId")) {
212 nextSignedPreKeyId
= getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
214 nextSignedPreKeyId
= 0;
216 signalProtocolStore
= jsonProcessot
.convertValue(getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
217 registered
= getNotNullNode(rootNode
, "registered").asBoolean();
218 JsonNode groupStoreNode
= rootNode
.get("groupStore");
219 if (groupStoreNode
!= null) {
220 groupStore
= jsonProcessot
.convertValue(groupStoreNode
, JsonGroupStore
.class);
222 if (groupStore
== null) {
223 groupStore
= new JsonGroupStore();
225 // Copy group avatars that were previously stored in the attachments folder
226 // to the new avatar folder
227 if (groupStore
.groupsWithLegacyAvatarId
.size() > 0) {
228 for (GroupInfo g
: groupStore
.groupsWithLegacyAvatarId
) {
229 File avatarFile
= getGroupAvatarFile(g
.groupId
);
230 File attachmentFile
= getAttachmentFile(g
.getAvatarId());
231 if (!avatarFile
.exists() && attachmentFile
.exists()) {
233 createPrivateDirectories(avatarsPath
);
234 Files
.copy(attachmentFile
.toPath(), avatarFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
235 } catch (Exception e
) {
240 groupStore
.groupsWithLegacyAvatarId
.clear();
244 JsonNode contactStoreNode
= rootNode
.get("contactStore");
245 if (contactStoreNode
!= null) {
246 contactStore
= jsonProcessot
.convertValue(contactStoreNode
, JsonContactsStore
.class);
248 if (contactStore
== null) {
249 contactStore
= new JsonContactsStore();
252 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, deviceId
, USER_AGENT
);
254 if (registered
&& accountManager
.getPreKeysCount() < PREKEY_MINIMUM_COUNT
) {
258 } catch (AuthorizationFailedException e
) {
259 System
.err
.println("Authorization failed, was the number registered elsewhere?");
263 private void save() {
264 if (username
== null) {
267 ObjectNode rootNode
= jsonProcessot
.createObjectNode();
268 rootNode
.put("username", username
)
269 .put("deviceId", deviceId
)
270 .put("password", password
)
271 .put("signalingKey", signalingKey
)
272 .put("preKeyIdOffset", preKeyIdOffset
)
273 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
274 .put("registered", registered
)
275 .putPOJO("axolotlStore", signalProtocolStore
)
276 .putPOJO("groupStore", groupStore
)
277 .putPOJO("contactStore", contactStore
)
281 fileChannel
.position(0);
282 jsonProcessot
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
283 fileChannel
.truncate(fileChannel
.position());
284 fileChannel
.force(false);
285 } catch (Exception e
) {
286 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
290 public void createNewIdentity() {
291 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
292 int registrationId
= KeyHelper
.generateRegistrationId(false);
293 signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
294 groupStore
= new JsonGroupStore();
299 public boolean isRegistered() {
303 public void register(boolean voiceVerification
) throws IOException
{
304 password
= Util
.getSecret(18);
306 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, USER_AGENT
);
308 if (voiceVerification
)
309 accountManager
.requestVoiceVerificationCode();
311 accountManager
.requestSmsVerificationCode();
317 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
318 password
= Util
.getSecret(18);
320 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, USER_AGENT
);
321 String uuid
= accountManager
.getNewDeviceUuid();
325 return new URI("tsdevice:/?uuid=" + URLEncoder
.encode(uuid
, "utf-8") + "&pub_key=" + URLEncoder
.encode(Base64
.encodeBytesWithoutPadding(signalProtocolStore
.getIdentityKeyPair().getPublicKey().serialize()), "utf-8"));
326 } catch (URISyntaxException e
) {
332 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
333 signalingKey
= Util
.getSecret(52);
334 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(signalProtocolStore
.getIdentityKeyPair(), signalingKey
, false, true, signalProtocolStore
.getLocalRegistrationId(), deviceName
);
335 deviceId
= ret
.getDeviceId();
336 username
= ret
.getNumber();
337 // TODO do this check before actually registering
339 throw new UserAlreadyExists(username
, getFileName());
341 signalProtocolStore
= new JsonSignalProtocolStore(ret
.getIdentity(), signalProtocolStore
.getLocalRegistrationId());
347 requestSyncContacts();
352 public List
<DeviceInfo
> getLinkedDevices() throws IOException
{
353 return accountManager
.getDevices();
356 public void removeLinkedDevices(int deviceId
) throws IOException
{
357 accountManager
.removeDevice(deviceId
);
360 public static Map
<String
, String
> getQueryMap(String query
) {
361 String
[] params
= query
.split("&");
362 Map
<String
, String
> map
= new HashMap
<>();
363 for (String param
: params
) {
366 name
= URLDecoder
.decode(param
.split("=")[0], "utf-8");
367 } catch (UnsupportedEncodingException e
) {
372 value
= URLDecoder
.decode(param
.split("=")[1], "utf-8");
373 } catch (UnsupportedEncodingException e
) {
376 map
.put(name
, value
);
381 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
382 Map
<String
, String
> query
= getQueryMap(linkUri
.getRawQuery());
383 String deviceIdentifier
= query
.get("uuid");
384 String publicKeyEncoded
= query
.get("pub_key");
386 if (TextUtils
.isEmpty(deviceIdentifier
) || TextUtils
.isEmpty(publicKeyEncoded
)) {
387 throw new RuntimeException("Invalid device link uri");
390 ECPublicKey deviceKey
= Curve
.decodePoint(Base64
.decode(publicKeyEncoded
), 0);
392 addDevice(deviceIdentifier
, deviceKey
);
395 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
396 IdentityKeyPair identityKeyPair
= signalProtocolStore
.getIdentityKeyPair();
397 String verificationCode
= accountManager
.getNewDeviceVerificationCode();
399 accountManager
.addDevice(deviceIdentifier
, deviceKey
, identityKeyPair
, verificationCode
);
402 private List
<PreKeyRecord
> generatePreKeys() {
403 List
<PreKeyRecord
> records
= new LinkedList
<>();
405 for (int i
= 0; i
< PREKEY_BATCH_SIZE
; i
++) {
406 int preKeyId
= (preKeyIdOffset
+ i
) % Medium
.MAX_VALUE
;
407 ECKeyPair keyPair
= Curve
.generateKeyPair();
408 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
410 signalProtocolStore
.storePreKey(preKeyId
, record);
414 preKeyIdOffset
= (preKeyIdOffset
+ PREKEY_BATCH_SIZE
+ 1) % Medium
.MAX_VALUE
;
420 private PreKeyRecord
getOrGenerateLastResortPreKey() {
421 if (signalProtocolStore
.containsPreKey(Medium
.MAX_VALUE
)) {
423 return signalProtocolStore
.loadPreKey(Medium
.MAX_VALUE
);
424 } catch (InvalidKeyIdException e
) {
425 signalProtocolStore
.removePreKey(Medium
.MAX_VALUE
);
429 ECKeyPair keyPair
= Curve
.generateKeyPair();
430 PreKeyRecord
record = new PreKeyRecord(Medium
.MAX_VALUE
, keyPair
);
432 signalProtocolStore
.storePreKey(Medium
.MAX_VALUE
, record);
438 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
440 ECKeyPair keyPair
= Curve
.generateKeyPair();
441 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
442 SignedPreKeyRecord
record = new SignedPreKeyRecord(nextSignedPreKeyId
, System
.currentTimeMillis(), keyPair
, signature
);
444 signalProtocolStore
.storeSignedPreKey(nextSignedPreKeyId
, record);
445 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
449 } catch (InvalidKeyException e
) {
450 throw new AssertionError(e
);
454 public void verifyAccount(String verificationCode
) throws IOException
{
455 verificationCode
= verificationCode
.replace("-", "");
456 signalingKey
= Util
.getSecret(52);
457 accountManager
.verifyAccountWithCode(verificationCode
, signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, true);
459 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
466 private void refreshPreKeys() throws IOException
{
467 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
468 PreKeyRecord lastResortKey
= getOrGenerateLastResortPreKey();
469 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(signalProtocolStore
.getIdentityKeyPair());
471 accountManager
.setPreKeys(signalProtocolStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
475 private static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
476 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
477 if (attachments
!= null) {
478 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
479 for (String attachment
: attachments
) {
481 SignalServiceAttachments
.add(createAttachment(new File(attachment
)));
482 } catch (IOException e
) {
483 throw new AttachmentInvalidException(attachment
, e
);
487 return SignalServiceAttachments
;
490 private static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
491 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
492 final long attachmentSize
= attachmentFile
.length();
493 String mime
= Files
.probeContentType(attachmentFile
.toPath());
495 mime
= "application/octet-stream";
497 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, null);
500 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(byte[] groupId
) throws IOException
{
501 File file
= getGroupAvatarFile(groupId
);
502 if (!file
.exists()) {
503 return Optional
.absent();
506 return Optional
.of(createAttachment(file
));
509 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(String number
) throws IOException
{
510 File file
= getContactAvatarFile(number
);
511 if (!file
.exists()) {
512 return Optional
.absent();
515 return Optional
.of(createAttachment(file
));
519 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
521 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
522 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
523 if (attachments
!= null) {
524 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
526 if (groupId
!= null) {
527 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
530 messageBuilder
.asGroupMessage(group
);
532 SignalServiceDataMessage message
= messageBuilder
.build();
534 GroupInfo g
= groupStore
.getGroup(groupId
);
536 throw new GroupNotFoundException(groupId
);
539 // Don't send group message to ourself
540 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
541 membersSend
.remove(this.username
);
542 sendMessage(message
, membersSend
);
545 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
{
546 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
550 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
551 .asGroupMessage(group
)
554 final GroupInfo g
= groupStore
.getGroup(groupId
);
556 throw new GroupNotFoundException(groupId
);
558 g
.members
.remove(this.username
);
559 groupStore
.updateGroup(g
);
561 sendMessage(message
, g
.members
);
564 public byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
566 if (groupId
== null) {
568 g
= new GroupInfo(Util
.getSecretBytes(16));
569 g
.members
.add(username
);
571 g
= groupStore
.getGroup(groupId
);
573 throw new GroupNotFoundException(groupId
);
581 if (members
!= null) {
582 for (String member
: members
) {
584 g
.members
.add(canonicalizeNumber(member
));
585 } catch (InvalidNumberException e
) {
586 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
587 System
.err
.println("Aborting…");
593 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
596 .withMembers(new ArrayList
<>(g
.members
));
598 File aFile
= getGroupAvatarFile(g
.groupId
);
599 if (avatarFile
!= null) {
600 createPrivateDirectories(avatarsPath
);
601 Files
.copy(Paths
.get(avatarFile
), aFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
603 if (aFile
.exists()) {
605 group
.withAvatar(createAttachment(aFile
));
606 } catch (IOException e
) {
607 throw new AttachmentInvalidException(avatarFile
, e
);
611 groupStore
.updateGroup(g
);
613 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
614 .asGroupMessage(group
.build())
617 // Don't send group message to ourself
618 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
619 membersSend
.remove(this.username
);
620 sendMessage(message
, membersSend
);
625 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
626 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
{
627 List
<String
> recipients
= new ArrayList
<>(1);
628 recipients
.add(recipient
);
629 sendMessage(message
, attachments
, recipients
);
633 public void sendMessage(String messageText
, List
<String
> attachments
,
634 List
<String
> recipients
)
635 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
{
636 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
637 if (attachments
!= null) {
638 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
640 SignalServiceDataMessage message
= messageBuilder
.build();
642 sendMessage(message
, recipients
);
646 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
{
647 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
648 .asEndSessionMessage()
651 sendMessage(message
, recipients
);
654 private void requestSyncGroups() throws IOException
{
655 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
).build();
656 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
658 sendMessage(message
);
659 } catch (UntrustedIdentityException e
) {
664 private void requestSyncContacts() throws IOException
{
665 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
).build();
666 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
668 sendMessage(message
);
669 } catch (UntrustedIdentityException e
) {
674 private void sendMessage(SignalServiceSyncMessage message
)
675 throws IOException
, UntrustedIdentityException
{
676 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(URL
, TRUST_STORE
, username
, password
,
677 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.<SignalServiceMessageSender
.EventListener
>absent());
679 messageSender
.sendMessage(message
);
680 } catch (UntrustedIdentityException e
) {
681 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
686 private void sendMessage(SignalServiceDataMessage message
, Collection
<String
> recipients
)
687 throws EncapsulatedExceptions
, IOException
{
688 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
689 for (String recipient
: recipients
) {
691 recipientsTS
.add(getPushAddress(recipient
));
692 } catch (InvalidNumberException e
) {
693 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
694 System
.err
.println("Aborting sending.");
701 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(URL
, TRUST_STORE
, username
, password
,
702 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.<SignalServiceMessageSender
.EventListener
>absent());
704 if (message
.getGroupInfo().isPresent()) {
706 messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), message
);
707 } catch (EncapsulatedExceptions encapsulatedExceptions
) {
708 for (UntrustedIdentityException e
: encapsulatedExceptions
.getUntrustedIdentityExceptions()) {
709 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
713 // Send to all individually, so sync messages are sent correctly
714 List
<UntrustedIdentityException
> untrustedIdentities
= new LinkedList
<>();
715 List
<UnregisteredUserException
> unregisteredUsers
= new LinkedList
<>();
716 List
<NetworkFailureException
> networkExceptions
= new LinkedList
<>();
717 for (SignalServiceAddress address
: recipientsTS
) {
719 messageSender
.sendMessage(address
, message
);
720 } catch (UntrustedIdentityException e
) {
721 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
722 untrustedIdentities
.add(e
);
723 } catch (UnregisteredUserException e
) {
724 unregisteredUsers
.add(e
);
725 } catch (PushNetworkException e
) {
726 networkExceptions
.add(new NetworkFailureException(address
.getNumber(), e
));
729 if (!untrustedIdentities
.isEmpty() || !unregisteredUsers
.isEmpty() || !networkExceptions
.isEmpty()) {
730 throw new EncapsulatedExceptions(untrustedIdentities
, unregisteredUsers
, networkExceptions
);
734 if (message
.isEndSession()) {
735 for (SignalServiceAddress recipient
: recipientsTS
) {
736 handleEndSession(recipient
.getNumber());
743 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws NoSessionException
, LegacyMessageException
, InvalidVersionException
, InvalidMessageException
, DuplicateMessageException
, InvalidKeyException
, InvalidKeyIdException
, org
.whispersystems
.libsignal
.UntrustedIdentityException
{
744 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), signalProtocolStore
);
746 return cipher
.decrypt(envelope
);
747 } catch (org
.whispersystems
.libsignal
.UntrustedIdentityException e
) {
748 // TODO temporarily store message, until user has accepted the key
749 signalProtocolStore
.saveIdentity(e
.getName(), e
.getUntrustedIdentity(), TrustLevel
.UNTRUSTED
);
751 } catch (Exception e
) {
756 private void handleEndSession(String source
) {
757 signalProtocolStore
.deleteAllSessions(source
);
760 public interface ReceiveMessageHandler
{
761 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
);
764 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
) {
765 if (message
.getGroupInfo().isPresent()) {
766 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
767 switch (groupInfo
.getType()) {
770 group
= groupStore
.getGroup(groupInfo
.getGroupId());
772 group
= new GroupInfo(groupInfo
.getGroupId());
775 if (groupInfo
.getAvatar().isPresent()) {
776 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
777 if (avatar
.isPointer()) {
779 retrieveGroupAvatarAttachment(avatar
.asPointer(), group
.groupId
);
780 } catch (IOException
| InvalidMessageException e
) {
781 System
.err
.println("Failed to retrieve group avatar (" + avatar
.asPointer().getId() + "): " + e
.getMessage());
786 if (groupInfo
.getName().isPresent()) {
787 group
.name
= groupInfo
.getName().get();
790 if (groupInfo
.getMembers().isPresent()) {
791 group
.members
.addAll(groupInfo
.getMembers().get());
794 groupStore
.updateGroup(group
);
799 group
= groupStore
.getGroup(groupInfo
.getGroupId());
801 group
.members
.remove(source
);
802 groupStore
.updateGroup(group
);
807 if (message
.isEndSession()) {
808 handleEndSession(isSync ? destination
: source
);
810 if (message
.getAttachments().isPresent()) {
811 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
812 if (attachment
.isPointer()) {
814 retrieveAttachment(attachment
.asPointer());
815 } catch (IOException
| InvalidMessageException e
) {
816 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
823 public void receiveMessages(int timeoutSeconds
, boolean returnOnTimeout
, ReceiveMessageHandler handler
) throws IOException
{
824 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
825 SignalServiceMessagePipe messagePipe
= null;
828 messagePipe
= messageReceiver
.createMessagePipe();
831 SignalServiceEnvelope envelope
;
832 SignalServiceContent content
= null;
834 envelope
= messagePipe
.read(timeoutSeconds
, TimeUnit
.SECONDS
);
835 if (!envelope
.isReceipt()) {
838 content
= decryptMessage(envelope
);
839 } catch (Exception e
) {
841 // TODO pass exception to handler instead
844 if (content
!= null) {
845 if (content
.getDataMessage().isPresent()) {
846 SignalServiceDataMessage message
= content
.getDataMessage().get();
847 handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
);
849 if (content
.getSyncMessage().isPresent()) {
850 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
851 if (syncMessage
.getSent().isPresent()) {
852 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
853 handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get());
855 if (syncMessage
.getRequest().isPresent()) {
856 RequestMessage rm
= syncMessage
.getRequest().get();
857 if (rm
.isContactsRequest()) {
860 } catch (UntrustedIdentityException e
) {
864 if (rm
.isGroupsRequest()) {
867 } catch (UntrustedIdentityException e
) {
872 if (syncMessage
.getGroups().isPresent()) {
874 DeviceGroupsInputStream s
= new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage
.getGroups().get().asPointer()));
876 while ((g
= s
.read()) != null) {
877 GroupInfo syncGroup
= groupStore
.getGroup(g
.getId());
878 if (syncGroup
== null) {
879 syncGroup
= new GroupInfo(g
.getId());
881 if (g
.getName().isPresent()) {
882 syncGroup
.name
= g
.getName().get();
884 syncGroup
.members
.addAll(g
.getMembers());
885 syncGroup
.active
= g
.isActive();
887 if (g
.getAvatar().isPresent()) {
888 retrieveGroupAvatarAttachment(g
.getAvatar().get(), syncGroup
.groupId
);
890 groupStore
.updateGroup(syncGroup
);
892 } catch (Exception e
) {
896 if (syncMessage
.getContacts().isPresent()) {
898 DeviceContactsInputStream s
= new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage
.getContacts().get().asPointer()));
900 while ((c
= s
.read()) != null) {
901 ContactInfo contact
= new ContactInfo();
902 contact
.number
= c
.getNumber();
903 if (c
.getName().isPresent()) {
904 contact
.name
= c
.getName().get();
906 contactStore
.updateContact(contact
);
908 if (c
.getAvatar().isPresent()) {
909 retrieveContactAvatarAttachment(c
.getAvatar().get(), contact
.number
);
912 } catch (Exception e
) {
920 handler
.handleMessage(envelope
, content
);
921 } catch (TimeoutException e
) {
924 } catch (InvalidVersionException e
) {
925 System
.err
.println("Ignoring error: " + e
.getMessage());
929 if (messagePipe
!= null)
930 messagePipe
.shutdown();
934 public File
getContactAvatarFile(String number
) {
935 return new File(avatarsPath
, "contact-" + number
);
938 private File
retrieveContactAvatarAttachment(SignalServiceAttachment attachment
, String number
) throws IOException
, InvalidMessageException
{
939 createPrivateDirectories(avatarsPath
);
940 if (attachment
.isPointer()) {
941 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
942 return retrieveAttachment(pointer
, getContactAvatarFile(number
), false);
944 SignalServiceAttachmentStream stream
= attachment
.asStream();
945 return retrieveAttachment(stream
, getContactAvatarFile(number
));
949 public File
getGroupAvatarFile(byte[] groupId
) {
950 return new File(avatarsPath
, "group-" + Base64
.encodeBytes(groupId
).replace("/", "_"));
953 private File
retrieveGroupAvatarAttachment(SignalServiceAttachment attachment
, byte[] groupId
) throws IOException
, InvalidMessageException
{
954 createPrivateDirectories(avatarsPath
);
955 if (attachment
.isPointer()) {
956 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
957 return retrieveAttachment(pointer
, getGroupAvatarFile(groupId
), false);
959 SignalServiceAttachmentStream stream
= attachment
.asStream();
960 return retrieveAttachment(stream
, getGroupAvatarFile(groupId
));
964 public File
getAttachmentFile(long attachmentId
) {
965 return new File(attachmentsPath
, attachmentId
+ "");
968 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
969 createPrivateDirectories(attachmentsPath
);
970 return retrieveAttachment(pointer
, getAttachmentFile(pointer
.getId()), true);
973 private File
retrieveAttachment(SignalServiceAttachmentStream stream
, File outputFile
) throws IOException
, InvalidMessageException
{
974 InputStream input
= stream
.getInputStream();
976 OutputStream output
= null;
978 output
= new FileOutputStream(outputFile
);
979 byte[] buffer
= new byte[4096];
982 while ((read
= input
.read(buffer
)) != -1) {
983 output
.write(buffer
, 0, read
);
985 } catch (FileNotFoundException e
) {
989 if (output
!= null) {
996 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
, File outputFile
, boolean storePreview
) throws IOException
, InvalidMessageException
{
997 if (storePreview
&& pointer
.getPreview().isPresent()) {
998 File previewFile
= new File(outputFile
+ ".preview");
999 OutputStream output
= null;
1001 output
= new FileOutputStream(previewFile
);
1002 byte[] preview
= pointer
.getPreview().get();
1003 output
.write(preview
, 0, preview
.length
);
1004 } catch (FileNotFoundException e
) {
1005 e
.printStackTrace();
1008 if (output
!= null) {
1014 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1016 File tmpFile
= File
.createTempFile("ts_attach_" + pointer
.getId(), ".tmp");
1017 InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
);
1019 OutputStream output
= null;
1021 output
= new FileOutputStream(outputFile
);
1022 byte[] buffer
= new byte[4096];
1025 while ((read
= input
.read(buffer
)) != -1) {
1026 output
.write(buffer
, 0, read
);
1028 } catch (FileNotFoundException e
) {
1029 e
.printStackTrace();
1032 if (output
!= null) {
1035 if (!tmpFile
.delete()) {
1036 System
.err
.println("Failed to delete temp file: " + tmpFile
);
1042 private InputStream
retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
1043 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1044 File file
= File
.createTempFile("ts_tmp", "tmp");
1045 file
.deleteOnExit();
1047 return messageReceiver
.retrieveAttachment(pointer
, file
);
1050 private String
canonicalizeNumber(String number
) throws InvalidNumberException
{
1051 String localNumber
= username
;
1052 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
1055 private SignalServiceAddress
getPushAddress(String number
) throws InvalidNumberException
{
1056 String e164number
= canonicalizeNumber(number
);
1057 return new SignalServiceAddress(e164number
);
1061 public boolean isRemote() {
1065 private void sendGroups() throws IOException
, UntrustedIdentityException
{
1066 File groupsFile
= File
.createTempFile("multidevice-group-update", ".tmp");
1069 DeviceGroupsOutputStream out
= new DeviceGroupsOutputStream(new FileOutputStream(groupsFile
));
1071 for (GroupInfo
record : groupStore
.getGroups()) {
1072 out
.write(new DeviceGroup(record.groupId
, Optional
.fromNullable(record.name
),
1073 new ArrayList
<>(record.members
), createGroupAvatarAttachment(record.groupId
),
1080 if (groupsFile
.exists() && groupsFile
.length() > 0) {
1081 FileInputStream contactsFileStream
= new FileInputStream(groupsFile
);
1082 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1083 .withStream(contactsFileStream
)
1084 .withContentType("application/octet-stream")
1085 .withLength(groupsFile
.length())
1088 sendMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
1091 groupsFile
.delete();
1095 private void sendContacts() throws IOException
, UntrustedIdentityException
{
1096 File contactsFile
= File
.createTempFile("multidevice-contact-update", ".tmp");
1099 DeviceContactsOutputStream out
= new DeviceContactsOutputStream(new FileOutputStream(contactsFile
));
1101 for (ContactInfo
record : contactStore
.getContacts()) {
1102 out
.write(new DeviceContact(record.number
, Optional
.fromNullable(record.name
),
1103 createContactAvatarAttachment(record.number
)));
1109 if (contactsFile
.exists() && contactsFile
.length() > 0) {
1110 FileInputStream contactsFileStream
= new FileInputStream(contactsFile
);
1111 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1112 .withStream(contactsFileStream
)
1113 .withContentType("application/octet-stream")
1114 .withLength(contactsFile
.length())
1117 sendMessage(SignalServiceSyncMessage
.forContacts(attachmentStream
));
1120 contactsFile
.delete();
1124 public ContactInfo
getContact(String number
) {
1125 return contactStore
.getContact(number
);
1128 public GroupInfo
getGroup(byte[] groupId
) {
1129 return groupStore
.getGroup(groupId
);
1132 public Map
<String
, List
<JsonIdentityKeyStore
.Identity
>> getIdentities() {
1133 return signalProtocolStore
.getIdentities();
1136 public List
<JsonIdentityKeyStore
.Identity
> getIdentities(String number
) {
1137 return signalProtocolStore
.getIdentities(number
);
1141 * Trust this the identity with this fingerprint
1143 * @param name username of the identity
1144 * @param fingerprint Fingerprint
1146 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) {
1147 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1151 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1152 if (!Arrays
.equals(id
.identityKey
.serialize(), fingerprint
)) {
1156 signalProtocolStore
.saveIdentity(name
, id
.identityKey
, TrustLevel
.TRUSTED_VERIFIED
);
1164 * Trust all keys of this identity without verification
1166 * @param name username of the identity
1168 public boolean trustIdentityAllKeys(String name
) {
1169 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1173 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1174 if (id
.trustLevel
== TrustLevel
.UNTRUSTED
) {
1175 signalProtocolStore
.saveIdentity(name
, id
.identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
);