2 Copyright (C) 2015-2021 AsamK and contributors
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
.manager
;
19 import org
.asamk
.signal
.manager
.api
.Device
;
20 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
21 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
22 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
23 import org
.asamk
.signal
.manager
.groups
.GroupId
;
24 import org
.asamk
.signal
.manager
.groups
.GroupIdV1
;
25 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
26 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
27 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
28 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
29 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
30 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
31 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
32 import org
.asamk
.signal
.manager
.helper
.GroupV2Helper
;
33 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
34 import org
.asamk
.signal
.manager
.helper
.ProfileHelper
;
35 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
36 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
37 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
38 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
39 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
40 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
41 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
42 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
43 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
44 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
45 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
46 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
47 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
48 import org
.asamk
.signal
.manager
.util
.IOUtils
;
49 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
50 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
51 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
52 import org
.asamk
.signal
.manager
.util
.Utils
;
53 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
54 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
55 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
56 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
57 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
58 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
59 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
60 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
61 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
62 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
63 import org
.signal
.libsignal
.metadata
.SelfSendException
;
64 import org
.signal
.libsignal
.metadata
.certificate
.CertificateValidator
;
65 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
66 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
67 import org
.signal
.zkgroup
.InvalidInputException
;
68 import org
.signal
.zkgroup
.VerificationFailedException
;
69 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
70 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
71 import org
.signal
.zkgroup
.profiles
.ClientZkProfileOperations
;
72 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
73 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
74 import org
.slf4j
.Logger
;
75 import org
.slf4j
.LoggerFactory
;
76 import org
.whispersystems
.libsignal
.IdentityKey
;
77 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
78 import org
.whispersystems
.libsignal
.InvalidKeyException
;
79 import org
.whispersystems
.libsignal
.InvalidMessageException
;
80 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
81 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
82 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
83 import org
.whispersystems
.libsignal
.util
.Pair
;
84 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
85 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
86 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
87 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
88 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
89 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
90 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
91 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
92 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
93 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
94 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Api
;
95 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
96 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
97 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
98 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
99 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
100 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
101 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
102 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
103 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
104 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
105 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
106 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupV2
;
107 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
108 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
109 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
110 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
111 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
112 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
113 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
114 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
117 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
118 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
119 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
120 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
121 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
122 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
123 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
124 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
125 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
126 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
127 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
128 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
129 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
130 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
131 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
132 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
133 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
134 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
135 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
136 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
137 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
138 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
139 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
141 import java
.io
.Closeable
;
143 import java
.io
.FileInputStream
;
144 import java
.io
.FileOutputStream
;
145 import java
.io
.IOException
;
146 import java
.io
.InputStream
;
147 import java
.io
.OutputStream
;
149 import java
.net
.URISyntaxException
;
150 import java
.net
.URLEncoder
;
151 import java
.nio
.charset
.StandardCharsets
;
152 import java
.nio
.file
.Files
;
153 import java
.security
.SignatureException
;
154 import java
.util
.ArrayList
;
155 import java
.util
.Arrays
;
156 import java
.util
.Base64
;
157 import java
.util
.Collection
;
158 import java
.util
.Date
;
159 import java
.util
.HashSet
;
160 import java
.util
.List
;
161 import java
.util
.Map
;
162 import java
.util
.Set
;
163 import java
.util
.UUID
;
164 import java
.util
.concurrent
.ExecutorService
;
165 import java
.util
.concurrent
.Executors
;
166 import java
.util
.concurrent
.TimeUnit
;
167 import java
.util
.concurrent
.TimeoutException
;
168 import java
.util
.concurrent
.locks
.ReentrantLock
;
169 import java
.util
.function
.Function
;
170 import java
.util
.stream
.Collectors
;
172 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
174 public class Manager
implements Closeable
{
176 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
178 private final CertificateValidator certificateValidator
;
180 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
181 private final String userAgent
;
183 private SignalAccount account
;
184 private final SignalServiceAccountManager accountManager
;
185 private final GroupsV2Api groupsV2Api
;
186 private final GroupsV2Operations groupsV2Operations
;
187 private final SignalServiceMessageReceiver messageReceiver
;
188 private final ClientZkProfileOperations clientZkProfileOperations
;
190 private final ExecutorService executor
= Executors
.newCachedThreadPool();
192 private SignalServiceMessagePipe messagePipe
= null;
193 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
195 private final UnidentifiedAccessHelper unidentifiedAccessHelper
;
196 private final ProfileHelper profileHelper
;
197 private final GroupV2Helper groupV2Helper
;
198 private final PinHelper pinHelper
;
199 private final AvatarStore avatarStore
;
200 private final AttachmentStore attachmentStore
;
201 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
202 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
205 public Lock
acquire() {
207 return LEGACY_LOCK
::unlock
;
212 SignalAccount account
,
213 PathConfig pathConfig
,
214 ServiceEnvironmentConfig serviceEnvironmentConfig
,
217 this.account
= account
;
218 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
219 this.certificateValidator
= new CertificateValidator(serviceEnvironmentConfig
.getUnidentifiedSenderTrustRoot());
220 this.userAgent
= userAgent
;
221 this.groupsV2Operations
= capabilities
.isGv2() ?
new GroupsV2Operations(ClientZkOperations
.create(
222 serviceEnvironmentConfig
.getSignalServiceConfiguration())) : null;
223 final SleepTimer timer
= new UptimeSleepTimer();
224 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
225 new DynamicCredentialsProvider(account
.getUuid(),
226 account
.getUsername(),
227 account
.getPassword(),
228 account
.getDeviceId()),
231 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
233 this.groupsV2Api
= accountManager
.getGroupsV2Api();
234 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
235 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
236 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
237 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
240 this.pinHelper
= new PinHelper(keyBackupService
);
241 this.clientZkProfileOperations
= capabilities
.isGv2()
242 ? ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration())
243 .getProfileOperations()
245 this.messageReceiver
= new SignalServiceMessageReceiver(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
247 account
.getUsername(),
248 account
.getPassword(),
249 account
.getDeviceId(),
253 clientZkProfileOperations
,
254 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
256 this.unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
257 account
.getProfileStore()::getProfileKey
,
258 this::getRecipientProfile
,
259 this::getSenderCertificate
);
260 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
261 unidentifiedAccessHelper
::getAccessFor
,
262 unidentified
-> unidentified ?
getOrCreateUnidentifiedMessagePipe() : getOrCreateMessagePipe(),
263 () -> messageReceiver
,
264 this::resolveSignalServiceAddress
);
265 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
266 this::getRecipientProfile
,
267 account
::getSelfRecipientId
,
270 this::getGroupAuthForToday
,
271 this::resolveSignalServiceAddress
);
272 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
273 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
276 public String
getUsername() {
277 return account
.getUsername();
280 public SignalServiceAddress
getSelfAddress() {
281 return account
.getSelfAddress();
284 public RecipientId
getSelfRecipientId() {
285 return account
.getSelfRecipientId();
288 private IdentityKeyPair
getIdentityKeyPair() {
289 return account
.getIdentityKeyPair();
292 public int getDeviceId() {
293 return account
.getDeviceId();
296 public static Manager
init(
297 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
298 ) throws IOException
, NotRegisteredException
{
299 var pathConfig
= PathConfig
.createDefault(settingsPath
);
301 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
302 throw new NotRegisteredException();
305 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
307 if (!account
.isRegistered()) {
308 throw new NotRegisteredException();
311 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
313 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
316 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
317 var pathConfig
= PathConfig
.createDefault(settingsPath
);
318 final var dataPath
= pathConfig
.getDataPath();
319 final var files
= dataPath
.listFiles();
325 return Arrays
.stream(files
)
326 .filter(File
::isFile
)
328 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
329 .collect(Collectors
.toList());
332 public void checkAccountState() throws IOException
{
333 if (account
.getLastReceiveTimestamp() == 0) {
334 logger
.warn("The Signal protocol expects that incoming messages are regularly received.");
336 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
337 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
340 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
344 if (accountManager
.getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
347 if (account
.getUuid() == null) {
348 account
.setUuid(accountManager
.getOwnUuid());
350 updateAccountAttributes();
354 * This is used for checking a set of phone numbers for registration on Signal
356 * @param numbers The set of phone number in question
357 * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
358 * @throws IOException if its unable to get the contacts to check if they're registered
360 public Map
<String
, Boolean
> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
361 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
362 var contactDetails
= getRegisteredUsers(numbers
);
364 var registeredUsers
= contactDetails
.keySet();
366 return numbers
.stream().collect(Collectors
.toMap(x
-> x
, registeredUsers
::contains
));
369 public void updateAccountAttributes() throws IOException
{
370 accountManager
.setAccountAttributes(account
.getEncryptedDeviceName(),
372 account
.getLocalRegistrationId(),
374 // set legacy pin only if no KBS master key is set
375 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
376 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
377 account
.getSelfUnidentifiedAccessKey(),
378 account
.isUnrestrictedUnidentifiedAccess(),
380 account
.isDiscoverableByPhoneNumber());
384 * @param givenName if null, the previous givenName will be kept
385 * @param familyName if null, the previous familyName will be kept
386 * @param about if null, the previous about text will be kept
387 * @param aboutEmoji if null, the previous about emoji will be kept
388 * @param avatar if avatar is null the image from the local avatar store is used (if present),
390 public void setProfile(
391 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
392 ) throws IOException
{
393 var profile
= getRecipientProfile(account
.getSelfRecipientId());
394 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
395 if (givenName
!= null) {
396 builder
.withGivenName(givenName
);
398 if (familyName
!= null) {
399 builder
.withFamilyName(familyName
);
402 builder
.withAbout(about
);
404 if (aboutEmoji
!= null) {
405 builder
.withAboutEmoji(aboutEmoji
);
407 var newProfile
= builder
.build();
409 try (final var streamDetails
= avatar
== null
410 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
411 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
412 accountManager
.setVersionedProfile(account
.getUuid(),
413 account
.getProfileKey(),
414 newProfile
.getInternalServiceName(),
415 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
416 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
421 if (avatar
!= null) {
422 if (avatar
.isPresent()) {
423 avatarStore
.storeProfileAvatar(getSelfAddress(),
424 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
426 avatarStore
.deleteProfileAvatar(getSelfAddress());
429 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
432 sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
433 } catch (UntrustedIdentityException ignored
) {
437 public void unregister() throws IOException
{
438 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
439 // If this is the master device, other users can't send messages to this number anymore.
440 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
441 accountManager
.setGcmId(Optional
.absent());
443 account
.setRegistered(false);
446 public void deleteAccount() throws IOException
{
447 accountManager
.deleteAccount();
449 account
.setRegistered(false);
452 public List
<Device
> getLinkedDevices() throws IOException
{
453 var devices
= accountManager
.getDevices();
454 account
.setMultiDevice(devices
.size() > 1);
455 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
456 return devices
.stream().map(d
-> {
457 String deviceName
= d
.getName();
458 if (deviceName
!= null) {
460 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
461 } catch (IOException e
) {
462 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
465 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
466 }).collect(Collectors
.toList());
469 public void removeLinkedDevices(int deviceId
) throws IOException
{
470 accountManager
.removeDevice(deviceId
);
471 var devices
= accountManager
.getDevices();
472 account
.setMultiDevice(devices
.size() > 1);
475 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
476 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
478 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
481 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
482 var identityKeyPair
= getIdentityKeyPair();
483 var verificationCode
= accountManager
.getNewDeviceVerificationCode();
485 accountManager
.addDevice(deviceIdentifier
,
488 Optional
.of(account
.getProfileKey().serialize()),
490 account
.setMultiDevice(true);
493 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
494 if (!account
.isMasterDevice()) {
495 throw new RuntimeException("Only master device can set a PIN");
497 if (pin
.isPresent()) {
498 final var masterKey
= account
.getPinMasterKey() != null
499 ? account
.getPinMasterKey()
500 : KeyUtils
.createMasterKey();
502 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
504 account
.setRegistrationLockPin(pin
.get(), masterKey
);
506 // Remove legacy registration lock
507 accountManager
.removeRegistrationLockV1();
510 pinHelper
.removeRegistrationLockPin();
512 account
.setRegistrationLockPin(null, null);
516 void refreshPreKeys() throws IOException
{
517 var oneTimePreKeys
= generatePreKeys();
518 final var identityKeyPair
= getIdentityKeyPair();
519 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
521 accountManager
.setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
524 private List
<PreKeyRecord
> generatePreKeys() {
525 final var offset
= account
.getPreKeyIdOffset();
527 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
528 account
.addPreKeys(records
);
533 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
534 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
536 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
537 account
.addSignedPreKey(record);
542 private SignalServiceMessagePipe
getOrCreateMessagePipe() {
543 if (messagePipe
== null) {
544 messagePipe
= messageReceiver
.createMessagePipe();
549 private SignalServiceMessagePipe
getOrCreateUnidentifiedMessagePipe() {
550 if (unidentifiedMessagePipe
== null) {
551 unidentifiedMessagePipe
= messageReceiver
.createUnidentifiedMessagePipe();
553 return unidentifiedMessagePipe
;
556 private SignalServiceMessageSender
createMessageSender() {
557 return new SignalServiceMessageSender(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
559 account
.getUsername(),
560 account
.getPassword(),
561 account
.getDeviceId(),
562 account
.getSignalProtocolStore(),
565 account
.isMultiDevice(),
566 Optional
.fromNullable(messagePipe
),
567 Optional
.fromNullable(unidentifiedMessagePipe
),
569 clientZkProfileOperations
,
571 ServiceConfig
.MAX_ENVELOPE_SIZE
,
572 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
575 public Profile
getRecipientProfile(
576 RecipientId recipientId
578 return getRecipientProfile(recipientId
, false);
581 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
583 Profile
getRecipientProfile(
584 RecipientId recipientId
, boolean force
586 var profile
= account
.getProfileStore().getProfile(recipientId
);
588 var now
= System
.currentTimeMillis();
589 // Profiles are cached for 24h before retrieving them again, unless forced
590 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
594 synchronized (pendingProfileRequest
) {
595 if (pendingProfileRequest
.contains(recipientId
)) {
598 pendingProfileRequest
.add(recipientId
);
600 final SignalServiceProfile encryptedProfile
;
602 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
604 synchronized (pendingProfileRequest
) {
605 pendingProfileRequest
.remove(recipientId
);
608 if (encryptedProfile
== null) {
612 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
613 if (profileKey
== null) {
614 profile
= new Profile(System
.currentTimeMillis(),
619 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
620 ProfileUtils
.getCapabilities(encryptedProfile
));
622 profile
= decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
624 account
.getProfileStore().storeProfile(recipientId
, profile
);
629 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
631 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
632 } catch (IOException e
) {
633 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
638 private ProfileAndCredential
retrieveProfileAndCredential(
639 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
640 ) throws IOException
{
641 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
642 final var profile
= profileAndCredential
.getProfile();
645 var newIdentity
= account
.getIdentityKeyStore()
646 .saveIdentity(recipientId
,
647 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
651 account
.getSessionStore().archiveSessions(recipientId
);
653 } catch (InvalidKeyException ignored
) {
654 logger
.warn("Got invalid identity key in profile for {}",
655 resolveSignalServiceAddress(recipientId
).getIdentifier());
657 return profileAndCredential
;
660 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
661 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
662 if (profileKeyCredential
!= null) {
663 return profileKeyCredential
;
666 ProfileAndCredential profileAndCredential
;
668 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
669 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
670 } catch (IOException e
) {
671 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
675 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
676 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
678 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
679 if (profileKey
!= null) {
680 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
682 profileAndCredential
.getProfile());
683 account
.getProfileStore().storeProfile(recipientId
, profile
);
686 return profileKeyCredential
;
689 private Profile
decryptProfileAndDownloadAvatar(
690 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
692 if (encryptedProfile
.getAvatar() != null) {
693 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
696 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
699 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
700 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
701 if (streamDetails
== null) {
702 return Optional
.absent();
705 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
708 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
709 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
710 if (streamDetails
== null) {
711 return Optional
.absent();
714 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
717 private GroupInfo
getGroupForSending(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
718 var g
= getGroup(groupId
);
720 throw new GroupNotFoundException(groupId
);
722 if (!g
.isMember(account
.getSelfRecipientId())) {
723 throw new NotAGroupMemberException(groupId
, g
.getTitle());
728 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
729 var g
= getGroup(groupId
);
731 throw new GroupNotFoundException(groupId
);
733 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
734 throw new NotAGroupMemberException(groupId
, g
.getTitle());
739 public List
<GroupInfo
> getGroups() {
740 return account
.getGroupStore().getGroups();
743 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
744 String messageText
, List
<String
> attachments
, GroupId groupId
745 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
746 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
747 if (attachments
!= null) {
748 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
751 return sendGroupMessage(messageBuilder
, groupId
);
754 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
755 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
756 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
757 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
758 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
760 resolveSignalServiceAddress(targetAuthorRecipientId
),
761 targetSentTimestamp
);
762 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
764 return sendGroupMessage(messageBuilder
, groupId
);
767 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
768 SignalServiceDataMessage
.Builder messageBuilder
, GroupId groupId
769 ) throws IOException
, GroupNotFoundException
, NotAGroupMemberException
{
770 final var g
= getGroupForSending(groupId
);
772 GroupUtils
.setGroupContext(messageBuilder
, g
);
773 messageBuilder
.withExpiration(g
.getMessageExpirationTime());
775 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
778 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(
779 GroupId groupId
, Set
<String
> groupAdmins
780 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, InvalidNumberException
, LastGroupAdminException
{
781 SignalServiceDataMessage
.Builder messageBuilder
;
783 final var g
= getGroupForUpdating(groupId
);
784 if (g
instanceof GroupInfoV1
) {
785 var groupInfoV1
= (GroupInfoV1
) g
;
786 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
).withId(groupId
.serialize()).build();
787 messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
788 groupInfoV1
.removeMember(account
.getSelfRecipientId());
789 account
.getGroupStore().updateGroup(groupInfoV1
);
791 final var groupInfoV2
= (GroupInfoV2
) g
;
792 final var currentAdmins
= g
.getAdminMembers();
793 final var newAdmins
= getSignalServiceAddresses(groupAdmins
);
794 newAdmins
.removeAll(currentAdmins
);
795 newAdmins
.retainAll(g
.getMembers());
796 if (currentAdmins
.contains(getSelfRecipientId())
797 && currentAdmins
.size() == 1
798 && g
.getMembers().size() > 1
799 && newAdmins
.size() == 0) {
800 // Last admin can't leave the group, unless she's also the last member
801 throw new LastGroupAdminException(g
.getGroupId(), g
.getTitle());
803 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
804 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
805 messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
806 account
.getGroupStore().updateGroup(groupInfoV2
);
809 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
812 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
813 String name
, List
<String
> members
, File avatarFile
814 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
815 return createGroup(name
, members
== null ?
null : getSignalServiceAddresses(members
), avatarFile
);
818 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
819 String name
, Set
<RecipientId
> members
, File avatarFile
820 ) throws IOException
, AttachmentInvalidException
{
821 final var selfRecipientId
= account
.getSelfRecipientId();
822 if (members
!= null && members
.contains(selfRecipientId
)) {
823 members
= new HashSet
<>(members
);
824 members
.remove(selfRecipientId
);
827 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
828 members
== null ? Set
.of() : members
,
831 SignalServiceDataMessage
.Builder messageBuilder
;
832 if (gv2Pair
== null) {
833 // Failed to create v2 group, creating v1 group instead
834 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
835 gv1
.addMembers(List
.of(selfRecipientId
));
836 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
837 return new Pair
<>(gv1
.getGroupId(), result
.second());
840 final var gv2
= gv2Pair
.first();
841 final var decryptedGroup
= gv2Pair
.second();
843 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
844 if (avatarFile
!= null) {
845 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
846 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
848 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
849 account
.getGroupStore().updateGroup(gv2
);
851 final var result
= sendMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
852 return new Pair
<>(gv2
.getGroupId(), result
.second());
855 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
859 List
<String
> members
,
860 List
<String
> removeMembers
,
862 List
<String
> removeAdmins
,
863 boolean resetGroupLink
,
864 GroupLinkState groupLinkState
,
865 GroupPermission addMemberPermission
,
866 GroupPermission editDetailsPermission
,
868 Integer expirationTimer
869 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
870 return updateGroup(groupId
,
873 members
== null ?
null : getSignalServiceAddresses(members
),
874 removeMembers
== null ?
null : getSignalServiceAddresses(removeMembers
),
875 admins
== null ?
null : getSignalServiceAddresses(admins
),
876 removeAdmins
== null ?
null : getSignalServiceAddresses(removeAdmins
),
880 editDetailsPermission
,
885 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
886 final GroupId groupId
,
888 final String description
,
889 final Set
<RecipientId
> members
,
890 final Set
<RecipientId
> removeMembers
,
891 final Set
<RecipientId
> admins
,
892 final Set
<RecipientId
> removeAdmins
,
893 final boolean resetGroupLink
,
894 final GroupLinkState groupLinkState
,
895 final GroupPermission addMemberPermission
,
896 final GroupPermission editDetailsPermission
,
897 final File avatarFile
,
898 final Integer expirationTimer
899 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
900 var group
= getGroupForUpdating(groupId
);
902 if (group
instanceof GroupInfoV2
) {
903 return updateGroupV2((GroupInfoV2
) group
,
913 editDetailsPermission
,
918 final var gv1
= (GroupInfoV1
) group
;
919 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
920 if (expirationTimer
!= null) {
921 setExpirationTimer(gv1
, expirationTimer
);
926 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
927 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
928 ) throws IOException
, AttachmentInvalidException
{
929 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
930 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
932 account
.getGroupStore().updateGroup(gv1
);
934 return sendMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
937 private void updateGroupV1Details(
938 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
939 ) throws IOException
{
944 if (members
!= null) {
945 final var newMemberAddresses
= members
.stream()
946 .filter(member
-> !g
.isMember(member
))
947 .map(this::resolveSignalServiceAddress
)
948 .collect(Collectors
.toList());
949 final var newE164Members
= new HashSet
<String
>();
950 for (var member
: newMemberAddresses
) {
951 if (!member
.getNumber().isPresent()) {
954 newE164Members
.add(member
.getNumber().get());
957 final var registeredUsers
= getRegisteredUsers(newE164Members
);
958 if (registeredUsers
.size() != newE164Members
.size()) {
959 // Some of the new members are not registered on Signal
960 newE164Members
.removeAll(registeredUsers
.keySet());
961 throw new IOException("Failed to add members "
962 + String
.join(", ", newE164Members
)
963 + " to group: Not registered on Signal");
966 g
.addMembers(members
);
969 if (avatarFile
!= null) {
970 avatarStore
.storeGroupAvatar(g
.getGroupId(),
971 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
975 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
976 final GroupInfoV2 group
,
978 final String description
,
979 final Set
<RecipientId
> members
,
980 final Set
<RecipientId
> removeMembers
,
981 final Set
<RecipientId
> admins
,
982 final Set
<RecipientId
> removeAdmins
,
983 final boolean resetGroupLink
,
984 final GroupLinkState groupLinkState
,
985 final GroupPermission addMemberPermission
,
986 final GroupPermission editDetailsPermission
,
987 final File avatarFile
,
988 Integer expirationTimer
989 ) throws IOException
{
990 Pair
<Long
, List
<SendMessageResult
>> result
= null;
991 if (group
.isPendingMember(account
.getSelfRecipientId())) {
992 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
993 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
996 if (members
!= null) {
997 final var newMembers
= new HashSet
<>(members
);
998 newMembers
.removeAll(group
.getMembers());
999 if (newMembers
.size() > 0) {
1000 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
1001 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1005 if (removeMembers
!= null) {
1006 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
1007 existingRemoveMembers
.retainAll(group
.getMembers());
1008 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
1009 if (existingRemoveMembers
.size() > 0) {
1010 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
1011 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1014 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
1015 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
1016 if (pendingRemoveMembers
.size() > 0) {
1017 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
1018 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1022 if (admins
!= null) {
1023 final var newAdmins
= new HashSet
<>(admins
);
1024 newAdmins
.retainAll(group
.getMembers());
1025 newAdmins
.removeAll(group
.getAdminMembers());
1026 if (newAdmins
.size() > 0) {
1027 for (var admin
: newAdmins
) {
1028 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1029 result
= sendUpdateGroupV2Message(group
,
1030 groupGroupChangePair
.first(),
1031 groupGroupChangePair
.second());
1036 if (removeAdmins
!= null) {
1037 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1038 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1039 if (existingRemoveAdmins
.size() > 0) {
1040 for (var admin
: existingRemoveAdmins
) {
1041 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1042 result
= sendUpdateGroupV2Message(group
,
1043 groupGroupChangePair
.first(),
1044 groupGroupChangePair
.second());
1049 if (resetGroupLink
) {
1050 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1051 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1054 if (groupLinkState
!= null) {
1055 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1056 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1059 if (addMemberPermission
!= null) {
1060 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1061 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1064 if (editDetailsPermission
!= null) {
1065 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1066 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1069 if (expirationTimer
!= null) {
1070 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1071 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1074 if (name
!= null || description
!= null || avatarFile
!= null) {
1075 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1076 if (avatarFile
!= null) {
1077 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1078 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1080 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1086 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1087 GroupInviteLinkUrl inviteLinkUrl
1088 ) throws IOException
, GroupLinkNotActiveException
{
1089 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1090 inviteLinkUrl
.getPassword());
1091 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1092 inviteLinkUrl
.getPassword(),
1094 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1095 groupJoinInfo
.getRevision() + 1,
1096 groupChange
.toByteArray());
1098 if (group
.getGroup() == null) {
1099 // Only requested member, can't send update to group members
1100 return new Pair
<>(group
.getGroupId(), List
.of());
1103 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1105 return new Pair
<>(group
.getGroupId(), result
.second());
1108 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1109 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1110 ) throws IOException
{
1111 final var selfRecipientId
= account
.getSelfRecipientId();
1112 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1113 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1114 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1116 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1117 account
.getGroupStore().updateGroup(group
);
1118 return sendMessage(messageBuilder
, members
);
1121 private static int currentTimeDays() {
1122 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1125 private GroupsV2AuthorizationString
getGroupAuthForToday(
1126 final GroupSecretParams groupSecretParams
1127 ) throws IOException
{
1128 final var today
= currentTimeDays();
1129 // Returns credentials for the next 7 days
1130 final var credentials
= groupsV2Api
.getCredentials(today
);
1131 // TODO cache credentials until they expire
1132 var authCredentialResponse
= credentials
.get(today
);
1134 return groupsV2Api
.getGroupsV2AuthorizationString(account
.getUuid(),
1137 authCredentialResponse
);
1138 } catch (VerificationFailedException e
) {
1139 throw new IOException(e
);
1143 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1144 GroupIdV1 groupId
, SignalServiceAddress recipient
1145 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1147 var group
= getGroupForSending(groupId
);
1148 if (!(group
instanceof GroupInfoV1
)) {
1149 throw new RuntimeException("Received an invalid group request for a v2 group!");
1151 g
= (GroupInfoV1
) group
;
1153 final var recipientId
= resolveRecipient(recipient
);
1154 if (!g
.isMember(recipientId
)) {
1155 throw new NotAGroupMemberException(groupId
, g
.name
);
1158 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1160 // Send group message only to the recipient who requested it
1161 return sendMessage(messageBuilder
, Set
.of(recipientId
));
1164 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1165 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1166 .withId(g
.getGroupId().serialize())
1168 .withMembers(g
.getMembers()
1170 .map(this::resolveSignalServiceAddress
)
1171 .collect(Collectors
.toList()));
1174 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1175 if (attachment
.isPresent()) {
1176 group
.withAvatar(attachment
.get());
1178 } catch (IOException e
) {
1179 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1182 return SignalServiceDataMessage
.newBuilder()
1183 .asGroupMessage(group
.build())
1184 .withExpiration(g
.getMessageExpirationTime());
1187 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1188 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1189 .withRevision(g
.getGroup().getRevision())
1190 .withSignedGroupChange(signedGroupChange
);
1191 return SignalServiceDataMessage
.newBuilder()
1192 .asGroupMessage(group
.build())
1193 .withExpiration(g
.getMessageExpirationTime());
1196 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1197 GroupIdV1 groupId
, SignalServiceAddress recipient
1198 ) throws IOException
{
1199 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1201 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1203 // Send group info request message to the recipient who sent us a message with this groupId
1204 return sendMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1208 SignalServiceAddress remoteAddress
, long messageId
1209 ) throws IOException
, UntrustedIdentityException
{
1210 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1212 System
.currentTimeMillis());
1214 createMessageSender().sendReceipt(remoteAddress
,
1215 unidentifiedAccessHelper
.getAccessFor(resolveRecipient(remoteAddress
)),
1219 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1220 String messageText
, List
<String
> attachments
, List
<String
> recipients
1221 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1222 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1223 if (attachments
!= null) {
1224 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1226 // Upload attachments here, so we only upload once even for multiple recipients
1227 var messageSender
= createMessageSender();
1228 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1229 for (var attachment
: attachmentStreams
) {
1230 if (attachment
.isStream()) {
1231 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1232 } else if (attachment
.isPointer()) {
1233 attachmentPointers
.add(attachment
.asPointer());
1237 messageBuilder
.withAttachments(attachmentPointers
);
1239 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1242 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1243 String messageText
, List
<String
> attachments
1244 ) throws IOException
, AttachmentInvalidException
{
1245 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1246 if (attachments
!= null) {
1247 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1249 return sendSelfMessage(messageBuilder
);
1252 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1253 long targetSentTimestamp
, List
<String
> recipients
1254 ) throws IOException
, InvalidNumberException
{
1255 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1256 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1257 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1260 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1261 long targetSentTimestamp
, GroupId groupId
1262 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1263 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1264 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1265 return sendGroupMessage(messageBuilder
, groupId
);
1268 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1269 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1270 ) throws IOException
, InvalidNumberException
{
1271 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1272 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1274 resolveSignalServiceAddress(targetAuthorRecipientId
),
1275 targetSentTimestamp
);
1276 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1277 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1280 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1281 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1283 final var signalServiceAddresses
= getSignalServiceAddresses(recipients
);
1285 return sendMessage(messageBuilder
, signalServiceAddresses
);
1286 } catch (Exception e
) {
1287 for (var address
: signalServiceAddresses
) {
1288 handleEndSession(address
);
1294 void renewSession(RecipientId recipientId
) throws IOException
{
1295 account
.getSessionStore().archiveSessions(recipientId
);
1296 if (!recipientId
.equals(getSelfRecipientId())) {
1297 sendNullMessage(recipientId
);
1301 public String
getContactName(String number
) throws InvalidNumberException
{
1302 var contact
= account
.getContactStore().getContact(canonicalizeAndResolveRecipient(number
));
1303 return contact
== null || contact
.getName() == null ?
"" : contact
.getName();
1306 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1307 if (!account
.isMasterDevice()) {
1308 throw new NotMasterDeviceException();
1310 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1311 var contact
= account
.getContactStore().getContact(recipientId
);
1312 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1313 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1316 public void setContactBlocked(
1317 String number
, boolean blocked
1318 ) throws InvalidNumberException
, NotMasterDeviceException
{
1319 if (!account
.isMasterDevice()) {
1320 throw new NotMasterDeviceException();
1322 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1325 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1326 var contact
= account
.getContactStore().getContact(recipientId
);
1327 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1328 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1331 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1332 var group
= getGroup(groupId
);
1333 if (group
== null) {
1334 throw new GroupNotFoundException(groupId
);
1337 group
.setBlocked(blocked
);
1338 account
.getGroupStore().updateGroup(group
);
1341 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1342 var contact
= account
.getContactStore().getContact(recipientId
);
1343 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1346 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1347 account
.getContactStore()
1348 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1351 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1352 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1353 sendMessage(messageBuilder
, Set
.of(recipientId
));
1357 * Change the expiration timer for a contact
1359 public void setExpirationTimer(
1360 String number
, int messageExpirationTimer
1361 ) throws IOException
, InvalidNumberException
{
1362 var recipientId
= canonicalizeAndResolveRecipient(number
);
1363 setExpirationTimer(recipientId
, messageExpirationTimer
);
1364 sendExpirationTimerUpdate(recipientId
);
1368 * Change the expiration timer for a group
1370 private void setExpirationTimer(
1371 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1372 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1373 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1374 account
.getGroupStore().updateGroup(groupInfoV1
);
1375 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1378 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1379 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1380 sendGroupMessage(messageBuilder
, groupId
);
1384 * Upload the sticker pack from path.
1386 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1387 * @return if successful, returns the URL to install the sticker pack in the signal app
1389 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1390 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1392 var messageSender
= createMessageSender();
1394 var packKey
= KeyUtils
.createStickerUploadKey();
1395 var packId
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1397 var sticker
= new Sticker(StickerPackId
.deserialize(Hex
.fromStringCondensed(packId
)), packKey
);
1398 account
.getStickerStore().updateSticker(sticker
);
1401 return new URI("https",
1404 "pack_id=" + URLEncoder
.encode(packId
, StandardCharsets
.UTF_8
) + "&pack_key=" + URLEncoder
.encode(
1405 Hex
.toStringCondensed(packKey
),
1406 StandardCharsets
.UTF_8
)).toString();
1407 } catch (URISyntaxException e
) {
1408 throw new AssertionError(e
);
1412 public void requestAllSyncData() throws IOException
{
1413 requestSyncGroups();
1414 requestSyncContacts();
1415 requestSyncBlocked();
1416 requestSyncConfiguration();
1420 private void requestSyncGroups() throws IOException
{
1421 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1422 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1424 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1426 sendSyncMessage(message
);
1427 } catch (UntrustedIdentityException e
) {
1428 throw new AssertionError(e
);
1432 private void requestSyncContacts() throws IOException
{
1433 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1434 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1436 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1438 sendSyncMessage(message
);
1439 } catch (UntrustedIdentityException e
) {
1440 throw new AssertionError(e
);
1444 private void requestSyncBlocked() throws IOException
{
1445 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1446 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1448 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1450 sendSyncMessage(message
);
1451 } catch (UntrustedIdentityException e
) {
1452 throw new AssertionError(e
);
1456 private void requestSyncConfiguration() throws IOException
{
1457 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1458 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1460 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1462 sendSyncMessage(message
);
1463 } catch (UntrustedIdentityException e
) {
1464 throw new AssertionError(e
);
1468 private void requestSyncKeys() throws IOException
{
1469 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1470 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1472 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1474 sendSyncMessage(message
);
1475 } catch (UntrustedIdentityException e
) {
1476 throw new AssertionError(e
);
1480 private byte[] getSenderCertificate() {
1483 if (account
.isPhoneNumberShared()) {
1484 certificate
= accountManager
.getSenderCertificate();
1486 certificate
= accountManager
.getSenderCertificateForPhoneNumberPrivacy();
1488 } catch (IOException e
) {
1489 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1492 // TODO cache for a day
1496 private void sendSyncMessage(SignalServiceSyncMessage message
) throws IOException
, UntrustedIdentityException
{
1497 var messageSender
= createMessageSender();
1498 messageSender
.sendMessage(message
, unidentifiedAccessHelper
.getAccessForSync());
1501 private Set
<RecipientId
> getSignalServiceAddresses(Collection
<String
> numbers
) throws InvalidNumberException
{
1502 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1503 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1505 for (var number
: numbers
) {
1506 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1507 if (resolvedAddress
.getUuid().isPresent()) {
1508 signalServiceAddresses
.add(resolvedAddress
);
1510 addressesMissingUuid
.add(resolvedAddress
);
1514 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1515 .map(a
-> a
.getNumber().get())
1516 .collect(Collectors
.toSet());
1517 Map
<String
, UUID
> registeredUsers
;
1519 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1520 } catch (IOException e
) {
1521 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1522 registeredUsers
= Map
.of();
1525 for (var address
: addressesMissingUuid
) {
1526 final var number
= address
.getNumber().get();
1527 if (registeredUsers
.containsKey(number
)) {
1528 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1529 registeredUsers
.get(number
),
1531 signalServiceAddresses
.add(newAddress
);
1533 signalServiceAddresses
.add(address
);
1537 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1540 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1541 final var address
= resolveSignalServiceAddress(recipientId
);
1542 if (!address
.getNumber().isPresent()) {
1545 final var number
= address
.getNumber().get();
1546 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1547 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1550 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1552 return accountManager
.getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1554 serviceEnvironmentConfig
.getCdsMrenclave());
1555 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1556 throw new IOException(e
);
1560 private Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1561 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientId
> recipientIds
1562 ) throws IOException
{
1563 final var timestamp
= System
.currentTimeMillis();
1564 messageBuilder
.withTimestamp(timestamp
);
1565 getOrCreateMessagePipe();
1566 getOrCreateUnidentifiedMessagePipe();
1567 SignalServiceDataMessage message
= null;
1569 message
= messageBuilder
.build();
1570 if (message
.getGroupContext().isPresent()) {
1572 var messageSender
= createMessageSender();
1573 final var isRecipientUpdate
= false;
1574 final var recipientIdList
= new ArrayList
<>(recipientIds
);
1575 final var addresses
= recipientIdList
.stream()
1576 .map(this::resolveSignalServiceAddress
)
1577 .collect(Collectors
.toList());
1578 var result
= messageSender
.sendMessage(addresses
,
1579 unidentifiedAccessHelper
.getAccessFor(recipientIdList
),
1583 for (var r
: result
) {
1584 if (r
.getIdentityFailure() != null) {
1585 final var recipientId
= resolveRecipient(r
.getAddress());
1586 final var newIdentity
= account
.getIdentityKeyStore()
1587 .saveIdentity(recipientId
, r
.getIdentityFailure().getIdentityKey(), new Date());
1589 account
.getSessionStore().archiveSessions(recipientId
);
1594 return new Pair
<>(timestamp
, result
);
1595 } catch (UntrustedIdentityException e
) {
1596 return new Pair
<>(timestamp
, List
.of());
1599 // Send to all individually, so sync messages are sent correctly
1600 messageBuilder
.withProfileKey(account
.getProfileKey().serialize());
1601 var results
= new ArrayList
<SendMessageResult
>(recipientIds
.size());
1602 for (var recipientId
: recipientIds
) {
1603 final var contact
= account
.getContactStore().getContact(recipientId
);
1604 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1605 messageBuilder
.withExpiration(expirationTime
);
1606 message
= messageBuilder
.build();
1607 results
.add(sendMessage(recipientId
, message
));
1609 return new Pair
<>(timestamp
, results
);
1612 if (message
!= null && message
.isEndSession()) {
1613 for (var recipient
: recipientIds
) {
1614 handleEndSession(recipient
);
1620 private Pair
<Long
, SendMessageResult
> sendSelfMessage(
1621 SignalServiceDataMessage
.Builder messageBuilder
1622 ) throws IOException
{
1623 final var timestamp
= System
.currentTimeMillis();
1624 messageBuilder
.withTimestamp(timestamp
);
1625 getOrCreateMessagePipe();
1626 getOrCreateUnidentifiedMessagePipe();
1627 final var recipientId
= account
.getSelfRecipientId();
1629 final var contact
= account
.getContactStore().getContact(recipientId
);
1630 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1631 messageBuilder
.withExpiration(expirationTime
);
1633 var message
= messageBuilder
.build();
1634 final var result
= sendSelfMessage(message
);
1635 return new Pair
<>(timestamp
, result
);
1638 private SendMessageResult
sendSelfMessage(SignalServiceDataMessage message
) throws IOException
{
1639 var messageSender
= createMessageSender();
1641 var recipientId
= account
.getSelfRecipientId();
1643 final var unidentifiedAccess
= unidentifiedAccessHelper
.getAccessFor(recipientId
);
1644 var recipient
= resolveSignalServiceAddress(recipientId
);
1645 var transcript
= new SentTranscriptMessage(Optional
.of(recipient
),
1646 message
.getTimestamp(),
1648 message
.getExpiresInSeconds(),
1649 Map
.of(recipient
, unidentifiedAccess
.isPresent()),
1651 var syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
1654 var startTime
= System
.currentTimeMillis();
1655 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
1656 return SendMessageResult
.success(recipient
,
1657 unidentifiedAccess
.isPresent(),
1659 System
.currentTimeMillis() - startTime
);
1660 } catch (UntrustedIdentityException e
) {
1661 return SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey());
1665 private SendMessageResult
sendMessage(
1666 RecipientId recipientId
, SignalServiceDataMessage message
1667 ) throws IOException
{
1668 var messageSender
= createMessageSender();
1670 final var address
= resolveSignalServiceAddress(recipientId
);
1673 return messageSender
.sendMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
), message
);
1674 } catch (UnregisteredUserException e
) {
1675 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1676 return messageSender
.sendMessage(resolveSignalServiceAddress(newRecipientId
),
1677 unidentifiedAccessHelper
.getAccessFor(newRecipientId
),
1680 } catch (UntrustedIdentityException e
) {
1681 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1685 private SendMessageResult
sendNullMessage(RecipientId recipientId
) throws IOException
{
1686 var messageSender
= createMessageSender();
1688 final var address
= resolveSignalServiceAddress(recipientId
);
1691 return messageSender
.sendNullMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
));
1692 } catch (UnregisteredUserException e
) {
1693 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1694 final var newAddress
= resolveSignalServiceAddress(newRecipientId
);
1695 return messageSender
.sendNullMessage(newAddress
, unidentifiedAccessHelper
.getAccessFor(newRecipientId
));
1697 } catch (UntrustedIdentityException e
) {
1698 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1702 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
{
1703 var cipher
= new SignalServiceCipher(account
.getSelfAddress(),
1704 account
.getSignalProtocolStore(),
1706 certificateValidator
);
1707 return cipher
.decrypt(envelope
);
1710 private void handleEndSession(RecipientId recipientId
) {
1711 account
.getSessionStore().deleteAllSessions(recipientId
);
1714 private List
<HandleAction
> handleSignalServiceDataMessage(
1715 SignalServiceDataMessage message
,
1717 SignalServiceAddress source
,
1718 SignalServiceAddress destination
,
1719 boolean ignoreAttachments
1721 var actions
= new ArrayList
<HandleAction
>();
1722 if (message
.getGroupContext().isPresent()) {
1723 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1724 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1725 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1726 var group
= getGroup(groupId
);
1727 if (group
== null || group
instanceof GroupInfoV1
) {
1728 var groupV1
= (GroupInfoV1
) group
;
1729 switch (groupInfo
.getType()) {
1731 if (groupV1
== null) {
1732 groupV1
= new GroupInfoV1(groupId
);
1735 if (groupInfo
.getAvatar().isPresent()) {
1736 var avatar
= groupInfo
.getAvatar().get();
1737 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1740 if (groupInfo
.getName().isPresent()) {
1741 groupV1
.name
= groupInfo
.getName().get();
1744 if (groupInfo
.getMembers().isPresent()) {
1745 groupV1
.addMembers(groupInfo
.getMembers()
1748 .map(this::resolveRecipient
)
1749 .collect(Collectors
.toSet()));
1752 account
.getGroupStore().updateGroup(groupV1
);
1756 if (groupV1
== null && !isSync
) {
1757 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1761 if (groupV1
!= null) {
1762 groupV1
.removeMember(resolveRecipient(source
));
1763 account
.getGroupStore().updateGroup(groupV1
);
1768 if (groupV1
!= null && !isSync
) {
1769 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1774 // Received a group v1 message for a v2 group
1777 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1778 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1779 final var groupMasterKey
= groupContext
.getMasterKey();
1781 getOrMigrateGroup(groupMasterKey
,
1782 groupContext
.getRevision(),
1783 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1787 final var conversationPartnerAddress
= isSync ? destination
: source
;
1788 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1789 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1791 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1792 if (message
.getGroupContext().isPresent()) {
1793 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1794 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1795 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1796 if (group
!= null) {
1797 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1798 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1799 account
.getGroupStore().updateGroup(group
);
1802 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1803 // disappearing message timer already stored in the DecryptedGroup
1805 } else if (conversationPartnerAddress
!= null) {
1806 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1809 if (!ignoreAttachments
) {
1810 if (message
.getAttachments().isPresent()) {
1811 for (var attachment
: message
.getAttachments().get()) {
1812 downloadAttachment(attachment
);
1815 if (message
.getSharedContacts().isPresent()) {
1816 for (var contact
: message
.getSharedContacts().get()) {
1817 if (contact
.getAvatar().isPresent()) {
1818 downloadAttachment(contact
.getAvatar().get().getAttachment());
1823 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1824 final ProfileKey profileKey
;
1826 profileKey
= new ProfileKey(message
.getProfileKey().get());
1827 } catch (InvalidInputException e
) {
1828 throw new AssertionError(e
);
1830 if (source
.matches(account
.getSelfAddress())) {
1831 this.account
.setProfileKey(profileKey
);
1833 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1835 if (message
.getPreviews().isPresent()) {
1836 final var previews
= message
.getPreviews().get();
1837 for (var preview
: previews
) {
1838 if (preview
.getImage().isPresent()) {
1839 downloadAttachment(preview
.getImage().get());
1843 if (message
.getQuote().isPresent()) {
1844 final var quote
= message
.getQuote().get();
1846 for (var quotedAttachment
: quote
.getAttachments()) {
1847 final var thumbnail
= quotedAttachment
.getThumbnail();
1848 if (thumbnail
!= null) {
1849 downloadAttachment(thumbnail
);
1853 if (message
.getSticker().isPresent()) {
1854 final var messageSticker
= message
.getSticker().get();
1855 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1856 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1857 if (sticker
== null) {
1858 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1859 account
.getStickerStore().updateSticker(sticker
);
1865 private GroupInfoV2
getOrMigrateGroup(
1866 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1868 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1870 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1871 var groupInfo
= getGroup(groupId
);
1872 final GroupInfoV2 groupInfoV2
;
1873 if (groupInfo
instanceof GroupInfoV1
) {
1874 // Received a v2 group message for a v1 group, we need to locally migrate the group
1875 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1876 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1877 logger
.info("Locally migrated group {} to group v2, id: {}",
1878 groupInfo
.getGroupId().toBase64(),
1879 groupInfoV2
.getGroupId().toBase64());
1880 } else if (groupInfo
instanceof GroupInfoV2
) {
1881 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1883 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1886 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1887 DecryptedGroup group
= null;
1888 if (signedGroupChange
!= null
1889 && groupInfoV2
.getGroup() != null
1890 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1891 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1895 if (group
== null) {
1896 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1898 if (group
!= null) {
1899 storeProfileKeysFromMembers(group
);
1900 final var avatar
= group
.getAvatar();
1901 if (avatar
!= null && !avatar
.isEmpty()) {
1902 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1905 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1906 account
.getGroupStore().updateGroup(groupInfoV2
);
1912 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1913 for (var member
: group
.getMembersList()) {
1914 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1915 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1917 account
.getProfileStore()
1918 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1919 } catch (InvalidInputException ignored
) {
1924 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1925 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1926 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1927 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1928 if (actions
!= null) {
1929 queuedActions
.addAll(actions
);
1932 for (var action
: queuedActions
) {
1934 action
.execute(this);
1935 } catch (Throwable e
) {
1936 logger
.warn("Message action failed.", e
);
1941 private List
<HandleAction
> retryFailedReceivedMessage(
1942 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1944 var envelope
= cachedMessage
.loadEnvelope();
1945 if (envelope
== null) {
1948 SignalServiceContent content
= null;
1949 List
<HandleAction
> actions
= null;
1950 if (!envelope
.isReceipt()) {
1952 content
= decryptMessage(envelope
);
1953 } catch (ProtocolUntrustedIdentityException e
) {
1954 if (!envelope
.hasSource()) {
1955 final var identifier
= e
.getSender();
1956 final var recipientId
= resolveRecipient(identifier
);
1958 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1959 } catch (IOException ioException
) {
1960 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1964 } catch (Exception er
) {
1965 // All other errors are not recoverable, so delete the cached message
1966 cachedMessage
.delete();
1969 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1971 handler
.handleMessage(envelope
, content
, null);
1972 cachedMessage
.delete();
1976 public void receiveMessages(
1979 boolean returnOnTimeout
,
1980 boolean ignoreAttachments
,
1981 ReceiveMessageHandler handler
1982 ) throws IOException
{
1983 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1985 Set
<HandleAction
> queuedActions
= null;
1987 final var messagePipe
= getOrCreateMessagePipe();
1989 var hasCaughtUpWithOldMessages
= false;
1992 SignalServiceEnvelope envelope
;
1993 SignalServiceContent content
= null;
1994 Exception exception
= null;
1995 final CachedMessage
[] cachedMessage
= {null};
1996 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
1998 var result
= messagePipe
.readOrEmpty(timeout
, unit
, envelope1
-> {
1999 final var recipientId
= envelope1
.hasSource()
2000 ?
resolveRecipient(envelope1
.getSourceIdentifier())
2002 // store message on disk, before acknowledging receipt to the server
2003 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
2005 if (result
.isPresent()) {
2006 envelope
= result
.get();
2008 // Received indicator that server queue is empty
2009 hasCaughtUpWithOldMessages
= true;
2011 if (queuedActions
!= null) {
2012 for (var action
: queuedActions
) {
2014 action
.execute(this);
2015 } catch (Throwable e
) {
2016 logger
.warn("Message action failed.", e
);
2019 queuedActions
.clear();
2020 queuedActions
= null;
2023 // Continue to wait another timeout for new messages
2026 } catch (TimeoutException e
) {
2027 if (returnOnTimeout
) return;
2031 if (envelope
.hasSource()) {
2032 // Store uuid if we don't have it already
2033 // address/uuid in envelope is sent by server
2034 resolveRecipientTrusted(envelope
.getSourceAddress());
2036 final var notAGroupMember
= isNotAGroupMember(envelope
, content
);
2037 if (!envelope
.isReceipt()) {
2039 content
= decryptMessage(envelope
);
2040 } catch (Exception e
) {
2043 if (!envelope
.hasSource() && content
!= null) {
2044 // Store uuid if we don't have it already
2045 // address/uuid is validated by unidentified sender certificate
2046 resolveRecipientTrusted(content
.getSender());
2048 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2049 if (exception
instanceof ProtocolInvalidMessageException
) {
2050 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
2051 logger
.debug("Received invalid message, queuing renew session action.");
2052 actions
.add(new RenewSessionAction(sender
));
2054 if (hasCaughtUpWithOldMessages
) {
2055 for (var action
: actions
) {
2057 action
.execute(this);
2058 } catch (Throwable e
) {
2059 logger
.warn("Message action failed.", e
);
2063 if (queuedActions
== null) {
2064 queuedActions
= new HashSet
<>();
2066 queuedActions
.addAll(actions
);
2069 if (isMessageBlocked(envelope
, content
)) {
2070 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
2071 } else if (notAGroupMember
) {
2072 logger
.info("Ignoring a message from a non group member: {}", envelope
.getTimestamp());
2074 handler
.handleMessage(envelope
, content
, exception
);
2076 if (cachedMessage
[0] != null) {
2077 if (exception
instanceof ProtocolUntrustedIdentityException
) {
2078 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
2079 final var recipientId
= resolveRecipient(identifier
);
2080 queuedActions
.add(new RetrieveProfileAction(recipientId
));
2081 if (!envelope
.hasSource()) {
2083 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
2084 } catch (IOException ioException
) {
2085 logger
.warn("Failed to move cached message to recipient folder: {}",
2086 ioException
.getMessage());
2090 cachedMessage
[0].delete();
2096 private boolean isMessageBlocked(
2097 SignalServiceEnvelope envelope
, SignalServiceContent content
2099 SignalServiceAddress source
;
2100 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2101 source
= envelope
.getSourceAddress();
2102 } else if (content
!= null) {
2103 source
= content
.getSender();
2107 final var recipientId
= resolveRecipient(source
);
2108 if (isContactBlocked(recipientId
)) {
2112 if (content
!= null && content
.getDataMessage().isPresent()) {
2113 var message
= content
.getDataMessage().get();
2114 if (message
.getGroupContext().isPresent()) {
2115 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2116 var group
= getGroup(groupId
);
2117 if (group
!= null && group
.isBlocked()) {
2125 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2126 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2127 return isContactBlocked(recipientId
);
2130 private boolean isContactBlocked(final RecipientId recipientId
) {
2131 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2132 return sourceContact
!= null && sourceContact
.isBlocked();
2135 private boolean isNotAGroupMember(
2136 SignalServiceEnvelope envelope
, SignalServiceContent content
2138 SignalServiceAddress source
;
2139 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2140 source
= envelope
.getSourceAddress();
2141 } else if (content
!= null) {
2142 source
= content
.getSender();
2147 if (content
!= null && content
.getDataMessage().isPresent()) {
2148 var message
= content
.getDataMessage().get();
2149 if (message
.getGroupContext().isPresent()) {
2150 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2151 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2152 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2156 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2157 var group
= getGroup(groupId
);
2158 if (group
!= null && !group
.isMember(resolveRecipient(source
))) {
2166 private List
<HandleAction
> handleMessage(
2167 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2169 var actions
= new ArrayList
<HandleAction
>();
2170 if (content
!= null) {
2171 final SignalServiceAddress sender
;
2172 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2173 sender
= envelope
.getSourceAddress();
2175 sender
= content
.getSender();
2178 if (content
.getDataMessage().isPresent()) {
2179 var message
= content
.getDataMessage().get();
2181 if (content
.isNeedsReceipt()) {
2182 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2185 actions
.addAll(handleSignalServiceDataMessage(message
,
2188 account
.getSelfAddress(),
2189 ignoreAttachments
));
2191 if (content
.getSyncMessage().isPresent()) {
2192 account
.setMultiDevice(true);
2193 var syncMessage
= content
.getSyncMessage().get();
2194 if (syncMessage
.getSent().isPresent()) {
2195 var message
= syncMessage
.getSent().get();
2196 final var destination
= message
.getDestination().orNull();
2197 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2201 ignoreAttachments
));
2203 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2204 var rm
= syncMessage
.getRequest().get();
2205 if (rm
.isContactsRequest()) {
2206 actions
.add(SendSyncContactsAction
.create());
2208 if (rm
.isGroupsRequest()) {
2209 actions
.add(SendSyncGroupsAction
.create());
2211 if (rm
.isBlockedListRequest()) {
2212 actions
.add(SendSyncBlockedListAction
.create());
2214 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2216 if (syncMessage
.getGroups().isPresent()) {
2217 File tmpFile
= null;
2219 tmpFile
= IOUtils
.createTempFile();
2220 final var groupsMessage
= syncMessage
.getGroups().get();
2221 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2222 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2224 while ((g
= s
.read()) != null) {
2225 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2226 if (syncGroup
!= null) {
2227 if (g
.getName().isPresent()) {
2228 syncGroup
.name
= g
.getName().get();
2230 syncGroup
.addMembers(g
.getMembers()
2232 .map(this::resolveRecipient
)
2233 .collect(Collectors
.toSet()));
2234 if (!g
.isActive()) {
2235 syncGroup
.removeMember(account
.getSelfRecipientId());
2237 // Add ourself to the member set as it's marked as active
2238 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2240 syncGroup
.blocked
= g
.isBlocked();
2241 if (g
.getColor().isPresent()) {
2242 syncGroup
.color
= g
.getColor().get();
2245 if (g
.getAvatar().isPresent()) {
2246 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2248 syncGroup
.archived
= g
.isArchived();
2249 account
.getGroupStore().updateGroup(syncGroup
);
2253 } catch (Exception e
) {
2254 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2258 if (tmpFile
!= null) {
2260 Files
.delete(tmpFile
.toPath());
2261 } catch (IOException e
) {
2262 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2269 if (syncMessage
.getBlockedList().isPresent()) {
2270 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2271 for (var address
: blockedListMessage
.getAddresses()) {
2272 setContactBlocked(resolveRecipient(address
), true);
2274 for (var groupId
: blockedListMessage
.getGroupIds()
2276 .map(GroupId
::unknownVersion
)
2277 .collect(Collectors
.toSet())) {
2279 setGroupBlocked(groupId
, true);
2280 } catch (GroupNotFoundException e
) {
2281 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2282 groupId
.toBase64());
2286 if (syncMessage
.getContacts().isPresent()) {
2287 File tmpFile
= null;
2289 tmpFile
= IOUtils
.createTempFile();
2290 final var contactsMessage
= syncMessage
.getContacts().get();
2291 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2292 .asPointer(), tmpFile
)) {
2293 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2295 while ((c
= s
.read()) != null) {
2296 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2297 account
.setProfileKey(c
.getProfileKey().get());
2299 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2300 var contact
= account
.getContactStore().getContact(recipientId
);
2301 final var builder
= contact
== null
2302 ? Contact
.newBuilder()
2303 : Contact
.newBuilder(contact
);
2304 if (c
.getName().isPresent()) {
2305 builder
.withName(c
.getName().get());
2307 if (c
.getColor().isPresent()) {
2308 builder
.withColor(c
.getColor().get());
2310 if (c
.getProfileKey().isPresent()) {
2311 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2313 if (c
.getVerified().isPresent()) {
2314 final var verifiedMessage
= c
.getVerified().get();
2315 account
.getIdentityKeyStore()
2316 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2317 verifiedMessage
.getIdentityKey(),
2318 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2320 if (c
.getExpirationTimer().isPresent()) {
2321 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2323 builder
.withBlocked(c
.isBlocked());
2324 builder
.withArchived(c
.isArchived());
2325 account
.getContactStore().storeContact(recipientId
, builder
.build());
2327 if (c
.getAvatar().isPresent()) {
2328 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2332 } catch (Exception e
) {
2333 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2337 if (tmpFile
!= null) {
2339 Files
.delete(tmpFile
.toPath());
2340 } catch (IOException e
) {
2341 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2348 if (syncMessage
.getVerified().isPresent()) {
2349 final var verifiedMessage
= syncMessage
.getVerified().get();
2350 account
.getIdentityKeyStore()
2351 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2352 verifiedMessage
.getIdentityKey(),
2353 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2355 if (syncMessage
.getStickerPackOperations().isPresent()) {
2356 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2357 for (var m
: stickerPackOperationMessages
) {
2358 if (!m
.getPackId().isPresent()) {
2361 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2362 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2363 if (sticker
== null) {
2364 if (!m
.getPackKey().isPresent()) {
2367 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2369 sticker
.setInstalled(!m
.getType().isPresent()
2370 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
);
2371 account
.getStickerStore().updateSticker(sticker
);
2374 if (syncMessage
.getFetchType().isPresent()) {
2375 switch (syncMessage
.getFetchType().get()) {
2377 getRecipientProfile(account
.getSelfRecipientId(), true);
2378 case STORAGE_MANIFEST
:
2382 if (syncMessage
.getKeys().isPresent()) {
2383 final var keysMessage
= syncMessage
.getKeys().get();
2384 if (keysMessage
.getStorageService().isPresent()) {
2385 final var storageKey
= keysMessage
.getStorageService().get();
2386 account
.setStorageKey(storageKey
);
2389 if (syncMessage
.getConfiguration().isPresent()) {
2397 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2399 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2400 } catch (IOException e
) {
2401 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2405 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2407 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2408 } catch (IOException e
) {
2409 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2413 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2415 avatarStore
.storeGroupAvatar(groupId
,
2416 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2417 } catch (IOException e
) {
2418 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2422 private void downloadProfileAvatar(
2423 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2426 avatarStore
.storeProfileAvatar(address
,
2427 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2428 } catch (Throwable e
) {
2429 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2433 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2434 return attachmentStore
.getAttachmentFile(attachmentId
);
2437 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2438 if (!attachment
.isPointer()) {
2439 logger
.warn("Invalid state, can't store an attachment stream.");
2442 var pointer
= attachment
.asPointer();
2443 if (pointer
.getPreview().isPresent()) {
2444 final var preview
= pointer
.getPreview().get();
2446 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2447 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2448 } catch (IOException e
) {
2449 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2454 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2455 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2456 } catch (IOException e
) {
2457 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2461 private void retrieveGroupV2Avatar(
2462 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2463 ) throws IOException
{
2464 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
2466 var tmpFile
= IOUtils
.createTempFile();
2467 try (InputStream input
= messageReceiver
.retrieveGroupsV2ProfileAvatar(cdnKey
,
2469 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2470 var encryptedData
= IOUtils
.readFully(input
);
2472 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2473 outputStream
.write(decryptedData
);
2476 Files
.delete(tmpFile
.toPath());
2477 } catch (IOException e
) {
2478 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2485 private void retrieveProfileAvatar(
2486 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2487 ) throws IOException
{
2488 var tmpFile
= IOUtils
.createTempFile();
2489 try (var input
= messageReceiver
.retrieveProfileAvatar(avatarPath
,
2492 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2493 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2494 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2497 Files
.delete(tmpFile
.toPath());
2498 } catch (IOException e
) {
2499 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2506 private void retrieveAttachment(
2507 final SignalServiceAttachment attachment
, final OutputStream outputStream
2508 ) throws IOException
{
2509 if (attachment
.isPointer()) {
2510 var pointer
= attachment
.asPointer();
2511 retrieveAttachmentPointer(pointer
, outputStream
);
2513 var stream
= attachment
.asStream();
2514 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2518 private void retrieveAttachmentPointer(
2519 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2520 ) throws IOException
{
2521 var tmpFile
= IOUtils
.createTempFile();
2522 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2523 IOUtils
.copyStream(input
, outputStream
);
2524 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2525 throw new IOException(e
);
2528 Files
.delete(tmpFile
.toPath());
2529 } catch (IOException e
) {
2530 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2537 private InputStream
retrieveAttachmentAsStream(
2538 SignalServiceAttachmentPointer pointer
, File tmpFile
2539 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2540 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2543 void sendGroups() throws IOException
, UntrustedIdentityException
{
2544 var groupsFile
= IOUtils
.createTempFile();
2547 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2548 var out
= new DeviceGroupsOutputStream(fos
);
2549 for (var record : getGroups()) {
2550 if (record instanceof GroupInfoV1
) {
2551 var groupInfo
= (GroupInfoV1
) record;
2552 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2553 Optional
.fromNullable(groupInfo
.name
),
2554 groupInfo
.getMembers()
2556 .map(this::resolveSignalServiceAddress
)
2557 .collect(Collectors
.toList()),
2558 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2559 groupInfo
.isMember(account
.getSelfRecipientId()),
2560 Optional
.of(groupInfo
.messageExpirationTime
),
2561 Optional
.fromNullable(groupInfo
.color
),
2564 groupInfo
.archived
));
2569 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2570 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2571 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2572 .withStream(groupsFileStream
)
2573 .withContentType("application/octet-stream")
2574 .withLength(groupsFile
.length())
2577 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2582 Files
.delete(groupsFile
.toPath());
2583 } catch (IOException e
) {
2584 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2589 public void sendContacts() throws IOException
, UntrustedIdentityException
{
2590 var contactsFile
= IOUtils
.createTempFile();
2593 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2594 var out
= new DeviceContactsOutputStream(fos
);
2595 for (var contactPair
: account
.getContactStore().getContacts()) {
2596 final var recipientId
= contactPair
.first();
2597 final var contact
= contactPair
.second();
2598 final var address
= resolveSignalServiceAddress(recipientId
);
2600 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2601 VerifiedMessage verifiedMessage
= null;
2602 if (currentIdentity
!= null) {
2603 verifiedMessage
= new VerifiedMessage(address
,
2604 currentIdentity
.getIdentityKey(),
2605 currentIdentity
.getTrustLevel().toVerifiedState(),
2606 currentIdentity
.getDateAdded().getTime());
2609 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2610 out
.write(new DeviceContact(address
,
2611 Optional
.fromNullable(contact
.getName()),
2612 createContactAvatarAttachment(address
),
2613 Optional
.fromNullable(contact
.getColor()),
2614 Optional
.fromNullable(verifiedMessage
),
2615 Optional
.fromNullable(profileKey
),
2616 contact
.isBlocked(),
2617 Optional
.of(contact
.getMessageExpirationTime()),
2619 contact
.isArchived()));
2622 if (account
.getProfileKey() != null) {
2623 // Send our own profile key as well
2624 out
.write(new DeviceContact(account
.getSelfAddress(),
2629 Optional
.of(account
.getProfileKey()),
2637 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2638 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2639 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2640 .withStream(contactsFileStream
)
2641 .withContentType("application/octet-stream")
2642 .withLength(contactsFile
.length())
2645 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
2650 Files
.delete(contactsFile
.toPath());
2651 } catch (IOException e
) {
2652 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2657 void sendBlockedList() throws IOException
, UntrustedIdentityException
{
2658 var addresses
= new ArrayList
<SignalServiceAddress
>();
2659 for (var record : account
.getContactStore().getContacts()) {
2660 if (record.second().isBlocked()) {
2661 addresses
.add(resolveSignalServiceAddress(record.first()));
2664 var groupIds
= new ArrayList
<byte[]>();
2665 for (var record : getGroups()) {
2666 if (record.isBlocked()) {
2667 groupIds
.add(record.getGroupId().serialize());
2670 sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2673 private void sendVerifiedMessage(
2674 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2675 ) throws IOException
, UntrustedIdentityException
{
2676 var verifiedMessage
= new VerifiedMessage(destination
,
2678 trustLevel
.toVerifiedState(),
2679 System
.currentTimeMillis());
2680 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2683 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2684 return account
.getContactStore().getContacts();
2687 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2688 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2689 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2690 if (recipient
== null) {
2694 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2695 return recipient
.getContact().getName();
2698 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2699 return recipient
.getProfile().getDisplayName();
2705 public GroupInfo
getGroup(GroupId groupId
) {
2706 final var group
= account
.getGroupStore().getGroup(groupId
);
2707 if (group
instanceof GroupInfoV2
&& ((GroupInfoV2
) group
).getGroup() == null) {
2708 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2709 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2710 account
.getGroupStore().updateGroup(group
);
2715 public List
<IdentityInfo
> getIdentities() {
2716 return account
.getIdentityKeyStore().getIdentities();
2719 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2720 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2721 return identity
== null ? List
.of() : List
.of(identity
);
2725 * Trust this the identity with this fingerprint
2727 * @param name username of the identity
2728 * @param fingerprint Fingerprint
2730 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2731 var recipientId
= canonicalizeAndResolveRecipient(name
);
2732 return trustIdentity(recipientId
,
2733 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2734 TrustLevel
.TRUSTED_VERIFIED
);
2738 * Trust this the identity with this safety number
2740 * @param name username of the identity
2741 * @param safetyNumber Safety number
2743 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2744 var recipientId
= canonicalizeAndResolveRecipient(name
);
2745 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2746 return trustIdentity(recipientId
,
2747 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2748 TrustLevel
.TRUSTED_VERIFIED
);
2752 * Trust all keys of this identity without verification
2754 * @param name username of the identity
2756 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2757 var recipientId
= canonicalizeAndResolveRecipient(name
);
2758 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2761 private boolean trustIdentity(
2762 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2764 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2765 if (identity
== null) {
2769 if (!verifier
.apply(identity
.getIdentityKey())) {
2773 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2775 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2776 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2777 } catch (IOException
| UntrustedIdentityException e
) {
2778 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2784 public String
computeSafetyNumber(
2785 SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
2787 return Utils
.computeSafetyNumber(ServiceConfig
.capabilities
.isUuid(),
2788 account
.getSelfAddress(),
2789 getIdentityKeyPair().getPublicKey(),
2795 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2796 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2798 return resolveSignalServiceAddress(address
);
2802 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2803 if (address
.matches(account
.getSelfAddress())) {
2804 return account
.getSelfAddress();
2807 return account
.getRecipientStore().resolveServiceAddress(address
);
2810 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2811 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2814 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2815 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2817 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2819 return resolveRecipient(canonicalizedNumber
);
2822 private RecipientId
resolveRecipient(final String identifier
) {
2823 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2825 return resolveRecipient(address
);
2828 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2829 return account
.getRecipientStore().resolveRecipient(address
);
2832 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2833 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2837 public void close() throws IOException
{
2841 void close(boolean closeAccount
) throws IOException
{
2842 executor
.shutdown();
2844 if (messagePipe
!= null) {
2845 messagePipe
.shutdown();
2849 if (unidentifiedMessagePipe
!= null) {
2850 unidentifiedMessagePipe
.shutdown();
2851 unidentifiedMessagePipe
= null;
2854 if (closeAccount
&& account
!= null) {
2860 public interface ReceiveMessageHandler
{
2862 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);