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