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