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
.ConflictException
;
125 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
126 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
127 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
128 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
129 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
130 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
131 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
132 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
133 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
134 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
135 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
136 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
137 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
138 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
139 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
140 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
142 import java
.io
.Closeable
;
144 import java
.io
.FileInputStream
;
145 import java
.io
.FileOutputStream
;
146 import java
.io
.IOException
;
147 import java
.io
.InputStream
;
148 import java
.io
.OutputStream
;
150 import java
.net
.URISyntaxException
;
151 import java
.net
.URLEncoder
;
152 import java
.nio
.charset
.StandardCharsets
;
153 import java
.nio
.file
.Files
;
154 import java
.security
.SignatureException
;
155 import java
.util
.ArrayList
;
156 import java
.util
.Arrays
;
157 import java
.util
.Base64
;
158 import java
.util
.Collection
;
159 import java
.util
.Date
;
160 import java
.util
.HashSet
;
161 import java
.util
.List
;
162 import java
.util
.Map
;
163 import java
.util
.Set
;
164 import java
.util
.UUID
;
165 import java
.util
.concurrent
.ExecutorService
;
166 import java
.util
.concurrent
.Executors
;
167 import java
.util
.concurrent
.TimeUnit
;
168 import java
.util
.concurrent
.TimeoutException
;
169 import java
.util
.concurrent
.locks
.ReentrantLock
;
170 import java
.util
.function
.Function
;
171 import java
.util
.stream
.Collectors
;
173 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
175 public class Manager
implements Closeable
{
177 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
179 private final CertificateValidator certificateValidator
;
181 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
182 private final String userAgent
;
184 private SignalAccount account
;
185 private final SignalServiceAccountManager accountManager
;
186 private final GroupsV2Api groupsV2Api
;
187 private final GroupsV2Operations groupsV2Operations
;
188 private final SignalServiceMessageReceiver messageReceiver
;
189 private final ClientZkProfileOperations clientZkProfileOperations
;
191 private final ExecutorService executor
= Executors
.newCachedThreadPool();
193 private SignalServiceMessagePipe messagePipe
= null;
194 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
196 private final UnidentifiedAccessHelper unidentifiedAccessHelper
;
197 private final ProfileHelper profileHelper
;
198 private final GroupV2Helper groupV2Helper
;
199 private final PinHelper pinHelper
;
200 private final AvatarStore avatarStore
;
201 private final AttachmentStore attachmentStore
;
202 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
203 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
206 public Lock
acquire() {
208 return LEGACY_LOCK
::unlock
;
213 SignalAccount account
,
214 PathConfig pathConfig
,
215 ServiceEnvironmentConfig serviceEnvironmentConfig
,
218 this.account
= account
;
219 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
220 this.certificateValidator
= new CertificateValidator(serviceEnvironmentConfig
.getUnidentifiedSenderTrustRoot());
221 this.userAgent
= userAgent
;
222 this.groupsV2Operations
= capabilities
.isGv2() ?
new GroupsV2Operations(ClientZkOperations
.create(
223 serviceEnvironmentConfig
.getSignalServiceConfiguration())) : null;
224 final SleepTimer timer
= new UptimeSleepTimer();
225 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
226 new DynamicCredentialsProvider(account
.getUuid(),
227 account
.getUsername(),
228 account
.getPassword(),
229 account
.getDeviceId()),
232 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
234 this.groupsV2Api
= accountManager
.getGroupsV2Api();
235 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
236 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
237 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
238 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
241 this.pinHelper
= new PinHelper(keyBackupService
);
242 this.clientZkProfileOperations
= capabilities
.isGv2()
243 ? ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration())
244 .getProfileOperations()
246 this.messageReceiver
= new SignalServiceMessageReceiver(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
248 account
.getUsername(),
249 account
.getPassword(),
250 account
.getDeviceId(),
254 clientZkProfileOperations
,
255 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
257 this.unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
258 account
.getProfileStore()::getProfileKey
,
259 this::getRecipientProfile
,
260 this::getSenderCertificate
);
261 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
262 unidentifiedAccessHelper
::getAccessFor
,
263 unidentified
-> unidentified ?
getOrCreateUnidentifiedMessagePipe() : getOrCreateMessagePipe(),
264 () -> messageReceiver
,
265 this::resolveSignalServiceAddress
);
266 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
267 this::getRecipientProfile
,
268 account
::getSelfRecipientId
,
271 this::getGroupAuthForToday
,
272 this::resolveSignalServiceAddress
);
273 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
274 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
277 public String
getUsername() {
278 return account
.getUsername();
281 public SignalServiceAddress
getSelfAddress() {
282 return account
.getSelfAddress();
285 public RecipientId
getSelfRecipientId() {
286 return account
.getSelfRecipientId();
289 private IdentityKeyPair
getIdentityKeyPair() {
290 return account
.getIdentityKeyPair();
293 public int getDeviceId() {
294 return account
.getDeviceId();
297 public static Manager
init(
298 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
299 ) throws IOException
, NotRegisteredException
{
300 var pathConfig
= PathConfig
.createDefault(settingsPath
);
302 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
303 throw new NotRegisteredException();
306 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
308 if (!account
.isRegistered()) {
309 throw new NotRegisteredException();
312 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
314 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
317 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
318 var pathConfig
= PathConfig
.createDefault(settingsPath
);
319 final var dataPath
= pathConfig
.getDataPath();
320 final var files
= dataPath
.listFiles();
326 return Arrays
.stream(files
)
327 .filter(File
::isFile
)
329 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
330 .collect(Collectors
.toList());
333 public void checkAccountState() throws IOException
{
334 if (account
.getLastReceiveTimestamp() == 0) {
335 logger
.warn("The Signal protocol expects that incoming messages are regularly received.");
337 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
338 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
341 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
345 if (accountManager
.getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
348 if (account
.getUuid() == null) {
349 account
.setUuid(accountManager
.getOwnUuid());
351 updateAccountAttributes();
355 * This is used for checking a set of phone numbers for registration on Signal
357 * @param numbers The set of phone number in question
358 * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
359 * @throws IOException if its unable to get the contacts to check if they're registered
361 public Map
<String
, Boolean
> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
362 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
363 var contactDetails
= getRegisteredUsers(numbers
);
365 var registeredUsers
= contactDetails
.keySet();
367 return numbers
.stream().collect(Collectors
.toMap(x
-> x
, registeredUsers
::contains
));
370 public void updateAccountAttributes() throws IOException
{
371 accountManager
.setAccountAttributes(account
.getEncryptedDeviceName(),
373 account
.getLocalRegistrationId(),
375 // set legacy pin only if no KBS master key is set
376 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
377 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
378 account
.getSelfUnidentifiedAccessKey(),
379 account
.isUnrestrictedUnidentifiedAccess(),
381 account
.isDiscoverableByPhoneNumber());
385 * @param givenName if null, the previous givenName will be kept
386 * @param familyName if null, the previous familyName will be kept
387 * @param about if null, the previous about text will be kept
388 * @param aboutEmoji if null, the previous about emoji will be kept
389 * @param avatar if avatar is null the image from the local avatar store is used (if present),
391 public void setProfile(
392 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
393 ) throws IOException
{
394 var profile
= getRecipientProfile(account
.getSelfRecipientId());
395 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
396 if (givenName
!= null) {
397 builder
.withGivenName(givenName
);
399 if (familyName
!= null) {
400 builder
.withFamilyName(familyName
);
403 builder
.withAbout(about
);
405 if (aboutEmoji
!= null) {
406 builder
.withAboutEmoji(aboutEmoji
);
408 var newProfile
= builder
.build();
410 try (final var streamDetails
= avatar
== null
411 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
412 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
413 accountManager
.setVersionedProfile(account
.getUuid(),
414 account
.getProfileKey(),
415 newProfile
.getInternalServiceName(),
416 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
417 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
422 if (avatar
!= null) {
423 if (avatar
.isPresent()) {
424 avatarStore
.storeProfileAvatar(getSelfAddress(),
425 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
427 avatarStore
.deleteProfileAvatar(getSelfAddress());
430 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
433 sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
434 } catch (UntrustedIdentityException ignored
) {
438 public void unregister() throws IOException
{
439 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
440 // If this is the master device, other users can't send messages to this number anymore.
441 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
442 accountManager
.setGcmId(Optional
.absent());
444 account
.setRegistered(false);
447 public void deleteAccount() throws IOException
{
448 accountManager
.deleteAccount();
450 account
.setRegistered(false);
453 public List
<Device
> getLinkedDevices() throws IOException
{
454 var devices
= accountManager
.getDevices();
455 account
.setMultiDevice(devices
.size() > 1);
456 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
457 return devices
.stream().map(d
-> {
458 String deviceName
= d
.getName();
459 if (deviceName
!= null) {
461 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
462 } catch (IOException e
) {
463 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
466 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
467 }).collect(Collectors
.toList());
470 public void removeLinkedDevices(int deviceId
) throws IOException
{
471 accountManager
.removeDevice(deviceId
);
472 var devices
= accountManager
.getDevices();
473 account
.setMultiDevice(devices
.size() > 1);
476 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
477 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
479 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
482 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
483 var identityKeyPair
= getIdentityKeyPair();
484 var verificationCode
= accountManager
.getNewDeviceVerificationCode();
486 accountManager
.addDevice(deviceIdentifier
,
489 Optional
.of(account
.getProfileKey().serialize()),
491 account
.setMultiDevice(true);
494 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
495 if (!account
.isMasterDevice()) {
496 throw new RuntimeException("Only master device can set a PIN");
498 if (pin
.isPresent()) {
499 final var masterKey
= account
.getPinMasterKey() != null
500 ? account
.getPinMasterKey()
501 : KeyUtils
.createMasterKey();
503 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
505 account
.setRegistrationLockPin(pin
.get(), masterKey
);
507 // Remove legacy registration lock
508 accountManager
.removeRegistrationLockV1();
511 pinHelper
.removeRegistrationLockPin();
513 account
.setRegistrationLockPin(null, null);
517 void refreshPreKeys() throws IOException
{
518 var oneTimePreKeys
= generatePreKeys();
519 final var identityKeyPair
= getIdentityKeyPair();
520 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
522 accountManager
.setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
525 private List
<PreKeyRecord
> generatePreKeys() {
526 final var offset
= account
.getPreKeyIdOffset();
528 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
529 account
.addPreKeys(records
);
534 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
535 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
537 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
538 account
.addSignedPreKey(record);
543 private SignalServiceMessagePipe
getOrCreateMessagePipe() {
544 if (messagePipe
== null) {
545 messagePipe
= messageReceiver
.createMessagePipe();
550 private SignalServiceMessagePipe
getOrCreateUnidentifiedMessagePipe() {
551 if (unidentifiedMessagePipe
== null) {
552 unidentifiedMessagePipe
= messageReceiver
.createUnidentifiedMessagePipe();
554 return unidentifiedMessagePipe
;
557 private SignalServiceMessageSender
createMessageSender() {
558 return new SignalServiceMessageSender(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
560 account
.getUsername(),
561 account
.getPassword(),
562 account
.getDeviceId(),
563 account
.getSignalProtocolStore(),
566 account
.isMultiDevice(),
567 Optional
.fromNullable(messagePipe
),
568 Optional
.fromNullable(unidentifiedMessagePipe
),
570 clientZkProfileOperations
,
572 ServiceConfig
.MAX_ENVELOPE_SIZE
,
573 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
576 public Profile
getRecipientProfile(
577 RecipientId recipientId
579 return getRecipientProfile(recipientId
, false);
582 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
584 Profile
getRecipientProfile(
585 RecipientId recipientId
, boolean force
587 var profile
= account
.getProfileStore().getProfile(recipientId
);
589 var now
= System
.currentTimeMillis();
590 // Profiles are cached for 24h before retrieving them again, unless forced
591 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
595 synchronized (pendingProfileRequest
) {
596 if (pendingProfileRequest
.contains(recipientId
)) {
599 pendingProfileRequest
.add(recipientId
);
601 final SignalServiceProfile encryptedProfile
;
603 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
605 synchronized (pendingProfileRequest
) {
606 pendingProfileRequest
.remove(recipientId
);
609 if (encryptedProfile
== null) {
613 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
614 if (profileKey
== null) {
615 profile
= new Profile(System
.currentTimeMillis(),
620 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
621 ProfileUtils
.getCapabilities(encryptedProfile
));
623 profile
= decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
625 account
.getProfileStore().storeProfile(recipientId
, profile
);
630 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
632 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
633 } catch (IOException e
) {
634 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
639 private ProfileAndCredential
retrieveProfileAndCredential(
640 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
641 ) throws IOException
{
642 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
643 final var profile
= profileAndCredential
.getProfile();
646 var newIdentity
= account
.getIdentityKeyStore()
647 .saveIdentity(recipientId
,
648 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
652 account
.getSessionStore().archiveSessions(recipientId
);
654 } catch (InvalidKeyException ignored
) {
655 logger
.warn("Got invalid identity key in profile for {}",
656 resolveSignalServiceAddress(recipientId
).getIdentifier());
658 return profileAndCredential
;
661 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
662 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
663 if (profileKeyCredential
!= null) {
664 return profileKeyCredential
;
667 ProfileAndCredential profileAndCredential
;
669 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
670 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
671 } catch (IOException e
) {
672 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
676 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
677 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
679 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
680 if (profileKey
!= null) {
681 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
683 profileAndCredential
.getProfile());
684 account
.getProfileStore().storeProfile(recipientId
, profile
);
687 return profileKeyCredential
;
690 private Profile
decryptProfileAndDownloadAvatar(
691 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
693 if (encryptedProfile
.getAvatar() != null) {
694 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
697 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
700 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
701 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
702 if (streamDetails
== null) {
703 return Optional
.absent();
706 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
709 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
710 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
711 if (streamDetails
== null) {
712 return Optional
.absent();
715 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
718 private GroupInfo
getGroupForSending(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
719 var g
= getGroup(groupId
);
721 throw new GroupNotFoundException(groupId
);
723 if (!g
.isMember(account
.getSelfRecipientId())) {
724 throw new NotAGroupMemberException(groupId
, g
.getTitle());
729 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
730 var g
= getGroup(groupId
);
732 throw new GroupNotFoundException(groupId
);
734 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
735 throw new NotAGroupMemberException(groupId
, g
.getTitle());
740 public List
<GroupInfo
> getGroups() {
741 return account
.getGroupStore().getGroups();
744 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
745 String messageText
, List
<String
> attachments
, GroupId groupId
746 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
747 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
748 if (attachments
!= null) {
749 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
752 return sendGroupMessage(messageBuilder
, groupId
);
755 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
756 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
757 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
758 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
759 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
761 resolveSignalServiceAddress(targetAuthorRecipientId
),
762 targetSentTimestamp
);
763 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
765 return sendGroupMessage(messageBuilder
, groupId
);
768 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
769 SignalServiceDataMessage
.Builder messageBuilder
, GroupId groupId
770 ) throws IOException
, GroupNotFoundException
, NotAGroupMemberException
{
771 final var g
= getGroupForSending(groupId
);
773 GroupUtils
.setGroupContext(messageBuilder
, g
);
774 messageBuilder
.withExpiration(g
.getMessageExpirationTime());
776 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
779 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(
780 GroupId groupId
, Set
<String
> groupAdmins
781 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, InvalidNumberException
, LastGroupAdminException
{
782 var group
= getGroupForUpdating(groupId
);
783 if (group
instanceof GroupInfoV1
) {
784 return quitGroupV1((GroupInfoV1
) group
);
787 final var newAdmins
= getSignalServiceAddresses(groupAdmins
);
789 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
790 } catch (ConflictException e
) {
791 // Detected conflicting update, refreshing group and trying again
792 group
= getGroup(groupId
, true);
793 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
797 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV1(final GroupInfoV1 groupInfoV1
) throws IOException
{
798 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
799 .withId(groupInfoV1
.getGroupId().serialize())
802 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
803 groupInfoV1
.removeMember(account
.getSelfRecipientId());
804 account
.getGroupStore().updateGroup(groupInfoV1
);
805 return sendMessage(messageBuilder
, groupInfoV1
.getMembersWithout(account
.getSelfRecipientId()));
808 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV2(
809 final GroupInfoV2 groupInfoV2
, final Set
<RecipientId
> newAdmins
810 ) throws LastGroupAdminException
, IOException
{
811 final var currentAdmins
= groupInfoV2
.getAdminMembers();
812 newAdmins
.removeAll(currentAdmins
);
813 newAdmins
.retainAll(groupInfoV2
.getMembers());
814 if (currentAdmins
.contains(getSelfRecipientId())
815 && currentAdmins
.size() == 1
816 && groupInfoV2
.getMembers().size() > 1
817 && newAdmins
.size() == 0) {
818 // Last admin can't leave the group, unless she's also the last member
819 throw new LastGroupAdminException(groupInfoV2
.getGroupId(), groupInfoV2
.getTitle());
821 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
822 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
823 var messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
824 account
.getGroupStore().updateGroup(groupInfoV2
);
825 return sendMessage(messageBuilder
, groupInfoV2
.getMembersWithout(account
.getSelfRecipientId()));
828 public void deleteGroup(GroupId groupId
) throws IOException
{
829 account
.getGroupStore().deleteGroup(groupId
);
830 avatarStore
.deleteGroupAvatar(groupId
);
833 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
834 String name
, List
<String
> members
, File avatarFile
835 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
836 return createGroup(name
, members
== null ?
null : getSignalServiceAddresses(members
), avatarFile
);
839 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
840 String name
, Set
<RecipientId
> members
, File avatarFile
841 ) throws IOException
, AttachmentInvalidException
{
842 final var selfRecipientId
= account
.getSelfRecipientId();
843 if (members
!= null && members
.contains(selfRecipientId
)) {
844 members
= new HashSet
<>(members
);
845 members
.remove(selfRecipientId
);
848 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
849 members
== null ? Set
.of() : members
,
852 SignalServiceDataMessage
.Builder messageBuilder
;
853 if (gv2Pair
== null) {
854 // Failed to create v2 group, creating v1 group instead
855 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
856 gv1
.addMembers(List
.of(selfRecipientId
));
857 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
858 return new Pair
<>(gv1
.getGroupId(), result
.second());
861 final var gv2
= gv2Pair
.first();
862 final var decryptedGroup
= gv2Pair
.second();
864 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
865 if (avatarFile
!= null) {
866 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
867 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
869 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
870 account
.getGroupStore().updateGroup(gv2
);
872 final var result
= sendMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
873 return new Pair
<>(gv2
.getGroupId(), result
.second());
876 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
880 List
<String
> members
,
881 List
<String
> removeMembers
,
883 List
<String
> removeAdmins
,
884 boolean resetGroupLink
,
885 GroupLinkState groupLinkState
,
886 GroupPermission addMemberPermission
,
887 GroupPermission editDetailsPermission
,
889 Integer expirationTimer
890 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
891 return updateGroup(groupId
,
894 members
== null ?
null : getSignalServiceAddresses(members
),
895 removeMembers
== null ?
null : getSignalServiceAddresses(removeMembers
),
896 admins
== null ?
null : getSignalServiceAddresses(admins
),
897 removeAdmins
== null ?
null : getSignalServiceAddresses(removeAdmins
),
901 editDetailsPermission
,
906 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
907 final GroupId groupId
,
909 final String description
,
910 final Set
<RecipientId
> members
,
911 final Set
<RecipientId
> removeMembers
,
912 final Set
<RecipientId
> admins
,
913 final Set
<RecipientId
> removeAdmins
,
914 final boolean resetGroupLink
,
915 final GroupLinkState groupLinkState
,
916 final GroupPermission addMemberPermission
,
917 final GroupPermission editDetailsPermission
,
918 final File avatarFile
,
919 final Integer expirationTimer
920 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
921 var group
= getGroupForUpdating(groupId
);
923 if (group
instanceof GroupInfoV2
) {
925 return updateGroupV2((GroupInfoV2
) group
,
935 editDetailsPermission
,
938 } catch (ConflictException e
) {
939 // Detected conflicting update, refreshing group and trying again
940 group
= getGroup(groupId
, true);
941 return updateGroupV2((GroupInfoV2
) group
,
951 editDetailsPermission
,
957 final var gv1
= (GroupInfoV1
) group
;
958 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
959 if (expirationTimer
!= null) {
960 setExpirationTimer(gv1
, expirationTimer
);
965 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
966 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
967 ) throws IOException
, AttachmentInvalidException
{
968 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
969 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
971 account
.getGroupStore().updateGroup(gv1
);
973 return sendMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
976 private void updateGroupV1Details(
977 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
978 ) throws IOException
{
983 if (members
!= null) {
984 final var newMemberAddresses
= members
.stream()
985 .filter(member
-> !g
.isMember(member
))
986 .map(this::resolveSignalServiceAddress
)
987 .collect(Collectors
.toList());
988 final var newE164Members
= new HashSet
<String
>();
989 for (var member
: newMemberAddresses
) {
990 if (!member
.getNumber().isPresent()) {
993 newE164Members
.add(member
.getNumber().get());
996 final var registeredUsers
= getRegisteredUsers(newE164Members
);
997 if (registeredUsers
.size() != newE164Members
.size()) {
998 // Some of the new members are not registered on Signal
999 newE164Members
.removeAll(registeredUsers
.keySet());
1000 throw new IOException("Failed to add members "
1001 + String
.join(", ", newE164Members
)
1002 + " to group: Not registered on Signal");
1005 g
.addMembers(members
);
1008 if (avatarFile
!= null) {
1009 avatarStore
.storeGroupAvatar(g
.getGroupId(),
1010 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1014 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
1015 final GroupInfoV2 group
,
1017 final String description
,
1018 final Set
<RecipientId
> members
,
1019 final Set
<RecipientId
> removeMembers
,
1020 final Set
<RecipientId
> admins
,
1021 final Set
<RecipientId
> removeAdmins
,
1022 final boolean resetGroupLink
,
1023 final GroupLinkState groupLinkState
,
1024 final GroupPermission addMemberPermission
,
1025 final GroupPermission editDetailsPermission
,
1026 final File avatarFile
,
1027 Integer expirationTimer
1028 ) throws IOException
{
1029 Pair
<Long
, List
<SendMessageResult
>> result
= null;
1030 if (group
.isPendingMember(account
.getSelfRecipientId())) {
1031 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
1032 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1035 if (members
!= null) {
1036 final var newMembers
= new HashSet
<>(members
);
1037 newMembers
.removeAll(group
.getMembers());
1038 if (newMembers
.size() > 0) {
1039 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
1040 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1044 if (removeMembers
!= null) {
1045 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
1046 existingRemoveMembers
.retainAll(group
.getMembers());
1047 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
1048 if (existingRemoveMembers
.size() > 0) {
1049 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
1050 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1053 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
1054 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
1055 if (pendingRemoveMembers
.size() > 0) {
1056 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
1057 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1061 if (admins
!= null) {
1062 final var newAdmins
= new HashSet
<>(admins
);
1063 newAdmins
.retainAll(group
.getMembers());
1064 newAdmins
.removeAll(group
.getAdminMembers());
1065 if (newAdmins
.size() > 0) {
1066 for (var admin
: newAdmins
) {
1067 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1068 result
= sendUpdateGroupV2Message(group
,
1069 groupGroupChangePair
.first(),
1070 groupGroupChangePair
.second());
1075 if (removeAdmins
!= null) {
1076 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1077 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1078 if (existingRemoveAdmins
.size() > 0) {
1079 for (var admin
: existingRemoveAdmins
) {
1080 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1081 result
= sendUpdateGroupV2Message(group
,
1082 groupGroupChangePair
.first(),
1083 groupGroupChangePair
.second());
1088 if (resetGroupLink
) {
1089 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1090 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1093 if (groupLinkState
!= null) {
1094 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1095 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1098 if (addMemberPermission
!= null) {
1099 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1100 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1103 if (editDetailsPermission
!= null) {
1104 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1105 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1108 if (expirationTimer
!= null) {
1109 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1110 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1113 if (name
!= null || description
!= null || avatarFile
!= null) {
1114 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1115 if (avatarFile
!= null) {
1116 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1117 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1119 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1125 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1126 GroupInviteLinkUrl inviteLinkUrl
1127 ) throws IOException
, GroupLinkNotActiveException
{
1128 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1129 inviteLinkUrl
.getPassword());
1130 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1131 inviteLinkUrl
.getPassword(),
1133 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1134 groupJoinInfo
.getRevision() + 1,
1135 groupChange
.toByteArray());
1137 if (group
.getGroup() == null) {
1138 // Only requested member, can't send update to group members
1139 return new Pair
<>(group
.getGroupId(), List
.of());
1142 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1144 return new Pair
<>(group
.getGroupId(), result
.second());
1147 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1148 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1149 ) throws IOException
{
1150 final var selfRecipientId
= account
.getSelfRecipientId();
1151 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1152 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1153 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1155 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1156 account
.getGroupStore().updateGroup(group
);
1157 return sendMessage(messageBuilder
, members
);
1160 private static int currentTimeDays() {
1161 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1164 private GroupsV2AuthorizationString
getGroupAuthForToday(
1165 final GroupSecretParams groupSecretParams
1166 ) throws IOException
{
1167 final var today
= currentTimeDays();
1168 // Returns credentials for the next 7 days
1169 final var credentials
= groupsV2Api
.getCredentials(today
);
1170 // TODO cache credentials until they expire
1171 var authCredentialResponse
= credentials
.get(today
);
1173 return groupsV2Api
.getGroupsV2AuthorizationString(account
.getUuid(),
1176 authCredentialResponse
);
1177 } catch (VerificationFailedException e
) {
1178 throw new IOException(e
);
1182 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1183 GroupIdV1 groupId
, SignalServiceAddress recipient
1184 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1186 var group
= getGroupForSending(groupId
);
1187 if (!(group
instanceof GroupInfoV1
)) {
1188 throw new RuntimeException("Received an invalid group request for a v2 group!");
1190 g
= (GroupInfoV1
) group
;
1192 final var recipientId
= resolveRecipient(recipient
);
1193 if (!g
.isMember(recipientId
)) {
1194 throw new NotAGroupMemberException(groupId
, g
.name
);
1197 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1199 // Send group message only to the recipient who requested it
1200 return sendMessage(messageBuilder
, Set
.of(recipientId
));
1203 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1204 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1205 .withId(g
.getGroupId().serialize())
1207 .withMembers(g
.getMembers()
1209 .map(this::resolveSignalServiceAddress
)
1210 .collect(Collectors
.toList()));
1213 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1214 if (attachment
.isPresent()) {
1215 group
.withAvatar(attachment
.get());
1217 } catch (IOException e
) {
1218 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1221 return SignalServiceDataMessage
.newBuilder()
1222 .asGroupMessage(group
.build())
1223 .withExpiration(g
.getMessageExpirationTime());
1226 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1227 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1228 .withRevision(g
.getGroup().getRevision())
1229 .withSignedGroupChange(signedGroupChange
);
1230 return SignalServiceDataMessage
.newBuilder()
1231 .asGroupMessage(group
.build())
1232 .withExpiration(g
.getMessageExpirationTime());
1235 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1236 GroupIdV1 groupId
, SignalServiceAddress recipient
1237 ) throws IOException
{
1238 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1240 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1242 // Send group info request message to the recipient who sent us a message with this groupId
1243 return sendMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1247 SignalServiceAddress remoteAddress
, long messageId
1248 ) throws IOException
, UntrustedIdentityException
{
1249 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1251 System
.currentTimeMillis());
1253 createMessageSender().sendReceipt(remoteAddress
,
1254 unidentifiedAccessHelper
.getAccessFor(resolveRecipient(remoteAddress
)),
1258 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1259 String messageText
, List
<String
> attachments
, List
<String
> recipients
1260 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1261 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1262 if (attachments
!= null) {
1263 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1265 // Upload attachments here, so we only upload once even for multiple recipients
1266 var messageSender
= createMessageSender();
1267 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1268 for (var attachment
: attachmentStreams
) {
1269 if (attachment
.isStream()) {
1270 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1271 } else if (attachment
.isPointer()) {
1272 attachmentPointers
.add(attachment
.asPointer());
1276 messageBuilder
.withAttachments(attachmentPointers
);
1278 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1281 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1282 String messageText
, List
<String
> attachments
1283 ) throws IOException
, AttachmentInvalidException
{
1284 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1285 if (attachments
!= null) {
1286 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1288 return sendSelfMessage(messageBuilder
);
1291 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1292 long targetSentTimestamp
, List
<String
> recipients
1293 ) throws IOException
, InvalidNumberException
{
1294 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1295 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1296 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1299 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1300 long targetSentTimestamp
, GroupId groupId
1301 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1302 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1303 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1304 return sendGroupMessage(messageBuilder
, groupId
);
1307 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1308 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1309 ) throws IOException
, InvalidNumberException
{
1310 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1311 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1313 resolveSignalServiceAddress(targetAuthorRecipientId
),
1314 targetSentTimestamp
);
1315 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1316 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1319 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1320 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1322 final var signalServiceAddresses
= getSignalServiceAddresses(recipients
);
1324 return sendMessage(messageBuilder
, signalServiceAddresses
);
1325 } catch (Exception e
) {
1326 for (var address
: signalServiceAddresses
) {
1327 handleEndSession(address
);
1333 void renewSession(RecipientId recipientId
) throws IOException
{
1334 account
.getSessionStore().archiveSessions(recipientId
);
1335 if (!recipientId
.equals(getSelfRecipientId())) {
1336 sendNullMessage(recipientId
);
1340 public String
getContactName(String number
) throws InvalidNumberException
{
1341 var contact
= account
.getContactStore().getContact(canonicalizeAndResolveRecipient(number
));
1342 return contact
== null || contact
.getName() == null ?
"" : contact
.getName();
1345 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1346 if (!account
.isMasterDevice()) {
1347 throw new NotMasterDeviceException();
1349 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1350 var contact
= account
.getContactStore().getContact(recipientId
);
1351 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1352 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1355 public void setContactBlocked(
1356 String number
, boolean blocked
1357 ) throws InvalidNumberException
, NotMasterDeviceException
{
1358 if (!account
.isMasterDevice()) {
1359 throw new NotMasterDeviceException();
1361 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1364 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1365 var contact
= account
.getContactStore().getContact(recipientId
);
1366 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1367 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1370 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1371 var group
= getGroup(groupId
);
1372 if (group
== null) {
1373 throw new GroupNotFoundException(groupId
);
1376 group
.setBlocked(blocked
);
1377 account
.getGroupStore().updateGroup(group
);
1380 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1381 var contact
= account
.getContactStore().getContact(recipientId
);
1382 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1385 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1386 account
.getContactStore()
1387 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1390 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1391 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1392 sendMessage(messageBuilder
, Set
.of(recipientId
));
1396 * Change the expiration timer for a contact
1398 public void setExpirationTimer(
1399 String number
, int messageExpirationTimer
1400 ) throws IOException
, InvalidNumberException
{
1401 var recipientId
= canonicalizeAndResolveRecipient(number
);
1402 setExpirationTimer(recipientId
, messageExpirationTimer
);
1403 sendExpirationTimerUpdate(recipientId
);
1407 * Change the expiration timer for a group
1409 private void setExpirationTimer(
1410 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1411 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1412 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1413 account
.getGroupStore().updateGroup(groupInfoV1
);
1414 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1417 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1418 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1419 sendGroupMessage(messageBuilder
, groupId
);
1423 * Upload the sticker pack from path.
1425 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1426 * @return if successful, returns the URL to install the sticker pack in the signal app
1428 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1429 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1431 var messageSender
= createMessageSender();
1433 var packKey
= KeyUtils
.createStickerUploadKey();
1434 var packId
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1436 var sticker
= new Sticker(StickerPackId
.deserialize(Hex
.fromStringCondensed(packId
)), packKey
);
1437 account
.getStickerStore().updateSticker(sticker
);
1440 return new URI("https",
1443 "pack_id=" + URLEncoder
.encode(packId
, StandardCharsets
.UTF_8
) + "&pack_key=" + URLEncoder
.encode(
1444 Hex
.toStringCondensed(packKey
),
1445 StandardCharsets
.UTF_8
)).toString();
1446 } catch (URISyntaxException e
) {
1447 throw new AssertionError(e
);
1451 public void requestAllSyncData() throws IOException
{
1452 requestSyncGroups();
1453 requestSyncContacts();
1454 requestSyncBlocked();
1455 requestSyncConfiguration();
1459 private void requestSyncGroups() throws IOException
{
1460 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1461 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1463 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1465 sendSyncMessage(message
);
1466 } catch (UntrustedIdentityException e
) {
1467 throw new AssertionError(e
);
1471 private void requestSyncContacts() throws IOException
{
1472 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1473 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1475 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1477 sendSyncMessage(message
);
1478 } catch (UntrustedIdentityException e
) {
1479 throw new AssertionError(e
);
1483 private void requestSyncBlocked() throws IOException
{
1484 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1485 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1487 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1489 sendSyncMessage(message
);
1490 } catch (UntrustedIdentityException e
) {
1491 throw new AssertionError(e
);
1495 private void requestSyncConfiguration() throws IOException
{
1496 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1497 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1499 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1501 sendSyncMessage(message
);
1502 } catch (UntrustedIdentityException e
) {
1503 throw new AssertionError(e
);
1507 private void requestSyncKeys() throws IOException
{
1508 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1509 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1511 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1513 sendSyncMessage(message
);
1514 } catch (UntrustedIdentityException e
) {
1515 throw new AssertionError(e
);
1519 private byte[] getSenderCertificate() {
1522 if (account
.isPhoneNumberShared()) {
1523 certificate
= accountManager
.getSenderCertificate();
1525 certificate
= accountManager
.getSenderCertificateForPhoneNumberPrivacy();
1527 } catch (IOException e
) {
1528 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1531 // TODO cache for a day
1535 private void sendSyncMessage(SignalServiceSyncMessage message
) throws IOException
, UntrustedIdentityException
{
1536 var messageSender
= createMessageSender();
1537 messageSender
.sendMessage(message
, unidentifiedAccessHelper
.getAccessForSync());
1540 private Set
<RecipientId
> getSignalServiceAddresses(Collection
<String
> numbers
) throws InvalidNumberException
{
1541 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1542 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1544 for (var number
: numbers
) {
1545 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1546 if (resolvedAddress
.getUuid().isPresent()) {
1547 signalServiceAddresses
.add(resolvedAddress
);
1549 addressesMissingUuid
.add(resolvedAddress
);
1553 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1554 .map(a
-> a
.getNumber().get())
1555 .collect(Collectors
.toSet());
1556 Map
<String
, UUID
> registeredUsers
;
1558 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1559 } catch (IOException e
) {
1560 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1561 registeredUsers
= Map
.of();
1564 for (var address
: addressesMissingUuid
) {
1565 final var number
= address
.getNumber().get();
1566 if (registeredUsers
.containsKey(number
)) {
1567 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1568 registeredUsers
.get(number
),
1570 signalServiceAddresses
.add(newAddress
);
1572 signalServiceAddresses
.add(address
);
1576 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1579 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1580 final var address
= resolveSignalServiceAddress(recipientId
);
1581 if (!address
.getNumber().isPresent()) {
1584 final var number
= address
.getNumber().get();
1585 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1586 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1589 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1591 return accountManager
.getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1593 serviceEnvironmentConfig
.getCdsMrenclave());
1594 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1595 throw new IOException(e
);
1599 private Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1600 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientId
> recipientIds
1601 ) throws IOException
{
1602 final var timestamp
= System
.currentTimeMillis();
1603 messageBuilder
.withTimestamp(timestamp
);
1604 getOrCreateMessagePipe();
1605 getOrCreateUnidentifiedMessagePipe();
1606 SignalServiceDataMessage message
= null;
1608 message
= messageBuilder
.build();
1609 if (message
.getGroupContext().isPresent()) {
1611 var messageSender
= createMessageSender();
1612 final var isRecipientUpdate
= false;
1613 final var recipientIdList
= new ArrayList
<>(recipientIds
);
1614 final var addresses
= recipientIdList
.stream()
1615 .map(this::resolveSignalServiceAddress
)
1616 .collect(Collectors
.toList());
1617 var result
= messageSender
.sendMessage(addresses
,
1618 unidentifiedAccessHelper
.getAccessFor(recipientIdList
),
1622 for (var r
: result
) {
1623 if (r
.getIdentityFailure() != null) {
1624 final var recipientId
= resolveRecipient(r
.getAddress());
1625 final var newIdentity
= account
.getIdentityKeyStore()
1626 .saveIdentity(recipientId
, r
.getIdentityFailure().getIdentityKey(), new Date());
1628 account
.getSessionStore().archiveSessions(recipientId
);
1633 return new Pair
<>(timestamp
, result
);
1634 } catch (UntrustedIdentityException e
) {
1635 return new Pair
<>(timestamp
, List
.of());
1638 // Send to all individually, so sync messages are sent correctly
1639 messageBuilder
.withProfileKey(account
.getProfileKey().serialize());
1640 var results
= new ArrayList
<SendMessageResult
>(recipientIds
.size());
1641 for (var recipientId
: recipientIds
) {
1642 final var contact
= account
.getContactStore().getContact(recipientId
);
1643 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1644 messageBuilder
.withExpiration(expirationTime
);
1645 message
= messageBuilder
.build();
1646 results
.add(sendMessage(recipientId
, message
));
1648 return new Pair
<>(timestamp
, results
);
1651 if (message
!= null && message
.isEndSession()) {
1652 for (var recipient
: recipientIds
) {
1653 handleEndSession(recipient
);
1659 private Pair
<Long
, SendMessageResult
> sendSelfMessage(
1660 SignalServiceDataMessage
.Builder messageBuilder
1661 ) throws IOException
{
1662 final var timestamp
= System
.currentTimeMillis();
1663 messageBuilder
.withTimestamp(timestamp
);
1664 getOrCreateMessagePipe();
1665 getOrCreateUnidentifiedMessagePipe();
1666 final var recipientId
= account
.getSelfRecipientId();
1668 final var contact
= account
.getContactStore().getContact(recipientId
);
1669 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1670 messageBuilder
.withExpiration(expirationTime
);
1672 var message
= messageBuilder
.build();
1673 final var result
= sendSelfMessage(message
);
1674 return new Pair
<>(timestamp
, result
);
1677 private SendMessageResult
sendSelfMessage(SignalServiceDataMessage message
) throws IOException
{
1678 var messageSender
= createMessageSender();
1680 var recipientId
= account
.getSelfRecipientId();
1682 final var unidentifiedAccess
= unidentifiedAccessHelper
.getAccessFor(recipientId
);
1683 var recipient
= resolveSignalServiceAddress(recipientId
);
1684 var transcript
= new SentTranscriptMessage(Optional
.of(recipient
),
1685 message
.getTimestamp(),
1687 message
.getExpiresInSeconds(),
1688 Map
.of(recipient
, unidentifiedAccess
.isPresent()),
1690 var syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
1693 var startTime
= System
.currentTimeMillis();
1694 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
1695 return SendMessageResult
.success(recipient
,
1696 unidentifiedAccess
.isPresent(),
1698 System
.currentTimeMillis() - startTime
);
1699 } catch (UntrustedIdentityException e
) {
1700 return SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey());
1704 private SendMessageResult
sendMessage(
1705 RecipientId recipientId
, SignalServiceDataMessage message
1706 ) throws IOException
{
1707 var messageSender
= createMessageSender();
1709 final var address
= resolveSignalServiceAddress(recipientId
);
1712 return messageSender
.sendMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
), message
);
1713 } catch (UnregisteredUserException e
) {
1714 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1715 return messageSender
.sendMessage(resolveSignalServiceAddress(newRecipientId
),
1716 unidentifiedAccessHelper
.getAccessFor(newRecipientId
),
1719 } catch (UntrustedIdentityException e
) {
1720 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1724 private SendMessageResult
sendNullMessage(RecipientId recipientId
) throws IOException
{
1725 var messageSender
= createMessageSender();
1727 final var address
= resolveSignalServiceAddress(recipientId
);
1730 return messageSender
.sendNullMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
));
1731 } catch (UnregisteredUserException e
) {
1732 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1733 final var newAddress
= resolveSignalServiceAddress(newRecipientId
);
1734 return messageSender
.sendNullMessage(newAddress
, unidentifiedAccessHelper
.getAccessFor(newRecipientId
));
1736 } catch (UntrustedIdentityException e
) {
1737 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1741 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
{
1742 var cipher
= new SignalServiceCipher(account
.getSelfAddress(),
1743 account
.getSignalProtocolStore(),
1745 certificateValidator
);
1746 return cipher
.decrypt(envelope
);
1749 private void handleEndSession(RecipientId recipientId
) {
1750 account
.getSessionStore().deleteAllSessions(recipientId
);
1753 private List
<HandleAction
> handleSignalServiceDataMessage(
1754 SignalServiceDataMessage message
,
1756 SignalServiceAddress source
,
1757 SignalServiceAddress destination
,
1758 boolean ignoreAttachments
1760 var actions
= new ArrayList
<HandleAction
>();
1761 if (message
.getGroupContext().isPresent()) {
1762 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1763 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1764 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1765 var group
= getGroup(groupId
);
1766 if (group
== null || group
instanceof GroupInfoV1
) {
1767 var groupV1
= (GroupInfoV1
) group
;
1768 switch (groupInfo
.getType()) {
1770 if (groupV1
== null) {
1771 groupV1
= new GroupInfoV1(groupId
);
1774 if (groupInfo
.getAvatar().isPresent()) {
1775 var avatar
= groupInfo
.getAvatar().get();
1776 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1779 if (groupInfo
.getName().isPresent()) {
1780 groupV1
.name
= groupInfo
.getName().get();
1783 if (groupInfo
.getMembers().isPresent()) {
1784 groupV1
.addMembers(groupInfo
.getMembers()
1787 .map(this::resolveRecipient
)
1788 .collect(Collectors
.toSet()));
1791 account
.getGroupStore().updateGroup(groupV1
);
1795 if (groupV1
== null && !isSync
) {
1796 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1800 if (groupV1
!= null) {
1801 groupV1
.removeMember(resolveRecipient(source
));
1802 account
.getGroupStore().updateGroup(groupV1
);
1807 if (groupV1
!= null && !isSync
) {
1808 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1813 // Received a group v1 message for a v2 group
1816 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1817 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1818 final var groupMasterKey
= groupContext
.getMasterKey();
1820 getOrMigrateGroup(groupMasterKey
,
1821 groupContext
.getRevision(),
1822 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1826 final var conversationPartnerAddress
= isSync ? destination
: source
;
1827 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1828 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1830 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1831 if (message
.getGroupContext().isPresent()) {
1832 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1833 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1834 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1835 if (group
!= null) {
1836 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1837 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1838 account
.getGroupStore().updateGroup(group
);
1841 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1842 // disappearing message timer already stored in the DecryptedGroup
1844 } else if (conversationPartnerAddress
!= null) {
1845 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1848 if (!ignoreAttachments
) {
1849 if (message
.getAttachments().isPresent()) {
1850 for (var attachment
: message
.getAttachments().get()) {
1851 downloadAttachment(attachment
);
1854 if (message
.getSharedContacts().isPresent()) {
1855 for (var contact
: message
.getSharedContacts().get()) {
1856 if (contact
.getAvatar().isPresent()) {
1857 downloadAttachment(contact
.getAvatar().get().getAttachment());
1862 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1863 final ProfileKey profileKey
;
1865 profileKey
= new ProfileKey(message
.getProfileKey().get());
1866 } catch (InvalidInputException e
) {
1867 throw new AssertionError(e
);
1869 if (source
.matches(account
.getSelfAddress())) {
1870 this.account
.setProfileKey(profileKey
);
1872 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1874 if (message
.getPreviews().isPresent()) {
1875 final var previews
= message
.getPreviews().get();
1876 for (var preview
: previews
) {
1877 if (preview
.getImage().isPresent()) {
1878 downloadAttachment(preview
.getImage().get());
1882 if (message
.getQuote().isPresent()) {
1883 final var quote
= message
.getQuote().get();
1885 for (var quotedAttachment
: quote
.getAttachments()) {
1886 final var thumbnail
= quotedAttachment
.getThumbnail();
1887 if (thumbnail
!= null) {
1888 downloadAttachment(thumbnail
);
1892 if (message
.getSticker().isPresent()) {
1893 final var messageSticker
= message
.getSticker().get();
1894 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1895 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1896 if (sticker
== null) {
1897 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1898 account
.getStickerStore().updateSticker(sticker
);
1904 private GroupInfoV2
getOrMigrateGroup(
1905 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1907 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1909 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1910 var groupInfo
= getGroup(groupId
);
1911 final GroupInfoV2 groupInfoV2
;
1912 if (groupInfo
instanceof GroupInfoV1
) {
1913 // Received a v2 group message for a v1 group, we need to locally migrate the group
1914 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1915 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1916 logger
.info("Locally migrated group {} to group v2, id: {}",
1917 groupInfo
.getGroupId().toBase64(),
1918 groupInfoV2
.getGroupId().toBase64());
1919 } else if (groupInfo
instanceof GroupInfoV2
) {
1920 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1922 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1925 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1926 DecryptedGroup group
= null;
1927 if (signedGroupChange
!= null
1928 && groupInfoV2
.getGroup() != null
1929 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1930 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1934 if (group
== null) {
1935 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1937 if (group
!= null) {
1938 storeProfileKeysFromMembers(group
);
1939 final var avatar
= group
.getAvatar();
1940 if (avatar
!= null && !avatar
.isEmpty()) {
1941 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1944 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1945 account
.getGroupStore().updateGroup(groupInfoV2
);
1951 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1952 for (var member
: group
.getMembersList()) {
1953 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1954 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1956 account
.getProfileStore()
1957 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1958 } catch (InvalidInputException ignored
) {
1963 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1964 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1965 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1966 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1967 if (actions
!= null) {
1968 queuedActions
.addAll(actions
);
1971 for (var action
: queuedActions
) {
1973 action
.execute(this);
1974 } catch (Throwable e
) {
1975 logger
.warn("Message action failed.", e
);
1980 private List
<HandleAction
> retryFailedReceivedMessage(
1981 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1983 var envelope
= cachedMessage
.loadEnvelope();
1984 if (envelope
== null) {
1987 SignalServiceContent content
= null;
1988 List
<HandleAction
> actions
= null;
1989 if (!envelope
.isReceipt()) {
1991 content
= decryptMessage(envelope
);
1992 } catch (ProtocolUntrustedIdentityException e
) {
1993 if (!envelope
.hasSource()) {
1994 final var identifier
= e
.getSender();
1995 final var recipientId
= resolveRecipient(identifier
);
1997 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1998 } catch (IOException ioException
) {
1999 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
2003 } catch (Exception er
) {
2004 // All other errors are not recoverable, so delete the cached message
2005 cachedMessage
.delete();
2008 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2010 handler
.handleMessage(envelope
, content
, null);
2011 cachedMessage
.delete();
2015 public void receiveMessages(
2018 boolean returnOnTimeout
,
2019 boolean ignoreAttachments
,
2020 ReceiveMessageHandler handler
2021 ) throws IOException
{
2022 retryFailedReceivedMessages(handler
, ignoreAttachments
);
2024 Set
<HandleAction
> queuedActions
= null;
2026 final var messagePipe
= getOrCreateMessagePipe();
2028 var hasCaughtUpWithOldMessages
= false;
2031 SignalServiceEnvelope envelope
;
2032 SignalServiceContent content
= null;
2033 Exception exception
= null;
2034 final CachedMessage
[] cachedMessage
= {null};
2035 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
2036 logger
.debug("Checking for new message from server");
2038 var result
= messagePipe
.readOrEmpty(timeout
, unit
, envelope1
-> {
2039 final var recipientId
= envelope1
.hasSource()
2040 ?
resolveRecipient(envelope1
.getSourceIdentifier())
2042 // store message on disk, before acknowledging receipt to the server
2043 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
2045 logger
.debug("New message received from server");
2046 if (result
.isPresent()) {
2047 envelope
= result
.get();
2049 // Received indicator that server queue is empty
2050 hasCaughtUpWithOldMessages
= true;
2052 if (queuedActions
!= null) {
2053 for (var action
: queuedActions
) {
2055 action
.execute(this);
2056 } catch (Throwable e
) {
2057 logger
.warn("Message action failed.", e
);
2060 queuedActions
.clear();
2061 queuedActions
= null;
2064 // Continue to wait another timeout for new messages
2067 } catch (TimeoutException e
) {
2068 if (returnOnTimeout
) return;
2072 if (envelope
.hasSource()) {
2073 // Store uuid if we don't have it already
2074 // address/uuid in envelope is sent by server
2075 resolveRecipientTrusted(envelope
.getSourceAddress());
2077 final var notAGroupMember
= isNotAGroupMember(envelope
, content
);
2078 if (!envelope
.isReceipt()) {
2080 content
= decryptMessage(envelope
);
2081 } catch (Exception e
) {
2084 if (!envelope
.hasSource() && content
!= null) {
2085 // Store uuid if we don't have it already
2086 // address/uuid is validated by unidentified sender certificate
2087 resolveRecipientTrusted(content
.getSender());
2089 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2090 if (exception
instanceof ProtocolInvalidMessageException
) {
2091 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
2092 logger
.debug("Received invalid message, queuing renew session action.");
2093 actions
.add(new RenewSessionAction(sender
));
2095 if (hasCaughtUpWithOldMessages
) {
2096 for (var action
: actions
) {
2098 action
.execute(this);
2099 } catch (Throwable e
) {
2100 logger
.warn("Message action failed.", e
);
2104 if (queuedActions
== null) {
2105 queuedActions
= new HashSet
<>();
2107 queuedActions
.addAll(actions
);
2110 if (isMessageBlocked(envelope
, content
)) {
2111 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
2112 } else if (notAGroupMember
) {
2113 logger
.info("Ignoring a message from a non group member: {}", envelope
.getTimestamp());
2115 handler
.handleMessage(envelope
, content
, exception
);
2117 if (cachedMessage
[0] != null) {
2118 if (exception
instanceof ProtocolUntrustedIdentityException
) {
2119 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
2120 final var recipientId
= resolveRecipient(identifier
);
2121 queuedActions
.add(new RetrieveProfileAction(recipientId
));
2122 if (!envelope
.hasSource()) {
2124 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
2125 } catch (IOException ioException
) {
2126 logger
.warn("Failed to move cached message to recipient folder: {}",
2127 ioException
.getMessage());
2131 cachedMessage
[0].delete();
2137 private boolean isMessageBlocked(
2138 SignalServiceEnvelope envelope
, SignalServiceContent content
2140 SignalServiceAddress source
;
2141 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2142 source
= envelope
.getSourceAddress();
2143 } else if (content
!= null) {
2144 source
= content
.getSender();
2148 final var recipientId
= resolveRecipient(source
);
2149 if (isContactBlocked(recipientId
)) {
2153 if (content
!= null && content
.getDataMessage().isPresent()) {
2154 var message
= content
.getDataMessage().get();
2155 if (message
.getGroupContext().isPresent()) {
2156 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2157 var group
= getGroup(groupId
);
2158 if (group
!= null && group
.isBlocked()) {
2166 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2167 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2168 return isContactBlocked(recipientId
);
2171 private boolean isContactBlocked(final RecipientId recipientId
) {
2172 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2173 return sourceContact
!= null && sourceContact
.isBlocked();
2176 private boolean isNotAGroupMember(
2177 SignalServiceEnvelope envelope
, SignalServiceContent content
2179 SignalServiceAddress source
;
2180 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2181 source
= envelope
.getSourceAddress();
2182 } else if (content
!= null) {
2183 source
= content
.getSender();
2188 if (content
!= null && content
.getDataMessage().isPresent()) {
2189 var message
= content
.getDataMessage().get();
2190 if (message
.getGroupContext().isPresent()) {
2191 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2192 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2193 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2197 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2198 var group
= getGroup(groupId
);
2199 if (group
!= null && !group
.isMember(resolveRecipient(source
))) {
2207 private List
<HandleAction
> handleMessage(
2208 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2210 var actions
= new ArrayList
<HandleAction
>();
2211 if (content
!= null) {
2212 final SignalServiceAddress sender
;
2213 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2214 sender
= envelope
.getSourceAddress();
2216 sender
= content
.getSender();
2219 if (content
.getDataMessage().isPresent()) {
2220 var message
= content
.getDataMessage().get();
2222 if (content
.isNeedsReceipt()) {
2223 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2226 actions
.addAll(handleSignalServiceDataMessage(message
,
2229 account
.getSelfAddress(),
2230 ignoreAttachments
));
2232 if (content
.getSyncMessage().isPresent()) {
2233 account
.setMultiDevice(true);
2234 var syncMessage
= content
.getSyncMessage().get();
2235 if (syncMessage
.getSent().isPresent()) {
2236 var message
= syncMessage
.getSent().get();
2237 final var destination
= message
.getDestination().orNull();
2238 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2242 ignoreAttachments
));
2244 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2245 var rm
= syncMessage
.getRequest().get();
2246 if (rm
.isContactsRequest()) {
2247 actions
.add(SendSyncContactsAction
.create());
2249 if (rm
.isGroupsRequest()) {
2250 actions
.add(SendSyncGroupsAction
.create());
2252 if (rm
.isBlockedListRequest()) {
2253 actions
.add(SendSyncBlockedListAction
.create());
2255 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2257 if (syncMessage
.getGroups().isPresent()) {
2258 File tmpFile
= null;
2260 tmpFile
= IOUtils
.createTempFile();
2261 final var groupsMessage
= syncMessage
.getGroups().get();
2262 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2263 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2268 } catch (IOException e
) {
2269 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2275 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2276 if (syncGroup
!= null) {
2277 if (g
.getName().isPresent()) {
2278 syncGroup
.name
= g
.getName().get();
2280 syncGroup
.addMembers(g
.getMembers()
2282 .map(this::resolveRecipient
)
2283 .collect(Collectors
.toSet()));
2284 if (!g
.isActive()) {
2285 syncGroup
.removeMember(account
.getSelfRecipientId());
2287 // Add ourself to the member set as it's marked as active
2288 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2290 syncGroup
.blocked
= g
.isBlocked();
2291 if (g
.getColor().isPresent()) {
2292 syncGroup
.color
= g
.getColor().get();
2295 if (g
.getAvatar().isPresent()) {
2296 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2298 syncGroup
.archived
= g
.isArchived();
2299 account
.getGroupStore().updateGroup(syncGroup
);
2303 } catch (Exception e
) {
2304 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2308 if (tmpFile
!= null) {
2310 Files
.delete(tmpFile
.toPath());
2311 } catch (IOException e
) {
2312 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2319 if (syncMessage
.getBlockedList().isPresent()) {
2320 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2321 for (var address
: blockedListMessage
.getAddresses()) {
2322 setContactBlocked(resolveRecipient(address
), true);
2324 for (var groupId
: blockedListMessage
.getGroupIds()
2326 .map(GroupId
::unknownVersion
)
2327 .collect(Collectors
.toSet())) {
2329 setGroupBlocked(groupId
, true);
2330 } catch (GroupNotFoundException e
) {
2331 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2332 groupId
.toBase64());
2336 if (syncMessage
.getContacts().isPresent()) {
2337 File tmpFile
= null;
2339 tmpFile
= IOUtils
.createTempFile();
2340 final var contactsMessage
= syncMessage
.getContacts().get();
2341 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2342 .asPointer(), tmpFile
)) {
2343 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2348 } catch (IOException e
) {
2349 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2356 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2357 account
.setProfileKey(c
.getProfileKey().get());
2359 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2360 var contact
= account
.getContactStore().getContact(recipientId
);
2361 final var builder
= contact
== null
2362 ? Contact
.newBuilder()
2363 : Contact
.newBuilder(contact
);
2364 if (c
.getName().isPresent()) {
2365 builder
.withName(c
.getName().get());
2367 if (c
.getColor().isPresent()) {
2368 builder
.withColor(c
.getColor().get());
2370 if (c
.getProfileKey().isPresent()) {
2371 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2373 if (c
.getVerified().isPresent()) {
2374 final var verifiedMessage
= c
.getVerified().get();
2375 account
.getIdentityKeyStore()
2376 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2377 verifiedMessage
.getIdentityKey(),
2378 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2380 if (c
.getExpirationTimer().isPresent()) {
2381 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2383 builder
.withBlocked(c
.isBlocked());
2384 builder
.withArchived(c
.isArchived());
2385 account
.getContactStore().storeContact(recipientId
, builder
.build());
2387 if (c
.getAvatar().isPresent()) {
2388 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2392 } catch (Exception e
) {
2393 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2397 if (tmpFile
!= null) {
2399 Files
.delete(tmpFile
.toPath());
2400 } catch (IOException e
) {
2401 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2408 if (syncMessage
.getVerified().isPresent()) {
2409 final var verifiedMessage
= syncMessage
.getVerified().get();
2410 account
.getIdentityKeyStore()
2411 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2412 verifiedMessage
.getIdentityKey(),
2413 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2415 if (syncMessage
.getStickerPackOperations().isPresent()) {
2416 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2417 for (var m
: stickerPackOperationMessages
) {
2418 if (!m
.getPackId().isPresent()) {
2421 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2422 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2423 if (sticker
== null) {
2424 if (!m
.getPackKey().isPresent()) {
2427 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2429 sticker
.setInstalled(!m
.getType().isPresent()
2430 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
);
2431 account
.getStickerStore().updateSticker(sticker
);
2434 if (syncMessage
.getFetchType().isPresent()) {
2435 switch (syncMessage
.getFetchType().get()) {
2437 getRecipientProfile(account
.getSelfRecipientId(), true);
2438 case STORAGE_MANIFEST
:
2442 if (syncMessage
.getKeys().isPresent()) {
2443 final var keysMessage
= syncMessage
.getKeys().get();
2444 if (keysMessage
.getStorageService().isPresent()) {
2445 final var storageKey
= keysMessage
.getStorageService().get();
2446 account
.setStorageKey(storageKey
);
2449 if (syncMessage
.getConfiguration().isPresent()) {
2457 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2459 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2460 } catch (IOException e
) {
2461 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2465 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2467 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2468 } catch (IOException e
) {
2469 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2473 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2475 avatarStore
.storeGroupAvatar(groupId
,
2476 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2477 } catch (IOException e
) {
2478 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2482 private void downloadProfileAvatar(
2483 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2486 avatarStore
.storeProfileAvatar(address
,
2487 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2488 } catch (Throwable e
) {
2489 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2493 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2494 return attachmentStore
.getAttachmentFile(attachmentId
);
2497 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2498 if (!attachment
.isPointer()) {
2499 logger
.warn("Invalid state, can't store an attachment stream.");
2502 var pointer
= attachment
.asPointer();
2503 if (pointer
.getPreview().isPresent()) {
2504 final var preview
= pointer
.getPreview().get();
2506 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2507 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2508 } catch (IOException e
) {
2509 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2514 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2515 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2516 } catch (IOException e
) {
2517 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2521 private void retrieveGroupV2Avatar(
2522 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2523 ) throws IOException
{
2524 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
2526 var tmpFile
= IOUtils
.createTempFile();
2527 try (InputStream input
= messageReceiver
.retrieveGroupsV2ProfileAvatar(cdnKey
,
2529 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2530 var encryptedData
= IOUtils
.readFully(input
);
2532 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2533 outputStream
.write(decryptedData
);
2536 Files
.delete(tmpFile
.toPath());
2537 } catch (IOException e
) {
2538 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2545 private void retrieveProfileAvatar(
2546 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2547 ) throws IOException
{
2548 var tmpFile
= IOUtils
.createTempFile();
2549 try (var input
= messageReceiver
.retrieveProfileAvatar(avatarPath
,
2552 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2553 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2554 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2557 Files
.delete(tmpFile
.toPath());
2558 } catch (IOException e
) {
2559 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2566 private void retrieveAttachment(
2567 final SignalServiceAttachment attachment
, final OutputStream outputStream
2568 ) throws IOException
{
2569 if (attachment
.isPointer()) {
2570 var pointer
= attachment
.asPointer();
2571 retrieveAttachmentPointer(pointer
, outputStream
);
2573 var stream
= attachment
.asStream();
2574 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2578 private void retrieveAttachmentPointer(
2579 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2580 ) throws IOException
{
2581 var tmpFile
= IOUtils
.createTempFile();
2582 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2583 IOUtils
.copyStream(input
, outputStream
);
2584 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2585 throw new IOException(e
);
2588 Files
.delete(tmpFile
.toPath());
2589 } catch (IOException e
) {
2590 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2597 private InputStream
retrieveAttachmentAsStream(
2598 SignalServiceAttachmentPointer pointer
, File tmpFile
2599 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2600 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2603 void sendGroups() throws IOException
, UntrustedIdentityException
{
2604 var groupsFile
= IOUtils
.createTempFile();
2607 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2608 var out
= new DeviceGroupsOutputStream(fos
);
2609 for (var record : getGroups()) {
2610 if (record instanceof GroupInfoV1
) {
2611 var groupInfo
= (GroupInfoV1
) record;
2612 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2613 Optional
.fromNullable(groupInfo
.name
),
2614 groupInfo
.getMembers()
2616 .map(this::resolveSignalServiceAddress
)
2617 .collect(Collectors
.toList()),
2618 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2619 groupInfo
.isMember(account
.getSelfRecipientId()),
2620 Optional
.of(groupInfo
.messageExpirationTime
),
2621 Optional
.fromNullable(groupInfo
.color
),
2624 groupInfo
.archived
));
2629 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2630 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2631 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2632 .withStream(groupsFileStream
)
2633 .withContentType("application/octet-stream")
2634 .withLength(groupsFile
.length())
2637 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2642 Files
.delete(groupsFile
.toPath());
2643 } catch (IOException e
) {
2644 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2649 public void sendContacts() throws IOException
, UntrustedIdentityException
{
2650 var contactsFile
= IOUtils
.createTempFile();
2653 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2654 var out
= new DeviceContactsOutputStream(fos
);
2655 for (var contactPair
: account
.getContactStore().getContacts()) {
2656 final var recipientId
= contactPair
.first();
2657 final var contact
= contactPair
.second();
2658 final var address
= resolveSignalServiceAddress(recipientId
);
2660 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2661 VerifiedMessage verifiedMessage
= null;
2662 if (currentIdentity
!= null) {
2663 verifiedMessage
= new VerifiedMessage(address
,
2664 currentIdentity
.getIdentityKey(),
2665 currentIdentity
.getTrustLevel().toVerifiedState(),
2666 currentIdentity
.getDateAdded().getTime());
2669 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2670 out
.write(new DeviceContact(address
,
2671 Optional
.fromNullable(contact
.getName()),
2672 createContactAvatarAttachment(address
),
2673 Optional
.fromNullable(contact
.getColor()),
2674 Optional
.fromNullable(verifiedMessage
),
2675 Optional
.fromNullable(profileKey
),
2676 contact
.isBlocked(),
2677 Optional
.of(contact
.getMessageExpirationTime()),
2679 contact
.isArchived()));
2682 if (account
.getProfileKey() != null) {
2683 // Send our own profile key as well
2684 out
.write(new DeviceContact(account
.getSelfAddress(),
2689 Optional
.of(account
.getProfileKey()),
2697 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2698 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2699 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2700 .withStream(contactsFileStream
)
2701 .withContentType("application/octet-stream")
2702 .withLength(contactsFile
.length())
2705 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
2710 Files
.delete(contactsFile
.toPath());
2711 } catch (IOException e
) {
2712 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2717 void sendBlockedList() throws IOException
, UntrustedIdentityException
{
2718 var addresses
= new ArrayList
<SignalServiceAddress
>();
2719 for (var record : account
.getContactStore().getContacts()) {
2720 if (record.second().isBlocked()) {
2721 addresses
.add(resolveSignalServiceAddress(record.first()));
2724 var groupIds
= new ArrayList
<byte[]>();
2725 for (var record : getGroups()) {
2726 if (record.isBlocked()) {
2727 groupIds
.add(record.getGroupId().serialize());
2730 sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2733 private void sendVerifiedMessage(
2734 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2735 ) throws IOException
, UntrustedIdentityException
{
2736 var verifiedMessage
= new VerifiedMessage(destination
,
2738 trustLevel
.toVerifiedState(),
2739 System
.currentTimeMillis());
2740 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2743 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2744 return account
.getContactStore().getContacts();
2747 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2748 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2749 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2750 if (recipient
== null) {
2754 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2755 return recipient
.getContact().getName();
2758 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2759 return recipient
.getProfile().getDisplayName();
2765 public GroupInfo
getGroup(GroupId groupId
) {
2766 return getGroup(groupId
, false);
2769 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2770 final var group
= account
.getGroupStore().getGroup(groupId
);
2771 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2772 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2773 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2774 account
.getGroupStore().updateGroup(group
);
2779 public List
<IdentityInfo
> getIdentities() {
2780 return account
.getIdentityKeyStore().getIdentities();
2783 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2784 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2785 return identity
== null ? List
.of() : List
.of(identity
);
2789 * Trust this the identity with this fingerprint
2791 * @param name username of the identity
2792 * @param fingerprint Fingerprint
2794 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2795 var recipientId
= canonicalizeAndResolveRecipient(name
);
2796 return trustIdentity(recipientId
,
2797 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2798 TrustLevel
.TRUSTED_VERIFIED
);
2802 * Trust this the identity with this safety number
2804 * @param name username of the identity
2805 * @param safetyNumber Safety number
2807 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2808 var recipientId
= canonicalizeAndResolveRecipient(name
);
2809 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2810 return trustIdentity(recipientId
,
2811 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2812 TrustLevel
.TRUSTED_VERIFIED
);
2816 * Trust all keys of this identity without verification
2818 * @param name username of the identity
2820 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2821 var recipientId
= canonicalizeAndResolveRecipient(name
);
2822 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2825 private boolean trustIdentity(
2826 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2828 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2829 if (identity
== null) {
2833 if (!verifier
.apply(identity
.getIdentityKey())) {
2837 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2839 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2840 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2841 } catch (IOException
| UntrustedIdentityException e
) {
2842 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2848 public String
computeSafetyNumber(
2849 SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
2851 return Utils
.computeSafetyNumber(ServiceConfig
.capabilities
.isUuid(),
2852 account
.getSelfAddress(),
2853 getIdentityKeyPair().getPublicKey(),
2859 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2860 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2862 return resolveSignalServiceAddress(address
);
2866 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2867 if (address
.matches(account
.getSelfAddress())) {
2868 return account
.getSelfAddress();
2871 return account
.getRecipientStore().resolveServiceAddress(address
);
2874 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2875 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2878 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2879 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2881 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2883 return resolveRecipient(canonicalizedNumber
);
2886 private RecipientId
resolveRecipient(final String identifier
) {
2887 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2889 return resolveRecipient(address
);
2892 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2893 return account
.getRecipientStore().resolveRecipient(address
);
2896 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2897 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2901 public void close() throws IOException
{
2905 void close(boolean closeAccount
) throws IOException
{
2906 executor
.shutdown();
2908 if (messagePipe
!= null) {
2909 messagePipe
.shutdown();
2913 if (unidentifiedMessagePipe
!= null) {
2914 unidentifiedMessagePipe
.shutdown();
2915 unidentifiedMessagePipe
= null;
2918 if (closeAccount
&& account
!= null) {
2924 public interface ReceiveMessageHandler
{
2926 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);