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