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 canonicalized number and uuid. If a number is not registered the uuid is null.
331 * @throws IOException if its unable to get the contacts to check if they're registered
333 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
334 Map
<String
, String
> canonicalizedNumbers
= numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
336 return canonicalizePhoneNumber(n
);
337 } catch (InvalidNumberException e
) {
342 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
343 var contactDetails
= getRegisteredUsers(canonicalizedNumbers
.values()
345 .filter(s
-> !s
.isEmpty())
346 .collect(Collectors
.toSet()));
348 // Store numbers as recipients so we have the number/uuid association
349 contactDetails
.forEach((number
, uuid
) -> resolveRecipientTrusted(new SignalServiceAddress(uuid
, number
)));
351 return numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
352 final var number
= canonicalizedNumbers
.get(n
);
353 final var uuid
= contactDetails
.get(number
);
354 return new Pair
<>(number
.isEmpty() ?
null : number
, uuid
);
358 public void updateAccountAttributes() throws IOException
{
359 dependencies
.getAccountManager()
360 .setAccountAttributes(account
.getEncryptedDeviceName(),
362 account
.getLocalRegistrationId(),
364 // set legacy pin only if no KBS master key is set
365 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
366 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
367 account
.getSelfUnidentifiedAccessKey(),
368 account
.isUnrestrictedUnidentifiedAccess(),
370 account
.isDiscoverableByPhoneNumber());
374 * @param givenName if null, the previous givenName will be kept
375 * @param familyName if null, the previous familyName will be kept
376 * @param about if null, the previous about text will be kept
377 * @param aboutEmoji if null, the previous about emoji will be kept
378 * @param avatar if avatar is null the image from the local avatar store is used (if present),
380 public void setProfile(
381 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
382 ) throws IOException
{
383 var profile
= getRecipientProfile(account
.getSelfRecipientId());
384 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
385 if (givenName
!= null) {
386 builder
.withGivenName(givenName
);
388 if (familyName
!= null) {
389 builder
.withFamilyName(familyName
);
392 builder
.withAbout(about
);
394 if (aboutEmoji
!= null) {
395 builder
.withAboutEmoji(aboutEmoji
);
397 var newProfile
= builder
.build();
399 try (final var streamDetails
= avatar
== null
400 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
401 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
402 dependencies
.getAccountManager()
403 .setVersionedProfile(account
.getUuid(),
404 account
.getProfileKey(),
405 newProfile
.getInternalServiceName(),
406 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
407 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
412 if (avatar
!= null) {
413 if (avatar
.isPresent()) {
414 avatarStore
.storeProfileAvatar(getSelfAddress(),
415 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
417 avatarStore
.deleteProfileAvatar(getSelfAddress());
420 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
422 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
425 public void unregister() throws IOException
{
426 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
427 // If this is the master device, other users can't send messages to this number anymore.
428 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
429 dependencies
.getAccountManager().setGcmId(Optional
.absent());
431 account
.setRegistered(false);
434 public void deleteAccount() throws IOException
{
435 dependencies
.getAccountManager().deleteAccount();
437 account
.setRegistered(false);
440 public List
<Device
> getLinkedDevices() throws IOException
{
441 var devices
= dependencies
.getAccountManager().getDevices();
442 account
.setMultiDevice(devices
.size() > 1);
443 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
444 return devices
.stream().map(d
-> {
445 String deviceName
= d
.getName();
446 if (deviceName
!= null) {
448 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
449 } catch (IOException e
) {
450 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
453 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
454 }).collect(Collectors
.toList());
457 public void removeLinkedDevices(int deviceId
) throws IOException
{
458 dependencies
.getAccountManager().removeDevice(deviceId
);
459 var devices
= dependencies
.getAccountManager().getDevices();
460 account
.setMultiDevice(devices
.size() > 1);
463 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
464 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
466 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
469 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
470 var identityKeyPair
= getIdentityKeyPair();
471 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
473 dependencies
.getAccountManager()
474 .addDevice(deviceIdentifier
,
477 Optional
.of(account
.getProfileKey().serialize()),
479 account
.setMultiDevice(true);
482 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
483 if (!account
.isMasterDevice()) {
484 throw new RuntimeException("Only master device can set a PIN");
486 if (pin
.isPresent()) {
487 final var masterKey
= account
.getPinMasterKey() != null
488 ? account
.getPinMasterKey()
489 : KeyUtils
.createMasterKey();
491 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
493 account
.setRegistrationLockPin(pin
.get(), masterKey
);
496 pinHelper
.removeRegistrationLockPin();
498 account
.setRegistrationLockPin(null, null);
502 void refreshPreKeys() throws IOException
{
503 var oneTimePreKeys
= generatePreKeys();
504 final var identityKeyPair
= getIdentityKeyPair();
505 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
507 dependencies
.getAccountManager().setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
510 private List
<PreKeyRecord
> generatePreKeys() {
511 final var offset
= account
.getPreKeyIdOffset();
513 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
514 account
.addPreKeys(records
);
519 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
520 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
522 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
523 account
.addSignedPreKey(record);
528 public Profile
getRecipientProfile(
529 RecipientId recipientId
531 return getRecipientProfile(recipientId
, false);
534 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
536 Profile
getRecipientProfile(
537 RecipientId recipientId
, boolean force
539 var profile
= account
.getProfileStore().getProfile(recipientId
);
541 var now
= System
.currentTimeMillis();
542 // Profiles are cached for 24h before retrieving them again, unless forced
543 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
547 synchronized (pendingProfileRequest
) {
548 if (pendingProfileRequest
.contains(recipientId
)) {
551 pendingProfileRequest
.add(recipientId
);
553 final SignalServiceProfile encryptedProfile
;
555 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
557 synchronized (pendingProfileRequest
) {
558 pendingProfileRequest
.remove(recipientId
);
561 if (encryptedProfile
== null) {
565 profile
= decryptProfileIfKeyKnown(recipientId
, encryptedProfile
);
566 account
.getProfileStore().storeProfile(recipientId
, profile
);
571 private Profile
decryptProfileIfKeyKnown(
572 final RecipientId recipientId
, final SignalServiceProfile encryptedProfile
574 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
575 if (profileKey
== null) {
576 return new Profile(System
.currentTimeMillis(),
581 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
582 ProfileUtils
.getCapabilities(encryptedProfile
));
585 return decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
588 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
590 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
591 } catch (IOException e
) {
592 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
597 private ProfileAndCredential
retrieveProfileAndCredential(
598 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
599 ) throws IOException
{
600 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
601 final var profile
= profileAndCredential
.getProfile();
604 var newIdentity
= account
.getIdentityKeyStore()
605 .saveIdentity(recipientId
,
606 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
610 account
.getSessionStore().archiveSessions(recipientId
);
612 } catch (InvalidKeyException ignored
) {
613 logger
.warn("Got invalid identity key in profile for {}",
614 resolveSignalServiceAddress(recipientId
).getIdentifier());
616 return profileAndCredential
;
619 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
620 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
621 if (profileKeyCredential
!= null) {
622 return profileKeyCredential
;
625 ProfileAndCredential profileAndCredential
;
627 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
628 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
629 } catch (IOException e
) {
630 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
634 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
635 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
637 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
638 if (profileKey
!= null) {
639 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
641 profileAndCredential
.getProfile());
642 account
.getProfileStore().storeProfile(recipientId
, profile
);
645 return profileKeyCredential
;
648 private Profile
decryptProfileAndDownloadAvatar(
649 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
651 if (encryptedProfile
.getAvatar() != null) {
652 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
655 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
658 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
659 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
660 if (streamDetails
== null) {
661 return Optional
.absent();
664 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
667 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
668 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
669 if (streamDetails
== null) {
670 return Optional
.absent();
673 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
676 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
677 var g
= getGroup(groupId
);
679 throw new GroupNotFoundException(groupId
);
681 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
682 throw new NotAGroupMemberException(groupId
, g
.getTitle());
687 public List
<GroupInfo
> getGroups() {
688 return account
.getGroupStore().getGroups();
691 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
692 String messageText
, List
<String
> attachments
, GroupId groupId
693 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
694 final var messageBuilder
= createMessageBuilder().withBody(messageText
);
695 if (attachments
!= null) {
696 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
699 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
702 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
703 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
704 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
705 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
706 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
708 resolveSignalServiceAddress(targetAuthorRecipientId
),
709 targetSentTimestamp
);
710 final var messageBuilder
= createMessageBuilder().withReaction(reaction
);
712 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
715 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(
716 GroupId groupId
, Set
<String
> groupAdmins
717 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, InvalidNumberException
, LastGroupAdminException
{
718 var group
= getGroupForUpdating(groupId
);
719 if (group
instanceof GroupInfoV1
) {
720 return quitGroupV1((GroupInfoV1
) group
);
723 final var newAdmins
= getRecipientIds(groupAdmins
);
725 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
726 } catch (ConflictException e
) {
727 // Detected conflicting update, refreshing group and trying again
728 group
= getGroup(groupId
, true);
729 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
733 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV1(final GroupInfoV1 groupInfoV1
) throws IOException
{
734 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
735 .withId(groupInfoV1
.getGroupId().serialize())
738 var messageBuilder
= createMessageBuilder().asGroupMessage(group
);
739 groupInfoV1
.removeMember(account
.getSelfRecipientId());
740 account
.getGroupStore().updateGroup(groupInfoV1
);
741 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
742 groupInfoV1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
745 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV2(
746 final GroupInfoV2 groupInfoV2
, final Set
<RecipientId
> newAdmins
747 ) throws LastGroupAdminException
, IOException
{
748 final var currentAdmins
= groupInfoV2
.getAdminMembers();
749 newAdmins
.removeAll(currentAdmins
);
750 newAdmins
.retainAll(groupInfoV2
.getMembers());
751 if (currentAdmins
.contains(getSelfRecipientId())
752 && currentAdmins
.size() == 1
753 && groupInfoV2
.getMembers().size() > 1
754 && newAdmins
.size() == 0) {
755 // Last admin can't leave the group, unless she's also the last member
756 throw new LastGroupAdminException(groupInfoV2
.getGroupId(), groupInfoV2
.getTitle());
758 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
759 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
760 var messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
761 account
.getGroupStore().updateGroup(groupInfoV2
);
762 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
763 groupInfoV2
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
766 public void deleteGroup(GroupId groupId
) throws IOException
{
767 account
.getGroupStore().deleteGroup(groupId
);
768 avatarStore
.deleteGroupAvatar(groupId
);
771 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
772 String name
, List
<String
> members
, File avatarFile
773 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
774 return createGroup(name
, members
== null ?
null : getRecipientIds(members
), avatarFile
);
777 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
778 String name
, Set
<RecipientId
> members
, File avatarFile
779 ) throws IOException
, AttachmentInvalidException
{
780 final var selfRecipientId
= account
.getSelfRecipientId();
781 if (members
!= null && members
.contains(selfRecipientId
)) {
782 members
= new HashSet
<>(members
);
783 members
.remove(selfRecipientId
);
786 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
787 members
== null ? Set
.of() : members
,
790 SignalServiceDataMessage
.Builder messageBuilder
;
791 if (gv2Pair
== null) {
792 // Failed to create v2 group, creating v1 group instead
793 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
794 gv1
.addMembers(List
.of(selfRecipientId
));
795 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
796 return new Pair
<>(gv1
.getGroupId(), result
.second());
799 final var gv2
= gv2Pair
.first();
800 final var decryptedGroup
= gv2Pair
.second();
802 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
803 if (avatarFile
!= null) {
804 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
805 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
807 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
808 account
.getGroupStore().updateGroup(gv2
);
810 final var result
= sendHelper
.sendGroupMessage(messageBuilder
.build(),
811 gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
812 return new Pair
<>(gv2
.getGroupId(), result
.second());
815 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
819 List
<String
> members
,
820 List
<String
> removeMembers
,
822 List
<String
> removeAdmins
,
823 boolean resetGroupLink
,
824 GroupLinkState groupLinkState
,
825 GroupPermission addMemberPermission
,
826 GroupPermission editDetailsPermission
,
828 Integer expirationTimer
,
829 Boolean isAnnouncementGroup
830 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
831 return updateGroup(groupId
,
834 members
== null ?
null : getRecipientIds(members
),
835 removeMembers
== null ?
null : getRecipientIds(removeMembers
),
836 admins
== null ?
null : getRecipientIds(admins
),
837 removeAdmins
== null ?
null : getRecipientIds(removeAdmins
),
841 editDetailsPermission
,
844 isAnnouncementGroup
);
847 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
848 final GroupId groupId
,
850 final String description
,
851 final Set
<RecipientId
> members
,
852 final Set
<RecipientId
> removeMembers
,
853 final Set
<RecipientId
> admins
,
854 final Set
<RecipientId
> removeAdmins
,
855 final boolean resetGroupLink
,
856 final GroupLinkState groupLinkState
,
857 final GroupPermission addMemberPermission
,
858 final GroupPermission editDetailsPermission
,
859 final File avatarFile
,
860 final Integer expirationTimer
,
861 final Boolean isAnnouncementGroup
862 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
863 var group
= getGroupForUpdating(groupId
);
865 if (group
instanceof GroupInfoV2
) {
867 return updateGroupV2((GroupInfoV2
) group
,
877 editDetailsPermission
,
880 isAnnouncementGroup
);
881 } catch (ConflictException e
) {
882 // Detected conflicting update, refreshing group and trying again
883 group
= getGroup(groupId
, true);
884 return updateGroupV2((GroupInfoV2
) group
,
894 editDetailsPermission
,
897 isAnnouncementGroup
);
901 final var gv1
= (GroupInfoV1
) group
;
902 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
903 if (expirationTimer
!= null) {
904 setExpirationTimer(gv1
, expirationTimer
);
909 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
910 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
911 ) throws IOException
, AttachmentInvalidException
{
912 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
913 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
915 account
.getGroupStore().updateGroup(gv1
);
917 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
918 gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
921 private void updateGroupV1Details(
922 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
923 ) throws IOException
{
928 if (members
!= null) {
929 final var newMemberAddresses
= members
.stream()
930 .filter(member
-> !g
.isMember(member
))
931 .map(this::resolveSignalServiceAddress
)
932 .collect(Collectors
.toList());
933 final var newE164Members
= new HashSet
<String
>();
934 for (var member
: newMemberAddresses
) {
935 if (!member
.getNumber().isPresent()) {
938 newE164Members
.add(member
.getNumber().get());
941 final var registeredUsers
= getRegisteredUsers(newE164Members
);
942 if (registeredUsers
.size() != newE164Members
.size()) {
943 // Some of the new members are not registered on Signal
944 newE164Members
.removeAll(registeredUsers
.keySet());
945 throw new IOException("Failed to add members "
946 + String
.join(", ", newE164Members
)
947 + " to group: Not registered on Signal");
950 g
.addMembers(members
);
953 if (avatarFile
!= null) {
954 avatarStore
.storeGroupAvatar(g
.getGroupId(),
955 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
959 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
960 final GroupInfoV2 group
,
962 final String description
,
963 final Set
<RecipientId
> members
,
964 final Set
<RecipientId
> removeMembers
,
965 final Set
<RecipientId
> admins
,
966 final Set
<RecipientId
> removeAdmins
,
967 final boolean resetGroupLink
,
968 final GroupLinkState groupLinkState
,
969 final GroupPermission addMemberPermission
,
970 final GroupPermission editDetailsPermission
,
971 final File avatarFile
,
972 final Integer expirationTimer
,
973 final Boolean isAnnouncementGroup
974 ) throws IOException
{
975 Pair
<Long
, List
<SendMessageResult
>> result
= null;
976 if (group
.isPendingMember(account
.getSelfRecipientId())) {
977 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
978 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
981 if (members
!= null) {
982 final var newMembers
= new HashSet
<>(members
);
983 newMembers
.removeAll(group
.getMembers());
984 if (newMembers
.size() > 0) {
985 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
986 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
990 if (removeMembers
!= null) {
991 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
992 existingRemoveMembers
.retainAll(group
.getMembers());
993 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
994 if (existingRemoveMembers
.size() > 0) {
995 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
996 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
999 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
1000 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
1001 if (pendingRemoveMembers
.size() > 0) {
1002 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
1003 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1007 if (admins
!= null) {
1008 final var newAdmins
= new HashSet
<>(admins
);
1009 newAdmins
.retainAll(group
.getMembers());
1010 newAdmins
.removeAll(group
.getAdminMembers());
1011 if (newAdmins
.size() > 0) {
1012 for (var admin
: newAdmins
) {
1013 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1014 result
= sendUpdateGroupV2Message(group
,
1015 groupGroupChangePair
.first(),
1016 groupGroupChangePair
.second());
1021 if (removeAdmins
!= null) {
1022 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1023 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1024 if (existingRemoveAdmins
.size() > 0) {
1025 for (var admin
: existingRemoveAdmins
) {
1026 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1027 result
= sendUpdateGroupV2Message(group
,
1028 groupGroupChangePair
.first(),
1029 groupGroupChangePair
.second());
1034 if (resetGroupLink
) {
1035 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1036 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1039 if (groupLinkState
!= null) {
1040 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1041 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1044 if (addMemberPermission
!= null) {
1045 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1046 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1049 if (editDetailsPermission
!= null) {
1050 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1051 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1054 if (expirationTimer
!= null) {
1055 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1056 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1059 if (isAnnouncementGroup
!= null) {
1060 var groupGroupChangePair
= groupV2Helper
.setIsAnnouncementGroup(group
, isAnnouncementGroup
);
1061 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1064 if (name
!= null || description
!= null || avatarFile
!= null) {
1065 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1066 if (avatarFile
!= null) {
1067 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1068 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1070 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1076 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1077 GroupInviteLinkUrl inviteLinkUrl
1078 ) throws IOException
, GroupLinkNotActiveException
{
1079 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1080 inviteLinkUrl
.getPassword());
1081 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1082 inviteLinkUrl
.getPassword(),
1084 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1085 groupJoinInfo
.getRevision() + 1,
1086 groupChange
.toByteArray());
1088 if (group
.getGroup() == null) {
1089 // Only requested member, can't send update to group members
1090 return new Pair
<>(group
.getGroupId(), List
.of());
1093 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1095 return new Pair
<>(group
.getGroupId(), result
.second());
1098 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1099 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1100 ) throws IOException
{
1101 final var selfRecipientId
= account
.getSelfRecipientId();
1102 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1103 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1104 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1106 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1107 account
.getGroupStore().updateGroup(group
);
1108 return sendHelper
.sendGroupMessage(messageBuilder
.build(), members
);
1111 private static int currentTimeDays() {
1112 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1115 private GroupsV2AuthorizationString
getGroupAuthForToday(
1116 final GroupSecretParams groupSecretParams
1117 ) throws IOException
{
1118 final var today
= currentTimeDays();
1119 // Returns credentials for the next 7 days
1120 final var credentials
= dependencies
.getGroupsV2Api().getCredentials(today
);
1121 // TODO cache credentials until they expire
1122 var authCredentialResponse
= credentials
.get(today
);
1124 return dependencies
.getGroupsV2Api()
1125 .getGroupsV2AuthorizationString(account
.getUuid(),
1128 authCredentialResponse
);
1129 } catch (VerificationFailedException e
) {
1130 throw new IOException(e
);
1134 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1135 GroupIdV1 groupId
, SignalServiceAddress recipient
1136 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1138 var group
= getGroupForUpdating(groupId
);
1139 if (!(group
instanceof GroupInfoV1
)) {
1140 throw new IOException("Received an invalid group request for a v2 group!");
1142 g
= (GroupInfoV1
) group
;
1144 final var recipientId
= resolveRecipient(recipient
);
1145 if (!g
.isMember(recipientId
)) {
1146 throw new NotAGroupMemberException(groupId
, g
.name
);
1149 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1151 // Send group message only to the recipient who requested it
1152 return sendHelper
.sendGroupMessage(messageBuilder
.build(), Set
.of(recipientId
));
1155 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1156 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1157 .withId(g
.getGroupId().serialize())
1159 .withMembers(g
.getMembers()
1161 .map(this::resolveSignalServiceAddress
)
1162 .collect(Collectors
.toList()));
1165 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1166 if (attachment
.isPresent()) {
1167 group
.withAvatar(attachment
.get());
1169 } catch (IOException e
) {
1170 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1173 return createMessageBuilder().asGroupMessage(group
.build()).withExpiration(g
.getMessageExpirationTime());
1176 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1177 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1178 .withRevision(g
.getGroup().getRevision())
1179 .withSignedGroupChange(signedGroupChange
);
1180 return createMessageBuilder().asGroupMessage(group
.build()).withExpiration(g
.getMessageExpirationTime());
1183 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1184 GroupIdV1 groupId
, SignalServiceAddress recipient
1185 ) throws IOException
{
1186 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1188 var messageBuilder
= createMessageBuilder().asGroupMessage(group
.build());
1190 // Send group info request message to the recipient who sent us a message with this groupId
1191 return sendHelper
.sendGroupMessage(messageBuilder
.build(), Set
.of(resolveRecipient(recipient
)));
1195 SignalServiceAddress remoteAddress
, long messageId
1196 ) throws IOException
, UntrustedIdentityException
{
1197 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1199 System
.currentTimeMillis());
1201 sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(remoteAddress
));
1204 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1205 String messageText
, List
<String
> attachments
, List
<String
> recipients
1206 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1207 final var messageBuilder
= createMessageBuilder().withBody(messageText
);
1208 if (attachments
!= null) {
1209 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1211 // Upload attachments here, so we only upload once even for multiple recipients
1212 var messageSender
= dependencies
.getMessageSender();
1213 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1214 for (var attachment
: attachmentStreams
) {
1215 if (attachment
.isStream()) {
1216 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1217 } else if (attachment
.isPointer()) {
1218 attachmentPointers
.add(attachment
.asPointer());
1222 messageBuilder
.withAttachments(attachmentPointers
);
1224 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1227 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1228 String messageText
, List
<String
> attachments
1229 ) throws IOException
, AttachmentInvalidException
{
1230 final var messageBuilder
= createMessageBuilder().withBody(messageText
);
1231 if (attachments
!= null) {
1232 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1234 return sendHelper
.sendSelfMessage(messageBuilder
);
1237 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1238 long targetSentTimestamp
, List
<String
> recipients
1239 ) throws IOException
, InvalidNumberException
{
1240 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1241 final var messageBuilder
= createMessageBuilder().withRemoteDelete(delete
);
1242 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1245 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1246 long targetSentTimestamp
, GroupId groupId
1247 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1248 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1249 final var messageBuilder
= createMessageBuilder().withRemoteDelete(delete
);
1250 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1253 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1254 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1255 ) throws IOException
, InvalidNumberException
{
1256 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1257 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1259 resolveSignalServiceAddress(targetAuthorRecipientId
),
1260 targetSentTimestamp
);
1261 final var messageBuilder
= createMessageBuilder().withReaction(reaction
);
1262 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1265 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1266 var messageBuilder
= createMessageBuilder().asEndSessionMessage();
1268 final var recipientIds
= getRecipientIds(recipients
);
1270 return sendHelper
.sendMessage(messageBuilder
, recipientIds
);
1272 for (var recipientId
: recipientIds
) {
1273 handleEndSession(recipientId
);
1278 void renewSession(RecipientId recipientId
) throws IOException
{
1279 account
.getSessionStore().archiveSessions(recipientId
);
1280 if (!recipientId
.equals(getSelfRecipientId())) {
1281 sendHelper
.sendNullMessage(recipientId
);
1285 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1286 if (!account
.isMasterDevice()) {
1287 throw new NotMasterDeviceException();
1289 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1290 var contact
= account
.getContactStore().getContact(recipientId
);
1291 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1292 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1295 public void setContactBlocked(
1296 String number
, boolean blocked
1297 ) throws InvalidNumberException
, NotMasterDeviceException
{
1298 if (!account
.isMasterDevice()) {
1299 throw new NotMasterDeviceException();
1301 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1304 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1305 var contact
= account
.getContactStore().getContact(recipientId
);
1306 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1307 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1310 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1311 var group
= getGroup(groupId
);
1312 if (group
== null) {
1313 throw new GroupNotFoundException(groupId
);
1316 group
.setBlocked(blocked
);
1317 account
.getGroupStore().updateGroup(group
);
1320 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1321 var contact
= account
.getContactStore().getContact(recipientId
);
1322 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1325 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1326 account
.getContactStore()
1327 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1330 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1331 final var messageBuilder
= createMessageBuilder().asExpirationUpdate();
1332 sendHelper
.sendMessage(messageBuilder
, Set
.of(recipientId
));
1336 * Change the expiration timer for a contact
1338 public void setExpirationTimer(
1339 String number
, int messageExpirationTimer
1340 ) throws IOException
, InvalidNumberException
{
1341 var recipientId
= canonicalizeAndResolveRecipient(number
);
1342 setExpirationTimer(recipientId
, messageExpirationTimer
);
1343 sendExpirationTimerUpdate(recipientId
);
1347 * Change the expiration timer for a group
1349 private void setExpirationTimer(
1350 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1351 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1352 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1353 account
.getGroupStore().updateGroup(groupInfoV1
);
1354 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1357 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1358 final var messageBuilder
= createMessageBuilder().asExpirationUpdate();
1359 sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1363 * Upload the sticker pack from path.
1365 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1366 * @return if successful, returns the URL to install the sticker pack in the signal app
1368 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1369 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1371 var messageSender
= dependencies
.getMessageSender();
1373 var packKey
= KeyUtils
.createStickerUploadKey();
1374 var packIdString
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1375 var packId
= StickerPackId
.deserialize(Hex
.fromStringCondensed(packIdString
));
1377 var sticker
= new Sticker(packId
, packKey
);
1378 account
.getStickerStore().updateSticker(sticker
);
1381 return new URI("https",
1385 + URLEncoder
.encode(Hex
.toStringCondensed(packId
.serialize()), StandardCharsets
.UTF_8
)
1387 + URLEncoder
.encode(Hex
.toStringCondensed(packKey
), StandardCharsets
.UTF_8
)).toString();
1388 } catch (URISyntaxException e
) {
1389 throw new AssertionError(e
);
1393 public void requestAllSyncData() throws IOException
{
1394 requestSyncGroups();
1395 requestSyncContacts();
1396 requestSyncBlocked();
1397 requestSyncConfiguration();
1401 private void requestSyncGroups() throws IOException
{
1402 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1403 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1405 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1406 sendHelper
.sendSyncMessage(message
);
1409 private void requestSyncContacts() throws IOException
{
1410 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1411 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1413 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1414 sendHelper
.sendSyncMessage(message
);
1417 private void requestSyncBlocked() throws IOException
{
1418 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1419 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1421 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1422 sendHelper
.sendSyncMessage(message
);
1425 private void requestSyncConfiguration() throws IOException
{
1426 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1427 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1429 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1430 sendHelper
.sendSyncMessage(message
);
1433 private void requestSyncKeys() throws IOException
{
1434 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1435 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1437 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1438 sendHelper
.sendSyncMessage(message
);
1441 private byte[] getSenderCertificate() {
1444 if (account
.isPhoneNumberShared()) {
1445 certificate
= dependencies
.getAccountManager().getSenderCertificate();
1447 certificate
= dependencies
.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
1449 } catch (IOException e
) {
1450 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1453 // TODO cache for a day
1457 private Set
<RecipientId
> getRecipientIds(Collection
<String
> numbers
) throws InvalidNumberException
{
1458 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1459 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1461 for (var number
: numbers
) {
1462 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1463 if (resolvedAddress
.getUuid().isPresent()) {
1464 signalServiceAddresses
.add(resolvedAddress
);
1466 addressesMissingUuid
.add(resolvedAddress
);
1470 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1471 .map(a
-> a
.getNumber().get())
1472 .collect(Collectors
.toSet());
1473 Map
<String
, UUID
> registeredUsers
;
1475 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1476 } catch (IOException e
) {
1477 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1478 registeredUsers
= Map
.of();
1481 for (var address
: addressesMissingUuid
) {
1482 final var number
= address
.getNumber().get();
1483 if (registeredUsers
.containsKey(number
)) {
1484 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1485 registeredUsers
.get(number
),
1487 signalServiceAddresses
.add(newAddress
);
1489 signalServiceAddresses
.add(address
);
1493 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1496 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1497 final var address
= resolveSignalServiceAddress(recipientId
);
1498 if (!address
.getNumber().isPresent()) {
1501 final var number
= address
.getNumber().get();
1502 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1503 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1506 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1508 return dependencies
.getAccountManager()
1509 .getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1511 serviceEnvironmentConfig
.getCdsMrenclave());
1512 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1513 throw new IOException(e
);
1517 public void sendTypingMessage(
1518 TypingAction action
, Set
<String
> recipients
1519 ) throws IOException
, UntrustedIdentityException
, InvalidNumberException
{
1520 final var timestamp
= System
.currentTimeMillis();
1521 var message
= new SignalServiceTypingMessage(action
.toSignalService(), timestamp
, Optional
.absent());
1522 sendHelper
.sendTypingMessage(message
, getRecipientIds(recipients
));
1525 public void sendGroupTypingMessage(
1526 TypingAction action
, GroupId groupId
1527 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1528 final var timestamp
= System
.currentTimeMillis();
1529 final var message
= new SignalServiceTypingMessage(action
.toSignalService(),
1531 Optional
.of(groupId
.serialize()));
1532 sendHelper
.sendGroupTypingMessage(message
, groupId
);
1535 private SignalServiceDataMessage
.Builder
createMessageBuilder() {
1536 final var timestamp
= System
.currentTimeMillis();
1538 var messageBuilder
= SignalServiceDataMessage
.newBuilder();
1539 messageBuilder
.withTimestamp(timestamp
);
1540 return messageBuilder
;
1543 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
, InvalidMessageStructureException
{
1544 return dependencies
.getCipher().decrypt(envelope
);
1547 private void handleEndSession(RecipientId recipientId
) {
1548 account
.getSessionStore().deleteAllSessions(recipientId
);
1551 private List
<HandleAction
> handleSignalServiceDataMessage(
1552 SignalServiceDataMessage message
,
1554 SignalServiceAddress source
,
1555 SignalServiceAddress destination
,
1556 boolean ignoreAttachments
1558 var actions
= new ArrayList
<HandleAction
>();
1559 if (message
.getGroupContext().isPresent()) {
1560 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1561 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1562 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1563 var group
= getGroup(groupId
);
1564 if (group
== null || group
instanceof GroupInfoV1
) {
1565 var groupV1
= (GroupInfoV1
) group
;
1566 switch (groupInfo
.getType()) {
1568 if (groupV1
== null) {
1569 groupV1
= new GroupInfoV1(groupId
);
1572 if (groupInfo
.getAvatar().isPresent()) {
1573 var avatar
= groupInfo
.getAvatar().get();
1574 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1577 if (groupInfo
.getName().isPresent()) {
1578 groupV1
.name
= groupInfo
.getName().get();
1581 if (groupInfo
.getMembers().isPresent()) {
1582 groupV1
.addMembers(groupInfo
.getMembers()
1585 .map(this::resolveRecipient
)
1586 .collect(Collectors
.toSet()));
1589 account
.getGroupStore().updateGroup(groupV1
);
1593 if (groupV1
== null && !isSync
) {
1594 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1598 if (groupV1
!= null) {
1599 groupV1
.removeMember(resolveRecipient(source
));
1600 account
.getGroupStore().updateGroup(groupV1
);
1605 if (groupV1
!= null && !isSync
) {
1606 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1611 // Received a group v1 message for a v2 group
1614 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1615 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1616 final var groupMasterKey
= groupContext
.getMasterKey();
1618 getOrMigrateGroup(groupMasterKey
,
1619 groupContext
.getRevision(),
1620 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1624 final var conversationPartnerAddress
= isSync ? destination
: source
;
1625 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1626 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1628 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1629 if (message
.getGroupContext().isPresent()) {
1630 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1631 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1632 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1633 if (group
!= null) {
1634 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1635 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1636 account
.getGroupStore().updateGroup(group
);
1639 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1640 // disappearing message timer already stored in the DecryptedGroup
1642 } else if (conversationPartnerAddress
!= null) {
1643 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1646 if (!ignoreAttachments
) {
1647 if (message
.getAttachments().isPresent()) {
1648 for (var attachment
: message
.getAttachments().get()) {
1649 downloadAttachment(attachment
);
1652 if (message
.getSharedContacts().isPresent()) {
1653 for (var contact
: message
.getSharedContacts().get()) {
1654 if (contact
.getAvatar().isPresent()) {
1655 downloadAttachment(contact
.getAvatar().get().getAttachment());
1660 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1661 final ProfileKey profileKey
;
1663 profileKey
= new ProfileKey(message
.getProfileKey().get());
1664 } catch (InvalidInputException e
) {
1665 throw new AssertionError(e
);
1667 if (source
.matches(account
.getSelfAddress())) {
1668 this.account
.setProfileKey(profileKey
);
1670 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1672 if (message
.getPreviews().isPresent()) {
1673 final var previews
= message
.getPreviews().get();
1674 for (var preview
: previews
) {
1675 if (preview
.getImage().isPresent()) {
1676 downloadAttachment(preview
.getImage().get());
1680 if (message
.getQuote().isPresent()) {
1681 final var quote
= message
.getQuote().get();
1683 for (var quotedAttachment
: quote
.getAttachments()) {
1684 final var thumbnail
= quotedAttachment
.getThumbnail();
1685 if (thumbnail
!= null) {
1686 downloadAttachment(thumbnail
);
1690 if (message
.getSticker().isPresent()) {
1691 final var messageSticker
= message
.getSticker().get();
1692 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1693 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1694 if (sticker
== null) {
1695 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1696 account
.getStickerStore().updateSticker(sticker
);
1698 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, messageSticker
.getPackKey()));
1703 private GroupInfoV2
getOrMigrateGroup(
1704 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1706 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1708 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1709 var groupInfo
= getGroup(groupId
);
1710 final GroupInfoV2 groupInfoV2
;
1711 if (groupInfo
instanceof GroupInfoV1
) {
1712 // Received a v2 group message for a v1 group, we need to locally migrate the group
1713 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1714 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1715 logger
.info("Locally migrated group {} to group v2, id: {}",
1716 groupInfo
.getGroupId().toBase64(),
1717 groupInfoV2
.getGroupId().toBase64());
1718 } else if (groupInfo
instanceof GroupInfoV2
) {
1719 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1721 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1724 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1725 DecryptedGroup group
= null;
1726 if (signedGroupChange
!= null
1727 && groupInfoV2
.getGroup() != null
1728 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1729 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1733 if (group
== null) {
1734 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1736 if (group
!= null) {
1737 storeProfileKeysFromMembers(group
);
1738 final var avatar
= group
.getAvatar();
1739 if (avatar
!= null && !avatar
.isEmpty()) {
1740 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1743 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1744 account
.getGroupStore().updateGroup(groupInfoV2
);
1750 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1751 for (var member
: group
.getMembersList()) {
1752 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1753 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1755 account
.getProfileStore()
1756 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1757 } catch (InvalidInputException ignored
) {
1762 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1763 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1764 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1765 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1766 if (actions
!= null) {
1767 queuedActions
.addAll(actions
);
1770 for (var action
: queuedActions
) {
1772 action
.execute(this);
1773 } catch (Throwable e
) {
1774 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1775 Thread
.currentThread().interrupt();
1777 logger
.warn("Message action failed.", e
);
1782 private List
<HandleAction
> retryFailedReceivedMessage(
1783 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1785 var envelope
= cachedMessage
.loadEnvelope();
1786 if (envelope
== null) {
1789 SignalServiceContent content
= null;
1790 List
<HandleAction
> actions
= null;
1791 if (!envelope
.isReceipt()) {
1793 content
= decryptMessage(envelope
);
1794 } catch (ProtocolUntrustedIdentityException e
) {
1795 if (!envelope
.hasSource()) {
1796 final var identifier
= e
.getSender();
1797 final var recipientId
= resolveRecipient(identifier
);
1799 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1800 } catch (IOException ioException
) {
1801 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1805 } catch (Exception er
) {
1806 // All other errors are not recoverable, so delete the cached message
1807 cachedMessage
.delete();
1810 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1812 handler
.handleMessage(envelope
, content
, null);
1813 cachedMessage
.delete();
1817 public void receiveMessages(
1820 boolean returnOnTimeout
,
1821 boolean ignoreAttachments
,
1822 ReceiveMessageHandler handler
1823 ) throws IOException
, InterruptedException
{
1824 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1826 Set
<HandleAction
> queuedActions
= null;
1828 final var signalWebSocket
= dependencies
.getSignalWebSocket();
1829 signalWebSocket
.connect();
1831 var hasCaughtUpWithOldMessages
= false;
1833 while (!Thread
.interrupted()) {
1834 SignalServiceEnvelope envelope
;
1835 SignalServiceContent content
= null;
1836 Exception exception
= null;
1837 final CachedMessage
[] cachedMessage
= {null};
1838 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
1839 logger
.debug("Checking for new message from server");
1841 var result
= signalWebSocket
.readOrEmpty(unit
.toMillis(timeout
), envelope1
-> {
1842 final var recipientId
= envelope1
.hasSource()
1843 ?
resolveRecipient(envelope1
.getSourceIdentifier())
1845 // store message on disk, before acknowledging receipt to the server
1846 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1848 logger
.debug("New message received from server");
1849 if (result
.isPresent()) {
1850 envelope
= result
.get();
1852 // Received indicator that server queue is empty
1853 hasCaughtUpWithOldMessages
= true;
1855 if (queuedActions
!= null) {
1856 for (var action
: queuedActions
) {
1858 action
.execute(this);
1859 } catch (Throwable e
) {
1860 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1861 Thread
.currentThread().interrupt();
1863 logger
.warn("Message action failed.", e
);
1866 queuedActions
.clear();
1867 queuedActions
= null;
1870 // Continue to wait another timeout for new messages
1873 } catch (AssertionError e
) {
1874 if (e
.getCause() instanceof InterruptedException
) {
1875 throw (InterruptedException
) e
.getCause();
1879 } catch (WebSocketUnavailableException e
) {
1880 logger
.debug("Pipe unexpectedly unavailable, connecting");
1881 signalWebSocket
.connect();
1883 } catch (TimeoutException e
) {
1884 if (returnOnTimeout
) return;
1888 if (envelope
.hasSource()) {
1889 // Store uuid if we don't have it already
1890 // address/uuid in envelope is sent by server
1891 resolveRecipientTrusted(envelope
.getSourceAddress());
1893 if (!envelope
.isReceipt()) {
1895 content
= decryptMessage(envelope
);
1896 } catch (Exception e
) {
1899 if (!envelope
.hasSource() && content
!= null) {
1900 // Store uuid if we don't have it already
1901 // address/uuid is validated by unidentified sender certificate
1902 resolveRecipientTrusted(content
.getSender());
1904 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1905 if (exception
instanceof ProtocolInvalidMessageException
) {
1906 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
1907 logger
.debug("Received invalid message, queuing renew session action.");
1908 actions
.add(new RenewSessionAction(sender
));
1910 if (hasCaughtUpWithOldMessages
) {
1911 for (var action
: actions
) {
1913 action
.execute(this);
1914 } catch (Throwable e
) {
1915 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1916 Thread
.currentThread().interrupt();
1918 logger
.warn("Message action failed.", e
);
1922 if (queuedActions
== null) {
1923 queuedActions
= new HashSet
<>();
1925 queuedActions
.addAll(actions
);
1928 final var notAllowedToSendToGroup
= isNotAllowedToSendToGroup(envelope
, content
);
1929 if (isMessageBlocked(envelope
, content
)) {
1930 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
1931 } else if (notAllowedToSendToGroup
) {
1932 logger
.info("Ignoring a group message from an unauthorized sender (no member or admin): {} {}",
1933 (envelope
.hasSource() ? envelope
.getSourceAddress() : content
.getSender()).getIdentifier(),
1934 envelope
.getTimestamp());
1936 handler
.handleMessage(envelope
, content
, exception
);
1938 if (cachedMessage
[0] != null) {
1939 if (exception
instanceof ProtocolUntrustedIdentityException
) {
1940 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
1941 final var recipientId
= resolveRecipient(identifier
);
1942 queuedActions
.add(new RetrieveProfileAction(recipientId
));
1943 if (!envelope
.hasSource()) {
1945 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
1946 } catch (IOException ioException
) {
1947 logger
.warn("Failed to move cached message to recipient folder: {}",
1948 ioException
.getMessage());
1952 cachedMessage
[0].delete();
1958 private boolean isMessageBlocked(
1959 SignalServiceEnvelope envelope
, SignalServiceContent content
1961 SignalServiceAddress source
;
1962 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
1963 source
= envelope
.getSourceAddress();
1964 } else if (content
!= null) {
1965 source
= content
.getSender();
1969 final var recipientId
= resolveRecipient(source
);
1970 if (isContactBlocked(recipientId
)) {
1974 if (content
!= null && content
.getDataMessage().isPresent()) {
1975 var message
= content
.getDataMessage().get();
1976 if (message
.getGroupContext().isPresent()) {
1977 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
1978 var group
= getGroup(groupId
);
1979 if (group
!= null && group
.isBlocked()) {
1987 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
1988 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
1989 return isContactBlocked(recipientId
);
1992 private boolean isContactBlocked(final RecipientId recipientId
) {
1993 var sourceContact
= account
.getContactStore().getContact(recipientId
);
1994 return sourceContact
!= null && sourceContact
.isBlocked();
1997 private boolean isNotAllowedToSendToGroup(
1998 SignalServiceEnvelope envelope
, SignalServiceContent content
2000 SignalServiceAddress source
;
2001 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2002 source
= envelope
.getSourceAddress();
2003 } else if (content
!= null) {
2004 source
= content
.getSender();
2009 if (content
== null || !content
.getDataMessage().isPresent()) {
2013 var message
= content
.getDataMessage().get();
2014 if (!message
.getGroupContext().isPresent()) {
2018 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2019 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2020 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2025 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2026 var group
= getGroup(groupId
);
2027 if (group
== null) {
2031 final var recipientId
= resolveRecipient(source
);
2032 return !group
.isMember(recipientId
) || (
2033 group
.isAnnouncementGroup() && !group
.isAdmin(recipientId
)
2037 private List
<HandleAction
> handleMessage(
2038 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2040 var actions
= new ArrayList
<HandleAction
>();
2041 if (content
!= null) {
2042 final SignalServiceAddress sender
;
2043 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2044 sender
= envelope
.getSourceAddress();
2046 sender
= content
.getSender();
2049 if (content
.getDataMessage().isPresent()) {
2050 var message
= content
.getDataMessage().get();
2052 if (content
.isNeedsReceipt()) {
2053 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2056 actions
.addAll(handleSignalServiceDataMessage(message
,
2059 account
.getSelfAddress(),
2060 ignoreAttachments
));
2062 if (content
.getSyncMessage().isPresent()) {
2063 account
.setMultiDevice(true);
2064 var syncMessage
= content
.getSyncMessage().get();
2065 if (syncMessage
.getSent().isPresent()) {
2066 var message
= syncMessage
.getSent().get();
2067 final var destination
= message
.getDestination().orNull();
2068 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2072 ignoreAttachments
));
2074 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2075 var rm
= syncMessage
.getRequest().get();
2076 if (rm
.isContactsRequest()) {
2077 actions
.add(SendSyncContactsAction
.create());
2079 if (rm
.isGroupsRequest()) {
2080 actions
.add(SendSyncGroupsAction
.create());
2082 if (rm
.isBlockedListRequest()) {
2083 actions
.add(SendSyncBlockedListAction
.create());
2085 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2087 if (syncMessage
.getGroups().isPresent()) {
2088 File tmpFile
= null;
2090 tmpFile
= IOUtils
.createTempFile();
2091 final var groupsMessage
= syncMessage
.getGroups().get();
2092 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2093 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2098 } catch (IOException e
) {
2099 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2105 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2106 if (syncGroup
!= null) {
2107 if (g
.getName().isPresent()) {
2108 syncGroup
.name
= g
.getName().get();
2110 syncGroup
.addMembers(g
.getMembers()
2112 .map(this::resolveRecipient
)
2113 .collect(Collectors
.toSet()));
2114 if (!g
.isActive()) {
2115 syncGroup
.removeMember(account
.getSelfRecipientId());
2117 // Add ourself to the member set as it's marked as active
2118 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2120 syncGroup
.blocked
= g
.isBlocked();
2121 if (g
.getColor().isPresent()) {
2122 syncGroup
.color
= g
.getColor().get();
2125 if (g
.getAvatar().isPresent()) {
2126 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2128 syncGroup
.archived
= g
.isArchived();
2129 account
.getGroupStore().updateGroup(syncGroup
);
2133 } catch (Exception e
) {
2134 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2138 if (tmpFile
!= null) {
2140 Files
.delete(tmpFile
.toPath());
2141 } catch (IOException e
) {
2142 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2149 if (syncMessage
.getBlockedList().isPresent()) {
2150 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2151 for (var address
: blockedListMessage
.getAddresses()) {
2152 setContactBlocked(resolveRecipient(address
), true);
2154 for (var groupId
: blockedListMessage
.getGroupIds()
2156 .map(GroupId
::unknownVersion
)
2157 .collect(Collectors
.toSet())) {
2159 setGroupBlocked(groupId
, true);
2160 } catch (GroupNotFoundException e
) {
2161 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2162 groupId
.toBase64());
2166 if (syncMessage
.getContacts().isPresent()) {
2167 File tmpFile
= null;
2169 tmpFile
= IOUtils
.createTempFile();
2170 final var contactsMessage
= syncMessage
.getContacts().get();
2171 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2172 .asPointer(), tmpFile
)) {
2173 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2178 } catch (IOException e
) {
2179 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2186 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2187 account
.setProfileKey(c
.getProfileKey().get());
2189 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2190 var contact
= account
.getContactStore().getContact(recipientId
);
2191 final var builder
= contact
== null
2192 ? Contact
.newBuilder()
2193 : Contact
.newBuilder(contact
);
2194 if (c
.getName().isPresent()) {
2195 builder
.withName(c
.getName().get());
2197 if (c
.getColor().isPresent()) {
2198 builder
.withColor(c
.getColor().get());
2200 if (c
.getProfileKey().isPresent()) {
2201 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2203 if (c
.getVerified().isPresent()) {
2204 final var verifiedMessage
= c
.getVerified().get();
2205 account
.getIdentityKeyStore()
2206 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2207 verifiedMessage
.getIdentityKey(),
2208 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2210 if (c
.getExpirationTimer().isPresent()) {
2211 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2213 builder
.withBlocked(c
.isBlocked());
2214 builder
.withArchived(c
.isArchived());
2215 account
.getContactStore().storeContact(recipientId
, builder
.build());
2217 if (c
.getAvatar().isPresent()) {
2218 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2222 } catch (Exception e
) {
2223 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2227 if (tmpFile
!= null) {
2229 Files
.delete(tmpFile
.toPath());
2230 } catch (IOException e
) {
2231 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2238 if (syncMessage
.getVerified().isPresent()) {
2239 final var verifiedMessage
= syncMessage
.getVerified().get();
2240 account
.getIdentityKeyStore()
2241 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2242 verifiedMessage
.getIdentityKey(),
2243 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2245 if (syncMessage
.getStickerPackOperations().isPresent()) {
2246 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2247 for (var m
: stickerPackOperationMessages
) {
2248 if (!m
.getPackId().isPresent()) {
2251 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2252 final var installed
= !m
.getType().isPresent()
2253 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
;
2255 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2256 if (m
.getPackKey().isPresent()) {
2257 if (sticker
== null) {
2258 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2261 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, m
.getPackKey().get()));
2265 if (sticker
!= null) {
2266 sticker
.setInstalled(installed
);
2267 account
.getStickerStore().updateSticker(sticker
);
2271 if (syncMessage
.getFetchType().isPresent()) {
2272 switch (syncMessage
.getFetchType().get()) {
2274 getRecipientProfile(account
.getSelfRecipientId(), true);
2275 case STORAGE_MANIFEST
:
2279 if (syncMessage
.getKeys().isPresent()) {
2280 final var keysMessage
= syncMessage
.getKeys().get();
2281 if (keysMessage
.getStorageService().isPresent()) {
2282 final var storageKey
= keysMessage
.getStorageService().get();
2283 account
.setStorageKey(storageKey
);
2286 if (syncMessage
.getConfiguration().isPresent()) {
2294 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2296 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2297 } catch (IOException e
) {
2298 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2302 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2304 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2305 } catch (IOException e
) {
2306 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2310 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2312 avatarStore
.storeGroupAvatar(groupId
,
2313 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2314 } catch (IOException e
) {
2315 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2319 private void downloadProfileAvatar(
2320 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2323 avatarStore
.storeProfileAvatar(address
,
2324 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2325 } catch (Throwable e
) {
2326 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2327 Thread
.currentThread().interrupt();
2329 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2333 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2334 return attachmentStore
.getAttachmentFile(attachmentId
);
2337 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2338 if (!attachment
.isPointer()) {
2339 logger
.warn("Invalid state, can't store an attachment stream.");
2342 var pointer
= attachment
.asPointer();
2343 if (pointer
.getPreview().isPresent()) {
2344 final var preview
= pointer
.getPreview().get();
2346 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2347 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2348 } catch (IOException e
) {
2349 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2354 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2355 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2356 } catch (IOException e
) {
2357 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2361 private void retrieveGroupV2Avatar(
2362 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2363 ) throws IOException
{
2364 var groupOperations
= dependencies
.getGroupsV2Operations().forGroup(groupSecretParams
);
2366 var tmpFile
= IOUtils
.createTempFile();
2367 try (InputStream input
= dependencies
.getMessageReceiver()
2368 .retrieveGroupsV2ProfileAvatar(cdnKey
, tmpFile
, ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2369 var encryptedData
= IOUtils
.readFully(input
);
2371 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2372 outputStream
.write(decryptedData
);
2375 Files
.delete(tmpFile
.toPath());
2376 } catch (IOException e
) {
2377 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2384 private void retrieveProfileAvatar(
2385 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2386 ) throws IOException
{
2387 var tmpFile
= IOUtils
.createTempFile();
2388 try (var input
= dependencies
.getMessageReceiver()
2389 .retrieveProfileAvatar(avatarPath
,
2392 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2393 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2394 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2397 Files
.delete(tmpFile
.toPath());
2398 } catch (IOException e
) {
2399 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2406 private void retrieveAttachment(
2407 final SignalServiceAttachment attachment
, final OutputStream outputStream
2408 ) throws IOException
{
2409 if (attachment
.isPointer()) {
2410 var pointer
= attachment
.asPointer();
2411 retrieveAttachmentPointer(pointer
, outputStream
);
2413 var stream
= attachment
.asStream();
2414 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2418 private void retrieveAttachmentPointer(
2419 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2420 ) throws IOException
{
2421 var tmpFile
= IOUtils
.createTempFile();
2422 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2423 IOUtils
.copyStream(input
, outputStream
);
2424 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2425 throw new IOException(e
);
2428 Files
.delete(tmpFile
.toPath());
2429 } catch (IOException e
) {
2430 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2437 private InputStream
retrieveAttachmentAsStream(
2438 SignalServiceAttachmentPointer pointer
, File tmpFile
2439 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2440 return dependencies
.getMessageReceiver()
2441 .retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2444 void sendGroups() throws IOException
{
2445 var groupsFile
= IOUtils
.createTempFile();
2448 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2449 var out
= new DeviceGroupsOutputStream(fos
);
2450 for (var record : getGroups()) {
2451 if (record instanceof GroupInfoV1
) {
2452 var groupInfo
= (GroupInfoV1
) record;
2453 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2454 Optional
.fromNullable(groupInfo
.name
),
2455 groupInfo
.getMembers()
2457 .map(this::resolveSignalServiceAddress
)
2458 .collect(Collectors
.toList()),
2459 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2460 groupInfo
.isMember(account
.getSelfRecipientId()),
2461 Optional
.of(groupInfo
.messageExpirationTime
),
2462 Optional
.fromNullable(groupInfo
.color
),
2465 groupInfo
.archived
));
2470 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2471 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2472 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2473 .withStream(groupsFileStream
)
2474 .withContentType("application/octet-stream")
2475 .withLength(groupsFile
.length())
2478 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2483 Files
.delete(groupsFile
.toPath());
2484 } catch (IOException e
) {
2485 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2490 public void sendContacts() throws IOException
{
2491 var contactsFile
= IOUtils
.createTempFile();
2494 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2495 var out
= new DeviceContactsOutputStream(fos
);
2496 for (var contactPair
: account
.getContactStore().getContacts()) {
2497 final var recipientId
= contactPair
.first();
2498 final var contact
= contactPair
.second();
2499 final var address
= resolveSignalServiceAddress(recipientId
);
2501 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2502 VerifiedMessage verifiedMessage
= null;
2503 if (currentIdentity
!= null) {
2504 verifiedMessage
= new VerifiedMessage(address
,
2505 currentIdentity
.getIdentityKey(),
2506 currentIdentity
.getTrustLevel().toVerifiedState(),
2507 currentIdentity
.getDateAdded().getTime());
2510 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2511 out
.write(new DeviceContact(address
,
2512 Optional
.fromNullable(contact
.getName()),
2513 createContactAvatarAttachment(address
),
2514 Optional
.fromNullable(contact
.getColor()),
2515 Optional
.fromNullable(verifiedMessage
),
2516 Optional
.fromNullable(profileKey
),
2517 contact
.isBlocked(),
2518 Optional
.of(contact
.getMessageExpirationTime()),
2520 contact
.isArchived()));
2523 if (account
.getProfileKey() != null) {
2524 // Send our own profile key as well
2525 out
.write(new DeviceContact(account
.getSelfAddress(),
2530 Optional
.of(account
.getProfileKey()),
2538 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2539 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2540 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2541 .withStream(contactsFileStream
)
2542 .withContentType("application/octet-stream")
2543 .withLength(contactsFile
.length())
2546 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
,
2552 Files
.delete(contactsFile
.toPath());
2553 } catch (IOException e
) {
2554 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2559 void sendBlockedList() throws IOException
{
2560 var addresses
= new ArrayList
<SignalServiceAddress
>();
2561 for (var record : account
.getContactStore().getContacts()) {
2562 if (record.second().isBlocked()) {
2563 addresses
.add(resolveSignalServiceAddress(record.first()));
2566 var groupIds
= new ArrayList
<byte[]>();
2567 for (var record : getGroups()) {
2568 if (record.isBlocked()) {
2569 groupIds
.add(record.getGroupId().serialize());
2572 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2575 private void sendVerifiedMessage(
2576 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2577 ) throws IOException
{
2578 var verifiedMessage
= new VerifiedMessage(destination
,
2580 trustLevel
.toVerifiedState(),
2581 System
.currentTimeMillis());
2582 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2585 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2586 return account
.getContactStore().getContacts();
2589 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2590 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2591 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2592 if (recipient
== null) {
2596 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2597 return recipient
.getContact().getName();
2600 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2601 return recipient
.getProfile().getDisplayName();
2607 public GroupInfo
getGroup(GroupId groupId
) {
2608 return getGroup(groupId
, false);
2611 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2612 final var group
= account
.getGroupStore().getGroup(groupId
);
2613 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2614 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2615 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2616 account
.getGroupStore().updateGroup(group
);
2621 public List
<IdentityInfo
> getIdentities() {
2622 return account
.getIdentityKeyStore().getIdentities();
2625 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2626 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2627 return identity
== null ? List
.of() : List
.of(identity
);
2631 * Trust this the identity with this fingerprint
2633 * @param name username of the identity
2634 * @param fingerprint Fingerprint
2636 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2637 var recipientId
= canonicalizeAndResolveRecipient(name
);
2638 return trustIdentity(recipientId
,
2639 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2640 TrustLevel
.TRUSTED_VERIFIED
);
2644 * Trust this the identity with this safety number
2646 * @param name username of the identity
2647 * @param safetyNumber Safety number
2649 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2650 var recipientId
= canonicalizeAndResolveRecipient(name
);
2651 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2652 return trustIdentity(recipientId
,
2653 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2654 TrustLevel
.TRUSTED_VERIFIED
);
2658 * Trust all keys of this identity without verification
2660 * @param name username of the identity
2662 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2663 var recipientId
= canonicalizeAndResolveRecipient(name
);
2664 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2667 private boolean trustIdentity(
2668 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2670 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2671 if (identity
== null) {
2675 if (!verifier
.apply(identity
.getIdentityKey())) {
2679 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2681 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2682 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2683 } catch (IOException e
) {
2684 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2690 private void handleIdentityFailure(
2691 final RecipientId recipientId
, final SendMessageResult
.IdentityFailure identityFailure
2693 final var identityKey
= identityFailure
.getIdentityKey();
2694 if (identityKey
!= null) {
2695 final var newIdentity
= account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
2697 account
.getSessionStore().archiveSessions(recipientId
);
2700 // Retrieve profile to get the current identity key from the server
2701 retrieveEncryptedProfile(recipientId
);
2705 public String
computeSafetyNumber(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2706 final var fingerprint
= Utils
.computeSafetyNumber(capabilities
.isUuid(),
2707 account
.getSelfAddress(),
2708 getIdentityKeyPair().getPublicKey(),
2711 return fingerprint
== null ?
null : fingerprint
.getDisplayableFingerprint().getDisplayText();
2714 public byte[] computeSafetyNumberForScanning(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2715 final var fingerprint
= Utils
.computeSafetyNumber(capabilities
.isUuid(),
2716 account
.getSelfAddress(),
2717 getIdentityKeyPair().getPublicKey(),
2720 return fingerprint
== null ?
null : fingerprint
.getScannableFingerprint().getSerialized();
2724 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2725 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2727 return resolveSignalServiceAddress(address
);
2731 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2732 if (address
.matches(account
.getSelfAddress())) {
2733 return account
.getSelfAddress();
2736 return account
.getRecipientStore().resolveServiceAddress(address
);
2739 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2740 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2743 private RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2744 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
) ? identifier
: canonicalizePhoneNumber(identifier
);
2746 return resolveRecipient(canonicalizedNumber
);
2749 private String
canonicalizePhoneNumber(final String number
) throws InvalidNumberException
{
2750 return PhoneNumberFormatter
.formatNumber(number
, account
.getUsername());
2753 private RecipientId
resolveRecipient(final String identifier
) {
2754 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2756 return resolveRecipient(address
);
2759 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2760 return account
.getRecipientStore().resolveRecipient(address
);
2763 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2764 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2767 private void enqueueJob(Job job
) {
2768 var context
= new Context(account
,
2769 dependencies
.getAccountManager(),
2770 dependencies
.getMessageReceiver(),
2776 public void close() throws IOException
{
2780 void close(boolean closeAccount
) throws IOException
{
2781 executor
.shutdown();
2783 dependencies
.getSignalWebSocket().disconnect();
2785 if (closeAccount
&& account
!= null) {
2791 public interface ReceiveMessageHandler
{
2793 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);