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