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