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
.SendHelper
;
37 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
38 import org
.asamk
.signal
.manager
.jobs
.Context
;
39 import org
.asamk
.signal
.manager
.jobs
.Job
;
40 import org
.asamk
.signal
.manager
.jobs
.RetrieveStickerPackJob
;
41 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
42 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
43 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
44 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
45 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
46 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
47 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
48 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
49 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
50 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
51 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
52 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
53 import org
.asamk
.signal
.manager
.util
.IOUtils
;
54 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
55 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
56 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
57 import org
.asamk
.signal
.manager
.util
.Utils
;
58 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
59 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
60 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
61 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
62 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
63 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
64 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
65 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
66 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
67 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
68 import org
.signal
.libsignal
.metadata
.SelfSendException
;
69 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
70 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
71 import org
.signal
.zkgroup
.InvalidInputException
;
72 import org
.signal
.zkgroup
.VerificationFailedException
;
73 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
74 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
75 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
76 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
77 import org
.slf4j
.Logger
;
78 import org
.slf4j
.LoggerFactory
;
79 import org
.whispersystems
.libsignal
.IdentityKey
;
80 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
81 import org
.whispersystems
.libsignal
.InvalidKeyException
;
82 import org
.whispersystems
.libsignal
.InvalidMessageException
;
83 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
84 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
85 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
86 import org
.whispersystems
.libsignal
.util
.Pair
;
87 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
88 import org
.whispersystems
.signalservice
.api
.InvalidMessageStructureException
;
89 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
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
.SignalServiceSyncMessage
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
117 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
118 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
119 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
120 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.ConflictException
;
121 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
122 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
123 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
124 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
125 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
126 import org
.whispersystems
.signalservice
.api
.websocket
.WebSocketUnavailableException
;
127 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
128 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
129 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
130 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
131 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
132 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
133 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
134 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
136 import java
.io
.Closeable
;
138 import java
.io
.FileInputStream
;
139 import java
.io
.FileOutputStream
;
140 import java
.io
.IOException
;
141 import java
.io
.InputStream
;
142 import java
.io
.OutputStream
;
144 import java
.net
.URISyntaxException
;
145 import java
.net
.URLEncoder
;
146 import java
.nio
.charset
.StandardCharsets
;
147 import java
.nio
.file
.Files
;
148 import java
.security
.SignatureException
;
149 import java
.util
.ArrayList
;
150 import java
.util
.Arrays
;
151 import java
.util
.Base64
;
152 import java
.util
.Collection
;
153 import java
.util
.Date
;
154 import java
.util
.HashSet
;
155 import java
.util
.List
;
156 import java
.util
.Map
;
157 import java
.util
.Set
;
158 import java
.util
.UUID
;
159 import java
.util
.concurrent
.ExecutorService
;
160 import java
.util
.concurrent
.Executors
;
161 import java
.util
.concurrent
.TimeUnit
;
162 import java
.util
.concurrent
.TimeoutException
;
163 import java
.util
.concurrent
.locks
.ReentrantLock
;
164 import java
.util
.function
.Function
;
165 import java
.util
.stream
.Collectors
;
167 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
169 public class Manager
implements Closeable
{
171 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
173 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
174 private final SignalDependencies dependencies
;
176 private SignalAccount account
;
178 private final ExecutorService executor
= Executors
.newCachedThreadPool();
180 private final ProfileHelper profileHelper
;
181 private final GroupV2Helper groupV2Helper
;
182 private final PinHelper pinHelper
;
183 private final SendHelper sendHelper
;
185 private final AvatarStore avatarStore
;
186 private final AttachmentStore attachmentStore
;
187 private final StickerPackStore stickerPackStore
;
188 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
189 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
192 public Lock
acquire() {
194 return LEGACY_LOCK
::unlock
;
199 SignalAccount account
,
200 PathConfig pathConfig
,
201 ServiceEnvironmentConfig serviceEnvironmentConfig
,
204 this.account
= account
;
205 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
207 final var credentialsProvider
= new DynamicCredentialsProvider(account
.getUuid(),
208 account
.getUsername(),
209 account
.getPassword(),
210 account
.getDeviceId());
211 this.dependencies
= new SignalDependencies(account
.getSelfAddress(),
212 serviceEnvironmentConfig
,
215 account
.getSignalProtocolStore(),
218 this.pinHelper
= new PinHelper(dependencies
.getKeyBackupService());
220 final var unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
221 account
.getProfileStore()::getProfileKey
,
222 this::getRecipientProfile
,
223 this::getSenderCertificate
);
224 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
225 unidentifiedAccessHelper
::getAccessFor
,
226 dependencies
::getProfileService
,
227 dependencies
::getMessageReceiver
,
228 this::resolveSignalServiceAddress
);
229 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
230 this::getRecipientProfile
,
231 account
::getSelfRecipientId
,
232 dependencies
.getGroupsV2Operations(),
233 dependencies
.getGroupsV2Api(),
234 this::getGroupAuthForToday
,
235 this::resolveSignalServiceAddress
);
236 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
237 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
238 this.stickerPackStore
= new StickerPackStore(pathConfig
.getStickerPacksPath());
239 this.sendHelper
= new SendHelper(account
,
241 unidentifiedAccessHelper
,
242 this::resolveSignalServiceAddress
,
243 this::resolveRecipient
,
244 this::handleIdentityFailure
,
246 this::refreshRegisteredUser
);
249 public String
getUsername() {
250 return account
.getUsername();
253 public SignalServiceAddress
getSelfAddress() {
254 return account
.getSelfAddress();
257 public RecipientId
getSelfRecipientId() {
258 return account
.getSelfRecipientId();
261 private IdentityKeyPair
getIdentityKeyPair() {
262 return account
.getIdentityKeyPair();
265 public int getDeviceId() {
266 return account
.getDeviceId();
269 public static Manager
init(
270 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
271 ) throws IOException
, NotRegisteredException
{
272 var pathConfig
= PathConfig
.createDefault(settingsPath
);
274 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
275 throw new NotRegisteredException();
278 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
280 if (!account
.isRegistered()) {
281 throw new NotRegisteredException();
284 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
286 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
289 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
290 var pathConfig
= PathConfig
.createDefault(settingsPath
);
291 final var dataPath
= pathConfig
.getDataPath();
292 final var files
= dataPath
.listFiles();
298 return Arrays
.stream(files
)
299 .filter(File
::isFile
)
301 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
302 .collect(Collectors
.toList());
305 public void checkAccountState() throws IOException
{
306 if (account
.getLastReceiveTimestamp() == 0) {
307 logger
.warn("The Signal protocol expects that incoming messages are regularly received.");
309 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
310 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
313 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
317 if (dependencies
.getAccountManager().getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
320 if (account
.getUuid() == null) {
321 account
.setUuid(dependencies
.getAccountManager().getOwnUuid());
323 updateAccountAttributes();
327 * This is used for checking a set of phone numbers for registration on Signal
329 * @param numbers The set of phone number in question
330 * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
331 * @throws IOException if its unable to get the contacts to check if they're registered
333 public Map
<String
, Boolean
> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
334 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
335 var contactDetails
= getRegisteredUsers(numbers
);
337 var registeredUsers
= contactDetails
.keySet();
339 return numbers
.stream().collect(Collectors
.toMap(x
-> x
, registeredUsers
::contains
));
342 public void updateAccountAttributes() throws IOException
{
343 dependencies
.getAccountManager()
344 .setAccountAttributes(account
.getEncryptedDeviceName(),
346 account
.getLocalRegistrationId(),
348 // set legacy pin only if no KBS master key is set
349 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
350 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
351 account
.getSelfUnidentifiedAccessKey(),
352 account
.isUnrestrictedUnidentifiedAccess(),
354 account
.isDiscoverableByPhoneNumber());
358 * @param givenName if null, the previous givenName will be kept
359 * @param familyName if null, the previous familyName will be kept
360 * @param about if null, the previous about text will be kept
361 * @param aboutEmoji if null, the previous about emoji will be kept
362 * @param avatar if avatar is null the image from the local avatar store is used (if present),
364 public void setProfile(
365 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
366 ) throws IOException
{
367 var profile
= getRecipientProfile(account
.getSelfRecipientId());
368 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
369 if (givenName
!= null) {
370 builder
.withGivenName(givenName
);
372 if (familyName
!= null) {
373 builder
.withFamilyName(familyName
);
376 builder
.withAbout(about
);
378 if (aboutEmoji
!= null) {
379 builder
.withAboutEmoji(aboutEmoji
);
381 var newProfile
= builder
.build();
383 try (final var streamDetails
= avatar
== null
384 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
385 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
386 dependencies
.getAccountManager()
387 .setVersionedProfile(account
.getUuid(),
388 account
.getProfileKey(),
389 newProfile
.getInternalServiceName(),
390 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
391 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
396 if (avatar
!= null) {
397 if (avatar
.isPresent()) {
398 avatarStore
.storeProfileAvatar(getSelfAddress(),
399 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
401 avatarStore
.deleteProfileAvatar(getSelfAddress());
404 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
406 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
409 public void unregister() throws IOException
{
410 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
411 // If this is the master device, other users can't send messages to this number anymore.
412 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
413 dependencies
.getAccountManager().setGcmId(Optional
.absent());
415 account
.setRegistered(false);
418 public void deleteAccount() throws IOException
{
419 dependencies
.getAccountManager().deleteAccount();
421 account
.setRegistered(false);
424 public List
<Device
> getLinkedDevices() throws IOException
{
425 var devices
= dependencies
.getAccountManager().getDevices();
426 account
.setMultiDevice(devices
.size() > 1);
427 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
428 return devices
.stream().map(d
-> {
429 String deviceName
= d
.getName();
430 if (deviceName
!= null) {
432 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
433 } catch (IOException e
) {
434 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
437 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
438 }).collect(Collectors
.toList());
441 public void removeLinkedDevices(int deviceId
) throws IOException
{
442 dependencies
.getAccountManager().removeDevice(deviceId
);
443 var devices
= dependencies
.getAccountManager().getDevices();
444 account
.setMultiDevice(devices
.size() > 1);
447 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
448 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
450 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
453 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
454 var identityKeyPair
= getIdentityKeyPair();
455 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
457 dependencies
.getAccountManager()
458 .addDevice(deviceIdentifier
,
461 Optional
.of(account
.getProfileKey().serialize()),
463 account
.setMultiDevice(true);
466 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
467 if (!account
.isMasterDevice()) {
468 throw new RuntimeException("Only master device can set a PIN");
470 if (pin
.isPresent()) {
471 final var masterKey
= account
.getPinMasterKey() != null
472 ? account
.getPinMasterKey()
473 : KeyUtils
.createMasterKey();
475 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
477 account
.setRegistrationLockPin(pin
.get(), masterKey
);
480 pinHelper
.removeRegistrationLockPin();
482 account
.setRegistrationLockPin(null, null);
486 void refreshPreKeys() throws IOException
{
487 var oneTimePreKeys
= generatePreKeys();
488 final var identityKeyPair
= getIdentityKeyPair();
489 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
491 dependencies
.getAccountManager().setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
494 private List
<PreKeyRecord
> generatePreKeys() {
495 final var offset
= account
.getPreKeyIdOffset();
497 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
498 account
.addPreKeys(records
);
503 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
504 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
506 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
507 account
.addSignedPreKey(record);
512 public Profile
getRecipientProfile(
513 RecipientId recipientId
515 return getRecipientProfile(recipientId
, false);
518 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
520 Profile
getRecipientProfile(
521 RecipientId recipientId
, boolean force
523 var profile
= account
.getProfileStore().getProfile(recipientId
);
525 var now
= System
.currentTimeMillis();
526 // Profiles are cached for 24h before retrieving them again, unless forced
527 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
531 synchronized (pendingProfileRequest
) {
532 if (pendingProfileRequest
.contains(recipientId
)) {
535 pendingProfileRequest
.add(recipientId
);
537 final SignalServiceProfile encryptedProfile
;
539 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
541 synchronized (pendingProfileRequest
) {
542 pendingProfileRequest
.remove(recipientId
);
545 if (encryptedProfile
== null) {
549 profile
= decryptProfileIfKeyKnown(recipientId
, encryptedProfile
);
550 account
.getProfileStore().storeProfile(recipientId
, profile
);
555 private Profile
decryptProfileIfKeyKnown(
556 final RecipientId recipientId
, final SignalServiceProfile encryptedProfile
558 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
559 if (profileKey
== null) {
560 return new Profile(System
.currentTimeMillis(),
565 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
566 ProfileUtils
.getCapabilities(encryptedProfile
));
569 return decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
572 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
574 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
575 } catch (IOException e
) {
576 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
581 private ProfileAndCredential
retrieveProfileAndCredential(
582 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
583 ) throws IOException
{
584 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
585 final var profile
= profileAndCredential
.getProfile();
588 var newIdentity
= account
.getIdentityKeyStore()
589 .saveIdentity(recipientId
,
590 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
594 account
.getSessionStore().archiveSessions(recipientId
);
596 } catch (InvalidKeyException ignored
) {
597 logger
.warn("Got invalid identity key in profile for {}",
598 resolveSignalServiceAddress(recipientId
).getIdentifier());
600 return profileAndCredential
;
603 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
604 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
605 if (profileKeyCredential
!= null) {
606 return profileKeyCredential
;
609 ProfileAndCredential profileAndCredential
;
611 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
612 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
613 } catch (IOException e
) {
614 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
618 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
619 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
621 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
622 if (profileKey
!= null) {
623 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
625 profileAndCredential
.getProfile());
626 account
.getProfileStore().storeProfile(recipientId
, profile
);
629 return profileKeyCredential
;
632 private Profile
decryptProfileAndDownloadAvatar(
633 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
635 if (encryptedProfile
.getAvatar() != null) {
636 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
639 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
642 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
643 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
644 if (streamDetails
== null) {
645 return Optional
.absent();
648 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
651 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
652 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
653 if (streamDetails
== null) {
654 return Optional
.absent();
657 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
660 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
661 var g
= getGroup(groupId
);
663 throw new GroupNotFoundException(groupId
);
665 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
666 throw new NotAGroupMemberException(groupId
, g
.getTitle());
671 public List
<GroupInfo
> getGroups() {
672 return account
.getGroupStore().getGroups();
675 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
676 String messageText
, List
<String
> attachments
, GroupId groupId
677 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
678 final var messageBuilder
= createMessageBuilder().withBody(messageText
);
679 if (attachments
!= null) {
680 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
683 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
686 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
687 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
688 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
689 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
690 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
692 resolveSignalServiceAddress(targetAuthorRecipientId
),
693 targetSentTimestamp
);
694 final var messageBuilder
= createMessageBuilder().withReaction(reaction
);
696 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
699 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(
700 GroupId groupId
, Set
<String
> groupAdmins
701 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, InvalidNumberException
, LastGroupAdminException
{
702 var group
= getGroupForUpdating(groupId
);
703 if (group
instanceof GroupInfoV1
) {
704 return quitGroupV1((GroupInfoV1
) group
);
707 final var newAdmins
= getRecipientIds(groupAdmins
);
709 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
710 } catch (ConflictException e
) {
711 // Detected conflicting update, refreshing group and trying again
712 group
= getGroup(groupId
, true);
713 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
717 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV1(final GroupInfoV1 groupInfoV1
) throws IOException
{
718 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
719 .withId(groupInfoV1
.getGroupId().serialize())
722 var messageBuilder
= createMessageBuilder().asGroupMessage(group
);
723 groupInfoV1
.removeMember(account
.getSelfRecipientId());
724 account
.getGroupStore().updateGroup(groupInfoV1
);
725 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
726 groupInfoV1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
729 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV2(
730 final GroupInfoV2 groupInfoV2
, final Set
<RecipientId
> newAdmins
731 ) throws LastGroupAdminException
, IOException
{
732 final var currentAdmins
= groupInfoV2
.getAdminMembers();
733 newAdmins
.removeAll(currentAdmins
);
734 newAdmins
.retainAll(groupInfoV2
.getMembers());
735 if (currentAdmins
.contains(getSelfRecipientId())
736 && currentAdmins
.size() == 1
737 && groupInfoV2
.getMembers().size() > 1
738 && newAdmins
.size() == 0) {
739 // Last admin can't leave the group, unless she's also the last member
740 throw new LastGroupAdminException(groupInfoV2
.getGroupId(), groupInfoV2
.getTitle());
742 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
743 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
744 var messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
745 account
.getGroupStore().updateGroup(groupInfoV2
);
746 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
747 groupInfoV2
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
750 public void deleteGroup(GroupId groupId
) throws IOException
{
751 account
.getGroupStore().deleteGroup(groupId
);
752 avatarStore
.deleteGroupAvatar(groupId
);
755 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
756 String name
, List
<String
> members
, File avatarFile
757 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
758 return createGroup(name
, members
== null ?
null : getRecipientIds(members
), avatarFile
);
761 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
762 String name
, Set
<RecipientId
> members
, File avatarFile
763 ) throws IOException
, AttachmentInvalidException
{
764 final var selfRecipientId
= account
.getSelfRecipientId();
765 if (members
!= null && members
.contains(selfRecipientId
)) {
766 members
= new HashSet
<>(members
);
767 members
.remove(selfRecipientId
);
770 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
771 members
== null ? Set
.of() : members
,
774 SignalServiceDataMessage
.Builder messageBuilder
;
775 if (gv2Pair
== null) {
776 // Failed to create v2 group, creating v1 group instead
777 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
778 gv1
.addMembers(List
.of(selfRecipientId
));
779 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
780 return new Pair
<>(gv1
.getGroupId(), result
.second());
783 final var gv2
= gv2Pair
.first();
784 final var decryptedGroup
= gv2Pair
.second();
786 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
787 if (avatarFile
!= null) {
788 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
789 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
791 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
792 account
.getGroupStore().updateGroup(gv2
);
794 final var result
= sendHelper
.sendGroupMessage(messageBuilder
.build(),
795 gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
796 return new Pair
<>(gv2
.getGroupId(), result
.second());
799 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
803 List
<String
> members
,
804 List
<String
> removeMembers
,
806 List
<String
> removeAdmins
,
807 boolean resetGroupLink
,
808 GroupLinkState groupLinkState
,
809 GroupPermission addMemberPermission
,
810 GroupPermission editDetailsPermission
,
812 Integer expirationTimer
,
813 Boolean isAnnouncementGroup
814 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
815 return updateGroup(groupId
,
818 members
== null ?
null : getRecipientIds(members
),
819 removeMembers
== null ?
null : getRecipientIds(removeMembers
),
820 admins
== null ?
null : getRecipientIds(admins
),
821 removeAdmins
== null ?
null : getRecipientIds(removeAdmins
),
825 editDetailsPermission
,
828 isAnnouncementGroup
);
831 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
832 final GroupId groupId
,
834 final String description
,
835 final Set
<RecipientId
> members
,
836 final Set
<RecipientId
> removeMembers
,
837 final Set
<RecipientId
> admins
,
838 final Set
<RecipientId
> removeAdmins
,
839 final boolean resetGroupLink
,
840 final GroupLinkState groupLinkState
,
841 final GroupPermission addMemberPermission
,
842 final GroupPermission editDetailsPermission
,
843 final File avatarFile
,
844 final Integer expirationTimer
,
845 final Boolean isAnnouncementGroup
846 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
847 var group
= getGroupForUpdating(groupId
);
849 if (group
instanceof GroupInfoV2
) {
851 return updateGroupV2((GroupInfoV2
) group
,
861 editDetailsPermission
,
864 isAnnouncementGroup
);
865 } catch (ConflictException e
) {
866 // Detected conflicting update, refreshing group and trying again
867 group
= getGroup(groupId
, true);
868 return updateGroupV2((GroupInfoV2
) group
,
878 editDetailsPermission
,
881 isAnnouncementGroup
);
885 final var gv1
= (GroupInfoV1
) group
;
886 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
887 if (expirationTimer
!= null) {
888 setExpirationTimer(gv1
, expirationTimer
);
893 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
894 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
895 ) throws IOException
, AttachmentInvalidException
{
896 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
897 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
899 account
.getGroupStore().updateGroup(gv1
);
901 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
902 gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
905 private void updateGroupV1Details(
906 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
907 ) throws IOException
{
912 if (members
!= null) {
913 final var newMemberAddresses
= members
.stream()
914 .filter(member
-> !g
.isMember(member
))
915 .map(this::resolveSignalServiceAddress
)
916 .collect(Collectors
.toList());
917 final var newE164Members
= new HashSet
<String
>();
918 for (var member
: newMemberAddresses
) {
919 if (!member
.getNumber().isPresent()) {
922 newE164Members
.add(member
.getNumber().get());
925 final var registeredUsers
= getRegisteredUsers(newE164Members
);
926 if (registeredUsers
.size() != newE164Members
.size()) {
927 // Some of the new members are not registered on Signal
928 newE164Members
.removeAll(registeredUsers
.keySet());
929 throw new IOException("Failed to add members "
930 + String
.join(", ", newE164Members
)
931 + " to group: Not registered on Signal");
934 g
.addMembers(members
);
937 if (avatarFile
!= null) {
938 avatarStore
.storeGroupAvatar(g
.getGroupId(),
939 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
943 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
944 final GroupInfoV2 group
,
946 final String description
,
947 final Set
<RecipientId
> members
,
948 final Set
<RecipientId
> removeMembers
,
949 final Set
<RecipientId
> admins
,
950 final Set
<RecipientId
> removeAdmins
,
951 final boolean resetGroupLink
,
952 final GroupLinkState groupLinkState
,
953 final GroupPermission addMemberPermission
,
954 final GroupPermission editDetailsPermission
,
955 final File avatarFile
,
956 final Integer expirationTimer
,
957 final Boolean isAnnouncementGroup
958 ) throws IOException
{
959 Pair
<Long
, List
<SendMessageResult
>> result
= null;
960 if (group
.isPendingMember(account
.getSelfRecipientId())) {
961 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
962 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
965 if (members
!= null) {
966 final var newMembers
= new HashSet
<>(members
);
967 newMembers
.removeAll(group
.getMembers());
968 if (newMembers
.size() > 0) {
969 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
970 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
974 if (removeMembers
!= null) {
975 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
976 existingRemoveMembers
.retainAll(group
.getMembers());
977 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
978 if (existingRemoveMembers
.size() > 0) {
979 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
980 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
983 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
984 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
985 if (pendingRemoveMembers
.size() > 0) {
986 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
987 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
991 if (admins
!= null) {
992 final var newAdmins
= new HashSet
<>(admins
);
993 newAdmins
.retainAll(group
.getMembers());
994 newAdmins
.removeAll(group
.getAdminMembers());
995 if (newAdmins
.size() > 0) {
996 for (var admin
: newAdmins
) {
997 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
998 result
= sendUpdateGroupV2Message(group
,
999 groupGroupChangePair
.first(),
1000 groupGroupChangePair
.second());
1005 if (removeAdmins
!= null) {
1006 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1007 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1008 if (existingRemoveAdmins
.size() > 0) {
1009 for (var admin
: existingRemoveAdmins
) {
1010 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1011 result
= sendUpdateGroupV2Message(group
,
1012 groupGroupChangePair
.first(),
1013 groupGroupChangePair
.second());
1018 if (resetGroupLink
) {
1019 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1020 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1023 if (groupLinkState
!= null) {
1024 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1025 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1028 if (addMemberPermission
!= null) {
1029 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1030 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1033 if (editDetailsPermission
!= null) {
1034 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1035 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1038 if (expirationTimer
!= null) {
1039 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1040 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1043 if (isAnnouncementGroup
!= null) {
1044 var groupGroupChangePair
= groupV2Helper
.setIsAnnouncementGroup(group
, isAnnouncementGroup
);
1045 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1048 if (name
!= null || description
!= null || avatarFile
!= null) {
1049 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1050 if (avatarFile
!= null) {
1051 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1052 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1054 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1060 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1061 GroupInviteLinkUrl inviteLinkUrl
1062 ) throws IOException
, GroupLinkNotActiveException
{
1063 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1064 inviteLinkUrl
.getPassword());
1065 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1066 inviteLinkUrl
.getPassword(),
1068 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1069 groupJoinInfo
.getRevision() + 1,
1070 groupChange
.toByteArray());
1072 if (group
.getGroup() == null) {
1073 // Only requested member, can't send update to group members
1074 return new Pair
<>(group
.getGroupId(), List
.of());
1077 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1079 return new Pair
<>(group
.getGroupId(), result
.second());
1082 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1083 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1084 ) throws IOException
{
1085 final var selfRecipientId
= account
.getSelfRecipientId();
1086 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1087 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1088 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1090 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1091 account
.getGroupStore().updateGroup(group
);
1092 return sendHelper
.sendGroupMessage(messageBuilder
.build(), members
);
1095 private static int currentTimeDays() {
1096 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1099 private GroupsV2AuthorizationString
getGroupAuthForToday(
1100 final GroupSecretParams groupSecretParams
1101 ) throws IOException
{
1102 final var today
= currentTimeDays();
1103 // Returns credentials for the next 7 days
1104 final var credentials
= dependencies
.getGroupsV2Api().getCredentials(today
);
1105 // TODO cache credentials until they expire
1106 var authCredentialResponse
= credentials
.get(today
);
1108 return dependencies
.getGroupsV2Api()
1109 .getGroupsV2AuthorizationString(account
.getUuid(),
1112 authCredentialResponse
);
1113 } catch (VerificationFailedException e
) {
1114 throw new IOException(e
);
1118 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1119 GroupIdV1 groupId
, SignalServiceAddress recipient
1120 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1122 var group
= getGroupForUpdating(groupId
);
1123 if (!(group
instanceof GroupInfoV1
)) {
1124 throw new IOException("Received an invalid group request for a v2 group!");
1126 g
= (GroupInfoV1
) group
;
1128 final var recipientId
= resolveRecipient(recipient
);
1129 if (!g
.isMember(recipientId
)) {
1130 throw new NotAGroupMemberException(groupId
, g
.name
);
1133 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1135 // Send group message only to the recipient who requested it
1136 return sendHelper
.sendGroupMessage(messageBuilder
.build(), Set
.of(recipientId
));
1139 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1140 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1141 .withId(g
.getGroupId().serialize())
1143 .withMembers(g
.getMembers()
1145 .map(this::resolveSignalServiceAddress
)
1146 .collect(Collectors
.toList()));
1149 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1150 if (attachment
.isPresent()) {
1151 group
.withAvatar(attachment
.get());
1153 } catch (IOException e
) {
1154 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1157 return createMessageBuilder().asGroupMessage(group
.build()).withExpiration(g
.getMessageExpirationTime());
1160 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1161 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1162 .withRevision(g
.getGroup().getRevision())
1163 .withSignedGroupChange(signedGroupChange
);
1164 return createMessageBuilder().asGroupMessage(group
.build()).withExpiration(g
.getMessageExpirationTime());
1167 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1168 GroupIdV1 groupId
, SignalServiceAddress recipient
1169 ) throws IOException
{
1170 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1172 var messageBuilder
= createMessageBuilder().asGroupMessage(group
.build());
1174 // Send group info request message to the recipient who sent us a message with this groupId
1175 return sendHelper
.sendGroupMessage(messageBuilder
.build(), Set
.of(resolveRecipient(recipient
)));
1179 SignalServiceAddress remoteAddress
, long messageId
1180 ) throws IOException
, UntrustedIdentityException
{
1181 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1183 System
.currentTimeMillis());
1185 sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(remoteAddress
));
1188 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1189 String messageText
, List
<String
> attachments
, List
<String
> recipients
1190 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1191 final var messageBuilder
= createMessageBuilder().withBody(messageText
);
1192 if (attachments
!= null) {
1193 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1195 // Upload attachments here, so we only upload once even for multiple recipients
1196 var messageSender
= dependencies
.getMessageSender();
1197 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1198 for (var attachment
: attachmentStreams
) {
1199 if (attachment
.isStream()) {
1200 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1201 } else if (attachment
.isPointer()) {
1202 attachmentPointers
.add(attachment
.asPointer());
1206 messageBuilder
.withAttachments(attachmentPointers
);
1208 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1211 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1212 String messageText
, List
<String
> attachments
1213 ) throws IOException
, AttachmentInvalidException
{
1214 final var messageBuilder
= createMessageBuilder().withBody(messageText
);
1215 if (attachments
!= null) {
1216 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1218 return sendHelper
.sendSelfMessage(messageBuilder
);
1221 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1222 long targetSentTimestamp
, List
<String
> recipients
1223 ) throws IOException
, InvalidNumberException
{
1224 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1225 final var messageBuilder
= createMessageBuilder().withRemoteDelete(delete
);
1226 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1229 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1230 long targetSentTimestamp
, GroupId groupId
1231 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1232 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1233 final var messageBuilder
= createMessageBuilder().withRemoteDelete(delete
);
1234 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1237 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1238 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1239 ) throws IOException
, InvalidNumberException
{
1240 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1241 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1243 resolveSignalServiceAddress(targetAuthorRecipientId
),
1244 targetSentTimestamp
);
1245 final var messageBuilder
= createMessageBuilder().withReaction(reaction
);
1246 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1249 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1250 var messageBuilder
= createMessageBuilder().asEndSessionMessage();
1252 final var recipientIds
= getRecipientIds(recipients
);
1254 return sendHelper
.sendMessage(messageBuilder
, recipientIds
);
1256 for (var recipientId
: recipientIds
) {
1257 handleEndSession(recipientId
);
1262 void renewSession(RecipientId recipientId
) throws IOException
{
1263 account
.getSessionStore().archiveSessions(recipientId
);
1264 if (!recipientId
.equals(getSelfRecipientId())) {
1265 sendHelper
.sendNullMessage(recipientId
);
1269 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1270 if (!account
.isMasterDevice()) {
1271 throw new NotMasterDeviceException();
1273 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1274 var contact
= account
.getContactStore().getContact(recipientId
);
1275 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1276 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1279 public void setContactBlocked(
1280 String number
, boolean blocked
1281 ) throws InvalidNumberException
, NotMasterDeviceException
{
1282 if (!account
.isMasterDevice()) {
1283 throw new NotMasterDeviceException();
1285 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1288 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1289 var contact
= account
.getContactStore().getContact(recipientId
);
1290 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1291 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1294 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1295 var group
= getGroup(groupId
);
1296 if (group
== null) {
1297 throw new GroupNotFoundException(groupId
);
1300 group
.setBlocked(blocked
);
1301 account
.getGroupStore().updateGroup(group
);
1304 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1305 var contact
= account
.getContactStore().getContact(recipientId
);
1306 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1309 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1310 account
.getContactStore()
1311 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1314 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1315 final var messageBuilder
= createMessageBuilder().asExpirationUpdate();
1316 sendHelper
.sendMessage(messageBuilder
, Set
.of(recipientId
));
1320 * Change the expiration timer for a contact
1322 public void setExpirationTimer(
1323 String number
, int messageExpirationTimer
1324 ) throws IOException
, InvalidNumberException
{
1325 var recipientId
= canonicalizeAndResolveRecipient(number
);
1326 setExpirationTimer(recipientId
, messageExpirationTimer
);
1327 sendExpirationTimerUpdate(recipientId
);
1331 * Change the expiration timer for a group
1333 private void setExpirationTimer(
1334 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1335 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1336 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1337 account
.getGroupStore().updateGroup(groupInfoV1
);
1338 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1341 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1342 final var messageBuilder
= createMessageBuilder().asExpirationUpdate();
1343 sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1347 * Upload the sticker pack from path.
1349 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1350 * @return if successful, returns the URL to install the sticker pack in the signal app
1352 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1353 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1355 var messageSender
= dependencies
.getMessageSender();
1357 var packKey
= KeyUtils
.createStickerUploadKey();
1358 var packIdString
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1359 var packId
= StickerPackId
.deserialize(Hex
.fromStringCondensed(packIdString
));
1361 var sticker
= new Sticker(packId
, packKey
);
1362 account
.getStickerStore().updateSticker(sticker
);
1365 return new URI("https",
1369 + URLEncoder
.encode(Hex
.toStringCondensed(packId
.serialize()), StandardCharsets
.UTF_8
)
1371 + URLEncoder
.encode(Hex
.toStringCondensed(packKey
), StandardCharsets
.UTF_8
)).toString();
1372 } catch (URISyntaxException e
) {
1373 throw new AssertionError(e
);
1377 public void requestAllSyncData() throws IOException
{
1378 requestSyncGroups();
1379 requestSyncContacts();
1380 requestSyncBlocked();
1381 requestSyncConfiguration();
1385 private void requestSyncGroups() throws IOException
{
1386 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1387 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1389 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1390 sendHelper
.sendSyncMessage(message
);
1393 private void requestSyncContacts() throws IOException
{
1394 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1395 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1397 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1398 sendHelper
.sendSyncMessage(message
);
1401 private void requestSyncBlocked() throws IOException
{
1402 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1403 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1405 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1406 sendHelper
.sendSyncMessage(message
);
1409 private void requestSyncConfiguration() throws IOException
{
1410 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1411 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1413 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1414 sendHelper
.sendSyncMessage(message
);
1417 private void requestSyncKeys() throws IOException
{
1418 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1419 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1421 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1422 sendHelper
.sendSyncMessage(message
);
1425 private byte[] getSenderCertificate() {
1428 if (account
.isPhoneNumberShared()) {
1429 certificate
= dependencies
.getAccountManager().getSenderCertificate();
1431 certificate
= dependencies
.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
1433 } catch (IOException e
) {
1434 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1437 // TODO cache for a day
1441 private Set
<RecipientId
> getRecipientIds(Collection
<String
> numbers
) throws InvalidNumberException
{
1442 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1443 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1445 for (var number
: numbers
) {
1446 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1447 if (resolvedAddress
.getUuid().isPresent()) {
1448 signalServiceAddresses
.add(resolvedAddress
);
1450 addressesMissingUuid
.add(resolvedAddress
);
1454 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1455 .map(a
-> a
.getNumber().get())
1456 .collect(Collectors
.toSet());
1457 Map
<String
, UUID
> registeredUsers
;
1459 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1460 } catch (IOException e
) {
1461 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1462 registeredUsers
= Map
.of();
1465 for (var address
: addressesMissingUuid
) {
1466 final var number
= address
.getNumber().get();
1467 if (registeredUsers
.containsKey(number
)) {
1468 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1469 registeredUsers
.get(number
),
1471 signalServiceAddresses
.add(newAddress
);
1473 signalServiceAddresses
.add(address
);
1477 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1480 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1481 final var address
= resolveSignalServiceAddress(recipientId
);
1482 if (!address
.getNumber().isPresent()) {
1485 final var number
= address
.getNumber().get();
1486 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1487 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1490 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1492 return dependencies
.getAccountManager()
1493 .getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1495 serviceEnvironmentConfig
.getCdsMrenclave());
1496 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1497 throw new IOException(e
);
1501 public void sendTypingMessage(
1502 TypingAction action
, Set
<String
> recipients
1503 ) throws IOException
, UntrustedIdentityException
, InvalidNumberException
{
1504 final var timestamp
= System
.currentTimeMillis();
1505 var message
= new SignalServiceTypingMessage(action
.toSignalService(), timestamp
, Optional
.absent());
1506 sendHelper
.sendTypingMessage(message
, getRecipientIds(recipients
));
1509 public void sendGroupTypingMessage(
1510 TypingAction action
, GroupId groupId
1511 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1512 final var timestamp
= System
.currentTimeMillis();
1513 final var message
= new SignalServiceTypingMessage(action
.toSignalService(),
1515 Optional
.of(groupId
.serialize()));
1516 sendHelper
.sendGroupTypingMessage(message
, groupId
);
1519 private SignalServiceDataMessage
.Builder
createMessageBuilder() {
1520 final var timestamp
= System
.currentTimeMillis();
1522 var messageBuilder
= SignalServiceDataMessage
.newBuilder();
1523 messageBuilder
.withTimestamp(timestamp
);
1524 return messageBuilder
;
1527 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
, InvalidMessageStructureException
{
1528 return dependencies
.getCipher().decrypt(envelope
);
1531 private void handleEndSession(RecipientId recipientId
) {
1532 account
.getSessionStore().deleteAllSessions(recipientId
);
1535 private List
<HandleAction
> handleSignalServiceDataMessage(
1536 SignalServiceDataMessage message
,
1538 SignalServiceAddress source
,
1539 SignalServiceAddress destination
,
1540 boolean ignoreAttachments
1542 var actions
= new ArrayList
<HandleAction
>();
1543 if (message
.getGroupContext().isPresent()) {
1544 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1545 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1546 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1547 var group
= getGroup(groupId
);
1548 if (group
== null || group
instanceof GroupInfoV1
) {
1549 var groupV1
= (GroupInfoV1
) group
;
1550 switch (groupInfo
.getType()) {
1552 if (groupV1
== null) {
1553 groupV1
= new GroupInfoV1(groupId
);
1556 if (groupInfo
.getAvatar().isPresent()) {
1557 var avatar
= groupInfo
.getAvatar().get();
1558 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1561 if (groupInfo
.getName().isPresent()) {
1562 groupV1
.name
= groupInfo
.getName().get();
1565 if (groupInfo
.getMembers().isPresent()) {
1566 groupV1
.addMembers(groupInfo
.getMembers()
1569 .map(this::resolveRecipient
)
1570 .collect(Collectors
.toSet()));
1573 account
.getGroupStore().updateGroup(groupV1
);
1577 if (groupV1
== null && !isSync
) {
1578 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1582 if (groupV1
!= null) {
1583 groupV1
.removeMember(resolveRecipient(source
));
1584 account
.getGroupStore().updateGroup(groupV1
);
1589 if (groupV1
!= null && !isSync
) {
1590 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1595 // Received a group v1 message for a v2 group
1598 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1599 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1600 final var groupMasterKey
= groupContext
.getMasterKey();
1602 getOrMigrateGroup(groupMasterKey
,
1603 groupContext
.getRevision(),
1604 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1608 final var conversationPartnerAddress
= isSync ? destination
: source
;
1609 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1610 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1612 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1613 if (message
.getGroupContext().isPresent()) {
1614 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1615 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1616 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1617 if (group
!= null) {
1618 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1619 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1620 account
.getGroupStore().updateGroup(group
);
1623 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1624 // disappearing message timer already stored in the DecryptedGroup
1626 } else if (conversationPartnerAddress
!= null) {
1627 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1630 if (!ignoreAttachments
) {
1631 if (message
.getAttachments().isPresent()) {
1632 for (var attachment
: message
.getAttachments().get()) {
1633 downloadAttachment(attachment
);
1636 if (message
.getSharedContacts().isPresent()) {
1637 for (var contact
: message
.getSharedContacts().get()) {
1638 if (contact
.getAvatar().isPresent()) {
1639 downloadAttachment(contact
.getAvatar().get().getAttachment());
1644 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1645 final ProfileKey profileKey
;
1647 profileKey
= new ProfileKey(message
.getProfileKey().get());
1648 } catch (InvalidInputException e
) {
1649 throw new AssertionError(e
);
1651 if (source
.matches(account
.getSelfAddress())) {
1652 this.account
.setProfileKey(profileKey
);
1654 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1656 if (message
.getPreviews().isPresent()) {
1657 final var previews
= message
.getPreviews().get();
1658 for (var preview
: previews
) {
1659 if (preview
.getImage().isPresent()) {
1660 downloadAttachment(preview
.getImage().get());
1664 if (message
.getQuote().isPresent()) {
1665 final var quote
= message
.getQuote().get();
1667 for (var quotedAttachment
: quote
.getAttachments()) {
1668 final var thumbnail
= quotedAttachment
.getThumbnail();
1669 if (thumbnail
!= null) {
1670 downloadAttachment(thumbnail
);
1674 if (message
.getSticker().isPresent()) {
1675 final var messageSticker
= message
.getSticker().get();
1676 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1677 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1678 if (sticker
== null) {
1679 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1680 account
.getStickerStore().updateSticker(sticker
);
1682 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, messageSticker
.getPackKey()));
1687 private GroupInfoV2
getOrMigrateGroup(
1688 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1690 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1692 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1693 var groupInfo
= getGroup(groupId
);
1694 final GroupInfoV2 groupInfoV2
;
1695 if (groupInfo
instanceof GroupInfoV1
) {
1696 // Received a v2 group message for a v1 group, we need to locally migrate the group
1697 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1698 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1699 logger
.info("Locally migrated group {} to group v2, id: {}",
1700 groupInfo
.getGroupId().toBase64(),
1701 groupInfoV2
.getGroupId().toBase64());
1702 } else if (groupInfo
instanceof GroupInfoV2
) {
1703 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1705 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1708 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1709 DecryptedGroup group
= null;
1710 if (signedGroupChange
!= null
1711 && groupInfoV2
.getGroup() != null
1712 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1713 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1717 if (group
== null) {
1718 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1720 if (group
!= null) {
1721 storeProfileKeysFromMembers(group
);
1722 final var avatar
= group
.getAvatar();
1723 if (avatar
!= null && !avatar
.isEmpty()) {
1724 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1727 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1728 account
.getGroupStore().updateGroup(groupInfoV2
);
1734 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1735 for (var member
: group
.getMembersList()) {
1736 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1737 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1739 account
.getProfileStore()
1740 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1741 } catch (InvalidInputException ignored
) {
1746 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1747 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1748 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1749 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1750 if (actions
!= null) {
1751 queuedActions
.addAll(actions
);
1754 for (var action
: queuedActions
) {
1756 action
.execute(this);
1757 } catch (Throwable e
) {
1758 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1759 Thread
.currentThread().interrupt();
1761 logger
.warn("Message action failed.", e
);
1766 private List
<HandleAction
> retryFailedReceivedMessage(
1767 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1769 var envelope
= cachedMessage
.loadEnvelope();
1770 if (envelope
== null) {
1773 SignalServiceContent content
= null;
1774 List
<HandleAction
> actions
= null;
1775 if (!envelope
.isReceipt()) {
1777 content
= decryptMessage(envelope
);
1778 } catch (ProtocolUntrustedIdentityException e
) {
1779 if (!envelope
.hasSource()) {
1780 final var identifier
= e
.getSender();
1781 final var recipientId
= resolveRecipient(identifier
);
1783 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1784 } catch (IOException ioException
) {
1785 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1789 } catch (Exception er
) {
1790 // All other errors are not recoverable, so delete the cached message
1791 cachedMessage
.delete();
1794 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1796 handler
.handleMessage(envelope
, content
, null);
1797 cachedMessage
.delete();
1801 public void receiveMessages(
1804 boolean returnOnTimeout
,
1805 boolean ignoreAttachments
,
1806 ReceiveMessageHandler handler
1807 ) throws IOException
, InterruptedException
{
1808 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1810 Set
<HandleAction
> queuedActions
= null;
1812 final var signalWebSocket
= dependencies
.getSignalWebSocket();
1813 signalWebSocket
.connect();
1815 var hasCaughtUpWithOldMessages
= false;
1817 while (!Thread
.interrupted()) {
1818 SignalServiceEnvelope envelope
;
1819 SignalServiceContent content
= null;
1820 Exception exception
= null;
1821 final CachedMessage
[] cachedMessage
= {null};
1822 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
1823 logger
.debug("Checking for new message from server");
1825 var result
= signalWebSocket
.readOrEmpty(unit
.toMillis(timeout
), envelope1
-> {
1826 final var recipientId
= envelope1
.hasSource()
1827 ?
resolveRecipient(envelope1
.getSourceIdentifier())
1829 // store message on disk, before acknowledging receipt to the server
1830 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1832 logger
.debug("New message received from server");
1833 if (result
.isPresent()) {
1834 envelope
= result
.get();
1836 // Received indicator that server queue is empty
1837 hasCaughtUpWithOldMessages
= true;
1839 if (queuedActions
!= null) {
1840 for (var action
: queuedActions
) {
1842 action
.execute(this);
1843 } catch (Throwable e
) {
1844 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1845 Thread
.currentThread().interrupt();
1847 logger
.warn("Message action failed.", e
);
1850 queuedActions
.clear();
1851 queuedActions
= null;
1854 // Continue to wait another timeout for new messages
1857 } catch (AssertionError e
) {
1858 if (e
.getCause() instanceof InterruptedException
) {
1859 throw (InterruptedException
) e
.getCause();
1863 } catch (WebSocketUnavailableException e
) {
1864 logger
.debug("Pipe unexpectedly unavailable, connecting");
1865 signalWebSocket
.connect();
1867 } catch (TimeoutException e
) {
1868 if (returnOnTimeout
) return;
1872 if (envelope
.hasSource()) {
1873 // Store uuid if we don't have it already
1874 // address/uuid in envelope is sent by server
1875 resolveRecipientTrusted(envelope
.getSourceAddress());
1877 if (!envelope
.isReceipt()) {
1879 content
= decryptMessage(envelope
);
1880 } catch (Exception e
) {
1883 if (!envelope
.hasSource() && content
!= null) {
1884 // Store uuid if we don't have it already
1885 // address/uuid is validated by unidentified sender certificate
1886 resolveRecipientTrusted(content
.getSender());
1888 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1889 if (exception
instanceof ProtocolInvalidMessageException
) {
1890 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
1891 logger
.debug("Received invalid message, queuing renew session action.");
1892 actions
.add(new RenewSessionAction(sender
));
1894 if (hasCaughtUpWithOldMessages
) {
1895 for (var action
: actions
) {
1897 action
.execute(this);
1898 } catch (Throwable e
) {
1899 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1900 Thread
.currentThread().interrupt();
1902 logger
.warn("Message action failed.", e
);
1906 if (queuedActions
== null) {
1907 queuedActions
= new HashSet
<>();
1909 queuedActions
.addAll(actions
);
1912 final var notAllowedToSendToGroup
= isNotAllowedToSendToGroup(envelope
, content
);
1913 if (isMessageBlocked(envelope
, content
)) {
1914 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
1915 } else if (notAllowedToSendToGroup
) {
1916 logger
.info("Ignoring a group message from an unauthorized sender (no member or admin): {} {}",
1917 (envelope
.hasSource() ? envelope
.getSourceAddress() : content
.getSender()).getIdentifier(),
1918 envelope
.getTimestamp());
1920 handler
.handleMessage(envelope
, content
, exception
);
1922 if (cachedMessage
[0] != null) {
1923 if (exception
instanceof ProtocolUntrustedIdentityException
) {
1924 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
1925 final var recipientId
= resolveRecipient(identifier
);
1926 queuedActions
.add(new RetrieveProfileAction(recipientId
));
1927 if (!envelope
.hasSource()) {
1929 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
1930 } catch (IOException ioException
) {
1931 logger
.warn("Failed to move cached message to recipient folder: {}",
1932 ioException
.getMessage());
1936 cachedMessage
[0].delete();
1942 private boolean isMessageBlocked(
1943 SignalServiceEnvelope envelope
, SignalServiceContent content
1945 SignalServiceAddress source
;
1946 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
1947 source
= envelope
.getSourceAddress();
1948 } else if (content
!= null) {
1949 source
= content
.getSender();
1953 final var recipientId
= resolveRecipient(source
);
1954 if (isContactBlocked(recipientId
)) {
1958 if (content
!= null && content
.getDataMessage().isPresent()) {
1959 var message
= content
.getDataMessage().get();
1960 if (message
.getGroupContext().isPresent()) {
1961 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
1962 var group
= getGroup(groupId
);
1963 if (group
!= null && group
.isBlocked()) {
1971 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
1972 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
1973 return isContactBlocked(recipientId
);
1976 private boolean isContactBlocked(final RecipientId recipientId
) {
1977 var sourceContact
= account
.getContactStore().getContact(recipientId
);
1978 return sourceContact
!= null && sourceContact
.isBlocked();
1981 private boolean isNotAllowedToSendToGroup(
1982 SignalServiceEnvelope envelope
, SignalServiceContent content
1984 SignalServiceAddress source
;
1985 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
1986 source
= envelope
.getSourceAddress();
1987 } else if (content
!= null) {
1988 source
= content
.getSender();
1993 if (content
== null || !content
.getDataMessage().isPresent()) {
1997 var message
= content
.getDataMessage().get();
1998 if (!message
.getGroupContext().isPresent()) {
2002 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2003 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2004 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2009 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2010 var group
= getGroup(groupId
);
2011 if (group
== null) {
2015 final var recipientId
= resolveRecipient(source
);
2016 return !group
.isMember(recipientId
) || (
2017 group
.isAnnouncementGroup() && !group
.isAdmin(recipientId
)
2021 private List
<HandleAction
> handleMessage(
2022 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2024 var actions
= new ArrayList
<HandleAction
>();
2025 if (content
!= null) {
2026 final SignalServiceAddress sender
;
2027 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2028 sender
= envelope
.getSourceAddress();
2030 sender
= content
.getSender();
2033 if (content
.getDataMessage().isPresent()) {
2034 var message
= content
.getDataMessage().get();
2036 if (content
.isNeedsReceipt()) {
2037 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2040 actions
.addAll(handleSignalServiceDataMessage(message
,
2043 account
.getSelfAddress(),
2044 ignoreAttachments
));
2046 if (content
.getSyncMessage().isPresent()) {
2047 account
.setMultiDevice(true);
2048 var syncMessage
= content
.getSyncMessage().get();
2049 if (syncMessage
.getSent().isPresent()) {
2050 var message
= syncMessage
.getSent().get();
2051 final var destination
= message
.getDestination().orNull();
2052 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2056 ignoreAttachments
));
2058 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2059 var rm
= syncMessage
.getRequest().get();
2060 if (rm
.isContactsRequest()) {
2061 actions
.add(SendSyncContactsAction
.create());
2063 if (rm
.isGroupsRequest()) {
2064 actions
.add(SendSyncGroupsAction
.create());
2066 if (rm
.isBlockedListRequest()) {
2067 actions
.add(SendSyncBlockedListAction
.create());
2069 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2071 if (syncMessage
.getGroups().isPresent()) {
2072 File tmpFile
= null;
2074 tmpFile
= IOUtils
.createTempFile();
2075 final var groupsMessage
= syncMessage
.getGroups().get();
2076 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2077 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2082 } catch (IOException e
) {
2083 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2089 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2090 if (syncGroup
!= null) {
2091 if (g
.getName().isPresent()) {
2092 syncGroup
.name
= g
.getName().get();
2094 syncGroup
.addMembers(g
.getMembers()
2096 .map(this::resolveRecipient
)
2097 .collect(Collectors
.toSet()));
2098 if (!g
.isActive()) {
2099 syncGroup
.removeMember(account
.getSelfRecipientId());
2101 // Add ourself to the member set as it's marked as active
2102 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2104 syncGroup
.blocked
= g
.isBlocked();
2105 if (g
.getColor().isPresent()) {
2106 syncGroup
.color
= g
.getColor().get();
2109 if (g
.getAvatar().isPresent()) {
2110 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2112 syncGroup
.archived
= g
.isArchived();
2113 account
.getGroupStore().updateGroup(syncGroup
);
2117 } catch (Exception e
) {
2118 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2122 if (tmpFile
!= null) {
2124 Files
.delete(tmpFile
.toPath());
2125 } catch (IOException e
) {
2126 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2133 if (syncMessage
.getBlockedList().isPresent()) {
2134 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2135 for (var address
: blockedListMessage
.getAddresses()) {
2136 setContactBlocked(resolveRecipient(address
), true);
2138 for (var groupId
: blockedListMessage
.getGroupIds()
2140 .map(GroupId
::unknownVersion
)
2141 .collect(Collectors
.toSet())) {
2143 setGroupBlocked(groupId
, true);
2144 } catch (GroupNotFoundException e
) {
2145 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2146 groupId
.toBase64());
2150 if (syncMessage
.getContacts().isPresent()) {
2151 File tmpFile
= null;
2153 tmpFile
= IOUtils
.createTempFile();
2154 final var contactsMessage
= syncMessage
.getContacts().get();
2155 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2156 .asPointer(), tmpFile
)) {
2157 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2162 } catch (IOException e
) {
2163 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2170 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2171 account
.setProfileKey(c
.getProfileKey().get());
2173 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2174 var contact
= account
.getContactStore().getContact(recipientId
);
2175 final var builder
= contact
== null
2176 ? Contact
.newBuilder()
2177 : Contact
.newBuilder(contact
);
2178 if (c
.getName().isPresent()) {
2179 builder
.withName(c
.getName().get());
2181 if (c
.getColor().isPresent()) {
2182 builder
.withColor(c
.getColor().get());
2184 if (c
.getProfileKey().isPresent()) {
2185 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2187 if (c
.getVerified().isPresent()) {
2188 final var verifiedMessage
= c
.getVerified().get();
2189 account
.getIdentityKeyStore()
2190 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2191 verifiedMessage
.getIdentityKey(),
2192 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2194 if (c
.getExpirationTimer().isPresent()) {
2195 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2197 builder
.withBlocked(c
.isBlocked());
2198 builder
.withArchived(c
.isArchived());
2199 account
.getContactStore().storeContact(recipientId
, builder
.build());
2201 if (c
.getAvatar().isPresent()) {
2202 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2206 } catch (Exception e
) {
2207 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2211 if (tmpFile
!= null) {
2213 Files
.delete(tmpFile
.toPath());
2214 } catch (IOException e
) {
2215 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2222 if (syncMessage
.getVerified().isPresent()) {
2223 final var verifiedMessage
= syncMessage
.getVerified().get();
2224 account
.getIdentityKeyStore()
2225 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2226 verifiedMessage
.getIdentityKey(),
2227 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2229 if (syncMessage
.getStickerPackOperations().isPresent()) {
2230 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2231 for (var m
: stickerPackOperationMessages
) {
2232 if (!m
.getPackId().isPresent()) {
2235 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2236 final var installed
= !m
.getType().isPresent()
2237 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
;
2239 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2240 if (m
.getPackKey().isPresent()) {
2241 if (sticker
== null) {
2242 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2245 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, m
.getPackKey().get()));
2249 if (sticker
!= null) {
2250 sticker
.setInstalled(installed
);
2251 account
.getStickerStore().updateSticker(sticker
);
2255 if (syncMessage
.getFetchType().isPresent()) {
2256 switch (syncMessage
.getFetchType().get()) {
2258 getRecipientProfile(account
.getSelfRecipientId(), true);
2259 case STORAGE_MANIFEST
:
2263 if (syncMessage
.getKeys().isPresent()) {
2264 final var keysMessage
= syncMessage
.getKeys().get();
2265 if (keysMessage
.getStorageService().isPresent()) {
2266 final var storageKey
= keysMessage
.getStorageService().get();
2267 account
.setStorageKey(storageKey
);
2270 if (syncMessage
.getConfiguration().isPresent()) {
2278 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2280 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2281 } catch (IOException e
) {
2282 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2286 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2288 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2289 } catch (IOException e
) {
2290 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2294 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2296 avatarStore
.storeGroupAvatar(groupId
,
2297 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2298 } catch (IOException e
) {
2299 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2303 private void downloadProfileAvatar(
2304 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2307 avatarStore
.storeProfileAvatar(address
,
2308 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2309 } catch (Throwable e
) {
2310 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2311 Thread
.currentThread().interrupt();
2313 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2317 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2318 return attachmentStore
.getAttachmentFile(attachmentId
);
2321 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2322 if (!attachment
.isPointer()) {
2323 logger
.warn("Invalid state, can't store an attachment stream.");
2326 var pointer
= attachment
.asPointer();
2327 if (pointer
.getPreview().isPresent()) {
2328 final var preview
= pointer
.getPreview().get();
2330 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2331 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2332 } catch (IOException e
) {
2333 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2338 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2339 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2340 } catch (IOException e
) {
2341 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2345 private void retrieveGroupV2Avatar(
2346 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2347 ) throws IOException
{
2348 var groupOperations
= dependencies
.getGroupsV2Operations().forGroup(groupSecretParams
);
2350 var tmpFile
= IOUtils
.createTempFile();
2351 try (InputStream input
= dependencies
.getMessageReceiver()
2352 .retrieveGroupsV2ProfileAvatar(cdnKey
, tmpFile
, ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2353 var encryptedData
= IOUtils
.readFully(input
);
2355 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2356 outputStream
.write(decryptedData
);
2359 Files
.delete(tmpFile
.toPath());
2360 } catch (IOException e
) {
2361 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2368 private void retrieveProfileAvatar(
2369 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2370 ) throws IOException
{
2371 var tmpFile
= IOUtils
.createTempFile();
2372 try (var input
= dependencies
.getMessageReceiver()
2373 .retrieveProfileAvatar(avatarPath
,
2376 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2377 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2378 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2381 Files
.delete(tmpFile
.toPath());
2382 } catch (IOException e
) {
2383 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2390 private void retrieveAttachment(
2391 final SignalServiceAttachment attachment
, final OutputStream outputStream
2392 ) throws IOException
{
2393 if (attachment
.isPointer()) {
2394 var pointer
= attachment
.asPointer();
2395 retrieveAttachmentPointer(pointer
, outputStream
);
2397 var stream
= attachment
.asStream();
2398 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2402 private void retrieveAttachmentPointer(
2403 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2404 ) throws IOException
{
2405 var tmpFile
= IOUtils
.createTempFile();
2406 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2407 IOUtils
.copyStream(input
, outputStream
);
2408 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2409 throw new IOException(e
);
2412 Files
.delete(tmpFile
.toPath());
2413 } catch (IOException e
) {
2414 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2421 private InputStream
retrieveAttachmentAsStream(
2422 SignalServiceAttachmentPointer pointer
, File tmpFile
2423 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2424 return dependencies
.getMessageReceiver()
2425 .retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2428 void sendGroups() throws IOException
{
2429 var groupsFile
= IOUtils
.createTempFile();
2432 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2433 var out
= new DeviceGroupsOutputStream(fos
);
2434 for (var record : getGroups()) {
2435 if (record instanceof GroupInfoV1
) {
2436 var groupInfo
= (GroupInfoV1
) record;
2437 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2438 Optional
.fromNullable(groupInfo
.name
),
2439 groupInfo
.getMembers()
2441 .map(this::resolveSignalServiceAddress
)
2442 .collect(Collectors
.toList()),
2443 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2444 groupInfo
.isMember(account
.getSelfRecipientId()),
2445 Optional
.of(groupInfo
.messageExpirationTime
),
2446 Optional
.fromNullable(groupInfo
.color
),
2449 groupInfo
.archived
));
2454 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2455 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2456 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2457 .withStream(groupsFileStream
)
2458 .withContentType("application/octet-stream")
2459 .withLength(groupsFile
.length())
2462 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2467 Files
.delete(groupsFile
.toPath());
2468 } catch (IOException e
) {
2469 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2474 public void sendContacts() throws IOException
{
2475 var contactsFile
= IOUtils
.createTempFile();
2478 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2479 var out
= new DeviceContactsOutputStream(fos
);
2480 for (var contactPair
: account
.getContactStore().getContacts()) {
2481 final var recipientId
= contactPair
.first();
2482 final var contact
= contactPair
.second();
2483 final var address
= resolveSignalServiceAddress(recipientId
);
2485 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2486 VerifiedMessage verifiedMessage
= null;
2487 if (currentIdentity
!= null) {
2488 verifiedMessage
= new VerifiedMessage(address
,
2489 currentIdentity
.getIdentityKey(),
2490 currentIdentity
.getTrustLevel().toVerifiedState(),
2491 currentIdentity
.getDateAdded().getTime());
2494 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2495 out
.write(new DeviceContact(address
,
2496 Optional
.fromNullable(contact
.getName()),
2497 createContactAvatarAttachment(address
),
2498 Optional
.fromNullable(contact
.getColor()),
2499 Optional
.fromNullable(verifiedMessage
),
2500 Optional
.fromNullable(profileKey
),
2501 contact
.isBlocked(),
2502 Optional
.of(contact
.getMessageExpirationTime()),
2504 contact
.isArchived()));
2507 if (account
.getProfileKey() != null) {
2508 // Send our own profile key as well
2509 out
.write(new DeviceContact(account
.getSelfAddress(),
2514 Optional
.of(account
.getProfileKey()),
2522 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2523 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2524 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2525 .withStream(contactsFileStream
)
2526 .withContentType("application/octet-stream")
2527 .withLength(contactsFile
.length())
2530 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
,
2536 Files
.delete(contactsFile
.toPath());
2537 } catch (IOException e
) {
2538 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2543 void sendBlockedList() throws IOException
{
2544 var addresses
= new ArrayList
<SignalServiceAddress
>();
2545 for (var record : account
.getContactStore().getContacts()) {
2546 if (record.second().isBlocked()) {
2547 addresses
.add(resolveSignalServiceAddress(record.first()));
2550 var groupIds
= new ArrayList
<byte[]>();
2551 for (var record : getGroups()) {
2552 if (record.isBlocked()) {
2553 groupIds
.add(record.getGroupId().serialize());
2556 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2559 private void sendVerifiedMessage(
2560 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2561 ) throws IOException
{
2562 var verifiedMessage
= new VerifiedMessage(destination
,
2564 trustLevel
.toVerifiedState(),
2565 System
.currentTimeMillis());
2566 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2569 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2570 return account
.getContactStore().getContacts();
2573 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2574 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2575 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2576 if (recipient
== null) {
2580 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2581 return recipient
.getContact().getName();
2584 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2585 return recipient
.getProfile().getDisplayName();
2591 public GroupInfo
getGroup(GroupId groupId
) {
2592 return getGroup(groupId
, false);
2595 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2596 final var group
= account
.getGroupStore().getGroup(groupId
);
2597 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2598 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2599 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2600 account
.getGroupStore().updateGroup(group
);
2605 public List
<IdentityInfo
> getIdentities() {
2606 return account
.getIdentityKeyStore().getIdentities();
2609 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2610 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2611 return identity
== null ? List
.of() : List
.of(identity
);
2615 * Trust this the identity with this fingerprint
2617 * @param name username of the identity
2618 * @param fingerprint Fingerprint
2620 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2621 var recipientId
= canonicalizeAndResolveRecipient(name
);
2622 return trustIdentity(recipientId
,
2623 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2624 TrustLevel
.TRUSTED_VERIFIED
);
2628 * Trust this the identity with this safety number
2630 * @param name username of the identity
2631 * @param safetyNumber Safety number
2633 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2634 var recipientId
= canonicalizeAndResolveRecipient(name
);
2635 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2636 return trustIdentity(recipientId
,
2637 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2638 TrustLevel
.TRUSTED_VERIFIED
);
2642 * Trust all keys of this identity without verification
2644 * @param name username of the identity
2646 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2647 var recipientId
= canonicalizeAndResolveRecipient(name
);
2648 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2651 private boolean trustIdentity(
2652 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2654 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2655 if (identity
== null) {
2659 if (!verifier
.apply(identity
.getIdentityKey())) {
2663 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2665 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2666 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2667 } catch (IOException e
) {
2668 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2674 private void handleIdentityFailure(
2675 final RecipientId recipientId
, final SendMessageResult
.IdentityFailure identityFailure
2677 final var identityKey
= identityFailure
.getIdentityKey();
2678 if (identityKey
!= null) {
2679 final var newIdentity
= account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
2681 account
.getSessionStore().archiveSessions(recipientId
);
2684 // Retrieve profile to get the current identity key from the server
2685 retrieveEncryptedProfile(recipientId
);
2689 public String
computeSafetyNumber(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2690 final var fingerprint
= Utils
.computeSafetyNumber(capabilities
.isUuid(),
2691 account
.getSelfAddress(),
2692 getIdentityKeyPair().getPublicKey(),
2695 return fingerprint
== null ?
null : fingerprint
.getDisplayableFingerprint().getDisplayText();
2698 public byte[] computeSafetyNumberForScanning(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2699 final var fingerprint
= Utils
.computeSafetyNumber(capabilities
.isUuid(),
2700 account
.getSelfAddress(),
2701 getIdentityKeyPair().getPublicKey(),
2704 return fingerprint
== null ?
null : fingerprint
.getScannableFingerprint().getSerialized();
2708 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2709 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2711 return resolveSignalServiceAddress(address
);
2715 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2716 if (address
.matches(account
.getSelfAddress())) {
2717 return account
.getSelfAddress();
2720 return account
.getRecipientStore().resolveServiceAddress(address
);
2723 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2724 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2727 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2728 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2730 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2732 return resolveRecipient(canonicalizedNumber
);
2735 private RecipientId
resolveRecipient(final String identifier
) {
2736 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2738 return resolveRecipient(address
);
2741 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2742 return account
.getRecipientStore().resolveRecipient(address
);
2745 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2746 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2749 private void enqueueJob(Job job
) {
2750 var context
= new Context(account
,
2751 dependencies
.getAccountManager(),
2752 dependencies
.getMessageReceiver(),
2758 public void close() throws IOException
{
2762 void close(boolean closeAccount
) throws IOException
{
2763 executor
.shutdown();
2765 dependencies
.getSignalWebSocket().disconnect();
2767 if (closeAccount
&& account
!= null) {
2773 public interface ReceiveMessageHandler
{
2775 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);