]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/Manager.java
Improve behavior, when authorization fails
[signal-cli] / src / main / java / org / asamk / signal / manager / Manager.java
1 /*
2 Copyright (C) 2015-2020 AsamK and contributors
3
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.
8
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.
13
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/>.
16 */
17 package org.asamk.signal.manager;
18
19 import com.fasterxml.jackson.databind.ObjectMapper;
20
21 import org.asamk.Signal;
22 import org.asamk.signal.AttachmentInvalidException;
23 import org.asamk.signal.GroupNotFoundException;
24 import org.asamk.signal.JsonStickerPack;
25 import org.asamk.signal.NotAGroupMemberException;
26 import org.asamk.signal.StickerPackInvalidException;
27 import org.asamk.signal.TrustLevel;
28 import org.asamk.signal.UserAlreadyExists;
29 import org.asamk.signal.storage.SignalAccount;
30 import org.asamk.signal.storage.contacts.ContactInfo;
31 import org.asamk.signal.storage.groups.GroupInfo;
32 import org.asamk.signal.storage.groups.JsonGroupStore;
33 import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
34 import org.asamk.signal.util.IOUtils;
35 import org.asamk.signal.util.Util;
36 import org.signal.libsignal.metadata.InvalidMetadataMessageException;
37 import org.signal.libsignal.metadata.InvalidMetadataVersionException;
38 import org.signal.libsignal.metadata.ProtocolDuplicateMessageException;
39 import org.signal.libsignal.metadata.ProtocolInvalidKeyException;
40 import org.signal.libsignal.metadata.ProtocolInvalidKeyIdException;
41 import org.signal.libsignal.metadata.ProtocolInvalidMessageException;
42 import org.signal.libsignal.metadata.ProtocolInvalidVersionException;
43 import org.signal.libsignal.metadata.ProtocolLegacyMessageException;
44 import org.signal.libsignal.metadata.ProtocolNoSessionException;
45 import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
46 import org.signal.libsignal.metadata.SelfSendException;
47 import org.signal.libsignal.metadata.certificate.InvalidCertificateException;
48 import org.signal.zkgroup.InvalidInputException;
49 import org.signal.zkgroup.VerificationFailedException;
50 import org.signal.zkgroup.profiles.ProfileKey;
51 import org.whispersystems.libsignal.IdentityKey;
52 import org.whispersystems.libsignal.IdentityKeyPair;
53 import org.whispersystems.libsignal.InvalidKeyException;
54 import org.whispersystems.libsignal.InvalidMessageException;
55 import org.whispersystems.libsignal.InvalidVersionException;
56 import org.whispersystems.libsignal.ecc.Curve;
57 import org.whispersystems.libsignal.ecc.ECKeyPair;
58 import org.whispersystems.libsignal.ecc.ECPublicKey;
59 import org.whispersystems.libsignal.state.PreKeyRecord;
60 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
61 import org.whispersystems.libsignal.util.KeyHelper;
62 import org.whispersystems.libsignal.util.Medium;
63 import org.whispersystems.libsignal.util.Pair;
64 import org.whispersystems.libsignal.util.guava.Optional;
65 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
66 import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
67 import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
68 import org.whispersystems.signalservice.api.SignalServiceMessageSender;
69 import org.whispersystems.signalservice.api.crypto.InvalidCiphertextException;
70 import org.whispersystems.signalservice.api.crypto.ProfileCipher;
71 import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
72 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
73 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
74 import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
75 import org.whispersystems.signalservice.api.messages.SendMessageResult;
76 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
77 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
78 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
79 import org.whispersystems.signalservice.api.messages.SignalServiceContent;
80 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
81 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
82 import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
83 import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
84 import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload;
85 import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload.StickerInfo;
86 import org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage;
87 import org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage;
88 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContact;
89 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsInputStream;
90 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream;
91 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup;
92 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsInputStream;
93 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream;
94 import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo;
95 import org.whispersystems.signalservice.api.messages.multidevice.RequestMessage;
96 import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
97 import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
98 import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
99 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
100 import org.whispersystems.signalservice.api.push.ContactTokenDetails;
101 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
102 import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
103 import org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException;
104 import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
105 import org.whispersystems.signalservice.api.util.InvalidNumberException;
106 import org.whispersystems.signalservice.api.util.SleepTimer;
107 import org.whispersystems.signalservice.api.util.StreamDetails;
108 import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
109 import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
110 import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
111 import org.whispersystems.signalservice.internal.util.Hex;
112 import org.whispersystems.util.Base64;
113
114 import java.io.File;
115 import java.io.FileInputStream;
116 import java.io.FileNotFoundException;
117 import java.io.FileOutputStream;
118 import java.io.IOException;
119 import java.io.InputStream;
120 import java.io.OutputStream;
121 import java.net.URI;
122 import java.net.URISyntaxException;
123 import java.net.URLEncoder;
124 import java.nio.file.Files;
125 import java.nio.file.Paths;
126 import java.nio.file.StandardCopyOption;
127 import java.util.ArrayList;
128 import java.util.Arrays;
129 import java.util.Collection;
130 import java.util.Collections;
131 import java.util.Date;
132 import java.util.HashSet;
133 import java.util.LinkedList;
134 import java.util.List;
135 import java.util.Locale;
136 import java.util.Map;
137 import java.util.Objects;
138 import java.util.Set;
139 import java.util.UUID;
140 import java.util.concurrent.TimeUnit;
141 import java.util.concurrent.TimeoutException;
142 import java.util.zip.ZipEntry;
143 import java.util.zip.ZipFile;
144
145 public class Manager implements Signal {
146
147 private static final SignalServiceProfile.Capabilities capabilities = new SignalServiceProfile.Capabilities(false, false);
148
149 private final String settingsPath;
150 private final String dataPath;
151 private final String attachmentsPath;
152 private final String avatarsPath;
153 private final SleepTimer timer = new UptimeSleepTimer();
154
155 private SignalAccount account;
156 private String username;
157 private SignalServiceAccountManager accountManager;
158 private SignalServiceMessagePipe messagePipe = null;
159 private SignalServiceMessagePipe unidentifiedMessagePipe = null;
160
161 public Manager(String username, String settingsPath) {
162 this.username = username;
163 this.settingsPath = settingsPath;
164 this.dataPath = this.settingsPath + "/data";
165 this.attachmentsPath = this.settingsPath + "/attachments";
166 this.avatarsPath = this.settingsPath + "/avatars";
167
168 }
169
170 public String getUsername() {
171 return username;
172 }
173
174 public SignalServiceAddress getSelfAddress() {
175 return account.getSelfAddress();
176 }
177
178 private SignalServiceAccountManager getSignalServiceAccountManager() {
179 return new SignalServiceAccountManager(BaseConfig.serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer);
180 }
181
182 private IdentityKey getIdentity() {
183 return account.getSignalProtocolStore().getIdentityKeyPair().getPublicKey();
184 }
185
186 public int getDeviceId() {
187 return account.getDeviceId();
188 }
189
190 private String getMessageCachePath() {
191 return this.dataPath + "/" + username + ".d/msg-cache";
192 }
193
194 private String getMessageCachePath(String sender) {
195 return getMessageCachePath() + "/" + sender.replace("/", "_");
196 }
197
198 private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException {
199 String cachePath = getMessageCachePath(sender);
200 IOUtils.createPrivateDirectories(cachePath);
201 return new File(cachePath + "/" + now + "_" + timestamp);
202 }
203
204 public boolean userHasKeys() {
205 return account != null && account.getSignalProtocolStore() != null;
206 }
207
208 public void init() throws IOException {
209 if (!SignalAccount.userExists(dataPath, username)) {
210 return;
211 }
212 account = SignalAccount.load(dataPath, username);
213
214 migrateLegacyConfigs();
215
216 accountManager = getSignalServiceAccountManager();
217 if (account.isRegistered()) {
218 if (accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) {
219 refreshPreKeys();
220 account.save();
221 }
222 if (account.getUuid() == null) {
223 account.setUuid(accountManager.getOwnUuid());
224 account.save();
225 }
226 }
227 }
228
229 private void migrateLegacyConfigs() {
230 // Copy group avatars that were previously stored in the attachments folder
231 // to the new avatar folder
232 if (JsonGroupStore.groupsWithLegacyAvatarId.size() > 0) {
233 for (GroupInfo g : JsonGroupStore.groupsWithLegacyAvatarId) {
234 File avatarFile = getGroupAvatarFile(g.groupId);
235 File attachmentFile = getAttachmentFile(g.getAvatarId());
236 if (!avatarFile.exists() && attachmentFile.exists()) {
237 try {
238 IOUtils.createPrivateDirectories(avatarsPath);
239 Files.copy(attachmentFile.toPath(), avatarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
240 } catch (Exception e) {
241 // Ignore
242 }
243 }
244 }
245 JsonGroupStore.groupsWithLegacyAvatarId.clear();
246 account.save();
247 }
248 if (account.getProfileKey() == null) {
249 // Old config file, creating new profile key
250 account.setProfileKey(KeyUtils.createProfileKey());
251 account.save();
252 }
253 }
254
255 private void createNewIdentity() throws IOException {
256 IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
257 int registrationId = KeyHelper.generateRegistrationId(false);
258 if (username == null) {
259 account = SignalAccount.createTemporaryAccount(identityKey, registrationId);
260 } else {
261 ProfileKey profileKey = KeyUtils.createProfileKey();
262 account = SignalAccount.create(dataPath, username, identityKey, registrationId, profileKey);
263 account.save();
264 }
265 }
266
267 public boolean isRegistered() {
268 return account != null && account.isRegistered();
269 }
270
271 public void register(boolean voiceVerification) throws IOException {
272 if (account == null) {
273 createNewIdentity();
274 }
275 account.setPassword(KeyUtils.createPassword());
276 account.setUuid(null);
277 accountManager = getSignalServiceAccountManager();
278
279 if (voiceVerification) {
280 accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.absent(), Optional.absent());
281 } else {
282 accountManager.requestSmsVerificationCode(false, Optional.absent(), Optional.absent());
283 }
284
285 account.setRegistered(false);
286 account.save();
287 }
288
289 public void updateAccountAttributes() throws IOException {
290 accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), account.getRegistrationLock(), getSelfUnidentifiedAccessKey(), false, capabilities);
291 }
292
293 public void setProfileName(String name) throws IOException {
294 accountManager.setProfileName(account.getProfileKey(), name);
295 }
296
297 public void setProfileAvatar(File avatar) throws IOException {
298 final StreamDetails streamDetails = Utils.createStreamDetailsFromFile(avatar);
299 accountManager.setProfileAvatar(account.getProfileKey(), streamDetails);
300 streamDetails.getStream().close();
301 }
302
303 public void removeProfileAvatar() throws IOException {
304 accountManager.setProfileAvatar(account.getProfileKey(), null);
305 }
306
307 public void unregister() throws IOException {
308 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
309 // If this is the master device, other users can't send messages to this number anymore.
310 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
311 accountManager.setGcmId(Optional.absent());
312
313 account.setRegistered(false);
314 account.save();
315 }
316
317 public String getDeviceLinkUri() throws TimeoutException, IOException {
318 if (account == null) {
319 createNewIdentity();
320 }
321 account.setPassword(KeyUtils.createPassword());
322 accountManager = getSignalServiceAccountManager();
323 String uuid = accountManager.getNewDeviceUuid();
324
325 return Utils.createDeviceLinkUri(new Utils.DeviceLinkInfo(uuid, getIdentity().getPublicKey()));
326 }
327
328 public void finishDeviceLink(String deviceName) throws IOException, InvalidKeyException, TimeoutException, UserAlreadyExists {
329 account.setSignalingKey(KeyUtils.createSignalingKey());
330 SignalServiceAccountManager.NewDeviceRegistrationReturn ret = accountManager.finishNewDeviceRegistration(account.getSignalProtocolStore().getIdentityKeyPair(), account.getSignalingKey(), false, true, account.getSignalProtocolStore().getLocalRegistrationId(), deviceName);
331
332 username = ret.getNumber();
333 // TODO do this check before actually registering
334 if (SignalAccount.userExists(dataPath, username)) {
335 throw new UserAlreadyExists(username, SignalAccount.getFileName(dataPath, username));
336 }
337
338 // Create new account with the synced identity
339 byte[] profileKeyBytes = ret.getProfileKey();
340 ProfileKey profileKey;
341 if (profileKeyBytes == null) {
342 profileKey = KeyUtils.createProfileKey();
343 } else {
344 try {
345 profileKey = new ProfileKey(profileKeyBytes);
346 } catch (InvalidInputException e) {
347 throw new IOException("Received invalid profileKey", e);
348 }
349 }
350 account = SignalAccount.createLinkedAccount(dataPath, username, ret.getUuid(), account.getPassword(), ret.getDeviceId(), ret.getIdentity(), account.getSignalProtocolStore().getLocalRegistrationId(), account.getSignalingKey(), profileKey);
351
352 refreshPreKeys();
353
354 requestSyncGroups();
355 requestSyncContacts();
356 requestSyncBlocked();
357 requestSyncConfiguration();
358
359 account.save();
360 }
361
362 public List<DeviceInfo> getLinkedDevices() throws IOException {
363 List<DeviceInfo> devices = accountManager.getDevices();
364 account.setMultiDevice(devices.size() > 1);
365 account.save();
366 return devices;
367 }
368
369 public void removeLinkedDevices(int deviceId) throws IOException {
370 accountManager.removeDevice(deviceId);
371 List<DeviceInfo> devices = accountManager.getDevices();
372 account.setMultiDevice(devices.size() > 1);
373 account.save();
374 }
375
376 public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
377 Utils.DeviceLinkInfo info = Utils.parseDeviceLinkUri(linkUri);
378
379 addDevice(info.deviceIdentifier, info.deviceKey);
380 }
381
382 private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
383 IdentityKeyPair identityKeyPair = account.getSignalProtocolStore().getIdentityKeyPair();
384 String verificationCode = accountManager.getNewDeviceVerificationCode();
385
386 accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, Optional.of(account.getProfileKey().serialize()), verificationCode);
387 account.setMultiDevice(true);
388 account.save();
389 }
390
391 private List<PreKeyRecord> generatePreKeys() {
392 List<PreKeyRecord> records = new ArrayList<>(BaseConfig.PREKEY_BATCH_SIZE);
393
394 final int offset = account.getPreKeyIdOffset();
395 for (int i = 0; i < BaseConfig.PREKEY_BATCH_SIZE; i++) {
396 int preKeyId = (offset + i) % Medium.MAX_VALUE;
397 ECKeyPair keyPair = Curve.generateKeyPair();
398 PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
399
400 records.add(record);
401 }
402
403 account.addPreKeys(records);
404 account.save();
405
406 return records;
407 }
408
409 private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
410 try {
411 ECKeyPair keyPair = Curve.generateKeyPair();
412 byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
413 SignedPreKeyRecord record = new SignedPreKeyRecord(account.getNextSignedPreKeyId(), System.currentTimeMillis(), keyPair, signature);
414
415 account.addSignedPreKey(record);
416 account.save();
417
418 return record;
419 } catch (InvalidKeyException e) {
420 throw new AssertionError(e);
421 }
422 }
423
424 public void verifyAccount(String verificationCode, String pin) throws IOException {
425 verificationCode = verificationCode.replace("-", "");
426 account.setSignalingKey(KeyUtils.createSignalingKey());
427 // TODO make unrestricted unidentified access configurable
428 UUID uuid = accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, null, getSelfUnidentifiedAccessKey(), false, capabilities);
429
430 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
431 account.setRegistered(true);
432 account.setUuid(uuid);
433 account.setRegistrationLockPin(pin);
434
435 refreshPreKeys();
436 account.save();
437 }
438
439 public void setRegistrationLockPin(Optional<String> pin) throws IOException {
440 if (pin.isPresent()) {
441 account.setRegistrationLockPin(pin.get());
442 throw new RuntimeException("Not implemented anymore, will be replaced with KBS");
443 } else {
444 account.setRegistrationLockPin(null);
445 accountManager.removeV1Pin();
446 }
447 account.save();
448 }
449
450 private void refreshPreKeys() throws IOException {
451 List<PreKeyRecord> oneTimePreKeys = generatePreKeys();
452 final IdentityKeyPair identityKeyPair = account.getSignalProtocolStore().getIdentityKeyPair();
453 SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(identityKeyPair);
454
455 accountManager.setPreKeys(getIdentity(), signedPreKeyRecord, oneTimePreKeys);
456 }
457
458 private SignalServiceMessageReceiver getMessageReceiver() {
459 return new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer);
460 }
461
462 private SignalServiceMessageSender getMessageSender() {
463 return new SignalServiceMessageSender(BaseConfig.serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(),
464 account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent());
465 }
466
467 private SignalServiceProfile getRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess) throws IOException {
468 SignalServiceMessagePipe pipe = unidentifiedMessagePipe != null && unidentifiedAccess.isPresent() ? unidentifiedMessagePipe
469 : messagePipe;
470
471 if (pipe != null) {
472 try {
473 return pipe.getProfile(address, Optional.absent(), unidentifiedAccess, SignalServiceProfile.RequestType.PROFILE).getProfile();
474 } catch (IOException ignored) {
475 }
476 }
477
478 SignalServiceMessageReceiver receiver = getMessageReceiver();
479 try {
480 return receiver.retrieveProfile(address, Optional.absent(), unidentifiedAccess, SignalServiceProfile.RequestType.PROFILE).getProfile();
481 } catch (VerificationFailedException e) {
482 throw new AssertionError(e);
483 }
484 }
485
486 private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException {
487 File file = getGroupAvatarFile(groupId);
488 if (!file.exists()) {
489 return Optional.absent();
490 }
491
492 return Optional.of(Utils.createAttachment(file));
493 }
494
495 private Optional<SignalServiceAttachmentStream> createContactAvatarAttachment(String number) throws IOException {
496 File file = getContactAvatarFile(number);
497 if (!file.exists()) {
498 return Optional.absent();
499 }
500
501 return Optional.of(Utils.createAttachment(file));
502 }
503
504 private GroupInfo getGroupForSending(byte[] groupId) throws GroupNotFoundException, NotAGroupMemberException {
505 GroupInfo g = account.getGroupStore().getGroup(groupId);
506 if (g == null) {
507 throw new GroupNotFoundException(groupId);
508 }
509 if (!g.isMember(account.getSelfAddress())) {
510 throw new NotAGroupMemberException(groupId, g.name);
511 }
512 return g;
513 }
514
515 public List<GroupInfo> getGroups() {
516 return account.getGroupStore().getGroups();
517 }
518
519 @Override
520 public void sendGroupMessage(String messageText, List<String> attachments,
521 byte[] groupId)
522 throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
523 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
524 if (attachments != null) {
525 messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
526 }
527 if (groupId != null) {
528 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
529 .withId(groupId)
530 .build();
531 messageBuilder.asGroupMessage(group);
532 }
533
534 final GroupInfo g = getGroupForSending(groupId);
535
536 messageBuilder.withExpiration(g.messageExpirationTime);
537
538 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
539 }
540
541 public void sendGroupMessageReaction(String emoji, boolean remove, SignalServiceAddress targetAuthor,
542 long targetSentTimestamp, byte[] groupId)
543 throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
544 SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, targetAuthor, targetSentTimestamp);
545 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
546 .withReaction(reaction);
547 if (groupId != null) {
548 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
549 .withId(groupId)
550 .build();
551 messageBuilder.asGroupMessage(group);
552 }
553 final GroupInfo g = getGroupForSending(groupId);
554 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
555 }
556
557 public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions {
558 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
559 .withId(groupId)
560 .build();
561
562 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
563 .asGroupMessage(group);
564
565 final GroupInfo g = getGroupForSending(groupId);
566 g.removeMember(account.getSelfAddress());
567 account.getGroupStore().updateGroup(g);
568
569 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
570 }
571
572 private byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<SignalServiceAddress> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
573 GroupInfo g;
574 if (groupId == null) {
575 // Create new group
576 g = new GroupInfo(KeyUtils.createGroupId());
577 g.addMembers(Collections.singleton(account.getSelfAddress()));
578 } else {
579 g = getGroupForSending(groupId);
580 }
581
582 if (name != null) {
583 g.name = name;
584 }
585
586 if (members != null) {
587 final Set<String> newE164Members = new HashSet<>();
588 for (SignalServiceAddress member : members) {
589 if (g.isMember(member) || !member.getNumber().isPresent()) {
590 continue;
591 }
592 newE164Members.add(member.getNumber().get());
593 }
594
595 final List<ContactTokenDetails> contacts = accountManager.getContacts(newE164Members);
596 if (contacts.size() != newE164Members.size()) {
597 // Some of the new members are not registered on Signal
598 for (ContactTokenDetails contact : contacts) {
599 newE164Members.remove(contact.getNumber());
600 }
601 System.err.println("Failed to add members " + Util.join(", ", newE164Members) + " to group: Not registered on Signal");
602 System.err.println("Aborting…");
603 System.exit(1);
604 }
605
606 g.addMembers(members);
607 }
608
609 if (avatarFile != null) {
610 IOUtils.createPrivateDirectories(avatarsPath);
611 File aFile = getGroupAvatarFile(g.groupId);
612 Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
613 }
614
615 account.getGroupStore().updateGroup(g);
616
617 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
618
619 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
620 return g.groupId;
621 }
622
623 private void sendUpdateGroupMessage(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions {
624 if (groupId == null) {
625 return;
626 }
627 GroupInfo g = getGroupForSending(groupId);
628
629 if (!g.isMember(recipient)) {
630 return;
631 }
632
633 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
634
635 // Send group message only to the recipient who requested it
636 sendMessageLegacy(messageBuilder, Collections.singleton(recipient));
637 }
638
639 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) {
640 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
641 .withId(g.groupId)
642 .withName(g.name)
643 .withMembers(new ArrayList<>(g.getMembers()));
644
645 File aFile = getGroupAvatarFile(g.groupId);
646 if (aFile.exists()) {
647 try {
648 group.withAvatar(Utils.createAttachment(aFile));
649 } catch (IOException e) {
650 throw new AttachmentInvalidException(aFile.toString(), e);
651 }
652 }
653
654 return SignalServiceDataMessage.newBuilder()
655 .asGroupMessage(group.build())
656 .withExpiration(g.messageExpirationTime);
657 }
658
659 private void sendGroupInfoRequest(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions {
660 if (groupId == null) {
661 return;
662 }
663
664 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO)
665 .withId(groupId);
666
667 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
668 .asGroupMessage(group.build());
669
670 // Send group info request message to the recipient who sent us a message with this groupId
671 sendMessageLegacy(messageBuilder, Collections.singleton(recipient));
672 }
673
674 private void sendReceipt(SignalServiceAddress remoteAddress, long messageId) throws IOException, UntrustedIdentityException {
675 SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.DELIVERY,
676 Collections.singletonList(messageId),
677 System.currentTimeMillis());
678
679 getMessageSender().sendReceipt(remoteAddress, getAccessFor(remoteAddress), receiptMessage);
680 }
681
682 @Override
683 public void sendMessage(String message, List<String> attachments, String recipient)
684 throws EncapsulatedExceptions, AttachmentInvalidException, IOException, InvalidNumberException {
685 List<String> recipients = new ArrayList<>(1);
686 recipients.add(recipient);
687 sendMessage(message, attachments, recipients);
688 }
689
690 @Override
691 public void sendMessage(String messageText, List<String> attachments,
692 List<String> recipients)
693 throws IOException, EncapsulatedExceptions, AttachmentInvalidException, InvalidNumberException {
694 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
695 if (attachments != null) {
696 List<SignalServiceAttachment> attachmentStreams = Utils.getSignalServiceAttachments(attachments);
697
698 // Upload attachments here, so we only upload once even for multiple recipients
699 SignalServiceMessageSender messageSender = getMessageSender();
700 List<SignalServiceAttachment> attachmentPointers = new ArrayList<>(attachmentStreams.size());
701 for (SignalServiceAttachment attachment : attachmentStreams) {
702 if (attachment.isStream()) {
703 attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
704 } else if (attachment.isPointer()) {
705 attachmentPointers.add(attachment.asPointer());
706 }
707 }
708
709 messageBuilder.withAttachments(attachmentPointers);
710 }
711 sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
712 }
713
714 public void sendMessageReaction(String emoji, boolean remove, SignalServiceAddress targetAuthor,
715 long targetSentTimestamp, List<String> recipients)
716 throws IOException, EncapsulatedExceptions, AttachmentInvalidException, InvalidNumberException {
717 SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, targetAuthor, targetSentTimestamp);
718 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
719 .withReaction(reaction);
720 sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
721 }
722
723 @Override
724 public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions, InvalidNumberException {
725 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
726 .asEndSessionMessage();
727
728 sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
729 }
730
731 @Override
732 public String getContactName(String number) throws InvalidNumberException {
733 String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
734 ContactInfo contact = account.getContactStore().getContact(new SignalServiceAddress(null, canonicalizedNumber));
735 if (contact == null) {
736 return "";
737 } else {
738 return contact.name;
739 }
740 }
741
742 @Override
743 public void setContactName(String number, String name) throws InvalidNumberException {
744 String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
745 final SignalServiceAddress address = new SignalServiceAddress(null, canonicalizedNumber);
746 ContactInfo contact = account.getContactStore().getContact(address);
747 if (contact == null) {
748 contact = new ContactInfo(address);
749 System.err.println("Add contact " + canonicalizedNumber + " named " + name);
750 } else {
751 System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
752 }
753 contact.name = name;
754 account.getContactStore().updateContact(contact);
755 account.save();
756 }
757
758 @Override
759 public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
760 number = Utils.canonicalizeNumber(number, account.getUsername());
761 final SignalServiceAddress address = new SignalServiceAddress(null, number);
762 ContactInfo contact = account.getContactStore().getContact(address);
763 if (contact == null) {
764 contact = new ContactInfo(address);
765 System.err.println("Adding and " + (blocked ? "blocking" : "unblocking") + " contact " + number);
766 } else {
767 System.err.println((blocked ? "Blocking" : "Unblocking") + " contact " + number);
768 }
769 contact.blocked = blocked;
770 account.getContactStore().updateContact(contact);
771 account.save();
772 }
773
774 @Override
775 public void setGroupBlocked(final byte[] groupId, final boolean blocked) throws GroupNotFoundException {
776 GroupInfo group = getGroup(groupId);
777 if (group == null) {
778 throw new GroupNotFoundException(groupId);
779 } else {
780 System.err.println((blocked ? "Blocking" : "Unblocking") + " group " + Base64.encodeBytes(groupId));
781 group.blocked = blocked;
782 account.getGroupStore().updateGroup(group);
783 account.save();
784 }
785 }
786
787 @Override
788 public List<byte[]> getGroupIds() {
789 List<GroupInfo> groups = getGroups();
790 List<byte[]> ids = new ArrayList<>(groups.size());
791 for (GroupInfo group : groups) {
792 ids.add(group.groupId);
793 }
794 return ids;
795 }
796
797 @Override
798 public String getGroupName(byte[] groupId) {
799 GroupInfo group = getGroup(groupId);
800 if (group == null) {
801 return "";
802 } else {
803 return group.name;
804 }
805 }
806
807 @Override
808 public List<String> getGroupMembers(byte[] groupId) {
809 GroupInfo group = getGroup(groupId);
810 if (group == null) {
811 return Collections.emptyList();
812 } else {
813 return new ArrayList<>(group.getMembersE164());
814 }
815 }
816
817 @Override
818 public byte[] updateGroup(byte[] groupId, String name, List<String> members, String avatar) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException {
819 if (groupId.length == 0) {
820 groupId = null;
821 }
822 if (name.isEmpty()) {
823 name = null;
824 }
825 if (members.size() == 0) {
826 members = null;
827 }
828 if (avatar.isEmpty()) {
829 avatar = null;
830 }
831 return sendUpdateGroupMessage(groupId, name, members == null ? null : getSignalServiceAddresses(members), avatar);
832 }
833
834 /**
835 * Change the expiration timer for a contact
836 */
837 public void setExpirationTimer(SignalServiceAddress address, int messageExpirationTimer) {
838 ContactInfo c = account.getContactStore().getContact(address);
839 c.messageExpirationTime = messageExpirationTimer;
840 account.getContactStore().updateContact(c);
841 }
842
843 /**
844 * Change the expiration timer for a group
845 */
846 public void setExpirationTimer(byte[] groupId, int messageExpirationTimer) {
847 GroupInfo g = account.getGroupStore().getGroup(groupId);
848 g.messageExpirationTime = messageExpirationTimer;
849 account.getGroupStore().updateGroup(g);
850 }
851
852 /**
853 * Upload the sticker pack from path.
854 *
855 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
856 * @return if successful, returns the URL to install the sticker pack in the signal app
857 */
858 public String uploadStickerPack(String path) throws IOException, StickerPackInvalidException {
859 SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
860
861 SignalServiceMessageSender messageSender = getMessageSender();
862
863 byte[] packKey = KeyUtils.createStickerUploadKey();
864 String packId = messageSender.uploadStickerManifest(manifest, packKey);
865
866 try {
867 return new URI("https", "signal.art", "/addstickers/", "pack_id=" + URLEncoder.encode(packId, "utf-8") + "&pack_key=" + URLEncoder.encode(Hex.toStringCondensed(packKey), "utf-8"))
868 .toString();
869 } catch (URISyntaxException e) {
870 throw new AssertionError(e);
871 }
872 }
873
874 private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload(final String path) throws IOException, StickerPackInvalidException {
875 ZipFile zip = null;
876 String rootPath = null;
877
878 final File file = new File(path);
879 if (file.getName().endsWith(".zip")) {
880 zip = new ZipFile(file);
881 } else if (file.getName().equals("manifest.json")) {
882 rootPath = file.getParent();
883 } else {
884 throw new StickerPackInvalidException("Could not find manifest.json");
885 }
886
887 JsonStickerPack pack = parseStickerPack(rootPath, zip);
888
889 if (pack.stickers == null) {
890 throw new StickerPackInvalidException("Must set a 'stickers' field.");
891 }
892
893 if (pack.stickers.isEmpty()) {
894 throw new StickerPackInvalidException("Must include stickers.");
895 }
896
897 List<StickerInfo> stickers = new ArrayList<>(pack.stickers.size());
898 for (JsonStickerPack.JsonSticker sticker : pack.stickers) {
899 if (sticker.file == null) {
900 throw new StickerPackInvalidException("Must set a 'file' field on each sticker.");
901 }
902
903 Pair<InputStream, Long> data;
904 try {
905 data = getInputStreamAndLength(rootPath, zip, sticker.file);
906 } catch (IOException ignored) {
907 throw new StickerPackInvalidException("Could not find find " + sticker.file);
908 }
909
910 StickerInfo stickerInfo = new StickerInfo(data.first(), data.second(), Optional.fromNullable(sticker.emoji).or(""));
911 stickers.add(stickerInfo);
912 }
913
914 StickerInfo cover = null;
915 if (pack.cover != null) {
916 if (pack.cover.file == null) {
917 throw new StickerPackInvalidException("Must set a 'file' field on the cover.");
918 }
919
920 Pair<InputStream, Long> data;
921 try {
922 data = getInputStreamAndLength(rootPath, zip, pack.cover.file);
923 } catch (IOException ignored) {
924 throw new StickerPackInvalidException("Could not find find " + pack.cover.file);
925 }
926
927 cover = new StickerInfo(data.first(), data.second(), Optional.fromNullable(pack.cover.emoji).or(""));
928 }
929
930 return new SignalServiceStickerManifestUpload(
931 pack.title,
932 pack.author,
933 cover,
934 stickers);
935 }
936
937 private static JsonStickerPack parseStickerPack(String rootPath, ZipFile zip) throws IOException {
938 InputStream inputStream;
939 if (zip != null) {
940 inputStream = zip.getInputStream(zip.getEntry("manifest.json"));
941 } else {
942 inputStream = new FileInputStream((new File(rootPath, "manifest.json")));
943 }
944 return new ObjectMapper().readValue(inputStream, JsonStickerPack.class);
945 }
946
947 private static Pair<InputStream, Long> getInputStreamAndLength(final String rootPath, final ZipFile zip, final String subfile) throws IOException {
948 if (zip != null) {
949 final ZipEntry entry = zip.getEntry(subfile);
950 return new Pair<>(zip.getInputStream(entry), entry.getSize());
951 } else {
952 final File file = new File(rootPath, subfile);
953 return new Pair<>(new FileInputStream(file), file.length());
954 }
955 }
956
957 private void requestSyncGroups() throws IOException {
958 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.GROUPS).build();
959 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
960 try {
961 sendSyncMessage(message);
962 } catch (UntrustedIdentityException e) {
963 e.printStackTrace();
964 }
965 }
966
967 private void requestSyncContacts() throws IOException {
968 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONTACTS).build();
969 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
970 try {
971 sendSyncMessage(message);
972 } catch (UntrustedIdentityException e) {
973 e.printStackTrace();
974 }
975 }
976
977 private void requestSyncBlocked() throws IOException {
978 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.BLOCKED).build();
979 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
980 try {
981 sendSyncMessage(message);
982 } catch (UntrustedIdentityException e) {
983 e.printStackTrace();
984 }
985 }
986
987 private void requestSyncConfiguration() throws IOException {
988 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONFIGURATION).build();
989 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
990 try {
991 sendSyncMessage(message);
992 } catch (UntrustedIdentityException e) {
993 e.printStackTrace();
994 }
995 }
996
997 private byte[] getSenderCertificate() throws IOException {
998 byte[] certificate = accountManager.getSenderCertificate();
999 // TODO cache for a day
1000 return certificate;
1001 }
1002
1003 private byte[] getSelfUnidentifiedAccessKey() {
1004 return UnidentifiedAccess.deriveAccessKeyFrom(account.getProfileKey());
1005 }
1006
1007 private static SignalProfile decryptProfile(SignalServiceProfile encryptedProfile, ProfileKey profileKey) throws IOException {
1008 ProfileCipher profileCipher = new ProfileCipher(profileKey);
1009 try {
1010 return new SignalProfile(
1011 encryptedProfile.getIdentityKey(),
1012 encryptedProfile.getName() == null ? null : new String(profileCipher.decryptName(Base64.decode(encryptedProfile.getName()))),
1013 encryptedProfile.getAvatar(),
1014 encryptedProfile.getUnidentifiedAccess() == null || !profileCipher.verifyUnidentifiedAccess(Base64.decode(encryptedProfile.getUnidentifiedAccess())) ? null : encryptedProfile.getUnidentifiedAccess(),
1015 encryptedProfile.isUnrestrictedUnidentifiedAccess()
1016 );
1017 } catch (InvalidCiphertextException e) {
1018 return null;
1019 }
1020 }
1021
1022 private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) throws IOException {
1023 ContactInfo contact = account.getContactStore().getContact(recipient);
1024 if (contact == null || contact.profileKey == null) {
1025 return null;
1026 }
1027 ProfileKey theirProfileKey;
1028 try {
1029 theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey));
1030 } catch (InvalidInputException e) {
1031 throw new AssertionError(e);
1032 }
1033 SignalProfile targetProfile = decryptProfile(getRecipientProfile(recipient, Optional.absent()), theirProfileKey);
1034
1035 if (targetProfile == null || targetProfile.getUnidentifiedAccess() == null) {
1036 return null;
1037 }
1038
1039 if (targetProfile.isUnrestrictedUnidentifiedAccess()) {
1040 return KeyUtils.createUnrestrictedUnidentifiedAccess();
1041 }
1042
1043 return UnidentifiedAccess.deriveAccessKeyFrom(theirProfileKey);
1044 }
1045
1046 private Optional<UnidentifiedAccessPair> getAccessForSync() throws IOException {
1047 byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
1048 byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();
1049
1050 if (selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
1051 return Optional.absent();
1052 }
1053
1054 try {
1055 return Optional.of(new UnidentifiedAccessPair(
1056 new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
1057 new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
1058 ));
1059 } catch (InvalidCertificateException e) {
1060 return Optional.absent();
1061 }
1062 }
1063
1064 private List<Optional<UnidentifiedAccessPair>> getAccessFor(Collection<SignalServiceAddress> recipients) throws IOException {
1065 List<Optional<UnidentifiedAccessPair>> result = new ArrayList<>(recipients.size());
1066 for (SignalServiceAddress recipient : recipients) {
1067 result.add(getAccessFor(recipient));
1068 }
1069 return result;
1070 }
1071
1072 private Optional<UnidentifiedAccessPair> getAccessFor(SignalServiceAddress recipient) throws IOException {
1073 byte[] recipientUnidentifiedAccessKey = getTargetUnidentifiedAccessKey(recipient);
1074 byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
1075 byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();
1076
1077 if (recipientUnidentifiedAccessKey == null || selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
1078 return Optional.absent();
1079 }
1080
1081 try {
1082 return Optional.of(new UnidentifiedAccessPair(
1083 new UnidentifiedAccess(recipientUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
1084 new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
1085 ));
1086 } catch (InvalidCertificateException e) {
1087 return Optional.absent();
1088 }
1089 }
1090
1091 private void sendSyncMessage(SignalServiceSyncMessage message)
1092 throws IOException, UntrustedIdentityException {
1093 SignalServiceMessageSender messageSender = getMessageSender();
1094 try {
1095 messageSender.sendMessage(message, getAccessForSync());
1096 } catch (UntrustedIdentityException e) {
1097 account.getSignalProtocolStore().saveIdentity(e.getIdentifier(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1098 throw e;
1099 }
1100 }
1101
1102 /**
1103 * This method throws an EncapsulatedExceptions exception instead of returning a list of SendMessageResult.
1104 */
1105 private void sendMessageLegacy(SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients)
1106 throws EncapsulatedExceptions, IOException {
1107 List<SendMessageResult> results = sendMessage(messageBuilder, recipients);
1108
1109 List<UntrustedIdentityException> untrustedIdentities = new LinkedList<>();
1110 List<UnregisteredUserException> unregisteredUsers = new LinkedList<>();
1111 List<NetworkFailureException> networkExceptions = new LinkedList<>();
1112
1113 for (SendMessageResult result : results) {
1114 if (result.isUnregisteredFailure()) {
1115 unregisteredUsers.add(new UnregisteredUserException(result.getAddress().getNumber().get(), null));
1116 } else if (result.isNetworkFailure()) {
1117 networkExceptions.add(new NetworkFailureException(result.getAddress().getNumber().get(), null));
1118 } else if (result.getIdentityFailure() != null) {
1119 untrustedIdentities.add(new UntrustedIdentityException("Untrusted", result.getAddress().getNumber().get(), result.getIdentityFailure().getIdentityKey()));
1120 }
1121 }
1122 if (!untrustedIdentities.isEmpty() || !unregisteredUsers.isEmpty() || !networkExceptions.isEmpty()) {
1123 throw new EncapsulatedExceptions(untrustedIdentities, unregisteredUsers, networkExceptions);
1124 }
1125 }
1126
1127 private Collection<SignalServiceAddress> getSignalServiceAddresses(Collection<String> numbers) throws InvalidNumberException {
1128 final Set<SignalServiceAddress> signalServiceAddresses = new HashSet<>(numbers.size());
1129 final String username = account.getUsername();
1130
1131 for (String number : numbers) {
1132 String canonicalizedNumber = Utils.canonicalizeNumber(number, username);
1133 if (canonicalizedNumber.equals(username)) {
1134 signalServiceAddresses.add(account.getSelfAddress());
1135 } else {
1136 SignalServiceAddress address = new SignalServiceAddress(null, canonicalizedNumber);
1137 ContactInfo contact = account.getContactStore().getContact(address);
1138 signalServiceAddresses.add(contact == null
1139 ? address
1140 : contact.getAddress());
1141 }
1142 }
1143 return signalServiceAddresses;
1144 }
1145
1146 private List<SendMessageResult> sendMessage(SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients)
1147 throws IOException {
1148 if (messagePipe == null) {
1149 messagePipe = getMessageReceiver().createMessagePipe();
1150 }
1151 if (unidentifiedMessagePipe == null) {
1152 unidentifiedMessagePipe = getMessageReceiver().createUnidentifiedMessagePipe();
1153 }
1154 SignalServiceDataMessage message = null;
1155 try {
1156 SignalServiceMessageSender messageSender = getMessageSender();
1157
1158 message = messageBuilder.build();
1159 if (message.getGroupInfo().isPresent()) {
1160 try {
1161 final boolean isRecipientUpdate = false;
1162 List<SendMessageResult> result = messageSender.sendMessage(new ArrayList<>(recipients), getAccessFor(recipients), isRecipientUpdate, message);
1163 for (SendMessageResult r : result) {
1164 if (r.getIdentityFailure() != null) {
1165 account.getSignalProtocolStore().saveIdentity(r.getAddress().getNumber().get(), r.getIdentityFailure().getIdentityKey(), TrustLevel.UNTRUSTED);
1166 }
1167 }
1168 return result;
1169 } catch (UntrustedIdentityException e) {
1170 account.getSignalProtocolStore().saveIdentity(e.getIdentifier(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1171 return Collections.emptyList();
1172 }
1173 } else if (recipients.size() == 1 && recipients.contains(account.getSelfAddress())) {
1174 SignalServiceAddress recipient = account.getSelfAddress();
1175 final Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);
1176 SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient),
1177 message.getTimestamp(),
1178 message,
1179 message.getExpiresInSeconds(),
1180 Collections.singletonMap(recipient, unidentifiedAccess.isPresent()),
1181 false);
1182 SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
1183
1184 List<SendMessageResult> results = new ArrayList<>(recipients.size());
1185 try {
1186 messageSender.sendMessage(syncMessage, unidentifiedAccess);
1187 } catch (UntrustedIdentityException e) {
1188 account.getSignalProtocolStore().saveIdentity(e.getIdentifier(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1189 results.add(SendMessageResult.identityFailure(recipient, e.getIdentityKey()));
1190 }
1191 return results;
1192 } else {
1193 // Send to all individually, so sync messages are sent correctly
1194 List<SendMessageResult> results = new ArrayList<>(recipients.size());
1195 for (SignalServiceAddress address : recipients) {
1196 ContactInfo contact = account.getContactStore().getContact(address);
1197 if (contact != null) {
1198 messageBuilder.withExpiration(contact.messageExpirationTime);
1199 messageBuilder.withProfileKey(account.getProfileKey().serialize());
1200 } else {
1201 messageBuilder.withExpiration(0);
1202 messageBuilder.withProfileKey(null);
1203 }
1204 message = messageBuilder.build();
1205 try {
1206 SendMessageResult result = messageSender.sendMessage(address, getAccessFor(address), message);
1207 results.add(result);
1208 } catch (UntrustedIdentityException e) {
1209 account.getSignalProtocolStore().saveIdentity(e.getIdentifier(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1210 results.add(SendMessageResult.identityFailure(address, e.getIdentityKey()));
1211 }
1212 }
1213 return results;
1214 }
1215 } finally {
1216 if (message != null && message.isEndSession()) {
1217 for (SignalServiceAddress recipient : recipients) {
1218 handleEndSession(recipient.getNumber().get());
1219 }
1220 }
1221 account.save();
1222 }
1223 }
1224
1225 private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, ProtocolUntrustedIdentityException, SelfSendException, UnsupportedDataMessageException {
1226 SignalServiceCipher cipher = new SignalServiceCipher(account.getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator());
1227 try {
1228 return cipher.decrypt(envelope);
1229 } catch (ProtocolUntrustedIdentityException e) {
1230 // TODO We don't get the new untrusted identity from ProtocolUntrustedIdentityException anymore ... we need to get it from somewhere else
1231 // account.getSignalProtocolStore().saveIdentity(e.getSender(), e.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
1232 throw e;
1233 }
1234 }
1235
1236 private void handleEndSession(String source) {
1237 account.getSignalProtocolStore().deleteAllSessions(source);
1238 }
1239
1240 private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, SignalServiceAddress source, SignalServiceAddress destination, boolean ignoreAttachments) {
1241 if (message.getGroupInfo().isPresent()) {
1242 SignalServiceGroup groupInfo = message.getGroupInfo().get();
1243 GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId());
1244 switch (groupInfo.getType()) {
1245 case UPDATE:
1246 if (group == null) {
1247 group = new GroupInfo(groupInfo.getGroupId());
1248 }
1249
1250 if (groupInfo.getAvatar().isPresent()) {
1251 SignalServiceAttachment avatar = groupInfo.getAvatar().get();
1252 if (avatar.isPointer()) {
1253 try {
1254 retrieveGroupAvatarAttachment(avatar.asPointer(), group.groupId);
1255 } catch (IOException | InvalidMessageException e) {
1256 System.err.println("Failed to retrieve group avatar (" + avatar.asPointer().getId() + "): " + e.getMessage());
1257 }
1258 }
1259 }
1260
1261 if (groupInfo.getName().isPresent()) {
1262 group.name = groupInfo.getName().get();
1263 }
1264
1265 if (groupInfo.getMembers().isPresent()) {
1266 group.addMembers(groupInfo.getMembers().get());
1267 }
1268
1269 account.getGroupStore().updateGroup(group);
1270 break;
1271 case DELIVER:
1272 if (group == null) {
1273 try {
1274 sendGroupInfoRequest(groupInfo.getGroupId(), source);
1275 } catch (IOException | EncapsulatedExceptions e) {
1276 e.printStackTrace();
1277 }
1278 }
1279 break;
1280 case QUIT:
1281 if (group == null) {
1282 try {
1283 sendGroupInfoRequest(groupInfo.getGroupId(), source);
1284 } catch (IOException | EncapsulatedExceptions e) {
1285 e.printStackTrace();
1286 }
1287 } else {
1288 group.removeMember(source);
1289 account.getGroupStore().updateGroup(group);
1290 }
1291 break;
1292 case REQUEST_INFO:
1293 if (group != null) {
1294 try {
1295 sendUpdateGroupMessage(groupInfo.getGroupId(), source);
1296 } catch (IOException | EncapsulatedExceptions e) {
1297 e.printStackTrace();
1298 } catch (NotAGroupMemberException e) {
1299 // We have left this group, so don't send a group update message
1300 }
1301 }
1302 break;
1303 }
1304 }
1305 if (message.isEndSession()) {
1306 handleEndSession(isSync ? destination.getNumber().get() : source.getNumber().get());
1307 }
1308 if (message.isExpirationUpdate() || message.getBody().isPresent()) {
1309 if (message.getGroupInfo().isPresent()) {
1310 SignalServiceGroup groupInfo = message.getGroupInfo().get();
1311 GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId());
1312 if (group == null) {
1313 group = new GroupInfo(groupInfo.getGroupId());
1314 }
1315 if (group.messageExpirationTime != message.getExpiresInSeconds()) {
1316 group.messageExpirationTime = message.getExpiresInSeconds();
1317 account.getGroupStore().updateGroup(group);
1318 }
1319 } else {
1320 ContactInfo contact = account.getContactStore().getContact(isSync ? destination : source);
1321 if (contact == null) {
1322 contact = new ContactInfo(isSync ? destination : source);
1323 }
1324 if (contact.messageExpirationTime != message.getExpiresInSeconds()) {
1325 contact.messageExpirationTime = message.getExpiresInSeconds();
1326 account.getContactStore().updateContact(contact);
1327 }
1328 }
1329 }
1330 if (message.getAttachments().isPresent() && !ignoreAttachments) {
1331 for (SignalServiceAttachment attachment : message.getAttachments().get()) {
1332 if (attachment.isPointer()) {
1333 try {
1334 retrieveAttachment(attachment.asPointer());
1335 } catch (IOException | InvalidMessageException e) {
1336 System.err.println("Failed to retrieve attachment (" + attachment.asPointer().getId() + "): " + e.getMessage());
1337 }
1338 }
1339 }
1340 }
1341 if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
1342 if (source.matches(account.getSelfAddress())) {
1343 try {
1344 this.account.setProfileKey(new ProfileKey(message.getProfileKey().get()));
1345 } catch (InvalidInputException ignored) {
1346 }
1347 ContactInfo contact = account.getContactStore().getContact(source);
1348 if (contact != null) {
1349 contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
1350 account.getContactStore().updateContact(contact);
1351 }
1352 } else {
1353 ContactInfo contact = account.getContactStore().getContact(source);
1354 if (contact == null) {
1355 contact = new ContactInfo(source);
1356 }
1357 contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
1358 account.getContactStore().updateContact(contact);
1359 }
1360 }
1361 if (message.getPreviews().isPresent()) {
1362 final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();
1363 for (SignalServiceDataMessage.Preview preview : previews) {
1364 if (preview.getImage().isPresent() && preview.getImage().get().isPointer()) {
1365 SignalServiceAttachmentPointer attachment = preview.getImage().get().asPointer();
1366 try {
1367 retrieveAttachment(attachment);
1368 } catch (IOException | InvalidMessageException e) {
1369 System.err.println("Failed to retrieve attachment (" + attachment.getId() + "): " + e.getMessage());
1370 }
1371 }
1372 }
1373 }
1374 }
1375
1376 private void retryFailedReceivedMessages(ReceiveMessageHandler handler, boolean ignoreAttachments) {
1377 final File cachePath = new File(getMessageCachePath());
1378 if (!cachePath.exists()) {
1379 return;
1380 }
1381 for (final File dir : Objects.requireNonNull(cachePath.listFiles())) {
1382 if (!dir.isDirectory()) {
1383 continue;
1384 }
1385
1386 for (final File fileEntry : Objects.requireNonNull(dir.listFiles())) {
1387 if (!fileEntry.isFile()) {
1388 continue;
1389 }
1390 SignalServiceEnvelope envelope;
1391 try {
1392 envelope = Utils.loadEnvelope(fileEntry);
1393 if (envelope == null) {
1394 continue;
1395 }
1396 } catch (IOException e) {
1397 e.printStackTrace();
1398 continue;
1399 }
1400 SignalServiceContent content = null;
1401 if (!envelope.isReceipt()) {
1402 try {
1403 content = decryptMessage(envelope);
1404 } catch (Exception e) {
1405 continue;
1406 }
1407 handleMessage(envelope, content, ignoreAttachments);
1408 }
1409 account.save();
1410 handler.handleMessage(envelope, content, null);
1411 try {
1412 Files.delete(fileEntry.toPath());
1413 } catch (IOException e) {
1414 System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage());
1415 }
1416 }
1417 // Try to delete directory if empty
1418 dir.delete();
1419 }
1420 }
1421
1422 public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, boolean ignoreAttachments, ReceiveMessageHandler handler) throws IOException {
1423 retryFailedReceivedMessages(handler, ignoreAttachments);
1424 final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
1425
1426 try {
1427 if (messagePipe == null) {
1428 messagePipe = messageReceiver.createMessagePipe();
1429 }
1430
1431 while (true) {
1432 SignalServiceEnvelope envelope;
1433 SignalServiceContent content = null;
1434 Exception exception = null;
1435 final long now = new Date().getTime();
1436 try {
1437 envelope = messagePipe.read(timeout, unit, envelope1 -> {
1438 // store message on disk, before acknowledging receipt to the server
1439 try {
1440 File cacheFile = getMessageCacheFile(envelope1.getSourceE164().get(), now, envelope1.getTimestamp());
1441 Utils.storeEnvelope(envelope1, cacheFile);
1442 } catch (IOException e) {
1443 System.err.println("Failed to store encrypted message in disk cache, ignoring: " + e.getMessage());
1444 }
1445 });
1446 } catch (TimeoutException e) {
1447 if (returnOnTimeout)
1448 return;
1449 continue;
1450 } catch (InvalidVersionException e) {
1451 System.err.println("Ignoring error: " + e.getMessage());
1452 continue;
1453 }
1454 if (!envelope.isReceipt()) {
1455 try {
1456 content = decryptMessage(envelope);
1457 } catch (Exception e) {
1458 exception = e;
1459 }
1460 handleMessage(envelope, content, ignoreAttachments);
1461 }
1462 account.save();
1463 if (!isMessageBlocked(envelope, content)) {
1464 handler.handleMessage(envelope, content, exception);
1465 }
1466 if (!(exception instanceof ProtocolUntrustedIdentityException)) {
1467 File cacheFile = null;
1468 try {
1469 cacheFile = getMessageCacheFile(envelope.getSourceE164().get(), now, envelope.getTimestamp());
1470 Files.delete(cacheFile.toPath());
1471 // Try to delete directory if empty
1472 new File(getMessageCachePath()).delete();
1473 } catch (IOException e) {
1474 System.err.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage());
1475 }
1476 }
1477 }
1478 } finally {
1479 if (messagePipe != null) {
1480 messagePipe.shutdown();
1481 messagePipe = null;
1482 }
1483 }
1484 }
1485
1486 private boolean isMessageBlocked(SignalServiceEnvelope envelope, SignalServiceContent content) {
1487 SignalServiceAddress source;
1488 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
1489 source = envelope.getSourceAddress();
1490 } else if (content != null) {
1491 source = content.getSender();
1492 } else {
1493 return false;
1494 }
1495 ContactInfo sourceContact = getContact(source.getNumber().get());
1496 if (sourceContact != null && sourceContact.blocked) {
1497 return true;
1498 }
1499
1500 if (content != null && content.getDataMessage().isPresent()) {
1501 SignalServiceDataMessage message = content.getDataMessage().get();
1502 if (message.getGroupInfo().isPresent()) {
1503 SignalServiceGroup groupInfo = message.getGroupInfo().get();
1504 GroupInfo group = getGroup(groupInfo.getGroupId());
1505 if (groupInfo.getType() == SignalServiceGroup.Type.DELIVER && group != null && group.blocked) {
1506 return true;
1507 }
1508 }
1509 }
1510 return false;
1511 }
1512
1513 private void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) {
1514 if (content != null) {
1515 SignalServiceAddress sender;
1516 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
1517 sender = envelope.getSourceAddress();
1518 } else {
1519 sender = content.getSender();
1520 }
1521 if (content.getDataMessage().isPresent()) {
1522 SignalServiceDataMessage message = content.getDataMessage().get();
1523
1524 if (content.isNeedsReceipt()) {
1525 try {
1526 sendReceipt(sender, message.getTimestamp());
1527 } catch (IOException | UntrustedIdentityException e) {
1528 e.printStackTrace();
1529 }
1530 }
1531
1532 handleSignalServiceDataMessage(message, false, sender, account.getSelfAddress(), ignoreAttachments);
1533 }
1534 if (content.getSyncMessage().isPresent()) {
1535 account.setMultiDevice(true);
1536 SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
1537 if (syncMessage.getSent().isPresent()) {
1538 SentTranscriptMessage message = syncMessage.getSent().get();
1539 handleSignalServiceDataMessage(message.getMessage(), true, sender, message.getDestination().orNull(), ignoreAttachments);
1540 }
1541 if (syncMessage.getRequest().isPresent()) {
1542 RequestMessage rm = syncMessage.getRequest().get();
1543 if (rm.isContactsRequest()) {
1544 try {
1545 sendContacts();
1546 } catch (UntrustedIdentityException | IOException e) {
1547 e.printStackTrace();
1548 }
1549 }
1550 if (rm.isGroupsRequest()) {
1551 try {
1552 sendGroups();
1553 } catch (UntrustedIdentityException | IOException e) {
1554 e.printStackTrace();
1555 }
1556 }
1557 if (rm.isBlockedListRequest()) {
1558 try {
1559 sendBlockedList();
1560 } catch (UntrustedIdentityException | IOException e) {
1561 e.printStackTrace();
1562 }
1563 }
1564 // TODO Handle rm.isConfigurationRequest();
1565 }
1566 if (syncMessage.getGroups().isPresent()) {
1567 File tmpFile = null;
1568 try {
1569 tmpFile = IOUtils.createTempFile();
1570 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)) {
1571 DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream);
1572 DeviceGroup g;
1573 while ((g = s.read()) != null) {
1574 GroupInfo syncGroup = account.getGroupStore().getGroup(g.getId());
1575 if (syncGroup == null) {
1576 syncGroup = new GroupInfo(g.getId());
1577 }
1578 if (g.getName().isPresent()) {
1579 syncGroup.name = g.getName().get();
1580 }
1581 syncGroup.addMembers(g.getMembers());
1582 if (!g.isActive()) {
1583 syncGroup.removeMember(account.getSelfAddress());
1584 } else {
1585 // Add ourself to the member set as it's marked as active
1586 syncGroup.addMembers(Collections.singleton(account.getSelfAddress()));
1587 }
1588 syncGroup.blocked = g.isBlocked();
1589 if (g.getColor().isPresent()) {
1590 syncGroup.color = g.getColor().get();
1591 }
1592
1593 if (g.getAvatar().isPresent()) {
1594 retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId);
1595 }
1596 syncGroup.inboxPosition = g.getInboxPosition().orNull();
1597 syncGroup.archived = g.isArchived();
1598 account.getGroupStore().updateGroup(syncGroup);
1599 }
1600 }
1601 } catch (Exception e) {
1602 e.printStackTrace();
1603 } finally {
1604 if (tmpFile != null) {
1605 try {
1606 Files.delete(tmpFile.toPath());
1607 } catch (IOException e) {
1608 System.err.println("Failed to delete received groups temp file “" + tmpFile + "”: " + e.getMessage());
1609 }
1610 }
1611 }
1612 }
1613 if (syncMessage.getBlockedList().isPresent()) {
1614 final BlockedListMessage blockedListMessage = syncMessage.getBlockedList().get();
1615 for (SignalServiceAddress address : blockedListMessage.getAddresses()) {
1616 if (address.getNumber().isPresent()) {
1617 try {
1618 setContactBlocked(address.getNumber().get(), true);
1619 } catch (InvalidNumberException e) {
1620 e.printStackTrace();
1621 }
1622 }
1623 }
1624 for (byte[] groupId : blockedListMessage.getGroupIds()) {
1625 try {
1626 setGroupBlocked(groupId, true);
1627 } catch (GroupNotFoundException e) {
1628 System.err.println("BlockedListMessage contained groupID that was not found in GroupStore: " + Base64.encodeBytes(groupId));
1629 }
1630 }
1631 }
1632 if (syncMessage.getContacts().isPresent()) {
1633 File tmpFile = null;
1634 try {
1635 tmpFile = IOUtils.createTempFile();
1636 final ContactsMessage contactsMessage = syncMessage.getContacts().get();
1637 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)) {
1638 DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream);
1639 if (contactsMessage.isComplete()) {
1640 account.getContactStore().clear();
1641 }
1642 DeviceContact c;
1643 while ((c = s.read()) != null) {
1644 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
1645 account.setProfileKey(c.getProfileKey().get());
1646 }
1647 ContactInfo contact = account.getContactStore().getContact(c.getAddress());
1648 if (contact == null) {
1649 contact = new ContactInfo(c.getAddress());
1650 }
1651 if (c.getName().isPresent()) {
1652 contact.name = c.getName().get();
1653 }
1654 if (c.getColor().isPresent()) {
1655 contact.color = c.getColor().get();
1656 }
1657 if (c.getProfileKey().isPresent()) {
1658 contact.profileKey = Base64.encodeBytes(c.getProfileKey().get().serialize());
1659 }
1660 if (c.getVerified().isPresent()) {
1661 final VerifiedMessage verifiedMessage = c.getVerified().get();
1662 account.getSignalProtocolStore().saveIdentity(verifiedMessage.getDestination().getNumber().get(), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
1663 }
1664 if (c.getExpirationTimer().isPresent()) {
1665 contact.messageExpirationTime = c.getExpirationTimer().get();
1666 }
1667 contact.blocked = c.isBlocked();
1668 contact.inboxPosition = c.getInboxPosition().orNull();
1669 contact.archived = c.isArchived();
1670 account.getContactStore().updateContact(contact);
1671
1672 if (c.getAvatar().isPresent()) {
1673 retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number);
1674 }
1675 }
1676 }
1677 } catch (Exception e) {
1678 e.printStackTrace();
1679 } finally {
1680 if (tmpFile != null) {
1681 try {
1682 Files.delete(tmpFile.toPath());
1683 } catch (IOException e) {
1684 System.err.println("Failed to delete received contacts temp file “" + tmpFile + "”: " + e.getMessage());
1685 }
1686 }
1687 }
1688 }
1689 if (syncMessage.getVerified().isPresent()) {
1690 final VerifiedMessage verifiedMessage = syncMessage.getVerified().get();
1691 account.getSignalProtocolStore().saveIdentity(verifiedMessage.getDestination().getNumber().get(), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
1692 }
1693 if (syncMessage.getConfiguration().isPresent()) {
1694 // TODO
1695 }
1696 }
1697 }
1698 }
1699
1700 private File getContactAvatarFile(String number) {
1701 return new File(avatarsPath, "contact-" + number);
1702 }
1703
1704 private File retrieveContactAvatarAttachment(SignalServiceAttachment attachment, String number) throws IOException, InvalidMessageException {
1705 IOUtils.createPrivateDirectories(avatarsPath);
1706 if (attachment.isPointer()) {
1707 SignalServiceAttachmentPointer pointer = attachment.asPointer();
1708 return retrieveAttachment(pointer, getContactAvatarFile(number), false);
1709 } else {
1710 SignalServiceAttachmentStream stream = attachment.asStream();
1711 return Utils.retrieveAttachment(stream, getContactAvatarFile(number));
1712 }
1713 }
1714
1715 private File getGroupAvatarFile(byte[] groupId) {
1716 return new File(avatarsPath, "group-" + Base64.encodeBytes(groupId).replace("/", "_"));
1717 }
1718
1719 private File retrieveGroupAvatarAttachment(SignalServiceAttachment attachment, byte[] groupId) throws IOException, InvalidMessageException {
1720 IOUtils.createPrivateDirectories(avatarsPath);
1721 if (attachment.isPointer()) {
1722 SignalServiceAttachmentPointer pointer = attachment.asPointer();
1723 return retrieveAttachment(pointer, getGroupAvatarFile(groupId), false);
1724 } else {
1725 SignalServiceAttachmentStream stream = attachment.asStream();
1726 return Utils.retrieveAttachment(stream, getGroupAvatarFile(groupId));
1727 }
1728 }
1729
1730 public File getAttachmentFile(long attachmentId) {
1731 return new File(attachmentsPath, attachmentId + "");
1732 }
1733
1734 private File retrieveAttachment(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException {
1735 IOUtils.createPrivateDirectories(attachmentsPath);
1736 return retrieveAttachment(pointer, getAttachmentFile(pointer.getId()), true);
1737 }
1738
1739 private File retrieveAttachment(SignalServiceAttachmentPointer pointer, File outputFile, boolean storePreview) throws IOException, InvalidMessageException {
1740 if (storePreview && pointer.getPreview().isPresent()) {
1741 File previewFile = new File(outputFile + ".preview");
1742 try (OutputStream output = new FileOutputStream(previewFile)) {
1743 byte[] preview = pointer.getPreview().get();
1744 output.write(preview, 0, preview.length);
1745 } catch (FileNotFoundException e) {
1746 e.printStackTrace();
1747 return null;
1748 }
1749 }
1750
1751 final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
1752
1753 File tmpFile = IOUtils.createTempFile();
1754 try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, BaseConfig.MAX_ATTACHMENT_SIZE)) {
1755 try (OutputStream output = new FileOutputStream(outputFile)) {
1756 byte[] buffer = new byte[4096];
1757 int read;
1758
1759 while ((read = input.read(buffer)) != -1) {
1760 output.write(buffer, 0, read);
1761 }
1762 } catch (FileNotFoundException e) {
1763 e.printStackTrace();
1764 return null;
1765 }
1766 } finally {
1767 try {
1768 Files.delete(tmpFile.toPath());
1769 } catch (IOException e) {
1770 System.err.println("Failed to delete received attachment temp file “" + tmpFile + "”: " + e.getMessage());
1771 }
1772 }
1773 return outputFile;
1774 }
1775
1776 private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException {
1777 final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
1778 return messageReceiver.retrieveAttachment(pointer, tmpFile, BaseConfig.MAX_ATTACHMENT_SIZE);
1779 }
1780
1781 @Override
1782 public boolean isRemote() {
1783 return false;
1784 }
1785
1786 private void sendGroups() throws IOException, UntrustedIdentityException {
1787 File groupsFile = IOUtils.createTempFile();
1788
1789 try {
1790 try (OutputStream fos = new FileOutputStream(groupsFile)) {
1791 DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
1792 for (GroupInfo record : account.getGroupStore().getGroups()) {
1793 out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
1794 new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId),
1795 record.isMember(account.getSelfAddress()), Optional.of(record.messageExpirationTime),
1796 Optional.fromNullable(record.color), record.blocked, Optional.fromNullable(record.inboxPosition), record.archived));
1797 }
1798 }
1799
1800 if (groupsFile.exists() && groupsFile.length() > 0) {
1801 try (FileInputStream groupsFileStream = new FileInputStream(groupsFile)) {
1802 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
1803 .withStream(groupsFileStream)
1804 .withContentType("application/octet-stream")
1805 .withLength(groupsFile.length())
1806 .build();
1807
1808 sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
1809 }
1810 }
1811 } finally {
1812 try {
1813 Files.delete(groupsFile.toPath());
1814 } catch (IOException e) {
1815 System.err.println("Failed to delete groups temp file “" + groupsFile + "”: " + e.getMessage());
1816 }
1817 }
1818 }
1819
1820 public void sendContacts() throws IOException, UntrustedIdentityException {
1821 File contactsFile = IOUtils.createTempFile();
1822
1823 try {
1824 try (OutputStream fos = new FileOutputStream(contactsFile)) {
1825 DeviceContactsOutputStream out = new DeviceContactsOutputStream(fos);
1826 for (ContactInfo record : account.getContactStore().getContacts()) {
1827 VerifiedMessage verifiedMessage = null;
1828 if (getIdentities().containsKey(record.number)) {
1829 JsonIdentityKeyStore.Identity currentIdentity = null;
1830 for (JsonIdentityKeyStore.Identity id : getIdentities().get(record.number)) {
1831 if (currentIdentity == null || id.getDateAdded().after(currentIdentity.getDateAdded())) {
1832 currentIdentity = id;
1833 }
1834 }
1835 if (currentIdentity != null) {
1836 verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
1837 }
1838 }
1839
1840 ProfileKey profileKey = null;
1841 try {
1842 profileKey = record.profileKey == null ? null : new ProfileKey(Base64.decode(record.profileKey));
1843 } catch (InvalidInputException ignored) {
1844 }
1845 out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name),
1846 createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
1847 Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked,
1848 Optional.of(record.messageExpirationTime),
1849 Optional.fromNullable(record.inboxPosition), record.archived));
1850 }
1851
1852 if (account.getProfileKey() != null) {
1853 // Send our own profile key as well
1854 out.write(new DeviceContact(account.getSelfAddress(),
1855 Optional.absent(), Optional.absent(),
1856 Optional.absent(), Optional.absent(),
1857 Optional.of(account.getProfileKey()),
1858 false, Optional.absent(), Optional.absent(), false));
1859 }
1860 }
1861
1862 if (contactsFile.exists() && contactsFile.length() > 0) {
1863 try (FileInputStream contactsFileStream = new FileInputStream(contactsFile)) {
1864 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
1865 .withStream(contactsFileStream)
1866 .withContentType("application/octet-stream")
1867 .withLength(contactsFile.length())
1868 .build();
1869
1870 sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true)));
1871 }
1872 }
1873 } finally {
1874 try {
1875 Files.delete(contactsFile.toPath());
1876 } catch (IOException e) {
1877 System.err.println("Failed to delete contacts temp file “" + contactsFile + "”: " + e.getMessage());
1878 }
1879 }
1880 }
1881
1882 private void sendBlockedList() throws IOException, UntrustedIdentityException {
1883 List<SignalServiceAddress> addresses = new ArrayList<>();
1884 for (ContactInfo record : account.getContactStore().getContacts()) {
1885 if (record.blocked) {
1886 addresses.add(record.getAddress());
1887 }
1888 }
1889 List<byte[]> groupIds = new ArrayList<>();
1890 for (GroupInfo record : account.getGroupStore().getGroups()) {
1891 if (record.blocked) {
1892 groupIds.add(record.groupId);
1893 }
1894 }
1895 sendSyncMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(addresses, groupIds)));
1896 }
1897
1898 private void sendVerifiedMessage(SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
1899 VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis());
1900 sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
1901 }
1902
1903 public List<ContactInfo> getContacts() {
1904 return account.getContactStore().getContacts();
1905 }
1906
1907 public ContactInfo getContact(String number) {
1908 return account.getContactStore().getContact(new SignalServiceAddress(null, number));
1909 }
1910
1911 public GroupInfo getGroup(byte[] groupId) {
1912 return account.getGroupStore().getGroup(groupId);
1913 }
1914
1915 public Map<String, List<JsonIdentityKeyStore.Identity>> getIdentities() {
1916 return account.getSignalProtocolStore().getIdentities();
1917 }
1918
1919 public Pair<String, List<JsonIdentityKeyStore.Identity>> getIdentities(String number) throws InvalidNumberException {
1920 String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
1921 return new Pair<>(canonicalizedNumber, account.getSignalProtocolStore().getIdentities(canonicalizedNumber));
1922 }
1923
1924 /**
1925 * Trust this the identity with this fingerprint
1926 *
1927 * @param name username of the identity
1928 * @param fingerprint Fingerprint
1929 */
1930 public boolean trustIdentityVerified(String name, byte[] fingerprint) {
1931 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(name);
1932 if (ids == null) {
1933 return false;
1934 }
1935 for (JsonIdentityKeyStore.Identity id : ids) {
1936 if (!Arrays.equals(id.getIdentityKey().serialize(), fingerprint)) {
1937 continue;
1938 }
1939
1940 account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1941 try {
1942 sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1943 } catch (IOException | UntrustedIdentityException e) {
1944 e.printStackTrace();
1945 }
1946 account.save();
1947 return true;
1948 }
1949 return false;
1950 }
1951
1952 /**
1953 * Trust this the identity with this safety number
1954 *
1955 * @param name username of the identity
1956 * @param safetyNumber Safety number
1957 */
1958 public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) {
1959 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(name);
1960 if (ids == null) {
1961 return false;
1962 }
1963 for (JsonIdentityKeyStore.Identity id : ids) {
1964 if (!safetyNumber.equals(computeSafetyNumber(name, id.getIdentityKey()))) {
1965 continue;
1966 }
1967
1968 account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1969 try {
1970 sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1971 } catch (IOException | UntrustedIdentityException e) {
1972 e.printStackTrace();
1973 }
1974 account.save();
1975 return true;
1976 }
1977 return false;
1978 }
1979
1980 /**
1981 * Trust all keys of this identity without verification
1982 *
1983 * @param name username of the identity
1984 */
1985 public boolean trustIdentityAllKeys(String name) {
1986 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(name);
1987 if (ids == null) {
1988 return false;
1989 }
1990 for (JsonIdentityKeyStore.Identity id : ids) {
1991 if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
1992 account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
1993 try {
1994 sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
1995 } catch (IOException | UntrustedIdentityException e) {
1996 e.printStackTrace();
1997 }
1998 }
1999 }
2000 account.save();
2001 return true;
2002 }
2003
2004 public String computeSafetyNumber(String theirUsername, IdentityKey theirIdentityKey) {
2005 return Utils.computeSafetyNumber(account.getUsername(), getIdentity(), theirUsername, theirIdentityKey);
2006 }
2007
2008 public interface ReceiveMessageHandler {
2009
2010 void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, Throwable e);
2011 }
2012 }