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
.GroupUtils
;
29 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
30 import org
.asamk
.signal
.manager
.helper
.GroupV2Helper
;
31 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
32 import org
.asamk
.signal
.manager
.helper
.ProfileHelper
;
33 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
34 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
35 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
36 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
37 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
38 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
39 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
40 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
41 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
42 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
43 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
44 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
45 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
46 import org
.asamk
.signal
.manager
.util
.IOUtils
;
47 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
48 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
49 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
50 import org
.asamk
.signal
.manager
.util
.Utils
;
51 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
52 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
53 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
54 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
55 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
56 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
57 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
58 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
59 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
60 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
61 import org
.signal
.libsignal
.metadata
.SelfSendException
;
62 import org
.signal
.libsignal
.metadata
.certificate
.CertificateValidator
;
63 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
64 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
65 import org
.signal
.zkgroup
.InvalidInputException
;
66 import org
.signal
.zkgroup
.VerificationFailedException
;
67 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
68 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
69 import org
.signal
.zkgroup
.profiles
.ClientZkProfileOperations
;
70 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
71 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
72 import org
.slf4j
.Logger
;
73 import org
.slf4j
.LoggerFactory
;
74 import org
.whispersystems
.libsignal
.IdentityKey
;
75 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
76 import org
.whispersystems
.libsignal
.InvalidKeyException
;
77 import org
.whispersystems
.libsignal
.InvalidMessageException
;
78 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
79 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
80 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
81 import org
.whispersystems
.libsignal
.util
.Pair
;
82 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
83 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
84 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
85 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
86 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
87 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
88 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
89 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
90 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
91 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
92 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Api
;
93 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
94 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
95 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
96 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
97 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
98 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
99 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
100 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
101 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
102 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
103 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
104 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupV2
;
105 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
106 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
107 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
108 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
109 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
110 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
111 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
112 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
113 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
114 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
117 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
118 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
119 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
120 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
121 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
122 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
123 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
124 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
125 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
126 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
127 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
128 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
129 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
130 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
131 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
132 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
133 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
134 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
135 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
136 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
137 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
139 import java
.io
.Closeable
;
141 import java
.io
.FileInputStream
;
142 import java
.io
.FileOutputStream
;
143 import java
.io
.IOException
;
144 import java
.io
.InputStream
;
145 import java
.io
.OutputStream
;
147 import java
.net
.URISyntaxException
;
148 import java
.net
.URLEncoder
;
149 import java
.nio
.charset
.StandardCharsets
;
150 import java
.nio
.file
.Files
;
151 import java
.security
.SignatureException
;
152 import java
.util
.ArrayList
;
153 import java
.util
.Arrays
;
154 import java
.util
.Base64
;
155 import java
.util
.Collection
;
156 import java
.util
.Date
;
157 import java
.util
.HashSet
;
158 import java
.util
.List
;
159 import java
.util
.Map
;
160 import java
.util
.Set
;
161 import java
.util
.UUID
;
162 import java
.util
.concurrent
.ExecutorService
;
163 import java
.util
.concurrent
.Executors
;
164 import java
.util
.concurrent
.TimeUnit
;
165 import java
.util
.concurrent
.TimeoutException
;
166 import java
.util
.concurrent
.locks
.ReentrantLock
;
167 import java
.util
.function
.Function
;
168 import java
.util
.stream
.Collectors
;
170 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
172 public class Manager
implements Closeable
{
174 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
176 private final CertificateValidator certificateValidator
;
178 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
179 private final String userAgent
;
181 private SignalAccount account
;
182 private final SignalServiceAccountManager accountManager
;
183 private final GroupsV2Api groupsV2Api
;
184 private final GroupsV2Operations groupsV2Operations
;
185 private final SignalServiceMessageReceiver messageReceiver
;
186 private final ClientZkProfileOperations clientZkProfileOperations
;
188 private final ExecutorService executor
= Executors
.newCachedThreadPool();
190 private SignalServiceMessagePipe messagePipe
= null;
191 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
193 private final UnidentifiedAccessHelper unidentifiedAccessHelper
;
194 private final ProfileHelper profileHelper
;
195 private final GroupV2Helper groupV2Helper
;
196 private final PinHelper pinHelper
;
197 private final AvatarStore avatarStore
;
198 private final AttachmentStore attachmentStore
;
199 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
200 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
203 public Lock
acquire() {
205 return LEGACY_LOCK
::unlock
;
210 SignalAccount account
,
211 PathConfig pathConfig
,
212 ServiceEnvironmentConfig serviceEnvironmentConfig
,
215 this.account
= account
;
216 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
217 this.certificateValidator
= new CertificateValidator(serviceEnvironmentConfig
.getUnidentifiedSenderTrustRoot());
218 this.userAgent
= userAgent
;
219 this.groupsV2Operations
= capabilities
.isGv2() ?
new GroupsV2Operations(ClientZkOperations
.create(
220 serviceEnvironmentConfig
.getSignalServiceConfiguration())) : null;
221 final SleepTimer timer
= new UptimeSleepTimer();
222 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
223 new DynamicCredentialsProvider(account
.getUuid(),
224 account
.getUsername(),
225 account
.getPassword(),
226 account
.getDeviceId()),
229 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
231 this.groupsV2Api
= accountManager
.getGroupsV2Api();
232 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
233 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
234 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
235 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
238 this.pinHelper
= new PinHelper(keyBackupService
);
239 this.clientZkProfileOperations
= capabilities
.isGv2()
240 ? ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration())
241 .getProfileOperations()
243 this.messageReceiver
= new SignalServiceMessageReceiver(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
245 account
.getUsername(),
246 account
.getPassword(),
247 account
.getDeviceId(),
251 clientZkProfileOperations
,
252 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
254 this.unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
255 account
.getProfileStore()::getProfileKey
,
256 this::getRecipientProfile
,
257 this::getSenderCertificate
);
258 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
259 unidentifiedAccessHelper
::getAccessFor
,
260 unidentified
-> unidentified ?
getOrCreateUnidentifiedMessagePipe() : getOrCreateMessagePipe(),
261 () -> messageReceiver
,
262 this::resolveSignalServiceAddress
);
263 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
264 this::getRecipientProfile
,
265 account
::getSelfRecipientId
,
268 this::getGroupAuthForToday
,
269 this::resolveSignalServiceAddress
);
270 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
271 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
274 public String
getUsername() {
275 return account
.getUsername();
278 public SignalServiceAddress
getSelfAddress() {
279 return account
.getSelfAddress();
282 public RecipientId
getSelfRecipientId() {
283 return account
.getSelfRecipientId();
286 private IdentityKeyPair
getIdentityKeyPair() {
287 return account
.getIdentityKeyPair();
290 public int getDeviceId() {
291 return account
.getDeviceId();
294 public static Manager
init(
295 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
296 ) throws IOException
, NotRegisteredException
{
297 var pathConfig
= PathConfig
.createDefault(settingsPath
);
299 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
300 throw new NotRegisteredException();
303 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
305 if (!account
.isRegistered()) {
306 throw new NotRegisteredException();
309 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
311 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
314 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
315 var pathConfig
= PathConfig
.createDefault(settingsPath
);
316 final var dataPath
= pathConfig
.getDataPath();
317 final var files
= dataPath
.listFiles();
323 return Arrays
.stream(files
)
324 .filter(File
::isFile
)
326 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
327 .collect(Collectors
.toList());
330 public void checkAccountState() throws IOException
{
331 if (accountManager
.getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
334 if (account
.getUuid() == null) {
335 account
.setUuid(accountManager
.getOwnUuid());
337 updateAccountAttributes();
341 * This is used for checking a set of phone numbers for registration on Signal
343 * @param numbers The set of phone number in question
344 * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
345 * @throws IOException if its unable to get the contacts to check if they're registered
347 public Map
<String
, Boolean
> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
348 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
349 var contactDetails
= getRegisteredUsers(numbers
);
351 var registeredUsers
= contactDetails
.keySet();
353 return numbers
.stream().collect(Collectors
.toMap(x
-> x
, registeredUsers
::contains
));
356 public void updateAccountAttributes() throws IOException
{
357 accountManager
.setAccountAttributes(account
.getEncryptedDeviceName(),
359 account
.getLocalRegistrationId(),
361 // set legacy pin only if no KBS master key is set
362 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
363 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
364 account
.getSelfUnidentifiedAccessKey(),
365 account
.isUnrestrictedUnidentifiedAccess(),
367 account
.isDiscoverableByPhoneNumber());
371 * @param givenName if null, the previous givenName will be kept
372 * @param familyName if null, the previous familyName will be kept
373 * @param about if null, the previous about text will be kept
374 * @param aboutEmoji if null, the previous about emoji will be kept
375 * @param avatar if avatar is null the image from the local avatar store is used (if present),
377 public void setProfile(
378 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
379 ) throws IOException
{
380 var profile
= getRecipientProfile(account
.getSelfRecipientId());
381 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
382 if (givenName
!= null) {
383 builder
.withGivenName(givenName
);
385 if (familyName
!= null) {
386 builder
.withFamilyName(familyName
);
389 builder
.withAbout(about
);
391 if (aboutEmoji
!= null) {
392 builder
.withAboutEmoji(aboutEmoji
);
394 var newProfile
= builder
.build();
396 try (final var streamDetails
= avatar
== null
397 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
398 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
399 accountManager
.setVersionedProfile(account
.getUuid(),
400 account
.getProfileKey(),
401 newProfile
.getInternalServiceName(),
402 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
403 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
408 if (avatar
!= null) {
409 if (avatar
.isPresent()) {
410 avatarStore
.storeProfileAvatar(getSelfAddress(),
411 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
413 avatarStore
.deleteProfileAvatar(getSelfAddress());
416 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
419 sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
420 } catch (UntrustedIdentityException ignored
) {
424 public void unregister() throws IOException
{
425 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
426 // If this is the master device, other users can't send messages to this number anymore.
427 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
428 accountManager
.setGcmId(Optional
.absent());
430 account
.setRegistered(false);
433 public void deleteAccount() throws IOException
{
434 accountManager
.deleteAccount();
436 account
.setRegistered(false);
439 public List
<Device
> getLinkedDevices() throws IOException
{
440 var devices
= accountManager
.getDevices();
441 account
.setMultiDevice(devices
.size() > 1);
442 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
443 return devices
.stream().map(d
-> {
444 String deviceName
= d
.getName();
445 if (deviceName
!= null) {
447 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
448 } catch (IOException e
) {
449 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
452 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
453 }).collect(Collectors
.toList());
456 public void removeLinkedDevices(int deviceId
) throws IOException
{
457 accountManager
.removeDevice(deviceId
);
458 var devices
= accountManager
.getDevices();
459 account
.setMultiDevice(devices
.size() > 1);
462 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
463 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
465 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
468 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
469 var identityKeyPair
= getIdentityKeyPair();
470 var verificationCode
= accountManager
.getNewDeviceVerificationCode();
472 accountManager
.addDevice(deviceIdentifier
,
475 Optional
.of(account
.getProfileKey().serialize()),
477 account
.setMultiDevice(true);
480 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
481 if (!account
.isMasterDevice()) {
482 throw new RuntimeException("Only master device can set a PIN");
484 if (pin
.isPresent()) {
485 final var masterKey
= account
.getPinMasterKey() != null
486 ? account
.getPinMasterKey()
487 : KeyUtils
.createMasterKey();
489 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
491 account
.setRegistrationLockPin(pin
.get(), masterKey
);
493 // Remove legacy registration lock
494 accountManager
.removeRegistrationLockV1();
497 pinHelper
.removeRegistrationLockPin();
499 account
.setRegistrationLockPin(null, null);
503 void refreshPreKeys() throws IOException
{
504 var oneTimePreKeys
= generatePreKeys();
505 final var identityKeyPair
= getIdentityKeyPair();
506 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
508 accountManager
.setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
511 private List
<PreKeyRecord
> generatePreKeys() {
512 final var offset
= account
.getPreKeyIdOffset();
514 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
515 account
.addPreKeys(records
);
520 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
521 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
523 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
524 account
.addSignedPreKey(record);
529 private SignalServiceMessagePipe
getOrCreateMessagePipe() {
530 if (messagePipe
== null) {
531 messagePipe
= messageReceiver
.createMessagePipe();
536 private SignalServiceMessagePipe
getOrCreateUnidentifiedMessagePipe() {
537 if (unidentifiedMessagePipe
== null) {
538 unidentifiedMessagePipe
= messageReceiver
.createUnidentifiedMessagePipe();
540 return unidentifiedMessagePipe
;
543 private SignalServiceMessageSender
createMessageSender() {
544 return new SignalServiceMessageSender(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
546 account
.getUsername(),
547 account
.getPassword(),
548 account
.getDeviceId(),
549 account
.getSignalProtocolStore(),
552 account
.isMultiDevice(),
553 Optional
.fromNullable(messagePipe
),
554 Optional
.fromNullable(unidentifiedMessagePipe
),
556 clientZkProfileOperations
,
558 ServiceConfig
.MAX_ENVELOPE_SIZE
,
559 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
562 public Profile
getRecipientProfile(
563 RecipientId recipientId
565 return getRecipientProfile(recipientId
, false);
568 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
570 Profile
getRecipientProfile(
571 RecipientId recipientId
, boolean force
573 var profile
= account
.getProfileStore().getProfile(recipientId
);
575 var now
= new Date().getTime();
576 // Profiles are cached for 24h before retrieving them again, unless forced
577 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
581 synchronized (pendingProfileRequest
) {
582 if (pendingProfileRequest
.contains(recipientId
)) {
585 pendingProfileRequest
.add(recipientId
);
587 final SignalServiceProfile encryptedProfile
;
589 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
591 synchronized (pendingProfileRequest
) {
592 pendingProfileRequest
.remove(recipientId
);
595 if (encryptedProfile
== null) {
599 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
600 if (profileKey
== null) {
601 profile
= new Profile(new Date().getTime(),
606 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
607 ProfileUtils
.getCapabilities(encryptedProfile
));
609 profile
= decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
611 account
.getProfileStore().storeProfile(recipientId
, profile
);
616 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
618 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
619 } catch (IOException e
) {
620 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
625 private ProfileAndCredential
retrieveProfileAndCredential(
626 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
627 ) throws IOException
{
628 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
629 final var profile
= profileAndCredential
.getProfile();
632 var newIdentity
= account
.getIdentityKeyStore()
633 .saveIdentity(recipientId
,
634 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
638 account
.getSessionStore().archiveSessions(recipientId
);
640 } catch (InvalidKeyException ignored
) {
641 logger
.warn("Got invalid identity key in profile for {}",
642 resolveSignalServiceAddress(recipientId
).getLegacyIdentifier());
644 return profileAndCredential
;
647 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
648 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
649 if (profileKeyCredential
!= null) {
650 return profileKeyCredential
;
653 ProfileAndCredential profileAndCredential
;
655 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
656 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
657 } catch (IOException e
) {
658 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
662 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
663 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
665 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
666 if (profileKey
!= null) {
667 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
669 profileAndCredential
.getProfile());
670 account
.getProfileStore().storeProfile(recipientId
, profile
);
673 return profileKeyCredential
;
676 private Profile
decryptProfileAndDownloadAvatar(
677 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
679 if (encryptedProfile
.getAvatar() != null) {
680 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
683 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
686 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
687 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
688 if (streamDetails
== null) {
689 return Optional
.absent();
692 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
695 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
696 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
697 if (streamDetails
== null) {
698 return Optional
.absent();
701 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
704 private GroupInfo
getGroupForSending(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
705 var g
= getGroup(groupId
);
707 throw new GroupNotFoundException(groupId
);
709 if (!g
.isMember(account
.getSelfRecipientId())) {
710 throw new NotAGroupMemberException(groupId
, g
.getTitle());
715 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
716 var g
= getGroup(groupId
);
718 throw new GroupNotFoundException(groupId
);
720 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
721 throw new NotAGroupMemberException(groupId
, g
.getTitle());
726 public List
<GroupInfo
> getGroups() {
727 return account
.getGroupStore().getGroups();
730 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
731 String messageText
, List
<String
> attachments
, GroupId groupId
732 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
733 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
734 if (attachments
!= null) {
735 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
738 return sendGroupMessage(messageBuilder
, groupId
);
741 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
742 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
743 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
744 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
745 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
747 resolveSignalServiceAddress(targetAuthorRecipientId
),
748 targetSentTimestamp
);
749 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
751 return sendGroupMessage(messageBuilder
, groupId
);
754 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
755 SignalServiceDataMessage
.Builder messageBuilder
, GroupId groupId
756 ) throws IOException
, GroupNotFoundException
, NotAGroupMemberException
{
757 final var g
= getGroupForSending(groupId
);
759 GroupUtils
.setGroupContext(messageBuilder
, g
);
760 messageBuilder
.withExpiration(g
.getMessageExpirationTime());
762 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
765 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(GroupId groupId
) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
{
766 SignalServiceDataMessage
.Builder messageBuilder
;
768 final var g
= getGroupForUpdating(groupId
);
769 if (g
instanceof GroupInfoV1
) {
770 var groupInfoV1
= (GroupInfoV1
) g
;
771 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
).withId(groupId
.serialize()).build();
772 messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
773 groupInfoV1
.removeMember(account
.getSelfRecipientId());
774 account
.getGroupStore().updateGroup(groupInfoV1
);
776 final var groupInfoV2
= (GroupInfoV2
) g
;
777 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
);
778 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
779 messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
780 account
.getGroupStore().updateGroup(groupInfoV2
);
783 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
786 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
787 String name
, List
<String
> members
, File avatarFile
788 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
789 return createGroup(name
, members
== null ?
null : getSignalServiceAddresses(members
), avatarFile
);
792 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
793 String name
, Set
<RecipientId
> members
, File avatarFile
794 ) throws IOException
, AttachmentInvalidException
{
795 final var selfRecipientId
= account
.getSelfRecipientId();
796 if (members
!= null && members
.contains(selfRecipientId
)) {
797 members
= new HashSet
<>(members
);
798 members
.remove(selfRecipientId
);
801 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
802 members
== null ? Set
.of() : members
,
805 SignalServiceDataMessage
.Builder messageBuilder
;
806 if (gv2Pair
== null) {
807 // Failed to create v2 group, creating v1 group instead
808 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
809 gv1
.addMembers(List
.of(selfRecipientId
));
810 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
811 return new Pair
<>(gv1
.getGroupId(), result
.second());
814 final var gv2
= gv2Pair
.first();
815 final var decryptedGroup
= gv2Pair
.second();
817 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
818 if (avatarFile
!= null) {
819 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
820 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
822 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
823 account
.getGroupStore().updateGroup(gv2
);
825 final var result
= sendMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
826 return new Pair
<>(gv2
.getGroupId(), result
.second());
829 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
833 List
<String
> members
,
834 List
<String
> removeMembers
,
836 List
<String
> removeAdmins
,
837 boolean resetGroupLink
,
838 GroupLinkState groupLinkState
,
840 Integer expirationTimer
841 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
842 return updateGroup(groupId
,
845 members
== null ?
null : getSignalServiceAddresses(members
),
846 removeMembers
== null ?
null : getSignalServiceAddresses(removeMembers
),
847 admins
== null ?
null : getSignalServiceAddresses(admins
),
848 removeAdmins
== null ?
null : getSignalServiceAddresses(removeAdmins
),
855 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
856 final GroupId groupId
,
858 final String description
,
859 final Set
<RecipientId
> members
,
860 final Set
<RecipientId
> removeMembers
,
861 final Set
<RecipientId
> admins
,
862 final Set
<RecipientId
> removeAdmins
,
863 final boolean resetGroupLink
,
864 final GroupLinkState groupLinkState
,
865 final File avatarFile
,
866 Integer expirationTimer
867 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
868 var group
= getGroupForUpdating(groupId
);
870 if (group
instanceof GroupInfoV2
) {
871 return updateGroupV2((GroupInfoV2
) group
,
884 final var gv1
= (GroupInfoV1
) group
;
885 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
886 if (expirationTimer
!= null) {
887 setExpirationTimer(gv1
, expirationTimer
);
892 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
893 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
894 ) throws IOException
, AttachmentInvalidException
{
895 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
896 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
898 account
.getGroupStore().updateGroup(gv1
);
900 return sendMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
903 private void updateGroupV1Details(
904 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
905 ) throws IOException
{
910 if (members
!= null) {
911 final var newMemberAddresses
= members
.stream()
912 .filter(member
-> !g
.isMember(member
))
913 .map(this::resolveSignalServiceAddress
)
914 .collect(Collectors
.toList());
915 final var newE164Members
= new HashSet
<String
>();
916 for (var member
: newMemberAddresses
) {
917 if (!member
.getNumber().isPresent()) {
920 newE164Members
.add(member
.getNumber().get());
923 final var registeredUsers
= getRegisteredUsers(newE164Members
);
924 if (registeredUsers
.size() != newE164Members
.size()) {
925 // Some of the new members are not registered on Signal
926 newE164Members
.removeAll(registeredUsers
.keySet());
927 throw new IOException("Failed to add members "
928 + String
.join(", ", newE164Members
)
929 + " to group: Not registered on Signal");
932 g
.addMembers(members
);
935 if (avatarFile
!= null) {
936 avatarStore
.storeGroupAvatar(g
.getGroupId(),
937 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
941 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
942 final GroupInfoV2 group
,
944 final String description
,
945 final Set
<RecipientId
> members
,
946 final Set
<RecipientId
> removeMembers
,
947 final Set
<RecipientId
> admins
,
948 final Set
<RecipientId
> removeAdmins
,
949 final boolean resetGroupLink
,
950 final GroupLinkState groupLinkState
,
951 final File avatarFile
,
952 Integer expirationTimer
953 ) throws IOException
{
954 Pair
<Long
, List
<SendMessageResult
>> result
= null;
955 if (group
.isPendingMember(account
.getSelfRecipientId())) {
956 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
957 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
960 if (members
!= null) {
961 final var newMembers
= new HashSet
<>(members
);
962 newMembers
.removeAll(group
.getMembers());
963 if (newMembers
.size() > 0) {
964 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
965 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
969 if (removeMembers
!= null) {
970 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
971 existingRemoveMembers
.retainAll(group
.getMembers());
972 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
973 if (existingRemoveMembers
.size() > 0) {
974 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
975 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
978 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
979 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
980 if (pendingRemoveMembers
.size() > 0) {
981 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
982 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
986 if (admins
!= null) {
987 final var newAdmins
= new HashSet
<>(admins
);
988 newAdmins
.retainAll(group
.getMembers());
989 newAdmins
.removeAll(group
.getAdminMembers());
990 if (newAdmins
.size() > 0) {
991 for (var admin
: newAdmins
) {
992 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
993 result
= sendUpdateGroupV2Message(group
,
994 groupGroupChangePair
.first(),
995 groupGroupChangePair
.second());
1000 if (removeAdmins
!= null) {
1001 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1002 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1003 if (existingRemoveAdmins
.size() > 0) {
1004 for (var admin
: existingRemoveAdmins
) {
1005 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1006 result
= sendUpdateGroupV2Message(group
,
1007 groupGroupChangePair
.first(),
1008 groupGroupChangePair
.second());
1013 if (resetGroupLink
) {
1014 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1015 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1018 if (groupLinkState
!= null) {
1019 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1020 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1023 if (expirationTimer
!= null) {
1024 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1025 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1028 if (result
== null || name
!= null || description
!= null || avatarFile
!= null) {
1029 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1030 if (avatarFile
!= null) {
1031 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1032 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1034 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1040 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1041 GroupInviteLinkUrl inviteLinkUrl
1042 ) throws IOException
, GroupLinkNotActiveException
{
1043 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1044 inviteLinkUrl
.getPassword());
1045 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1046 inviteLinkUrl
.getPassword(),
1048 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1049 groupJoinInfo
.getRevision() + 1,
1050 groupChange
.toByteArray());
1052 if (group
.getGroup() == null) {
1053 // Only requested member, can't send update to group members
1054 return new Pair
<>(group
.getGroupId(), List
.of());
1057 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1059 return new Pair
<>(group
.getGroupId(), result
.second());
1062 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1063 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1064 ) throws IOException
{
1065 final var selfRecipientId
= account
.getSelfRecipientId();
1066 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1067 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1068 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1070 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1071 account
.getGroupStore().updateGroup(group
);
1072 return sendMessage(messageBuilder
, members
);
1075 private static int currentTimeDays() {
1076 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1079 private GroupsV2AuthorizationString
getGroupAuthForToday(
1080 final GroupSecretParams groupSecretParams
1081 ) throws IOException
{
1082 final var today
= currentTimeDays();
1083 // Returns credentials for the next 7 days
1084 final var credentials
= groupsV2Api
.getCredentials(today
);
1085 // TODO cache credentials until they expire
1086 var authCredentialResponse
= credentials
.get(today
);
1088 return groupsV2Api
.getGroupsV2AuthorizationString(account
.getUuid(),
1091 authCredentialResponse
);
1092 } catch (VerificationFailedException e
) {
1093 throw new IOException(e
);
1097 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1098 GroupIdV1 groupId
, SignalServiceAddress recipient
1099 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1101 var group
= getGroupForSending(groupId
);
1102 if (!(group
instanceof GroupInfoV1
)) {
1103 throw new RuntimeException("Received an invalid group request for a v2 group!");
1105 g
= (GroupInfoV1
) group
;
1107 final var recipientId
= resolveRecipient(recipient
);
1108 if (!g
.isMember(recipientId
)) {
1109 throw new NotAGroupMemberException(groupId
, g
.name
);
1112 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1114 // Send group message only to the recipient who requested it
1115 return sendMessage(messageBuilder
, Set
.of(recipientId
));
1118 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1119 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1120 .withId(g
.getGroupId().serialize())
1122 .withMembers(g
.getMembers()
1124 .map(this::resolveSignalServiceAddress
)
1125 .collect(Collectors
.toList()));
1128 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1129 if (attachment
.isPresent()) {
1130 group
.withAvatar(attachment
.get());
1132 } catch (IOException e
) {
1133 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1136 return SignalServiceDataMessage
.newBuilder()
1137 .asGroupMessage(group
.build())
1138 .withExpiration(g
.getMessageExpirationTime());
1141 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1142 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1143 .withRevision(g
.getGroup().getRevision())
1144 .withSignedGroupChange(signedGroupChange
);
1145 return SignalServiceDataMessage
.newBuilder()
1146 .asGroupMessage(group
.build())
1147 .withExpiration(g
.getMessageExpirationTime());
1150 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1151 GroupIdV1 groupId
, SignalServiceAddress recipient
1152 ) throws IOException
{
1153 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1155 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1157 // Send group info request message to the recipient who sent us a message with this groupId
1158 return sendMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1162 SignalServiceAddress remoteAddress
, long messageId
1163 ) throws IOException
, UntrustedIdentityException
{
1164 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1166 System
.currentTimeMillis());
1168 createMessageSender().sendReceipt(remoteAddress
,
1169 unidentifiedAccessHelper
.getAccessFor(resolveRecipient(remoteAddress
)),
1173 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1174 String messageText
, List
<String
> attachments
, List
<String
> recipients
1175 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1176 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1177 if (attachments
!= null) {
1178 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1180 // Upload attachments here, so we only upload once even for multiple recipients
1181 var messageSender
= createMessageSender();
1182 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1183 for (var attachment
: attachmentStreams
) {
1184 if (attachment
.isStream()) {
1185 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1186 } else if (attachment
.isPointer()) {
1187 attachmentPointers
.add(attachment
.asPointer());
1191 messageBuilder
.withAttachments(attachmentPointers
);
1193 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1196 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1197 String messageText
, List
<String
> attachments
1198 ) throws IOException
, AttachmentInvalidException
{
1199 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1200 if (attachments
!= null) {
1201 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1203 return sendSelfMessage(messageBuilder
);
1206 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1207 long targetSentTimestamp
, List
<String
> recipients
1208 ) throws IOException
, InvalidNumberException
{
1209 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1210 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1211 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1214 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1215 long targetSentTimestamp
, GroupId groupId
1216 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1217 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1218 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1219 return sendGroupMessage(messageBuilder
, groupId
);
1222 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1223 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1224 ) throws IOException
, InvalidNumberException
{
1225 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1226 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1228 resolveSignalServiceAddress(targetAuthorRecipientId
),
1229 targetSentTimestamp
);
1230 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1231 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1234 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1235 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1237 final var signalServiceAddresses
= getSignalServiceAddresses(recipients
);
1239 return sendMessage(messageBuilder
, signalServiceAddresses
);
1240 } catch (Exception e
) {
1241 for (var address
: signalServiceAddresses
) {
1242 handleEndSession(address
);
1248 void renewSession(RecipientId recipientId
) throws IOException
{
1249 account
.getSessionStore().archiveSessions(recipientId
);
1250 if (!recipientId
.equals(getSelfRecipientId())) {
1251 sendNullMessage(recipientId
);
1255 public String
getContactName(String number
) throws InvalidNumberException
{
1256 var contact
= account
.getContactStore().getContact(canonicalizeAndResolveRecipient(number
));
1257 return contact
== null || contact
.getName() == null ?
"" : contact
.getName();
1260 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1261 if (!account
.isMasterDevice()) {
1262 throw new NotMasterDeviceException();
1264 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1265 var contact
= account
.getContactStore().getContact(recipientId
);
1266 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1267 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1270 public void setContactBlocked(
1271 String number
, boolean blocked
1272 ) throws InvalidNumberException
, NotMasterDeviceException
{
1273 if (!account
.isMasterDevice()) {
1274 throw new NotMasterDeviceException();
1276 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1279 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1280 var contact
= account
.getContactStore().getContact(recipientId
);
1281 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1282 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1285 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1286 var group
= getGroup(groupId
);
1287 if (group
== null) {
1288 throw new GroupNotFoundException(groupId
);
1291 group
.setBlocked(blocked
);
1292 account
.getGroupStore().updateGroup(group
);
1295 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1296 var contact
= account
.getContactStore().getContact(recipientId
);
1297 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1300 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1301 account
.getContactStore()
1302 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1305 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1306 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1307 sendMessage(messageBuilder
, Set
.of(recipientId
));
1311 * Change the expiration timer for a contact
1313 public void setExpirationTimer(
1314 String number
, int messageExpirationTimer
1315 ) throws IOException
, InvalidNumberException
{
1316 var recipientId
= canonicalizeAndResolveRecipient(number
);
1317 setExpirationTimer(recipientId
, messageExpirationTimer
);
1318 sendExpirationTimerUpdate(recipientId
);
1322 * Change the expiration timer for a group
1324 private void setExpirationTimer(
1325 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1326 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1327 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1328 account
.getGroupStore().updateGroup(groupInfoV1
);
1329 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1332 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1333 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1334 sendGroupMessage(messageBuilder
, groupId
);
1338 * Upload the sticker pack from path.
1340 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1341 * @return if successful, returns the URL to install the sticker pack in the signal app
1343 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1344 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1346 var messageSender
= createMessageSender();
1348 var packKey
= KeyUtils
.createStickerUploadKey();
1349 var packId
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1351 var sticker
= new Sticker(StickerPackId
.deserialize(Hex
.fromStringCondensed(packId
)), packKey
);
1352 account
.getStickerStore().updateSticker(sticker
);
1355 return new URI("https",
1358 "pack_id=" + URLEncoder
.encode(packId
, StandardCharsets
.UTF_8
) + "&pack_key=" + URLEncoder
.encode(
1359 Hex
.toStringCondensed(packKey
),
1360 StandardCharsets
.UTF_8
)).toString();
1361 } catch (URISyntaxException e
) {
1362 throw new AssertionError(e
);
1366 public void requestAllSyncData() throws IOException
{
1367 requestSyncGroups();
1368 requestSyncContacts();
1369 requestSyncBlocked();
1370 requestSyncConfiguration();
1374 private void requestSyncGroups() throws IOException
{
1375 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1376 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1378 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1380 sendSyncMessage(message
);
1381 } catch (UntrustedIdentityException e
) {
1382 throw new AssertionError(e
);
1386 private void requestSyncContacts() throws IOException
{
1387 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1388 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1390 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1392 sendSyncMessage(message
);
1393 } catch (UntrustedIdentityException e
) {
1394 throw new AssertionError(e
);
1398 private void requestSyncBlocked() throws IOException
{
1399 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1400 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1402 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1404 sendSyncMessage(message
);
1405 } catch (UntrustedIdentityException e
) {
1406 throw new AssertionError(e
);
1410 private void requestSyncConfiguration() throws IOException
{
1411 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1412 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1414 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1416 sendSyncMessage(message
);
1417 } catch (UntrustedIdentityException e
) {
1418 throw new AssertionError(e
);
1422 private void requestSyncKeys() throws IOException
{
1423 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1424 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1426 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1428 sendSyncMessage(message
);
1429 } catch (UntrustedIdentityException e
) {
1430 throw new AssertionError(e
);
1434 private byte[] getSenderCertificate() {
1437 if (account
.isPhoneNumberShared()) {
1438 certificate
= accountManager
.getSenderCertificate();
1440 certificate
= accountManager
.getSenderCertificateForPhoneNumberPrivacy();
1442 } catch (IOException e
) {
1443 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1446 // TODO cache for a day
1450 private void sendSyncMessage(SignalServiceSyncMessage message
) throws IOException
, UntrustedIdentityException
{
1451 var messageSender
= createMessageSender();
1452 messageSender
.sendMessage(message
, unidentifiedAccessHelper
.getAccessForSync());
1455 private Set
<RecipientId
> getSignalServiceAddresses(Collection
<String
> numbers
) throws InvalidNumberException
{
1456 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1457 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1459 for (var number
: numbers
) {
1460 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1461 if (resolvedAddress
.getUuid().isPresent()) {
1462 signalServiceAddresses
.add(resolvedAddress
);
1464 addressesMissingUuid
.add(resolvedAddress
);
1468 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1469 .map(a
-> a
.getNumber().get())
1470 .collect(Collectors
.toSet());
1471 Map
<String
, UUID
> registeredUsers
;
1473 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1474 } catch (IOException e
) {
1475 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1476 registeredUsers
= Map
.of();
1479 for (var address
: addressesMissingUuid
) {
1480 final var number
= address
.getNumber().get();
1481 if (registeredUsers
.containsKey(number
)) {
1482 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1483 registeredUsers
.get(number
),
1485 signalServiceAddresses
.add(newAddress
);
1487 signalServiceAddresses
.add(address
);
1491 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1494 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1495 final var address
= resolveSignalServiceAddress(recipientId
);
1496 if (!address
.getNumber().isPresent()) {
1499 final var number
= address
.getNumber().get();
1500 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1501 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1504 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1506 return accountManager
.getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1508 serviceEnvironmentConfig
.getCdsMrenclave());
1509 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1510 throw new IOException(e
);
1514 private Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1515 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientId
> recipientIds
1516 ) throws IOException
{
1517 final var timestamp
= System
.currentTimeMillis();
1518 messageBuilder
.withTimestamp(timestamp
);
1519 getOrCreateMessagePipe();
1520 getOrCreateUnidentifiedMessagePipe();
1521 SignalServiceDataMessage message
= null;
1523 message
= messageBuilder
.build();
1524 if (message
.getGroupContext().isPresent()) {
1526 var messageSender
= createMessageSender();
1527 final var isRecipientUpdate
= false;
1528 final var recipientIdList
= new ArrayList
<>(recipientIds
);
1529 final var addresses
= recipientIdList
.stream()
1530 .map(this::resolveSignalServiceAddress
)
1531 .collect(Collectors
.toList());
1532 var result
= messageSender
.sendMessage(addresses
,
1533 unidentifiedAccessHelper
.getAccessFor(recipientIdList
),
1537 for (var r
: result
) {
1538 if (r
.getIdentityFailure() != null) {
1539 final var recipientId
= resolveRecipient(r
.getAddress());
1540 final var newIdentity
= account
.getIdentityKeyStore()
1541 .saveIdentity(recipientId
, r
.getIdentityFailure().getIdentityKey(), new Date());
1543 account
.getSessionStore().archiveSessions(recipientId
);
1548 return new Pair
<>(timestamp
, result
);
1549 } catch (UntrustedIdentityException e
) {
1550 return new Pair
<>(timestamp
, List
.of());
1553 // Send to all individually, so sync messages are sent correctly
1554 messageBuilder
.withProfileKey(account
.getProfileKey().serialize());
1555 var results
= new ArrayList
<SendMessageResult
>(recipientIds
.size());
1556 for (var recipientId
: recipientIds
) {
1557 final var contact
= account
.getContactStore().getContact(recipientId
);
1558 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1559 messageBuilder
.withExpiration(expirationTime
);
1560 message
= messageBuilder
.build();
1561 results
.add(sendMessage(recipientId
, message
));
1563 return new Pair
<>(timestamp
, results
);
1566 if (message
!= null && message
.isEndSession()) {
1567 for (var recipient
: recipientIds
) {
1568 handleEndSession(recipient
);
1574 private Pair
<Long
, SendMessageResult
> sendSelfMessage(
1575 SignalServiceDataMessage
.Builder messageBuilder
1576 ) throws IOException
{
1577 final var timestamp
= System
.currentTimeMillis();
1578 messageBuilder
.withTimestamp(timestamp
);
1579 getOrCreateMessagePipe();
1580 getOrCreateUnidentifiedMessagePipe();
1581 final var recipientId
= account
.getSelfRecipientId();
1583 final var contact
= account
.getContactStore().getContact(recipientId
);
1584 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1585 messageBuilder
.withExpiration(expirationTime
);
1587 var message
= messageBuilder
.build();
1588 final var result
= sendSelfMessage(message
);
1589 return new Pair
<>(timestamp
, result
);
1592 private SendMessageResult
sendSelfMessage(SignalServiceDataMessage message
) throws IOException
{
1593 var messageSender
= createMessageSender();
1595 var recipientId
= account
.getSelfRecipientId();
1597 final var unidentifiedAccess
= unidentifiedAccessHelper
.getAccessFor(recipientId
);
1598 var recipient
= resolveSignalServiceAddress(recipientId
);
1599 var transcript
= new SentTranscriptMessage(Optional
.of(recipient
),
1600 message
.getTimestamp(),
1602 message
.getExpiresInSeconds(),
1603 Map
.of(recipient
, unidentifiedAccess
.isPresent()),
1605 var syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
1608 var startTime
= System
.currentTimeMillis();
1609 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
1610 return SendMessageResult
.success(recipient
,
1611 unidentifiedAccess
.isPresent(),
1613 System
.currentTimeMillis() - startTime
);
1614 } catch (UntrustedIdentityException e
) {
1615 return SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey());
1619 private SendMessageResult
sendMessage(
1620 RecipientId recipientId
, SignalServiceDataMessage message
1621 ) throws IOException
{
1622 var messageSender
= createMessageSender();
1624 final var address
= resolveSignalServiceAddress(recipientId
);
1627 return messageSender
.sendMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
), message
);
1628 } catch (UnregisteredUserException e
) {
1629 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1630 return messageSender
.sendMessage(resolveSignalServiceAddress(newRecipientId
),
1631 unidentifiedAccessHelper
.getAccessFor(newRecipientId
),
1634 } catch (UntrustedIdentityException e
) {
1635 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1639 private SendMessageResult
sendNullMessage(RecipientId recipientId
) throws IOException
{
1640 var messageSender
= createMessageSender();
1642 final var address
= resolveSignalServiceAddress(recipientId
);
1645 return messageSender
.sendNullMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
));
1646 } catch (UnregisteredUserException e
) {
1647 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1648 final var newAddress
= resolveSignalServiceAddress(newRecipientId
);
1649 return messageSender
.sendNullMessage(newAddress
, unidentifiedAccessHelper
.getAccessFor(newRecipientId
));
1651 } catch (UntrustedIdentityException e
) {
1652 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1656 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
{
1657 var cipher
= new SignalServiceCipher(account
.getSelfAddress(),
1658 account
.getSignalProtocolStore(),
1660 certificateValidator
);
1661 return cipher
.decrypt(envelope
);
1664 private void handleEndSession(RecipientId recipientId
) {
1665 account
.getSessionStore().deleteAllSessions(recipientId
);
1668 private List
<HandleAction
> handleSignalServiceDataMessage(
1669 SignalServiceDataMessage message
,
1671 SignalServiceAddress source
,
1672 SignalServiceAddress destination
,
1673 boolean ignoreAttachments
1675 var actions
= new ArrayList
<HandleAction
>();
1676 if (message
.getGroupContext().isPresent()) {
1677 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1678 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1679 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1680 var group
= getGroup(groupId
);
1681 if (group
== null || group
instanceof GroupInfoV1
) {
1682 var groupV1
= (GroupInfoV1
) group
;
1683 switch (groupInfo
.getType()) {
1685 if (groupV1
== null) {
1686 groupV1
= new GroupInfoV1(groupId
);
1689 if (groupInfo
.getAvatar().isPresent()) {
1690 var avatar
= groupInfo
.getAvatar().get();
1691 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1694 if (groupInfo
.getName().isPresent()) {
1695 groupV1
.name
= groupInfo
.getName().get();
1698 if (groupInfo
.getMembers().isPresent()) {
1699 groupV1
.addMembers(groupInfo
.getMembers()
1702 .map(this::resolveRecipient
)
1703 .collect(Collectors
.toSet()));
1706 account
.getGroupStore().updateGroup(groupV1
);
1710 if (groupV1
== null && !isSync
) {
1711 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1715 if (groupV1
!= null) {
1716 groupV1
.removeMember(resolveRecipient(source
));
1717 account
.getGroupStore().updateGroup(groupV1
);
1722 if (groupV1
!= null && !isSync
) {
1723 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1728 // Received a group v1 message for a v2 group
1731 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1732 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1733 final var groupMasterKey
= groupContext
.getMasterKey();
1735 getOrMigrateGroup(groupMasterKey
,
1736 groupContext
.getRevision(),
1737 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1741 final var conversationPartnerAddress
= isSync ? destination
: source
;
1742 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1743 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1745 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1746 if (message
.getGroupContext().isPresent()) {
1747 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1748 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1749 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1750 if (group
!= null) {
1751 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1752 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1753 account
.getGroupStore().updateGroup(group
);
1756 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1757 // disappearing message timer already stored in the DecryptedGroup
1759 } else if (conversationPartnerAddress
!= null) {
1760 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1763 if (!ignoreAttachments
) {
1764 if (message
.getAttachments().isPresent()) {
1765 for (var attachment
: message
.getAttachments().get()) {
1766 downloadAttachment(attachment
);
1769 if (message
.getSharedContacts().isPresent()) {
1770 for (var contact
: message
.getSharedContacts().get()) {
1771 if (contact
.getAvatar().isPresent()) {
1772 downloadAttachment(contact
.getAvatar().get().getAttachment());
1777 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1778 final ProfileKey profileKey
;
1780 profileKey
= new ProfileKey(message
.getProfileKey().get());
1781 } catch (InvalidInputException e
) {
1782 throw new AssertionError(e
);
1784 if (source
.matches(account
.getSelfAddress())) {
1785 this.account
.setProfileKey(profileKey
);
1787 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1789 if (message
.getPreviews().isPresent()) {
1790 final var previews
= message
.getPreviews().get();
1791 for (var preview
: previews
) {
1792 if (preview
.getImage().isPresent()) {
1793 downloadAttachment(preview
.getImage().get());
1797 if (message
.getQuote().isPresent()) {
1798 final var quote
= message
.getQuote().get();
1800 for (var quotedAttachment
: quote
.getAttachments()) {
1801 final var thumbnail
= quotedAttachment
.getThumbnail();
1802 if (thumbnail
!= null) {
1803 downloadAttachment(thumbnail
);
1807 if (message
.getSticker().isPresent()) {
1808 final var messageSticker
= message
.getSticker().get();
1809 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1810 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1811 if (sticker
== null) {
1812 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1813 account
.getStickerStore().updateSticker(sticker
);
1819 private GroupInfoV2
getOrMigrateGroup(
1820 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1822 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1824 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1825 var groupInfo
= getGroup(groupId
);
1826 final GroupInfoV2 groupInfoV2
;
1827 if (groupInfo
instanceof GroupInfoV1
) {
1828 // Received a v2 group message for a v1 group, we need to locally migrate the group
1829 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1830 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1831 logger
.info("Locally migrated group {} to group v2, id: {}",
1832 groupInfo
.getGroupId().toBase64(),
1833 groupInfoV2
.getGroupId().toBase64());
1834 } else if (groupInfo
instanceof GroupInfoV2
) {
1835 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1837 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1840 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1841 DecryptedGroup group
= null;
1842 if (signedGroupChange
!= null
1843 && groupInfoV2
.getGroup() != null
1844 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1845 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1849 if (group
== null) {
1850 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1852 if (group
!= null) {
1853 storeProfileKeysFromMembers(group
);
1854 final var avatar
= group
.getAvatar();
1855 if (avatar
!= null && !avatar
.isEmpty()) {
1856 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1859 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1860 account
.getGroupStore().updateGroup(groupInfoV2
);
1866 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1867 for (var member
: group
.getMembersList()) {
1868 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1869 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1871 account
.getProfileStore()
1872 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1873 } catch (InvalidInputException ignored
) {
1878 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1879 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1880 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1881 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1882 if (actions
!= null) {
1883 queuedActions
.addAll(actions
);
1886 for (var action
: queuedActions
) {
1888 action
.execute(this);
1889 } catch (Throwable e
) {
1890 logger
.warn("Message action failed.", e
);
1895 private List
<HandleAction
> retryFailedReceivedMessage(
1896 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1898 var envelope
= cachedMessage
.loadEnvelope();
1899 if (envelope
== null) {
1902 SignalServiceContent content
= null;
1903 List
<HandleAction
> actions
= null;
1904 if (!envelope
.isReceipt()) {
1906 content
= decryptMessage(envelope
);
1907 } catch (ProtocolUntrustedIdentityException e
) {
1908 if (!envelope
.hasSource()) {
1909 final var identifier
= e
.getSender();
1910 final var recipientId
= resolveRecipient(identifier
);
1912 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1913 } catch (IOException ioException
) {
1914 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1918 } catch (Exception er
) {
1919 // All other errors are not recoverable, so delete the cached message
1920 cachedMessage
.delete();
1923 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1925 handler
.handleMessage(envelope
, content
, null);
1926 cachedMessage
.delete();
1930 public void receiveMessages(
1933 boolean returnOnTimeout
,
1934 boolean ignoreAttachments
,
1935 ReceiveMessageHandler handler
1936 ) throws IOException
{
1937 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1939 Set
<HandleAction
> queuedActions
= null;
1941 final var messagePipe
= getOrCreateMessagePipe();
1943 var hasCaughtUpWithOldMessages
= false;
1946 SignalServiceEnvelope envelope
;
1947 SignalServiceContent content
= null;
1948 Exception exception
= null;
1949 final CachedMessage
[] cachedMessage
= {null};
1951 var result
= messagePipe
.readOrEmpty(timeout
, unit
, envelope1
-> {
1952 final var recipientId
= envelope1
.hasSource()
1953 ?
resolveRecipient(envelope1
.getSourceIdentifier())
1955 // store message on disk, before acknowledging receipt to the server
1956 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1958 if (result
.isPresent()) {
1959 envelope
= result
.get();
1961 // Received indicator that server queue is empty
1962 hasCaughtUpWithOldMessages
= true;
1964 if (queuedActions
!= null) {
1965 for (var action
: queuedActions
) {
1967 action
.execute(this);
1968 } catch (Throwable e
) {
1969 logger
.warn("Message action failed.", e
);
1972 queuedActions
.clear();
1973 queuedActions
= null;
1976 // Continue to wait another timeout for new messages
1979 } catch (TimeoutException e
) {
1980 if (returnOnTimeout
) return;
1984 if (envelope
.hasSource()) {
1985 // Store uuid if we don't have it already
1986 // address/uuid in envelope is sent by server
1987 resolveRecipientTrusted(envelope
.getSourceAddress());
1989 final var notAGroupMember
= isNotAGroupMember(envelope
, content
);
1990 if (!envelope
.isReceipt()) {
1992 content
= decryptMessage(envelope
);
1993 } catch (Exception e
) {
1996 if (!envelope
.hasSource() && content
!= null) {
1997 // Store uuid if we don't have it already
1998 // address/uuid is validated by unidentified sender certificate
1999 resolveRecipientTrusted(content
.getSender());
2001 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2002 if (exception
instanceof ProtocolInvalidMessageException
) {
2003 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
2004 logger
.debug("Received invalid message, queuing renew session action.");
2005 actions
.add(new RenewSessionAction(sender
));
2007 if (hasCaughtUpWithOldMessages
) {
2008 for (var action
: actions
) {
2010 action
.execute(this);
2011 } catch (Throwable e
) {
2012 logger
.warn("Message action failed.", e
);
2016 if (queuedActions
== null) {
2017 queuedActions
= new HashSet
<>();
2019 queuedActions
.addAll(actions
);
2022 if (isMessageBlocked(envelope
, content
)) {
2023 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
2024 } else if (notAGroupMember
) {
2025 logger
.info("Ignoring a message from a non group member: {}", envelope
.getTimestamp());
2027 handler
.handleMessage(envelope
, content
, exception
);
2029 if (cachedMessage
[0] != null) {
2030 if (exception
instanceof ProtocolUntrustedIdentityException
) {
2031 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
2032 final var recipientId
= resolveRecipient(identifier
);
2033 queuedActions
.add(new RetrieveProfileAction(recipientId
));
2034 if (!envelope
.hasSource()) {
2036 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
2037 } catch (IOException ioException
) {
2038 logger
.warn("Failed to move cached message to recipient folder: {}",
2039 ioException
.getMessage());
2043 cachedMessage
[0].delete();
2049 private boolean isMessageBlocked(
2050 SignalServiceEnvelope envelope
, SignalServiceContent content
2052 SignalServiceAddress source
;
2053 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2054 source
= envelope
.getSourceAddress();
2055 } else if (content
!= null) {
2056 source
= content
.getSender();
2060 final var recipientId
= resolveRecipient(source
);
2061 if (isContactBlocked(recipientId
)) {
2065 if (content
!= null && content
.getDataMessage().isPresent()) {
2066 var message
= content
.getDataMessage().get();
2067 if (message
.getGroupContext().isPresent()) {
2068 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2069 var group
= getGroup(groupId
);
2070 if (group
!= null && group
.isBlocked()) {
2078 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2079 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2080 return isContactBlocked(recipientId
);
2083 private boolean isContactBlocked(final RecipientId recipientId
) {
2084 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2085 return sourceContact
!= null && sourceContact
.isBlocked();
2088 private boolean isNotAGroupMember(
2089 SignalServiceEnvelope envelope
, SignalServiceContent content
2091 SignalServiceAddress source
;
2092 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2093 source
= envelope
.getSourceAddress();
2094 } else if (content
!= null) {
2095 source
= content
.getSender();
2100 if (content
!= null && content
.getDataMessage().isPresent()) {
2101 var message
= content
.getDataMessage().get();
2102 if (message
.getGroupContext().isPresent()) {
2103 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2104 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2105 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2109 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2110 var group
= getGroup(groupId
);
2111 if (group
!= null && !group
.isMember(resolveRecipient(source
))) {
2119 private List
<HandleAction
> handleMessage(
2120 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2122 var actions
= new ArrayList
<HandleAction
>();
2123 if (content
!= null) {
2124 final SignalServiceAddress sender
;
2125 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2126 sender
= envelope
.getSourceAddress();
2128 sender
= content
.getSender();
2131 if (content
.getDataMessage().isPresent()) {
2132 var message
= content
.getDataMessage().get();
2134 if (content
.isNeedsReceipt()) {
2135 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2138 actions
.addAll(handleSignalServiceDataMessage(message
,
2141 account
.getSelfAddress(),
2142 ignoreAttachments
));
2144 if (content
.getSyncMessage().isPresent()) {
2145 account
.setMultiDevice(true);
2146 var syncMessage
= content
.getSyncMessage().get();
2147 if (syncMessage
.getSent().isPresent()) {
2148 var message
= syncMessage
.getSent().get();
2149 final var destination
= message
.getDestination().orNull();
2150 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2154 ignoreAttachments
));
2156 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2157 var rm
= syncMessage
.getRequest().get();
2158 if (rm
.isContactsRequest()) {
2159 actions
.add(SendSyncContactsAction
.create());
2161 if (rm
.isGroupsRequest()) {
2162 actions
.add(SendSyncGroupsAction
.create());
2164 if (rm
.isBlockedListRequest()) {
2165 actions
.add(SendSyncBlockedListAction
.create());
2167 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2169 if (syncMessage
.getGroups().isPresent()) {
2170 File tmpFile
= null;
2172 tmpFile
= IOUtils
.createTempFile();
2173 final var groupsMessage
= syncMessage
.getGroups().get();
2174 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2175 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2177 while ((g
= s
.read()) != null) {
2178 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2179 if (syncGroup
!= null) {
2180 if (g
.getName().isPresent()) {
2181 syncGroup
.name
= g
.getName().get();
2183 syncGroup
.addMembers(g
.getMembers()
2185 .map(this::resolveRecipient
)
2186 .collect(Collectors
.toSet()));
2187 if (!g
.isActive()) {
2188 syncGroup
.removeMember(account
.getSelfRecipientId());
2190 // Add ourself to the member set as it's marked as active
2191 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2193 syncGroup
.blocked
= g
.isBlocked();
2194 if (g
.getColor().isPresent()) {
2195 syncGroup
.color
= g
.getColor().get();
2198 if (g
.getAvatar().isPresent()) {
2199 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2201 syncGroup
.archived
= g
.isArchived();
2202 account
.getGroupStore().updateGroup(syncGroup
);
2206 } catch (Exception e
) {
2207 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2211 if (tmpFile
!= null) {
2213 Files
.delete(tmpFile
.toPath());
2214 } catch (IOException e
) {
2215 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2222 if (syncMessage
.getBlockedList().isPresent()) {
2223 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2224 for (var address
: blockedListMessage
.getAddresses()) {
2225 setContactBlocked(resolveRecipient(address
), true);
2227 for (var groupId
: blockedListMessage
.getGroupIds()
2229 .map(GroupId
::unknownVersion
)
2230 .collect(Collectors
.toSet())) {
2232 setGroupBlocked(groupId
, true);
2233 } catch (GroupNotFoundException e
) {
2234 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2235 groupId
.toBase64());
2239 if (syncMessage
.getContacts().isPresent()) {
2240 File tmpFile
= null;
2242 tmpFile
= IOUtils
.createTempFile();
2243 final var contactsMessage
= syncMessage
.getContacts().get();
2244 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2245 .asPointer(), tmpFile
)) {
2246 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2248 while ((c
= s
.read()) != null) {
2249 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2250 account
.setProfileKey(c
.getProfileKey().get());
2252 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2253 var contact
= account
.getContactStore().getContact(recipientId
);
2254 final var builder
= contact
== null
2255 ? Contact
.newBuilder()
2256 : Contact
.newBuilder(contact
);
2257 if (c
.getName().isPresent()) {
2258 builder
.withName(c
.getName().get());
2260 if (c
.getColor().isPresent()) {
2261 builder
.withColor(c
.getColor().get());
2263 if (c
.getProfileKey().isPresent()) {
2264 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2266 if (c
.getVerified().isPresent()) {
2267 final var verifiedMessage
= c
.getVerified().get();
2268 account
.getIdentityKeyStore()
2269 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2270 verifiedMessage
.getIdentityKey(),
2271 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2273 if (c
.getExpirationTimer().isPresent()) {
2274 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2276 builder
.withBlocked(c
.isBlocked());
2277 builder
.withArchived(c
.isArchived());
2278 account
.getContactStore().storeContact(recipientId
, builder
.build());
2280 if (c
.getAvatar().isPresent()) {
2281 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2285 } catch (Exception e
) {
2286 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2290 if (tmpFile
!= null) {
2292 Files
.delete(tmpFile
.toPath());
2293 } catch (IOException e
) {
2294 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2301 if (syncMessage
.getVerified().isPresent()) {
2302 final var verifiedMessage
= syncMessage
.getVerified().get();
2303 account
.getIdentityKeyStore()
2304 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2305 verifiedMessage
.getIdentityKey(),
2306 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2308 if (syncMessage
.getStickerPackOperations().isPresent()) {
2309 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2310 for (var m
: stickerPackOperationMessages
) {
2311 if (!m
.getPackId().isPresent()) {
2314 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2315 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2316 if (sticker
== null) {
2317 if (!m
.getPackKey().isPresent()) {
2320 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2322 sticker
.setInstalled(!m
.getType().isPresent()
2323 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
);
2324 account
.getStickerStore().updateSticker(sticker
);
2327 if (syncMessage
.getFetchType().isPresent()) {
2328 switch (syncMessage
.getFetchType().get()) {
2330 getRecipientProfile(account
.getSelfRecipientId(), true);
2331 case STORAGE_MANIFEST
:
2335 if (syncMessage
.getKeys().isPresent()) {
2336 final var keysMessage
= syncMessage
.getKeys().get();
2337 if (keysMessage
.getStorageService().isPresent()) {
2338 final var storageKey
= keysMessage
.getStorageService().get();
2339 account
.setStorageKey(storageKey
);
2342 if (syncMessage
.getConfiguration().isPresent()) {
2350 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2352 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2353 } catch (IOException e
) {
2354 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2358 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2360 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2361 } catch (IOException e
) {
2362 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2366 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2368 avatarStore
.storeGroupAvatar(groupId
,
2369 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2370 } catch (IOException e
) {
2371 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2375 private void downloadProfileAvatar(
2376 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2379 avatarStore
.storeProfileAvatar(address
,
2380 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2381 } catch (Throwable e
) {
2382 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2386 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2387 return attachmentStore
.getAttachmentFile(attachmentId
);
2390 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2391 if (!attachment
.isPointer()) {
2392 logger
.warn("Invalid state, can't store an attachment stream.");
2395 var pointer
= attachment
.asPointer();
2396 if (pointer
.getPreview().isPresent()) {
2397 final var preview
= pointer
.getPreview().get();
2399 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2400 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2401 } catch (IOException e
) {
2402 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2407 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2408 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2409 } catch (IOException e
) {
2410 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2414 private void retrieveGroupV2Avatar(
2415 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2416 ) throws IOException
{
2417 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
2419 var tmpFile
= IOUtils
.createTempFile();
2420 try (InputStream input
= messageReceiver
.retrieveGroupsV2ProfileAvatar(cdnKey
,
2422 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2423 var encryptedData
= IOUtils
.readFully(input
);
2425 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2426 outputStream
.write(decryptedData
);
2429 Files
.delete(tmpFile
.toPath());
2430 } catch (IOException e
) {
2431 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2438 private void retrieveProfileAvatar(
2439 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2440 ) throws IOException
{
2441 var tmpFile
= IOUtils
.createTempFile();
2442 try (var input
= messageReceiver
.retrieveProfileAvatar(avatarPath
,
2445 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2446 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2447 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2450 Files
.delete(tmpFile
.toPath());
2451 } catch (IOException e
) {
2452 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2459 private void retrieveAttachment(
2460 final SignalServiceAttachment attachment
, final OutputStream outputStream
2461 ) throws IOException
{
2462 if (attachment
.isPointer()) {
2463 var pointer
= attachment
.asPointer();
2464 retrieveAttachmentPointer(pointer
, outputStream
);
2466 var stream
= attachment
.asStream();
2467 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2471 private void retrieveAttachmentPointer(
2472 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2473 ) throws IOException
{
2474 var tmpFile
= IOUtils
.createTempFile();
2475 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2476 IOUtils
.copyStream(input
, outputStream
);
2477 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2478 throw new IOException(e
);
2481 Files
.delete(tmpFile
.toPath());
2482 } catch (IOException e
) {
2483 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2490 private InputStream
retrieveAttachmentAsStream(
2491 SignalServiceAttachmentPointer pointer
, File tmpFile
2492 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2493 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2496 void sendGroups() throws IOException
, UntrustedIdentityException
{
2497 var groupsFile
= IOUtils
.createTempFile();
2500 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2501 var out
= new DeviceGroupsOutputStream(fos
);
2502 for (var record : getGroups()) {
2503 if (record instanceof GroupInfoV1
) {
2504 var groupInfo
= (GroupInfoV1
) record;
2505 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2506 Optional
.fromNullable(groupInfo
.name
),
2507 groupInfo
.getMembers()
2509 .map(this::resolveSignalServiceAddress
)
2510 .collect(Collectors
.toList()),
2511 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2512 groupInfo
.isMember(account
.getSelfRecipientId()),
2513 Optional
.of(groupInfo
.messageExpirationTime
),
2514 Optional
.fromNullable(groupInfo
.color
),
2517 groupInfo
.archived
));
2522 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2523 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2524 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2525 .withStream(groupsFileStream
)
2526 .withContentType("application/octet-stream")
2527 .withLength(groupsFile
.length())
2530 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2535 Files
.delete(groupsFile
.toPath());
2536 } catch (IOException e
) {
2537 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2542 public void sendContacts() throws IOException
, UntrustedIdentityException
{
2543 var contactsFile
= IOUtils
.createTempFile();
2546 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2547 var out
= new DeviceContactsOutputStream(fos
);
2548 for (var contactPair
: account
.getContactStore().getContacts()) {
2549 final var recipientId
= contactPair
.first();
2550 final var contact
= contactPair
.second();
2551 final var address
= resolveSignalServiceAddress(recipientId
);
2553 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2554 VerifiedMessage verifiedMessage
= null;
2555 if (currentIdentity
!= null) {
2556 verifiedMessage
= new VerifiedMessage(address
,
2557 currentIdentity
.getIdentityKey(),
2558 currentIdentity
.getTrustLevel().toVerifiedState(),
2559 currentIdentity
.getDateAdded().getTime());
2562 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2563 out
.write(new DeviceContact(address
,
2564 Optional
.fromNullable(contact
.getName()),
2565 createContactAvatarAttachment(address
),
2566 Optional
.fromNullable(contact
.getColor()),
2567 Optional
.fromNullable(verifiedMessage
),
2568 Optional
.fromNullable(profileKey
),
2569 contact
.isBlocked(),
2570 Optional
.of(contact
.getMessageExpirationTime()),
2572 contact
.isArchived()));
2575 if (account
.getProfileKey() != null) {
2576 // Send our own profile key as well
2577 out
.write(new DeviceContact(account
.getSelfAddress(),
2582 Optional
.of(account
.getProfileKey()),
2590 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2591 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2592 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2593 .withStream(contactsFileStream
)
2594 .withContentType("application/octet-stream")
2595 .withLength(contactsFile
.length())
2598 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
2603 Files
.delete(contactsFile
.toPath());
2604 } catch (IOException e
) {
2605 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2610 void sendBlockedList() throws IOException
, UntrustedIdentityException
{
2611 var addresses
= new ArrayList
<SignalServiceAddress
>();
2612 for (var record : account
.getContactStore().getContacts()) {
2613 if (record.second().isBlocked()) {
2614 addresses
.add(resolveSignalServiceAddress(record.first()));
2617 var groupIds
= new ArrayList
<byte[]>();
2618 for (var record : getGroups()) {
2619 if (record.isBlocked()) {
2620 groupIds
.add(record.getGroupId().serialize());
2623 sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2626 private void sendVerifiedMessage(
2627 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2628 ) throws IOException
, UntrustedIdentityException
{
2629 var verifiedMessage
= new VerifiedMessage(destination
,
2631 trustLevel
.toVerifiedState(),
2632 System
.currentTimeMillis());
2633 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2636 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2637 return account
.getContactStore().getContacts();
2640 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2641 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2642 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2643 if (recipient
== null) {
2647 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2648 return recipient
.getContact().getName();
2651 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2652 return recipient
.getProfile().getDisplayName();
2658 public GroupInfo
getGroup(GroupId groupId
) {
2659 final var group
= account
.getGroupStore().getGroup(groupId
);
2660 if (group
instanceof GroupInfoV2
&& ((GroupInfoV2
) group
).getGroup() == null) {
2661 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2662 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2663 account
.getGroupStore().updateGroup(group
);
2668 public List
<IdentityInfo
> getIdentities() {
2669 return account
.getIdentityKeyStore().getIdentities();
2672 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2673 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2674 return identity
== null ? List
.of() : List
.of(identity
);
2678 * Trust this the identity with this fingerprint
2680 * @param name username of the identity
2681 * @param fingerprint Fingerprint
2683 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2684 var recipientId
= canonicalizeAndResolveRecipient(name
);
2685 return trustIdentity(recipientId
,
2686 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2687 TrustLevel
.TRUSTED_VERIFIED
);
2691 * Trust this the identity with this safety number
2693 * @param name username of the identity
2694 * @param safetyNumber Safety number
2696 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2697 var recipientId
= canonicalizeAndResolveRecipient(name
);
2698 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2699 return trustIdentity(recipientId
,
2700 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2701 TrustLevel
.TRUSTED_VERIFIED
);
2705 * Trust all keys of this identity without verification
2707 * @param name username of the identity
2709 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2710 var recipientId
= canonicalizeAndResolveRecipient(name
);
2711 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2714 private boolean trustIdentity(
2715 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2717 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2718 if (identity
== null) {
2722 if (!verifier
.apply(identity
.getIdentityKey())) {
2726 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2728 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2729 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2730 } catch (IOException
| UntrustedIdentityException e
) {
2731 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2737 public String
computeSafetyNumber(
2738 SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
2740 return Utils
.computeSafetyNumber(ServiceConfig
.capabilities
.isUuid(),
2741 account
.getSelfAddress(),
2742 getIdentityKeyPair().getPublicKey(),
2748 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2749 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2751 return resolveSignalServiceAddress(address
);
2755 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2756 if (address
.matches(account
.getSelfAddress())) {
2757 return account
.getSelfAddress();
2760 return account
.getRecipientStore().resolveServiceAddress(address
);
2763 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2764 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2767 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2768 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2770 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2772 return resolveRecipient(canonicalizedNumber
);
2775 private RecipientId
resolveRecipient(final String identifier
) {
2776 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2778 return resolveRecipient(address
);
2781 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2782 return account
.getRecipientStore().resolveRecipient(address
);
2785 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2786 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2790 public void close() throws IOException
{
2794 void close(boolean closeAccount
) throws IOException
{
2795 executor
.shutdown();
2797 if (messagePipe
!= null) {
2798 messagePipe
.shutdown();
2802 if (unidentifiedMessagePipe
!= null) {
2803 unidentifiedMessagePipe
.shutdown();
2804 unidentifiedMessagePipe
= null;
2807 if (closeAccount
&& account
!= null) {
2813 public interface ReceiveMessageHandler
{
2815 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);