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
.config
.ServiceConfig
;
21 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
22 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
23 import org
.asamk
.signal
.manager
.groups
.GroupId
;
24 import org
.asamk
.signal
.manager
.groups
.GroupIdV1
;
25 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
26 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
27 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
28 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
29 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
30 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
31 import org
.asamk
.signal
.manager
.helper
.GroupV2Helper
;
32 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
33 import org
.asamk
.signal
.manager
.helper
.ProfileHelper
;
34 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
35 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
36 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
37 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
38 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
39 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
40 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
41 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
42 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
43 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
44 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
45 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
46 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
47 import org
.asamk
.signal
.manager
.util
.IOUtils
;
48 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
49 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
50 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
51 import org
.asamk
.signal
.manager
.util
.Utils
;
52 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
53 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
54 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
55 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
56 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
57 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
58 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
59 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
60 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
61 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
62 import org
.signal
.libsignal
.metadata
.SelfSendException
;
63 import org
.signal
.libsignal
.metadata
.certificate
.CertificateValidator
;
64 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
65 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
66 import org
.signal
.zkgroup
.InvalidInputException
;
67 import org
.signal
.zkgroup
.VerificationFailedException
;
68 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
69 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
70 import org
.signal
.zkgroup
.profiles
.ClientZkProfileOperations
;
71 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
72 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
73 import org
.slf4j
.Logger
;
74 import org
.slf4j
.LoggerFactory
;
75 import org
.whispersystems
.libsignal
.IdentityKey
;
76 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
77 import org
.whispersystems
.libsignal
.InvalidKeyException
;
78 import org
.whispersystems
.libsignal
.InvalidMessageException
;
79 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
80 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
81 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
82 import org
.whispersystems
.libsignal
.util
.Pair
;
83 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
84 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
85 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
86 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
87 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
88 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
89 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
90 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
91 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
92 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
93 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Api
;
94 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
95 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
96 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
97 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
98 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
99 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
100 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
101 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
102 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
103 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
104 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
105 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupV2
;
106 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
107 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
108 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
109 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
110 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
111 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
112 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
113 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
114 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
117 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
118 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
119 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
120 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
121 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
122 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
123 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
124 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
125 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
126 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
127 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
128 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
129 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
130 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
131 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
132 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
133 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
134 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
135 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
136 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
137 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
138 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
140 import java
.io
.Closeable
;
142 import java
.io
.FileInputStream
;
143 import java
.io
.FileOutputStream
;
144 import java
.io
.IOException
;
145 import java
.io
.InputStream
;
146 import java
.io
.OutputStream
;
148 import java
.net
.URISyntaxException
;
149 import java
.net
.URLEncoder
;
150 import java
.nio
.charset
.StandardCharsets
;
151 import java
.nio
.file
.Files
;
152 import java
.security
.SignatureException
;
153 import java
.util
.ArrayList
;
154 import java
.util
.Arrays
;
155 import java
.util
.Base64
;
156 import java
.util
.Collection
;
157 import java
.util
.Date
;
158 import java
.util
.HashSet
;
159 import java
.util
.List
;
160 import java
.util
.Map
;
161 import java
.util
.Set
;
162 import java
.util
.UUID
;
163 import java
.util
.concurrent
.ExecutorService
;
164 import java
.util
.concurrent
.Executors
;
165 import java
.util
.concurrent
.TimeUnit
;
166 import java
.util
.concurrent
.TimeoutException
;
167 import java
.util
.concurrent
.locks
.ReentrantLock
;
168 import java
.util
.function
.Function
;
169 import java
.util
.stream
.Collectors
;
171 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
173 public class Manager
implements Closeable
{
175 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
177 private final CertificateValidator certificateValidator
;
179 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
180 private final String userAgent
;
182 private SignalAccount account
;
183 private final SignalServiceAccountManager accountManager
;
184 private final GroupsV2Api groupsV2Api
;
185 private final GroupsV2Operations groupsV2Operations
;
186 private final SignalServiceMessageReceiver messageReceiver
;
187 private final ClientZkProfileOperations clientZkProfileOperations
;
189 private final ExecutorService executor
= Executors
.newCachedThreadPool();
191 private SignalServiceMessagePipe messagePipe
= null;
192 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
194 private final UnidentifiedAccessHelper unidentifiedAccessHelper
;
195 private final ProfileHelper profileHelper
;
196 private final GroupV2Helper groupV2Helper
;
197 private final PinHelper pinHelper
;
198 private final AvatarStore avatarStore
;
199 private final AttachmentStore attachmentStore
;
200 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
201 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
204 public Lock
acquire() {
206 return LEGACY_LOCK
::unlock
;
211 SignalAccount account
,
212 PathConfig pathConfig
,
213 ServiceEnvironmentConfig serviceEnvironmentConfig
,
216 this.account
= account
;
217 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
218 this.certificateValidator
= new CertificateValidator(serviceEnvironmentConfig
.getUnidentifiedSenderTrustRoot());
219 this.userAgent
= userAgent
;
220 this.groupsV2Operations
= capabilities
.isGv2() ?
new GroupsV2Operations(ClientZkOperations
.create(
221 serviceEnvironmentConfig
.getSignalServiceConfiguration())) : null;
222 final SleepTimer timer
= new UptimeSleepTimer();
223 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
224 new DynamicCredentialsProvider(account
.getUuid(),
225 account
.getUsername(),
226 account
.getPassword(),
227 account
.getDeviceId()),
230 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
232 this.groupsV2Api
= accountManager
.getGroupsV2Api();
233 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
234 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
235 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
236 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
239 this.pinHelper
= new PinHelper(keyBackupService
);
240 this.clientZkProfileOperations
= capabilities
.isGv2()
241 ? ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration())
242 .getProfileOperations()
244 this.messageReceiver
= new SignalServiceMessageReceiver(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
246 account
.getUsername(),
247 account
.getPassword(),
248 account
.getDeviceId(),
252 clientZkProfileOperations
,
253 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
255 this.unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
256 account
.getProfileStore()::getProfileKey
,
257 this::getRecipientProfile
,
258 this::getSenderCertificate
);
259 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
260 unidentifiedAccessHelper
::getAccessFor
,
261 unidentified
-> unidentified ?
getOrCreateUnidentifiedMessagePipe() : getOrCreateMessagePipe(),
262 () -> messageReceiver
,
263 this::resolveSignalServiceAddress
);
264 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
265 this::getRecipientProfile
,
266 account
::getSelfRecipientId
,
269 this::getGroupAuthForToday
,
270 this::resolveSignalServiceAddress
);
271 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
272 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
275 public String
getUsername() {
276 return account
.getUsername();
279 public SignalServiceAddress
getSelfAddress() {
280 return account
.getSelfAddress();
283 public RecipientId
getSelfRecipientId() {
284 return account
.getSelfRecipientId();
287 private IdentityKeyPair
getIdentityKeyPair() {
288 return account
.getIdentityKeyPair();
291 public int getDeviceId() {
292 return account
.getDeviceId();
295 public static Manager
init(
296 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
297 ) throws IOException
, NotRegisteredException
{
298 var pathConfig
= PathConfig
.createDefault(settingsPath
);
300 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
301 throw new NotRegisteredException();
304 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
306 if (!account
.isRegistered()) {
307 throw new NotRegisteredException();
310 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
312 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
315 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
316 var pathConfig
= PathConfig
.createDefault(settingsPath
);
317 final var dataPath
= pathConfig
.getDataPath();
318 final var files
= dataPath
.listFiles();
324 return Arrays
.stream(files
)
325 .filter(File
::isFile
)
327 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
328 .collect(Collectors
.toList());
331 public void checkAccountState() throws IOException
{
332 if (accountManager
.getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
335 if (account
.getUuid() == null) {
336 account
.setUuid(accountManager
.getOwnUuid());
338 updateAccountAttributes();
342 * This is used for checking a set of phone numbers for registration on Signal
344 * @param numbers The set of phone number in question
345 * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
346 * @throws IOException if its unable to get the contacts to check if they're registered
348 public Map
<String
, Boolean
> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
349 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
350 var contactDetails
= getRegisteredUsers(numbers
);
352 var registeredUsers
= contactDetails
.keySet();
354 return numbers
.stream().collect(Collectors
.toMap(x
-> x
, registeredUsers
::contains
));
357 public void updateAccountAttributes() throws IOException
{
358 accountManager
.setAccountAttributes(account
.getEncryptedDeviceName(),
360 account
.getLocalRegistrationId(),
362 // set legacy pin only if no KBS master key is set
363 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
364 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
365 account
.getSelfUnidentifiedAccessKey(),
366 account
.isUnrestrictedUnidentifiedAccess(),
368 account
.isDiscoverableByPhoneNumber());
372 * @param givenName if null, the previous givenName will be kept
373 * @param familyName if null, the previous familyName will be kept
374 * @param about if null, the previous about text will be kept
375 * @param aboutEmoji if null, the previous about emoji will be kept
376 * @param avatar if avatar is null the image from the local avatar store is used (if present),
378 public void setProfile(
379 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
380 ) throws IOException
{
381 var profile
= getRecipientProfile(account
.getSelfRecipientId());
382 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
383 if (givenName
!= null) {
384 builder
.withGivenName(givenName
);
386 if (familyName
!= null) {
387 builder
.withFamilyName(familyName
);
390 builder
.withAbout(about
);
392 if (aboutEmoji
!= null) {
393 builder
.withAboutEmoji(aboutEmoji
);
395 var newProfile
= builder
.build();
397 try (final var streamDetails
= avatar
== null
398 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
399 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
400 accountManager
.setVersionedProfile(account
.getUuid(),
401 account
.getProfileKey(),
402 newProfile
.getInternalServiceName(),
403 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
404 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
409 if (avatar
!= null) {
410 if (avatar
.isPresent()) {
411 avatarStore
.storeProfileAvatar(getSelfAddress(),
412 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
414 avatarStore
.deleteProfileAvatar(getSelfAddress());
417 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
420 sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
421 } catch (UntrustedIdentityException ignored
) {
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 accountManager
.setGcmId(Optional
.absent());
431 account
.setRegistered(false);
434 public void deleteAccount() throws IOException
{
435 accountManager
.deleteAccount();
437 account
.setRegistered(false);
440 public List
<Device
> getLinkedDevices() throws IOException
{
441 var devices
= accountManager
.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 accountManager
.removeDevice(deviceId
);
459 var devices
= accountManager
.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
= accountManager
.getNewDeviceVerificationCode();
473 accountManager
.addDevice(deviceIdentifier
,
476 Optional
.of(account
.getProfileKey().serialize()),
478 account
.setMultiDevice(true);
481 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
482 if (!account
.isMasterDevice()) {
483 throw new RuntimeException("Only master device can set a PIN");
485 if (pin
.isPresent()) {
486 final var masterKey
= account
.getPinMasterKey() != null
487 ? account
.getPinMasterKey()
488 : KeyUtils
.createMasterKey();
490 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
492 account
.setRegistrationLockPin(pin
.get(), masterKey
);
494 // Remove legacy registration lock
495 accountManager
.removeRegistrationLockV1();
498 pinHelper
.removeRegistrationLockPin();
500 account
.setRegistrationLockPin(null, null);
504 void refreshPreKeys() throws IOException
{
505 var oneTimePreKeys
= generatePreKeys();
506 final var identityKeyPair
= getIdentityKeyPair();
507 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
509 accountManager
.setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
512 private List
<PreKeyRecord
> generatePreKeys() {
513 final var offset
= account
.getPreKeyIdOffset();
515 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
516 account
.addPreKeys(records
);
521 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
522 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
524 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
525 account
.addSignedPreKey(record);
530 private SignalServiceMessagePipe
getOrCreateMessagePipe() {
531 if (messagePipe
== null) {
532 messagePipe
= messageReceiver
.createMessagePipe();
537 private SignalServiceMessagePipe
getOrCreateUnidentifiedMessagePipe() {
538 if (unidentifiedMessagePipe
== null) {
539 unidentifiedMessagePipe
= messageReceiver
.createUnidentifiedMessagePipe();
541 return unidentifiedMessagePipe
;
544 private SignalServiceMessageSender
createMessageSender() {
545 return new SignalServiceMessageSender(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
547 account
.getUsername(),
548 account
.getPassword(),
549 account
.getDeviceId(),
550 account
.getSignalProtocolStore(),
553 account
.isMultiDevice(),
554 Optional
.fromNullable(messagePipe
),
555 Optional
.fromNullable(unidentifiedMessagePipe
),
557 clientZkProfileOperations
,
559 ServiceConfig
.MAX_ENVELOPE_SIZE
,
560 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
563 public Profile
getRecipientProfile(
564 RecipientId recipientId
566 return getRecipientProfile(recipientId
, false);
569 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
571 Profile
getRecipientProfile(
572 RecipientId recipientId
, boolean force
574 var profile
= account
.getProfileStore().getProfile(recipientId
);
576 var now
= new Date().getTime();
577 // Profiles are cached for 24h before retrieving them again, unless forced
578 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
582 synchronized (pendingProfileRequest
) {
583 if (pendingProfileRequest
.contains(recipientId
)) {
586 pendingProfileRequest
.add(recipientId
);
588 final SignalServiceProfile encryptedProfile
;
590 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
592 synchronized (pendingProfileRequest
) {
593 pendingProfileRequest
.remove(recipientId
);
596 if (encryptedProfile
== null) {
600 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
601 if (profileKey
== null) {
602 profile
= new Profile(new Date().getTime(),
607 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
608 ProfileUtils
.getCapabilities(encryptedProfile
));
610 profile
= decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
612 account
.getProfileStore().storeProfile(recipientId
, profile
);
617 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
619 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
620 } catch (IOException e
) {
621 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
626 private ProfileAndCredential
retrieveProfileAndCredential(
627 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
628 ) throws IOException
{
629 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
630 final var profile
= profileAndCredential
.getProfile();
633 var newIdentity
= account
.getIdentityKeyStore()
634 .saveIdentity(recipientId
,
635 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
639 account
.getSessionStore().archiveSessions(recipientId
);
641 } catch (InvalidKeyException ignored
) {
642 logger
.warn("Got invalid identity key in profile for {}",
643 resolveSignalServiceAddress(recipientId
).getLegacyIdentifier());
645 return profileAndCredential
;
648 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
649 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
650 if (profileKeyCredential
!= null) {
651 return profileKeyCredential
;
654 ProfileAndCredential profileAndCredential
;
656 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
657 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
658 } catch (IOException e
) {
659 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
663 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
664 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
666 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
667 if (profileKey
!= null) {
668 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
670 profileAndCredential
.getProfile());
671 account
.getProfileStore().storeProfile(recipientId
, profile
);
674 return profileKeyCredential
;
677 private Profile
decryptProfileAndDownloadAvatar(
678 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
680 if (encryptedProfile
.getAvatar() != null) {
681 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
684 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
687 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
688 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
689 if (streamDetails
== null) {
690 return Optional
.absent();
693 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
696 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
697 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
698 if (streamDetails
== null) {
699 return Optional
.absent();
702 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
705 private GroupInfo
getGroupForSending(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
706 var g
= getGroup(groupId
);
708 throw new GroupNotFoundException(groupId
);
710 if (!g
.isMember(account
.getSelfRecipientId())) {
711 throw new NotAGroupMemberException(groupId
, g
.getTitle());
716 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
717 var g
= getGroup(groupId
);
719 throw new GroupNotFoundException(groupId
);
721 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
722 throw new NotAGroupMemberException(groupId
, g
.getTitle());
727 public List
<GroupInfo
> getGroups() {
728 return account
.getGroupStore().getGroups();
731 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
732 String messageText
, List
<String
> attachments
, GroupId groupId
733 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
734 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
735 if (attachments
!= null) {
736 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
739 return sendGroupMessage(messageBuilder
, groupId
);
742 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
743 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
744 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
745 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
746 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
748 resolveSignalServiceAddress(targetAuthorRecipientId
),
749 targetSentTimestamp
);
750 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
752 return sendGroupMessage(messageBuilder
, groupId
);
755 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
756 SignalServiceDataMessage
.Builder messageBuilder
, GroupId groupId
757 ) throws IOException
, GroupNotFoundException
, NotAGroupMemberException
{
758 final var g
= getGroupForSending(groupId
);
760 GroupUtils
.setGroupContext(messageBuilder
, g
);
761 messageBuilder
.withExpiration(g
.getMessageExpirationTime());
763 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
766 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(GroupId groupId
) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
{
767 SignalServiceDataMessage
.Builder messageBuilder
;
769 final var g
= getGroupForUpdating(groupId
);
770 if (g
instanceof GroupInfoV1
) {
771 var groupInfoV1
= (GroupInfoV1
) g
;
772 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
).withId(groupId
.serialize()).build();
773 messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
774 groupInfoV1
.removeMember(account
.getSelfRecipientId());
775 account
.getGroupStore().updateGroup(groupInfoV1
);
777 final var groupInfoV2
= (GroupInfoV2
) g
;
778 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
);
779 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
780 messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
781 account
.getGroupStore().updateGroup(groupInfoV2
);
784 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
787 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
788 String name
, List
<String
> members
, File avatarFile
789 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
790 return createGroup(name
, members
== null ?
null : getSignalServiceAddresses(members
), avatarFile
);
793 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
794 String name
, Set
<RecipientId
> members
, File avatarFile
795 ) throws IOException
, AttachmentInvalidException
{
796 final var selfRecipientId
= account
.getSelfRecipientId();
797 if (members
!= null && members
.contains(selfRecipientId
)) {
798 members
= new HashSet
<>(members
);
799 members
.remove(selfRecipientId
);
802 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
803 members
== null ? Set
.of() : members
,
806 SignalServiceDataMessage
.Builder messageBuilder
;
807 if (gv2Pair
== null) {
808 // Failed to create v2 group, creating v1 group instead
809 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
810 gv1
.addMembers(List
.of(selfRecipientId
));
811 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
812 return new Pair
<>(gv1
.getGroupId(), result
.second());
815 final var gv2
= gv2Pair
.first();
816 final var decryptedGroup
= gv2Pair
.second();
818 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
819 if (avatarFile
!= null) {
820 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
821 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
823 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
824 account
.getGroupStore().updateGroup(gv2
);
826 final var result
= sendMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
827 return new Pair
<>(gv2
.getGroupId(), result
.second());
830 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
834 List
<String
> members
,
835 List
<String
> removeMembers
,
837 List
<String
> removeAdmins
,
838 boolean resetGroupLink
,
839 GroupLinkState groupLinkState
,
840 GroupPermission addMemberPermission
,
841 GroupPermission editDetailsPermission
,
843 Integer expirationTimer
844 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
845 return updateGroup(groupId
,
848 members
== null ?
null : getSignalServiceAddresses(members
),
849 removeMembers
== null ?
null : getSignalServiceAddresses(removeMembers
),
850 admins
== null ?
null : getSignalServiceAddresses(admins
),
851 removeAdmins
== null ?
null : getSignalServiceAddresses(removeAdmins
),
855 editDetailsPermission
,
860 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
861 final GroupId groupId
,
863 final String description
,
864 final Set
<RecipientId
> members
,
865 final Set
<RecipientId
> removeMembers
,
866 final Set
<RecipientId
> admins
,
867 final Set
<RecipientId
> removeAdmins
,
868 final boolean resetGroupLink
,
869 final GroupLinkState groupLinkState
,
870 final GroupPermission addMemberPermission
,
871 final GroupPermission editDetailsPermission
,
872 final File avatarFile
,
873 final Integer expirationTimer
874 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
875 var group
= getGroupForUpdating(groupId
);
877 if (group
instanceof GroupInfoV2
) {
878 return updateGroupV2((GroupInfoV2
) group
,
888 editDetailsPermission
,
893 final var gv1
= (GroupInfoV1
) group
;
894 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
895 if (expirationTimer
!= null) {
896 setExpirationTimer(gv1
, expirationTimer
);
901 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
902 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
903 ) throws IOException
, AttachmentInvalidException
{
904 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
905 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
907 account
.getGroupStore().updateGroup(gv1
);
909 return sendMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
912 private void updateGroupV1Details(
913 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
914 ) throws IOException
{
919 if (members
!= null) {
920 final var newMemberAddresses
= members
.stream()
921 .filter(member
-> !g
.isMember(member
))
922 .map(this::resolveSignalServiceAddress
)
923 .collect(Collectors
.toList());
924 final var newE164Members
= new HashSet
<String
>();
925 for (var member
: newMemberAddresses
) {
926 if (!member
.getNumber().isPresent()) {
929 newE164Members
.add(member
.getNumber().get());
932 final var registeredUsers
= getRegisteredUsers(newE164Members
);
933 if (registeredUsers
.size() != newE164Members
.size()) {
934 // Some of the new members are not registered on Signal
935 newE164Members
.removeAll(registeredUsers
.keySet());
936 throw new IOException("Failed to add members "
937 + String
.join(", ", newE164Members
)
938 + " to group: Not registered on Signal");
941 g
.addMembers(members
);
944 if (avatarFile
!= null) {
945 avatarStore
.storeGroupAvatar(g
.getGroupId(),
946 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
950 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
951 final GroupInfoV2 group
,
953 final String description
,
954 final Set
<RecipientId
> members
,
955 final Set
<RecipientId
> removeMembers
,
956 final Set
<RecipientId
> admins
,
957 final Set
<RecipientId
> removeAdmins
,
958 final boolean resetGroupLink
,
959 final GroupLinkState groupLinkState
,
960 final GroupPermission addMemberPermission
,
961 final GroupPermission editDetailsPermission
,
962 final File avatarFile
,
963 Integer expirationTimer
964 ) throws IOException
{
965 Pair
<Long
, List
<SendMessageResult
>> result
= null;
966 if (group
.isPendingMember(account
.getSelfRecipientId())) {
967 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
968 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
971 if (members
!= null) {
972 final var newMembers
= new HashSet
<>(members
);
973 newMembers
.removeAll(group
.getMembers());
974 if (newMembers
.size() > 0) {
975 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
976 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
980 if (removeMembers
!= null) {
981 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
982 existingRemoveMembers
.retainAll(group
.getMembers());
983 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
984 if (existingRemoveMembers
.size() > 0) {
985 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
986 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
989 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
990 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
991 if (pendingRemoveMembers
.size() > 0) {
992 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
993 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
997 if (admins
!= null) {
998 final var newAdmins
= new HashSet
<>(admins
);
999 newAdmins
.retainAll(group
.getMembers());
1000 newAdmins
.removeAll(group
.getAdminMembers());
1001 if (newAdmins
.size() > 0) {
1002 for (var admin
: newAdmins
) {
1003 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1004 result
= sendUpdateGroupV2Message(group
,
1005 groupGroupChangePair
.first(),
1006 groupGroupChangePair
.second());
1011 if (removeAdmins
!= null) {
1012 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1013 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1014 if (existingRemoveAdmins
.size() > 0) {
1015 for (var admin
: existingRemoveAdmins
) {
1016 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1017 result
= sendUpdateGroupV2Message(group
,
1018 groupGroupChangePair
.first(),
1019 groupGroupChangePair
.second());
1024 if (resetGroupLink
) {
1025 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1026 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1029 if (groupLinkState
!= null) {
1030 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1031 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1034 if (addMemberPermission
!= null) {
1035 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1036 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1039 if (editDetailsPermission
!= null) {
1040 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1041 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1044 if (expirationTimer
!= null) {
1045 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1046 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1049 if (result
== null || name
!= null || description
!= null || avatarFile
!= null) {
1050 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1051 if (avatarFile
!= null) {
1052 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1053 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1055 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1061 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1062 GroupInviteLinkUrl inviteLinkUrl
1063 ) throws IOException
, GroupLinkNotActiveException
{
1064 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1065 inviteLinkUrl
.getPassword());
1066 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1067 inviteLinkUrl
.getPassword(),
1069 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1070 groupJoinInfo
.getRevision() + 1,
1071 groupChange
.toByteArray());
1073 if (group
.getGroup() == null) {
1074 // Only requested member, can't send update to group members
1075 return new Pair
<>(group
.getGroupId(), List
.of());
1078 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1080 return new Pair
<>(group
.getGroupId(), result
.second());
1083 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1084 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1085 ) throws IOException
{
1086 final var selfRecipientId
= account
.getSelfRecipientId();
1087 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1088 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1089 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1091 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1092 account
.getGroupStore().updateGroup(group
);
1093 return sendMessage(messageBuilder
, members
);
1096 private static int currentTimeDays() {
1097 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1100 private GroupsV2AuthorizationString
getGroupAuthForToday(
1101 final GroupSecretParams groupSecretParams
1102 ) throws IOException
{
1103 final var today
= currentTimeDays();
1104 // Returns credentials for the next 7 days
1105 final var credentials
= groupsV2Api
.getCredentials(today
);
1106 // TODO cache credentials until they expire
1107 var authCredentialResponse
= credentials
.get(today
);
1109 return groupsV2Api
.getGroupsV2AuthorizationString(account
.getUuid(),
1112 authCredentialResponse
);
1113 } catch (VerificationFailedException e
) {
1114 throw new IOException(e
);
1118 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1119 GroupIdV1 groupId
, SignalServiceAddress recipient
1120 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1122 var group
= getGroupForSending(groupId
);
1123 if (!(group
instanceof GroupInfoV1
)) {
1124 throw new RuntimeException("Received an invalid group request for a v2 group!");
1126 g
= (GroupInfoV1
) group
;
1128 final var recipientId
= resolveRecipient(recipient
);
1129 if (!g
.isMember(recipientId
)) {
1130 throw new NotAGroupMemberException(groupId
, g
.name
);
1133 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1135 // Send group message only to the recipient who requested it
1136 return sendMessage(messageBuilder
, Set
.of(recipientId
));
1139 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1140 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1141 .withId(g
.getGroupId().serialize())
1143 .withMembers(g
.getMembers()
1145 .map(this::resolveSignalServiceAddress
)
1146 .collect(Collectors
.toList()));
1149 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1150 if (attachment
.isPresent()) {
1151 group
.withAvatar(attachment
.get());
1153 } catch (IOException e
) {
1154 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1157 return SignalServiceDataMessage
.newBuilder()
1158 .asGroupMessage(group
.build())
1159 .withExpiration(g
.getMessageExpirationTime());
1162 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1163 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1164 .withRevision(g
.getGroup().getRevision())
1165 .withSignedGroupChange(signedGroupChange
);
1166 return SignalServiceDataMessage
.newBuilder()
1167 .asGroupMessage(group
.build())
1168 .withExpiration(g
.getMessageExpirationTime());
1171 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1172 GroupIdV1 groupId
, SignalServiceAddress recipient
1173 ) throws IOException
{
1174 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1176 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1178 // Send group info request message to the recipient who sent us a message with this groupId
1179 return sendMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1183 SignalServiceAddress remoteAddress
, long messageId
1184 ) throws IOException
, UntrustedIdentityException
{
1185 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1187 System
.currentTimeMillis());
1189 createMessageSender().sendReceipt(remoteAddress
,
1190 unidentifiedAccessHelper
.getAccessFor(resolveRecipient(remoteAddress
)),
1194 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1195 String messageText
, List
<String
> attachments
, List
<String
> recipients
1196 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1197 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1198 if (attachments
!= null) {
1199 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1201 // Upload attachments here, so we only upload once even for multiple recipients
1202 var messageSender
= createMessageSender();
1203 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1204 for (var attachment
: attachmentStreams
) {
1205 if (attachment
.isStream()) {
1206 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1207 } else if (attachment
.isPointer()) {
1208 attachmentPointers
.add(attachment
.asPointer());
1212 messageBuilder
.withAttachments(attachmentPointers
);
1214 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1217 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1218 String messageText
, List
<String
> attachments
1219 ) throws IOException
, AttachmentInvalidException
{
1220 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1221 if (attachments
!= null) {
1222 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1224 return sendSelfMessage(messageBuilder
);
1227 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1228 long targetSentTimestamp
, List
<String
> recipients
1229 ) throws IOException
, InvalidNumberException
{
1230 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1231 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1232 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1235 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1236 long targetSentTimestamp
, GroupId groupId
1237 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1238 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1239 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1240 return sendGroupMessage(messageBuilder
, groupId
);
1243 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1244 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1245 ) throws IOException
, InvalidNumberException
{
1246 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1247 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1249 resolveSignalServiceAddress(targetAuthorRecipientId
),
1250 targetSentTimestamp
);
1251 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1252 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1255 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1256 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1258 final var signalServiceAddresses
= getSignalServiceAddresses(recipients
);
1260 return sendMessage(messageBuilder
, signalServiceAddresses
);
1261 } catch (Exception e
) {
1262 for (var address
: signalServiceAddresses
) {
1263 handleEndSession(address
);
1269 void renewSession(RecipientId recipientId
) throws IOException
{
1270 account
.getSessionStore().archiveSessions(recipientId
);
1271 if (!recipientId
.equals(getSelfRecipientId())) {
1272 sendNullMessage(recipientId
);
1276 public String
getContactName(String number
) throws InvalidNumberException
{
1277 var contact
= account
.getContactStore().getContact(canonicalizeAndResolveRecipient(number
));
1278 return contact
== null || contact
.getName() == null ?
"" : contact
.getName();
1281 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1282 if (!account
.isMasterDevice()) {
1283 throw new NotMasterDeviceException();
1285 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1286 var contact
= account
.getContactStore().getContact(recipientId
);
1287 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1288 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1291 public void setContactBlocked(
1292 String number
, boolean blocked
1293 ) throws InvalidNumberException
, NotMasterDeviceException
{
1294 if (!account
.isMasterDevice()) {
1295 throw new NotMasterDeviceException();
1297 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1300 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1301 var contact
= account
.getContactStore().getContact(recipientId
);
1302 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1303 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1306 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1307 var group
= getGroup(groupId
);
1308 if (group
== null) {
1309 throw new GroupNotFoundException(groupId
);
1312 group
.setBlocked(blocked
);
1313 account
.getGroupStore().updateGroup(group
);
1316 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1317 var contact
= account
.getContactStore().getContact(recipientId
);
1318 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1321 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1322 account
.getContactStore()
1323 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1326 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1327 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1328 sendMessage(messageBuilder
, Set
.of(recipientId
));
1332 * Change the expiration timer for a contact
1334 public void setExpirationTimer(
1335 String number
, int messageExpirationTimer
1336 ) throws IOException
, InvalidNumberException
{
1337 var recipientId
= canonicalizeAndResolveRecipient(number
);
1338 setExpirationTimer(recipientId
, messageExpirationTimer
);
1339 sendExpirationTimerUpdate(recipientId
);
1343 * Change the expiration timer for a group
1345 private void setExpirationTimer(
1346 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1347 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1348 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1349 account
.getGroupStore().updateGroup(groupInfoV1
);
1350 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1353 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1354 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1355 sendGroupMessage(messageBuilder
, groupId
);
1359 * Upload the sticker pack from path.
1361 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1362 * @return if successful, returns the URL to install the sticker pack in the signal app
1364 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1365 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1367 var messageSender
= createMessageSender();
1369 var packKey
= KeyUtils
.createStickerUploadKey();
1370 var packId
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1372 var sticker
= new Sticker(StickerPackId
.deserialize(Hex
.fromStringCondensed(packId
)), packKey
);
1373 account
.getStickerStore().updateSticker(sticker
);
1376 return new URI("https",
1379 "pack_id=" + URLEncoder
.encode(packId
, StandardCharsets
.UTF_8
) + "&pack_key=" + URLEncoder
.encode(
1380 Hex
.toStringCondensed(packKey
),
1381 StandardCharsets
.UTF_8
)).toString();
1382 } catch (URISyntaxException e
) {
1383 throw new AssertionError(e
);
1387 public void requestAllSyncData() throws IOException
{
1388 requestSyncGroups();
1389 requestSyncContacts();
1390 requestSyncBlocked();
1391 requestSyncConfiguration();
1395 private void requestSyncGroups() throws IOException
{
1396 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1397 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1399 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1401 sendSyncMessage(message
);
1402 } catch (UntrustedIdentityException e
) {
1403 throw new AssertionError(e
);
1407 private void requestSyncContacts() throws IOException
{
1408 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1409 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1411 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1413 sendSyncMessage(message
);
1414 } catch (UntrustedIdentityException e
) {
1415 throw new AssertionError(e
);
1419 private void requestSyncBlocked() throws IOException
{
1420 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1421 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1423 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1425 sendSyncMessage(message
);
1426 } catch (UntrustedIdentityException e
) {
1427 throw new AssertionError(e
);
1431 private void requestSyncConfiguration() throws IOException
{
1432 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1433 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1435 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1437 sendSyncMessage(message
);
1438 } catch (UntrustedIdentityException e
) {
1439 throw new AssertionError(e
);
1443 private void requestSyncKeys() throws IOException
{
1444 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1445 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1447 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1449 sendSyncMessage(message
);
1450 } catch (UntrustedIdentityException e
) {
1451 throw new AssertionError(e
);
1455 private byte[] getSenderCertificate() {
1458 if (account
.isPhoneNumberShared()) {
1459 certificate
= accountManager
.getSenderCertificate();
1461 certificate
= accountManager
.getSenderCertificateForPhoneNumberPrivacy();
1463 } catch (IOException e
) {
1464 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1467 // TODO cache for a day
1471 private void sendSyncMessage(SignalServiceSyncMessage message
) throws IOException
, UntrustedIdentityException
{
1472 var messageSender
= createMessageSender();
1473 messageSender
.sendMessage(message
, unidentifiedAccessHelper
.getAccessForSync());
1476 private Set
<RecipientId
> getSignalServiceAddresses(Collection
<String
> numbers
) throws InvalidNumberException
{
1477 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1478 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1480 for (var number
: numbers
) {
1481 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1482 if (resolvedAddress
.getUuid().isPresent()) {
1483 signalServiceAddresses
.add(resolvedAddress
);
1485 addressesMissingUuid
.add(resolvedAddress
);
1489 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1490 .map(a
-> a
.getNumber().get())
1491 .collect(Collectors
.toSet());
1492 Map
<String
, UUID
> registeredUsers
;
1494 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1495 } catch (IOException e
) {
1496 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1497 registeredUsers
= Map
.of();
1500 for (var address
: addressesMissingUuid
) {
1501 final var number
= address
.getNumber().get();
1502 if (registeredUsers
.containsKey(number
)) {
1503 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1504 registeredUsers
.get(number
),
1506 signalServiceAddresses
.add(newAddress
);
1508 signalServiceAddresses
.add(address
);
1512 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1515 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1516 final var address
= resolveSignalServiceAddress(recipientId
);
1517 if (!address
.getNumber().isPresent()) {
1520 final var number
= address
.getNumber().get();
1521 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1522 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1525 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1527 return accountManager
.getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1529 serviceEnvironmentConfig
.getCdsMrenclave());
1530 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1531 throw new IOException(e
);
1535 private Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1536 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientId
> recipientIds
1537 ) throws IOException
{
1538 final var timestamp
= System
.currentTimeMillis();
1539 messageBuilder
.withTimestamp(timestamp
);
1540 getOrCreateMessagePipe();
1541 getOrCreateUnidentifiedMessagePipe();
1542 SignalServiceDataMessage message
= null;
1544 message
= messageBuilder
.build();
1545 if (message
.getGroupContext().isPresent()) {
1547 var messageSender
= createMessageSender();
1548 final var isRecipientUpdate
= false;
1549 final var recipientIdList
= new ArrayList
<>(recipientIds
);
1550 final var addresses
= recipientIdList
.stream()
1551 .map(this::resolveSignalServiceAddress
)
1552 .collect(Collectors
.toList());
1553 var result
= messageSender
.sendMessage(addresses
,
1554 unidentifiedAccessHelper
.getAccessFor(recipientIdList
),
1558 for (var r
: result
) {
1559 if (r
.getIdentityFailure() != null) {
1560 final var recipientId
= resolveRecipient(r
.getAddress());
1561 final var newIdentity
= account
.getIdentityKeyStore()
1562 .saveIdentity(recipientId
, r
.getIdentityFailure().getIdentityKey(), new Date());
1564 account
.getSessionStore().archiveSessions(recipientId
);
1569 return new Pair
<>(timestamp
, result
);
1570 } catch (UntrustedIdentityException e
) {
1571 return new Pair
<>(timestamp
, List
.of());
1574 // Send to all individually, so sync messages are sent correctly
1575 messageBuilder
.withProfileKey(account
.getProfileKey().serialize());
1576 var results
= new ArrayList
<SendMessageResult
>(recipientIds
.size());
1577 for (var recipientId
: recipientIds
) {
1578 final var contact
= account
.getContactStore().getContact(recipientId
);
1579 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1580 messageBuilder
.withExpiration(expirationTime
);
1581 message
= messageBuilder
.build();
1582 results
.add(sendMessage(recipientId
, message
));
1584 return new Pair
<>(timestamp
, results
);
1587 if (message
!= null && message
.isEndSession()) {
1588 for (var recipient
: recipientIds
) {
1589 handleEndSession(recipient
);
1595 private Pair
<Long
, SendMessageResult
> sendSelfMessage(
1596 SignalServiceDataMessage
.Builder messageBuilder
1597 ) throws IOException
{
1598 final var timestamp
= System
.currentTimeMillis();
1599 messageBuilder
.withTimestamp(timestamp
);
1600 getOrCreateMessagePipe();
1601 getOrCreateUnidentifiedMessagePipe();
1602 final var recipientId
= account
.getSelfRecipientId();
1604 final var contact
= account
.getContactStore().getContact(recipientId
);
1605 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1606 messageBuilder
.withExpiration(expirationTime
);
1608 var message
= messageBuilder
.build();
1609 final var result
= sendSelfMessage(message
);
1610 return new Pair
<>(timestamp
, result
);
1613 private SendMessageResult
sendSelfMessage(SignalServiceDataMessage message
) throws IOException
{
1614 var messageSender
= createMessageSender();
1616 var recipientId
= account
.getSelfRecipientId();
1618 final var unidentifiedAccess
= unidentifiedAccessHelper
.getAccessFor(recipientId
);
1619 var recipient
= resolveSignalServiceAddress(recipientId
);
1620 var transcript
= new SentTranscriptMessage(Optional
.of(recipient
),
1621 message
.getTimestamp(),
1623 message
.getExpiresInSeconds(),
1624 Map
.of(recipient
, unidentifiedAccess
.isPresent()),
1626 var syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
1629 var startTime
= System
.currentTimeMillis();
1630 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
1631 return SendMessageResult
.success(recipient
,
1632 unidentifiedAccess
.isPresent(),
1634 System
.currentTimeMillis() - startTime
);
1635 } catch (UntrustedIdentityException e
) {
1636 return SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey());
1640 private SendMessageResult
sendMessage(
1641 RecipientId recipientId
, SignalServiceDataMessage message
1642 ) throws IOException
{
1643 var messageSender
= createMessageSender();
1645 final var address
= resolveSignalServiceAddress(recipientId
);
1648 return messageSender
.sendMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
), message
);
1649 } catch (UnregisteredUserException e
) {
1650 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1651 return messageSender
.sendMessage(resolveSignalServiceAddress(newRecipientId
),
1652 unidentifiedAccessHelper
.getAccessFor(newRecipientId
),
1655 } catch (UntrustedIdentityException e
) {
1656 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1660 private SendMessageResult
sendNullMessage(RecipientId recipientId
) throws IOException
{
1661 var messageSender
= createMessageSender();
1663 final var address
= resolveSignalServiceAddress(recipientId
);
1666 return messageSender
.sendNullMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
));
1667 } catch (UnregisteredUserException e
) {
1668 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1669 final var newAddress
= resolveSignalServiceAddress(newRecipientId
);
1670 return messageSender
.sendNullMessage(newAddress
, unidentifiedAccessHelper
.getAccessFor(newRecipientId
));
1672 } catch (UntrustedIdentityException e
) {
1673 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1677 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
{
1678 var cipher
= new SignalServiceCipher(account
.getSelfAddress(),
1679 account
.getSignalProtocolStore(),
1681 certificateValidator
);
1682 return cipher
.decrypt(envelope
);
1685 private void handleEndSession(RecipientId recipientId
) {
1686 account
.getSessionStore().deleteAllSessions(recipientId
);
1689 private List
<HandleAction
> handleSignalServiceDataMessage(
1690 SignalServiceDataMessage message
,
1692 SignalServiceAddress source
,
1693 SignalServiceAddress destination
,
1694 boolean ignoreAttachments
1696 var actions
= new ArrayList
<HandleAction
>();
1697 if (message
.getGroupContext().isPresent()) {
1698 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1699 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1700 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1701 var group
= getGroup(groupId
);
1702 if (group
== null || group
instanceof GroupInfoV1
) {
1703 var groupV1
= (GroupInfoV1
) group
;
1704 switch (groupInfo
.getType()) {
1706 if (groupV1
== null) {
1707 groupV1
= new GroupInfoV1(groupId
);
1710 if (groupInfo
.getAvatar().isPresent()) {
1711 var avatar
= groupInfo
.getAvatar().get();
1712 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1715 if (groupInfo
.getName().isPresent()) {
1716 groupV1
.name
= groupInfo
.getName().get();
1719 if (groupInfo
.getMembers().isPresent()) {
1720 groupV1
.addMembers(groupInfo
.getMembers()
1723 .map(this::resolveRecipient
)
1724 .collect(Collectors
.toSet()));
1727 account
.getGroupStore().updateGroup(groupV1
);
1731 if (groupV1
== null && !isSync
) {
1732 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1736 if (groupV1
!= null) {
1737 groupV1
.removeMember(resolveRecipient(source
));
1738 account
.getGroupStore().updateGroup(groupV1
);
1743 if (groupV1
!= null && !isSync
) {
1744 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1749 // Received a group v1 message for a v2 group
1752 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1753 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1754 final var groupMasterKey
= groupContext
.getMasterKey();
1756 getOrMigrateGroup(groupMasterKey
,
1757 groupContext
.getRevision(),
1758 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1762 final var conversationPartnerAddress
= isSync ? destination
: source
;
1763 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1764 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1766 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1767 if (message
.getGroupContext().isPresent()) {
1768 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1769 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1770 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1771 if (group
!= null) {
1772 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1773 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1774 account
.getGroupStore().updateGroup(group
);
1777 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1778 // disappearing message timer already stored in the DecryptedGroup
1780 } else if (conversationPartnerAddress
!= null) {
1781 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1784 if (!ignoreAttachments
) {
1785 if (message
.getAttachments().isPresent()) {
1786 for (var attachment
: message
.getAttachments().get()) {
1787 downloadAttachment(attachment
);
1790 if (message
.getSharedContacts().isPresent()) {
1791 for (var contact
: message
.getSharedContacts().get()) {
1792 if (contact
.getAvatar().isPresent()) {
1793 downloadAttachment(contact
.getAvatar().get().getAttachment());
1798 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1799 final ProfileKey profileKey
;
1801 profileKey
= new ProfileKey(message
.getProfileKey().get());
1802 } catch (InvalidInputException e
) {
1803 throw new AssertionError(e
);
1805 if (source
.matches(account
.getSelfAddress())) {
1806 this.account
.setProfileKey(profileKey
);
1808 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1810 if (message
.getPreviews().isPresent()) {
1811 final var previews
= message
.getPreviews().get();
1812 for (var preview
: previews
) {
1813 if (preview
.getImage().isPresent()) {
1814 downloadAttachment(preview
.getImage().get());
1818 if (message
.getQuote().isPresent()) {
1819 final var quote
= message
.getQuote().get();
1821 for (var quotedAttachment
: quote
.getAttachments()) {
1822 final var thumbnail
= quotedAttachment
.getThumbnail();
1823 if (thumbnail
!= null) {
1824 downloadAttachment(thumbnail
);
1828 if (message
.getSticker().isPresent()) {
1829 final var messageSticker
= message
.getSticker().get();
1830 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1831 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1832 if (sticker
== null) {
1833 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1834 account
.getStickerStore().updateSticker(sticker
);
1840 private GroupInfoV2
getOrMigrateGroup(
1841 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1843 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1845 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1846 var groupInfo
= getGroup(groupId
);
1847 final GroupInfoV2 groupInfoV2
;
1848 if (groupInfo
instanceof GroupInfoV1
) {
1849 // Received a v2 group message for a v1 group, we need to locally migrate the group
1850 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1851 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1852 logger
.info("Locally migrated group {} to group v2, id: {}",
1853 groupInfo
.getGroupId().toBase64(),
1854 groupInfoV2
.getGroupId().toBase64());
1855 } else if (groupInfo
instanceof GroupInfoV2
) {
1856 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1858 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1861 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1862 DecryptedGroup group
= null;
1863 if (signedGroupChange
!= null
1864 && groupInfoV2
.getGroup() != null
1865 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1866 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1870 if (group
== null) {
1871 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1873 if (group
!= null) {
1874 storeProfileKeysFromMembers(group
);
1875 final var avatar
= group
.getAvatar();
1876 if (avatar
!= null && !avatar
.isEmpty()) {
1877 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1880 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1881 account
.getGroupStore().updateGroup(groupInfoV2
);
1887 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1888 for (var member
: group
.getMembersList()) {
1889 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1890 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1892 account
.getProfileStore()
1893 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1894 } catch (InvalidInputException ignored
) {
1899 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1900 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1901 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1902 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1903 if (actions
!= null) {
1904 queuedActions
.addAll(actions
);
1907 for (var action
: queuedActions
) {
1909 action
.execute(this);
1910 } catch (Throwable e
) {
1911 logger
.warn("Message action failed.", e
);
1916 private List
<HandleAction
> retryFailedReceivedMessage(
1917 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1919 var envelope
= cachedMessage
.loadEnvelope();
1920 if (envelope
== null) {
1923 SignalServiceContent content
= null;
1924 List
<HandleAction
> actions
= null;
1925 if (!envelope
.isReceipt()) {
1927 content
= decryptMessage(envelope
);
1928 } catch (ProtocolUntrustedIdentityException e
) {
1929 if (!envelope
.hasSource()) {
1930 final var identifier
= e
.getSender();
1931 final var recipientId
= resolveRecipient(identifier
);
1933 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1934 } catch (IOException ioException
) {
1935 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1939 } catch (Exception er
) {
1940 // All other errors are not recoverable, so delete the cached message
1941 cachedMessage
.delete();
1944 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1946 handler
.handleMessage(envelope
, content
, null);
1947 cachedMessage
.delete();
1951 public void receiveMessages(
1954 boolean returnOnTimeout
,
1955 boolean ignoreAttachments
,
1956 ReceiveMessageHandler handler
1957 ) throws IOException
{
1958 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1960 Set
<HandleAction
> queuedActions
= null;
1962 final var messagePipe
= getOrCreateMessagePipe();
1964 var hasCaughtUpWithOldMessages
= false;
1967 SignalServiceEnvelope envelope
;
1968 SignalServiceContent content
= null;
1969 Exception exception
= null;
1970 final CachedMessage
[] cachedMessage
= {null};
1972 var result
= messagePipe
.readOrEmpty(timeout
, unit
, envelope1
-> {
1973 final var recipientId
= envelope1
.hasSource()
1974 ?
resolveRecipient(envelope1
.getSourceIdentifier())
1976 // store message on disk, before acknowledging receipt to the server
1977 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1979 if (result
.isPresent()) {
1980 envelope
= result
.get();
1982 // Received indicator that server queue is empty
1983 hasCaughtUpWithOldMessages
= true;
1985 if (queuedActions
!= null) {
1986 for (var action
: queuedActions
) {
1988 action
.execute(this);
1989 } catch (Throwable e
) {
1990 logger
.warn("Message action failed.", e
);
1993 queuedActions
.clear();
1994 queuedActions
= null;
1997 // Continue to wait another timeout for new messages
2000 } catch (TimeoutException e
) {
2001 if (returnOnTimeout
) return;
2005 if (envelope
.hasSource()) {
2006 // Store uuid if we don't have it already
2007 // address/uuid in envelope is sent by server
2008 resolveRecipientTrusted(envelope
.getSourceAddress());
2010 final var notAGroupMember
= isNotAGroupMember(envelope
, content
);
2011 if (!envelope
.isReceipt()) {
2013 content
= decryptMessage(envelope
);
2014 } catch (Exception e
) {
2017 if (!envelope
.hasSource() && content
!= null) {
2018 // Store uuid if we don't have it already
2019 // address/uuid is validated by unidentified sender certificate
2020 resolveRecipientTrusted(content
.getSender());
2022 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2023 if (exception
instanceof ProtocolInvalidMessageException
) {
2024 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
2025 logger
.debug("Received invalid message, queuing renew session action.");
2026 actions
.add(new RenewSessionAction(sender
));
2028 if (hasCaughtUpWithOldMessages
) {
2029 for (var action
: actions
) {
2031 action
.execute(this);
2032 } catch (Throwable e
) {
2033 logger
.warn("Message action failed.", e
);
2037 if (queuedActions
== null) {
2038 queuedActions
= new HashSet
<>();
2040 queuedActions
.addAll(actions
);
2043 if (isMessageBlocked(envelope
, content
)) {
2044 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
2045 } else if (notAGroupMember
) {
2046 logger
.info("Ignoring a message from a non group member: {}", envelope
.getTimestamp());
2048 handler
.handleMessage(envelope
, content
, exception
);
2050 if (cachedMessage
[0] != null) {
2051 if (exception
instanceof ProtocolUntrustedIdentityException
) {
2052 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
2053 final var recipientId
= resolveRecipient(identifier
);
2054 queuedActions
.add(new RetrieveProfileAction(recipientId
));
2055 if (!envelope
.hasSource()) {
2057 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
2058 } catch (IOException ioException
) {
2059 logger
.warn("Failed to move cached message to recipient folder: {}",
2060 ioException
.getMessage());
2064 cachedMessage
[0].delete();
2070 private boolean isMessageBlocked(
2071 SignalServiceEnvelope envelope
, SignalServiceContent content
2073 SignalServiceAddress source
;
2074 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2075 source
= envelope
.getSourceAddress();
2076 } else if (content
!= null) {
2077 source
= content
.getSender();
2081 final var recipientId
= resolveRecipient(source
);
2082 if (isContactBlocked(recipientId
)) {
2086 if (content
!= null && content
.getDataMessage().isPresent()) {
2087 var message
= content
.getDataMessage().get();
2088 if (message
.getGroupContext().isPresent()) {
2089 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2090 var group
= getGroup(groupId
);
2091 if (group
!= null && group
.isBlocked()) {
2099 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2100 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2101 return isContactBlocked(recipientId
);
2104 private boolean isContactBlocked(final RecipientId recipientId
) {
2105 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2106 return sourceContact
!= null && sourceContact
.isBlocked();
2109 private boolean isNotAGroupMember(
2110 SignalServiceEnvelope envelope
, SignalServiceContent content
2112 SignalServiceAddress source
;
2113 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2114 source
= envelope
.getSourceAddress();
2115 } else if (content
!= null) {
2116 source
= content
.getSender();
2121 if (content
!= null && content
.getDataMessage().isPresent()) {
2122 var message
= content
.getDataMessage().get();
2123 if (message
.getGroupContext().isPresent()) {
2124 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2125 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2126 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2130 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2131 var group
= getGroup(groupId
);
2132 if (group
!= null && !group
.isMember(resolveRecipient(source
))) {
2140 private List
<HandleAction
> handleMessage(
2141 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2143 var actions
= new ArrayList
<HandleAction
>();
2144 if (content
!= null) {
2145 final SignalServiceAddress sender
;
2146 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2147 sender
= envelope
.getSourceAddress();
2149 sender
= content
.getSender();
2152 if (content
.getDataMessage().isPresent()) {
2153 var message
= content
.getDataMessage().get();
2155 if (content
.isNeedsReceipt()) {
2156 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2159 actions
.addAll(handleSignalServiceDataMessage(message
,
2162 account
.getSelfAddress(),
2163 ignoreAttachments
));
2165 if (content
.getSyncMessage().isPresent()) {
2166 account
.setMultiDevice(true);
2167 var syncMessage
= content
.getSyncMessage().get();
2168 if (syncMessage
.getSent().isPresent()) {
2169 var message
= syncMessage
.getSent().get();
2170 final var destination
= message
.getDestination().orNull();
2171 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2175 ignoreAttachments
));
2177 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2178 var rm
= syncMessage
.getRequest().get();
2179 if (rm
.isContactsRequest()) {
2180 actions
.add(SendSyncContactsAction
.create());
2182 if (rm
.isGroupsRequest()) {
2183 actions
.add(SendSyncGroupsAction
.create());
2185 if (rm
.isBlockedListRequest()) {
2186 actions
.add(SendSyncBlockedListAction
.create());
2188 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2190 if (syncMessage
.getGroups().isPresent()) {
2191 File tmpFile
= null;
2193 tmpFile
= IOUtils
.createTempFile();
2194 final var groupsMessage
= syncMessage
.getGroups().get();
2195 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2196 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2198 while ((g
= s
.read()) != null) {
2199 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2200 if (syncGroup
!= null) {
2201 if (g
.getName().isPresent()) {
2202 syncGroup
.name
= g
.getName().get();
2204 syncGroup
.addMembers(g
.getMembers()
2206 .map(this::resolveRecipient
)
2207 .collect(Collectors
.toSet()));
2208 if (!g
.isActive()) {
2209 syncGroup
.removeMember(account
.getSelfRecipientId());
2211 // Add ourself to the member set as it's marked as active
2212 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2214 syncGroup
.blocked
= g
.isBlocked();
2215 if (g
.getColor().isPresent()) {
2216 syncGroup
.color
= g
.getColor().get();
2219 if (g
.getAvatar().isPresent()) {
2220 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2222 syncGroup
.archived
= g
.isArchived();
2223 account
.getGroupStore().updateGroup(syncGroup
);
2227 } catch (Exception e
) {
2228 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2232 if (tmpFile
!= null) {
2234 Files
.delete(tmpFile
.toPath());
2235 } catch (IOException e
) {
2236 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2243 if (syncMessage
.getBlockedList().isPresent()) {
2244 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2245 for (var address
: blockedListMessage
.getAddresses()) {
2246 setContactBlocked(resolveRecipient(address
), true);
2248 for (var groupId
: blockedListMessage
.getGroupIds()
2250 .map(GroupId
::unknownVersion
)
2251 .collect(Collectors
.toSet())) {
2253 setGroupBlocked(groupId
, true);
2254 } catch (GroupNotFoundException e
) {
2255 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2256 groupId
.toBase64());
2260 if (syncMessage
.getContacts().isPresent()) {
2261 File tmpFile
= null;
2263 tmpFile
= IOUtils
.createTempFile();
2264 final var contactsMessage
= syncMessage
.getContacts().get();
2265 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2266 .asPointer(), tmpFile
)) {
2267 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2269 while ((c
= s
.read()) != null) {
2270 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2271 account
.setProfileKey(c
.getProfileKey().get());
2273 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2274 var contact
= account
.getContactStore().getContact(recipientId
);
2275 final var builder
= contact
== null
2276 ? Contact
.newBuilder()
2277 : Contact
.newBuilder(contact
);
2278 if (c
.getName().isPresent()) {
2279 builder
.withName(c
.getName().get());
2281 if (c
.getColor().isPresent()) {
2282 builder
.withColor(c
.getColor().get());
2284 if (c
.getProfileKey().isPresent()) {
2285 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2287 if (c
.getVerified().isPresent()) {
2288 final var verifiedMessage
= c
.getVerified().get();
2289 account
.getIdentityKeyStore()
2290 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2291 verifiedMessage
.getIdentityKey(),
2292 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2294 if (c
.getExpirationTimer().isPresent()) {
2295 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2297 builder
.withBlocked(c
.isBlocked());
2298 builder
.withArchived(c
.isArchived());
2299 account
.getContactStore().storeContact(recipientId
, builder
.build());
2301 if (c
.getAvatar().isPresent()) {
2302 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2306 } catch (Exception e
) {
2307 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2311 if (tmpFile
!= null) {
2313 Files
.delete(tmpFile
.toPath());
2314 } catch (IOException e
) {
2315 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2322 if (syncMessage
.getVerified().isPresent()) {
2323 final var verifiedMessage
= syncMessage
.getVerified().get();
2324 account
.getIdentityKeyStore()
2325 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2326 verifiedMessage
.getIdentityKey(),
2327 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2329 if (syncMessage
.getStickerPackOperations().isPresent()) {
2330 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2331 for (var m
: stickerPackOperationMessages
) {
2332 if (!m
.getPackId().isPresent()) {
2335 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2336 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2337 if (sticker
== null) {
2338 if (!m
.getPackKey().isPresent()) {
2341 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2343 sticker
.setInstalled(!m
.getType().isPresent()
2344 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
);
2345 account
.getStickerStore().updateSticker(sticker
);
2348 if (syncMessage
.getFetchType().isPresent()) {
2349 switch (syncMessage
.getFetchType().get()) {
2351 getRecipientProfile(account
.getSelfRecipientId(), true);
2352 case STORAGE_MANIFEST
:
2356 if (syncMessage
.getKeys().isPresent()) {
2357 final var keysMessage
= syncMessage
.getKeys().get();
2358 if (keysMessage
.getStorageService().isPresent()) {
2359 final var storageKey
= keysMessage
.getStorageService().get();
2360 account
.setStorageKey(storageKey
);
2363 if (syncMessage
.getConfiguration().isPresent()) {
2371 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2373 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2374 } catch (IOException e
) {
2375 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2379 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2381 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2382 } catch (IOException e
) {
2383 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2387 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2389 avatarStore
.storeGroupAvatar(groupId
,
2390 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2391 } catch (IOException e
) {
2392 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2396 private void downloadProfileAvatar(
2397 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2400 avatarStore
.storeProfileAvatar(address
,
2401 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2402 } catch (Throwable e
) {
2403 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2407 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2408 return attachmentStore
.getAttachmentFile(attachmentId
);
2411 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2412 if (!attachment
.isPointer()) {
2413 logger
.warn("Invalid state, can't store an attachment stream.");
2416 var pointer
= attachment
.asPointer();
2417 if (pointer
.getPreview().isPresent()) {
2418 final var preview
= pointer
.getPreview().get();
2420 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2421 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2422 } catch (IOException e
) {
2423 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2428 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2429 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2430 } catch (IOException e
) {
2431 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2435 private void retrieveGroupV2Avatar(
2436 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2437 ) throws IOException
{
2438 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
2440 var tmpFile
= IOUtils
.createTempFile();
2441 try (InputStream input
= messageReceiver
.retrieveGroupsV2ProfileAvatar(cdnKey
,
2443 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2444 var encryptedData
= IOUtils
.readFully(input
);
2446 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2447 outputStream
.write(decryptedData
);
2450 Files
.delete(tmpFile
.toPath());
2451 } catch (IOException e
) {
2452 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2459 private void retrieveProfileAvatar(
2460 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2461 ) throws IOException
{
2462 var tmpFile
= IOUtils
.createTempFile();
2463 try (var input
= messageReceiver
.retrieveProfileAvatar(avatarPath
,
2466 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2467 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2468 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2471 Files
.delete(tmpFile
.toPath());
2472 } catch (IOException e
) {
2473 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2480 private void retrieveAttachment(
2481 final SignalServiceAttachment attachment
, final OutputStream outputStream
2482 ) throws IOException
{
2483 if (attachment
.isPointer()) {
2484 var pointer
= attachment
.asPointer();
2485 retrieveAttachmentPointer(pointer
, outputStream
);
2487 var stream
= attachment
.asStream();
2488 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2492 private void retrieveAttachmentPointer(
2493 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2494 ) throws IOException
{
2495 var tmpFile
= IOUtils
.createTempFile();
2496 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2497 IOUtils
.copyStream(input
, outputStream
);
2498 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2499 throw new IOException(e
);
2502 Files
.delete(tmpFile
.toPath());
2503 } catch (IOException e
) {
2504 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2511 private InputStream
retrieveAttachmentAsStream(
2512 SignalServiceAttachmentPointer pointer
, File tmpFile
2513 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2514 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2517 void sendGroups() throws IOException
, UntrustedIdentityException
{
2518 var groupsFile
= IOUtils
.createTempFile();
2521 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2522 var out
= new DeviceGroupsOutputStream(fos
);
2523 for (var record : getGroups()) {
2524 if (record instanceof GroupInfoV1
) {
2525 var groupInfo
= (GroupInfoV1
) record;
2526 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2527 Optional
.fromNullable(groupInfo
.name
),
2528 groupInfo
.getMembers()
2530 .map(this::resolveSignalServiceAddress
)
2531 .collect(Collectors
.toList()),
2532 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2533 groupInfo
.isMember(account
.getSelfRecipientId()),
2534 Optional
.of(groupInfo
.messageExpirationTime
),
2535 Optional
.fromNullable(groupInfo
.color
),
2538 groupInfo
.archived
));
2543 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2544 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2545 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2546 .withStream(groupsFileStream
)
2547 .withContentType("application/octet-stream")
2548 .withLength(groupsFile
.length())
2551 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2556 Files
.delete(groupsFile
.toPath());
2557 } catch (IOException e
) {
2558 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2563 public void sendContacts() throws IOException
, UntrustedIdentityException
{
2564 var contactsFile
= IOUtils
.createTempFile();
2567 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2568 var out
= new DeviceContactsOutputStream(fos
);
2569 for (var contactPair
: account
.getContactStore().getContacts()) {
2570 final var recipientId
= contactPair
.first();
2571 final var contact
= contactPair
.second();
2572 final var address
= resolveSignalServiceAddress(recipientId
);
2574 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2575 VerifiedMessage verifiedMessage
= null;
2576 if (currentIdentity
!= null) {
2577 verifiedMessage
= new VerifiedMessage(address
,
2578 currentIdentity
.getIdentityKey(),
2579 currentIdentity
.getTrustLevel().toVerifiedState(),
2580 currentIdentity
.getDateAdded().getTime());
2583 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2584 out
.write(new DeviceContact(address
,
2585 Optional
.fromNullable(contact
.getName()),
2586 createContactAvatarAttachment(address
),
2587 Optional
.fromNullable(contact
.getColor()),
2588 Optional
.fromNullable(verifiedMessage
),
2589 Optional
.fromNullable(profileKey
),
2590 contact
.isBlocked(),
2591 Optional
.of(contact
.getMessageExpirationTime()),
2593 contact
.isArchived()));
2596 if (account
.getProfileKey() != null) {
2597 // Send our own profile key as well
2598 out
.write(new DeviceContact(account
.getSelfAddress(),
2603 Optional
.of(account
.getProfileKey()),
2611 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2612 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2613 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2614 .withStream(contactsFileStream
)
2615 .withContentType("application/octet-stream")
2616 .withLength(contactsFile
.length())
2619 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
2624 Files
.delete(contactsFile
.toPath());
2625 } catch (IOException e
) {
2626 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2631 void sendBlockedList() throws IOException
, UntrustedIdentityException
{
2632 var addresses
= new ArrayList
<SignalServiceAddress
>();
2633 for (var record : account
.getContactStore().getContacts()) {
2634 if (record.second().isBlocked()) {
2635 addresses
.add(resolveSignalServiceAddress(record.first()));
2638 var groupIds
= new ArrayList
<byte[]>();
2639 for (var record : getGroups()) {
2640 if (record.isBlocked()) {
2641 groupIds
.add(record.getGroupId().serialize());
2644 sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2647 private void sendVerifiedMessage(
2648 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2649 ) throws IOException
, UntrustedIdentityException
{
2650 var verifiedMessage
= new VerifiedMessage(destination
,
2652 trustLevel
.toVerifiedState(),
2653 System
.currentTimeMillis());
2654 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2657 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2658 return account
.getContactStore().getContacts();
2661 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2662 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2663 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2664 if (recipient
== null) {
2668 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2669 return recipient
.getContact().getName();
2672 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2673 return recipient
.getProfile().getDisplayName();
2679 public GroupInfo
getGroup(GroupId groupId
) {
2680 final var group
= account
.getGroupStore().getGroup(groupId
);
2681 if (group
instanceof GroupInfoV2
&& ((GroupInfoV2
) group
).getGroup() == null) {
2682 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2683 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2684 account
.getGroupStore().updateGroup(group
);
2689 public List
<IdentityInfo
> getIdentities() {
2690 return account
.getIdentityKeyStore().getIdentities();
2693 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2694 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2695 return identity
== null ? List
.of() : List
.of(identity
);
2699 * Trust this the identity with this fingerprint
2701 * @param name username of the identity
2702 * @param fingerprint Fingerprint
2704 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2705 var recipientId
= canonicalizeAndResolveRecipient(name
);
2706 return trustIdentity(recipientId
,
2707 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2708 TrustLevel
.TRUSTED_VERIFIED
);
2712 * Trust this the identity with this safety number
2714 * @param name username of the identity
2715 * @param safetyNumber Safety number
2717 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2718 var recipientId
= canonicalizeAndResolveRecipient(name
);
2719 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2720 return trustIdentity(recipientId
,
2721 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2722 TrustLevel
.TRUSTED_VERIFIED
);
2726 * Trust all keys of this identity without verification
2728 * @param name username of the identity
2730 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2731 var recipientId
= canonicalizeAndResolveRecipient(name
);
2732 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2735 private boolean trustIdentity(
2736 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2738 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2739 if (identity
== null) {
2743 if (!verifier
.apply(identity
.getIdentityKey())) {
2747 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2749 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2750 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2751 } catch (IOException
| UntrustedIdentityException e
) {
2752 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2758 public String
computeSafetyNumber(
2759 SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
2761 return Utils
.computeSafetyNumber(ServiceConfig
.capabilities
.isUuid(),
2762 account
.getSelfAddress(),
2763 getIdentityKeyPair().getPublicKey(),
2769 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2770 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2772 return resolveSignalServiceAddress(address
);
2776 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2777 if (address
.matches(account
.getSelfAddress())) {
2778 return account
.getSelfAddress();
2781 return account
.getRecipientStore().resolveServiceAddress(address
);
2784 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2785 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2788 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2789 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2791 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2793 return resolveRecipient(canonicalizedNumber
);
2796 private RecipientId
resolveRecipient(final String identifier
) {
2797 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2799 return resolveRecipient(address
);
2802 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2803 return account
.getRecipientStore().resolveRecipient(address
);
2806 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2807 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2811 public void close() throws IOException
{
2815 void close(boolean closeAccount
) throws IOException
{
2816 executor
.shutdown();
2818 if (messagePipe
!= null) {
2819 messagePipe
.shutdown();
2823 if (unidentifiedMessagePipe
!= null) {
2824 unidentifiedMessagePipe
.shutdown();
2825 unidentifiedMessagePipe
= null;
2828 if (closeAccount
&& account
!= null) {
2834 public interface ReceiveMessageHandler
{
2836 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);