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