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