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