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
.fingerprint
.Fingerprint
;
35 import org
.whispersystems
.libsignal
.fingerprint
.NumericFingerprintGenerator
;
36 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
37 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
38 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
39 import org
.whispersystems
.libsignal
.util
.Medium
;
40 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
41 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
42 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
43 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
44 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
45 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
46 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
47 import org
.whispersystems
.signalservice
.api
.messages
.*;
48 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.*;
49 import org
.whispersystems
.signalservice
.api
.push
.ContactTokenDetails
;
50 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
51 import org
.whispersystems
.signalservice
.api
.push
.TrustStore
;
52 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.*;
53 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
54 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
55 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
56 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceUrl
;
60 import java
.net
.URISyntaxException
;
61 import java
.net
.URLDecoder
;
62 import java
.net
.URLEncoder
;
63 import java
.nio
.channels
.Channels
;
64 import java
.nio
.channels
.FileChannel
;
65 import java
.nio
.channels
.FileLock
;
66 import java
.nio
.file
.Files
;
67 import java
.nio
.file
.Path
;
68 import java
.nio
.file
.Paths
;
69 import java
.nio
.file
.StandardCopyOption
;
70 import java
.nio
.file
.attribute
.PosixFilePermission
;
71 import java
.nio
.file
.attribute
.PosixFilePermissions
;
73 import java
.util
.concurrent
.TimeUnit
;
74 import java
.util
.concurrent
.TimeoutException
;
76 import static java
.nio
.file
.attribute
.PosixFilePermission
.*;
78 class Manager
implements Signal
{
79 private final static String URL
= "https://textsecure-service.whispersystems.org";
80 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
81 private final static SignalServiceUrl
[] serviceUrls
= new SignalServiceUrl
[]{new SignalServiceUrl(URL
, TRUST_STORE
)};
83 public final static String PROJECT_NAME
= Manager
.class.getPackage().getImplementationTitle();
84 public final static String PROJECT_VERSION
= Manager
.class.getPackage().getImplementationVersion();
85 private final static String USER_AGENT
= PROJECT_NAME
== null ?
null : PROJECT_NAME
+ " " + PROJECT_VERSION
;
87 private final static int PREKEY_MINIMUM_COUNT
= 20;
88 private static final int PREKEY_BATCH_SIZE
= 100;
90 private final String settingsPath
;
91 private final String dataPath
;
92 private final String attachmentsPath
;
93 private final String avatarsPath
;
95 private FileChannel fileChannel
;
96 private FileLock lock
;
98 private final ObjectMapper jsonProcessor
= new ObjectMapper();
99 private String username
;
100 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
101 private String password
;
102 private String signalingKey
;
103 private int preKeyIdOffset
;
104 private int nextSignedPreKeyId
;
106 private boolean registered
= false;
108 private JsonSignalProtocolStore signalProtocolStore
;
109 private SignalServiceAccountManager accountManager
;
110 private JsonGroupStore groupStore
;
111 private JsonContactsStore contactStore
;
112 private JsonThreadStore threadStore
;
113 private SignalServiceMessagePipe messagePipe
= null;
115 public Manager(String username
, String settingsPath
) {
116 this.username
= username
;
117 this.settingsPath
= settingsPath
;
118 this.dataPath
= this.settingsPath
+ "/data";
119 this.attachmentsPath
= this.settingsPath
+ "/attachments";
120 this.avatarsPath
= this.settingsPath
+ "/avatars";
122 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
123 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
124 jsonProcessor
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
125 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
126 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
127 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
130 public String
getUsername() {
134 private IdentityKey
getIdentity() {
135 return signalProtocolStore
.getIdentityKeyPair().getPublicKey();
138 public int getDeviceId() {
142 public String
getFileName() {
143 return dataPath
+ "/" + username
;
146 private String
getMessageCachePath() {
147 return this.dataPath
+ "/" + username
+ ".d/msg-cache";
150 private String
getMessageCachePath(String sender
) {
151 return getMessageCachePath() + "/" + sender
.replace("/", "_");
154 private File
getMessageCacheFile(String sender
, long now
, long timestamp
) throws IOException
{
155 String cachePath
= getMessageCachePath(sender
);
156 createPrivateDirectories(cachePath
);
157 return new File(cachePath
+ "/" + now
+ "_" + timestamp
);
160 private static void createPrivateDirectories(String path
) throws IOException
{
161 final Path file
= new File(path
).toPath();
163 Set
<PosixFilePermission
> perms
= EnumSet
.of(OWNER_READ
, OWNER_WRITE
, OWNER_EXECUTE
);
164 Files
.createDirectories(file
, PosixFilePermissions
.asFileAttribute(perms
));
165 } catch (UnsupportedOperationException e
) {
166 Files
.createDirectories(file
);
170 private static void createPrivateFile(String path
) throws IOException
{
171 final Path file
= new File(path
).toPath();
173 Set
<PosixFilePermission
> perms
= EnumSet
.of(OWNER_READ
, OWNER_WRITE
);
174 Files
.createFile(file
, PosixFilePermissions
.asFileAttribute(perms
));
175 } catch (UnsupportedOperationException e
) {
176 Files
.createFile(file
);
180 public boolean userExists() {
181 if (username
== null) {
184 File f
= new File(getFileName());
185 return !(!f
.exists() || f
.isDirectory());
188 public boolean userHasKeys() {
189 return signalProtocolStore
!= null;
192 private JsonNode
getNotNullNode(JsonNode parent
, String name
) throws InvalidObjectException
{
193 JsonNode node
= parent
.get(name
);
195 throw new InvalidObjectException(String
.format("Incorrect file format: expected parameter %s not found ", name
));
201 private void openFileChannel() throws IOException
{
202 if (fileChannel
!= null)
205 createPrivateDirectories(dataPath
);
206 if (!new File(getFileName()).exists()) {
207 createPrivateFile(getFileName());
209 fileChannel
= new RandomAccessFile(new File(getFileName()), "rw").getChannel();
210 lock
= fileChannel
.tryLock();
212 System
.err
.println("Config file is in use by another instance, waiting…");
213 lock
= fileChannel
.lock();
214 System
.err
.println("Config file lock acquired.");
218 public void init() throws IOException
{
221 migrateLegacyConfigs();
223 accountManager
= new SignalServiceAccountManager(serviceUrls
, username
, password
, deviceId
, USER_AGENT
);
225 if (registered
&& accountManager
.getPreKeysCount() < PREKEY_MINIMUM_COUNT
) {
229 } catch (AuthorizationFailedException e
) {
230 System
.err
.println("Authorization failed, was the number registered elsewhere?");
234 private void load() throws IOException
{
236 JsonNode rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
238 JsonNode node
= rootNode
.get("deviceId");
240 deviceId
= node
.asInt();
242 username
= getNotNullNode(rootNode
, "username").asText();
243 password
= getNotNullNode(rootNode
, "password").asText();
244 if (rootNode
.has("signalingKey")) {
245 signalingKey
= getNotNullNode(rootNode
, "signalingKey").asText();
247 if (rootNode
.has("preKeyIdOffset")) {
248 preKeyIdOffset
= getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
252 if (rootNode
.has("nextSignedPreKeyId")) {
253 nextSignedPreKeyId
= getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
255 nextSignedPreKeyId
= 0;
257 signalProtocolStore
= jsonProcessor
.convertValue(getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
258 registered
= getNotNullNode(rootNode
, "registered").asBoolean();
259 JsonNode groupStoreNode
= rootNode
.get("groupStore");
260 if (groupStoreNode
!= null) {
261 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
263 if (groupStore
== null) {
264 groupStore
= new JsonGroupStore();
267 JsonNode contactStoreNode
= rootNode
.get("contactStore");
268 if (contactStoreNode
!= null) {
269 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
271 if (contactStore
== null) {
272 contactStore
= new JsonContactsStore();
274 JsonNode threadStoreNode
= rootNode
.get("threadStore");
275 if (threadStoreNode
!= null) {
276 threadStore
= jsonProcessor
.convertValue(threadStoreNode
, JsonThreadStore
.class);
278 if (threadStore
== null) {
279 threadStore
= new JsonThreadStore();
283 private void migrateLegacyConfigs() {
284 // Copy group avatars that were previously stored in the attachments folder
285 // to the new avatar folder
286 if (JsonGroupStore
.groupsWithLegacyAvatarId
.size() > 0) {
287 for (GroupInfo g
: JsonGroupStore
.groupsWithLegacyAvatarId
) {
288 File avatarFile
= getGroupAvatarFile(g
.groupId
);
289 File attachmentFile
= getAttachmentFile(g
.getAvatarId());
290 if (!avatarFile
.exists() && attachmentFile
.exists()) {
292 createPrivateDirectories(avatarsPath
);
293 Files
.copy(attachmentFile
.toPath(), avatarFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
294 } catch (Exception e
) {
299 JsonGroupStore
.groupsWithLegacyAvatarId
.clear();
304 private void save() {
305 if (username
== null) {
308 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
309 rootNode
.put("username", username
)
310 .put("deviceId", deviceId
)
311 .put("password", password
)
312 .put("signalingKey", signalingKey
)
313 .put("preKeyIdOffset", preKeyIdOffset
)
314 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
315 .put("registered", registered
)
316 .putPOJO("axolotlStore", signalProtocolStore
)
317 .putPOJO("groupStore", groupStore
)
318 .putPOJO("contactStore", contactStore
)
319 .putPOJO("threadStore", threadStore
)
323 fileChannel
.position(0);
324 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
325 fileChannel
.truncate(fileChannel
.position());
326 fileChannel
.force(false);
327 } catch (Exception e
) {
328 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
332 public void createNewIdentity() {
333 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
334 int registrationId
= KeyHelper
.generateRegistrationId(false);
335 signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
336 groupStore
= new JsonGroupStore();
341 public boolean isRegistered() {
345 public void register(boolean voiceVerification
) throws IOException
{
346 password
= Util
.getSecret(18);
348 accountManager
= new SignalServiceAccountManager(serviceUrls
, username
, password
, USER_AGENT
);
350 if (voiceVerification
)
351 accountManager
.requestVoiceVerificationCode();
353 accountManager
.requestSmsVerificationCode();
359 public void updateAccountAttributes() throws IOException
{
360 accountManager
.setAccountAttributes(signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, true);
363 public void unregister() throws IOException
{
364 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
365 // If this is the master device, other users can't send messages to this number anymore.
366 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
367 accountManager
.setGcmId(Optional
.<String
>absent());
370 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
371 password
= Util
.getSecret(18);
373 accountManager
= new SignalServiceAccountManager(serviceUrls
, username
, password
, USER_AGENT
);
374 String uuid
= accountManager
.getNewDeviceUuid();
378 return new URI("tsdevice:/?uuid=" + URLEncoder
.encode(uuid
, "utf-8") + "&pub_key=" + URLEncoder
.encode(Base64
.encodeBytesWithoutPadding(signalProtocolStore
.getIdentityKeyPair().getPublicKey().serialize()), "utf-8"));
379 } catch (URISyntaxException e
) {
385 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
386 signalingKey
= Util
.getSecret(52);
387 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(signalProtocolStore
.getIdentityKeyPair(), signalingKey
, false, true, signalProtocolStore
.getLocalRegistrationId(), deviceName
);
388 deviceId
= ret
.getDeviceId();
389 username
= ret
.getNumber();
390 // TODO do this check before actually registering
392 throw new UserAlreadyExists(username
, getFileName());
394 signalProtocolStore
= new JsonSignalProtocolStore(ret
.getIdentity(), signalProtocolStore
.getLocalRegistrationId());
400 requestSyncContacts();
405 public List
<DeviceInfo
> getLinkedDevices() throws IOException
{
406 return accountManager
.getDevices();
409 public void removeLinkedDevices(int deviceId
) throws IOException
{
410 accountManager
.removeDevice(deviceId
);
413 public static Map
<String
, String
> getQueryMap(String query
) {
414 String
[] params
= query
.split("&");
415 Map
<String
, String
> map
= new HashMap
<>();
416 for (String param
: params
) {
419 name
= URLDecoder
.decode(param
.split("=")[0], "utf-8");
420 } catch (UnsupportedEncodingException e
) {
425 value
= URLDecoder
.decode(param
.split("=")[1], "utf-8");
426 } catch (UnsupportedEncodingException e
) {
429 map
.put(name
, value
);
434 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
435 Map
<String
, String
> query
= getQueryMap(linkUri
.getRawQuery());
436 String deviceIdentifier
= query
.get("uuid");
437 String publicKeyEncoded
= query
.get("pub_key");
439 if (TextUtils
.isEmpty(deviceIdentifier
) || TextUtils
.isEmpty(publicKeyEncoded
)) {
440 throw new RuntimeException("Invalid device link uri");
443 ECPublicKey deviceKey
= Curve
.decodePoint(Base64
.decode(publicKeyEncoded
), 0);
445 addDevice(deviceIdentifier
, deviceKey
);
448 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
449 IdentityKeyPair identityKeyPair
= signalProtocolStore
.getIdentityKeyPair();
450 String verificationCode
= accountManager
.getNewDeviceVerificationCode();
452 accountManager
.addDevice(deviceIdentifier
, deviceKey
, identityKeyPair
, verificationCode
);
455 private List
<PreKeyRecord
> generatePreKeys() {
456 List
<PreKeyRecord
> records
= new LinkedList
<>();
458 for (int i
= 0; i
< PREKEY_BATCH_SIZE
; i
++) {
459 int preKeyId
= (preKeyIdOffset
+ i
) % Medium
.MAX_VALUE
;
460 ECKeyPair keyPair
= Curve
.generateKeyPair();
461 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
463 signalProtocolStore
.storePreKey(preKeyId
, record);
467 preKeyIdOffset
= (preKeyIdOffset
+ PREKEY_BATCH_SIZE
+ 1) % Medium
.MAX_VALUE
;
473 private PreKeyRecord
getOrGenerateLastResortPreKey() {
474 if (signalProtocolStore
.containsPreKey(Medium
.MAX_VALUE
)) {
476 return signalProtocolStore
.loadPreKey(Medium
.MAX_VALUE
);
477 } catch (InvalidKeyIdException e
) {
478 signalProtocolStore
.removePreKey(Medium
.MAX_VALUE
);
482 ECKeyPair keyPair
= Curve
.generateKeyPair();
483 PreKeyRecord
record = new PreKeyRecord(Medium
.MAX_VALUE
, keyPair
);
485 signalProtocolStore
.storePreKey(Medium
.MAX_VALUE
, record);
491 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
493 ECKeyPair keyPair
= Curve
.generateKeyPair();
494 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
495 SignedPreKeyRecord
record = new SignedPreKeyRecord(nextSignedPreKeyId
, System
.currentTimeMillis(), keyPair
, signature
);
497 signalProtocolStore
.storeSignedPreKey(nextSignedPreKeyId
, record);
498 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
502 } catch (InvalidKeyException e
) {
503 throw new AssertionError(e
);
507 public void verifyAccount(String verificationCode
) throws IOException
{
508 verificationCode
= verificationCode
.replace("-", "");
509 signalingKey
= Util
.getSecret(52);
510 accountManager
.verifyAccountWithCode(verificationCode
, signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, true);
512 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
519 private void refreshPreKeys() throws IOException
{
520 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
521 PreKeyRecord lastResortKey
= getOrGenerateLastResortPreKey();
522 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(signalProtocolStore
.getIdentityKeyPair());
524 accountManager
.setPreKeys(signalProtocolStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
528 private static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
529 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
530 if (attachments
!= null) {
531 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
532 for (String attachment
: attachments
) {
534 SignalServiceAttachments
.add(createAttachment(new File(attachment
)));
535 } catch (IOException e
) {
536 throw new AttachmentInvalidException(attachment
, e
);
540 return SignalServiceAttachments
;
543 private static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
544 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
545 final long attachmentSize
= attachmentFile
.length();
546 String mime
= Files
.probeContentType(attachmentFile
.toPath());
548 mime
= "application/octet-stream";
550 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, null);
553 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(byte[] groupId
) throws IOException
{
554 File file
= getGroupAvatarFile(groupId
);
555 if (!file
.exists()) {
556 return Optional
.absent();
559 return Optional
.of(createAttachment(file
));
562 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(String number
) throws IOException
{
563 File file
= getContactAvatarFile(number
);
564 if (!file
.exists()) {
565 return Optional
.absent();
568 return Optional
.of(createAttachment(file
));
571 private GroupInfo
getGroupForSending(byte[] groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
572 GroupInfo g
= groupStore
.getGroup(groupId
);
574 throw new GroupNotFoundException(groupId
);
576 for (String member
: g
.members
) {
577 if (member
.equals(this.username
)) {
581 throw new NotAGroupMemberException(groupId
, g
.name
);
585 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
587 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
588 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
589 if (attachments
!= null) {
590 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
592 if (groupId
!= null) {
593 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
596 messageBuilder
.asGroupMessage(group
);
598 ThreadInfo thread
= threadStore
.getThread(Base64
.encodeBytes(groupId
));
599 if (thread
!= null) {
600 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
603 final GroupInfo g
= getGroupForSending(groupId
);
605 // Don't send group message to ourself
606 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
607 membersSend
.remove(this.username
);
608 sendMessage(messageBuilder
, membersSend
);
611 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
{
612 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
616 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
617 .asGroupMessage(group
);
619 final GroupInfo g
= getGroupForSending(groupId
);
620 g
.members
.remove(this.username
);
621 groupStore
.updateGroup(g
);
623 sendMessage(messageBuilder
, g
.members
);
626 private static String
join(CharSequence separator
, Iterable
<?
extends CharSequence
> list
) {
627 StringBuilder buf
= new StringBuilder();
628 for (CharSequence str
: list
) {
629 if (buf
.length() > 0) {
630 buf
.append(separator
);
635 return buf
.toString();
638 public byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
640 if (groupId
== null) {
642 g
= new GroupInfo(Util
.getSecretBytes(16));
643 g
.members
.add(username
);
645 g
= getGroupForSending(groupId
);
652 if (members
!= null) {
653 Set
<String
> newMembers
= new HashSet
<>();
654 for (String member
: members
) {
656 member
= canonicalizeNumber(member
);
657 } catch (InvalidNumberException e
) {
658 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
659 System
.err
.println("Aborting…");
662 if (g
.members
.contains(member
)) {
665 newMembers
.add(member
);
666 g
.members
.add(member
);
668 final List
<ContactTokenDetails
> contacts
= accountManager
.getContacts(newMembers
);
669 if (contacts
.size() != newMembers
.size()) {
670 // Some of the new members are not registered on Signal
671 for (ContactTokenDetails contact
: contacts
) {
672 newMembers
.remove(contact
.getNumber());
674 System
.err
.println("Failed to add members " + join(", ", newMembers
) + " to group: Not registered on Signal");
675 System
.err
.println("Aborting…");
680 if (avatarFile
!= null) {
681 createPrivateDirectories(avatarsPath
);
682 File aFile
= getGroupAvatarFile(g
.groupId
);
683 Files
.copy(Paths
.get(avatarFile
), aFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
686 groupStore
.updateGroup(g
);
688 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
690 // Don't send group message to ourself
691 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
692 membersSend
.remove(this.username
);
693 sendMessage(messageBuilder
, membersSend
);
697 private void sendUpdateGroupMessage(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
698 if (groupId
== null) {
701 GroupInfo g
= getGroupForSending(groupId
);
703 if (!g
.members
.contains(recipient
)) {
707 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
709 // Send group message only to the recipient who requested it
710 final List
<String
> membersSend
= new ArrayList
<>();
711 membersSend
.add(recipient
);
712 sendMessage(messageBuilder
, membersSend
);
715 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfo g
) {
716 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
719 .withMembers(new ArrayList
<>(g
.members
));
721 File aFile
= getGroupAvatarFile(g
.groupId
);
722 if (aFile
.exists()) {
724 group
.withAvatar(createAttachment(aFile
));
725 } catch (IOException e
) {
726 throw new AttachmentInvalidException(aFile
.toString(), e
);
730 return SignalServiceDataMessage
.newBuilder()
731 .asGroupMessage(group
.build());
734 private void sendGroupInfoRequest(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
735 if (groupId
== null) {
739 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
)
742 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
743 .asGroupMessage(group
.build());
745 // Send group info request message to the recipient who sent us a message with this groupId
746 final List
<String
> membersSend
= new ArrayList
<>();
747 membersSend
.add(recipient
);
748 sendMessage(messageBuilder
, membersSend
);
752 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
753 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
{
754 List
<String
> recipients
= new ArrayList
<>(1);
755 recipients
.add(recipient
);
756 sendMessage(message
, attachments
, recipients
);
760 public void sendMessage(String messageText
, List
<String
> attachments
,
761 List
<String
> recipients
)
762 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
{
763 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
764 if (attachments
!= null) {
765 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
767 sendMessage(messageBuilder
, recipients
);
771 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
{
772 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
773 .asEndSessionMessage();
775 sendMessage(messageBuilder
, recipients
);
778 private void requestSyncGroups() throws IOException
{
779 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
).build();
780 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
782 sendSyncMessage(message
);
783 } catch (UntrustedIdentityException e
) {
788 private void requestSyncContacts() throws IOException
{
789 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
).build();
790 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
792 sendSyncMessage(message
);
793 } catch (UntrustedIdentityException e
) {
798 private void sendSyncMessage(SignalServiceSyncMessage message
)
799 throws IOException
, UntrustedIdentityException
{
800 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(serviceUrls
, username
, password
,
801 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.fromNullable(messagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
803 messageSender
.sendMessage(message
);
804 } catch (UntrustedIdentityException e
) {
805 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
810 private void sendMessage(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
811 throws EncapsulatedExceptions
, IOException
{
812 Set
<SignalServiceAddress
> recipientsTS
= getSignalServiceAddresses(recipients
);
813 if (recipientsTS
== null) return;
815 SignalServiceDataMessage message
= null;
817 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(serviceUrls
, username
, password
,
818 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.fromNullable(messagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
820 message
= messageBuilder
.build();
821 if (message
.getGroupInfo().isPresent()) {
823 messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), message
);
824 } catch (EncapsulatedExceptions encapsulatedExceptions
) {
825 for (UntrustedIdentityException e
: encapsulatedExceptions
.getUntrustedIdentityExceptions()) {
826 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
830 // Send to all individually, so sync messages are sent correctly
831 List
<UntrustedIdentityException
> untrustedIdentities
= new LinkedList
<>();
832 List
<UnregisteredUserException
> unregisteredUsers
= new LinkedList
<>();
833 List
<NetworkFailureException
> networkExceptions
= new LinkedList
<>();
834 for (SignalServiceAddress address
: recipientsTS
) {
835 ThreadInfo thread
= threadStore
.getThread(address
.getNumber());
836 if (thread
!= null) {
837 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
839 messageBuilder
.withExpiration(0);
841 message
= messageBuilder
.build();
843 messageSender
.sendMessage(address
, message
);
844 } catch (UntrustedIdentityException e
) {
845 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
846 untrustedIdentities
.add(e
);
847 } catch (UnregisteredUserException e
) {
848 unregisteredUsers
.add(e
);
849 } catch (PushNetworkException e
) {
850 networkExceptions
.add(new NetworkFailureException(address
.getNumber(), e
));
853 if (!untrustedIdentities
.isEmpty() || !unregisteredUsers
.isEmpty() || !networkExceptions
.isEmpty()) {
854 throw new EncapsulatedExceptions(untrustedIdentities
, unregisteredUsers
, networkExceptions
);
858 if (message
!= null && message
.isEndSession()) {
859 for (SignalServiceAddress recipient
: recipientsTS
) {
860 handleEndSession(recipient
.getNumber());
867 private Set
<SignalServiceAddress
> getSignalServiceAddresses(Collection
<String
> recipients
) {
868 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
869 for (String recipient
: recipients
) {
871 recipientsTS
.add(getPushAddress(recipient
));
872 } catch (InvalidNumberException e
) {
873 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
874 System
.err
.println("Aborting sending.");
882 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws NoSessionException
, LegacyMessageException
, InvalidVersionException
, InvalidMessageException
, DuplicateMessageException
, InvalidKeyException
, InvalidKeyIdException
, org
.whispersystems
.libsignal
.UntrustedIdentityException
{
883 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), signalProtocolStore
);
885 return cipher
.decrypt(envelope
);
886 } catch (org
.whispersystems
.libsignal
.UntrustedIdentityException e
) {
887 signalProtocolStore
.saveIdentity(e
.getName(), e
.getUntrustedIdentity(), TrustLevel
.UNTRUSTED
);
892 private void handleEndSession(String source
) {
893 signalProtocolStore
.deleteAllSessions(source
);
896 public interface ReceiveMessageHandler
{
897 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);
900 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
, boolean ignoreAttachments
) {
902 if (message
.getGroupInfo().isPresent()) {
903 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
904 threadId
= Base64
.encodeBytes(groupInfo
.getGroupId());
905 GroupInfo group
= groupStore
.getGroup(groupInfo
.getGroupId());
906 switch (groupInfo
.getType()) {
909 group
= new GroupInfo(groupInfo
.getGroupId());
912 if (groupInfo
.getAvatar().isPresent()) {
913 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
914 if (avatar
.isPointer()) {
916 retrieveGroupAvatarAttachment(avatar
.asPointer(), group
.groupId
);
917 } catch (IOException
| InvalidMessageException e
) {
918 System
.err
.println("Failed to retrieve group avatar (" + avatar
.asPointer().getId() + "): " + e
.getMessage());
923 if (groupInfo
.getName().isPresent()) {
924 group
.name
= groupInfo
.getName().get();
927 if (groupInfo
.getMembers().isPresent()) {
928 group
.members
.addAll(groupInfo
.getMembers().get());
931 groupStore
.updateGroup(group
);
936 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
937 } catch (IOException
| EncapsulatedExceptions e
) {
945 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
946 } catch (IOException
| EncapsulatedExceptions e
) {
950 group
.members
.remove(source
);
951 groupStore
.updateGroup(group
);
957 sendUpdateGroupMessage(groupInfo
.getGroupId(), source
);
958 } catch (IOException
| EncapsulatedExceptions e
) {
960 } catch (NotAGroupMemberException e
) {
961 // We have left this group, so don't send a group update message
968 threadId
= destination
;
973 if (message
.isEndSession()) {
974 handleEndSession(isSync ? destination
: source
);
976 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
977 ThreadInfo thread
= threadStore
.getThread(threadId
);
978 if (thread
== null) {
979 thread
= new ThreadInfo();
980 thread
.id
= threadId
;
982 if (thread
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
983 thread
.messageExpirationTime
= message
.getExpiresInSeconds();
984 threadStore
.updateThread(thread
);
987 if (message
.getAttachments().isPresent() && !ignoreAttachments
) {
988 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
989 if (attachment
.isPointer()) {
991 retrieveAttachment(attachment
.asPointer());
992 } catch (IOException
| InvalidMessageException e
) {
993 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
1000 public void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1001 final File cachePath
= new File(getMessageCachePath());
1002 if (!cachePath
.exists()) {
1005 for (final File dir
: cachePath
.listFiles()) {
1006 if (!dir
.isDirectory()) {
1010 for (final File fileEntry
: dir
.listFiles()) {
1011 if (!fileEntry
.isFile()) {
1014 SignalServiceEnvelope envelope
;
1016 envelope
= loadEnvelope(fileEntry
);
1017 if (envelope
== null) {
1020 } catch (IOException e
) {
1021 e
.printStackTrace();
1024 SignalServiceContent content
= null;
1025 if (!envelope
.isReceipt()) {
1027 content
= decryptMessage(envelope
);
1028 } catch (Exception e
) {
1031 handleMessage(envelope
, content
, ignoreAttachments
);
1034 handler
.handleMessage(envelope
, content
, null);
1036 Files
.delete(fileEntry
.toPath());
1037 } catch (IOException e
) {
1038 System
.out
.println("Failed to delete cached message file “" + fileEntry
+ "”: " + e
.getMessage());
1044 public void receiveMessages(long timeout
, TimeUnit unit
, boolean returnOnTimeout
, boolean ignoreAttachments
, ReceiveMessageHandler handler
) throws IOException
{
1045 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1046 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(serviceUrls
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1049 if (messagePipe
== null) {
1050 messagePipe
= messageReceiver
.createMessagePipe();
1054 SignalServiceEnvelope envelope
;
1055 SignalServiceContent content
= null;
1056 Exception exception
= null;
1057 final long now
= new Date().getTime();
1059 envelope
= messagePipe
.read(timeout
, unit
, new SignalServiceMessagePipe
.MessagePipeCallback() {
1061 public void onMessage(SignalServiceEnvelope envelope
) {
1062 // store message on disk, before acknowledging receipt to the server
1064 File cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1065 storeEnvelope(envelope
, cacheFile
);
1066 } catch (IOException e
) {
1067 System
.err
.println("Failed to store encrypted message in disk cache, ignoring: " + e
.getMessage());
1071 } catch (TimeoutException e
) {
1072 if (returnOnTimeout
)
1075 } catch (InvalidVersionException e
) {
1076 System
.err
.println("Ignoring error: " + e
.getMessage());
1079 if (!envelope
.isReceipt()) {
1081 content
= decryptMessage(envelope
);
1082 } catch (Exception e
) {
1085 handleMessage(envelope
, content
, ignoreAttachments
);
1088 handler
.handleMessage(envelope
, content
, exception
);
1089 if (exception
== null || !(exception
instanceof org
.whispersystems
.libsignal
.UntrustedIdentityException
)) {
1090 File cacheFile
= null;
1092 cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1093 Files
.delete(cacheFile
.toPath());
1094 } catch (IOException e
) {
1095 System
.out
.println("Failed to delete cached message file “" + cacheFile
+ "”: " + e
.getMessage());
1100 if (messagePipe
!= null) {
1101 messagePipe
.shutdown();
1107 private void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
) {
1108 if (content
!= null) {
1109 if (content
.getDataMessage().isPresent()) {
1110 SignalServiceDataMessage message
= content
.getDataMessage().get();
1111 handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
, ignoreAttachments
);
1113 if (content
.getSyncMessage().isPresent()) {
1114 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
1115 if (syncMessage
.getSent().isPresent()) {
1116 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
1117 handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get(), ignoreAttachments
);
1119 if (syncMessage
.getRequest().isPresent()) {
1120 RequestMessage rm
= syncMessage
.getRequest().get();
1121 if (rm
.isContactsRequest()) {
1124 } catch (UntrustedIdentityException
| IOException e
) {
1125 e
.printStackTrace();
1128 if (rm
.isGroupsRequest()) {
1131 } catch (UntrustedIdentityException
| IOException e
) {
1132 e
.printStackTrace();
1136 if (syncMessage
.getGroups().isPresent()) {
1137 File tmpFile
= null;
1139 tmpFile
= Util
.createTempFile();
1140 DeviceGroupsInputStream s
= new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage
.getGroups().get().asPointer(), tmpFile
));
1142 while ((g
= s
.read()) != null) {
1143 GroupInfo syncGroup
= groupStore
.getGroup(g
.getId());
1144 if (syncGroup
== null) {
1145 syncGroup
= new GroupInfo(g
.getId());
1147 if (g
.getName().isPresent()) {
1148 syncGroup
.name
= g
.getName().get();
1150 syncGroup
.members
.addAll(g
.getMembers());
1151 syncGroup
.active
= g
.isActive();
1153 if (g
.getAvatar().isPresent()) {
1154 retrieveGroupAvatarAttachment(g
.getAvatar().get(), syncGroup
.groupId
);
1156 groupStore
.updateGroup(syncGroup
);
1158 } catch (Exception e
) {
1159 e
.printStackTrace();
1161 if (tmpFile
!= null) {
1163 Files
.delete(tmpFile
.toPath());
1164 } catch (IOException e
) {
1165 System
.out
.println("Failed to delete temp file “" + tmpFile
+ "”: " + e
.getMessage());
1169 if (syncMessage
.getBlockedList().isPresent()) {
1170 // TODO store list of blocked numbers
1173 if (syncMessage
.getContacts().isPresent()) {
1174 File tmpFile
= null;
1176 tmpFile
= Util
.createTempFile();
1177 DeviceContactsInputStream s
= new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage
.getContacts().get().asPointer(), tmpFile
));
1179 while ((c
= s
.read()) != null) {
1180 ContactInfo contact
= contactStore
.getContact(c
.getNumber());
1181 if (contact
== null) {
1182 contact
= new ContactInfo();
1183 contact
.number
= c
.getNumber();
1185 if (c
.getName().isPresent()) {
1186 contact
.name
= c
.getName().get();
1188 if (c
.getColor().isPresent()) {
1189 contact
.color
= c
.getColor().get();
1191 contactStore
.updateContact(contact
);
1193 if (c
.getAvatar().isPresent()) {
1194 retrieveContactAvatarAttachment(c
.getAvatar().get(), contact
.number
);
1197 } catch (Exception e
) {
1198 e
.printStackTrace();
1200 if (tmpFile
!= null) {
1202 Files
.delete(tmpFile
.toPath());
1203 } catch (IOException e
) {
1204 System
.out
.println("Failed to delete temp file “" + tmpFile
+ "”: " + e
.getMessage());
1213 private SignalServiceEnvelope
loadEnvelope(File file
) throws IOException
{
1214 try (FileInputStream f
= new FileInputStream(file
)) {
1215 DataInputStream
in = new DataInputStream(f
);
1216 int version
= in.readInt();
1220 int type
= in.readInt();
1221 String source
= in.readUTF();
1222 int sourceDevice
= in.readInt();
1223 String relay
= in.readUTF();
1224 long timestamp
= in.readLong();
1225 byte[] content
= null;
1226 int contentLen
= in.readInt();
1227 if (contentLen
> 0) {
1228 content
= new byte[contentLen
];
1229 in.readFully(content
);
1231 byte[] legacyMessage
= null;
1232 int legacyMessageLen
= in.readInt();
1233 if (legacyMessageLen
> 0) {
1234 legacyMessage
= new byte[legacyMessageLen
];
1235 in.readFully(legacyMessage
);
1237 return new SignalServiceEnvelope(type
, source
, sourceDevice
, relay
, timestamp
, legacyMessage
, content
);
1241 private void storeEnvelope(SignalServiceEnvelope envelope
, File file
) throws IOException
{
1242 try (FileOutputStream f
= new FileOutputStream(file
)) {
1243 try (DataOutputStream out
= new DataOutputStream(f
)) {
1244 out
.writeInt(1); // version
1245 out
.writeInt(envelope
.getType());
1246 out
.writeUTF(envelope
.getSource());
1247 out
.writeInt(envelope
.getSourceDevice());
1248 out
.writeUTF(envelope
.getRelay());
1249 out
.writeLong(envelope
.getTimestamp());
1250 if (envelope
.hasContent()) {
1251 out
.writeInt(envelope
.getContent().length
);
1252 out
.write(envelope
.getContent());
1256 if (envelope
.hasLegacyMessage()) {
1257 out
.writeInt(envelope
.getLegacyMessage().length
);
1258 out
.write(envelope
.getLegacyMessage());
1266 public File
getContactAvatarFile(String number
) {
1267 return new File(avatarsPath
, "contact-" + number
);
1270 private File
retrieveContactAvatarAttachment(SignalServiceAttachment attachment
, String number
) throws IOException
, InvalidMessageException
{
1271 createPrivateDirectories(avatarsPath
);
1272 if (attachment
.isPointer()) {
1273 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1274 return retrieveAttachment(pointer
, getContactAvatarFile(number
), false);
1276 SignalServiceAttachmentStream stream
= attachment
.asStream();
1277 return retrieveAttachment(stream
, getContactAvatarFile(number
));
1281 public File
getGroupAvatarFile(byte[] groupId
) {
1282 return new File(avatarsPath
, "group-" + Base64
.encodeBytes(groupId
).replace("/", "_"));
1285 private File
retrieveGroupAvatarAttachment(SignalServiceAttachment attachment
, byte[] groupId
) throws IOException
, InvalidMessageException
{
1286 createPrivateDirectories(avatarsPath
);
1287 if (attachment
.isPointer()) {
1288 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1289 return retrieveAttachment(pointer
, getGroupAvatarFile(groupId
), false);
1291 SignalServiceAttachmentStream stream
= attachment
.asStream();
1292 return retrieveAttachment(stream
, getGroupAvatarFile(groupId
));
1296 public File
getAttachmentFile(long attachmentId
) {
1297 return new File(attachmentsPath
, attachmentId
+ "");
1300 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
1301 createPrivateDirectories(attachmentsPath
);
1302 return retrieveAttachment(pointer
, getAttachmentFile(pointer
.getId()), true);
1305 private File
retrieveAttachment(SignalServiceAttachmentStream stream
, File outputFile
) throws IOException
, InvalidMessageException
{
1306 InputStream input
= stream
.getInputStream();
1308 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1309 byte[] buffer
= new byte[4096];
1312 while ((read
= input
.read(buffer
)) != -1) {
1313 output
.write(buffer
, 0, read
);
1315 } catch (FileNotFoundException e
) {
1316 e
.printStackTrace();
1322 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
, File outputFile
, boolean storePreview
) throws IOException
, InvalidMessageException
{
1323 if (storePreview
&& pointer
.getPreview().isPresent()) {
1324 File previewFile
= new File(outputFile
+ ".preview");
1325 try (OutputStream output
= new FileOutputStream(previewFile
)) {
1326 byte[] preview
= pointer
.getPreview().get();
1327 output
.write(preview
, 0, preview
.length
);
1328 } catch (FileNotFoundException e
) {
1329 e
.printStackTrace();
1334 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(serviceUrls
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1336 File tmpFile
= Util
.createTempFile();
1337 try (InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
)) {
1338 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1339 byte[] buffer
= new byte[4096];
1342 while ((read
= input
.read(buffer
)) != -1) {
1343 output
.write(buffer
, 0, read
);
1345 } catch (FileNotFoundException e
) {
1346 e
.printStackTrace();
1351 Files
.delete(tmpFile
.toPath());
1352 } catch (IOException e
) {
1353 System
.out
.println("Failed to delete temp file “" + tmpFile
+ "”: " + e
.getMessage());
1359 private InputStream
retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer
, File tmpFile
) throws IOException
, InvalidMessageException
{
1360 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(serviceUrls
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1361 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
);
1364 private String
canonicalizeNumber(String number
) throws InvalidNumberException
{
1365 String localNumber
= username
;
1366 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
1369 private SignalServiceAddress
getPushAddress(String number
) throws InvalidNumberException
{
1370 String e164number
= canonicalizeNumber(number
);
1371 return new SignalServiceAddress(e164number
);
1375 public boolean isRemote() {
1379 private void sendGroups() throws IOException
, UntrustedIdentityException
{
1380 File groupsFile
= Util
.createTempFile();
1383 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
1384 DeviceGroupsOutputStream out
= new DeviceGroupsOutputStream(fos
);
1385 for (GroupInfo
record : groupStore
.getGroups()) {
1386 out
.write(new DeviceGroup(record.groupId
, Optional
.fromNullable(record.name
),
1387 new ArrayList
<>(record.members
), createGroupAvatarAttachment(record.groupId
),
1392 if (groupsFile
.exists() && groupsFile
.length() > 0) {
1393 try (FileInputStream groupsFileStream
= new FileInputStream(groupsFile
)) {
1394 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1395 .withStream(groupsFileStream
)
1396 .withContentType("application/octet-stream")
1397 .withLength(groupsFile
.length())
1400 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
1405 Files
.delete(groupsFile
.toPath());
1406 } catch (IOException e
) {
1407 System
.out
.println("Failed to delete temp file “" + groupsFile
+ "”: " + e
.getMessage());
1412 private void sendContacts() throws IOException
, UntrustedIdentityException
{
1413 File contactsFile
= Util
.createTempFile();
1416 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
1417 DeviceContactsOutputStream out
= new DeviceContactsOutputStream(fos
);
1418 for (ContactInfo
record : contactStore
.getContacts()) {
1419 out
.write(new DeviceContact(record.number
, Optional
.fromNullable(record.name
),
1420 createContactAvatarAttachment(record.number
), Optional
.fromNullable(record.color
)));
1424 if (contactsFile
.exists() && contactsFile
.length() > 0) {
1425 try (FileInputStream contactsFileStream
= new FileInputStream(contactsFile
)) {
1426 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1427 .withStream(contactsFileStream
)
1428 .withContentType("application/octet-stream")
1429 .withLength(contactsFile
.length())
1432 sendSyncMessage(SignalServiceSyncMessage
.forContacts(attachmentStream
));
1437 Files
.delete(contactsFile
.toPath());
1438 } catch (IOException e
) {
1439 System
.out
.println("Failed to delete temp file “" + contactsFile
+ "”: " + e
.getMessage());
1444 public ContactInfo
getContact(String number
) {
1445 return contactStore
.getContact(number
);
1448 public GroupInfo
getGroup(byte[] groupId
) {
1449 return groupStore
.getGroup(groupId
);
1452 public Map
<String
, List
<JsonIdentityKeyStore
.Identity
>> getIdentities() {
1453 return signalProtocolStore
.getIdentities();
1456 public List
<JsonIdentityKeyStore
.Identity
> getIdentities(String number
) {
1457 return signalProtocolStore
.getIdentities(number
);
1461 * Trust this the identity with this fingerprint
1463 * @param name username of the identity
1464 * @param fingerprint Fingerprint
1466 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) {
1467 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1471 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1472 if (!Arrays
.equals(id
.identityKey
.serialize(), fingerprint
)) {
1476 signalProtocolStore
.saveIdentity(name
, id
.identityKey
, TrustLevel
.TRUSTED_VERIFIED
);
1484 * Trust this the identity with this safety number
1486 * @param name username of the identity
1487 * @param safetyNumber Safety number
1489 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) {
1490 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1494 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1495 if (!safetyNumber
.equals(computeSafetyNumber(name
, id
.identityKey
))) {
1499 signalProtocolStore
.saveIdentity(name
, id
.identityKey
, TrustLevel
.TRUSTED_VERIFIED
);
1507 * Trust all keys of this identity without verification
1509 * @param name username of the identity
1511 public boolean trustIdentityAllKeys(String name
) {
1512 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1516 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1517 if (id
.trustLevel
== TrustLevel
.UNTRUSTED
) {
1518 signalProtocolStore
.saveIdentity(name
, id
.identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
);
1525 public String
computeSafetyNumber(String theirUsername
, IdentityKey theirIdentityKey
) {
1526 Fingerprint fingerprint
= new NumericFingerprintGenerator(5200).createFor(username
, getIdentity(), theirUsername
, theirIdentityKey
);
1527 return fingerprint
.getDisplayableFingerprint().getDisplayText();