]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
Skip invalid contact records
[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 SignalServiceDataMessage.Builder messageBuilder;
783
784 final var g = getGroupForUpdating(groupId);
785 if (g instanceof GroupInfoV1) {
786 var groupInfoV1 = (GroupInfoV1) g;
787 var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT).withId(groupId.serialize()).build();
788 messageBuilder = SignalServiceDataMessage.newBuilder().asGroupMessage(group);
789 groupInfoV1.removeMember(account.getSelfRecipientId());
790 account.getGroupStore().updateGroup(groupInfoV1);
791 } else {
792 final var groupInfoV2 = (GroupInfoV2) g;
793 final var currentAdmins = g.getAdminMembers();
794 final var newAdmins = getSignalServiceAddresses(groupAdmins);
795 newAdmins.removeAll(currentAdmins);
796 newAdmins.retainAll(g.getMembers());
797 if (currentAdmins.contains(getSelfRecipientId())
798 && currentAdmins.size() == 1
799 && g.getMembers().size() > 1
800 && newAdmins.size() == 0) {
801 // Last admin can't leave the group, unless she's also the last member
802 throw new LastGroupAdminException(g.getGroupId(), g.getTitle());
803 }
804 final var groupGroupChangePair = groupV2Helper.leaveGroup(groupInfoV2, newAdmins);
805 groupInfoV2.setGroup(groupGroupChangePair.first(), this::resolveRecipient);
806 messageBuilder = getGroupUpdateMessageBuilder(groupInfoV2, groupGroupChangePair.second().toByteArray());
807 account.getGroupStore().updateGroup(groupInfoV2);
808 }
809
810 return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfRecipientId()));
811 }
812
813 public Pair<GroupId, List<SendMessageResult>> createGroup(
814 String name, List<String> members, File avatarFile
815 ) throws IOException, AttachmentInvalidException, InvalidNumberException {
816 return createGroup(name, members == null ? null : getSignalServiceAddresses(members), avatarFile);
817 }
818
819 private Pair<GroupId, List<SendMessageResult>> createGroup(
820 String name, Set<RecipientId> members, File avatarFile
821 ) throws IOException, AttachmentInvalidException {
822 final var selfRecipientId = account.getSelfRecipientId();
823 if (members != null && members.contains(selfRecipientId)) {
824 members = new HashSet<>(members);
825 members.remove(selfRecipientId);
826 }
827
828 var gv2Pair = groupV2Helper.createGroup(name == null ? "" : name,
829 members == null ? Set.of() : members,
830 avatarFile);
831
832 SignalServiceDataMessage.Builder messageBuilder;
833 if (gv2Pair == null) {
834 // Failed to create v2 group, creating v1 group instead
835 var gv1 = new GroupInfoV1(GroupIdV1.createRandom());
836 gv1.addMembers(List.of(selfRecipientId));
837 final var result = updateGroupV1(gv1, name, members, avatarFile);
838 return new Pair<>(gv1.getGroupId(), result.second());
839 }
840
841 final var gv2 = gv2Pair.first();
842 final var decryptedGroup = gv2Pair.second();
843
844 gv2.setGroup(decryptedGroup, this::resolveRecipient);
845 if (avatarFile != null) {
846 avatarStore.storeGroupAvatar(gv2.getGroupId(),
847 outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
848 }
849 messageBuilder = getGroupUpdateMessageBuilder(gv2, null);
850 account.getGroupStore().updateGroup(gv2);
851
852 final var result = sendMessage(messageBuilder, gv2.getMembersIncludingPendingWithout(selfRecipientId));
853 return new Pair<>(gv2.getGroupId(), result.second());
854 }
855
856 public Pair<Long, List<SendMessageResult>> updateGroup(
857 GroupId groupId,
858 String name,
859 String description,
860 List<String> members,
861 List<String> removeMembers,
862 List<String> admins,
863 List<String> removeAdmins,
864 boolean resetGroupLink,
865 GroupLinkState groupLinkState,
866 GroupPermission addMemberPermission,
867 GroupPermission editDetailsPermission,
868 File avatarFile,
869 Integer expirationTimer
870 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
871 return updateGroup(groupId,
872 name,
873 description,
874 members == null ? null : getSignalServiceAddresses(members),
875 removeMembers == null ? null : getSignalServiceAddresses(removeMembers),
876 admins == null ? null : getSignalServiceAddresses(admins),
877 removeAdmins == null ? null : getSignalServiceAddresses(removeAdmins),
878 resetGroupLink,
879 groupLinkState,
880 addMemberPermission,
881 editDetailsPermission,
882 avatarFile,
883 expirationTimer);
884 }
885
886 private Pair<Long, List<SendMessageResult>> updateGroup(
887 final GroupId groupId,
888 final String name,
889 final String description,
890 final Set<RecipientId> members,
891 final Set<RecipientId> removeMembers,
892 final Set<RecipientId> admins,
893 final Set<RecipientId> removeAdmins,
894 final boolean resetGroupLink,
895 final GroupLinkState groupLinkState,
896 final GroupPermission addMemberPermission,
897 final GroupPermission editDetailsPermission,
898 final File avatarFile,
899 final Integer expirationTimer
900 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
901 var group = getGroupForUpdating(groupId);
902
903 if (group instanceof GroupInfoV2) {
904 try {
905 return updateGroupV2((GroupInfoV2) group,
906 name,
907 description,
908 members,
909 removeMembers,
910 admins,
911 removeAdmins,
912 resetGroupLink,
913 groupLinkState,
914 addMemberPermission,
915 editDetailsPermission,
916 avatarFile,
917 expirationTimer);
918 } catch (ConflictException e) {
919 // Detected conflicting update, refreshing group and trying again
920 group = getGroup(groupId, true);
921 return updateGroupV2((GroupInfoV2) group,
922 name,
923 description,
924 members,
925 removeMembers,
926 admins,
927 removeAdmins,
928 resetGroupLink,
929 groupLinkState,
930 addMemberPermission,
931 editDetailsPermission,
932 avatarFile,
933 expirationTimer);
934 }
935 }
936
937 final var gv1 = (GroupInfoV1) group;
938 final var result = updateGroupV1(gv1, name, members, avatarFile);
939 if (expirationTimer != null) {
940 setExpirationTimer(gv1, expirationTimer);
941 }
942 return result;
943 }
944
945 private Pair<Long, List<SendMessageResult>> updateGroupV1(
946 final GroupInfoV1 gv1, final String name, final Set<RecipientId> members, final File avatarFile
947 ) throws IOException, AttachmentInvalidException {
948 updateGroupV1Details(gv1, name, members, avatarFile);
949 var messageBuilder = getGroupUpdateMessageBuilder(gv1);
950
951 account.getGroupStore().updateGroup(gv1);
952
953 return sendMessage(messageBuilder, gv1.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
954 }
955
956 private void updateGroupV1Details(
957 final GroupInfoV1 g, final String name, final Collection<RecipientId> members, final File avatarFile
958 ) throws IOException {
959 if (name != null) {
960 g.name = name;
961 }
962
963 if (members != null) {
964 final var newMemberAddresses = members.stream()
965 .filter(member -> !g.isMember(member))
966 .map(this::resolveSignalServiceAddress)
967 .collect(Collectors.toList());
968 final var newE164Members = new HashSet<String>();
969 for (var member : newMemberAddresses) {
970 if (!member.getNumber().isPresent()) {
971 continue;
972 }
973 newE164Members.add(member.getNumber().get());
974 }
975
976 final var registeredUsers = getRegisteredUsers(newE164Members);
977 if (registeredUsers.size() != newE164Members.size()) {
978 // Some of the new members are not registered on Signal
979 newE164Members.removeAll(registeredUsers.keySet());
980 throw new IOException("Failed to add members "
981 + String.join(", ", newE164Members)
982 + " to group: Not registered on Signal");
983 }
984
985 g.addMembers(members);
986 }
987
988 if (avatarFile != null) {
989 avatarStore.storeGroupAvatar(g.getGroupId(),
990 outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
991 }
992 }
993
994 private Pair<Long, List<SendMessageResult>> updateGroupV2(
995 final GroupInfoV2 group,
996 final String name,
997 final String description,
998 final Set<RecipientId> members,
999 final Set<RecipientId> removeMembers,
1000 final Set<RecipientId> admins,
1001 final Set<RecipientId> removeAdmins,
1002 final boolean resetGroupLink,
1003 final GroupLinkState groupLinkState,
1004 final GroupPermission addMemberPermission,
1005 final GroupPermission editDetailsPermission,
1006 final File avatarFile,
1007 Integer expirationTimer
1008 ) throws IOException {
1009 Pair<Long, List<SendMessageResult>> result = null;
1010 if (group.isPendingMember(account.getSelfRecipientId())) {
1011 var groupGroupChangePair = groupV2Helper.acceptInvite(group);
1012 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1013 }
1014
1015 if (members != null) {
1016 final var newMembers = new HashSet<>(members);
1017 newMembers.removeAll(group.getMembers());
1018 if (newMembers.size() > 0) {
1019 var groupGroupChangePair = groupV2Helper.addMembers(group, newMembers);
1020 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1021 }
1022 }
1023
1024 if (removeMembers != null) {
1025 var existingRemoveMembers = new HashSet<>(removeMembers);
1026 existingRemoveMembers.retainAll(group.getMembers());
1027 existingRemoveMembers.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
1028 if (existingRemoveMembers.size() > 0) {
1029 var groupGroupChangePair = groupV2Helper.removeMembers(group, existingRemoveMembers);
1030 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1031 }
1032
1033 var pendingRemoveMembers = new HashSet<>(removeMembers);
1034 pendingRemoveMembers.retainAll(group.getPendingMembers());
1035 if (pendingRemoveMembers.size() > 0) {
1036 var groupGroupChangePair = groupV2Helper.revokeInvitedMembers(group, pendingRemoveMembers);
1037 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1038 }
1039 }
1040
1041 if (admins != null) {
1042 final var newAdmins = new HashSet<>(admins);
1043 newAdmins.retainAll(group.getMembers());
1044 newAdmins.removeAll(group.getAdminMembers());
1045 if (newAdmins.size() > 0) {
1046 for (var admin : newAdmins) {
1047 var groupGroupChangePair = groupV2Helper.setMemberAdmin(group, admin, true);
1048 result = sendUpdateGroupV2Message(group,
1049 groupGroupChangePair.first(),
1050 groupGroupChangePair.second());
1051 }
1052 }
1053 }
1054
1055 if (removeAdmins != null) {
1056 final var existingRemoveAdmins = new HashSet<>(removeAdmins);
1057 existingRemoveAdmins.retainAll(group.getAdminMembers());
1058 if (existingRemoveAdmins.size() > 0) {
1059 for (var admin : existingRemoveAdmins) {
1060 var groupGroupChangePair = groupV2Helper.setMemberAdmin(group, admin, false);
1061 result = sendUpdateGroupV2Message(group,
1062 groupGroupChangePair.first(),
1063 groupGroupChangePair.second());
1064 }
1065 }
1066 }
1067
1068 if (resetGroupLink) {
1069 var groupGroupChangePair = groupV2Helper.resetGroupLinkPassword(group);
1070 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1071 }
1072
1073 if (groupLinkState != null) {
1074 var groupGroupChangePair = groupV2Helper.setGroupLinkState(group, groupLinkState);
1075 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1076 }
1077
1078 if (addMemberPermission != null) {
1079 var groupGroupChangePair = groupV2Helper.setAddMemberPermission(group, addMemberPermission);
1080 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1081 }
1082
1083 if (editDetailsPermission != null) {
1084 var groupGroupChangePair = groupV2Helper.setEditDetailsPermission(group, editDetailsPermission);
1085 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1086 }
1087
1088 if (expirationTimer != null) {
1089 var groupGroupChangePair = groupV2Helper.setMessageExpirationTimer(group, expirationTimer);
1090 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1091 }
1092
1093 if (name != null || description != null || avatarFile != null) {
1094 var groupGroupChangePair = groupV2Helper.updateGroup(group, name, description, avatarFile);
1095 if (avatarFile != null) {
1096 avatarStore.storeGroupAvatar(group.getGroupId(),
1097 outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
1098 }
1099 result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
1100 }
1101
1102 return result;
1103 }
1104
1105 public Pair<GroupId, List<SendMessageResult>> joinGroup(
1106 GroupInviteLinkUrl inviteLinkUrl
1107 ) throws IOException, GroupLinkNotActiveException {
1108 final var groupJoinInfo = groupV2Helper.getDecryptedGroupJoinInfo(inviteLinkUrl.getGroupMasterKey(),
1109 inviteLinkUrl.getPassword());
1110 final var groupChange = groupV2Helper.joinGroup(inviteLinkUrl.getGroupMasterKey(),
1111 inviteLinkUrl.getPassword(),
1112 groupJoinInfo);
1113 final var group = getOrMigrateGroup(inviteLinkUrl.getGroupMasterKey(),
1114 groupJoinInfo.getRevision() + 1,
1115 groupChange.toByteArray());
1116
1117 if (group.getGroup() == null) {
1118 // Only requested member, can't send update to group members
1119 return new Pair<>(group.getGroupId(), List.of());
1120 }
1121
1122 final var result = sendUpdateGroupV2Message(group, group.getGroup(), groupChange);
1123
1124 return new Pair<>(group.getGroupId(), result.second());
1125 }
1126
1127 private Pair<Long, List<SendMessageResult>> sendUpdateGroupV2Message(
1128 GroupInfoV2 group, DecryptedGroup newDecryptedGroup, GroupChange groupChange
1129 ) throws IOException {
1130 final var selfRecipientId = account.getSelfRecipientId();
1131 final var members = group.getMembersIncludingPendingWithout(selfRecipientId);
1132 group.setGroup(newDecryptedGroup, this::resolveRecipient);
1133 members.addAll(group.getMembersIncludingPendingWithout(selfRecipientId));
1134
1135 final var messageBuilder = getGroupUpdateMessageBuilder(group, groupChange.toByteArray());
1136 account.getGroupStore().updateGroup(group);
1137 return sendMessage(messageBuilder, members);
1138 }
1139
1140 private static int currentTimeDays() {
1141 return (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
1142 }
1143
1144 private GroupsV2AuthorizationString getGroupAuthForToday(
1145 final GroupSecretParams groupSecretParams
1146 ) throws IOException {
1147 final var today = currentTimeDays();
1148 // Returns credentials for the next 7 days
1149 final var credentials = groupsV2Api.getCredentials(today);
1150 // TODO cache credentials until they expire
1151 var authCredentialResponse = credentials.get(today);
1152 try {
1153 return groupsV2Api.getGroupsV2AuthorizationString(account.getUuid(),
1154 today,
1155 groupSecretParams,
1156 authCredentialResponse);
1157 } catch (VerificationFailedException e) {
1158 throw new IOException(e);
1159 }
1160 }
1161
1162 Pair<Long, List<SendMessageResult>> sendGroupInfoMessage(
1163 GroupIdV1 groupId, SignalServiceAddress recipient
1164 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
1165 GroupInfoV1 g;
1166 var group = getGroupForSending(groupId);
1167 if (!(group instanceof GroupInfoV1)) {
1168 throw new RuntimeException("Received an invalid group request for a v2 group!");
1169 }
1170 g = (GroupInfoV1) group;
1171
1172 final var recipientId = resolveRecipient(recipient);
1173 if (!g.isMember(recipientId)) {
1174 throw new NotAGroupMemberException(groupId, g.name);
1175 }
1176
1177 var messageBuilder = getGroupUpdateMessageBuilder(g);
1178
1179 // Send group message only to the recipient who requested it
1180 return sendMessage(messageBuilder, Set.of(recipientId));
1181 }
1182
1183 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV1 g) throws AttachmentInvalidException {
1184 var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
1185 .withId(g.getGroupId().serialize())
1186 .withName(g.name)
1187 .withMembers(g.getMembers()
1188 .stream()
1189 .map(this::resolveSignalServiceAddress)
1190 .collect(Collectors.toList()));
1191
1192 try {
1193 final var attachment = createGroupAvatarAttachment(g.getGroupId());
1194 if (attachment.isPresent()) {
1195 group.withAvatar(attachment.get());
1196 }
1197 } catch (IOException e) {
1198 throw new AttachmentInvalidException(g.getGroupId().toBase64(), e);
1199 }
1200
1201 return SignalServiceDataMessage.newBuilder()
1202 .asGroupMessage(group.build())
1203 .withExpiration(g.getMessageExpirationTime());
1204 }
1205
1206 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV2 g, byte[] signedGroupChange) {
1207 var group = SignalServiceGroupV2.newBuilder(g.getMasterKey())
1208 .withRevision(g.getGroup().getRevision())
1209 .withSignedGroupChange(signedGroupChange);
1210 return SignalServiceDataMessage.newBuilder()
1211 .asGroupMessage(group.build())
1212 .withExpiration(g.getMessageExpirationTime());
1213 }
1214
1215 Pair<Long, List<SendMessageResult>> sendGroupInfoRequest(
1216 GroupIdV1 groupId, SignalServiceAddress recipient
1217 ) throws IOException {
1218 var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO).withId(groupId.serialize());
1219
1220 var messageBuilder = SignalServiceDataMessage.newBuilder().asGroupMessage(group.build());
1221
1222 // Send group info request message to the recipient who sent us a message with this groupId
1223 return sendMessage(messageBuilder, Set.of(resolveRecipient(recipient)));
1224 }
1225
1226 void sendReceipt(
1227 SignalServiceAddress remoteAddress, long messageId
1228 ) throws IOException, UntrustedIdentityException {
1229 var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.DELIVERY,
1230 List.of(messageId),
1231 System.currentTimeMillis());
1232
1233 createMessageSender().sendReceipt(remoteAddress,
1234 unidentifiedAccessHelper.getAccessFor(resolveRecipient(remoteAddress)),
1235 receiptMessage);
1236 }
1237
1238 public Pair<Long, List<SendMessageResult>> sendMessage(
1239 String messageText, List<String> attachments, List<String> recipients
1240 ) throws IOException, AttachmentInvalidException, InvalidNumberException {
1241 final var messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
1242 if (attachments != null) {
1243 var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(attachments);
1244
1245 // Upload attachments here, so we only upload once even for multiple recipients
1246 var messageSender = createMessageSender();
1247 var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
1248 for (var attachment : attachmentStreams) {
1249 if (attachment.isStream()) {
1250 attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
1251 } else if (attachment.isPointer()) {
1252 attachmentPointers.add(attachment.asPointer());
1253 }
1254 }
1255
1256 messageBuilder.withAttachments(attachmentPointers);
1257 }
1258 return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
1259 }
1260
1261 public Pair<Long, SendMessageResult> sendSelfMessage(
1262 String messageText, List<String> attachments
1263 ) throws IOException, AttachmentInvalidException {
1264 final var messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
1265 if (attachments != null) {
1266 messageBuilder.withAttachments(AttachmentUtils.getSignalServiceAttachments(attachments));
1267 }
1268 return sendSelfMessage(messageBuilder);
1269 }
1270
1271 public Pair<Long, List<SendMessageResult>> sendRemoteDeleteMessage(
1272 long targetSentTimestamp, List<String> recipients
1273 ) throws IOException, InvalidNumberException {
1274 var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp);
1275 final var messageBuilder = SignalServiceDataMessage.newBuilder().withRemoteDelete(delete);
1276 return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
1277 }
1278
1279 public Pair<Long, List<SendMessageResult>> sendGroupRemoteDeleteMessage(
1280 long targetSentTimestamp, GroupId groupId
1281 ) throws IOException, NotAGroupMemberException, GroupNotFoundException {
1282 var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp);
1283 final var messageBuilder = SignalServiceDataMessage.newBuilder().withRemoteDelete(delete);
1284 return sendGroupMessage(messageBuilder, groupId);
1285 }
1286
1287 public Pair<Long, List<SendMessageResult>> sendMessageReaction(
1288 String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List<String> recipients
1289 ) throws IOException, InvalidNumberException {
1290 var targetAuthorRecipientId = canonicalizeAndResolveRecipient(targetAuthor);
1291 var reaction = new SignalServiceDataMessage.Reaction(emoji,
1292 remove,
1293 resolveSignalServiceAddress(targetAuthorRecipientId),
1294 targetSentTimestamp);
1295 final var messageBuilder = SignalServiceDataMessage.newBuilder().withReaction(reaction);
1296 return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
1297 }
1298
1299 public Pair<Long, List<SendMessageResult>> sendEndSessionMessage(List<String> recipients) throws IOException, InvalidNumberException {
1300 var messageBuilder = SignalServiceDataMessage.newBuilder().asEndSessionMessage();
1301
1302 final var signalServiceAddresses = getSignalServiceAddresses(recipients);
1303 try {
1304 return sendMessage(messageBuilder, signalServiceAddresses);
1305 } catch (Exception e) {
1306 for (var address : signalServiceAddresses) {
1307 handleEndSession(address);
1308 }
1309 throw e;
1310 }
1311 }
1312
1313 void renewSession(RecipientId recipientId) throws IOException {
1314 account.getSessionStore().archiveSessions(recipientId);
1315 if (!recipientId.equals(getSelfRecipientId())) {
1316 sendNullMessage(recipientId);
1317 }
1318 }
1319
1320 public String getContactName(String number) throws InvalidNumberException {
1321 var contact = account.getContactStore().getContact(canonicalizeAndResolveRecipient(number));
1322 return contact == null || contact.getName() == null ? "" : contact.getName();
1323 }
1324
1325 public void setContactName(String number, String name) throws InvalidNumberException, NotMasterDeviceException {
1326 if (!account.isMasterDevice()) {
1327 throw new NotMasterDeviceException();
1328 }
1329 final var recipientId = canonicalizeAndResolveRecipient(number);
1330 var contact = account.getContactStore().getContact(recipientId);
1331 final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
1332 account.getContactStore().storeContact(recipientId, builder.withName(name).build());
1333 }
1334
1335 public void setContactBlocked(
1336 String number, boolean blocked
1337 ) throws InvalidNumberException, NotMasterDeviceException {
1338 if (!account.isMasterDevice()) {
1339 throw new NotMasterDeviceException();
1340 }
1341 setContactBlocked(canonicalizeAndResolveRecipient(number), blocked);
1342 }
1343
1344 private void setContactBlocked(RecipientId recipientId, boolean blocked) {
1345 var contact = account.getContactStore().getContact(recipientId);
1346 final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
1347 account.getContactStore().storeContact(recipientId, builder.withBlocked(blocked).build());
1348 }
1349
1350 public void setGroupBlocked(final GroupId groupId, final boolean blocked) throws GroupNotFoundException {
1351 var group = getGroup(groupId);
1352 if (group == null) {
1353 throw new GroupNotFoundException(groupId);
1354 }
1355
1356 group.setBlocked(blocked);
1357 account.getGroupStore().updateGroup(group);
1358 }
1359
1360 private void setExpirationTimer(RecipientId recipientId, int messageExpirationTimer) {
1361 var contact = account.getContactStore().getContact(recipientId);
1362 if (contact != null && contact.getMessageExpirationTime() == messageExpirationTimer) {
1363 return;
1364 }
1365 final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
1366 account.getContactStore()
1367 .storeContact(recipientId, builder.withMessageExpirationTime(messageExpirationTimer).build());
1368 }
1369
1370 private void sendExpirationTimerUpdate(RecipientId recipientId) throws IOException {
1371 final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate();
1372 sendMessage(messageBuilder, Set.of(recipientId));
1373 }
1374
1375 /**
1376 * Change the expiration timer for a contact
1377 */
1378 public void setExpirationTimer(
1379 String number, int messageExpirationTimer
1380 ) throws IOException, InvalidNumberException {
1381 var recipientId = canonicalizeAndResolveRecipient(number);
1382 setExpirationTimer(recipientId, messageExpirationTimer);
1383 sendExpirationTimerUpdate(recipientId);
1384 }
1385
1386 /**
1387 * Change the expiration timer for a group
1388 */
1389 private void setExpirationTimer(
1390 GroupInfoV1 groupInfoV1, int messageExpirationTimer
1391 ) throws NotAGroupMemberException, GroupNotFoundException, IOException {
1392 groupInfoV1.messageExpirationTime = messageExpirationTimer;
1393 account.getGroupStore().updateGroup(groupInfoV1);
1394 sendExpirationTimerUpdate(groupInfoV1.getGroupId());
1395 }
1396
1397 private void sendExpirationTimerUpdate(GroupIdV1 groupId) throws IOException, NotAGroupMemberException, GroupNotFoundException {
1398 final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate();
1399 sendGroupMessage(messageBuilder, groupId);
1400 }
1401
1402 /**
1403 * Upload the sticker pack from path.
1404 *
1405 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1406 * @return if successful, returns the URL to install the sticker pack in the signal app
1407 */
1408 public String uploadStickerPack(File path) throws IOException, StickerPackInvalidException {
1409 var manifest = StickerUtils.getSignalServiceStickerManifestUpload(path);
1410
1411 var messageSender = createMessageSender();
1412
1413 var packKey = KeyUtils.createStickerUploadKey();
1414 var packId = messageSender.uploadStickerManifest(manifest, packKey);
1415
1416 var sticker = new Sticker(StickerPackId.deserialize(Hex.fromStringCondensed(packId)), packKey);
1417 account.getStickerStore().updateSticker(sticker);
1418
1419 try {
1420 return new URI("https",
1421 "signal.art",
1422 "/addstickers/",
1423 "pack_id=" + URLEncoder.encode(packId, StandardCharsets.UTF_8) + "&pack_key=" + URLEncoder.encode(
1424 Hex.toStringCondensed(packKey),
1425 StandardCharsets.UTF_8)).toString();
1426 } catch (URISyntaxException e) {
1427 throw new AssertionError(e);
1428 }
1429 }
1430
1431 public void requestAllSyncData() throws IOException {
1432 requestSyncGroups();
1433 requestSyncContacts();
1434 requestSyncBlocked();
1435 requestSyncConfiguration();
1436 requestSyncKeys();
1437 }
1438
1439 private void requestSyncGroups() throws IOException {
1440 var r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1441 .setType(SignalServiceProtos.SyncMessage.Request.Type.GROUPS)
1442 .build();
1443 var message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1444 try {
1445 sendSyncMessage(message);
1446 } catch (UntrustedIdentityException e) {
1447 throw new AssertionError(e);
1448 }
1449 }
1450
1451 private void requestSyncContacts() throws IOException {
1452 var r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1453 .setType(SignalServiceProtos.SyncMessage.Request.Type.CONTACTS)
1454 .build();
1455 var message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1456 try {
1457 sendSyncMessage(message);
1458 } catch (UntrustedIdentityException e) {
1459 throw new AssertionError(e);
1460 }
1461 }
1462
1463 private void requestSyncBlocked() throws IOException {
1464 var r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1465 .setType(SignalServiceProtos.SyncMessage.Request.Type.BLOCKED)
1466 .build();
1467 var message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1468 try {
1469 sendSyncMessage(message);
1470 } catch (UntrustedIdentityException e) {
1471 throw new AssertionError(e);
1472 }
1473 }
1474
1475 private void requestSyncConfiguration() throws IOException {
1476 var r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1477 .setType(SignalServiceProtos.SyncMessage.Request.Type.CONFIGURATION)
1478 .build();
1479 var message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1480 try {
1481 sendSyncMessage(message);
1482 } catch (UntrustedIdentityException e) {
1483 throw new AssertionError(e);
1484 }
1485 }
1486
1487 private void requestSyncKeys() throws IOException {
1488 var r = SignalServiceProtos.SyncMessage.Request.newBuilder()
1489 .setType(SignalServiceProtos.SyncMessage.Request.Type.KEYS)
1490 .build();
1491 var message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
1492 try {
1493 sendSyncMessage(message);
1494 } catch (UntrustedIdentityException e) {
1495 throw new AssertionError(e);
1496 }
1497 }
1498
1499 private byte[] getSenderCertificate() {
1500 byte[] certificate;
1501 try {
1502 if (account.isPhoneNumberShared()) {
1503 certificate = accountManager.getSenderCertificate();
1504 } else {
1505 certificate = accountManager.getSenderCertificateForPhoneNumberPrivacy();
1506 }
1507 } catch (IOException e) {
1508 logger.warn("Failed to get sender certificate, ignoring: {}", e.getMessage());
1509 return null;
1510 }
1511 // TODO cache for a day
1512 return certificate;
1513 }
1514
1515 private void sendSyncMessage(SignalServiceSyncMessage message) throws IOException, UntrustedIdentityException {
1516 var messageSender = createMessageSender();
1517 messageSender.sendMessage(message, unidentifiedAccessHelper.getAccessForSync());
1518 }
1519
1520 private Set<RecipientId> getSignalServiceAddresses(Collection<String> numbers) throws InvalidNumberException {
1521 final var signalServiceAddresses = new HashSet<SignalServiceAddress>(numbers.size());
1522 final var addressesMissingUuid = new HashSet<SignalServiceAddress>();
1523
1524 for (var number : numbers) {
1525 final var resolvedAddress = resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number));
1526 if (resolvedAddress.getUuid().isPresent()) {
1527 signalServiceAddresses.add(resolvedAddress);
1528 } else {
1529 addressesMissingUuid.add(resolvedAddress);
1530 }
1531 }
1532
1533 final var numbersMissingUuid = addressesMissingUuid.stream()
1534 .map(a -> a.getNumber().get())
1535 .collect(Collectors.toSet());
1536 Map<String, UUID> registeredUsers;
1537 try {
1538 registeredUsers = getRegisteredUsers(numbersMissingUuid);
1539 } catch (IOException e) {
1540 logger.warn("Failed to resolve uuids from server, ignoring: {}", e.getMessage());
1541 registeredUsers = Map.of();
1542 }
1543
1544 for (var address : addressesMissingUuid) {
1545 final var number = address.getNumber().get();
1546 if (registeredUsers.containsKey(number)) {
1547 final var newAddress = resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1548 registeredUsers.get(number),
1549 number)));
1550 signalServiceAddresses.add(newAddress);
1551 } else {
1552 signalServiceAddresses.add(address);
1553 }
1554 }
1555
1556 return signalServiceAddresses.stream().map(this::resolveRecipient).collect(Collectors.toSet());
1557 }
1558
1559 private RecipientId refreshRegisteredUser(RecipientId recipientId) throws IOException {
1560 final var address = resolveSignalServiceAddress(recipientId);
1561 if (!address.getNumber().isPresent()) {
1562 return recipientId;
1563 }
1564 final var number = address.getNumber().get();
1565 final var uuidMap = getRegisteredUsers(Set.of(number));
1566 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap.getOrDefault(number, null), number));
1567 }
1568
1569 private Map<String, UUID> getRegisteredUsers(final Set<String> numbers) throws IOException {
1570 try {
1571 return accountManager.getRegisteredUsers(ServiceConfig.getIasKeyStore(),
1572 numbers,
1573 serviceEnvironmentConfig.getCdsMrenclave());
1574 } catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) {
1575 throw new IOException(e);
1576 }
1577 }
1578
1579 private Pair<Long, List<SendMessageResult>> sendMessage(
1580 SignalServiceDataMessage.Builder messageBuilder, Set<RecipientId> recipientIds
1581 ) throws IOException {
1582 final var timestamp = System.currentTimeMillis();
1583 messageBuilder.withTimestamp(timestamp);
1584 getOrCreateMessagePipe();
1585 getOrCreateUnidentifiedMessagePipe();
1586 SignalServiceDataMessage message = null;
1587 try {
1588 message = messageBuilder.build();
1589 if (message.getGroupContext().isPresent()) {
1590 try {
1591 var messageSender = createMessageSender();
1592 final var isRecipientUpdate = false;
1593 final var recipientIdList = new ArrayList<>(recipientIds);
1594 final var addresses = recipientIdList.stream()
1595 .map(this::resolveSignalServiceAddress)
1596 .collect(Collectors.toList());
1597 var result = messageSender.sendMessage(addresses,
1598 unidentifiedAccessHelper.getAccessFor(recipientIdList),
1599 isRecipientUpdate,
1600 message);
1601
1602 for (var r : result) {
1603 if (r.getIdentityFailure() != null) {
1604 final var recipientId = resolveRecipient(r.getAddress());
1605 final var newIdentity = account.getIdentityKeyStore()
1606 .saveIdentity(recipientId, r.getIdentityFailure().getIdentityKey(), new Date());
1607 if (newIdentity) {
1608 account.getSessionStore().archiveSessions(recipientId);
1609 }
1610 }
1611 }
1612
1613 return new Pair<>(timestamp, result);
1614 } catch (UntrustedIdentityException e) {
1615 return new Pair<>(timestamp, List.of());
1616 }
1617 } else {
1618 // Send to all individually, so sync messages are sent correctly
1619 messageBuilder.withProfileKey(account.getProfileKey().serialize());
1620 var results = new ArrayList<SendMessageResult>(recipientIds.size());
1621 for (var recipientId : recipientIds) {
1622 final var contact = account.getContactStore().getContact(recipientId);
1623 final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0;
1624 messageBuilder.withExpiration(expirationTime);
1625 message = messageBuilder.build();
1626 results.add(sendMessage(recipientId, message));
1627 }
1628 return new Pair<>(timestamp, results);
1629 }
1630 } finally {
1631 if (message != null && message.isEndSession()) {
1632 for (var recipient : recipientIds) {
1633 handleEndSession(recipient);
1634 }
1635 }
1636 }
1637 }
1638
1639 private Pair<Long, SendMessageResult> sendSelfMessage(
1640 SignalServiceDataMessage.Builder messageBuilder
1641 ) throws IOException {
1642 final var timestamp = System.currentTimeMillis();
1643 messageBuilder.withTimestamp(timestamp);
1644 getOrCreateMessagePipe();
1645 getOrCreateUnidentifiedMessagePipe();
1646 final var recipientId = account.getSelfRecipientId();
1647
1648 final var contact = account.getContactStore().getContact(recipientId);
1649 final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0;
1650 messageBuilder.withExpiration(expirationTime);
1651
1652 var message = messageBuilder.build();
1653 final var result = sendSelfMessage(message);
1654 return new Pair<>(timestamp, result);
1655 }
1656
1657 private SendMessageResult sendSelfMessage(SignalServiceDataMessage message) throws IOException {
1658 var messageSender = createMessageSender();
1659
1660 var recipientId = account.getSelfRecipientId();
1661
1662 final var unidentifiedAccess = unidentifiedAccessHelper.getAccessFor(recipientId);
1663 var recipient = resolveSignalServiceAddress(recipientId);
1664 var transcript = new SentTranscriptMessage(Optional.of(recipient),
1665 message.getTimestamp(),
1666 message,
1667 message.getExpiresInSeconds(),
1668 Map.of(recipient, unidentifiedAccess.isPresent()),
1669 false);
1670 var syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
1671
1672 try {
1673 var startTime = System.currentTimeMillis();
1674 messageSender.sendMessage(syncMessage, unidentifiedAccess);
1675 return SendMessageResult.success(recipient,
1676 unidentifiedAccess.isPresent(),
1677 false,
1678 System.currentTimeMillis() - startTime);
1679 } catch (UntrustedIdentityException e) {
1680 return SendMessageResult.identityFailure(recipient, e.getIdentityKey());
1681 }
1682 }
1683
1684 private SendMessageResult sendMessage(
1685 RecipientId recipientId, SignalServiceDataMessage message
1686 ) throws IOException {
1687 var messageSender = createMessageSender();
1688
1689 final var address = resolveSignalServiceAddress(recipientId);
1690 try {
1691 try {
1692 return messageSender.sendMessage(address, unidentifiedAccessHelper.getAccessFor(recipientId), message);
1693 } catch (UnregisteredUserException e) {
1694 final var newRecipientId = refreshRegisteredUser(recipientId);
1695 return messageSender.sendMessage(resolveSignalServiceAddress(newRecipientId),
1696 unidentifiedAccessHelper.getAccessFor(newRecipientId),
1697 message);
1698 }
1699 } catch (UntrustedIdentityException e) {
1700 return SendMessageResult.identityFailure(address, e.getIdentityKey());
1701 }
1702 }
1703
1704 private SendMessageResult sendNullMessage(RecipientId recipientId) throws IOException {
1705 var messageSender = createMessageSender();
1706
1707 final var address = resolveSignalServiceAddress(recipientId);
1708 try {
1709 try {
1710 return messageSender.sendNullMessage(address, unidentifiedAccessHelper.getAccessFor(recipientId));
1711 } catch (UnregisteredUserException e) {
1712 final var newRecipientId = refreshRegisteredUser(recipientId);
1713 final var newAddress = resolveSignalServiceAddress(newRecipientId);
1714 return messageSender.sendNullMessage(newAddress, unidentifiedAccessHelper.getAccessFor(newRecipientId));
1715 }
1716 } catch (UntrustedIdentityException e) {
1717 return SendMessageResult.identityFailure(address, e.getIdentityKey());
1718 }
1719 }
1720
1721 private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, ProtocolUntrustedIdentityException {
1722 var cipher = new SignalServiceCipher(account.getSelfAddress(),
1723 account.getSignalProtocolStore(),
1724 sessionLock,
1725 certificateValidator);
1726 return cipher.decrypt(envelope);
1727 }
1728
1729 private void handleEndSession(RecipientId recipientId) {
1730 account.getSessionStore().deleteAllSessions(recipientId);
1731 }
1732
1733 private List<HandleAction> handleSignalServiceDataMessage(
1734 SignalServiceDataMessage message,
1735 boolean isSync,
1736 SignalServiceAddress source,
1737 SignalServiceAddress destination,
1738 boolean ignoreAttachments
1739 ) {
1740 var actions = new ArrayList<HandleAction>();
1741 if (message.getGroupContext().isPresent()) {
1742 if (message.getGroupContext().get().getGroupV1().isPresent()) {
1743 var groupInfo = message.getGroupContext().get().getGroupV1().get();
1744 var groupId = GroupId.v1(groupInfo.getGroupId());
1745 var group = getGroup(groupId);
1746 if (group == null || group instanceof GroupInfoV1) {
1747 var groupV1 = (GroupInfoV1) group;
1748 switch (groupInfo.getType()) {
1749 case UPDATE: {
1750 if (groupV1 == null) {
1751 groupV1 = new GroupInfoV1(groupId);
1752 }
1753
1754 if (groupInfo.getAvatar().isPresent()) {
1755 var avatar = groupInfo.getAvatar().get();
1756 downloadGroupAvatar(avatar, groupV1.getGroupId());
1757 }
1758
1759 if (groupInfo.getName().isPresent()) {
1760 groupV1.name = groupInfo.getName().get();
1761 }
1762
1763 if (groupInfo.getMembers().isPresent()) {
1764 groupV1.addMembers(groupInfo.getMembers()
1765 .get()
1766 .stream()
1767 .map(this::resolveRecipient)
1768 .collect(Collectors.toSet()));
1769 }
1770
1771 account.getGroupStore().updateGroup(groupV1);
1772 break;
1773 }
1774 case DELIVER:
1775 if (groupV1 == null && !isSync) {
1776 actions.add(new SendGroupInfoRequestAction(source, groupId));
1777 }
1778 break;
1779 case QUIT: {
1780 if (groupV1 != null) {
1781 groupV1.removeMember(resolveRecipient(source));
1782 account.getGroupStore().updateGroup(groupV1);
1783 }
1784 break;
1785 }
1786 case REQUEST_INFO:
1787 if (groupV1 != null && !isSync) {
1788 actions.add(new SendGroupInfoAction(source, groupV1.getGroupId()));
1789 }
1790 break;
1791 }
1792 } else {
1793 // Received a group v1 message for a v2 group
1794 }
1795 }
1796 if (message.getGroupContext().get().getGroupV2().isPresent()) {
1797 final var groupContext = message.getGroupContext().get().getGroupV2().get();
1798 final var groupMasterKey = groupContext.getMasterKey();
1799
1800 getOrMigrateGroup(groupMasterKey,
1801 groupContext.getRevision(),
1802 groupContext.hasSignedGroupChange() ? groupContext.getSignedGroupChange() : null);
1803 }
1804 }
1805
1806 final var conversationPartnerAddress = isSync ? destination : source;
1807 if (conversationPartnerAddress != null && message.isEndSession()) {
1808 handleEndSession(resolveRecipient(conversationPartnerAddress));
1809 }
1810 if (message.isExpirationUpdate() || message.getBody().isPresent()) {
1811 if (message.getGroupContext().isPresent()) {
1812 if (message.getGroupContext().get().getGroupV1().isPresent()) {
1813 var groupInfo = message.getGroupContext().get().getGroupV1().get();
1814 var group = account.getGroupStore().getOrCreateGroupV1(GroupId.v1(groupInfo.getGroupId()));
1815 if (group != null) {
1816 if (group.messageExpirationTime != message.getExpiresInSeconds()) {
1817 group.messageExpirationTime = message.getExpiresInSeconds();
1818 account.getGroupStore().updateGroup(group);
1819 }
1820 }
1821 } else if (message.getGroupContext().get().getGroupV2().isPresent()) {
1822 // disappearing message timer already stored in the DecryptedGroup
1823 }
1824 } else if (conversationPartnerAddress != null) {
1825 setExpirationTimer(resolveRecipient(conversationPartnerAddress), message.getExpiresInSeconds());
1826 }
1827 }
1828 if (!ignoreAttachments) {
1829 if (message.getAttachments().isPresent()) {
1830 for (var attachment : message.getAttachments().get()) {
1831 downloadAttachment(attachment);
1832 }
1833 }
1834 if (message.getSharedContacts().isPresent()) {
1835 for (var contact : message.getSharedContacts().get()) {
1836 if (contact.getAvatar().isPresent()) {
1837 downloadAttachment(contact.getAvatar().get().getAttachment());
1838 }
1839 }
1840 }
1841 }
1842 if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
1843 final ProfileKey profileKey;
1844 try {
1845 profileKey = new ProfileKey(message.getProfileKey().get());
1846 } catch (InvalidInputException e) {
1847 throw new AssertionError(e);
1848 }
1849 if (source.matches(account.getSelfAddress())) {
1850 this.account.setProfileKey(profileKey);
1851 }
1852 this.account.getProfileStore().storeProfileKey(resolveRecipient(source), profileKey);
1853 }
1854 if (message.getPreviews().isPresent()) {
1855 final var previews = message.getPreviews().get();
1856 for (var preview : previews) {
1857 if (preview.getImage().isPresent()) {
1858 downloadAttachment(preview.getImage().get());
1859 }
1860 }
1861 }
1862 if (message.getQuote().isPresent()) {
1863 final var quote = message.getQuote().get();
1864
1865 for (var quotedAttachment : quote.getAttachments()) {
1866 final var thumbnail = quotedAttachment.getThumbnail();
1867 if (thumbnail != null) {
1868 downloadAttachment(thumbnail);
1869 }
1870 }
1871 }
1872 if (message.getSticker().isPresent()) {
1873 final var messageSticker = message.getSticker().get();
1874 final var stickerPackId = StickerPackId.deserialize(messageSticker.getPackId());
1875 var sticker = account.getStickerStore().getSticker(stickerPackId);
1876 if (sticker == null) {
1877 sticker = new Sticker(stickerPackId, messageSticker.getPackKey());
1878 account.getStickerStore().updateSticker(sticker);
1879 }
1880 }
1881 return actions;
1882 }
1883
1884 private GroupInfoV2 getOrMigrateGroup(
1885 final GroupMasterKey groupMasterKey, final int revision, final byte[] signedGroupChange
1886 ) {
1887 final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
1888
1889 var groupId = GroupUtils.getGroupIdV2(groupSecretParams);
1890 var groupInfo = getGroup(groupId);
1891 final GroupInfoV2 groupInfoV2;
1892 if (groupInfo instanceof GroupInfoV1) {
1893 // Received a v2 group message for a v1 group, we need to locally migrate the group
1894 account.getGroupStore().deleteGroupV1(((GroupInfoV1) groupInfo).getGroupId());
1895 groupInfoV2 = new GroupInfoV2(groupId, groupMasterKey);
1896 logger.info("Locally migrated group {} to group v2, id: {}",
1897 groupInfo.getGroupId().toBase64(),
1898 groupInfoV2.getGroupId().toBase64());
1899 } else if (groupInfo instanceof GroupInfoV2) {
1900 groupInfoV2 = (GroupInfoV2) groupInfo;
1901 } else {
1902 groupInfoV2 = new GroupInfoV2(groupId, groupMasterKey);
1903 }
1904
1905 if (groupInfoV2.getGroup() == null || groupInfoV2.getGroup().getRevision() < revision) {
1906 DecryptedGroup group = null;
1907 if (signedGroupChange != null
1908 && groupInfoV2.getGroup() != null
1909 && groupInfoV2.getGroup().getRevision() + 1 == revision) {
1910 group = groupV2Helper.getUpdatedDecryptedGroup(groupInfoV2.getGroup(),
1911 signedGroupChange,
1912 groupMasterKey);
1913 }
1914 if (group == null) {
1915 group = groupV2Helper.getDecryptedGroup(groupSecretParams);
1916 }
1917 if (group != null) {
1918 storeProfileKeysFromMembers(group);
1919 final var avatar = group.getAvatar();
1920 if (avatar != null && !avatar.isEmpty()) {
1921 downloadGroupAvatar(groupId, groupSecretParams, avatar);
1922 }
1923 }
1924 groupInfoV2.setGroup(group, this::resolveRecipient);
1925 account.getGroupStore().updateGroup(groupInfoV2);
1926 }
1927
1928 return groupInfoV2;
1929 }
1930
1931 private void storeProfileKeysFromMembers(final DecryptedGroup group) {
1932 for (var member : group.getMembersList()) {
1933 final var uuid = UuidUtil.parseOrThrow(member.getUuid().toByteArray());
1934 final var recipientId = account.getRecipientStore().resolveRecipient(uuid);
1935 try {
1936 account.getProfileStore()
1937 .storeProfileKey(recipientId, new ProfileKey(member.getProfileKey().toByteArray()));
1938 } catch (InvalidInputException ignored) {
1939 }
1940 }
1941 }
1942
1943 private void retryFailedReceivedMessages(ReceiveMessageHandler handler, boolean ignoreAttachments) {
1944 Set<HandleAction> queuedActions = new HashSet<>();
1945 for (var cachedMessage : account.getMessageCache().getCachedMessages()) {
1946 var actions = retryFailedReceivedMessage(handler, ignoreAttachments, cachedMessage);
1947 if (actions != null) {
1948 queuedActions.addAll(actions);
1949 }
1950 }
1951 for (var action : queuedActions) {
1952 try {
1953 action.execute(this);
1954 } catch (Throwable e) {
1955 logger.warn("Message action failed.", e);
1956 }
1957 }
1958 }
1959
1960 private List<HandleAction> retryFailedReceivedMessage(
1961 final ReceiveMessageHandler handler, final boolean ignoreAttachments, final CachedMessage cachedMessage
1962 ) {
1963 var envelope = cachedMessage.loadEnvelope();
1964 if (envelope == null) {
1965 return null;
1966 }
1967 SignalServiceContent content = null;
1968 List<HandleAction> actions = null;
1969 if (!envelope.isReceipt()) {
1970 try {
1971 content = decryptMessage(envelope);
1972 } catch (ProtocolUntrustedIdentityException e) {
1973 if (!envelope.hasSource()) {
1974 final var identifier = e.getSender();
1975 final var recipientId = resolveRecipient(identifier);
1976 try {
1977 account.getMessageCache().replaceSender(cachedMessage, recipientId);
1978 } catch (IOException ioException) {
1979 logger.warn("Failed to move cached message to recipient folder: {}", ioException.getMessage());
1980 }
1981 }
1982 return null;
1983 } catch (Exception er) {
1984 // All other errors are not recoverable, so delete the cached message
1985 cachedMessage.delete();
1986 return null;
1987 }
1988 actions = handleMessage(envelope, content, ignoreAttachments);
1989 }
1990 handler.handleMessage(envelope, content, null);
1991 cachedMessage.delete();
1992 return actions;
1993 }
1994
1995 public void receiveMessages(
1996 long timeout,
1997 TimeUnit unit,
1998 boolean returnOnTimeout,
1999 boolean ignoreAttachments,
2000 ReceiveMessageHandler handler
2001 ) throws IOException {
2002 retryFailedReceivedMessages(handler, ignoreAttachments);
2003
2004 Set<HandleAction> queuedActions = null;
2005
2006 final var messagePipe = getOrCreateMessagePipe();
2007
2008 var hasCaughtUpWithOldMessages = false;
2009
2010 while (true) {
2011 SignalServiceEnvelope envelope;
2012 SignalServiceContent content = null;
2013 Exception exception = null;
2014 final CachedMessage[] cachedMessage = {null};
2015 account.setLastReceiveTimestamp(System.currentTimeMillis());
2016 try {
2017 var result = messagePipe.readOrEmpty(timeout, unit, envelope1 -> {
2018 final var recipientId = envelope1.hasSource()
2019 ? resolveRecipient(envelope1.getSourceIdentifier())
2020 : null;
2021 // store message on disk, before acknowledging receipt to the server
2022 cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId);
2023 });
2024 if (result.isPresent()) {
2025 envelope = result.get();
2026 } else {
2027 // Received indicator that server queue is empty
2028 hasCaughtUpWithOldMessages = true;
2029
2030 if (queuedActions != null) {
2031 for (var action : queuedActions) {
2032 try {
2033 action.execute(this);
2034 } catch (Throwable e) {
2035 logger.warn("Message action failed.", e);
2036 }
2037 }
2038 queuedActions.clear();
2039 queuedActions = null;
2040 }
2041
2042 // Continue to wait another timeout for new messages
2043 continue;
2044 }
2045 } catch (TimeoutException e) {
2046 if (returnOnTimeout) return;
2047 continue;
2048 }
2049
2050 if (envelope.hasSource()) {
2051 // Store uuid if we don't have it already
2052 // address/uuid in envelope is sent by server
2053 resolveRecipientTrusted(envelope.getSourceAddress());
2054 }
2055 final var notAGroupMember = isNotAGroupMember(envelope, content);
2056 if (!envelope.isReceipt()) {
2057 try {
2058 content = decryptMessage(envelope);
2059 } catch (Exception e) {
2060 exception = e;
2061 }
2062 if (!envelope.hasSource() && content != null) {
2063 // Store uuid if we don't have it already
2064 // address/uuid is validated by unidentified sender certificate
2065 resolveRecipientTrusted(content.getSender());
2066 }
2067 var actions = handleMessage(envelope, content, ignoreAttachments);
2068 if (exception instanceof ProtocolInvalidMessageException) {
2069 final var sender = resolveRecipient(((ProtocolInvalidMessageException) exception).getSender());
2070 logger.debug("Received invalid message, queuing renew session action.");
2071 actions.add(new RenewSessionAction(sender));
2072 }
2073 if (hasCaughtUpWithOldMessages) {
2074 for (var action : actions) {
2075 try {
2076 action.execute(this);
2077 } catch (Throwable e) {
2078 logger.warn("Message action failed.", e);
2079 }
2080 }
2081 } else {
2082 if (queuedActions == null) {
2083 queuedActions = new HashSet<>();
2084 }
2085 queuedActions.addAll(actions);
2086 }
2087 }
2088 if (isMessageBlocked(envelope, content)) {
2089 logger.info("Ignoring a message from blocked user/group: {}", envelope.getTimestamp());
2090 } else if (notAGroupMember) {
2091 logger.info("Ignoring a message from a non group member: {}", envelope.getTimestamp());
2092 } else {
2093 handler.handleMessage(envelope, content, exception);
2094 }
2095 if (cachedMessage[0] != null) {
2096 if (exception instanceof ProtocolUntrustedIdentityException) {
2097 final var identifier = ((ProtocolUntrustedIdentityException) exception).getSender();
2098 final var recipientId = resolveRecipient(identifier);
2099 queuedActions.add(new RetrieveProfileAction(recipientId));
2100 if (!envelope.hasSource()) {
2101 try {
2102 cachedMessage[0] = account.getMessageCache().replaceSender(cachedMessage[0], recipientId);
2103 } catch (IOException ioException) {
2104 logger.warn("Failed to move cached message to recipient folder: {}",
2105 ioException.getMessage());
2106 }
2107 }
2108 } else {
2109 cachedMessage[0].delete();
2110 }
2111 }
2112 }
2113 }
2114
2115 private boolean isMessageBlocked(
2116 SignalServiceEnvelope envelope, SignalServiceContent content
2117 ) {
2118 SignalServiceAddress source;
2119 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
2120 source = envelope.getSourceAddress();
2121 } else if (content != null) {
2122 source = content.getSender();
2123 } else {
2124 return false;
2125 }
2126 final var recipientId = resolveRecipient(source);
2127 if (isContactBlocked(recipientId)) {
2128 return true;
2129 }
2130
2131 if (content != null && content.getDataMessage().isPresent()) {
2132 var message = content.getDataMessage().get();
2133 if (message.getGroupContext().isPresent()) {
2134 var groupId = GroupUtils.getGroupId(message.getGroupContext().get());
2135 var group = getGroup(groupId);
2136 if (group != null && group.isBlocked()) {
2137 return true;
2138 }
2139 }
2140 }
2141 return false;
2142 }
2143
2144 public boolean isContactBlocked(final String identifier) throws InvalidNumberException {
2145 final var recipientId = canonicalizeAndResolveRecipient(identifier);
2146 return isContactBlocked(recipientId);
2147 }
2148
2149 private boolean isContactBlocked(final RecipientId recipientId) {
2150 var sourceContact = account.getContactStore().getContact(recipientId);
2151 return sourceContact != null && sourceContact.isBlocked();
2152 }
2153
2154 private boolean isNotAGroupMember(
2155 SignalServiceEnvelope envelope, SignalServiceContent content
2156 ) {
2157 SignalServiceAddress source;
2158 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
2159 source = envelope.getSourceAddress();
2160 } else if (content != null) {
2161 source = content.getSender();
2162 } else {
2163 return false;
2164 }
2165
2166 if (content != null && content.getDataMessage().isPresent()) {
2167 var message = content.getDataMessage().get();
2168 if (message.getGroupContext().isPresent()) {
2169 if (message.getGroupContext().get().getGroupV1().isPresent()) {
2170 var groupInfo = message.getGroupContext().get().getGroupV1().get();
2171 if (groupInfo.getType() == SignalServiceGroup.Type.QUIT) {
2172 return false;
2173 }
2174 }
2175 var groupId = GroupUtils.getGroupId(message.getGroupContext().get());
2176 var group = getGroup(groupId);
2177 if (group != null && !group.isMember(resolveRecipient(source))) {
2178 return true;
2179 }
2180 }
2181 }
2182 return false;
2183 }
2184
2185 private List<HandleAction> handleMessage(
2186 SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments
2187 ) {
2188 var actions = new ArrayList<HandleAction>();
2189 if (content != null) {
2190 final SignalServiceAddress sender;
2191 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
2192 sender = envelope.getSourceAddress();
2193 } else {
2194 sender = content.getSender();
2195 }
2196
2197 if (content.getDataMessage().isPresent()) {
2198 var message = content.getDataMessage().get();
2199
2200 if (content.isNeedsReceipt()) {
2201 actions.add(new SendReceiptAction(sender, message.getTimestamp()));
2202 }
2203
2204 actions.addAll(handleSignalServiceDataMessage(message,
2205 false,
2206 sender,
2207 account.getSelfAddress(),
2208 ignoreAttachments));
2209 }
2210 if (content.getSyncMessage().isPresent()) {
2211 account.setMultiDevice(true);
2212 var syncMessage = content.getSyncMessage().get();
2213 if (syncMessage.getSent().isPresent()) {
2214 var message = syncMessage.getSent().get();
2215 final var destination = message.getDestination().orNull();
2216 actions.addAll(handleSignalServiceDataMessage(message.getMessage(),
2217 true,
2218 sender,
2219 destination,
2220 ignoreAttachments));
2221 }
2222 if (syncMessage.getRequest().isPresent() && account.isMasterDevice()) {
2223 var rm = syncMessage.getRequest().get();
2224 if (rm.isContactsRequest()) {
2225 actions.add(SendSyncContactsAction.create());
2226 }
2227 if (rm.isGroupsRequest()) {
2228 actions.add(SendSyncGroupsAction.create());
2229 }
2230 if (rm.isBlockedListRequest()) {
2231 actions.add(SendSyncBlockedListAction.create());
2232 }
2233 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2234 }
2235 if (syncMessage.getGroups().isPresent()) {
2236 File tmpFile = null;
2237 try {
2238 tmpFile = IOUtils.createTempFile();
2239 final var groupsMessage = syncMessage.getGroups().get();
2240 try (var attachmentAsStream = retrieveAttachmentAsStream(groupsMessage.asPointer(), tmpFile)) {
2241 var s = new DeviceGroupsInputStream(attachmentAsStream);
2242 DeviceGroup g;
2243 while (true) {
2244 try {
2245 g = s.read();
2246 } catch (IOException e) {
2247 logger.warn("Sync groups contained invalid group, ignoring: {}", e.getMessage());
2248 continue;
2249 }
2250 if (g == null) {
2251 break;
2252 }
2253 var syncGroup = account.getGroupStore().getOrCreateGroupV1(GroupId.v1(g.getId()));
2254 if (syncGroup != null) {
2255 if (g.getName().isPresent()) {
2256 syncGroup.name = g.getName().get();
2257 }
2258 syncGroup.addMembers(g.getMembers()
2259 .stream()
2260 .map(this::resolveRecipient)
2261 .collect(Collectors.toSet()));
2262 if (!g.isActive()) {
2263 syncGroup.removeMember(account.getSelfRecipientId());
2264 } else {
2265 // Add ourself to the member set as it's marked as active
2266 syncGroup.addMembers(List.of(account.getSelfRecipientId()));
2267 }
2268 syncGroup.blocked = g.isBlocked();
2269 if (g.getColor().isPresent()) {
2270 syncGroup.color = g.getColor().get();
2271 }
2272
2273 if (g.getAvatar().isPresent()) {
2274 downloadGroupAvatar(g.getAvatar().get(), syncGroup.getGroupId());
2275 }
2276 syncGroup.archived = g.isArchived();
2277 account.getGroupStore().updateGroup(syncGroup);
2278 }
2279 }
2280 }
2281 } catch (Exception e) {
2282 logger.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2283 tmpFile,
2284 e.getMessage());
2285 } finally {
2286 if (tmpFile != null) {
2287 try {
2288 Files.delete(tmpFile.toPath());
2289 } catch (IOException e) {
2290 logger.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2291 tmpFile,
2292 e.getMessage());
2293 }
2294 }
2295 }
2296 }
2297 if (syncMessage.getBlockedList().isPresent()) {
2298 final var blockedListMessage = syncMessage.getBlockedList().get();
2299 for (var address : blockedListMessage.getAddresses()) {
2300 setContactBlocked(resolveRecipient(address), true);
2301 }
2302 for (var groupId : blockedListMessage.getGroupIds()
2303 .stream()
2304 .map(GroupId::unknownVersion)
2305 .collect(Collectors.toSet())) {
2306 try {
2307 setGroupBlocked(groupId, true);
2308 } catch (GroupNotFoundException e) {
2309 logger.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2310 groupId.toBase64());
2311 }
2312 }
2313 }
2314 if (syncMessage.getContacts().isPresent()) {
2315 File tmpFile = null;
2316 try {
2317 tmpFile = IOUtils.createTempFile();
2318 final var contactsMessage = syncMessage.getContacts().get();
2319 try (var attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream()
2320 .asPointer(), tmpFile)) {
2321 var s = new DeviceContactsInputStream(attachmentAsStream);
2322 DeviceContact c;
2323 while (true) {
2324 try {
2325 c = s.read();
2326 } catch (IOException e) {
2327 logger.warn("Sync contacts contained invalid contact, ignoring: {}",
2328 e.getMessage());
2329 continue;
2330 }
2331 if (c == null) {
2332 break;
2333 }
2334 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
2335 account.setProfileKey(c.getProfileKey().get());
2336 }
2337 final var recipientId = resolveRecipientTrusted(c.getAddress());
2338 var contact = account.getContactStore().getContact(recipientId);
2339 final var builder = contact == null
2340 ? Contact.newBuilder()
2341 : Contact.newBuilder(contact);
2342 if (c.getName().isPresent()) {
2343 builder.withName(c.getName().get());
2344 }
2345 if (c.getColor().isPresent()) {
2346 builder.withColor(c.getColor().get());
2347 }
2348 if (c.getProfileKey().isPresent()) {
2349 account.getProfileStore().storeProfileKey(recipientId, c.getProfileKey().get());
2350 }
2351 if (c.getVerified().isPresent()) {
2352 final var verifiedMessage = c.getVerified().get();
2353 account.getIdentityKeyStore()
2354 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage.getDestination()),
2355 verifiedMessage.getIdentityKey(),
2356 TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
2357 }
2358 if (c.getExpirationTimer().isPresent()) {
2359 builder.withMessageExpirationTime(c.getExpirationTimer().get());
2360 }
2361 builder.withBlocked(c.isBlocked());
2362 builder.withArchived(c.isArchived());
2363 account.getContactStore().storeContact(recipientId, builder.build());
2364
2365 if (c.getAvatar().isPresent()) {
2366 downloadContactAvatar(c.getAvatar().get(), c.getAddress());
2367 }
2368 }
2369 }
2370 } catch (Exception e) {
2371 logger.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2372 tmpFile,
2373 e.getMessage());
2374 } finally {
2375 if (tmpFile != null) {
2376 try {
2377 Files.delete(tmpFile.toPath());
2378 } catch (IOException e) {
2379 logger.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2380 tmpFile,
2381 e.getMessage());
2382 }
2383 }
2384 }
2385 }
2386 if (syncMessage.getVerified().isPresent()) {
2387 final var verifiedMessage = syncMessage.getVerified().get();
2388 account.getIdentityKeyStore()
2389 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage.getDestination()),
2390 verifiedMessage.getIdentityKey(),
2391 TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
2392 }
2393 if (syncMessage.getStickerPackOperations().isPresent()) {
2394 final var stickerPackOperationMessages = syncMessage.getStickerPackOperations().get();
2395 for (var m : stickerPackOperationMessages) {
2396 if (!m.getPackId().isPresent()) {
2397 continue;
2398 }
2399 final var stickerPackId = StickerPackId.deserialize(m.getPackId().get());
2400 var sticker = account.getStickerStore().getSticker(stickerPackId);
2401 if (sticker == null) {
2402 if (!m.getPackKey().isPresent()) {
2403 continue;
2404 }
2405 sticker = new Sticker(stickerPackId, m.getPackKey().get());
2406 }
2407 sticker.setInstalled(!m.getType().isPresent()
2408 || m.getType().get() == StickerPackOperationMessage.Type.INSTALL);
2409 account.getStickerStore().updateSticker(sticker);
2410 }
2411 }
2412 if (syncMessage.getFetchType().isPresent()) {
2413 switch (syncMessage.getFetchType().get()) {
2414 case LOCAL_PROFILE:
2415 getRecipientProfile(account.getSelfRecipientId(), true);
2416 case STORAGE_MANIFEST:
2417 // TODO
2418 }
2419 }
2420 if (syncMessage.getKeys().isPresent()) {
2421 final var keysMessage = syncMessage.getKeys().get();
2422 if (keysMessage.getStorageService().isPresent()) {
2423 final var storageKey = keysMessage.getStorageService().get();
2424 account.setStorageKey(storageKey);
2425 }
2426 }
2427 if (syncMessage.getConfiguration().isPresent()) {
2428 // TODO
2429 }
2430 }
2431 }
2432 return actions;
2433 }
2434
2435 private void downloadContactAvatar(SignalServiceAttachment avatar, SignalServiceAddress address) {
2436 try {
2437 avatarStore.storeContactAvatar(address, outputStream -> retrieveAttachment(avatar, outputStream));
2438 } catch (IOException e) {
2439 logger.warn("Failed to download avatar for contact {}, ignoring: {}", address, e.getMessage());
2440 }
2441 }
2442
2443 private void downloadGroupAvatar(SignalServiceAttachment avatar, GroupId groupId) {
2444 try {
2445 avatarStore.storeGroupAvatar(groupId, outputStream -> retrieveAttachment(avatar, outputStream));
2446 } catch (IOException e) {
2447 logger.warn("Failed to download avatar for group {}, ignoring: {}", groupId.toBase64(), e.getMessage());
2448 }
2449 }
2450
2451 private void downloadGroupAvatar(GroupId groupId, GroupSecretParams groupSecretParams, String cdnKey) {
2452 try {
2453 avatarStore.storeGroupAvatar(groupId,
2454 outputStream -> retrieveGroupV2Avatar(groupSecretParams, cdnKey, outputStream));
2455 } catch (IOException e) {
2456 logger.warn("Failed to download avatar for group {}, ignoring: {}", groupId.toBase64(), e.getMessage());
2457 }
2458 }
2459
2460 private void downloadProfileAvatar(
2461 SignalServiceAddress address, String avatarPath, ProfileKey profileKey
2462 ) {
2463 try {
2464 avatarStore.storeProfileAvatar(address,
2465 outputStream -> retrieveProfileAvatar(avatarPath, profileKey, outputStream));
2466 } catch (Throwable e) {
2467 logger.warn("Failed to download profile avatar, ignoring: {}", e.getMessage());
2468 }
2469 }
2470
2471 public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) {
2472 return attachmentStore.getAttachmentFile(attachmentId);
2473 }
2474
2475 private void downloadAttachment(final SignalServiceAttachment attachment) {
2476 if (!attachment.isPointer()) {
2477 logger.warn("Invalid state, can't store an attachment stream.");
2478 }
2479
2480 var pointer = attachment.asPointer();
2481 if (pointer.getPreview().isPresent()) {
2482 final var preview = pointer.getPreview().get();
2483 try {
2484 attachmentStore.storeAttachmentPreview(pointer.getRemoteId(),
2485 outputStream -> outputStream.write(preview, 0, preview.length));
2486 } catch (IOException e) {
2487 logger.warn("Failed to download attachment preview, ignoring: {}", e.getMessage());
2488 }
2489 }
2490
2491 try {
2492 attachmentStore.storeAttachment(pointer.getRemoteId(),
2493 outputStream -> retrieveAttachmentPointer(pointer, outputStream));
2494 } catch (IOException e) {
2495 logger.warn("Failed to download attachment ({}), ignoring: {}", pointer.getRemoteId(), e.getMessage());
2496 }
2497 }
2498
2499 private void retrieveGroupV2Avatar(
2500 GroupSecretParams groupSecretParams, String cdnKey, OutputStream outputStream
2501 ) throws IOException {
2502 var groupOperations = groupsV2Operations.forGroup(groupSecretParams);
2503
2504 var tmpFile = IOUtils.createTempFile();
2505 try (InputStream input = messageReceiver.retrieveGroupsV2ProfileAvatar(cdnKey,
2506 tmpFile,
2507 ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
2508 var encryptedData = IOUtils.readFully(input);
2509
2510 var decryptedData = groupOperations.decryptAvatar(encryptedData);
2511 outputStream.write(decryptedData);
2512 } finally {
2513 try {
2514 Files.delete(tmpFile.toPath());
2515 } catch (IOException e) {
2516 logger.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2517 tmpFile,
2518 e.getMessage());
2519 }
2520 }
2521 }
2522
2523 private void retrieveProfileAvatar(
2524 String avatarPath, ProfileKey profileKey, OutputStream outputStream
2525 ) throws IOException {
2526 var tmpFile = IOUtils.createTempFile();
2527 try (var input = messageReceiver.retrieveProfileAvatar(avatarPath,
2528 tmpFile,
2529 profileKey,
2530 ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
2531 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2532 IOUtils.copyStream(input, outputStream, (int) ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
2533 } finally {
2534 try {
2535 Files.delete(tmpFile.toPath());
2536 } catch (IOException e) {
2537 logger.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2538 tmpFile,
2539 e.getMessage());
2540 }
2541 }
2542 }
2543
2544 private void retrieveAttachment(
2545 final SignalServiceAttachment attachment, final OutputStream outputStream
2546 ) throws IOException {
2547 if (attachment.isPointer()) {
2548 var pointer = attachment.asPointer();
2549 retrieveAttachmentPointer(pointer, outputStream);
2550 } else {
2551 var stream = attachment.asStream();
2552 IOUtils.copyStream(stream.getInputStream(), outputStream);
2553 }
2554 }
2555
2556 private void retrieveAttachmentPointer(
2557 SignalServiceAttachmentPointer pointer, OutputStream outputStream
2558 ) throws IOException {
2559 var tmpFile = IOUtils.createTempFile();
2560 try (var input = retrieveAttachmentAsStream(pointer, tmpFile)) {
2561 IOUtils.copyStream(input, outputStream);
2562 } catch (MissingConfigurationException | InvalidMessageException e) {
2563 throw new IOException(e);
2564 } finally {
2565 try {
2566 Files.delete(tmpFile.toPath());
2567 } catch (IOException e) {
2568 logger.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2569 tmpFile,
2570 e.getMessage());
2571 }
2572 }
2573 }
2574
2575 private InputStream retrieveAttachmentAsStream(
2576 SignalServiceAttachmentPointer pointer, File tmpFile
2577 ) throws IOException, InvalidMessageException, MissingConfigurationException {
2578 return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
2579 }
2580
2581 void sendGroups() throws IOException, UntrustedIdentityException {
2582 var groupsFile = IOUtils.createTempFile();
2583
2584 try {
2585 try (OutputStream fos = new FileOutputStream(groupsFile)) {
2586 var out = new DeviceGroupsOutputStream(fos);
2587 for (var record : getGroups()) {
2588 if (record instanceof GroupInfoV1) {
2589 var groupInfo = (GroupInfoV1) record;
2590 out.write(new DeviceGroup(groupInfo.getGroupId().serialize(),
2591 Optional.fromNullable(groupInfo.name),
2592 groupInfo.getMembers()
2593 .stream()
2594 .map(this::resolveSignalServiceAddress)
2595 .collect(Collectors.toList()),
2596 createGroupAvatarAttachment(groupInfo.getGroupId()),
2597 groupInfo.isMember(account.getSelfRecipientId()),
2598 Optional.of(groupInfo.messageExpirationTime),
2599 Optional.fromNullable(groupInfo.color),
2600 groupInfo.blocked,
2601 Optional.absent(),
2602 groupInfo.archived));
2603 }
2604 }
2605 }
2606
2607 if (groupsFile.exists() && groupsFile.length() > 0) {
2608 try (var groupsFileStream = new FileInputStream(groupsFile)) {
2609 var attachmentStream = SignalServiceAttachment.newStreamBuilder()
2610 .withStream(groupsFileStream)
2611 .withContentType("application/octet-stream")
2612 .withLength(groupsFile.length())
2613 .build();
2614
2615 sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
2616 }
2617 }
2618 } finally {
2619 try {
2620 Files.delete(groupsFile.toPath());
2621 } catch (IOException e) {
2622 logger.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile, e.getMessage());
2623 }
2624 }
2625 }
2626
2627 public void sendContacts() throws IOException, UntrustedIdentityException {
2628 var contactsFile = IOUtils.createTempFile();
2629
2630 try {
2631 try (OutputStream fos = new FileOutputStream(contactsFile)) {
2632 var out = new DeviceContactsOutputStream(fos);
2633 for (var contactPair : account.getContactStore().getContacts()) {
2634 final var recipientId = contactPair.first();
2635 final var contact = contactPair.second();
2636 final var address = resolveSignalServiceAddress(recipientId);
2637
2638 var currentIdentity = account.getIdentityKeyStore().getIdentity(recipientId);
2639 VerifiedMessage verifiedMessage = null;
2640 if (currentIdentity != null) {
2641 verifiedMessage = new VerifiedMessage(address,
2642 currentIdentity.getIdentityKey(),
2643 currentIdentity.getTrustLevel().toVerifiedState(),
2644 currentIdentity.getDateAdded().getTime());
2645 }
2646
2647 var profileKey = account.getProfileStore().getProfileKey(recipientId);
2648 out.write(new DeviceContact(address,
2649 Optional.fromNullable(contact.getName()),
2650 createContactAvatarAttachment(address),
2651 Optional.fromNullable(contact.getColor()),
2652 Optional.fromNullable(verifiedMessage),
2653 Optional.fromNullable(profileKey),
2654 contact.isBlocked(),
2655 Optional.of(contact.getMessageExpirationTime()),
2656 Optional.absent(),
2657 contact.isArchived()));
2658 }
2659
2660 if (account.getProfileKey() != null) {
2661 // Send our own profile key as well
2662 out.write(new DeviceContact(account.getSelfAddress(),
2663 Optional.absent(),
2664 Optional.absent(),
2665 Optional.absent(),
2666 Optional.absent(),
2667 Optional.of(account.getProfileKey()),
2668 false,
2669 Optional.absent(),
2670 Optional.absent(),
2671 false));
2672 }
2673 }
2674
2675 if (contactsFile.exists() && contactsFile.length() > 0) {
2676 try (var contactsFileStream = new FileInputStream(contactsFile)) {
2677 var attachmentStream = SignalServiceAttachment.newStreamBuilder()
2678 .withStream(contactsFileStream)
2679 .withContentType("application/octet-stream")
2680 .withLength(contactsFile.length())
2681 .build();
2682
2683 sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true)));
2684 }
2685 }
2686 } finally {
2687 try {
2688 Files.delete(contactsFile.toPath());
2689 } catch (IOException e) {
2690 logger.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile, e.getMessage());
2691 }
2692 }
2693 }
2694
2695 void sendBlockedList() throws IOException, UntrustedIdentityException {
2696 var addresses = new ArrayList<SignalServiceAddress>();
2697 for (var record : account.getContactStore().getContacts()) {
2698 if (record.second().isBlocked()) {
2699 addresses.add(resolveSignalServiceAddress(record.first()));
2700 }
2701 }
2702 var groupIds = new ArrayList<byte[]>();
2703 for (var record : getGroups()) {
2704 if (record.isBlocked()) {
2705 groupIds.add(record.getGroupId().serialize());
2706 }
2707 }
2708 sendSyncMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(addresses, groupIds)));
2709 }
2710
2711 private void sendVerifiedMessage(
2712 SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel
2713 ) throws IOException, UntrustedIdentityException {
2714 var verifiedMessage = new VerifiedMessage(destination,
2715 identityKey,
2716 trustLevel.toVerifiedState(),
2717 System.currentTimeMillis());
2718 sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
2719 }
2720
2721 public List<Pair<RecipientId, Contact>> getContacts() {
2722 return account.getContactStore().getContacts();
2723 }
2724
2725 public String getContactOrProfileName(String number) throws InvalidNumberException {
2726 final var recipientId = canonicalizeAndResolveRecipient(number);
2727 final var recipient = account.getRecipientStore().getRecipient(recipientId);
2728 if (recipient == null) {
2729 return null;
2730 }
2731
2732 if (recipient.getContact() != null && !Util.isEmpty(recipient.getContact().getName())) {
2733 return recipient.getContact().getName();
2734 }
2735
2736 if (recipient.getProfile() != null && recipient.getProfile() != null) {
2737 return recipient.getProfile().getDisplayName();
2738 }
2739
2740 return null;
2741 }
2742
2743 public GroupInfo getGroup(GroupId groupId) {
2744 return getGroup(groupId, false);
2745 }
2746
2747 public GroupInfo getGroup(GroupId groupId, boolean forceUpdate) {
2748 final var group = account.getGroupStore().getGroup(groupId);
2749 if (group instanceof GroupInfoV2 && (forceUpdate || ((GroupInfoV2) group).getGroup() == null)) {
2750 final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(((GroupInfoV2) group).getMasterKey());
2751 ((GroupInfoV2) group).setGroup(groupV2Helper.getDecryptedGroup(groupSecretParams), this::resolveRecipient);
2752 account.getGroupStore().updateGroup(group);
2753 }
2754 return group;
2755 }
2756
2757 public List<IdentityInfo> getIdentities() {
2758 return account.getIdentityKeyStore().getIdentities();
2759 }
2760
2761 public List<IdentityInfo> getIdentities(String number) throws InvalidNumberException {
2762 final var identity = account.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number));
2763 return identity == null ? List.of() : List.of(identity);
2764 }
2765
2766 /**
2767 * Trust this the identity with this fingerprint
2768 *
2769 * @param name username of the identity
2770 * @param fingerprint Fingerprint
2771 */
2772 public boolean trustIdentityVerified(String name, byte[] fingerprint) throws InvalidNumberException {
2773 var recipientId = canonicalizeAndResolveRecipient(name);
2774 return trustIdentity(recipientId,
2775 identityKey -> Arrays.equals(identityKey.serialize(), fingerprint),
2776 TrustLevel.TRUSTED_VERIFIED);
2777 }
2778
2779 /**
2780 * Trust this the identity with this safety number
2781 *
2782 * @param name username of the identity
2783 * @param safetyNumber Safety number
2784 */
2785 public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) throws InvalidNumberException {
2786 var recipientId = canonicalizeAndResolveRecipient(name);
2787 var address = account.getRecipientStore().resolveServiceAddress(recipientId);
2788 return trustIdentity(recipientId,
2789 identityKey -> safetyNumber.equals(computeSafetyNumber(address, identityKey)),
2790 TrustLevel.TRUSTED_VERIFIED);
2791 }
2792
2793 /**
2794 * Trust all keys of this identity without verification
2795 *
2796 * @param name username of the identity
2797 */
2798 public boolean trustIdentityAllKeys(String name) throws InvalidNumberException {
2799 var recipientId = canonicalizeAndResolveRecipient(name);
2800 return trustIdentity(recipientId, identityKey -> true, TrustLevel.TRUSTED_UNVERIFIED);
2801 }
2802
2803 private boolean trustIdentity(
2804 RecipientId recipientId, Function<IdentityKey, Boolean> verifier, TrustLevel trustLevel
2805 ) {
2806 var identity = account.getIdentityKeyStore().getIdentity(recipientId);
2807 if (identity == null) {
2808 return false;
2809 }
2810
2811 if (!verifier.apply(identity.getIdentityKey())) {
2812 return false;
2813 }
2814
2815 account.getIdentityKeyStore().setIdentityTrustLevel(recipientId, identity.getIdentityKey(), trustLevel);
2816 try {
2817 var address = account.getRecipientStore().resolveServiceAddress(recipientId);
2818 sendVerifiedMessage(address, identity.getIdentityKey(), trustLevel);
2819 } catch (IOException | UntrustedIdentityException e) {
2820 logger.warn("Failed to send verification sync message: {}", e.getMessage());
2821 }
2822
2823 return true;
2824 }
2825
2826 public String computeSafetyNumber(
2827 SignalServiceAddress theirAddress, IdentityKey theirIdentityKey
2828 ) {
2829 return Utils.computeSafetyNumber(ServiceConfig.capabilities.isUuid(),
2830 account.getSelfAddress(),
2831 getIdentityKeyPair().getPublicKey(),
2832 theirAddress,
2833 theirIdentityKey);
2834 }
2835
2836 @Deprecated
2837 public SignalServiceAddress resolveSignalServiceAddress(String identifier) {
2838 var address = Utils.getSignalServiceAddressFromIdentifier(identifier);
2839
2840 return resolveSignalServiceAddress(address);
2841 }
2842
2843 @Deprecated
2844 public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
2845 if (address.matches(account.getSelfAddress())) {
2846 return account.getSelfAddress();
2847 }
2848
2849 return account.getRecipientStore().resolveServiceAddress(address);
2850 }
2851
2852 public SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) {
2853 return account.getRecipientStore().resolveServiceAddress(recipientId);
2854 }
2855
2856 public RecipientId canonicalizeAndResolveRecipient(String identifier) throws InvalidNumberException {
2857 var canonicalizedNumber = UuidUtil.isUuid(identifier)
2858 ? identifier
2859 : PhoneNumberFormatter.formatNumber(identifier, account.getUsername());
2860
2861 return resolveRecipient(canonicalizedNumber);
2862 }
2863
2864 private RecipientId resolveRecipient(final String identifier) {
2865 var address = Utils.getSignalServiceAddressFromIdentifier(identifier);
2866
2867 return resolveRecipient(address);
2868 }
2869
2870 public RecipientId resolveRecipient(SignalServiceAddress address) {
2871 return account.getRecipientStore().resolveRecipient(address);
2872 }
2873
2874 private RecipientId resolveRecipientTrusted(SignalServiceAddress address) {
2875 return account.getRecipientStore().resolveRecipientTrusted(address);
2876 }
2877
2878 @Override
2879 public void close() throws IOException {
2880 close(true);
2881 }
2882
2883 void close(boolean closeAccount) throws IOException {
2884 executor.shutdown();
2885
2886 if (messagePipe != null) {
2887 messagePipe.shutdown();
2888 messagePipe = null;
2889 }
2890
2891 if (unidentifiedMessagePipe != null) {
2892 unidentifiedMessagePipe.shutdown();
2893 unidentifiedMessagePipe = null;
2894 }
2895
2896 if (closeAccount && account != null) {
2897 account.close();
2898 }
2899 account = null;
2900 }
2901
2902 public interface ReceiveMessageHandler {
2903
2904 void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, Throwable e);
2905 }
2906 }