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