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