]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/Manager.java
Fix handling data messages of sync messages
[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.manager.helper.GroupHelper;
22 import org.asamk.signal.manager.helper.ProfileHelper;
23 import org.asamk.signal.manager.helper.UnidentifiedAccessHelper;
24 import org.asamk.signal.storage.SignalAccount;
25 import org.asamk.signal.storage.contacts.ContactInfo;
26 import org.asamk.signal.storage.groups.GroupInfo;
27 import org.asamk.signal.storage.groups.GroupInfoV1;
28 import org.asamk.signal.storage.groups.GroupInfoV2;
29 import org.asamk.signal.storage.profiles.SignalProfile;
30 import org.asamk.signal.storage.profiles.SignalProfileEntry;
31 import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
32 import org.asamk.signal.storage.stickers.Sticker;
33 import org.asamk.signal.util.IOUtils;
34 import org.asamk.signal.util.Util;
35 import org.signal.libsignal.metadata.InvalidMetadataMessageException;
36 import org.signal.libsignal.metadata.InvalidMetadataVersionException;
37 import org.signal.libsignal.metadata.ProtocolDuplicateMessageException;
38 import org.signal.libsignal.metadata.ProtocolInvalidKeyException;
39 import org.signal.libsignal.metadata.ProtocolInvalidKeyIdException;
40 import org.signal.libsignal.metadata.ProtocolInvalidMessageException;
41 import org.signal.libsignal.metadata.ProtocolInvalidVersionException;
42 import org.signal.libsignal.metadata.ProtocolLegacyMessageException;
43 import org.signal.libsignal.metadata.ProtocolNoSessionException;
44 import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
45 import org.signal.libsignal.metadata.SelfSendException;
46 import org.signal.storageservice.protos.groups.GroupChange;
47 import org.signal.storageservice.protos.groups.local.DecryptedGroup;
48 import org.signal.storageservice.protos.groups.local.DecryptedGroupJoinInfo;
49 import org.signal.storageservice.protos.groups.local.DecryptedMember;
50 import org.signal.zkgroup.InvalidInputException;
51 import org.signal.zkgroup.VerificationFailedException;
52 import org.signal.zkgroup.auth.AuthCredentialResponse;
53 import org.signal.zkgroup.groups.GroupMasterKey;
54 import org.signal.zkgroup.groups.GroupSecretParams;
55 import org.signal.zkgroup.profiles.ClientZkProfileOperations;
56 import org.signal.zkgroup.profiles.ProfileKey;
57 import org.signal.zkgroup.profiles.ProfileKeyCredential;
58 import org.whispersystems.libsignal.IdentityKey;
59 import org.whispersystems.libsignal.IdentityKeyPair;
60 import org.whispersystems.libsignal.InvalidKeyException;
61 import org.whispersystems.libsignal.InvalidMessageException;
62 import org.whispersystems.libsignal.InvalidVersionException;
63 import org.whispersystems.libsignal.ecc.Curve;
64 import org.whispersystems.libsignal.ecc.ECKeyPair;
65 import org.whispersystems.libsignal.ecc.ECPublicKey;
66 import org.whispersystems.libsignal.state.PreKeyRecord;
67 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
68 import org.whispersystems.libsignal.util.KeyHelper;
69 import org.whispersystems.libsignal.util.Medium;
70 import org.whispersystems.libsignal.util.Pair;
71 import org.whispersystems.libsignal.util.guava.Optional;
72 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
73 import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
74 import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
75 import org.whispersystems.signalservice.api.SignalServiceMessageSender;
76 import org.whispersystems.signalservice.api.crypto.InvalidCiphertextException;
77 import org.whispersystems.signalservice.api.crypto.ProfileCipher;
78 import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
79 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
80 import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
81 import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
82 import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
83 import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api;
84 import org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString;
85 import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
86 import org.whispersystems.signalservice.api.messages.SendMessageResult;
87 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
88 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
89 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
90 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
91 import org.whispersystems.signalservice.api.messages.SignalServiceContent;
92 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
93 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
94 import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
95 import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2;
96 import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
97 import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload;
98 import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload.StickerInfo;
99 import org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage;
100 import org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage;
101 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContact;
102 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsInputStream;
103 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream;
104 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup;
105 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsInputStream;
106 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream;
107 import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo;
108 import org.whispersystems.signalservice.api.messages.multidevice.RequestMessage;
109 import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
110 import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
111 import org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage;
112 import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
113 import org.whispersystems.signalservice.api.profiles.ProfileAndCredential;
114 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
115 import org.whispersystems.signalservice.api.push.ContactTokenDetails;
116 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
117 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
118 import org.whispersystems.signalservice.api.util.InvalidNumberException;
119 import org.whispersystems.signalservice.api.util.SleepTimer;
120 import org.whispersystems.signalservice.api.util.StreamDetails;
121 import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
122 import org.whispersystems.signalservice.api.util.UuidUtil;
123 import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
124 import org.whispersystems.signalservice.internal.contacts.crypto.Quote;
125 import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException;
126 import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
127 import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
128 import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
129 import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
130 import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
131 import org.whispersystems.signalservice.internal.util.Hex;
132 import org.whispersystems.util.Base64;
133
134 import java.io.Closeable;
135 import java.io.File;
136 import java.io.FileInputStream;
137 import java.io.FileNotFoundException;
138 import java.io.FileOutputStream;
139 import java.io.IOException;
140 import java.io.InputStream;
141 import java.io.OutputStream;
142 import java.net.URI;
143 import java.net.URISyntaxException;
144 import java.net.URLEncoder;
145 import java.nio.charset.StandardCharsets;
146 import java.nio.file.Files;
147 import java.nio.file.Paths;
148 import java.nio.file.StandardCopyOption;
149 import java.security.SignatureException;
150 import java.util.ArrayList;
151 import java.util.Arrays;
152 import java.util.Collection;
153 import java.util.Collections;
154 import java.util.Date;
155 import java.util.HashMap;
156 import java.util.HashSet;
157 import java.util.List;
158 import java.util.Locale;
159 import java.util.Map;
160 import java.util.Objects;
161 import java.util.Set;
162 import java.util.UUID;
163 import java.util.concurrent.ExecutorService;
164 import java.util.concurrent.TimeUnit;
165 import java.util.concurrent.TimeoutException;
166 import java.util.stream.Collectors;
167 import java.util.zip.ZipEntry;
168 import java.util.zip.ZipFile;
169
170 import static org.asamk.signal.manager.ServiceConfig.CDS_MRENCLAVE;
171 import static org.asamk.signal.manager.ServiceConfig.capabilities;
172 import static org.asamk.signal.manager.ServiceConfig.getIasKeyStore;
173
174 public class Manager implements Closeable {
175
176 private final SleepTimer timer = new UptimeSleepTimer();
177
178 private final SignalServiceConfiguration serviceConfiguration;
179 private final String userAgent;
180 private final boolean discoverableByPhoneNumber = true;
181 private final boolean unrestrictedUnidentifiedAccess = false;
182
183 private final SignalAccount account;
184 private final PathConfig pathConfig;
185 private SignalServiceAccountManager accountManager;
186 private GroupsV2Api groupsV2Api;
187 private final GroupsV2Operations groupsV2Operations;
188
189 private SignalServiceMessageReceiver messageReceiver = null;
190 private SignalServiceMessagePipe messagePipe = null;
191 private SignalServiceMessagePipe unidentifiedMessagePipe = null;
192
193 private final UnidentifiedAccessHelper unidentifiedAccessHelper;
194 private final ProfileHelper profileHelper;
195 private final GroupHelper groupHelper;
196
197 public Manager(
198 SignalAccount account,
199 PathConfig pathConfig,
200 SignalServiceConfiguration serviceConfiguration,
201 String userAgent
202 ) {
203 this.account = account;
204 this.pathConfig = pathConfig;
205 this.serviceConfiguration = serviceConfiguration;
206 this.userAgent = userAgent;
207 this.groupsV2Operations = capabilities.isGv2() ? new GroupsV2Operations(ClientZkOperations.create(
208 serviceConfiguration)) : null;
209 this.accountManager = createSignalServiceAccountManager();
210 this.groupsV2Api = accountManager.getGroupsV2Api();
211
212 this.account.setResolver(this::resolveSignalServiceAddress);
213
214 this.unidentifiedAccessHelper = new UnidentifiedAccessHelper(account::getProfileKey,
215 account.getProfileStore()::getProfileKey,
216 this::getRecipientProfile,
217 this::getSenderCertificate);
218 this.profileHelper = new ProfileHelper(account.getProfileStore()::getProfileKey,
219 unidentifiedAccessHelper::getAccessFor,
220 unidentified -> unidentified ? getOrCreateUnidentifiedMessagePipe() : getOrCreateMessagePipe(),
221 this::getOrCreateMessageReceiver);
222 this.groupHelper = new GroupHelper(this::getRecipientProfileKeyCredential,
223 this::getRecipientProfile,
224 account::getSelfAddress,
225 groupsV2Operations,
226 groupsV2Api,
227 this::getGroupAuthForToday);
228 }
229
230 public String getUsername() {
231 return account.getUsername();
232 }
233
234 public SignalServiceAddress getSelfAddress() {
235 return account.getSelfAddress();
236 }
237
238 private SignalServiceAccountManager createSignalServiceAccountManager() {
239 return new SignalServiceAccountManager(serviceConfiguration,
240 new DynamicCredentialsProvider(account.getUuid(),
241 account.getUsername(),
242 account.getPassword(),
243 null,
244 account.getDeviceId()),
245 userAgent,
246 groupsV2Operations,
247 timer);
248 }
249
250 private IdentityKeyPair getIdentityKeyPair() {
251 return account.getSignalProtocolStore().getIdentityKeyPair();
252 }
253
254 public int getDeviceId() {
255 return account.getDeviceId();
256 }
257
258 private String getMessageCachePath() {
259 return pathConfig.getDataPath() + "/" + account.getUsername() + ".d/msg-cache";
260 }
261
262 private String getMessageCachePath(String sender) {
263 if (sender == null || sender.isEmpty()) {
264 return getMessageCachePath();
265 }
266
267 return getMessageCachePath() + "/" + sender.replace("/", "_");
268 }
269
270 private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException {
271 String cachePath = getMessageCachePath(sender);
272 IOUtils.createPrivateDirectories(cachePath);
273 return new File(cachePath + "/" + now + "_" + timestamp);
274 }
275
276 public static Manager init(
277 String username, String settingsPath, SignalServiceConfiguration serviceConfiguration, String userAgent
278 ) throws IOException {
279 PathConfig pathConfig = PathConfig.createDefault(settingsPath);
280
281 if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) {
282 IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
283 int registrationId = KeyHelper.generateRegistrationId(false);
284
285 ProfileKey profileKey = KeyUtils.createProfileKey();
286 SignalAccount account = SignalAccount.create(pathConfig.getDataPath(),
287 username,
288 identityKey,
289 registrationId,
290 profileKey);
291 account.save();
292
293 return new Manager(account, pathConfig, serviceConfiguration, userAgent);
294 }
295
296 SignalAccount account = SignalAccount.load(pathConfig.getDataPath(), username);
297
298 Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent);
299
300 m.migrateLegacyConfigs();
301
302 return m;
303 }
304
305 private void migrateLegacyConfigs() {
306 if (account.getProfileKey() == null && isRegistered()) {
307 // Old config file, creating new profile key
308 account.setProfileKey(KeyUtils.createProfileKey());
309 account.save();
310 }
311 // Store profile keys only in profile store
312 for (ContactInfo contact : account.getContactStore().getContacts()) {
313 String profileKeyString = contact.profileKey;
314 if (profileKeyString == null) {
315 continue;
316 }
317 final ProfileKey profileKey;
318 try {
319 profileKey = new ProfileKey(Base64.decode(profileKeyString));
320 } catch (InvalidInputException | IOException e) {
321 continue;
322 }
323 contact.profileKey = null;
324 account.getProfileStore().storeProfileKey(contact.getAddress(), profileKey);
325 }
326 // Ensure our profile key is stored in profile store
327 account.getProfileStore().storeProfileKey(getSelfAddress(), account.getProfileKey());
328 }
329
330 public void checkAccountState() throws IOException {
331 if (account.isRegistered()) {
332 if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
333 refreshPreKeys();
334 account.save();
335 }
336 if (account.getUuid() == null) {
337 account.setUuid(accountManager.getOwnUuid());
338 account.save();
339 }
340 updateAccountAttributes();
341 }
342 }
343
344 public boolean isRegistered() {
345 return account.isRegistered();
346 }
347
348 public void register(boolean voiceVerification, String captcha) throws IOException {
349 account.setPassword(KeyUtils.createPassword());
350
351 // Resetting UUID, because registering doesn't work otherwise
352 account.setUuid(null);
353 accountManager = createSignalServiceAccountManager();
354 this.groupsV2Api = accountManager.getGroupsV2Api();
355
356 if (voiceVerification) {
357 accountManager.requestVoiceVerificationCode(Locale.getDefault(),
358 Optional.fromNullable(captcha),
359 Optional.absent());
360 } else {
361 accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent());
362 }
363
364 account.setRegistered(false);
365 account.save();
366 }
367
368 public void updateAccountAttributes() throws IOException {
369 accountManager.setAccountAttributes(account.getSignalingKey(),
370 account.getSignalProtocolStore().getLocalRegistrationId(),
371 true,
372 account.getRegistrationLockPin(),
373 account.getRegistrationLock(),
374 unidentifiedAccessHelper.getSelfUnidentifiedAccessKey(),
375 unrestrictedUnidentifiedAccess,
376 capabilities,
377 discoverableByPhoneNumber);
378 }
379
380 public void setProfile(String name, File avatar) throws IOException {
381 try (final StreamDetails streamDetails = avatar == null ? null : Utils.createStreamDetailsFromFile(avatar)) {
382 accountManager.setVersionedProfile(account.getUuid(), account.getProfileKey(), name, streamDetails);
383 }
384 }
385
386 public void unregister() throws IOException {
387 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
388 // If this is the master device, other users can't send messages to this number anymore.
389 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
390 accountManager.setGcmId(Optional.absent());
391
392 account.setRegistered(false);
393 account.save();
394 }
395
396 public List<DeviceInfo> getLinkedDevices() throws IOException {
397 List<DeviceInfo> devices = accountManager.getDevices();
398 account.setMultiDevice(devices.size() > 1);
399 account.save();
400 return devices;
401 }
402
403 public void removeLinkedDevices(int deviceId) throws IOException {
404 accountManager.removeDevice(deviceId);
405 List<DeviceInfo> devices = accountManager.getDevices();
406 account.setMultiDevice(devices.size() > 1);
407 account.save();
408 }
409
410 public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
411 Utils.DeviceLinkInfo info = Utils.parseDeviceLinkUri(linkUri);
412
413 addDevice(info.deviceIdentifier, info.deviceKey);
414 }
415
416 private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
417 IdentityKeyPair identityKeyPair = getIdentityKeyPair();
418 String verificationCode = accountManager.getNewDeviceVerificationCode();
419
420 accountManager.addDevice(deviceIdentifier,
421 deviceKey,
422 identityKeyPair,
423 Optional.of(account.getProfileKey().serialize()),
424 verificationCode);
425 account.setMultiDevice(true);
426 account.save();
427 }
428
429 private List<PreKeyRecord> generatePreKeys() {
430 List<PreKeyRecord> records = new ArrayList<>(ServiceConfig.PREKEY_BATCH_SIZE);
431
432 final int offset = account.getPreKeyIdOffset();
433 for (int i = 0; i < ServiceConfig.PREKEY_BATCH_SIZE; i++) {
434 int preKeyId = (offset + i) % Medium.MAX_VALUE;
435 ECKeyPair keyPair = Curve.generateKeyPair();
436 PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
437
438 records.add(record);
439 }
440
441 account.addPreKeys(records);
442 account.save();
443
444 return records;
445 }
446
447 private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
448 try {
449 ECKeyPair keyPair = Curve.generateKeyPair();
450 byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(),
451 keyPair.getPublicKey().serialize());
452 SignedPreKeyRecord record = new SignedPreKeyRecord(account.getNextSignedPreKeyId(),
453 System.currentTimeMillis(),
454 keyPair,
455 signature);
456
457 account.addSignedPreKey(record);
458 account.save();
459
460 return record;
461 } catch (InvalidKeyException e) {
462 throw new AssertionError(e);
463 }
464 }
465
466 public void verifyAccount(String verificationCode, String pin) throws IOException {
467 verificationCode = verificationCode.replace("-", "");
468 account.setSignalingKey(KeyUtils.createSignalingKey());
469 // TODO make unrestricted unidentified access configurable
470 VerifyAccountResponse response = accountManager.verifyAccountWithCode(verificationCode,
471 account.getSignalingKey(),
472 account.getSignalProtocolStore().getLocalRegistrationId(),
473 true,
474 pin,
475 null,
476 unidentifiedAccessHelper.getSelfUnidentifiedAccessKey(),
477 unrestrictedUnidentifiedAccess,
478 capabilities,
479 discoverableByPhoneNumber);
480
481 UUID uuid = UuidUtil.parseOrNull(response.getUuid());
482 // TODO response.isStorageCapable()
483 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
484 account.setRegistered(true);
485 account.setUuid(uuid);
486 account.setRegistrationLockPin(pin);
487 account.getSignalProtocolStore()
488 .saveIdentity(account.getSelfAddress(),
489 getIdentityKeyPair().getPublicKey(),
490 TrustLevel.TRUSTED_VERIFIED);
491
492 refreshPreKeys();
493 account.save();
494 }
495
496 public void setRegistrationLockPin(Optional<String> pin) throws IOException {
497 if (pin.isPresent()) {
498 account.setRegistrationLockPin(pin.get());
499 throw new RuntimeException("Not implemented anymore, will be replaced with KBS");
500 } else {
501 account.setRegistrationLockPin(null);
502 accountManager.removeRegistrationLockV1();
503 }
504 account.save();
505 }
506
507 void refreshPreKeys() throws IOException {
508 List<PreKeyRecord> oneTimePreKeys = generatePreKeys();
509 final IdentityKeyPair identityKeyPair = getIdentityKeyPair();
510 SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(identityKeyPair);
511
512 accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
513 }
514
515 private SignalServiceMessageReceiver createMessageReceiver() {
516 final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(
517 serviceConfiguration).getProfileOperations() : null;
518 return new SignalServiceMessageReceiver(serviceConfiguration,
519 account.getUuid(),
520 account.getUsername(),
521 account.getPassword(),
522 account.getDeviceId(),
523 account.getSignalingKey(),
524 userAgent,
525 null,
526 timer,
527 clientZkProfileOperations);
528 }
529
530 private SignalServiceMessageReceiver getOrCreateMessageReceiver() {
531 if (messageReceiver == null) {
532 messageReceiver = createMessageReceiver();
533 }
534 return messageReceiver;
535 }
536
537 private SignalServiceMessagePipe getOrCreateMessagePipe() {
538 if (messagePipe == null) {
539 messagePipe = getOrCreateMessageReceiver().createMessagePipe();
540 }
541 return messagePipe;
542 }
543
544 private SignalServiceMessagePipe getOrCreateUnidentifiedMessagePipe() {
545 if (unidentifiedMessagePipe == null) {
546 unidentifiedMessagePipe = getOrCreateMessageReceiver().createUnidentifiedMessagePipe();
547 }
548 return unidentifiedMessagePipe;
549 }
550
551 private SignalServiceMessageSender createMessageSender() {
552 final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(
553 serviceConfiguration).getProfileOperations() : null;
554 final ExecutorService executor = null;
555 return new SignalServiceMessageSender(serviceConfiguration,
556 account.getUuid(),
557 account.getUsername(),
558 account.getPassword(),
559 account.getDeviceId(),
560 account.getSignalProtocolStore(),
561 userAgent,
562 account.isMultiDevice(),
563 Optional.fromNullable(messagePipe),
564 Optional.fromNullable(unidentifiedMessagePipe),
565 Optional.absent(),
566 clientZkProfileOperations,
567 executor,
568 ServiceConfig.MAX_ENVELOPE_SIZE);
569 }
570
571 private SignalServiceProfile getEncryptedRecipientProfile(SignalServiceAddress address) throws IOException {
572 return profileHelper.retrieveProfileSync(address, SignalServiceProfile.RequestType.PROFILE).getProfile();
573 }
574
575 private SignalProfile getRecipientProfile(
576 SignalServiceAddress address
577 ) {
578 SignalProfileEntry profileEntry = account.getProfileStore().getProfileEntry(address);
579 if (profileEntry == null) {
580 return null;
581 }
582 long now = new Date().getTime();
583 // Profiles are cache for 24h before retrieving them again
584 if (!profileEntry.isRequestPending() && (
585 profileEntry.getProfile() == null || now - profileEntry.getLastUpdateTimestamp() > 24 * 60 * 60 * 1000
586 )) {
587 ProfileKey profileKey = profileEntry.getProfileKey();
588 profileEntry.setRequestPending(true);
589 SignalProfile profile;
590 try {
591 profile = retrieveRecipientProfile(address, profileKey);
592 } catch (IOException e) {
593 System.err.println("Failed to retrieve profile, ignoring: " + e.getMessage());
594 profileEntry.setRequestPending(false);
595 return null;
596 }
597 profileEntry.setRequestPending(false);
598 account.getProfileStore()
599 .updateProfile(address, profileKey, now, profile, profileEntry.getProfileKeyCredential());
600 return profile;
601 }
602 return profileEntry.getProfile();
603 }
604
605 private ProfileKeyCredential getRecipientProfileKeyCredential(SignalServiceAddress address) {
606 SignalProfileEntry profileEntry = account.getProfileStore().getProfileEntry(address);
607 if (profileEntry == null) {
608 return null;
609 }
610 if (profileEntry.getProfileKeyCredential() == null) {
611 ProfileAndCredential profileAndCredential;
612 try {
613 profileAndCredential = profileHelper.retrieveProfileSync(address,
614 SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL);
615 } catch (IOException e) {
616 System.err.println("Failed to retrieve profile key credential, ignoring: " + e.getMessage());
617 return null;
618 }
619
620 long now = new Date().getTime();
621 final ProfileKeyCredential profileKeyCredential = profileAndCredential.getProfileKeyCredential().orNull();
622 final SignalProfile profile = decryptProfile(address,
623 profileEntry.getProfileKey(),
624 profileAndCredential.getProfile());
625 account.getProfileStore()
626 .updateProfile(address, profileEntry.getProfileKey(), now, profile, profileKeyCredential);
627 return profileKeyCredential;
628 }
629 return profileEntry.getProfileKeyCredential();
630 }
631
632 private SignalProfile retrieveRecipientProfile(
633 SignalServiceAddress address, ProfileKey profileKey
634 ) throws IOException {
635 final SignalServiceProfile encryptedProfile = getEncryptedRecipientProfile(address);
636
637 return decryptProfile(address, profileKey, encryptedProfile);
638 }
639
640 private SignalProfile decryptProfile(
641 final SignalServiceAddress address, final ProfileKey profileKey, final SignalServiceProfile encryptedProfile
642 ) {
643 File avatarFile = null;
644 try {
645 avatarFile = encryptedProfile.getAvatar() == null
646 ? null
647 : retrieveProfileAvatar(address, encryptedProfile.getAvatar(), profileKey);
648 } catch (Throwable e) {
649 System.err.println("Failed to retrieve profile avatar, ignoring: " + e.getMessage());
650 }
651
652 ProfileCipher profileCipher = new ProfileCipher(profileKey);
653 try {
654 String name;
655 try {
656 name = encryptedProfile.getName() == null
657 ? null
658 : new String(profileCipher.decryptName(Base64.decode(encryptedProfile.getName())));
659 } catch (IOException e) {
660 name = null;
661 }
662 String unidentifiedAccess;
663 try {
664 unidentifiedAccess = encryptedProfile.getUnidentifiedAccess() == null
665 || !profileCipher.verifyUnidentifiedAccess(Base64.decode(encryptedProfile.getUnidentifiedAccess()))
666 ? null
667 : encryptedProfile.getUnidentifiedAccess();
668 } catch (IOException e) {
669 unidentifiedAccess = null;
670 }
671 return new SignalProfile(encryptedProfile.getIdentityKey(),
672 name,
673 avatarFile,
674 unidentifiedAccess,
675 encryptedProfile.isUnrestrictedUnidentifiedAccess(),
676 encryptedProfile.getCapabilities());
677 } catch (InvalidCiphertextException e) {
678 return null;
679 }
680 }
681
682 private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(GroupId groupId) throws IOException {
683 File file = getGroupAvatarFile(groupId);
684 if (!file.exists()) {
685 return Optional.absent();
686 }
687
688 return Optional.of(Utils.createAttachment(file));
689 }
690
691 private Optional<SignalServiceAttachmentStream> createContactAvatarAttachment(String number) throws IOException {
692 File file = getContactAvatarFile(number);
693 if (!file.exists()) {
694 return Optional.absent();
695 }
696
697 return Optional.of(Utils.createAttachment(file));
698 }
699
700 private GroupInfo getGroupForSending(GroupId groupId) throws GroupNotFoundException, NotAGroupMemberException {
701 GroupInfo g = account.getGroupStore().getGroup(groupId);
702 if (g == null) {
703 throw new GroupNotFoundException(groupId);
704 }
705 if (!g.isMember(account.getSelfAddress())) {
706 throw new NotAGroupMemberException(groupId, g.getTitle());
707 }
708 return g;
709 }
710
711 private GroupInfo getGroupForUpdating(GroupId groupId) throws GroupNotFoundException, NotAGroupMemberException {
712 GroupInfo g = account.getGroupStore().getGroup(groupId);
713 if (g == null) {
714 throw new GroupNotFoundException(groupId);
715 }
716 if (!g.isMember(account.getSelfAddress()) && !g.isPendingMember(account.getSelfAddress())) {
717 throw new NotAGroupMemberException(groupId, g.getTitle());
718 }
719 return g;
720 }
721
722 public List<GroupInfo> getGroups() {
723 return account.getGroupStore().getGroups();
724 }
725
726 public Pair<Long, List<SendMessageResult>> sendGroupMessage(
727 SignalServiceDataMessage.Builder messageBuilder, GroupId groupId
728 ) throws IOException, GroupNotFoundException, NotAGroupMemberException {
729 final GroupInfo g = getGroupForSending(groupId);
730
731 GroupUtils.setGroupContext(messageBuilder, g);
732 messageBuilder.withExpiration(g.getMessageExpirationTime());
733
734 return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
735 }
736
737 public Pair<Long, List<SendMessageResult>> sendGroupMessage(
738 String messageText, List<String> attachments, GroupId groupId
739 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
740 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
741 .withBody(messageText);
742 if (attachments != null) {
743 messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
744 }
745
746 return sendGroupMessage(messageBuilder, groupId);
747 }
748
749 public Pair<Long, List<SendMessageResult>> sendGroupMessageReaction(
750 String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, GroupId groupId
751 ) throws IOException, InvalidNumberException, NotAGroupMemberException, GroupNotFoundException {
752 SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji,
753 remove,
754 canonicalizeAndResolveSignalServiceAddress(targetAuthor),
755 targetSentTimestamp);
756 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
757 .withReaction(reaction);
758
759 return sendGroupMessage(messageBuilder, groupId);
760 }
761
762 public Pair<Long, List<SendMessageResult>> sendQuitGroupMessage(GroupId groupId) throws GroupNotFoundException, IOException, NotAGroupMemberException {
763
764 SignalServiceDataMessage.Builder messageBuilder;
765
766 final GroupInfo g = getGroupForUpdating(groupId);
767 if (g instanceof GroupInfoV1) {
768 GroupInfoV1 groupInfoV1 = (GroupInfoV1) g;
769 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
770 .withId(groupId.serialize())
771 .build();
772 messageBuilder = SignalServiceDataMessage.newBuilder().asGroupMessage(group);
773 groupInfoV1.removeMember(account.getSelfAddress());
774 account.getGroupStore().updateGroup(groupInfoV1);
775 } else {
776 final GroupInfoV2 groupInfoV2 = (GroupInfoV2) g;
777 final Pair<DecryptedGroup, GroupChange> groupGroupChangePair = groupHelper.leaveGroup(groupInfoV2);
778 groupInfoV2.setGroup(groupGroupChangePair.first());
779 messageBuilder = getGroupUpdateMessageBuilder(groupInfoV2, groupGroupChangePair.second().toByteArray());
780 account.getGroupStore().updateGroup(groupInfoV2);
781 }
782
783 return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
784 }
785
786 private Pair<GroupId, List<SendMessageResult>> sendUpdateGroupMessage(
787 GroupId groupId, String name, Collection<SignalServiceAddress> members, String avatarFile
788 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
789 GroupInfo g;
790 SignalServiceDataMessage.Builder messageBuilder;
791 if (groupId == null) {
792 // Create new group
793 GroupInfoV2 gv2 = groupHelper.createGroupV2(name, members, avatarFile);
794 if (gv2 == null) {
795 GroupInfoV1 gv1 = new GroupInfoV1(GroupIdV1.createRandom());
796 gv1.addMembers(Collections.singleton(account.getSelfAddress()));
797 updateGroupV1(gv1, name, members, avatarFile);
798 messageBuilder = getGroupUpdateMessageBuilder(gv1);
799 g = gv1;
800 } else {
801 messageBuilder = getGroupUpdateMessageBuilder(gv2, null);
802 g = gv2;
803 }
804 } else {
805 GroupInfo group = getGroupForUpdating(groupId);
806 if (group instanceof GroupInfoV2) {
807 final GroupInfoV2 groupInfoV2 = (GroupInfoV2) group;
808
809 Pair<Long, List<SendMessageResult>> result = null;
810 if (groupInfoV2.isPendingMember(getSelfAddress())) {
811 Pair<DecryptedGroup, GroupChange> groupGroupChangePair = groupHelper.acceptInvite(groupInfoV2);
812 result = sendUpdateGroupMessage(groupInfoV2,
813 groupGroupChangePair.first(),
814 groupGroupChangePair.second());
815 }
816
817 if (members != null) {
818 final Set<SignalServiceAddress> newMembers = new HashSet<>(members);
819 newMembers.removeAll(group.getMembers());
820 if (newMembers.size() > 0) {
821 Pair<DecryptedGroup, GroupChange> groupGroupChangePair = groupHelper.updateGroupV2(groupInfoV2,
822 newMembers);
823 result = sendUpdateGroupMessage(groupInfoV2,
824 groupGroupChangePair.first(),
825 groupGroupChangePair.second());
826 }
827 }
828 if (result == null || name != null || avatarFile != null) {
829 Pair<DecryptedGroup, GroupChange> groupGroupChangePair = groupHelper.updateGroupV2(groupInfoV2,
830 name,
831 avatarFile);
832 result = sendUpdateGroupMessage(groupInfoV2,
833 groupGroupChangePair.first(),
834 groupGroupChangePair.second());
835 }
836
837 return new Pair<>(group.getGroupId(), result.second());
838 } else {
839 GroupInfoV1 gv1 = (GroupInfoV1) group;
840 updateGroupV1(gv1, name, members, avatarFile);
841 messageBuilder = getGroupUpdateMessageBuilder(gv1);
842 g = gv1;
843 }
844 }
845
846 account.getGroupStore().updateGroup(g);
847
848 final Pair<Long, List<SendMessageResult>> result = sendMessage(messageBuilder,
849 g.getMembersIncludingPendingWithout(account.getSelfAddress()));
850 return new Pair<>(g.getGroupId(), result.second());
851 }
852
853 public Pair<GroupId, List<SendMessageResult>> joinGroup(
854 GroupInviteLinkUrl inviteLinkUrl
855 ) throws IOException, GroupLinkNotActiveException {
856 return sendJoinGroupMessage(inviteLinkUrl);
857 }
858
859 private Pair<GroupId, List<SendMessageResult>> sendJoinGroupMessage(
860 GroupInviteLinkUrl inviteLinkUrl
861 ) throws IOException, GroupLinkNotActiveException {
862 final DecryptedGroupJoinInfo groupJoinInfo = groupHelper.getDecryptedGroupJoinInfo(inviteLinkUrl.getGroupMasterKey(),
863 inviteLinkUrl.getPassword());
864 final GroupChange groupChange = groupHelper.joinGroup(inviteLinkUrl.getGroupMasterKey(),
865 inviteLinkUrl.getPassword(),
866 groupJoinInfo);
867 final GroupInfoV2 group = getOrMigrateGroup(inviteLinkUrl.getGroupMasterKey(),
868 groupJoinInfo.getRevision() + 1,
869 groupChange.toByteArray());
870
871 if (group.getGroup() == null) {
872 // Only requested member, can't send update to group members
873 return new Pair<>(group.getGroupId(), List.of());
874 }
875
876 final Pair<Long, List<SendMessageResult>> result = sendUpdateGroupMessage(group, group.getGroup(), groupChange);
877
878 return new Pair<>(group.getGroupId(), result.second());
879 }
880
881 private Pair<Long, List<SendMessageResult>> sendUpdateGroupMessage(
882 GroupInfoV2 group, DecryptedGroup newDecryptedGroup, GroupChange groupChange
883 ) throws IOException {
884 group.setGroup(newDecryptedGroup);
885 final SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(group,
886 groupChange.toByteArray());
887 account.getGroupStore().updateGroup(group);
888 return sendMessage(messageBuilder, group.getMembersIncludingPendingWithout(account.getSelfAddress()));
889 }
890
891 private void updateGroupV1(
892 final GroupInfoV1 g,
893 final String name,
894 final Collection<SignalServiceAddress> members,
895 final String avatarFile
896 ) throws IOException {
897 if (name != null) {
898 g.name = name;
899 }
900
901 if (members != null) {
902 final Set<String> newE164Members = new HashSet<>();
903 for (SignalServiceAddress member : members) {
904 if (g.isMember(member) || !member.getNumber().isPresent()) {
905 continue;
906 }
907 newE164Members.add(member.getNumber().get());
908 }
909
910 final List<ContactTokenDetails> contacts = accountManager.getContacts(newE164Members);
911 if (contacts.size() != newE164Members.size()) {
912 // Some of the new members are not registered on Signal
913 for (ContactTokenDetails contact : contacts) {
914 newE164Members.remove(contact.getNumber());
915 }
916 throw new IOException("Failed to add members "
917 + Util.join(", ", newE164Members)
918 + " to group: Not registered on Signal");
919 }
920
921 g.addMembers(members);
922 }
923
924 if (avatarFile != null) {
925 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
926 File aFile = getGroupAvatarFile(g.getGroupId());
927 Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
928 }
929 }
930
931 Pair<Long, List<SendMessageResult>> sendUpdateGroupMessage(
932 GroupIdV1 groupId, SignalServiceAddress recipient
933 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
934 GroupInfoV1 g;
935 GroupInfo group = getGroupForSending(groupId);
936 if (!(group instanceof GroupInfoV1)) {
937 throw new RuntimeException("Received an invalid group request for a v2 group!");
938 }
939 g = (GroupInfoV1) group;
940
941 if (!g.isMember(recipient)) {
942 throw new NotAGroupMemberException(groupId, g.name);
943 }
944
945 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
946
947 // Send group message only to the recipient who requested it
948 return sendMessage(messageBuilder, Collections.singleton(recipient));
949 }
950
951 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV1 g) throws AttachmentInvalidException {
952 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
953 .withId(g.getGroupId().serialize())
954 .withName(g.name)
955 .withMembers(new ArrayList<>(g.getMembers()));
956
957 File aFile = getGroupAvatarFile(g.getGroupId());
958 if (aFile.exists()) {
959 try {
960 group.withAvatar(Utils.createAttachment(aFile));
961 } catch (IOException e) {
962 throw new AttachmentInvalidException(aFile.toString(), e);
963 }
964 }
965
966 return SignalServiceDataMessage.newBuilder()
967 .asGroupMessage(group.build())
968 .withExpiration(g.getMessageExpirationTime());
969 }
970
971 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV2 g, byte[] signedGroupChange) {
972 SignalServiceGroupV2.Builder group = SignalServiceGroupV2.newBuilder(g.getMasterKey())
973 .withRevision(g.getGroup().getRevision())
974 .withSignedGroupChange(signedGroupChange);
975 return SignalServiceDataMessage.newBuilder()
976 .asGroupMessage(group.build())
977 .withExpiration(g.getMessageExpirationTime());
978 }
979
980 Pair<Long, List<SendMessageResult>> sendGroupInfoRequest(
981 GroupIdV1 groupId, SignalServiceAddress recipient
982 ) throws IOException {
983 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO)
984 .withId(groupId.serialize());
985
986 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
987 .asGroupMessage(group.build());
988
989 // Send group info request message to the recipient who sent us a message with this groupId
990 return sendMessage(messageBuilder, Collections.singleton(recipient));
991 }
992
993 void sendReceipt(
994 SignalServiceAddress remoteAddress, long messageId
995 ) throws IOException, UntrustedIdentityException {
996 SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.DELIVERY,
997 Collections.singletonList(messageId),
998 System.currentTimeMillis());
999
1000 createMessageSender().sendReceipt(remoteAddress,
1001 unidentifiedAccessHelper.getAccessFor(remoteAddress),
1002 receiptMessage);
1003 }
1004
1005 public Pair<Long, List<SendMessageResult>> sendMessage(
1006 String messageText, List<String> attachments, List<String> recipients
1007 ) throws IOException, AttachmentInvalidException, InvalidNumberException {
1008 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
1009 .withBody(messageText);
1010 if (attachments != null) {
1011 List<SignalServiceAttachment> attachmentStreams = Utils.getSignalServiceAttachments(attachments);
1012
1013 // Upload attachments here, so we only upload once even for multiple recipients
1014 SignalServiceMessageSender messageSender = createMessageSender();
1015 List<SignalServiceAttachment> attachmentPointers = new ArrayList<>(attachmentStreams.size());
1016 for (SignalServiceAttachment attachment : attachmentStreams) {
1017 if (attachment.isStream()) {
1018 attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
1019 } else if (attachment.isPointer()) {
1020 attachmentPointers.add(attachment.asPointer());
1021 }
1022 }
1023
1024 messageBuilder.withAttachments(attachmentPointers);
1025 }
1026 return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
1027 }
1028
1029 public Pair<Long, List<SendMessageResult>> sendMessageReaction(
1030 String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List<String> recipients
1031 ) throws IOException, InvalidNumberException {
1032 SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji,
1033 remove,
1034 canonicalizeAndResolveSignalServiceAddress(targetAuthor),
1035 targetSentTimestamp);
1036 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
1037 .withReaction(reaction);
1038 return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
1039 }
1040
1041 public Pair<Long, List<SendMessageResult>> sendEndSessionMessage(List<String> recipients) throws IOException, InvalidNumberException {
1042 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().asEndSessionMessage();
1043
1044 final Collection<SignalServiceAddress> signalServiceAddresses = getSignalServiceAddresses(recipients);
1045 try {
1046 return sendMessage(messageBuilder, signalServiceAddresses);
1047 } catch (Exception e) {
1048 for (SignalServiceAddress address : signalServiceAddresses) {
1049 handleEndSession(address);
1050 }
1051 account.save();
1052 throw e;
1053 }
1054 }
1055
1056 public String getContactName(String number) throws InvalidNumberException {
1057 ContactInfo contact = account.getContactStore().getContact(canonicalizeAndResolveSignalServiceAddress(number));
1058 if (contact == null) {
1059 return "";
1060 } else {
1061 return contact.name;
1062 }
1063 }
1064
1065 public void setContactName(String number, String name) throws InvalidNumberException {
1066 final SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(number);
1067 ContactInfo contact = account.getContactStore().getContact(address);
1068 if (contact == null) {
1069 contact = new ContactInfo(address);
1070 }
1071 contact.name = name;
1072 account.getContactStore().updateContact(contact);
1073 account.save();
1074 }
1075
1076 public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
1077 setContactBlocked(canonicalizeAndResolveSignalServiceAddress(number), blocked);
1078 }
1079
1080 private void setContactBlocked(SignalServiceAddress address, boolean blocked) {
1081 ContactInfo contact = account.getContactStore().getContact(address);
1082 if (contact == null) {
1083 contact = new ContactInfo(address);
1084 }
1085 contact.blocked = blocked;
1086 account.getContactStore().updateContact(contact);
1087 account.save();
1088 }
1089
1090 public void setGroupBlocked(final GroupId groupId, final boolean blocked) throws GroupNotFoundException {
1091 GroupInfo group = getGroup(groupId);
1092 if (group == null) {
1093 throw new GroupNotFoundException(groupId);
1094 }
1095
1096 group.setBlocked(blocked);
1097 account.getGroupStore().updateGroup(group);
1098 account.save();
1099 }
1100
1101 public Pair<GroupId, List<SendMessageResult>> updateGroup(
1102 GroupId groupId, String name, List<String> members, String avatar
1103 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
1104 return sendUpdateGroupMessage(groupId,
1105 name,
1106 members == null ? null : getSignalServiceAddresses(members),
1107 avatar);
1108 }
1109
1110 /**
1111 * Change the expiration timer for a contact
1112 */
1113 public void setExpirationTimer(SignalServiceAddress address, int messageExpirationTimer) throws IOException {
1114 ContactInfo contact = account.getContactStore().getContact(address);
1115 contact.messageExpirationTime = messageExpirationTimer;
1116 account.getContactStore().updateContact(contact);
1117 sendExpirationTimerUpdate(address);
1118 account.save();
1119 }
1120
1121 private void sendExpirationTimerUpdate(SignalServiceAddress address) throws IOException {
1122 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
1123 .asExpirationUpdate();
1124 sendMessage(messageBuilder, Collections.singleton(address));
1125 }
1126
1127 /**
1128 * Change the expiration timer for a contact
1129 */
1130 public void setExpirationTimer(
1131 String number, int messageExpirationTimer
1132 ) throws IOException, InvalidNumberException {
1133 SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(number);
1134 setExpirationTimer(address, messageExpirationTimer);
1135 }
1136
1137 /**
1138 * Change the expiration timer for a group
1139 */
1140 public void setExpirationTimer(GroupId groupId, int messageExpirationTimer) {
1141 GroupInfo g = account.getGroupStore().getGroup(groupId);
1142 if (g instanceof GroupInfoV1) {
1143 GroupInfoV1 groupInfoV1 = (GroupInfoV1) g;
1144 groupInfoV1.messageExpirationTime = messageExpirationTimer;
1145 account.getGroupStore().updateGroup(groupInfoV1);
1146 } else {
1147 throw new RuntimeException("TODO Not implemented!");
1148 }
1149 }
1150
1151 /**
1152 * Upload the sticker pack from path.
1153 *
1154 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1155 * @return if successful, returns the URL to install the sticker pack in the signal app
1156 */
1157 public String uploadStickerPack(String path) throws IOException, StickerPackInvalidException {
1158 SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
1159
1160 SignalServiceMessageSender messageSender = createMessageSender();
1161
1162 byte[] packKey = KeyUtils.createStickerUploadKey();
1163 String packId = messageSender.uploadStickerManifest(manifest, packKey);
1164
1165 Sticker sticker = new Sticker(Hex.fromStringCondensed(packId), packKey);
1166 account.getStickerStore().updateSticker(sticker);
1167 account.save();
1168
1169 try {
1170 return new URI("https",
1171 "signal.art",
1172 "/addstickers/",
1173 "pack_id=" + URLEncoder.encode(packId, StandardCharsets.UTF_8) + "&pack_key=" + URLEncoder.encode(
1174 Hex.toStringCondensed(packKey),
1175 StandardCharsets.UTF_8)).toString();
1176 } catch (URISyntaxException e) {
1177 throw new AssertionError(e);
1178 }
1179 }
1180
1181 private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload(
1182 final String path
1183 ) throws IOException, StickerPackInvalidException {
1184 ZipFile zip = null;
1185 String rootPath = null;
1186
1187 final File file = new File(path);
1188 if (file.getName().endsWith(".zip")) {
1189 zip = new ZipFile(file);
1190 } else if (file.getName().equals("manifest.json")) {
1191 rootPath = file.getParent();
1192 } else {
1193 throw new StickerPackInvalidException("Could not find manifest.json");
1194 }
1195
1196 JsonStickerPack pack = parseStickerPack(rootPath, zip);
1197
1198 if (pack.stickers == null) {
1199 throw new StickerPackInvalidException("Must set a 'stickers' field.");
1200 }
1201
1202 if (pack.stickers.isEmpty()) {
1203 throw new StickerPackInvalidException("Must include stickers.");
1204 }
1205
1206 List<StickerInfo> stickers = new ArrayList<>(pack.stickers.size());
1207 for (JsonStickerPack.JsonSticker sticker : pack.stickers) {
1208 if (sticker.file == null) {
1209 throw new StickerPackInvalidException("Must set a 'file' field on each sticker.");
1210 }
1211
1212 Pair<InputStream, Long> data;
1213 try {
1214 data = getInputStreamAndLength(rootPath, zip, sticker.file);
1215 } catch (IOException ignored) {
1216 throw new StickerPackInvalidException("Could not find find " + sticker.file);
1217 }
1218
1219 String contentType = Utils.getFileMimeType(new File(sticker.file), null);
1220 StickerInfo stickerInfo = new StickerInfo(data.first(),
1221 data.second(),
1222 Optional.fromNullable(sticker.emoji).or(""),
1223 contentType);
1224 stickers.add(stickerInfo);
1225 }
1226
1227 StickerInfo cover = null;
1228 if (pack.cover != null) {
1229 if (pack.cover.file == null) {
1230 throw new StickerPackInvalidException("Must set a 'file' field on the cover.");
1231 }
1232
1233 Pair<InputStream, Long> data;
1234 try {
1235 data = getInputStreamAndLength(rootPath, zip, pack.cover.file);
1236 } catch (IOException ignored) {
1237 throw new StickerPackInvalidException("Could not find find " + pack.cover.file);
1238 }
1239
1240 String contentType = Utils.getFileMimeType(new File(pack.cover.file), null);
1241 cover = new StickerInfo(data.first(),
1242 data.second(),
1243 Optional.fromNullable(pack.cover.emoji).or(""),
1244 contentType);
1245 }
1246
1247 return new SignalServiceStickerManifestUpload(pack.title, pack.author, cover, stickers);
1248 }
1249
1250 private static JsonStickerPack parseStickerPack(String rootPath, ZipFile zip) throws IOException {
1251 InputStream inputStream;
1252 if (zip != null) {
1253 inputStream = zip.getInputStream(zip.getEntry("manifest.json"));
1254 } else {
1255 inputStream = new FileInputStream((new File(rootPath, "manifest.json")));
1256 }
1257 return new ObjectMapper().readValue(inputStream, JsonStickerPack.class);
1258 }
1259
1260 private static Pair<InputStream, Long> getInputStreamAndLength(
1261 final String rootPath, final ZipFile zip, final String subfile
1262 ) throws IOException {
1263 if (zip != null) {
1264 final ZipEntry entry = zip.getEntry(subfile);
1265 return new Pair<>(zip.getInputStream(entry), entry.getSize());
1266 } else {
1267 final File file = new File(rootPath, subfile);
1268 return new Pair<>(new FileInputStream(file), file.length());
1269 }
1270 }
1271
1272 void requestSyncGroups() throws IOException {
1273 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1274 .setType(SignalServiceProtos.SyncMessage.Request.Type.GROUPS)
1275 .build();
1276 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1277 try {
1278 sendSyncMessage(message);
1279 } catch (UntrustedIdentityException e) {
1280 e.printStackTrace();
1281 }
1282 }
1283
1284 void requestSyncContacts() throws IOException {
1285 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1286 .setType(SignalServiceProtos.SyncMessage.Request.Type.CONTACTS)
1287 .build();
1288 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1289 try {
1290 sendSyncMessage(message);
1291 } catch (UntrustedIdentityException e) {
1292 e.printStackTrace();
1293 }
1294 }
1295
1296 void requestSyncBlocked() throws IOException {
1297 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1298 .setType(SignalServiceProtos.SyncMessage.Request.Type.BLOCKED)
1299 .build();
1300 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1301 try {
1302 sendSyncMessage(message);
1303 } catch (UntrustedIdentityException e) {
1304 e.printStackTrace();
1305 }
1306 }
1307
1308 void requestSyncConfiguration() throws IOException {
1309 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1310 .setType(SignalServiceProtos.SyncMessage.Request.Type.CONFIGURATION)
1311 .build();
1312 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1313 try {
1314 sendSyncMessage(message);
1315 } catch (UntrustedIdentityException e) {
1316 e.printStackTrace();
1317 }
1318 }
1319
1320 private byte[] getSenderCertificate() {
1321 // TODO support UUID capable sender certificates
1322 // byte[] certificate = accountManager.getSenderCertificateForPhoneNumberPrivacy();
1323 byte[] certificate;
1324 try {
1325 certificate = accountManager.getSenderCertificate();
1326 } catch (IOException e) {
1327 System.err.println("Failed to get sender certificate: " + e);
1328 return null;
1329 }
1330 // TODO cache for a day
1331 return certificate;
1332 }
1333
1334 private void sendSyncMessage(SignalServiceSyncMessage message) throws IOException, UntrustedIdentityException {
1335 SignalServiceMessageSender messageSender = createMessageSender();
1336 try {
1337 messageSender.sendMessage(message, unidentifiedAccessHelper.getAccessForSync());
1338 } catch (UntrustedIdentityException e) {
1339 account.getSignalProtocolStore()
1340 .saveIdentity(resolveSignalServiceAddress(e.getIdentifier()),
1341 e.getIdentityKey(),
1342 TrustLevel.UNTRUSTED);
1343 throw e;
1344 }
1345 }
1346
1347 private Collection<SignalServiceAddress> getSignalServiceAddresses(Collection<String> numbers) throws InvalidNumberException {
1348 final Set<SignalServiceAddress> signalServiceAddresses = new HashSet<>(numbers.size());
1349 final Set<SignalServiceAddress> missingUuids = new HashSet<>();
1350
1351 for (String number : numbers) {
1352 final SignalServiceAddress resolvedAddress = canonicalizeAndResolveSignalServiceAddress(number);
1353 if (resolvedAddress.getUuid().isPresent()) {
1354 signalServiceAddresses.add(resolvedAddress);
1355 } else {
1356 missingUuids.add(resolvedAddress);
1357 }
1358 }
1359
1360 Map<String, UUID> registeredUsers;
1361 try {
1362 registeredUsers = accountManager.getRegisteredUsers(getIasKeyStore(),
1363 missingUuids.stream().map(a -> a.getNumber().get()).collect(Collectors.toSet()),
1364 CDS_MRENCLAVE);
1365 } catch (IOException | Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException e) {
1366 System.err.println("Failed to resolve uuids from server: " + e.getMessage());
1367 registeredUsers = new HashMap<>();
1368 }
1369
1370 for (SignalServiceAddress address : missingUuids) {
1371 final String number = address.getNumber().get();
1372 if (registeredUsers.containsKey(number)) {
1373 final SignalServiceAddress newAddress = resolveSignalServiceAddress(new SignalServiceAddress(
1374 registeredUsers.get(number),
1375 number));
1376 signalServiceAddresses.add(newAddress);
1377 } else {
1378 signalServiceAddresses.add(address);
1379 }
1380 }
1381
1382 return signalServiceAddresses;
1383 }
1384
1385 private Pair<Long, List<SendMessageResult>> sendMessage(
1386 SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients
1387 ) throws IOException {
1388 recipients = recipients.stream().map(this::resolveSignalServiceAddress).collect(Collectors.toSet());
1389 final long timestamp = System.currentTimeMillis();
1390 messageBuilder.withTimestamp(timestamp);
1391 getOrCreateMessagePipe();
1392 getOrCreateUnidentifiedMessagePipe();
1393 SignalServiceDataMessage message = null;
1394 try {
1395 message = messageBuilder.build();
1396 if (message.getGroupContext().isPresent()) {
1397 try {
1398 SignalServiceMessageSender messageSender = createMessageSender();
1399 final boolean isRecipientUpdate = false;
1400 List<SendMessageResult> result = messageSender.sendMessage(new ArrayList<>(recipients),
1401 unidentifiedAccessHelper.getAccessFor(recipients),
1402 isRecipientUpdate,
1403 message);
1404 for (SendMessageResult r : result) {
1405 if (r.getIdentityFailure() != null) {
1406 account.getSignalProtocolStore()
1407 .saveIdentity(r.getAddress(),
1408 r.getIdentityFailure().getIdentityKey(),
1409 TrustLevel.UNTRUSTED);
1410 }
1411 }
1412 return new Pair<>(timestamp, result);
1413 } catch (UntrustedIdentityException e) {
1414 account.getSignalProtocolStore()
1415 .saveIdentity(resolveSignalServiceAddress(e.getIdentifier()),
1416 e.getIdentityKey(),
1417 TrustLevel.UNTRUSTED);
1418 return new Pair<>(timestamp, Collections.emptyList());
1419 }
1420 } else {
1421 // Send to all individually, so sync messages are sent correctly
1422 List<SendMessageResult> results = new ArrayList<>(recipients.size());
1423 for (SignalServiceAddress address : recipients) {
1424 ContactInfo contact = account.getContactStore().getContact(address);
1425 if (contact != null) {
1426 messageBuilder.withExpiration(contact.messageExpirationTime);
1427 messageBuilder.withProfileKey(account.getProfileKey().serialize());
1428 } else {
1429 messageBuilder.withExpiration(0);
1430 messageBuilder.withProfileKey(null);
1431 }
1432 message = messageBuilder.build();
1433 if (address.matches(account.getSelfAddress())) {
1434 results.add(sendSelfMessage(message));
1435 } else {
1436 results.add(sendMessage(address, message));
1437 }
1438 }
1439 return new Pair<>(timestamp, results);
1440 }
1441 } finally {
1442 if (message != null && message.isEndSession()) {
1443 for (SignalServiceAddress recipient : recipients) {
1444 handleEndSession(recipient);
1445 }
1446 }
1447 account.save();
1448 }
1449 }
1450
1451 private SendMessageResult sendSelfMessage(SignalServiceDataMessage message) throws IOException {
1452 SignalServiceMessageSender messageSender = createMessageSender();
1453
1454 SignalServiceAddress recipient = account.getSelfAddress();
1455
1456 final Optional<UnidentifiedAccessPair> unidentifiedAccess = unidentifiedAccessHelper.getAccessFor(recipient);
1457 SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient),
1458 message.getTimestamp(),
1459 message,
1460 message.getExpiresInSeconds(),
1461 Collections.singletonMap(recipient, unidentifiedAccess.isPresent()),
1462 false);
1463 SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
1464
1465 try {
1466 long startTime = System.currentTimeMillis();
1467 messageSender.sendMessage(syncMessage, unidentifiedAccess);
1468 return SendMessageResult.success(recipient,
1469 unidentifiedAccess.isPresent(),
1470 false,
1471 System.currentTimeMillis() - startTime);
1472 } catch (UntrustedIdentityException e) {
1473 account.getSignalProtocolStore()
1474 .saveIdentity(resolveSignalServiceAddress(e.getIdentifier()),
1475 e.getIdentityKey(),
1476 TrustLevel.UNTRUSTED);
1477 return SendMessageResult.identityFailure(recipient, e.getIdentityKey());
1478 }
1479 }
1480
1481 private SendMessageResult sendMessage(
1482 SignalServiceAddress address, SignalServiceDataMessage message
1483 ) throws IOException {
1484 SignalServiceMessageSender messageSender = createMessageSender();
1485
1486 try {
1487 return messageSender.sendMessage(address, unidentifiedAccessHelper.getAccessFor(address), message);
1488 } catch (UntrustedIdentityException e) {
1489 account.getSignalProtocolStore()
1490 .saveIdentity(resolveSignalServiceAddress(e.getIdentifier()),
1491 e.getIdentityKey(),
1492 TrustLevel.UNTRUSTED);
1493 return SendMessageResult.identityFailure(address, e.getIdentityKey());
1494 }
1495 }
1496
1497 private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, org.whispersystems.libsignal.UntrustedIdentityException {
1498 SignalServiceCipher cipher = new SignalServiceCipher(account.getSelfAddress(),
1499 account.getSignalProtocolStore(),
1500 Utils.getCertificateValidator());
1501 try {
1502 return cipher.decrypt(envelope);
1503 } catch (ProtocolUntrustedIdentityException e) {
1504 if (e.getCause() instanceof org.whispersystems.libsignal.UntrustedIdentityException) {
1505 org.whispersystems.libsignal.UntrustedIdentityException identityException = (org.whispersystems.libsignal.UntrustedIdentityException) e
1506 .getCause();
1507 account.getSignalProtocolStore()
1508 .saveIdentity(resolveSignalServiceAddress(identityException.getName()),
1509 identityException.getUntrustedIdentity(),
1510 TrustLevel.UNTRUSTED);
1511 throw identityException;
1512 }
1513 throw new AssertionError(e);
1514 }
1515 }
1516
1517 private void handleEndSession(SignalServiceAddress source) {
1518 account.getSignalProtocolStore().deleteAllSessions(source);
1519 }
1520
1521 private static int currentTimeDays() {
1522 return (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
1523 }
1524
1525 private GroupsV2AuthorizationString getGroupAuthForToday(
1526 final GroupSecretParams groupSecretParams
1527 ) throws IOException {
1528 final int today = currentTimeDays();
1529 // Returns credentials for the next 7 days
1530 final HashMap<Integer, AuthCredentialResponse> credentials = groupsV2Api.getCredentials(today);
1531 // TODO cache credentials until they expire
1532 AuthCredentialResponse authCredentialResponse = credentials.get(today);
1533 try {
1534 return groupsV2Api.getGroupsV2AuthorizationString(account.getUuid(),
1535 today,
1536 groupSecretParams,
1537 authCredentialResponse);
1538 } catch (VerificationFailedException e) {
1539 throw new IOException(e);
1540 }
1541 }
1542
1543 private List<HandleAction> handleSignalServiceDataMessage(
1544 SignalServiceDataMessage message,
1545 boolean isSync,
1546 SignalServiceAddress source,
1547 SignalServiceAddress destination,
1548 boolean ignoreAttachments
1549 ) {
1550 List<HandleAction> actions = new ArrayList<>();
1551 if (message.getGroupContext().isPresent()) {
1552 if (message.getGroupContext().get().getGroupV1().isPresent()) {
1553 SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
1554 GroupIdV1 groupId = GroupId.v1(groupInfo.getGroupId());
1555 GroupInfo group = account.getGroupStore().getGroup(groupId);
1556 if (group == null || group instanceof GroupInfoV1) {
1557 GroupInfoV1 groupV1 = (GroupInfoV1) group;
1558 switch (groupInfo.getType()) {
1559 case UPDATE: {
1560 if (groupV1 == null) {
1561 groupV1 = new GroupInfoV1(groupId);
1562 }
1563
1564 if (groupInfo.getAvatar().isPresent()) {
1565 SignalServiceAttachment avatar = groupInfo.getAvatar().get();
1566 if (avatar.isPointer()) {
1567 try {
1568 retrieveGroupAvatarAttachment(avatar.asPointer(), groupV1.getGroupId());
1569 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1570 System.err.println("Failed to retrieve group avatar (" + avatar.asPointer()
1571 .getRemoteId() + "): " + e.getMessage());
1572 }
1573 }
1574 }
1575
1576 if (groupInfo.getName().isPresent()) {
1577 groupV1.name = groupInfo.getName().get();
1578 }
1579
1580 if (groupInfo.getMembers().isPresent()) {
1581 groupV1.addMembers(groupInfo.getMembers()
1582 .get()
1583 .stream()
1584 .map(this::resolveSignalServiceAddress)
1585 .collect(Collectors.toSet()));
1586 }
1587
1588 account.getGroupStore().updateGroup(groupV1);
1589 break;
1590 }
1591 case DELIVER:
1592 if (groupV1 == null && !isSync) {
1593 actions.add(new SendGroupInfoRequestAction(source, groupV1.getGroupId()));
1594 }
1595 break;
1596 case QUIT: {
1597 if (groupV1 != null) {
1598 groupV1.removeMember(source);
1599 account.getGroupStore().updateGroup(groupV1);
1600 }
1601 break;
1602 }
1603 case REQUEST_INFO:
1604 if (groupV1 != null && !isSync) {
1605 actions.add(new SendGroupUpdateAction(source, groupV1.getGroupId()));
1606 }
1607 break;
1608 }
1609 } else {
1610 // Received a group v1 message for a v2 group
1611 }
1612 }
1613 if (message.getGroupContext().get().getGroupV2().isPresent()) {
1614 final SignalServiceGroupV2 groupContext = message.getGroupContext().get().getGroupV2().get();
1615 final GroupMasterKey groupMasterKey = groupContext.getMasterKey();
1616
1617 getOrMigrateGroup(groupMasterKey,
1618 groupContext.getRevision(),
1619 groupContext.hasSignedGroupChange() ? groupContext.getSignedGroupChange() : null);
1620 }
1621 }
1622
1623 final SignalServiceAddress conversationPartnerAddress = isSync ? destination : source;
1624 if (conversationPartnerAddress != null && message.isEndSession()) {
1625 handleEndSession(conversationPartnerAddress);
1626 }
1627 if (message.isExpirationUpdate() || message.getBody().isPresent()) {
1628 if (message.getGroupContext().isPresent()) {
1629 if (message.getGroupContext().get().getGroupV1().isPresent()) {
1630 SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
1631 GroupInfoV1 group = account.getGroupStore().getOrCreateGroupV1(GroupId.v1(groupInfo.getGroupId()));
1632 if (group != null) {
1633 if (group.messageExpirationTime != message.getExpiresInSeconds()) {
1634 group.messageExpirationTime = message.getExpiresInSeconds();
1635 account.getGroupStore().updateGroup(group);
1636 }
1637 }
1638 } else if (message.getGroupContext().get().getGroupV2().isPresent()) {
1639 // disappearing message timer already stored in the DecryptedGroup
1640 }
1641 } else if (conversationPartnerAddress != null) {
1642 ContactInfo contact = account.getContactStore().getContact(conversationPartnerAddress);
1643 if (contact == null) {
1644 contact = new ContactInfo(conversationPartnerAddress);
1645 }
1646 if (contact.messageExpirationTime != message.getExpiresInSeconds()) {
1647 contact.messageExpirationTime = message.getExpiresInSeconds();
1648 account.getContactStore().updateContact(contact);
1649 }
1650 }
1651 }
1652 if (message.getAttachments().isPresent() && !ignoreAttachments) {
1653 for (SignalServiceAttachment attachment : message.getAttachments().get()) {
1654 if (attachment.isPointer()) {
1655 try {
1656 retrieveAttachment(attachment.asPointer());
1657 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1658 System.err.println("Failed to retrieve attachment ("
1659 + attachment.asPointer().getRemoteId()
1660 + "): "
1661 + e.getMessage());
1662 }
1663 }
1664 }
1665 }
1666 if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
1667 final ProfileKey profileKey;
1668 try {
1669 profileKey = new ProfileKey(message.getProfileKey().get());
1670 } catch (InvalidInputException e) {
1671 throw new AssertionError(e);
1672 }
1673 if (source.matches(account.getSelfAddress())) {
1674 this.account.setProfileKey(profileKey);
1675 }
1676 this.account.getProfileStore().storeProfileKey(source, profileKey);
1677 }
1678 if (message.getPreviews().isPresent()) {
1679 final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();
1680 for (SignalServiceDataMessage.Preview preview : previews) {
1681 if (preview.getImage().isPresent() && preview.getImage().get().isPointer()) {
1682 SignalServiceAttachmentPointer attachment = preview.getImage().get().asPointer();
1683 try {
1684 retrieveAttachment(attachment);
1685 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1686 System.err.println("Failed to retrieve attachment ("
1687 + attachment.getRemoteId()
1688 + "): "
1689 + e.getMessage());
1690 }
1691 }
1692 }
1693 }
1694 if (message.getQuote().isPresent()) {
1695 final SignalServiceDataMessage.Quote quote = message.getQuote().get();
1696
1697 for (SignalServiceDataMessage.Quote.QuotedAttachment quotedAttachment : quote.getAttachments()) {
1698 final SignalServiceAttachment attachment = quotedAttachment.getThumbnail();
1699 if (attachment != null && attachment.isPointer()) {
1700 try {
1701 retrieveAttachment(attachment.asPointer());
1702 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1703 System.err.println("Failed to retrieve attachment ("
1704 + attachment.asPointer().getRemoteId()
1705 + "): "
1706 + e.getMessage());
1707 }
1708 }
1709 }
1710 }
1711 if (message.getSticker().isPresent()) {
1712 final SignalServiceDataMessage.Sticker messageSticker = message.getSticker().get();
1713 Sticker sticker = account.getStickerStore().getSticker(messageSticker.getPackId());
1714 if (sticker == null) {
1715 sticker = new Sticker(messageSticker.getPackId(), messageSticker.getPackKey());
1716 account.getStickerStore().updateSticker(sticker);
1717 }
1718 }
1719 return actions;
1720 }
1721
1722 private GroupInfoV2 getOrMigrateGroup(
1723 final GroupMasterKey groupMasterKey, final int revision, final byte[] signedGroupChange
1724 ) {
1725 final GroupSecretParams groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
1726
1727 GroupIdV2 groupId = GroupUtils.getGroupIdV2(groupSecretParams);
1728 GroupInfo groupInfo = account.getGroupStore().getGroup(groupId);
1729 final GroupInfoV2 groupInfoV2;
1730 if (groupInfo instanceof GroupInfoV1) {
1731 // Received a v2 group message for a v1 group, we need to locally migrate the group
1732 account.getGroupStore().deleteGroup(groupInfo.getGroupId());
1733 groupInfoV2 = new GroupInfoV2(groupId, groupMasterKey);
1734 System.err.println("Locally migrated group "
1735 + groupInfo.getGroupId().toBase64()
1736 + " to group v2, id: "
1737 + groupInfoV2.getGroupId().toBase64()
1738 + " !!!");
1739 } else if (groupInfo instanceof GroupInfoV2) {
1740 groupInfoV2 = (GroupInfoV2) groupInfo;
1741 } else {
1742 groupInfoV2 = new GroupInfoV2(groupId, groupMasterKey);
1743 }
1744
1745 if (groupInfoV2.getGroup() == null || groupInfoV2.getGroup().getRevision() < revision) {
1746 DecryptedGroup group = null;
1747 if (signedGroupChange != null
1748 && groupInfoV2.getGroup() != null
1749 && groupInfoV2.getGroup().getRevision() + 1 == revision) {
1750 group = groupHelper.getUpdatedDecryptedGroup(groupInfoV2.getGroup(), signedGroupChange, groupMasterKey);
1751 }
1752 if (group == null) {
1753 group = groupHelper.getDecryptedGroup(groupSecretParams);
1754 }
1755 if (group != null) {
1756 storeProfileKeysFromMembers(group);
1757 try {
1758 retrieveGroupAvatar(groupId, groupSecretParams, group.getAvatar());
1759 } catch (IOException e) {
1760 System.err.println("Failed to download group avatar, ignoring ...");
1761 }
1762 }
1763 groupInfoV2.setGroup(group);
1764 account.getGroupStore().updateGroup(groupInfoV2);
1765 }
1766
1767 return groupInfoV2;
1768 }
1769
1770 private void storeProfileKeysFromMembers(final DecryptedGroup group) {
1771 for (DecryptedMember member : group.getMembersList()) {
1772 final SignalServiceAddress address = resolveSignalServiceAddress(new SignalServiceAddress(UuidUtil.parseOrThrow(
1773 member.getUuid().toByteArray()), null));
1774 try {
1775 account.getProfileStore()
1776 .storeProfileKey(address, new ProfileKey(member.getProfileKey().toByteArray()));
1777 } catch (InvalidInputException ignored) {
1778 }
1779 }
1780 }
1781
1782 private void retryFailedReceivedMessages(
1783 ReceiveMessageHandler handler, boolean ignoreAttachments
1784 ) {
1785 final File cachePath = new File(getMessageCachePath());
1786 if (!cachePath.exists()) {
1787 return;
1788 }
1789 for (final File dir : Objects.requireNonNull(cachePath.listFiles())) {
1790 if (!dir.isDirectory()) {
1791 retryFailedReceivedMessage(handler, ignoreAttachments, dir);
1792 continue;
1793 }
1794
1795 for (final File fileEntry : Objects.requireNonNull(dir.listFiles())) {
1796 if (!fileEntry.isFile()) {
1797 continue;
1798 }
1799 retryFailedReceivedMessage(handler, ignoreAttachments, fileEntry);
1800 }
1801 // Try to delete directory if empty
1802 dir.delete();
1803 }
1804 }
1805
1806 private void retryFailedReceivedMessage(
1807 final ReceiveMessageHandler handler, final boolean ignoreAttachments, final File fileEntry
1808 ) {
1809 SignalServiceEnvelope envelope;
1810 try {
1811 envelope = Utils.loadEnvelope(fileEntry);
1812 if (envelope == null) {
1813 return;
1814 }
1815 } catch (IOException e) {
1816 e.printStackTrace();
1817 return;
1818 }
1819 SignalServiceContent content = null;
1820 if (!envelope.isReceipt()) {
1821 try {
1822 content = decryptMessage(envelope);
1823 } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
1824 return;
1825 } catch (Exception er) {
1826 // All other errors are not recoverable, so delete the cached message
1827 try {
1828 Files.delete(fileEntry.toPath());
1829 } catch (IOException e) {
1830 System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage());
1831 }
1832 return;
1833 }
1834 List<HandleAction> actions = handleMessage(envelope, content, ignoreAttachments);
1835 for (HandleAction action : actions) {
1836 try {
1837 action.execute(this);
1838 } catch (Throwable e) {
1839 e.printStackTrace();
1840 }
1841 }
1842 }
1843 account.save();
1844 handler.handleMessage(envelope, content, null);
1845 try {
1846 Files.delete(fileEntry.toPath());
1847 } catch (IOException e) {
1848 System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage());
1849 }
1850 }
1851
1852 public void receiveMessages(
1853 long timeout,
1854 TimeUnit unit,
1855 boolean returnOnTimeout,
1856 boolean ignoreAttachments,
1857 ReceiveMessageHandler handler
1858 ) throws IOException {
1859 retryFailedReceivedMessages(handler, ignoreAttachments);
1860
1861 Set<HandleAction> queuedActions = null;
1862
1863 getOrCreateMessagePipe();
1864
1865 boolean hasCaughtUpWithOldMessages = false;
1866
1867 while (true) {
1868 SignalServiceEnvelope envelope;
1869 SignalServiceContent content = null;
1870 Exception exception = null;
1871 final long now = new Date().getTime();
1872 try {
1873 Optional<SignalServiceEnvelope> result = messagePipe.readOrEmpty(timeout, unit, envelope1 -> {
1874 // store message on disk, before acknowledging receipt to the server
1875 try {
1876 String source = envelope1.getSourceE164().isPresent() ? envelope1.getSourceE164().get() : "";
1877 File cacheFile = getMessageCacheFile(source, now, envelope1.getTimestamp());
1878 Utils.storeEnvelope(envelope1, cacheFile);
1879 } catch (IOException e) {
1880 System.err.println("Failed to store encrypted message in disk cache, ignoring: "
1881 + e.getMessage());
1882 }
1883 });
1884 if (result.isPresent()) {
1885 envelope = result.get();
1886 } else {
1887 // Received indicator that server queue is empty
1888 hasCaughtUpWithOldMessages = true;
1889
1890 if (queuedActions != null) {
1891 for (HandleAction action : queuedActions) {
1892 try {
1893 action.execute(this);
1894 } catch (Throwable e) {
1895 e.printStackTrace();
1896 }
1897 }
1898 account.save();
1899 queuedActions.clear();
1900 queuedActions = null;
1901 }
1902
1903 // Continue to wait another timeout for new messages
1904 continue;
1905 }
1906 } catch (TimeoutException e) {
1907 if (returnOnTimeout) return;
1908 continue;
1909 } catch (InvalidVersionException e) {
1910 System.err.println("Ignoring error: " + e.getMessage());
1911 continue;
1912 }
1913
1914 if (envelope.hasSource()) {
1915 // Store uuid if we don't have it already
1916 SignalServiceAddress source = envelope.getSourceAddress();
1917 resolveSignalServiceAddress(source);
1918 }
1919 if (!envelope.isReceipt()) {
1920 try {
1921 content = decryptMessage(envelope);
1922 } catch (Exception e) {
1923 exception = e;
1924 }
1925 List<HandleAction> actions = handleMessage(envelope, content, ignoreAttachments);
1926 if (hasCaughtUpWithOldMessages) {
1927 for (HandleAction action : actions) {
1928 try {
1929 action.execute(this);
1930 } catch (Throwable e) {
1931 e.printStackTrace();
1932 }
1933 }
1934 } else {
1935 if (queuedActions == null) {
1936 queuedActions = new HashSet<>();
1937 }
1938 queuedActions.addAll(actions);
1939 }
1940 }
1941 account.save();
1942 if (!isMessageBlocked(envelope, content)) {
1943 handler.handleMessage(envelope, content, exception);
1944 }
1945 if (!(exception instanceof org.whispersystems.libsignal.UntrustedIdentityException)) {
1946 File cacheFile = null;
1947 try {
1948 String source = envelope.getSourceE164().isPresent() ? envelope.getSourceE164().get() : "";
1949 cacheFile = getMessageCacheFile(source, now, envelope.getTimestamp());
1950 Files.delete(cacheFile.toPath());
1951 // Try to delete directory if empty
1952 new File(getMessageCachePath()).delete();
1953 } catch (IOException e) {
1954 System.err.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage());
1955 }
1956 }
1957 }
1958 }
1959
1960 private boolean isMessageBlocked(
1961 SignalServiceEnvelope envelope, SignalServiceContent content
1962 ) {
1963 SignalServiceAddress source;
1964 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
1965 source = envelope.getSourceAddress();
1966 } else if (content != null) {
1967 source = content.getSender();
1968 } else {
1969 return false;
1970 }
1971 ContactInfo sourceContact = account.getContactStore().getContact(source);
1972 if (sourceContact != null && sourceContact.blocked) {
1973 return true;
1974 }
1975
1976 if (content != null && content.getDataMessage().isPresent()) {
1977 SignalServiceDataMessage message = content.getDataMessage().get();
1978 if (message.getGroupContext().isPresent()) {
1979 if (message.getGroupContext().get().getGroupV1().isPresent()) {
1980 SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
1981 if (groupInfo.getType() != SignalServiceGroup.Type.DELIVER) {
1982 return false;
1983 }
1984 }
1985 GroupId groupId = GroupUtils.getGroupId(message.getGroupContext().get());
1986 GroupInfo group = account.getGroupStore().getGroup(groupId);
1987 if (group != null && group.isBlocked()) {
1988 return true;
1989 }
1990 }
1991 }
1992 return false;
1993 }
1994
1995 private List<HandleAction> handleMessage(
1996 SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments
1997 ) {
1998 List<HandleAction> actions = new ArrayList<>();
1999 if (content != null) {
2000 final SignalServiceAddress sender;
2001 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
2002 sender = envelope.getSourceAddress();
2003 } else {
2004 sender = content.getSender();
2005 }
2006 // Store uuid if we don't have it already
2007 resolveSignalServiceAddress(sender);
2008
2009 if (content.getDataMessage().isPresent()) {
2010 SignalServiceDataMessage message = content.getDataMessage().get();
2011
2012 if (content.isNeedsReceipt()) {
2013 actions.add(new SendReceiptAction(sender, message.getTimestamp()));
2014 }
2015
2016 actions.addAll(handleSignalServiceDataMessage(message,
2017 false,
2018 sender,
2019 account.getSelfAddress(),
2020 ignoreAttachments));
2021 }
2022 if (content.getSyncMessage().isPresent()) {
2023 account.setMultiDevice(true);
2024 SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
2025 if (syncMessage.getSent().isPresent()) {
2026 SentTranscriptMessage message = syncMessage.getSent().get();
2027 final SignalServiceAddress destination = message.getDestination().orNull();
2028 actions.addAll(handleSignalServiceDataMessage(message.getMessage(),
2029 true,
2030 sender,
2031 destination,
2032 ignoreAttachments));
2033 }
2034 if (syncMessage.getRequest().isPresent()) {
2035 RequestMessage rm = syncMessage.getRequest().get();
2036 if (rm.isContactsRequest()) {
2037 actions.add(SendSyncContactsAction.create());
2038 }
2039 if (rm.isGroupsRequest()) {
2040 actions.add(SendSyncGroupsAction.create());
2041 }
2042 if (rm.isBlockedListRequest()) {
2043 actions.add(SendSyncBlockedListAction.create());
2044 }
2045 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2046 }
2047 if (syncMessage.getGroups().isPresent()) {
2048 File tmpFile = null;
2049 try {
2050 tmpFile = IOUtils.createTempFile();
2051 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups()
2052 .get()
2053 .asPointer(), tmpFile)) {
2054 DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream);
2055 DeviceGroup g;
2056 while ((g = s.read()) != null) {
2057 GroupInfoV1 syncGroup = account.getGroupStore()
2058 .getOrCreateGroupV1(GroupId.v1(g.getId()));
2059 if (syncGroup != null) {
2060 if (g.getName().isPresent()) {
2061 syncGroup.name = g.getName().get();
2062 }
2063 syncGroup.addMembers(g.getMembers()
2064 .stream()
2065 .map(this::resolveSignalServiceAddress)
2066 .collect(Collectors.toSet()));
2067 if (!g.isActive()) {
2068 syncGroup.removeMember(account.getSelfAddress());
2069 } else {
2070 // Add ourself to the member set as it's marked as active
2071 syncGroup.addMembers(Collections.singleton(account.getSelfAddress()));
2072 }
2073 syncGroup.blocked = g.isBlocked();
2074 if (g.getColor().isPresent()) {
2075 syncGroup.color = g.getColor().get();
2076 }
2077
2078 if (g.getAvatar().isPresent()) {
2079 retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.getGroupId());
2080 }
2081 syncGroup.inboxPosition = g.getInboxPosition().orNull();
2082 syncGroup.archived = g.isArchived();
2083 account.getGroupStore().updateGroup(syncGroup);
2084 }
2085 }
2086 }
2087 } catch (Exception e) {
2088 e.printStackTrace();
2089 } finally {
2090 if (tmpFile != null) {
2091 try {
2092 Files.delete(tmpFile.toPath());
2093 } catch (IOException e) {
2094 System.err.println("Failed to delete received groups temp file “"
2095 + tmpFile
2096 + "”: "
2097 + e.getMessage());
2098 }
2099 }
2100 }
2101 }
2102 if (syncMessage.getBlockedList().isPresent()) {
2103 final BlockedListMessage blockedListMessage = syncMessage.getBlockedList().get();
2104 for (SignalServiceAddress address : blockedListMessage.getAddresses()) {
2105 setContactBlocked(resolveSignalServiceAddress(address), true);
2106 }
2107 for (GroupId groupId : blockedListMessage.getGroupIds()
2108 .stream()
2109 .map(GroupId::unknownVersion)
2110 .collect(Collectors.toSet())) {
2111 try {
2112 setGroupBlocked(groupId, true);
2113 } catch (GroupNotFoundException e) {
2114 System.err.println("BlockedListMessage contained groupID that was not found in GroupStore: "
2115 + groupId.toBase64());
2116 }
2117 }
2118 }
2119 if (syncMessage.getContacts().isPresent()) {
2120 File tmpFile = null;
2121 try {
2122 tmpFile = IOUtils.createTempFile();
2123 final ContactsMessage contactsMessage = syncMessage.getContacts().get();
2124 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream()
2125 .asPointer(), tmpFile)) {
2126 DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream);
2127 if (contactsMessage.isComplete()) {
2128 account.getContactStore().clear();
2129 }
2130 DeviceContact c;
2131 while ((c = s.read()) != null) {
2132 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
2133 account.setProfileKey(c.getProfileKey().get());
2134 }
2135 final SignalServiceAddress address = resolveSignalServiceAddress(c.getAddress());
2136 ContactInfo contact = account.getContactStore().getContact(address);
2137 if (contact == null) {
2138 contact = new ContactInfo(address);
2139 }
2140 if (c.getName().isPresent()) {
2141 contact.name = c.getName().get();
2142 }
2143 if (c.getColor().isPresent()) {
2144 contact.color = c.getColor().get();
2145 }
2146 if (c.getProfileKey().isPresent()) {
2147 account.getProfileStore().storeProfileKey(address, c.getProfileKey().get());
2148 }
2149 if (c.getVerified().isPresent()) {
2150 final VerifiedMessage verifiedMessage = c.getVerified().get();
2151 account.getSignalProtocolStore()
2152 .setIdentityTrustLevel(verifiedMessage.getDestination(),
2153 verifiedMessage.getIdentityKey(),
2154 TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
2155 }
2156 if (c.getExpirationTimer().isPresent()) {
2157 contact.messageExpirationTime = c.getExpirationTimer().get();
2158 }
2159 contact.blocked = c.isBlocked();
2160 contact.inboxPosition = c.getInboxPosition().orNull();
2161 contact.archived = c.isArchived();
2162 account.getContactStore().updateContact(contact);
2163
2164 if (c.getAvatar().isPresent()) {
2165 retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number);
2166 }
2167 }
2168 }
2169 } catch (Exception e) {
2170 e.printStackTrace();
2171 } finally {
2172 if (tmpFile != null) {
2173 try {
2174 Files.delete(tmpFile.toPath());
2175 } catch (IOException e) {
2176 System.err.println("Failed to delete received contacts temp file “"
2177 + tmpFile
2178 + "”: "
2179 + e.getMessage());
2180 }
2181 }
2182 }
2183 }
2184 if (syncMessage.getVerified().isPresent()) {
2185 final VerifiedMessage verifiedMessage = syncMessage.getVerified().get();
2186 account.getSignalProtocolStore()
2187 .setIdentityTrustLevel(resolveSignalServiceAddress(verifiedMessage.getDestination()),
2188 verifiedMessage.getIdentityKey(),
2189 TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
2190 }
2191 if (syncMessage.getStickerPackOperations().isPresent()) {
2192 final List<StickerPackOperationMessage> stickerPackOperationMessages = syncMessage.getStickerPackOperations()
2193 .get();
2194 for (StickerPackOperationMessage m : stickerPackOperationMessages) {
2195 if (!m.getPackId().isPresent()) {
2196 continue;
2197 }
2198 Sticker sticker = account.getStickerStore().getSticker(m.getPackId().get());
2199 if (sticker == null) {
2200 if (!m.getPackKey().isPresent()) {
2201 continue;
2202 }
2203 sticker = new Sticker(m.getPackId().get(), m.getPackKey().get());
2204 }
2205 sticker.setInstalled(!m.getType().isPresent()
2206 || m.getType().get() == StickerPackOperationMessage.Type.INSTALL);
2207 account.getStickerStore().updateSticker(sticker);
2208 }
2209 }
2210 if (syncMessage.getConfiguration().isPresent()) {
2211 // TODO
2212 }
2213 }
2214 }
2215 return actions;
2216 }
2217
2218 private File getContactAvatarFile(String number) {
2219 return new File(pathConfig.getAvatarsPath(), "contact-" + number);
2220 }
2221
2222 private File retrieveContactAvatarAttachment(
2223 SignalServiceAttachment attachment, String number
2224 ) throws IOException, InvalidMessageException, MissingConfigurationException {
2225 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
2226 if (attachment.isPointer()) {
2227 SignalServiceAttachmentPointer pointer = attachment.asPointer();
2228 return retrieveAttachment(pointer, getContactAvatarFile(number), false);
2229 } else {
2230 SignalServiceAttachmentStream stream = attachment.asStream();
2231 return Utils.retrieveAttachment(stream, getContactAvatarFile(number));
2232 }
2233 }
2234
2235 private File getGroupAvatarFile(GroupId groupId) {
2236 return new File(pathConfig.getAvatarsPath(), "group-" + groupId.toBase64().replace("/", "_"));
2237 }
2238
2239 private File retrieveGroupAvatarAttachment(
2240 SignalServiceAttachment attachment, GroupId groupId
2241 ) throws IOException, InvalidMessageException, MissingConfigurationException {
2242 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
2243 if (attachment.isPointer()) {
2244 SignalServiceAttachmentPointer pointer = attachment.asPointer();
2245 return retrieveAttachment(pointer, getGroupAvatarFile(groupId), false);
2246 } else {
2247 SignalServiceAttachmentStream stream = attachment.asStream();
2248 return Utils.retrieveAttachment(stream, getGroupAvatarFile(groupId));
2249 }
2250 }
2251
2252 private File retrieveGroupAvatar(
2253 GroupId groupId, GroupSecretParams groupSecretParams, String cdnKey
2254 ) throws IOException {
2255 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
2256 SignalServiceMessageReceiver receiver = getOrCreateMessageReceiver();
2257 File outputFile = getGroupAvatarFile(groupId);
2258 GroupsV2Operations.GroupOperations groupOperations = groupsV2Operations.forGroup(groupSecretParams);
2259
2260 File tmpFile = IOUtils.createTempFile();
2261 tmpFile.deleteOnExit();
2262 try (InputStream input = receiver.retrieveGroupsV2ProfileAvatar(cdnKey,
2263 tmpFile,
2264 ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
2265 byte[] encryptedData = IOUtils.readFully(input);
2266
2267 byte[] decryptedData = groupOperations.decryptAvatar(encryptedData);
2268 try (OutputStream output = new FileOutputStream(outputFile)) {
2269 output.write(decryptedData);
2270 }
2271 } finally {
2272 try {
2273 Files.delete(tmpFile.toPath());
2274 } catch (IOException e) {
2275 System.err.println("Failed to delete received avatar temp file “" + tmpFile + "”: " + e.getMessage());
2276 }
2277 }
2278 return outputFile;
2279 }
2280
2281 private File getProfileAvatarFile(SignalServiceAddress address) {
2282 return new File(pathConfig.getAvatarsPath(), "profile-" + address.getLegacyIdentifier());
2283 }
2284
2285 private File retrieveProfileAvatar(
2286 SignalServiceAddress address, String avatarPath, ProfileKey profileKey
2287 ) throws IOException {
2288 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
2289 SignalServiceMessageReceiver receiver = getOrCreateMessageReceiver();
2290 File outputFile = getProfileAvatarFile(address);
2291
2292 File tmpFile = IOUtils.createTempFile();
2293 try (InputStream input = receiver.retrieveProfileAvatar(avatarPath,
2294 tmpFile,
2295 profileKey,
2296 ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
2297 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2298 IOUtils.copyStreamToFile(input, outputFile, (int) ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
2299 } finally {
2300 try {
2301 Files.delete(tmpFile.toPath());
2302 } catch (IOException e) {
2303 System.err.println("Failed to delete received avatar temp file “" + tmpFile + "”: " + e.getMessage());
2304 }
2305 }
2306 return outputFile;
2307 }
2308
2309 public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) {
2310 return new File(pathConfig.getAttachmentsPath(), attachmentId.toString());
2311 }
2312
2313 private File retrieveAttachment(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException, MissingConfigurationException {
2314 IOUtils.createPrivateDirectories(pathConfig.getAttachmentsPath());
2315 return retrieveAttachment(pointer, getAttachmentFile(pointer.getRemoteId()), true);
2316 }
2317
2318 private File retrieveAttachment(
2319 SignalServiceAttachmentPointer pointer, File outputFile, boolean storePreview
2320 ) throws IOException, InvalidMessageException, MissingConfigurationException {
2321 if (storePreview && pointer.getPreview().isPresent()) {
2322 File previewFile = new File(outputFile + ".preview");
2323 try (OutputStream output = new FileOutputStream(previewFile)) {
2324 byte[] preview = pointer.getPreview().get();
2325 output.write(preview, 0, preview.length);
2326 } catch (FileNotFoundException e) {
2327 e.printStackTrace();
2328 return null;
2329 }
2330 }
2331
2332 final SignalServiceMessageReceiver messageReceiver = getOrCreateMessageReceiver();
2333
2334 File tmpFile = IOUtils.createTempFile();
2335 try (InputStream input = messageReceiver.retrieveAttachment(pointer,
2336 tmpFile,
2337 ServiceConfig.MAX_ATTACHMENT_SIZE)) {
2338 IOUtils.copyStreamToFile(input, outputFile);
2339 } finally {
2340 try {
2341 Files.delete(tmpFile.toPath());
2342 } catch (IOException e) {
2343 System.err.println("Failed to delete received attachment temp file “"
2344 + tmpFile
2345 + "”: "
2346 + e.getMessage());
2347 }
2348 }
2349 return outputFile;
2350 }
2351
2352 private InputStream retrieveAttachmentAsStream(
2353 SignalServiceAttachmentPointer pointer, File tmpFile
2354 ) throws IOException, InvalidMessageException, MissingConfigurationException {
2355 final SignalServiceMessageReceiver messageReceiver = getOrCreateMessageReceiver();
2356 return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
2357 }
2358
2359 void sendGroups() throws IOException, UntrustedIdentityException {
2360 File groupsFile = IOUtils.createTempFile();
2361
2362 try {
2363 try (OutputStream fos = new FileOutputStream(groupsFile)) {
2364 DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
2365 for (GroupInfo record : account.getGroupStore().getGroups()) {
2366 if (record instanceof GroupInfoV1) {
2367 GroupInfoV1 groupInfo = (GroupInfoV1) record;
2368 out.write(new DeviceGroup(groupInfo.getGroupId().serialize(),
2369 Optional.fromNullable(groupInfo.name),
2370 new ArrayList<>(groupInfo.getMembers()),
2371 createGroupAvatarAttachment(groupInfo.getGroupId()),
2372 groupInfo.isMember(account.getSelfAddress()),
2373 Optional.of(groupInfo.messageExpirationTime),
2374 Optional.fromNullable(groupInfo.color),
2375 groupInfo.blocked,
2376 Optional.fromNullable(groupInfo.inboxPosition),
2377 groupInfo.archived));
2378 }
2379 }
2380 }
2381
2382 if (groupsFile.exists() && groupsFile.length() > 0) {
2383 try (FileInputStream groupsFileStream = new FileInputStream(groupsFile)) {
2384 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
2385 .withStream(groupsFileStream)
2386 .withContentType("application/octet-stream")
2387 .withLength(groupsFile.length())
2388 .build();
2389
2390 sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
2391 }
2392 }
2393 } finally {
2394 try {
2395 Files.delete(groupsFile.toPath());
2396 } catch (IOException e) {
2397 System.err.println("Failed to delete groups temp file “" + groupsFile + "”: " + e.getMessage());
2398 }
2399 }
2400 }
2401
2402 public void sendContacts() throws IOException, UntrustedIdentityException {
2403 File contactsFile = IOUtils.createTempFile();
2404
2405 try {
2406 try (OutputStream fos = new FileOutputStream(contactsFile)) {
2407 DeviceContactsOutputStream out = new DeviceContactsOutputStream(fos);
2408 for (ContactInfo record : account.getContactStore().getContacts()) {
2409 VerifiedMessage verifiedMessage = null;
2410 JsonIdentityKeyStore.Identity currentIdentity = account.getSignalProtocolStore()
2411 .getIdentity(record.getAddress());
2412 if (currentIdentity != null) {
2413 verifiedMessage = new VerifiedMessage(record.getAddress(),
2414 currentIdentity.getIdentityKey(),
2415 currentIdentity.getTrustLevel().toVerifiedState(),
2416 currentIdentity.getDateAdded().getTime());
2417 }
2418
2419 ProfileKey profileKey = account.getProfileStore().getProfileKey(record.getAddress());
2420 out.write(new DeviceContact(record.getAddress(),
2421 Optional.fromNullable(record.name),
2422 createContactAvatarAttachment(record.number),
2423 Optional.fromNullable(record.color),
2424 Optional.fromNullable(verifiedMessage),
2425 Optional.fromNullable(profileKey),
2426 record.blocked,
2427 Optional.of(record.messageExpirationTime),
2428 Optional.fromNullable(record.inboxPosition),
2429 record.archived));
2430 }
2431
2432 if (account.getProfileKey() != null) {
2433 // Send our own profile key as well
2434 out.write(new DeviceContact(account.getSelfAddress(),
2435 Optional.absent(),
2436 Optional.absent(),
2437 Optional.absent(),
2438 Optional.absent(),
2439 Optional.of(account.getProfileKey()),
2440 false,
2441 Optional.absent(),
2442 Optional.absent(),
2443 false));
2444 }
2445 }
2446
2447 if (contactsFile.exists() && contactsFile.length() > 0) {
2448 try (FileInputStream contactsFileStream = new FileInputStream(contactsFile)) {
2449 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
2450 .withStream(contactsFileStream)
2451 .withContentType("application/octet-stream")
2452 .withLength(contactsFile.length())
2453 .build();
2454
2455 sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true)));
2456 }
2457 }
2458 } finally {
2459 try {
2460 Files.delete(contactsFile.toPath());
2461 } catch (IOException e) {
2462 System.err.println("Failed to delete contacts temp file “" + contactsFile + "”: " + e.getMessage());
2463 }
2464 }
2465 }
2466
2467 void sendBlockedList() throws IOException, UntrustedIdentityException {
2468 List<SignalServiceAddress> addresses = new ArrayList<>();
2469 for (ContactInfo record : account.getContactStore().getContacts()) {
2470 if (record.blocked) {
2471 addresses.add(record.getAddress());
2472 }
2473 }
2474 List<byte[]> groupIds = new ArrayList<>();
2475 for (GroupInfo record : account.getGroupStore().getGroups()) {
2476 if (record.isBlocked()) {
2477 groupIds.add(record.getGroupId().serialize());
2478 }
2479 }
2480 sendSyncMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(addresses, groupIds)));
2481 }
2482
2483 private void sendVerifiedMessage(
2484 SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel
2485 ) throws IOException, UntrustedIdentityException {
2486 VerifiedMessage verifiedMessage = new VerifiedMessage(destination,
2487 identityKey,
2488 trustLevel.toVerifiedState(),
2489 System.currentTimeMillis());
2490 sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
2491 }
2492
2493 public List<ContactInfo> getContacts() {
2494 return account.getContactStore().getContacts();
2495 }
2496
2497 public ContactInfo getContact(String number) {
2498 return account.getContactStore().getContact(Util.getSignalServiceAddressFromIdentifier(number));
2499 }
2500
2501 public GroupInfo getGroup(GroupId groupId) {
2502 return account.getGroupStore().getGroup(groupId);
2503 }
2504
2505 public List<JsonIdentityKeyStore.Identity> getIdentities() {
2506 return account.getSignalProtocolStore().getIdentities();
2507 }
2508
2509 public List<JsonIdentityKeyStore.Identity> getIdentities(String number) throws InvalidNumberException {
2510 return account.getSignalProtocolStore().getIdentities(canonicalizeAndResolveSignalServiceAddress(number));
2511 }
2512
2513 /**
2514 * Trust this the identity with this fingerprint
2515 *
2516 * @param name username of the identity
2517 * @param fingerprint Fingerprint
2518 */
2519 public boolean trustIdentityVerified(String name, byte[] fingerprint) throws InvalidNumberException {
2520 SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(name);
2521 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
2522 if (ids == null) {
2523 return false;
2524 }
2525 for (JsonIdentityKeyStore.Identity id : ids) {
2526 if (!Arrays.equals(id.getIdentityKey().serialize(), fingerprint)) {
2527 continue;
2528 }
2529
2530 account.getSignalProtocolStore()
2531 .setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
2532 try {
2533 sendVerifiedMessage(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
2534 } catch (IOException | UntrustedIdentityException e) {
2535 e.printStackTrace();
2536 }
2537 account.save();
2538 return true;
2539 }
2540 return false;
2541 }
2542
2543 /**
2544 * Trust this the identity with this safety number
2545 *
2546 * @param name username of the identity
2547 * @param safetyNumber Safety number
2548 */
2549 public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) throws InvalidNumberException {
2550 SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(name);
2551 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
2552 if (ids == null) {
2553 return false;
2554 }
2555 for (JsonIdentityKeyStore.Identity id : ids) {
2556 if (!safetyNumber.equals(computeSafetyNumber(address, id.getIdentityKey()))) {
2557 continue;
2558 }
2559
2560 account.getSignalProtocolStore()
2561 .setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
2562 try {
2563 sendVerifiedMessage(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
2564 } catch (IOException | UntrustedIdentityException e) {
2565 e.printStackTrace();
2566 }
2567 account.save();
2568 return true;
2569 }
2570 return false;
2571 }
2572
2573 /**
2574 * Trust all keys of this identity without verification
2575 *
2576 * @param name username of the identity
2577 */
2578 public boolean trustIdentityAllKeys(String name) {
2579 SignalServiceAddress address = resolveSignalServiceAddress(name);
2580 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
2581 if (ids == null) {
2582 return false;
2583 }
2584 for (JsonIdentityKeyStore.Identity id : ids) {
2585 if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
2586 account.getSignalProtocolStore()
2587 .setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
2588 try {
2589 sendVerifiedMessage(address, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
2590 } catch (IOException | UntrustedIdentityException e) {
2591 e.printStackTrace();
2592 }
2593 }
2594 }
2595 account.save();
2596 return true;
2597 }
2598
2599 public String computeSafetyNumber(
2600 SignalServiceAddress theirAddress, IdentityKey theirIdentityKey
2601 ) {
2602 return Utils.computeSafetyNumber(account.getSelfAddress(),
2603 getIdentityKeyPair().getPublicKey(),
2604 theirAddress,
2605 theirIdentityKey);
2606 }
2607
2608 void saveAccount() {
2609 account.save();
2610 }
2611
2612 public SignalServiceAddress canonicalizeAndResolveSignalServiceAddress(String identifier) throws InvalidNumberException {
2613 String canonicalizedNumber = UuidUtil.isUuid(identifier)
2614 ? identifier
2615 : Util.canonicalizeNumber(identifier, account.getUsername());
2616 return resolveSignalServiceAddress(canonicalizedNumber);
2617 }
2618
2619 public SignalServiceAddress resolveSignalServiceAddress(String identifier) {
2620 SignalServiceAddress address = Util.getSignalServiceAddressFromIdentifier(identifier);
2621
2622 return resolveSignalServiceAddress(address);
2623 }
2624
2625 public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
2626 if (address.matches(account.getSelfAddress())) {
2627 return account.getSelfAddress();
2628 }
2629
2630 return account.getRecipientStore().resolveServiceAddress(address);
2631 }
2632
2633 @Override
2634 public void close() throws IOException {
2635 if (messagePipe != null) {
2636 messagePipe.shutdown();
2637 messagePipe = null;
2638 }
2639
2640 if (unidentifiedMessagePipe != null) {
2641 unidentifiedMessagePipe.shutdown();
2642 unidentifiedMessagePipe = null;
2643 }
2644
2645 account.close();
2646 }
2647
2648 public interface ReceiveMessageHandler {
2649
2650 void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, Throwable e);
2651 }
2652 }