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