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