2 Copyright (C) 2015-2021 AsamK and contributors
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.
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.
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/>.
17 package org
.asamk
.signal
.manager
;
19 import org
.asamk
.signal
.manager
.api
.Device
;
20 import org
.asamk
.signal
.manager
.api
.TypingAction
;
21 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
22 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
23 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
24 import org
.asamk
.signal
.manager
.groups
.GroupId
;
25 import org
.asamk
.signal
.manager
.groups
.GroupIdV1
;
26 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
27 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
28 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
29 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
30 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
31 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
32 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
33 import org
.asamk
.signal
.manager
.helper
.GroupV2Helper
;
34 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
35 import org
.asamk
.signal
.manager
.helper
.ProfileHelper
;
36 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
37 import org
.asamk
.signal
.manager
.jobs
.Context
;
38 import org
.asamk
.signal
.manager
.jobs
.Job
;
39 import org
.asamk
.signal
.manager
.jobs
.RetrieveStickerPackJob
;
40 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
41 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
42 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
43 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
44 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
45 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
46 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
47 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
48 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
49 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
50 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
51 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
52 import org
.asamk
.signal
.manager
.util
.IOUtils
;
53 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
54 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
55 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
56 import org
.asamk
.signal
.manager
.util
.Utils
;
57 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
58 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
59 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
60 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
61 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
62 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
63 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
64 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
65 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
66 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
67 import org
.signal
.libsignal
.metadata
.SelfSendException
;
68 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
69 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
70 import org
.signal
.zkgroup
.InvalidInputException
;
71 import org
.signal
.zkgroup
.VerificationFailedException
;
72 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
73 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
74 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
75 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
76 import org
.slf4j
.Logger
;
77 import org
.slf4j
.LoggerFactory
;
78 import org
.whispersystems
.libsignal
.IdentityKey
;
79 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
80 import org
.whispersystems
.libsignal
.InvalidKeyException
;
81 import org
.whispersystems
.libsignal
.InvalidMessageException
;
82 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
83 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
84 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
85 import org
.whispersystems
.libsignal
.util
.Pair
;
86 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
87 import org
.whispersystems
.signalservice
.api
.InvalidMessageStructureException
;
88 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
89 import org
.whispersystems
.signalservice
.api
.crypto
.ContentHint
;
90 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
91 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
92 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
93 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
94 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
95 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
96 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
97 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
98 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
99 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
100 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
101 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
102 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupV2
;
103 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
104 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTypingMessage
;
105 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
106 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
107 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
108 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
109 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
110 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
111 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
112 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
113 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
114 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
117 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
118 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
119 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
120 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
121 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.ConflictException
;
122 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
123 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
124 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
125 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
126 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
127 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
128 import org
.whispersystems
.signalservice
.api
.websocket
.WebSocketUnavailableException
;
129 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
130 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
131 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
132 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
133 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
134 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
135 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
136 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
138 import java
.io
.Closeable
;
140 import java
.io
.FileInputStream
;
141 import java
.io
.FileOutputStream
;
142 import java
.io
.IOException
;
143 import java
.io
.InputStream
;
144 import java
.io
.OutputStream
;
146 import java
.net
.URISyntaxException
;
147 import java
.net
.URLEncoder
;
148 import java
.nio
.charset
.StandardCharsets
;
149 import java
.nio
.file
.Files
;
150 import java
.security
.SignatureException
;
151 import java
.util
.ArrayList
;
152 import java
.util
.Arrays
;
153 import java
.util
.Base64
;
154 import java
.util
.Collection
;
155 import java
.util
.Date
;
156 import java
.util
.HashSet
;
157 import java
.util
.List
;
158 import java
.util
.Map
;
159 import java
.util
.Set
;
160 import java
.util
.UUID
;
161 import java
.util
.concurrent
.ExecutorService
;
162 import java
.util
.concurrent
.Executors
;
163 import java
.util
.concurrent
.TimeUnit
;
164 import java
.util
.concurrent
.TimeoutException
;
165 import java
.util
.concurrent
.locks
.ReentrantLock
;
166 import java
.util
.function
.Function
;
167 import java
.util
.stream
.Collectors
;
169 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
171 public class Manager
implements Closeable
{
173 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
175 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
176 private final SignalDependencies dependencies
;
178 private SignalAccount account
;
180 private final ExecutorService executor
= Executors
.newCachedThreadPool();
182 private final UnidentifiedAccessHelper unidentifiedAccessHelper
;
183 private final ProfileHelper profileHelper
;
184 private final GroupV2Helper groupV2Helper
;
185 private final PinHelper pinHelper
;
186 private final AvatarStore avatarStore
;
187 private final AttachmentStore attachmentStore
;
188 private final StickerPackStore stickerPackStore
;
189 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
190 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
193 public Lock
acquire() {
195 return LEGACY_LOCK
::unlock
;
200 SignalAccount account
,
201 PathConfig pathConfig
,
202 ServiceEnvironmentConfig serviceEnvironmentConfig
,
205 this.account
= account
;
206 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
208 final var credentialsProvider
= new DynamicCredentialsProvider(account
.getUuid(),
209 account
.getUsername(),
210 account
.getPassword(),
211 account
.getDeviceId());
212 this.dependencies
= new SignalDependencies(account
.getSelfAddress(),
213 serviceEnvironmentConfig
,
216 account
.getSignalProtocolStore(),
219 this.pinHelper
= new PinHelper(dependencies
.getKeyBackupService());
221 this.unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
222 account
.getProfileStore()::getProfileKey
,
223 this::getRecipientProfile
,
224 this::getSenderCertificate
);
225 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
226 unidentifiedAccessHelper
::getAccessFor
,
227 dependencies
::getProfileService
,
228 dependencies
::getMessageReceiver
,
229 this::resolveSignalServiceAddress
);
230 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
231 this::getRecipientProfile
,
232 account
::getSelfRecipientId
,
233 dependencies
.getGroupsV2Operations(),
234 dependencies
.getGroupsV2Api(),
235 this::getGroupAuthForToday
,
236 this::resolveSignalServiceAddress
);
237 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
238 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
239 this.stickerPackStore
= new StickerPackStore(pathConfig
.getStickerPacksPath());
242 public String
getUsername() {
243 return account
.getUsername();
246 public SignalServiceAddress
getSelfAddress() {
247 return account
.getSelfAddress();
250 public RecipientId
getSelfRecipientId() {
251 return account
.getSelfRecipientId();
254 private IdentityKeyPair
getIdentityKeyPair() {
255 return account
.getIdentityKeyPair();
258 public int getDeviceId() {
259 return account
.getDeviceId();
262 public static Manager
init(
263 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
264 ) throws IOException
, NotRegisteredException
{
265 var pathConfig
= PathConfig
.createDefault(settingsPath
);
267 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
268 throw new NotRegisteredException();
271 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
273 if (!account
.isRegistered()) {
274 throw new NotRegisteredException();
277 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
279 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
282 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
283 var pathConfig
= PathConfig
.createDefault(settingsPath
);
284 final var dataPath
= pathConfig
.getDataPath();
285 final var files
= dataPath
.listFiles();
291 return Arrays
.stream(files
)
292 .filter(File
::isFile
)
294 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
295 .collect(Collectors
.toList());
298 public void checkAccountState() throws IOException
{
299 if (account
.getLastReceiveTimestamp() == 0) {
300 logger
.warn("The Signal protocol expects that incoming messages are regularly received.");
302 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
303 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
306 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
310 if (dependencies
.getAccountManager().getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
313 if (account
.getUuid() == null) {
314 account
.setUuid(dependencies
.getAccountManager().getOwnUuid());
316 updateAccountAttributes();
320 * This is used for checking a set of phone numbers for registration on Signal
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 get the contacts to check if they're registered
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 var contactDetails
= getRegisteredUsers(numbers
);
330 var registeredUsers
= contactDetails
.keySet();
332 return numbers
.stream().collect(Collectors
.toMap(x
-> x
, registeredUsers
::contains
));
335 public void updateAccountAttributes() throws IOException
{
336 dependencies
.getAccountManager()
337 .setAccountAttributes(account
.getEncryptedDeviceName(),
339 account
.getLocalRegistrationId(),
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(),
347 account
.isDiscoverableByPhoneNumber());
351 * @param givenName if null, the previous givenName will be kept
352 * @param familyName if null, the previous familyName will be kept
353 * @param about if null, the previous about text will be kept
354 * @param aboutEmoji if null, the previous about emoji will be kept
355 * @param avatar if avatar is null the image from the local avatar store is used (if present),
357 public void setProfile(
358 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
359 ) throws IOException
{
360 var profile
= getRecipientProfile(account
.getSelfRecipientId());
361 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
362 if (givenName
!= null) {
363 builder
.withGivenName(givenName
);
365 if (familyName
!= null) {
366 builder
.withFamilyName(familyName
);
369 builder
.withAbout(about
);
371 if (aboutEmoji
!= null) {
372 builder
.withAboutEmoji(aboutEmoji
);
374 var newProfile
= builder
.build();
376 try (final var streamDetails
= avatar
== null
377 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
378 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
379 dependencies
.getAccountManager()
380 .setVersionedProfile(account
.getUuid(),
381 account
.getProfileKey(),
382 newProfile
.getInternalServiceName(),
383 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
384 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
389 if (avatar
!= null) {
390 if (avatar
.isPresent()) {
391 avatarStore
.storeProfileAvatar(getSelfAddress(),
392 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
394 avatarStore
.deleteProfileAvatar(getSelfAddress());
397 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
400 sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
401 } catch (UntrustedIdentityException ignored
) {
405 public void unregister() throws IOException
{
406 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
407 // If this is the master device, other users can't send messages to this number anymore.
408 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
409 dependencies
.getAccountManager().setGcmId(Optional
.absent());
411 account
.setRegistered(false);
414 public void deleteAccount() throws IOException
{
415 dependencies
.getAccountManager().deleteAccount();
417 account
.setRegistered(false);
420 public List
<Device
> getLinkedDevices() throws IOException
{
421 var devices
= dependencies
.getAccountManager().getDevices();
422 account
.setMultiDevice(devices
.size() > 1);
423 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
424 return devices
.stream().map(d
-> {
425 String deviceName
= d
.getName();
426 if (deviceName
!= null) {
428 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
429 } catch (IOException e
) {
430 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
433 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
434 }).collect(Collectors
.toList());
437 public void removeLinkedDevices(int deviceId
) throws IOException
{
438 dependencies
.getAccountManager().removeDevice(deviceId
);
439 var devices
= dependencies
.getAccountManager().getDevices();
440 account
.setMultiDevice(devices
.size() > 1);
443 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
444 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
446 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
449 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
450 var identityKeyPair
= getIdentityKeyPair();
451 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
453 dependencies
.getAccountManager()
454 .addDevice(deviceIdentifier
,
457 Optional
.of(account
.getProfileKey().serialize()),
459 account
.setMultiDevice(true);
462 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
463 if (!account
.isMasterDevice()) {
464 throw new RuntimeException("Only master device can set a PIN");
466 if (pin
.isPresent()) {
467 final var masterKey
= account
.getPinMasterKey() != null
468 ? account
.getPinMasterKey()
469 : KeyUtils
.createMasterKey();
471 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
473 account
.setRegistrationLockPin(pin
.get(), masterKey
);
475 // Remove legacy registration lock
476 dependencies
.getAccountManager().removeRegistrationLockV1();
479 pinHelper
.removeRegistrationLockPin();
481 account
.setRegistrationLockPin(null, null);
485 void refreshPreKeys() throws IOException
{
486 var oneTimePreKeys
= generatePreKeys();
487 final var identityKeyPair
= getIdentityKeyPair();
488 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
490 dependencies
.getAccountManager().setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
493 private List
<PreKeyRecord
> generatePreKeys() {
494 final var offset
= account
.getPreKeyIdOffset();
496 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
497 account
.addPreKeys(records
);
502 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
503 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
505 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
506 account
.addSignedPreKey(record);
511 public Profile
getRecipientProfile(
512 RecipientId recipientId
514 return getRecipientProfile(recipientId
, false);
517 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
519 Profile
getRecipientProfile(
520 RecipientId recipientId
, boolean force
522 var profile
= account
.getProfileStore().getProfile(recipientId
);
524 var now
= System
.currentTimeMillis();
525 // Profiles are cached for 24h before retrieving them again, unless forced
526 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
530 synchronized (pendingProfileRequest
) {
531 if (pendingProfileRequest
.contains(recipientId
)) {
534 pendingProfileRequest
.add(recipientId
);
536 final SignalServiceProfile encryptedProfile
;
538 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
540 synchronized (pendingProfileRequest
) {
541 pendingProfileRequest
.remove(recipientId
);
544 if (encryptedProfile
== null) {
548 profile
= decryptProfileIfKeyKnown(recipientId
, encryptedProfile
);
549 account
.getProfileStore().storeProfile(recipientId
, profile
);
554 private Profile
decryptProfileIfKeyKnown(
555 final RecipientId recipientId
, final SignalServiceProfile encryptedProfile
557 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
558 if (profileKey
== null) {
559 return new Profile(System
.currentTimeMillis(),
564 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
565 ProfileUtils
.getCapabilities(encryptedProfile
));
568 return decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
571 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
573 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
574 } catch (IOException e
) {
575 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
580 private ProfileAndCredential
retrieveProfileAndCredential(
581 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
582 ) throws IOException
{
583 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
584 final var profile
= profileAndCredential
.getProfile();
587 var newIdentity
= account
.getIdentityKeyStore()
588 .saveIdentity(recipientId
,
589 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
593 account
.getSessionStore().archiveSessions(recipientId
);
595 } catch (InvalidKeyException ignored
) {
596 logger
.warn("Got invalid identity key in profile for {}",
597 resolveSignalServiceAddress(recipientId
).getIdentifier());
599 return profileAndCredential
;
602 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
603 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
604 if (profileKeyCredential
!= null) {
605 return profileKeyCredential
;
608 ProfileAndCredential profileAndCredential
;
610 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
611 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
612 } catch (IOException e
) {
613 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
617 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
618 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
620 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
621 if (profileKey
!= null) {
622 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
624 profileAndCredential
.getProfile());
625 account
.getProfileStore().storeProfile(recipientId
, profile
);
628 return profileKeyCredential
;
631 private Profile
decryptProfileAndDownloadAvatar(
632 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
634 if (encryptedProfile
.getAvatar() != null) {
635 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
638 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
641 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
642 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
643 if (streamDetails
== null) {
644 return Optional
.absent();
647 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
650 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
651 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
652 if (streamDetails
== null) {
653 return Optional
.absent();
656 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
659 private GroupInfo
getGroupForSending(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
660 var g
= getGroup(groupId
);
662 throw new GroupNotFoundException(groupId
);
664 if (!g
.isMember(account
.getSelfRecipientId())) {
665 throw new NotAGroupMemberException(groupId
, g
.getTitle());
670 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
671 var g
= getGroup(groupId
);
673 throw new GroupNotFoundException(groupId
);
675 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
676 throw new NotAGroupMemberException(groupId
, g
.getTitle());
681 public List
<GroupInfo
> getGroups() {
682 return account
.getGroupStore().getGroups();
685 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
686 String messageText
, List
<String
> attachments
, GroupId groupId
687 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
688 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
689 if (attachments
!= null) {
690 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
693 return sendGroupMessage(messageBuilder
, groupId
);
696 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
697 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
698 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
699 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
700 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
702 resolveSignalServiceAddress(targetAuthorRecipientId
),
703 targetSentTimestamp
);
704 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
706 return sendGroupMessage(messageBuilder
, groupId
);
709 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
710 SignalServiceDataMessage
.Builder messageBuilder
, GroupId groupId
711 ) throws IOException
, GroupNotFoundException
, NotAGroupMemberException
{
712 final var g
= getGroupForSending(groupId
);
714 GroupUtils
.setGroupContext(messageBuilder
, g
);
715 messageBuilder
.withExpiration(g
.getMessageExpirationTime());
717 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
720 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(
721 GroupId groupId
, Set
<String
> groupAdmins
722 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, InvalidNumberException
, LastGroupAdminException
{
723 var group
= getGroupForUpdating(groupId
);
724 if (group
instanceof GroupInfoV1
) {
725 return quitGroupV1((GroupInfoV1
) group
);
728 final var newAdmins
= getSignalServiceAddresses(groupAdmins
);
730 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
731 } catch (ConflictException e
) {
732 // Detected conflicting update, refreshing group and trying again
733 group
= getGroup(groupId
, true);
734 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
738 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV1(final GroupInfoV1 groupInfoV1
) throws IOException
{
739 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
740 .withId(groupInfoV1
.getGroupId().serialize())
743 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
744 groupInfoV1
.removeMember(account
.getSelfRecipientId());
745 account
.getGroupStore().updateGroup(groupInfoV1
);
746 return sendMessage(messageBuilder
, groupInfoV1
.getMembersWithout(account
.getSelfRecipientId()));
749 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV2(
750 final GroupInfoV2 groupInfoV2
, final Set
<RecipientId
> newAdmins
751 ) throws LastGroupAdminException
, IOException
{
752 final var currentAdmins
= groupInfoV2
.getAdminMembers();
753 newAdmins
.removeAll(currentAdmins
);
754 newAdmins
.retainAll(groupInfoV2
.getMembers());
755 if (currentAdmins
.contains(getSelfRecipientId())
756 && currentAdmins
.size() == 1
757 && groupInfoV2
.getMembers().size() > 1
758 && newAdmins
.size() == 0) {
759 // Last admin can't leave the group, unless she's also the last member
760 throw new LastGroupAdminException(groupInfoV2
.getGroupId(), groupInfoV2
.getTitle());
762 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
763 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
764 var messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
765 account
.getGroupStore().updateGroup(groupInfoV2
);
766 return sendMessage(messageBuilder
, groupInfoV2
.getMembersWithout(account
.getSelfRecipientId()));
769 public void deleteGroup(GroupId groupId
) throws IOException
{
770 account
.getGroupStore().deleteGroup(groupId
);
771 avatarStore
.deleteGroupAvatar(groupId
);
774 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
775 String name
, List
<String
> members
, File avatarFile
776 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
777 return createGroup(name
, members
== null ?
null : getSignalServiceAddresses(members
), avatarFile
);
780 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
781 String name
, Set
<RecipientId
> members
, File avatarFile
782 ) throws IOException
, AttachmentInvalidException
{
783 final var selfRecipientId
= account
.getSelfRecipientId();
784 if (members
!= null && members
.contains(selfRecipientId
)) {
785 members
= new HashSet
<>(members
);
786 members
.remove(selfRecipientId
);
789 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
790 members
== null ? Set
.of() : members
,
793 SignalServiceDataMessage
.Builder messageBuilder
;
794 if (gv2Pair
== null) {
795 // Failed to create v2 group, creating v1 group instead
796 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
797 gv1
.addMembers(List
.of(selfRecipientId
));
798 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
799 return new Pair
<>(gv1
.getGroupId(), result
.second());
802 final var gv2
= gv2Pair
.first();
803 final var decryptedGroup
= gv2Pair
.second();
805 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
806 if (avatarFile
!= null) {
807 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
808 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
810 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
811 account
.getGroupStore().updateGroup(gv2
);
813 final var result
= sendMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
814 return new Pair
<>(gv2
.getGroupId(), result
.second());
817 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
821 List
<String
> members
,
822 List
<String
> removeMembers
,
824 List
<String
> removeAdmins
,
825 boolean resetGroupLink
,
826 GroupLinkState groupLinkState
,
827 GroupPermission addMemberPermission
,
828 GroupPermission editDetailsPermission
,
830 Integer expirationTimer
831 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
832 return updateGroup(groupId
,
835 members
== null ?
null : getSignalServiceAddresses(members
),
836 removeMembers
== null ?
null : getSignalServiceAddresses(removeMembers
),
837 admins
== null ?
null : getSignalServiceAddresses(admins
),
838 removeAdmins
== null ?
null : getSignalServiceAddresses(removeAdmins
),
842 editDetailsPermission
,
847 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
848 final GroupId groupId
,
850 final String description
,
851 final Set
<RecipientId
> members
,
852 final Set
<RecipientId
> removeMembers
,
853 final Set
<RecipientId
> admins
,
854 final Set
<RecipientId
> removeAdmins
,
855 final boolean resetGroupLink
,
856 final GroupLinkState groupLinkState
,
857 final GroupPermission addMemberPermission
,
858 final GroupPermission editDetailsPermission
,
859 final File avatarFile
,
860 final Integer expirationTimer
861 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
862 var group
= getGroupForUpdating(groupId
);
864 if (group
instanceof GroupInfoV2
) {
866 return updateGroupV2((GroupInfoV2
) group
,
876 editDetailsPermission
,
879 } catch (ConflictException e
) {
880 // Detected conflicting update, refreshing group and trying again
881 group
= getGroup(groupId
, true);
882 return updateGroupV2((GroupInfoV2
) group
,
892 editDetailsPermission
,
898 final var gv1
= (GroupInfoV1
) group
;
899 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
900 if (expirationTimer
!= null) {
901 setExpirationTimer(gv1
, expirationTimer
);
906 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
907 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
908 ) throws IOException
, AttachmentInvalidException
{
909 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
910 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
912 account
.getGroupStore().updateGroup(gv1
);
914 return sendMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
917 private void updateGroupV1Details(
918 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
919 ) throws IOException
{
924 if (members
!= null) {
925 final var newMemberAddresses
= members
.stream()
926 .filter(member
-> !g
.isMember(member
))
927 .map(this::resolveSignalServiceAddress
)
928 .collect(Collectors
.toList());
929 final var newE164Members
= new HashSet
<String
>();
930 for (var member
: newMemberAddresses
) {
931 if (!member
.getNumber().isPresent()) {
934 newE164Members
.add(member
.getNumber().get());
937 final var registeredUsers
= getRegisteredUsers(newE164Members
);
938 if (registeredUsers
.size() != newE164Members
.size()) {
939 // Some of the new members are not registered on Signal
940 newE164Members
.removeAll(registeredUsers
.keySet());
941 throw new IOException("Failed to add members "
942 + String
.join(", ", newE164Members
)
943 + " to group: Not registered on Signal");
946 g
.addMembers(members
);
949 if (avatarFile
!= null) {
950 avatarStore
.storeGroupAvatar(g
.getGroupId(),
951 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
955 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
956 final GroupInfoV2 group
,
958 final String description
,
959 final Set
<RecipientId
> members
,
960 final Set
<RecipientId
> removeMembers
,
961 final Set
<RecipientId
> admins
,
962 final Set
<RecipientId
> removeAdmins
,
963 final boolean resetGroupLink
,
964 final GroupLinkState groupLinkState
,
965 final GroupPermission addMemberPermission
,
966 final GroupPermission editDetailsPermission
,
967 final File avatarFile
,
968 Integer expirationTimer
969 ) throws IOException
{
970 Pair
<Long
, List
<SendMessageResult
>> result
= null;
971 if (group
.isPendingMember(account
.getSelfRecipientId())) {
972 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
973 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
976 if (members
!= null) {
977 final var newMembers
= new HashSet
<>(members
);
978 newMembers
.removeAll(group
.getMembers());
979 if (newMembers
.size() > 0) {
980 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
981 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
985 if (removeMembers
!= null) {
986 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
987 existingRemoveMembers
.retainAll(group
.getMembers());
988 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
989 if (existingRemoveMembers
.size() > 0) {
990 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
991 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
994 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
995 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
996 if (pendingRemoveMembers
.size() > 0) {
997 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
998 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1002 if (admins
!= null) {
1003 final var newAdmins
= new HashSet
<>(admins
);
1004 newAdmins
.retainAll(group
.getMembers());
1005 newAdmins
.removeAll(group
.getAdminMembers());
1006 if (newAdmins
.size() > 0) {
1007 for (var admin
: newAdmins
) {
1008 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1009 result
= sendUpdateGroupV2Message(group
,
1010 groupGroupChangePair
.first(),
1011 groupGroupChangePair
.second());
1016 if (removeAdmins
!= null) {
1017 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1018 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1019 if (existingRemoveAdmins
.size() > 0) {
1020 for (var admin
: existingRemoveAdmins
) {
1021 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1022 result
= sendUpdateGroupV2Message(group
,
1023 groupGroupChangePair
.first(),
1024 groupGroupChangePair
.second());
1029 if (resetGroupLink
) {
1030 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1031 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1034 if (groupLinkState
!= null) {
1035 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1036 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1039 if (addMemberPermission
!= null) {
1040 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1041 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1044 if (editDetailsPermission
!= null) {
1045 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1046 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1049 if (expirationTimer
!= null) {
1050 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1051 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1054 if (name
!= null || description
!= null || avatarFile
!= null) {
1055 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1056 if (avatarFile
!= null) {
1057 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1058 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1060 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1066 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1067 GroupInviteLinkUrl inviteLinkUrl
1068 ) throws IOException
, GroupLinkNotActiveException
{
1069 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1070 inviteLinkUrl
.getPassword());
1071 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1072 inviteLinkUrl
.getPassword(),
1074 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1075 groupJoinInfo
.getRevision() + 1,
1076 groupChange
.toByteArray());
1078 if (group
.getGroup() == null) {
1079 // Only requested member, can't send update to group members
1080 return new Pair
<>(group
.getGroupId(), List
.of());
1083 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1085 return new Pair
<>(group
.getGroupId(), result
.second());
1088 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1089 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1090 ) throws IOException
{
1091 final var selfRecipientId
= account
.getSelfRecipientId();
1092 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1093 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1094 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1096 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1097 account
.getGroupStore().updateGroup(group
);
1098 return sendMessage(messageBuilder
, members
);
1101 private static int currentTimeDays() {
1102 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1105 private GroupsV2AuthorizationString
getGroupAuthForToday(
1106 final GroupSecretParams groupSecretParams
1107 ) throws IOException
{
1108 final var today
= currentTimeDays();
1109 // Returns credentials for the next 7 days
1110 final var credentials
= dependencies
.getGroupsV2Api().getCredentials(today
);
1111 // TODO cache credentials until they expire
1112 var authCredentialResponse
= credentials
.get(today
);
1114 return dependencies
.getGroupsV2Api()
1115 .getGroupsV2AuthorizationString(account
.getUuid(),
1118 authCredentialResponse
);
1119 } catch (VerificationFailedException e
) {
1120 throw new IOException(e
);
1124 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1125 GroupIdV1 groupId
, SignalServiceAddress recipient
1126 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1128 var group
= getGroupForSending(groupId
);
1129 if (!(group
instanceof GroupInfoV1
)) {
1130 throw new RuntimeException("Received an invalid group request for a v2 group!");
1132 g
= (GroupInfoV1
) group
;
1134 final var recipientId
= resolveRecipient(recipient
);
1135 if (!g
.isMember(recipientId
)) {
1136 throw new NotAGroupMemberException(groupId
, g
.name
);
1139 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1141 // Send group message only to the recipient who requested it
1142 return sendMessage(messageBuilder
, Set
.of(recipientId
));
1145 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1146 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1147 .withId(g
.getGroupId().serialize())
1149 .withMembers(g
.getMembers()
1151 .map(this::resolveSignalServiceAddress
)
1152 .collect(Collectors
.toList()));
1155 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1156 if (attachment
.isPresent()) {
1157 group
.withAvatar(attachment
.get());
1159 } catch (IOException e
) {
1160 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1163 return SignalServiceDataMessage
.newBuilder()
1164 .asGroupMessage(group
.build())
1165 .withExpiration(g
.getMessageExpirationTime());
1168 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1169 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1170 .withRevision(g
.getGroup().getRevision())
1171 .withSignedGroupChange(signedGroupChange
);
1172 return SignalServiceDataMessage
.newBuilder()
1173 .asGroupMessage(group
.build())
1174 .withExpiration(g
.getMessageExpirationTime());
1177 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1178 GroupIdV1 groupId
, SignalServiceAddress recipient
1179 ) throws IOException
{
1180 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1182 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1184 // Send group info request message to the recipient who sent us a message with this groupId
1185 return sendMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1189 SignalServiceAddress remoteAddress
, long messageId
1190 ) throws IOException
, UntrustedIdentityException
{
1191 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1193 System
.currentTimeMillis());
1195 dependencies
.getMessageSender()
1196 .sendReceipt(remoteAddress
,
1197 unidentifiedAccessHelper
.getAccessFor(resolveRecipient(remoteAddress
)),
1201 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1202 String messageText
, List
<String
> attachments
, List
<String
> recipients
1203 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1204 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1205 if (attachments
!= null) {
1206 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1208 // Upload attachments here, so we only upload once even for multiple recipients
1209 var messageSender
= dependencies
.getMessageSender();
1210 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1211 for (var attachment
: attachmentStreams
) {
1212 if (attachment
.isStream()) {
1213 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1214 } else if (attachment
.isPointer()) {
1215 attachmentPointers
.add(attachment
.asPointer());
1219 messageBuilder
.withAttachments(attachmentPointers
);
1221 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1224 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1225 String messageText
, List
<String
> attachments
1226 ) throws IOException
, AttachmentInvalidException
{
1227 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1228 if (attachments
!= null) {
1229 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1231 return sendSelfMessage(messageBuilder
);
1234 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1235 long targetSentTimestamp
, List
<String
> recipients
1236 ) throws IOException
, InvalidNumberException
{
1237 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1238 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1239 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1242 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1243 long targetSentTimestamp
, GroupId groupId
1244 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1245 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1246 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1247 return sendGroupMessage(messageBuilder
, groupId
);
1250 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1251 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1252 ) throws IOException
, InvalidNumberException
{
1253 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1254 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1256 resolveSignalServiceAddress(targetAuthorRecipientId
),
1257 targetSentTimestamp
);
1258 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1259 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1262 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1263 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1265 final var signalServiceAddresses
= getSignalServiceAddresses(recipients
);
1267 return sendMessage(messageBuilder
, signalServiceAddresses
);
1268 } catch (Exception e
) {
1269 for (var address
: signalServiceAddresses
) {
1270 handleEndSession(address
);
1276 void renewSession(RecipientId recipientId
) throws IOException
{
1277 account
.getSessionStore().archiveSessions(recipientId
);
1278 if (!recipientId
.equals(getSelfRecipientId())) {
1279 sendNullMessage(recipientId
);
1283 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1284 if (!account
.isMasterDevice()) {
1285 throw new NotMasterDeviceException();
1287 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1288 var contact
= account
.getContactStore().getContact(recipientId
);
1289 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1290 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1293 public void setContactBlocked(
1294 String number
, boolean blocked
1295 ) throws InvalidNumberException
, NotMasterDeviceException
{
1296 if (!account
.isMasterDevice()) {
1297 throw new NotMasterDeviceException();
1299 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1302 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1303 var contact
= account
.getContactStore().getContact(recipientId
);
1304 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1305 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1308 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1309 var group
= getGroup(groupId
);
1310 if (group
== null) {
1311 throw new GroupNotFoundException(groupId
);
1314 group
.setBlocked(blocked
);
1315 account
.getGroupStore().updateGroup(group
);
1318 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1319 var contact
= account
.getContactStore().getContact(recipientId
);
1320 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1323 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1324 account
.getContactStore()
1325 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1328 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1329 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1330 sendMessage(messageBuilder
, Set
.of(recipientId
));
1334 * Change the expiration timer for a contact
1336 public void setExpirationTimer(
1337 String number
, int messageExpirationTimer
1338 ) throws IOException
, InvalidNumberException
{
1339 var recipientId
= canonicalizeAndResolveRecipient(number
);
1340 setExpirationTimer(recipientId
, messageExpirationTimer
);
1341 sendExpirationTimerUpdate(recipientId
);
1345 * Change the expiration timer for a group
1347 private void setExpirationTimer(
1348 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1349 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1350 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1351 account
.getGroupStore().updateGroup(groupInfoV1
);
1352 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1355 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1356 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1357 sendGroupMessage(messageBuilder
, groupId
);
1361 * Upload the sticker pack from path.
1363 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1364 * @return if successful, returns the URL to install the sticker pack in the signal app
1366 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1367 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1369 var messageSender
= dependencies
.getMessageSender();
1371 var packKey
= KeyUtils
.createStickerUploadKey();
1372 var packIdString
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1373 var packId
= StickerPackId
.deserialize(Hex
.fromStringCondensed(packIdString
));
1375 var sticker
= new Sticker(packId
, packKey
);
1376 account
.getStickerStore().updateSticker(sticker
);
1379 return new URI("https",
1383 + URLEncoder
.encode(Hex
.toStringCondensed(packId
.serialize()), StandardCharsets
.UTF_8
)
1385 + URLEncoder
.encode(Hex
.toStringCondensed(packKey
), StandardCharsets
.UTF_8
)).toString();
1386 } catch (URISyntaxException e
) {
1387 throw new AssertionError(e
);
1391 public void requestAllSyncData() throws IOException
{
1392 requestSyncGroups();
1393 requestSyncContacts();
1394 requestSyncBlocked();
1395 requestSyncConfiguration();
1399 private void requestSyncGroups() throws IOException
{
1400 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1401 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1403 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1405 sendSyncMessage(message
);
1406 } catch (UntrustedIdentityException e
) {
1407 throw new AssertionError(e
);
1411 private void requestSyncContacts() throws IOException
{
1412 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1413 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1415 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1417 sendSyncMessage(message
);
1418 } catch (UntrustedIdentityException e
) {
1419 throw new AssertionError(e
);
1423 private void requestSyncBlocked() throws IOException
{
1424 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1425 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1427 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1429 sendSyncMessage(message
);
1430 } catch (UntrustedIdentityException e
) {
1431 throw new AssertionError(e
);
1435 private void requestSyncConfiguration() throws IOException
{
1436 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1437 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1439 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1441 sendSyncMessage(message
);
1442 } catch (UntrustedIdentityException e
) {
1443 throw new AssertionError(e
);
1447 private void requestSyncKeys() throws IOException
{
1448 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1449 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1451 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1453 sendSyncMessage(message
);
1454 } catch (UntrustedIdentityException e
) {
1455 throw new AssertionError(e
);
1459 private byte[] getSenderCertificate() {
1462 if (account
.isPhoneNumberShared()) {
1463 certificate
= dependencies
.getAccountManager().getSenderCertificate();
1465 certificate
= dependencies
.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
1467 } catch (IOException e
) {
1468 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1471 // TODO cache for a day
1475 private void sendSyncMessage(SignalServiceSyncMessage message
) throws IOException
, UntrustedIdentityException
{
1476 var messageSender
= dependencies
.getMessageSender();
1477 messageSender
.sendSyncMessage(message
, unidentifiedAccessHelper
.getAccessForSync());
1480 private Set
<RecipientId
> getSignalServiceAddresses(Collection
<String
> numbers
) throws InvalidNumberException
{
1481 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1482 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1484 for (var number
: numbers
) {
1485 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1486 if (resolvedAddress
.getUuid().isPresent()) {
1487 signalServiceAddresses
.add(resolvedAddress
);
1489 addressesMissingUuid
.add(resolvedAddress
);
1493 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1494 .map(a
-> a
.getNumber().get())
1495 .collect(Collectors
.toSet());
1496 Map
<String
, UUID
> registeredUsers
;
1498 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1499 } catch (IOException e
) {
1500 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1501 registeredUsers
= Map
.of();
1504 for (var address
: addressesMissingUuid
) {
1505 final var number
= address
.getNumber().get();
1506 if (registeredUsers
.containsKey(number
)) {
1507 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1508 registeredUsers
.get(number
),
1510 signalServiceAddresses
.add(newAddress
);
1512 signalServiceAddresses
.add(address
);
1516 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1519 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1520 final var address
= resolveSignalServiceAddress(recipientId
);
1521 if (!address
.getNumber().isPresent()) {
1524 final var number
= address
.getNumber().get();
1525 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1526 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1529 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1531 return dependencies
.getAccountManager()
1532 .getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1534 serviceEnvironmentConfig
.getCdsMrenclave());
1535 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1536 throw new IOException(e
);
1540 public void sendTypingMessage(
1541 TypingAction action
, Set
<String
> recipients
1542 ) throws IOException
, UntrustedIdentityException
, InvalidNumberException
{
1543 sendTypingMessageInternal(action
, getSignalServiceAddresses(recipients
));
1546 private void sendTypingMessageInternal(
1547 TypingAction action
, Set
<RecipientId
> recipientIds
1548 ) throws IOException
, UntrustedIdentityException
{
1549 final var timestamp
= System
.currentTimeMillis();
1550 var message
= new SignalServiceTypingMessage(action
.toSignalService(), timestamp
, Optional
.absent());
1551 var messageSender
= dependencies
.getMessageSender();
1552 for (var recipientId
: recipientIds
) {
1553 final var address
= resolveSignalServiceAddress(recipientId
);
1554 messageSender
.sendTyping(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
), message
);
1558 public void sendGroupTypingMessage(
1559 TypingAction action
, GroupId groupId
1560 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1561 final var timestamp
= System
.currentTimeMillis();
1562 final var g
= getGroupForSending(groupId
);
1563 final var message
= new SignalServiceTypingMessage(action
.toSignalService(),
1565 Optional
.of(groupId
.serialize()));
1566 final var messageSender
= dependencies
.getMessageSender();
1567 final var recipientIdList
= new ArrayList
<>(g
.getMembersWithout(account
.getSelfRecipientId()));
1568 final var addresses
= recipientIdList
.stream()
1569 .map(this::resolveSignalServiceAddress
)
1570 .collect(Collectors
.toList());
1571 messageSender
.sendTyping(addresses
, unidentifiedAccessHelper
.getAccessFor(recipientIdList
), message
, null);
1574 private Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1575 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientId
> recipientIds
1576 ) throws IOException
{
1577 final var timestamp
= System
.currentTimeMillis();
1578 messageBuilder
.withTimestamp(timestamp
);
1580 SignalServiceDataMessage message
= null;
1582 message
= messageBuilder
.build();
1583 if (message
.getGroupContext().isPresent()) {
1585 var messageSender
= dependencies
.getMessageSender();
1586 final var isRecipientUpdate
= false;
1587 final var recipientIdList
= new ArrayList
<>(recipientIds
);
1588 final var addresses
= recipientIdList
.stream()
1589 .map(this::resolveSignalServiceAddress
)
1590 .collect(Collectors
.toList());
1591 var result
= messageSender
.sendDataMessage(addresses
,
1592 unidentifiedAccessHelper
.getAccessFor(recipientIdList
),
1594 ContentHint
.DEFAULT
,
1596 sendResult
-> logger
.trace("Partial message send result: {}", sendResult
.isSuccess()),
1599 for (var r
: result
) {
1600 if (r
.getIdentityFailure() != null) {
1601 final var recipientId
= resolveRecipient(r
.getAddress());
1602 final var newIdentity
= account
.getIdentityKeyStore()
1603 .saveIdentity(recipientId
, r
.getIdentityFailure().getIdentityKey(), new Date());
1605 account
.getSessionStore().archiveSessions(recipientId
);
1610 return new Pair
<>(timestamp
, result
);
1611 } catch (UntrustedIdentityException e
) {
1612 return new Pair
<>(timestamp
, List
.of());
1615 // Send to all individually, so sync messages are sent correctly
1616 messageBuilder
.withProfileKey(account
.getProfileKey().serialize());
1617 var results
= new ArrayList
<SendMessageResult
>(recipientIds
.size());
1618 for (var recipientId
: recipientIds
) {
1619 final var contact
= account
.getContactStore().getContact(recipientId
);
1620 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1621 messageBuilder
.withExpiration(expirationTime
);
1622 message
= messageBuilder
.build();
1623 results
.add(sendMessage(recipientId
, message
));
1625 return new Pair
<>(timestamp
, results
);
1628 if (message
!= null && message
.isEndSession()) {
1629 for (var recipient
: recipientIds
) {
1630 handleEndSession(recipient
);
1636 private Pair
<Long
, SendMessageResult
> sendSelfMessage(
1637 SignalServiceDataMessage
.Builder messageBuilder
1638 ) throws IOException
{
1639 final var timestamp
= System
.currentTimeMillis();
1640 messageBuilder
.withTimestamp(timestamp
);
1641 final var recipientId
= account
.getSelfRecipientId();
1643 final var contact
= account
.getContactStore().getContact(recipientId
);
1644 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1645 messageBuilder
.withExpiration(expirationTime
);
1647 var message
= messageBuilder
.build();
1648 final var result
= sendSelfMessage(message
);
1649 return new Pair
<>(timestamp
, result
);
1652 private SendMessageResult
sendSelfMessage(SignalServiceDataMessage message
) throws IOException
{
1653 var messageSender
= dependencies
.getMessageSender();
1655 var recipientId
= account
.getSelfRecipientId();
1657 final var unidentifiedAccess
= unidentifiedAccessHelper
.getAccessFor(recipientId
);
1658 var recipient
= resolveSignalServiceAddress(recipientId
);
1659 var transcript
= new SentTranscriptMessage(Optional
.of(recipient
),
1660 message
.getTimestamp(),
1662 message
.getExpiresInSeconds(),
1663 Map
.of(recipient
, unidentifiedAccess
.isPresent()),
1665 var syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
1668 return messageSender
.sendSyncMessage(syncMessage
, unidentifiedAccess
);
1669 } catch (UntrustedIdentityException e
) {
1670 return SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey());
1674 private SendMessageResult
sendMessage(
1675 RecipientId recipientId
, SignalServiceDataMessage message
1676 ) throws IOException
{
1677 var messageSender
= dependencies
.getMessageSender();
1679 final var address
= resolveSignalServiceAddress(recipientId
);
1682 return messageSender
.sendDataMessage(address
,
1683 unidentifiedAccessHelper
.getAccessFor(recipientId
),
1684 ContentHint
.DEFAULT
,
1686 } catch (UnregisteredUserException e
) {
1687 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1688 return messageSender
.sendDataMessage(resolveSignalServiceAddress(newRecipientId
),
1689 unidentifiedAccessHelper
.getAccessFor(newRecipientId
),
1690 ContentHint
.DEFAULT
,
1693 } catch (UntrustedIdentityException e
) {
1694 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1698 private SendMessageResult
sendNullMessage(RecipientId recipientId
) throws IOException
{
1699 var messageSender
= dependencies
.getMessageSender();
1701 final var address
= resolveSignalServiceAddress(recipientId
);
1704 return messageSender
.sendNullMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
));
1705 } catch (UnregisteredUserException e
) {
1706 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1707 final var newAddress
= resolveSignalServiceAddress(newRecipientId
);
1708 return messageSender
.sendNullMessage(newAddress
, unidentifiedAccessHelper
.getAccessFor(newRecipientId
));
1710 } catch (UntrustedIdentityException e
) {
1711 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1715 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
, InvalidMessageStructureException
{
1716 return dependencies
.getCipher().decrypt(envelope
);
1719 private void handleEndSession(RecipientId recipientId
) {
1720 account
.getSessionStore().deleteAllSessions(recipientId
);
1723 private List
<HandleAction
> handleSignalServiceDataMessage(
1724 SignalServiceDataMessage message
,
1726 SignalServiceAddress source
,
1727 SignalServiceAddress destination
,
1728 boolean ignoreAttachments
1730 var actions
= new ArrayList
<HandleAction
>();
1731 if (message
.getGroupContext().isPresent()) {
1732 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1733 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1734 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1735 var group
= getGroup(groupId
);
1736 if (group
== null || group
instanceof GroupInfoV1
) {
1737 var groupV1
= (GroupInfoV1
) group
;
1738 switch (groupInfo
.getType()) {
1740 if (groupV1
== null) {
1741 groupV1
= new GroupInfoV1(groupId
);
1744 if (groupInfo
.getAvatar().isPresent()) {
1745 var avatar
= groupInfo
.getAvatar().get();
1746 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1749 if (groupInfo
.getName().isPresent()) {
1750 groupV1
.name
= groupInfo
.getName().get();
1753 if (groupInfo
.getMembers().isPresent()) {
1754 groupV1
.addMembers(groupInfo
.getMembers()
1757 .map(this::resolveRecipient
)
1758 .collect(Collectors
.toSet()));
1761 account
.getGroupStore().updateGroup(groupV1
);
1765 if (groupV1
== null && !isSync
) {
1766 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1770 if (groupV1
!= null) {
1771 groupV1
.removeMember(resolveRecipient(source
));
1772 account
.getGroupStore().updateGroup(groupV1
);
1777 if (groupV1
!= null && !isSync
) {
1778 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1783 // Received a group v1 message for a v2 group
1786 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1787 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1788 final var groupMasterKey
= groupContext
.getMasterKey();
1790 getOrMigrateGroup(groupMasterKey
,
1791 groupContext
.getRevision(),
1792 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1796 final var conversationPartnerAddress
= isSync ? destination
: source
;
1797 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1798 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1800 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1801 if (message
.getGroupContext().isPresent()) {
1802 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1803 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1804 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1805 if (group
!= null) {
1806 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1807 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1808 account
.getGroupStore().updateGroup(group
);
1811 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1812 // disappearing message timer already stored in the DecryptedGroup
1814 } else if (conversationPartnerAddress
!= null) {
1815 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1818 if (!ignoreAttachments
) {
1819 if (message
.getAttachments().isPresent()) {
1820 for (var attachment
: message
.getAttachments().get()) {
1821 downloadAttachment(attachment
);
1824 if (message
.getSharedContacts().isPresent()) {
1825 for (var contact
: message
.getSharedContacts().get()) {
1826 if (contact
.getAvatar().isPresent()) {
1827 downloadAttachment(contact
.getAvatar().get().getAttachment());
1832 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1833 final ProfileKey profileKey
;
1835 profileKey
= new ProfileKey(message
.getProfileKey().get());
1836 } catch (InvalidInputException e
) {
1837 throw new AssertionError(e
);
1839 if (source
.matches(account
.getSelfAddress())) {
1840 this.account
.setProfileKey(profileKey
);
1842 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1844 if (message
.getPreviews().isPresent()) {
1845 final var previews
= message
.getPreviews().get();
1846 for (var preview
: previews
) {
1847 if (preview
.getImage().isPresent()) {
1848 downloadAttachment(preview
.getImage().get());
1852 if (message
.getQuote().isPresent()) {
1853 final var quote
= message
.getQuote().get();
1855 for (var quotedAttachment
: quote
.getAttachments()) {
1856 final var thumbnail
= quotedAttachment
.getThumbnail();
1857 if (thumbnail
!= null) {
1858 downloadAttachment(thumbnail
);
1862 if (message
.getSticker().isPresent()) {
1863 final var messageSticker
= message
.getSticker().get();
1864 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1865 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1866 if (sticker
== null) {
1867 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1868 account
.getStickerStore().updateSticker(sticker
);
1870 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, messageSticker
.getPackKey()));
1875 private GroupInfoV2
getOrMigrateGroup(
1876 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1878 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1880 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1881 var groupInfo
= getGroup(groupId
);
1882 final GroupInfoV2 groupInfoV2
;
1883 if (groupInfo
instanceof GroupInfoV1
) {
1884 // Received a v2 group message for a v1 group, we need to locally migrate the group
1885 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1886 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1887 logger
.info("Locally migrated group {} to group v2, id: {}",
1888 groupInfo
.getGroupId().toBase64(),
1889 groupInfoV2
.getGroupId().toBase64());
1890 } else if (groupInfo
instanceof GroupInfoV2
) {
1891 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1893 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1896 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1897 DecryptedGroup group
= null;
1898 if (signedGroupChange
!= null
1899 && groupInfoV2
.getGroup() != null
1900 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1901 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1905 if (group
== null) {
1906 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1908 if (group
!= null) {
1909 storeProfileKeysFromMembers(group
);
1910 final var avatar
= group
.getAvatar();
1911 if (avatar
!= null && !avatar
.isEmpty()) {
1912 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1915 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1916 account
.getGroupStore().updateGroup(groupInfoV2
);
1922 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1923 for (var member
: group
.getMembersList()) {
1924 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1925 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1927 account
.getProfileStore()
1928 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1929 } catch (InvalidInputException ignored
) {
1934 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1935 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1936 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1937 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1938 if (actions
!= null) {
1939 queuedActions
.addAll(actions
);
1942 for (var action
: queuedActions
) {
1944 action
.execute(this);
1945 } catch (Throwable e
) {
1946 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1947 Thread
.currentThread().interrupt();
1949 logger
.warn("Message action failed.", e
);
1954 private List
<HandleAction
> retryFailedReceivedMessage(
1955 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1957 var envelope
= cachedMessage
.loadEnvelope();
1958 if (envelope
== null) {
1961 SignalServiceContent content
= null;
1962 List
<HandleAction
> actions
= null;
1963 if (!envelope
.isReceipt()) {
1965 content
= decryptMessage(envelope
);
1966 } catch (ProtocolUntrustedIdentityException e
) {
1967 if (!envelope
.hasSource()) {
1968 final var identifier
= e
.getSender();
1969 final var recipientId
= resolveRecipient(identifier
);
1971 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1972 } catch (IOException ioException
) {
1973 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1977 } catch (Exception er
) {
1978 // All other errors are not recoverable, so delete the cached message
1979 cachedMessage
.delete();
1982 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1984 handler
.handleMessage(envelope
, content
, null);
1985 cachedMessage
.delete();
1989 public void receiveMessages(
1992 boolean returnOnTimeout
,
1993 boolean ignoreAttachments
,
1994 ReceiveMessageHandler handler
1995 ) throws IOException
, InterruptedException
{
1996 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1998 Set
<HandleAction
> queuedActions
= null;
2000 final var signalWebSocket
= dependencies
.getSignalWebSocket();
2001 signalWebSocket
.connect();
2003 var hasCaughtUpWithOldMessages
= false;
2005 while (!Thread
.interrupted()) {
2006 SignalServiceEnvelope envelope
;
2007 SignalServiceContent content
= null;
2008 Exception exception
= null;
2009 final CachedMessage
[] cachedMessage
= {null};
2010 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
2011 logger
.debug("Checking for new message from server");
2013 var result
= signalWebSocket
.readOrEmpty(unit
.toMillis(timeout
), envelope1
-> {
2014 final var recipientId
= envelope1
.hasSource()
2015 ?
resolveRecipient(envelope1
.getSourceIdentifier())
2017 // store message on disk, before acknowledging receipt to the server
2018 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
2020 logger
.debug("New message received from server");
2021 if (result
.isPresent()) {
2022 envelope
= result
.get();
2024 // Received indicator that server queue is empty
2025 hasCaughtUpWithOldMessages
= true;
2027 if (queuedActions
!= null) {
2028 for (var action
: queuedActions
) {
2030 action
.execute(this);
2031 } catch (Throwable e
) {
2032 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2033 Thread
.currentThread().interrupt();
2035 logger
.warn("Message action failed.", e
);
2038 queuedActions
.clear();
2039 queuedActions
= null;
2042 // Continue to wait another timeout for new messages
2045 } catch (AssertionError e
) {
2046 if (e
.getCause() instanceof InterruptedException
) {
2047 throw (InterruptedException
) e
.getCause();
2051 } catch (WebSocketUnavailableException e
) {
2052 logger
.debug("Pipe unexpectedly unavailable, connecting");
2053 signalWebSocket
.connect();
2055 } catch (TimeoutException e
) {
2056 if (returnOnTimeout
) return;
2060 if (envelope
.hasSource()) {
2061 // Store uuid if we don't have it already
2062 // address/uuid in envelope is sent by server
2063 resolveRecipientTrusted(envelope
.getSourceAddress());
2065 final var notAGroupMember
= isNotAGroupMember(envelope
, content
);
2066 if (!envelope
.isReceipt()) {
2068 content
= decryptMessage(envelope
);
2069 } catch (Exception e
) {
2072 if (!envelope
.hasSource() && content
!= null) {
2073 // Store uuid if we don't have it already
2074 // address/uuid is validated by unidentified sender certificate
2075 resolveRecipientTrusted(content
.getSender());
2077 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2078 if (exception
instanceof ProtocolInvalidMessageException
) {
2079 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
2080 logger
.debug("Received invalid message, queuing renew session action.");
2081 actions
.add(new RenewSessionAction(sender
));
2083 if (hasCaughtUpWithOldMessages
) {
2084 for (var action
: actions
) {
2086 action
.execute(this);
2087 } catch (Throwable e
) {
2088 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2089 Thread
.currentThread().interrupt();
2091 logger
.warn("Message action failed.", e
);
2095 if (queuedActions
== null) {
2096 queuedActions
= new HashSet
<>();
2098 queuedActions
.addAll(actions
);
2101 if (isMessageBlocked(envelope
, content
)) {
2102 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
2103 } else if (notAGroupMember
) {
2104 logger
.info("Ignoring a message from a non group member: {}", envelope
.getTimestamp());
2106 handler
.handleMessage(envelope
, content
, exception
);
2108 if (cachedMessage
[0] != null) {
2109 if (exception
instanceof ProtocolUntrustedIdentityException
) {
2110 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
2111 final var recipientId
= resolveRecipient(identifier
);
2112 queuedActions
.add(new RetrieveProfileAction(recipientId
));
2113 if (!envelope
.hasSource()) {
2115 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
2116 } catch (IOException ioException
) {
2117 logger
.warn("Failed to move cached message to recipient folder: {}",
2118 ioException
.getMessage());
2122 cachedMessage
[0].delete();
2128 private boolean isMessageBlocked(
2129 SignalServiceEnvelope envelope
, SignalServiceContent content
2131 SignalServiceAddress source
;
2132 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2133 source
= envelope
.getSourceAddress();
2134 } else if (content
!= null) {
2135 source
= content
.getSender();
2139 final var recipientId
= resolveRecipient(source
);
2140 if (isContactBlocked(recipientId
)) {
2144 if (content
!= null && content
.getDataMessage().isPresent()) {
2145 var message
= content
.getDataMessage().get();
2146 if (message
.getGroupContext().isPresent()) {
2147 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2148 var group
= getGroup(groupId
);
2149 if (group
!= null && group
.isBlocked()) {
2157 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2158 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2159 return isContactBlocked(recipientId
);
2162 private boolean isContactBlocked(final RecipientId recipientId
) {
2163 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2164 return sourceContact
!= null && sourceContact
.isBlocked();
2167 private boolean isNotAGroupMember(
2168 SignalServiceEnvelope envelope
, SignalServiceContent content
2170 SignalServiceAddress source
;
2171 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2172 source
= envelope
.getSourceAddress();
2173 } else if (content
!= null) {
2174 source
= content
.getSender();
2179 if (content
!= null && content
.getDataMessage().isPresent()) {
2180 var message
= content
.getDataMessage().get();
2181 if (message
.getGroupContext().isPresent()) {
2182 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2183 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2184 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2188 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2189 var group
= getGroup(groupId
);
2190 if (group
!= null && !group
.isMember(resolveRecipient(source
))) {
2198 private List
<HandleAction
> handleMessage(
2199 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2201 var actions
= new ArrayList
<HandleAction
>();
2202 if (content
!= null) {
2203 final SignalServiceAddress sender
;
2204 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2205 sender
= envelope
.getSourceAddress();
2207 sender
= content
.getSender();
2210 if (content
.getDataMessage().isPresent()) {
2211 var message
= content
.getDataMessage().get();
2213 if (content
.isNeedsReceipt()) {
2214 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2217 actions
.addAll(handleSignalServiceDataMessage(message
,
2220 account
.getSelfAddress(),
2221 ignoreAttachments
));
2223 if (content
.getSyncMessage().isPresent()) {
2224 account
.setMultiDevice(true);
2225 var syncMessage
= content
.getSyncMessage().get();
2226 if (syncMessage
.getSent().isPresent()) {
2227 var message
= syncMessage
.getSent().get();
2228 final var destination
= message
.getDestination().orNull();
2229 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2233 ignoreAttachments
));
2235 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2236 var rm
= syncMessage
.getRequest().get();
2237 if (rm
.isContactsRequest()) {
2238 actions
.add(SendSyncContactsAction
.create());
2240 if (rm
.isGroupsRequest()) {
2241 actions
.add(SendSyncGroupsAction
.create());
2243 if (rm
.isBlockedListRequest()) {
2244 actions
.add(SendSyncBlockedListAction
.create());
2246 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2248 if (syncMessage
.getGroups().isPresent()) {
2249 File tmpFile
= null;
2251 tmpFile
= IOUtils
.createTempFile();
2252 final var groupsMessage
= syncMessage
.getGroups().get();
2253 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2254 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2259 } catch (IOException e
) {
2260 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2266 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2267 if (syncGroup
!= null) {
2268 if (g
.getName().isPresent()) {
2269 syncGroup
.name
= g
.getName().get();
2271 syncGroup
.addMembers(g
.getMembers()
2273 .map(this::resolveRecipient
)
2274 .collect(Collectors
.toSet()));
2275 if (!g
.isActive()) {
2276 syncGroup
.removeMember(account
.getSelfRecipientId());
2278 // Add ourself to the member set as it's marked as active
2279 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2281 syncGroup
.blocked
= g
.isBlocked();
2282 if (g
.getColor().isPresent()) {
2283 syncGroup
.color
= g
.getColor().get();
2286 if (g
.getAvatar().isPresent()) {
2287 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2289 syncGroup
.archived
= g
.isArchived();
2290 account
.getGroupStore().updateGroup(syncGroup
);
2294 } catch (Exception e
) {
2295 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2299 if (tmpFile
!= null) {
2301 Files
.delete(tmpFile
.toPath());
2302 } catch (IOException e
) {
2303 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2310 if (syncMessage
.getBlockedList().isPresent()) {
2311 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2312 for (var address
: blockedListMessage
.getAddresses()) {
2313 setContactBlocked(resolveRecipient(address
), true);
2315 for (var groupId
: blockedListMessage
.getGroupIds()
2317 .map(GroupId
::unknownVersion
)
2318 .collect(Collectors
.toSet())) {
2320 setGroupBlocked(groupId
, true);
2321 } catch (GroupNotFoundException e
) {
2322 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2323 groupId
.toBase64());
2327 if (syncMessage
.getContacts().isPresent()) {
2328 File tmpFile
= null;
2330 tmpFile
= IOUtils
.createTempFile();
2331 final var contactsMessage
= syncMessage
.getContacts().get();
2332 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2333 .asPointer(), tmpFile
)) {
2334 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2339 } catch (IOException e
) {
2340 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2347 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2348 account
.setProfileKey(c
.getProfileKey().get());
2350 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2351 var contact
= account
.getContactStore().getContact(recipientId
);
2352 final var builder
= contact
== null
2353 ? Contact
.newBuilder()
2354 : Contact
.newBuilder(contact
);
2355 if (c
.getName().isPresent()) {
2356 builder
.withName(c
.getName().get());
2358 if (c
.getColor().isPresent()) {
2359 builder
.withColor(c
.getColor().get());
2361 if (c
.getProfileKey().isPresent()) {
2362 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2364 if (c
.getVerified().isPresent()) {
2365 final var verifiedMessage
= c
.getVerified().get();
2366 account
.getIdentityKeyStore()
2367 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2368 verifiedMessage
.getIdentityKey(),
2369 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2371 if (c
.getExpirationTimer().isPresent()) {
2372 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2374 builder
.withBlocked(c
.isBlocked());
2375 builder
.withArchived(c
.isArchived());
2376 account
.getContactStore().storeContact(recipientId
, builder
.build());
2378 if (c
.getAvatar().isPresent()) {
2379 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2383 } catch (Exception e
) {
2384 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2388 if (tmpFile
!= null) {
2390 Files
.delete(tmpFile
.toPath());
2391 } catch (IOException e
) {
2392 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2399 if (syncMessage
.getVerified().isPresent()) {
2400 final var verifiedMessage
= syncMessage
.getVerified().get();
2401 account
.getIdentityKeyStore()
2402 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2403 verifiedMessage
.getIdentityKey(),
2404 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2406 if (syncMessage
.getStickerPackOperations().isPresent()) {
2407 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2408 for (var m
: stickerPackOperationMessages
) {
2409 if (!m
.getPackId().isPresent()) {
2412 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2413 final var installed
= !m
.getType().isPresent()
2414 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
;
2416 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2417 if (m
.getPackKey().isPresent()) {
2418 if (sticker
== null) {
2419 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2422 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, m
.getPackKey().get()));
2426 if (sticker
!= null) {
2427 sticker
.setInstalled(installed
);
2428 account
.getStickerStore().updateSticker(sticker
);
2432 if (syncMessage
.getFetchType().isPresent()) {
2433 switch (syncMessage
.getFetchType().get()) {
2435 getRecipientProfile(account
.getSelfRecipientId(), true);
2436 case STORAGE_MANIFEST
:
2440 if (syncMessage
.getKeys().isPresent()) {
2441 final var keysMessage
= syncMessage
.getKeys().get();
2442 if (keysMessage
.getStorageService().isPresent()) {
2443 final var storageKey
= keysMessage
.getStorageService().get();
2444 account
.setStorageKey(storageKey
);
2447 if (syncMessage
.getConfiguration().isPresent()) {
2455 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2457 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2458 } catch (IOException e
) {
2459 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2463 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2465 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2466 } catch (IOException e
) {
2467 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2471 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2473 avatarStore
.storeGroupAvatar(groupId
,
2474 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2475 } catch (IOException e
) {
2476 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2480 private void downloadProfileAvatar(
2481 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2484 avatarStore
.storeProfileAvatar(address
,
2485 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2486 } catch (Throwable e
) {
2487 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2488 Thread
.currentThread().interrupt();
2490 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2494 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2495 return attachmentStore
.getAttachmentFile(attachmentId
);
2498 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2499 if (!attachment
.isPointer()) {
2500 logger
.warn("Invalid state, can't store an attachment stream.");
2503 var pointer
= attachment
.asPointer();
2504 if (pointer
.getPreview().isPresent()) {
2505 final var preview
= pointer
.getPreview().get();
2507 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2508 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2509 } catch (IOException e
) {
2510 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2515 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2516 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2517 } catch (IOException e
) {
2518 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2522 private void retrieveGroupV2Avatar(
2523 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2524 ) throws IOException
{
2525 var groupOperations
= dependencies
.getGroupsV2Operations().forGroup(groupSecretParams
);
2527 var tmpFile
= IOUtils
.createTempFile();
2528 try (InputStream input
= dependencies
.getMessageReceiver()
2529 .retrieveGroupsV2ProfileAvatar(cdnKey
, tmpFile
, ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2530 var encryptedData
= IOUtils
.readFully(input
);
2532 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2533 outputStream
.write(decryptedData
);
2536 Files
.delete(tmpFile
.toPath());
2537 } catch (IOException e
) {
2538 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2545 private void retrieveProfileAvatar(
2546 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2547 ) throws IOException
{
2548 var tmpFile
= IOUtils
.createTempFile();
2549 try (var input
= dependencies
.getMessageReceiver()
2550 .retrieveProfileAvatar(avatarPath
,
2553 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2554 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2555 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2558 Files
.delete(tmpFile
.toPath());
2559 } catch (IOException e
) {
2560 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2567 private void retrieveAttachment(
2568 final SignalServiceAttachment attachment
, final OutputStream outputStream
2569 ) throws IOException
{
2570 if (attachment
.isPointer()) {
2571 var pointer
= attachment
.asPointer();
2572 retrieveAttachmentPointer(pointer
, outputStream
);
2574 var stream
= attachment
.asStream();
2575 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2579 private void retrieveAttachmentPointer(
2580 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2581 ) throws IOException
{
2582 var tmpFile
= IOUtils
.createTempFile();
2583 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2584 IOUtils
.copyStream(input
, outputStream
);
2585 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2586 throw new IOException(e
);
2589 Files
.delete(tmpFile
.toPath());
2590 } catch (IOException e
) {
2591 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2598 private InputStream
retrieveAttachmentAsStream(
2599 SignalServiceAttachmentPointer pointer
, File tmpFile
2600 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2601 return dependencies
.getMessageReceiver()
2602 .retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2605 void sendGroups() throws IOException
, UntrustedIdentityException
{
2606 var groupsFile
= IOUtils
.createTempFile();
2609 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2610 var out
= new DeviceGroupsOutputStream(fos
);
2611 for (var record : getGroups()) {
2612 if (record instanceof GroupInfoV1
) {
2613 var groupInfo
= (GroupInfoV1
) record;
2614 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2615 Optional
.fromNullable(groupInfo
.name
),
2616 groupInfo
.getMembers()
2618 .map(this::resolveSignalServiceAddress
)
2619 .collect(Collectors
.toList()),
2620 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2621 groupInfo
.isMember(account
.getSelfRecipientId()),
2622 Optional
.of(groupInfo
.messageExpirationTime
),
2623 Optional
.fromNullable(groupInfo
.color
),
2626 groupInfo
.archived
));
2631 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2632 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2633 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2634 .withStream(groupsFileStream
)
2635 .withContentType("application/octet-stream")
2636 .withLength(groupsFile
.length())
2639 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2644 Files
.delete(groupsFile
.toPath());
2645 } catch (IOException e
) {
2646 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2651 public void sendContacts() throws IOException
, UntrustedIdentityException
{
2652 var contactsFile
= IOUtils
.createTempFile();
2655 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2656 var out
= new DeviceContactsOutputStream(fos
);
2657 for (var contactPair
: account
.getContactStore().getContacts()) {
2658 final var recipientId
= contactPair
.first();
2659 final var contact
= contactPair
.second();
2660 final var address
= resolveSignalServiceAddress(recipientId
);
2662 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2663 VerifiedMessage verifiedMessage
= null;
2664 if (currentIdentity
!= null) {
2665 verifiedMessage
= new VerifiedMessage(address
,
2666 currentIdentity
.getIdentityKey(),
2667 currentIdentity
.getTrustLevel().toVerifiedState(),
2668 currentIdentity
.getDateAdded().getTime());
2671 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2672 out
.write(new DeviceContact(address
,
2673 Optional
.fromNullable(contact
.getName()),
2674 createContactAvatarAttachment(address
),
2675 Optional
.fromNullable(contact
.getColor()),
2676 Optional
.fromNullable(verifiedMessage
),
2677 Optional
.fromNullable(profileKey
),
2678 contact
.isBlocked(),
2679 Optional
.of(contact
.getMessageExpirationTime()),
2681 contact
.isArchived()));
2684 if (account
.getProfileKey() != null) {
2685 // Send our own profile key as well
2686 out
.write(new DeviceContact(account
.getSelfAddress(),
2691 Optional
.of(account
.getProfileKey()),
2699 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2700 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2701 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2702 .withStream(contactsFileStream
)
2703 .withContentType("application/octet-stream")
2704 .withLength(contactsFile
.length())
2707 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
2712 Files
.delete(contactsFile
.toPath());
2713 } catch (IOException e
) {
2714 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2719 void sendBlockedList() throws IOException
, UntrustedIdentityException
{
2720 var addresses
= new ArrayList
<SignalServiceAddress
>();
2721 for (var record : account
.getContactStore().getContacts()) {
2722 if (record.second().isBlocked()) {
2723 addresses
.add(resolveSignalServiceAddress(record.first()));
2726 var groupIds
= new ArrayList
<byte[]>();
2727 for (var record : getGroups()) {
2728 if (record.isBlocked()) {
2729 groupIds
.add(record.getGroupId().serialize());
2732 sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2735 private void sendVerifiedMessage(
2736 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2737 ) throws IOException
, UntrustedIdentityException
{
2738 var verifiedMessage
= new VerifiedMessage(destination
,
2740 trustLevel
.toVerifiedState(),
2741 System
.currentTimeMillis());
2742 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2745 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2746 return account
.getContactStore().getContacts();
2749 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2750 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2751 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2752 if (recipient
== null) {
2756 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2757 return recipient
.getContact().getName();
2760 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2761 return recipient
.getProfile().getDisplayName();
2767 public GroupInfo
getGroup(GroupId groupId
) {
2768 return getGroup(groupId
, false);
2771 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2772 final var group
= account
.getGroupStore().getGroup(groupId
);
2773 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2774 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2775 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2776 account
.getGroupStore().updateGroup(group
);
2781 public List
<IdentityInfo
> getIdentities() {
2782 return account
.getIdentityKeyStore().getIdentities();
2785 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2786 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2787 return identity
== null ? List
.of() : List
.of(identity
);
2791 * Trust this the identity with this fingerprint
2793 * @param name username of the identity
2794 * @param fingerprint Fingerprint
2796 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2797 var recipientId
= canonicalizeAndResolveRecipient(name
);
2798 return trustIdentity(recipientId
,
2799 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2800 TrustLevel
.TRUSTED_VERIFIED
);
2804 * Trust this the identity with this safety number
2806 * @param name username of the identity
2807 * @param safetyNumber Safety number
2809 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2810 var recipientId
= canonicalizeAndResolveRecipient(name
);
2811 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2812 return trustIdentity(recipientId
,
2813 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2814 TrustLevel
.TRUSTED_VERIFIED
);
2818 * Trust all keys of this identity without verification
2820 * @param name username of the identity
2822 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2823 var recipientId
= canonicalizeAndResolveRecipient(name
);
2824 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2827 private boolean trustIdentity(
2828 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2830 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2831 if (identity
== null) {
2835 if (!verifier
.apply(identity
.getIdentityKey())) {
2839 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2841 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2842 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2843 } catch (IOException
| UntrustedIdentityException e
) {
2844 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2850 public String
computeSafetyNumber(
2851 SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
2853 return Utils
.computeSafetyNumber(ServiceConfig
.capabilities
.isUuid(),
2854 account
.getSelfAddress(),
2855 getIdentityKeyPair().getPublicKey(),
2861 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2862 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2864 return resolveSignalServiceAddress(address
);
2868 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2869 if (address
.matches(account
.getSelfAddress())) {
2870 return account
.getSelfAddress();
2873 return account
.getRecipientStore().resolveServiceAddress(address
);
2876 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2877 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2880 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2881 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2883 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2885 return resolveRecipient(canonicalizedNumber
);
2888 private RecipientId
resolveRecipient(final String identifier
) {
2889 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2891 return resolveRecipient(address
);
2894 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2895 return account
.getRecipientStore().resolveRecipient(address
);
2898 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2899 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2902 private void enqueueJob(Job job
) {
2903 var context
= new Context(account
,
2904 dependencies
.getAccountManager(),
2905 dependencies
.getMessageReceiver(),
2911 public void close() throws IOException
{
2915 void close(boolean closeAccount
) throws IOException
{
2916 executor
.shutdown();
2918 dependencies
.getSignalWebSocket().disconnect();
2920 if (closeAccount
&& account
!= null) {
2926 public interface ReceiveMessageHandler
{
2928 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);