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 SignalServiceDataMessage
.Builder messageBuilder
;
784 final var g
= getGroupForUpdating(groupId
);
785 if (g
instanceof GroupInfoV1
) {
786 var groupInfoV1
= (GroupInfoV1
) g
;
787 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
).withId(groupId
.serialize()).build();
788 messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
789 groupInfoV1
.removeMember(account
.getSelfRecipientId());
790 account
.getGroupStore().updateGroup(groupInfoV1
);
792 final var groupInfoV2
= (GroupInfoV2
) g
;
793 final var currentAdmins
= g
.getAdminMembers();
794 final var newAdmins
= getSignalServiceAddresses(groupAdmins
);
795 newAdmins
.removeAll(currentAdmins
);
796 newAdmins
.retainAll(g
.getMembers());
797 if (currentAdmins
.contains(getSelfRecipientId())
798 && currentAdmins
.size() == 1
799 && g
.getMembers().size() > 1
800 && newAdmins
.size() == 0) {
801 // Last admin can't leave the group, unless she's also the last member
802 throw new LastGroupAdminException(g
.getGroupId(), g
.getTitle());
804 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
805 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
806 messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
807 account
.getGroupStore().updateGroup(groupInfoV2
);
810 return sendMessage(messageBuilder
, g
.getMembersWithout(account
.getSelfRecipientId()));
813 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
814 String name
, List
<String
> members
, File avatarFile
815 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
816 return createGroup(name
, members
== null ?
null : getSignalServiceAddresses(members
), avatarFile
);
819 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
820 String name
, Set
<RecipientId
> members
, File avatarFile
821 ) throws IOException
, AttachmentInvalidException
{
822 final var selfRecipientId
= account
.getSelfRecipientId();
823 if (members
!= null && members
.contains(selfRecipientId
)) {
824 members
= new HashSet
<>(members
);
825 members
.remove(selfRecipientId
);
828 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
829 members
== null ? Set
.of() : members
,
832 SignalServiceDataMessage
.Builder messageBuilder
;
833 if (gv2Pair
== null) {
834 // Failed to create v2 group, creating v1 group instead
835 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
836 gv1
.addMembers(List
.of(selfRecipientId
));
837 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
838 return new Pair
<>(gv1
.getGroupId(), result
.second());
841 final var gv2
= gv2Pair
.first();
842 final var decryptedGroup
= gv2Pair
.second();
844 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
845 if (avatarFile
!= null) {
846 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
847 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
849 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
850 account
.getGroupStore().updateGroup(gv2
);
852 final var result
= sendMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
853 return new Pair
<>(gv2
.getGroupId(), result
.second());
856 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
860 List
<String
> members
,
861 List
<String
> removeMembers
,
863 List
<String
> removeAdmins
,
864 boolean resetGroupLink
,
865 GroupLinkState groupLinkState
,
866 GroupPermission addMemberPermission
,
867 GroupPermission editDetailsPermission
,
869 Integer expirationTimer
870 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
871 return updateGroup(groupId
,
874 members
== null ?
null : getSignalServiceAddresses(members
),
875 removeMembers
== null ?
null : getSignalServiceAddresses(removeMembers
),
876 admins
== null ?
null : getSignalServiceAddresses(admins
),
877 removeAdmins
== null ?
null : getSignalServiceAddresses(removeAdmins
),
881 editDetailsPermission
,
886 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
887 final GroupId groupId
,
889 final String description
,
890 final Set
<RecipientId
> members
,
891 final Set
<RecipientId
> removeMembers
,
892 final Set
<RecipientId
> admins
,
893 final Set
<RecipientId
> removeAdmins
,
894 final boolean resetGroupLink
,
895 final GroupLinkState groupLinkState
,
896 final GroupPermission addMemberPermission
,
897 final GroupPermission editDetailsPermission
,
898 final File avatarFile
,
899 final Integer expirationTimer
900 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
901 var group
= getGroupForUpdating(groupId
);
903 if (group
instanceof GroupInfoV2
) {
905 return updateGroupV2((GroupInfoV2
) group
,
915 editDetailsPermission
,
918 } catch (ConflictException e
) {
919 // Detected conflicting update, refreshing group and trying again
920 group
= getGroup(groupId
, true);
921 return updateGroupV2((GroupInfoV2
) group
,
931 editDetailsPermission
,
937 final var gv1
= (GroupInfoV1
) group
;
938 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
939 if (expirationTimer
!= null) {
940 setExpirationTimer(gv1
, expirationTimer
);
945 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
946 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
947 ) throws IOException
, AttachmentInvalidException
{
948 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
949 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
951 account
.getGroupStore().updateGroup(gv1
);
953 return sendMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
956 private void updateGroupV1Details(
957 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
958 ) throws IOException
{
963 if (members
!= null) {
964 final var newMemberAddresses
= members
.stream()
965 .filter(member
-> !g
.isMember(member
))
966 .map(this::resolveSignalServiceAddress
)
967 .collect(Collectors
.toList());
968 final var newE164Members
= new HashSet
<String
>();
969 for (var member
: newMemberAddresses
) {
970 if (!member
.getNumber().isPresent()) {
973 newE164Members
.add(member
.getNumber().get());
976 final var registeredUsers
= getRegisteredUsers(newE164Members
);
977 if (registeredUsers
.size() != newE164Members
.size()) {
978 // Some of the new members are not registered on Signal
979 newE164Members
.removeAll(registeredUsers
.keySet());
980 throw new IOException("Failed to add members "
981 + String
.join(", ", newE164Members
)
982 + " to group: Not registered on Signal");
985 g
.addMembers(members
);
988 if (avatarFile
!= null) {
989 avatarStore
.storeGroupAvatar(g
.getGroupId(),
990 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
994 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
995 final GroupInfoV2 group
,
997 final String description
,
998 final Set
<RecipientId
> members
,
999 final Set
<RecipientId
> removeMembers
,
1000 final Set
<RecipientId
> admins
,
1001 final Set
<RecipientId
> removeAdmins
,
1002 final boolean resetGroupLink
,
1003 final GroupLinkState groupLinkState
,
1004 final GroupPermission addMemberPermission
,
1005 final GroupPermission editDetailsPermission
,
1006 final File avatarFile
,
1007 Integer expirationTimer
1008 ) throws IOException
{
1009 Pair
<Long
, List
<SendMessageResult
>> result
= null;
1010 if (group
.isPendingMember(account
.getSelfRecipientId())) {
1011 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
1012 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1015 if (members
!= null) {
1016 final var newMembers
= new HashSet
<>(members
);
1017 newMembers
.removeAll(group
.getMembers());
1018 if (newMembers
.size() > 0) {
1019 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
1020 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1024 if (removeMembers
!= null) {
1025 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
1026 existingRemoveMembers
.retainAll(group
.getMembers());
1027 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
1028 if (existingRemoveMembers
.size() > 0) {
1029 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
1030 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1033 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
1034 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
1035 if (pendingRemoveMembers
.size() > 0) {
1036 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
1037 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1041 if (admins
!= null) {
1042 final var newAdmins
= new HashSet
<>(admins
);
1043 newAdmins
.retainAll(group
.getMembers());
1044 newAdmins
.removeAll(group
.getAdminMembers());
1045 if (newAdmins
.size() > 0) {
1046 for (var admin
: newAdmins
) {
1047 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1048 result
= sendUpdateGroupV2Message(group
,
1049 groupGroupChangePair
.first(),
1050 groupGroupChangePair
.second());
1055 if (removeAdmins
!= null) {
1056 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1057 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1058 if (existingRemoveAdmins
.size() > 0) {
1059 for (var admin
: existingRemoveAdmins
) {
1060 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1061 result
= sendUpdateGroupV2Message(group
,
1062 groupGroupChangePair
.first(),
1063 groupGroupChangePair
.second());
1068 if (resetGroupLink
) {
1069 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1070 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1073 if (groupLinkState
!= null) {
1074 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1075 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1078 if (addMemberPermission
!= null) {
1079 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1080 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1083 if (editDetailsPermission
!= null) {
1084 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1085 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1088 if (expirationTimer
!= null) {
1089 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1090 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1093 if (name
!= null || description
!= null || avatarFile
!= null) {
1094 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1095 if (avatarFile
!= null) {
1096 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1097 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1099 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1105 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1106 GroupInviteLinkUrl inviteLinkUrl
1107 ) throws IOException
, GroupLinkNotActiveException
{
1108 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1109 inviteLinkUrl
.getPassword());
1110 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1111 inviteLinkUrl
.getPassword(),
1113 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1114 groupJoinInfo
.getRevision() + 1,
1115 groupChange
.toByteArray());
1117 if (group
.getGroup() == null) {
1118 // Only requested member, can't send update to group members
1119 return new Pair
<>(group
.getGroupId(), List
.of());
1122 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1124 return new Pair
<>(group
.getGroupId(), result
.second());
1127 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1128 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1129 ) throws IOException
{
1130 final var selfRecipientId
= account
.getSelfRecipientId();
1131 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1132 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1133 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1135 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1136 account
.getGroupStore().updateGroup(group
);
1137 return sendMessage(messageBuilder
, members
);
1140 private static int currentTimeDays() {
1141 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1144 private GroupsV2AuthorizationString
getGroupAuthForToday(
1145 final GroupSecretParams groupSecretParams
1146 ) throws IOException
{
1147 final var today
= currentTimeDays();
1148 // Returns credentials for the next 7 days
1149 final var credentials
= groupsV2Api
.getCredentials(today
);
1150 // TODO cache credentials until they expire
1151 var authCredentialResponse
= credentials
.get(today
);
1153 return groupsV2Api
.getGroupsV2AuthorizationString(account
.getUuid(),
1156 authCredentialResponse
);
1157 } catch (VerificationFailedException e
) {
1158 throw new IOException(e
);
1162 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1163 GroupIdV1 groupId
, SignalServiceAddress recipient
1164 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1166 var group
= getGroupForSending(groupId
);
1167 if (!(group
instanceof GroupInfoV1
)) {
1168 throw new RuntimeException("Received an invalid group request for a v2 group!");
1170 g
= (GroupInfoV1
) group
;
1172 final var recipientId
= resolveRecipient(recipient
);
1173 if (!g
.isMember(recipientId
)) {
1174 throw new NotAGroupMemberException(groupId
, g
.name
);
1177 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1179 // Send group message only to the recipient who requested it
1180 return sendMessage(messageBuilder
, Set
.of(recipientId
));
1183 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1184 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1185 .withId(g
.getGroupId().serialize())
1187 .withMembers(g
.getMembers()
1189 .map(this::resolveSignalServiceAddress
)
1190 .collect(Collectors
.toList()));
1193 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1194 if (attachment
.isPresent()) {
1195 group
.withAvatar(attachment
.get());
1197 } catch (IOException e
) {
1198 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1201 return SignalServiceDataMessage
.newBuilder()
1202 .asGroupMessage(group
.build())
1203 .withExpiration(g
.getMessageExpirationTime());
1206 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1207 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1208 .withRevision(g
.getGroup().getRevision())
1209 .withSignedGroupChange(signedGroupChange
);
1210 return SignalServiceDataMessage
.newBuilder()
1211 .asGroupMessage(group
.build())
1212 .withExpiration(g
.getMessageExpirationTime());
1215 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1216 GroupIdV1 groupId
, SignalServiceAddress recipient
1217 ) throws IOException
{
1218 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1220 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1222 // Send group info request message to the recipient who sent us a message with this groupId
1223 return sendMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1227 SignalServiceAddress remoteAddress
, long messageId
1228 ) throws IOException
, UntrustedIdentityException
{
1229 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1231 System
.currentTimeMillis());
1233 createMessageSender().sendReceipt(remoteAddress
,
1234 unidentifiedAccessHelper
.getAccessFor(resolveRecipient(remoteAddress
)),
1238 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1239 String messageText
, List
<String
> attachments
, List
<String
> recipients
1240 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1241 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1242 if (attachments
!= null) {
1243 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
1245 // Upload attachments here, so we only upload once even for multiple recipients
1246 var messageSender
= createMessageSender();
1247 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1248 for (var attachment
: attachmentStreams
) {
1249 if (attachment
.isStream()) {
1250 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1251 } else if (attachment
.isPointer()) {
1252 attachmentPointers
.add(attachment
.asPointer());
1256 messageBuilder
.withAttachments(attachmentPointers
);
1258 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1261 public Pair
<Long
, SendMessageResult
> sendSelfMessage(
1262 String messageText
, List
<String
> attachments
1263 ) throws IOException
, AttachmentInvalidException
{
1264 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
1265 if (attachments
!= null) {
1266 messageBuilder
.withAttachments(AttachmentUtils
.getSignalServiceAttachments(attachments
));
1268 return sendSelfMessage(messageBuilder
);
1271 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1272 long targetSentTimestamp
, List
<String
> recipients
1273 ) throws IOException
, InvalidNumberException
{
1274 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1275 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1276 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1279 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1280 long targetSentTimestamp
, GroupId groupId
1281 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1282 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1283 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1284 return sendGroupMessage(messageBuilder
, groupId
);
1287 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1288 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1289 ) throws IOException
, InvalidNumberException
{
1290 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1291 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1293 resolveSignalServiceAddress(targetAuthorRecipientId
),
1294 targetSentTimestamp
);
1295 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1296 return sendMessage(messageBuilder
, getSignalServiceAddresses(recipients
));
1299 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1300 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1302 final var signalServiceAddresses
= getSignalServiceAddresses(recipients
);
1304 return sendMessage(messageBuilder
, signalServiceAddresses
);
1305 } catch (Exception e
) {
1306 for (var address
: signalServiceAddresses
) {
1307 handleEndSession(address
);
1313 void renewSession(RecipientId recipientId
) throws IOException
{
1314 account
.getSessionStore().archiveSessions(recipientId
);
1315 if (!recipientId
.equals(getSelfRecipientId())) {
1316 sendNullMessage(recipientId
);
1320 public String
getContactName(String number
) throws InvalidNumberException
{
1321 var contact
= account
.getContactStore().getContact(canonicalizeAndResolveRecipient(number
));
1322 return contact
== null || contact
.getName() == null ?
"" : contact
.getName();
1325 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1326 if (!account
.isMasterDevice()) {
1327 throw new NotMasterDeviceException();
1329 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1330 var contact
= account
.getContactStore().getContact(recipientId
);
1331 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1332 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1335 public void setContactBlocked(
1336 String number
, boolean blocked
1337 ) throws InvalidNumberException
, NotMasterDeviceException
{
1338 if (!account
.isMasterDevice()) {
1339 throw new NotMasterDeviceException();
1341 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1344 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1345 var contact
= account
.getContactStore().getContact(recipientId
);
1346 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1347 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1350 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1351 var group
= getGroup(groupId
);
1352 if (group
== null) {
1353 throw new GroupNotFoundException(groupId
);
1356 group
.setBlocked(blocked
);
1357 account
.getGroupStore().updateGroup(group
);
1360 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1361 var contact
= account
.getContactStore().getContact(recipientId
);
1362 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1365 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1366 account
.getContactStore()
1367 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1370 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1371 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1372 sendMessage(messageBuilder
, Set
.of(recipientId
));
1376 * Change the expiration timer for a contact
1378 public void setExpirationTimer(
1379 String number
, int messageExpirationTimer
1380 ) throws IOException
, InvalidNumberException
{
1381 var recipientId
= canonicalizeAndResolveRecipient(number
);
1382 setExpirationTimer(recipientId
, messageExpirationTimer
);
1383 sendExpirationTimerUpdate(recipientId
);
1387 * Change the expiration timer for a group
1389 private void setExpirationTimer(
1390 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1391 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1392 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1393 account
.getGroupStore().updateGroup(groupInfoV1
);
1394 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1397 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1398 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1399 sendGroupMessage(messageBuilder
, groupId
);
1403 * Upload the sticker pack from path.
1405 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1406 * @return if successful, returns the URL to install the sticker pack in the signal app
1408 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1409 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1411 var messageSender
= createMessageSender();
1413 var packKey
= KeyUtils
.createStickerUploadKey();
1414 var packId
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1416 var sticker
= new Sticker(StickerPackId
.deserialize(Hex
.fromStringCondensed(packId
)), packKey
);
1417 account
.getStickerStore().updateSticker(sticker
);
1420 return new URI("https",
1423 "pack_id=" + URLEncoder
.encode(packId
, StandardCharsets
.UTF_8
) + "&pack_key=" + URLEncoder
.encode(
1424 Hex
.toStringCondensed(packKey
),
1425 StandardCharsets
.UTF_8
)).toString();
1426 } catch (URISyntaxException e
) {
1427 throw new AssertionError(e
);
1431 public void requestAllSyncData() throws IOException
{
1432 requestSyncGroups();
1433 requestSyncContacts();
1434 requestSyncBlocked();
1435 requestSyncConfiguration();
1439 private void requestSyncGroups() throws IOException
{
1440 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1441 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1443 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1445 sendSyncMessage(message
);
1446 } catch (UntrustedIdentityException e
) {
1447 throw new AssertionError(e
);
1451 private void requestSyncContacts() throws IOException
{
1452 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1453 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1455 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1457 sendSyncMessage(message
);
1458 } catch (UntrustedIdentityException e
) {
1459 throw new AssertionError(e
);
1463 private void requestSyncBlocked() throws IOException
{
1464 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1465 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1467 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1469 sendSyncMessage(message
);
1470 } catch (UntrustedIdentityException e
) {
1471 throw new AssertionError(e
);
1475 private void requestSyncConfiguration() throws IOException
{
1476 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1477 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1479 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1481 sendSyncMessage(message
);
1482 } catch (UntrustedIdentityException e
) {
1483 throw new AssertionError(e
);
1487 private void requestSyncKeys() throws IOException
{
1488 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1489 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1491 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1493 sendSyncMessage(message
);
1494 } catch (UntrustedIdentityException e
) {
1495 throw new AssertionError(e
);
1499 private byte[] getSenderCertificate() {
1502 if (account
.isPhoneNumberShared()) {
1503 certificate
= accountManager
.getSenderCertificate();
1505 certificate
= accountManager
.getSenderCertificateForPhoneNumberPrivacy();
1507 } catch (IOException e
) {
1508 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1511 // TODO cache for a day
1515 private void sendSyncMessage(SignalServiceSyncMessage message
) throws IOException
, UntrustedIdentityException
{
1516 var messageSender
= createMessageSender();
1517 messageSender
.sendMessage(message
, unidentifiedAccessHelper
.getAccessForSync());
1520 private Set
<RecipientId
> getSignalServiceAddresses(Collection
<String
> numbers
) throws InvalidNumberException
{
1521 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1522 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1524 for (var number
: numbers
) {
1525 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1526 if (resolvedAddress
.getUuid().isPresent()) {
1527 signalServiceAddresses
.add(resolvedAddress
);
1529 addressesMissingUuid
.add(resolvedAddress
);
1533 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1534 .map(a
-> a
.getNumber().get())
1535 .collect(Collectors
.toSet());
1536 Map
<String
, UUID
> registeredUsers
;
1538 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1539 } catch (IOException e
) {
1540 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1541 registeredUsers
= Map
.of();
1544 for (var address
: addressesMissingUuid
) {
1545 final var number
= address
.getNumber().get();
1546 if (registeredUsers
.containsKey(number
)) {
1547 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1548 registeredUsers
.get(number
),
1550 signalServiceAddresses
.add(newAddress
);
1552 signalServiceAddresses
.add(address
);
1556 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1559 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1560 final var address
= resolveSignalServiceAddress(recipientId
);
1561 if (!address
.getNumber().isPresent()) {
1564 final var number
= address
.getNumber().get();
1565 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1566 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1569 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1571 return accountManager
.getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1573 serviceEnvironmentConfig
.getCdsMrenclave());
1574 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1575 throw new IOException(e
);
1579 private Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1580 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientId
> recipientIds
1581 ) throws IOException
{
1582 final var timestamp
= System
.currentTimeMillis();
1583 messageBuilder
.withTimestamp(timestamp
);
1584 getOrCreateMessagePipe();
1585 getOrCreateUnidentifiedMessagePipe();
1586 SignalServiceDataMessage message
= null;
1588 message
= messageBuilder
.build();
1589 if (message
.getGroupContext().isPresent()) {
1591 var messageSender
= createMessageSender();
1592 final var isRecipientUpdate
= false;
1593 final var recipientIdList
= new ArrayList
<>(recipientIds
);
1594 final var addresses
= recipientIdList
.stream()
1595 .map(this::resolveSignalServiceAddress
)
1596 .collect(Collectors
.toList());
1597 var result
= messageSender
.sendMessage(addresses
,
1598 unidentifiedAccessHelper
.getAccessFor(recipientIdList
),
1602 for (var r
: result
) {
1603 if (r
.getIdentityFailure() != null) {
1604 final var recipientId
= resolveRecipient(r
.getAddress());
1605 final var newIdentity
= account
.getIdentityKeyStore()
1606 .saveIdentity(recipientId
, r
.getIdentityFailure().getIdentityKey(), new Date());
1608 account
.getSessionStore().archiveSessions(recipientId
);
1613 return new Pair
<>(timestamp
, result
);
1614 } catch (UntrustedIdentityException e
) {
1615 return new Pair
<>(timestamp
, List
.of());
1618 // Send to all individually, so sync messages are sent correctly
1619 messageBuilder
.withProfileKey(account
.getProfileKey().serialize());
1620 var results
= new ArrayList
<SendMessageResult
>(recipientIds
.size());
1621 for (var recipientId
: recipientIds
) {
1622 final var contact
= account
.getContactStore().getContact(recipientId
);
1623 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1624 messageBuilder
.withExpiration(expirationTime
);
1625 message
= messageBuilder
.build();
1626 results
.add(sendMessage(recipientId
, message
));
1628 return new Pair
<>(timestamp
, results
);
1631 if (message
!= null && message
.isEndSession()) {
1632 for (var recipient
: recipientIds
) {
1633 handleEndSession(recipient
);
1639 private Pair
<Long
, SendMessageResult
> sendSelfMessage(
1640 SignalServiceDataMessage
.Builder messageBuilder
1641 ) throws IOException
{
1642 final var timestamp
= System
.currentTimeMillis();
1643 messageBuilder
.withTimestamp(timestamp
);
1644 getOrCreateMessagePipe();
1645 getOrCreateUnidentifiedMessagePipe();
1646 final var recipientId
= account
.getSelfRecipientId();
1648 final var contact
= account
.getContactStore().getContact(recipientId
);
1649 final var expirationTime
= contact
!= null ? contact
.getMessageExpirationTime() : 0;
1650 messageBuilder
.withExpiration(expirationTime
);
1652 var message
= messageBuilder
.build();
1653 final var result
= sendSelfMessage(message
);
1654 return new Pair
<>(timestamp
, result
);
1657 private SendMessageResult
sendSelfMessage(SignalServiceDataMessage message
) throws IOException
{
1658 var messageSender
= createMessageSender();
1660 var recipientId
= account
.getSelfRecipientId();
1662 final var unidentifiedAccess
= unidentifiedAccessHelper
.getAccessFor(recipientId
);
1663 var recipient
= resolveSignalServiceAddress(recipientId
);
1664 var transcript
= new SentTranscriptMessage(Optional
.of(recipient
),
1665 message
.getTimestamp(),
1667 message
.getExpiresInSeconds(),
1668 Map
.of(recipient
, unidentifiedAccess
.isPresent()),
1670 var syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
1673 var startTime
= System
.currentTimeMillis();
1674 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
1675 return SendMessageResult
.success(recipient
,
1676 unidentifiedAccess
.isPresent(),
1678 System
.currentTimeMillis() - startTime
);
1679 } catch (UntrustedIdentityException e
) {
1680 return SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey());
1684 private SendMessageResult
sendMessage(
1685 RecipientId recipientId
, SignalServiceDataMessage message
1686 ) throws IOException
{
1687 var messageSender
= createMessageSender();
1689 final var address
= resolveSignalServiceAddress(recipientId
);
1692 return messageSender
.sendMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
), message
);
1693 } catch (UnregisteredUserException e
) {
1694 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1695 return messageSender
.sendMessage(resolveSignalServiceAddress(newRecipientId
),
1696 unidentifiedAccessHelper
.getAccessFor(newRecipientId
),
1699 } catch (UntrustedIdentityException e
) {
1700 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1704 private SendMessageResult
sendNullMessage(RecipientId recipientId
) throws IOException
{
1705 var messageSender
= createMessageSender();
1707 final var address
= resolveSignalServiceAddress(recipientId
);
1710 return messageSender
.sendNullMessage(address
, unidentifiedAccessHelper
.getAccessFor(recipientId
));
1711 } catch (UnregisteredUserException e
) {
1712 final var newRecipientId
= refreshRegisteredUser(recipientId
);
1713 final var newAddress
= resolveSignalServiceAddress(newRecipientId
);
1714 return messageSender
.sendNullMessage(newAddress
, unidentifiedAccessHelper
.getAccessFor(newRecipientId
));
1716 } catch (UntrustedIdentityException e
) {
1717 return SendMessageResult
.identityFailure(address
, e
.getIdentityKey());
1721 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
{
1722 var cipher
= new SignalServiceCipher(account
.getSelfAddress(),
1723 account
.getSignalProtocolStore(),
1725 certificateValidator
);
1726 return cipher
.decrypt(envelope
);
1729 private void handleEndSession(RecipientId recipientId
) {
1730 account
.getSessionStore().deleteAllSessions(recipientId
);
1733 private List
<HandleAction
> handleSignalServiceDataMessage(
1734 SignalServiceDataMessage message
,
1736 SignalServiceAddress source
,
1737 SignalServiceAddress destination
,
1738 boolean ignoreAttachments
1740 var actions
= new ArrayList
<HandleAction
>();
1741 if (message
.getGroupContext().isPresent()) {
1742 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1743 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1744 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1745 var group
= getGroup(groupId
);
1746 if (group
== null || group
instanceof GroupInfoV1
) {
1747 var groupV1
= (GroupInfoV1
) group
;
1748 switch (groupInfo
.getType()) {
1750 if (groupV1
== null) {
1751 groupV1
= new GroupInfoV1(groupId
);
1754 if (groupInfo
.getAvatar().isPresent()) {
1755 var avatar
= groupInfo
.getAvatar().get();
1756 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1759 if (groupInfo
.getName().isPresent()) {
1760 groupV1
.name
= groupInfo
.getName().get();
1763 if (groupInfo
.getMembers().isPresent()) {
1764 groupV1
.addMembers(groupInfo
.getMembers()
1767 .map(this::resolveRecipient
)
1768 .collect(Collectors
.toSet()));
1771 account
.getGroupStore().updateGroup(groupV1
);
1775 if (groupV1
== null && !isSync
) {
1776 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1780 if (groupV1
!= null) {
1781 groupV1
.removeMember(resolveRecipient(source
));
1782 account
.getGroupStore().updateGroup(groupV1
);
1787 if (groupV1
!= null && !isSync
) {
1788 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1793 // Received a group v1 message for a v2 group
1796 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1797 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1798 final var groupMasterKey
= groupContext
.getMasterKey();
1800 getOrMigrateGroup(groupMasterKey
,
1801 groupContext
.getRevision(),
1802 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1806 final var conversationPartnerAddress
= isSync ? destination
: source
;
1807 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1808 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1810 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1811 if (message
.getGroupContext().isPresent()) {
1812 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1813 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1814 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1815 if (group
!= null) {
1816 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1817 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1818 account
.getGroupStore().updateGroup(group
);
1821 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1822 // disappearing message timer already stored in the DecryptedGroup
1824 } else if (conversationPartnerAddress
!= null) {
1825 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1828 if (!ignoreAttachments
) {
1829 if (message
.getAttachments().isPresent()) {
1830 for (var attachment
: message
.getAttachments().get()) {
1831 downloadAttachment(attachment
);
1834 if (message
.getSharedContacts().isPresent()) {
1835 for (var contact
: message
.getSharedContacts().get()) {
1836 if (contact
.getAvatar().isPresent()) {
1837 downloadAttachment(contact
.getAvatar().get().getAttachment());
1842 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1843 final ProfileKey profileKey
;
1845 profileKey
= new ProfileKey(message
.getProfileKey().get());
1846 } catch (InvalidInputException e
) {
1847 throw new AssertionError(e
);
1849 if (source
.matches(account
.getSelfAddress())) {
1850 this.account
.setProfileKey(profileKey
);
1852 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1854 if (message
.getPreviews().isPresent()) {
1855 final var previews
= message
.getPreviews().get();
1856 for (var preview
: previews
) {
1857 if (preview
.getImage().isPresent()) {
1858 downloadAttachment(preview
.getImage().get());
1862 if (message
.getQuote().isPresent()) {
1863 final var quote
= message
.getQuote().get();
1865 for (var quotedAttachment
: quote
.getAttachments()) {
1866 final var thumbnail
= quotedAttachment
.getThumbnail();
1867 if (thumbnail
!= null) {
1868 downloadAttachment(thumbnail
);
1872 if (message
.getSticker().isPresent()) {
1873 final var messageSticker
= message
.getSticker().get();
1874 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1875 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1876 if (sticker
== null) {
1877 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1878 account
.getStickerStore().updateSticker(sticker
);
1884 private GroupInfoV2
getOrMigrateGroup(
1885 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1887 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1889 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1890 var groupInfo
= getGroup(groupId
);
1891 final GroupInfoV2 groupInfoV2
;
1892 if (groupInfo
instanceof GroupInfoV1
) {
1893 // Received a v2 group message for a v1 group, we need to locally migrate the group
1894 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1895 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1896 logger
.info("Locally migrated group {} to group v2, id: {}",
1897 groupInfo
.getGroupId().toBase64(),
1898 groupInfoV2
.getGroupId().toBase64());
1899 } else if (groupInfo
instanceof GroupInfoV2
) {
1900 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1902 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1905 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1906 DecryptedGroup group
= null;
1907 if (signedGroupChange
!= null
1908 && groupInfoV2
.getGroup() != null
1909 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1910 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1914 if (group
== null) {
1915 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1917 if (group
!= null) {
1918 storeProfileKeysFromMembers(group
);
1919 final var avatar
= group
.getAvatar();
1920 if (avatar
!= null && !avatar
.isEmpty()) {
1921 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1924 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1925 account
.getGroupStore().updateGroup(groupInfoV2
);
1931 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1932 for (var member
: group
.getMembersList()) {
1933 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1934 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1936 account
.getProfileStore()
1937 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1938 } catch (InvalidInputException ignored
) {
1943 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1944 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1945 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1946 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1947 if (actions
!= null) {
1948 queuedActions
.addAll(actions
);
1951 for (var action
: queuedActions
) {
1953 action
.execute(this);
1954 } catch (Throwable e
) {
1955 logger
.warn("Message action failed.", e
);
1960 private List
<HandleAction
> retryFailedReceivedMessage(
1961 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1963 var envelope
= cachedMessage
.loadEnvelope();
1964 if (envelope
== null) {
1967 SignalServiceContent content
= null;
1968 List
<HandleAction
> actions
= null;
1969 if (!envelope
.isReceipt()) {
1971 content
= decryptMessage(envelope
);
1972 } catch (ProtocolUntrustedIdentityException e
) {
1973 if (!envelope
.hasSource()) {
1974 final var identifier
= e
.getSender();
1975 final var recipientId
= resolveRecipient(identifier
);
1977 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1978 } catch (IOException ioException
) {
1979 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1983 } catch (Exception er
) {
1984 // All other errors are not recoverable, so delete the cached message
1985 cachedMessage
.delete();
1988 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1990 handler
.handleMessage(envelope
, content
, null);
1991 cachedMessage
.delete();
1995 public void receiveMessages(
1998 boolean returnOnTimeout
,
1999 boolean ignoreAttachments
,
2000 ReceiveMessageHandler handler
2001 ) throws IOException
{
2002 retryFailedReceivedMessages(handler
, ignoreAttachments
);
2004 Set
<HandleAction
> queuedActions
= null;
2006 final var messagePipe
= getOrCreateMessagePipe();
2008 var hasCaughtUpWithOldMessages
= false;
2011 SignalServiceEnvelope envelope
;
2012 SignalServiceContent content
= null;
2013 Exception exception
= null;
2014 final CachedMessage
[] cachedMessage
= {null};
2015 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
2017 var result
= messagePipe
.readOrEmpty(timeout
, unit
, envelope1
-> {
2018 final var recipientId
= envelope1
.hasSource()
2019 ?
resolveRecipient(envelope1
.getSourceIdentifier())
2021 // store message on disk, before acknowledging receipt to the server
2022 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
2024 if (result
.isPresent()) {
2025 envelope
= result
.get();
2027 // Received indicator that server queue is empty
2028 hasCaughtUpWithOldMessages
= true;
2030 if (queuedActions
!= null) {
2031 for (var action
: queuedActions
) {
2033 action
.execute(this);
2034 } catch (Throwable e
) {
2035 logger
.warn("Message action failed.", e
);
2038 queuedActions
.clear();
2039 queuedActions
= null;
2042 // Continue to wait another timeout for new messages
2045 } catch (TimeoutException e
) {
2046 if (returnOnTimeout
) return;
2050 if (envelope
.hasSource()) {
2051 // Store uuid if we don't have it already
2052 // address/uuid in envelope is sent by server
2053 resolveRecipientTrusted(envelope
.getSourceAddress());
2055 final var notAGroupMember
= isNotAGroupMember(envelope
, content
);
2056 if (!envelope
.isReceipt()) {
2058 content
= decryptMessage(envelope
);
2059 } catch (Exception e
) {
2062 if (!envelope
.hasSource() && content
!= null) {
2063 // Store uuid if we don't have it already
2064 // address/uuid is validated by unidentified sender certificate
2065 resolveRecipientTrusted(content
.getSender());
2067 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
2068 if (exception
instanceof ProtocolInvalidMessageException
) {
2069 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
2070 logger
.debug("Received invalid message, queuing renew session action.");
2071 actions
.add(new RenewSessionAction(sender
));
2073 if (hasCaughtUpWithOldMessages
) {
2074 for (var action
: actions
) {
2076 action
.execute(this);
2077 } catch (Throwable e
) {
2078 logger
.warn("Message action failed.", e
);
2082 if (queuedActions
== null) {
2083 queuedActions
= new HashSet
<>();
2085 queuedActions
.addAll(actions
);
2088 if (isMessageBlocked(envelope
, content
)) {
2089 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
2090 } else if (notAGroupMember
) {
2091 logger
.info("Ignoring a message from a non group member: {}", envelope
.getTimestamp());
2093 handler
.handleMessage(envelope
, content
, exception
);
2095 if (cachedMessage
[0] != null) {
2096 if (exception
instanceof ProtocolUntrustedIdentityException
) {
2097 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
2098 final var recipientId
= resolveRecipient(identifier
);
2099 queuedActions
.add(new RetrieveProfileAction(recipientId
));
2100 if (!envelope
.hasSource()) {
2102 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
2103 } catch (IOException ioException
) {
2104 logger
.warn("Failed to move cached message to recipient folder: {}",
2105 ioException
.getMessage());
2109 cachedMessage
[0].delete();
2115 private boolean isMessageBlocked(
2116 SignalServiceEnvelope envelope
, SignalServiceContent content
2118 SignalServiceAddress source
;
2119 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2120 source
= envelope
.getSourceAddress();
2121 } else if (content
!= null) {
2122 source
= content
.getSender();
2126 final var recipientId
= resolveRecipient(source
);
2127 if (isContactBlocked(recipientId
)) {
2131 if (content
!= null && content
.getDataMessage().isPresent()) {
2132 var message
= content
.getDataMessage().get();
2133 if (message
.getGroupContext().isPresent()) {
2134 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2135 var group
= getGroup(groupId
);
2136 if (group
!= null && group
.isBlocked()) {
2144 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2145 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2146 return isContactBlocked(recipientId
);
2149 private boolean isContactBlocked(final RecipientId recipientId
) {
2150 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2151 return sourceContact
!= null && sourceContact
.isBlocked();
2154 private boolean isNotAGroupMember(
2155 SignalServiceEnvelope envelope
, SignalServiceContent content
2157 SignalServiceAddress source
;
2158 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2159 source
= envelope
.getSourceAddress();
2160 } else if (content
!= null) {
2161 source
= content
.getSender();
2166 if (content
!= null && content
.getDataMessage().isPresent()) {
2167 var message
= content
.getDataMessage().get();
2168 if (message
.getGroupContext().isPresent()) {
2169 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2170 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2171 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2175 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2176 var group
= getGroup(groupId
);
2177 if (group
!= null && !group
.isMember(resolveRecipient(source
))) {
2185 private List
<HandleAction
> handleMessage(
2186 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2188 var actions
= new ArrayList
<HandleAction
>();
2189 if (content
!= null) {
2190 final SignalServiceAddress sender
;
2191 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2192 sender
= envelope
.getSourceAddress();
2194 sender
= content
.getSender();
2197 if (content
.getDataMessage().isPresent()) {
2198 var message
= content
.getDataMessage().get();
2200 if (content
.isNeedsReceipt()) {
2201 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2204 actions
.addAll(handleSignalServiceDataMessage(message
,
2207 account
.getSelfAddress(),
2208 ignoreAttachments
));
2210 if (content
.getSyncMessage().isPresent()) {
2211 account
.setMultiDevice(true);
2212 var syncMessage
= content
.getSyncMessage().get();
2213 if (syncMessage
.getSent().isPresent()) {
2214 var message
= syncMessage
.getSent().get();
2215 final var destination
= message
.getDestination().orNull();
2216 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2220 ignoreAttachments
));
2222 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2223 var rm
= syncMessage
.getRequest().get();
2224 if (rm
.isContactsRequest()) {
2225 actions
.add(SendSyncContactsAction
.create());
2227 if (rm
.isGroupsRequest()) {
2228 actions
.add(SendSyncGroupsAction
.create());
2230 if (rm
.isBlockedListRequest()) {
2231 actions
.add(SendSyncBlockedListAction
.create());
2233 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2235 if (syncMessage
.getGroups().isPresent()) {
2236 File tmpFile
= null;
2238 tmpFile
= IOUtils
.createTempFile();
2239 final var groupsMessage
= syncMessage
.getGroups().get();
2240 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2241 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2246 } catch (IOException e
) {
2247 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2253 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2254 if (syncGroup
!= null) {
2255 if (g
.getName().isPresent()) {
2256 syncGroup
.name
= g
.getName().get();
2258 syncGroup
.addMembers(g
.getMembers()
2260 .map(this::resolveRecipient
)
2261 .collect(Collectors
.toSet()));
2262 if (!g
.isActive()) {
2263 syncGroup
.removeMember(account
.getSelfRecipientId());
2265 // Add ourself to the member set as it's marked as active
2266 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2268 syncGroup
.blocked
= g
.isBlocked();
2269 if (g
.getColor().isPresent()) {
2270 syncGroup
.color
= g
.getColor().get();
2273 if (g
.getAvatar().isPresent()) {
2274 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2276 syncGroup
.archived
= g
.isArchived();
2277 account
.getGroupStore().updateGroup(syncGroup
);
2281 } catch (Exception e
) {
2282 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2286 if (tmpFile
!= null) {
2288 Files
.delete(tmpFile
.toPath());
2289 } catch (IOException e
) {
2290 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2297 if (syncMessage
.getBlockedList().isPresent()) {
2298 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2299 for (var address
: blockedListMessage
.getAddresses()) {
2300 setContactBlocked(resolveRecipient(address
), true);
2302 for (var groupId
: blockedListMessage
.getGroupIds()
2304 .map(GroupId
::unknownVersion
)
2305 .collect(Collectors
.toSet())) {
2307 setGroupBlocked(groupId
, true);
2308 } catch (GroupNotFoundException e
) {
2309 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2310 groupId
.toBase64());
2314 if (syncMessage
.getContacts().isPresent()) {
2315 File tmpFile
= null;
2317 tmpFile
= IOUtils
.createTempFile();
2318 final var contactsMessage
= syncMessage
.getContacts().get();
2319 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2320 .asPointer(), tmpFile
)) {
2321 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2326 } catch (IOException e
) {
2327 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2334 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2335 account
.setProfileKey(c
.getProfileKey().get());
2337 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2338 var contact
= account
.getContactStore().getContact(recipientId
);
2339 final var builder
= contact
== null
2340 ? Contact
.newBuilder()
2341 : Contact
.newBuilder(contact
);
2342 if (c
.getName().isPresent()) {
2343 builder
.withName(c
.getName().get());
2345 if (c
.getColor().isPresent()) {
2346 builder
.withColor(c
.getColor().get());
2348 if (c
.getProfileKey().isPresent()) {
2349 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2351 if (c
.getVerified().isPresent()) {
2352 final var verifiedMessage
= c
.getVerified().get();
2353 account
.getIdentityKeyStore()
2354 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2355 verifiedMessage
.getIdentityKey(),
2356 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2358 if (c
.getExpirationTimer().isPresent()) {
2359 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2361 builder
.withBlocked(c
.isBlocked());
2362 builder
.withArchived(c
.isArchived());
2363 account
.getContactStore().storeContact(recipientId
, builder
.build());
2365 if (c
.getAvatar().isPresent()) {
2366 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2370 } catch (Exception e
) {
2371 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2375 if (tmpFile
!= null) {
2377 Files
.delete(tmpFile
.toPath());
2378 } catch (IOException e
) {
2379 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2386 if (syncMessage
.getVerified().isPresent()) {
2387 final var verifiedMessage
= syncMessage
.getVerified().get();
2388 account
.getIdentityKeyStore()
2389 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2390 verifiedMessage
.getIdentityKey(),
2391 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2393 if (syncMessage
.getStickerPackOperations().isPresent()) {
2394 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2395 for (var m
: stickerPackOperationMessages
) {
2396 if (!m
.getPackId().isPresent()) {
2399 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2400 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2401 if (sticker
== null) {
2402 if (!m
.getPackKey().isPresent()) {
2405 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2407 sticker
.setInstalled(!m
.getType().isPresent()
2408 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
);
2409 account
.getStickerStore().updateSticker(sticker
);
2412 if (syncMessage
.getFetchType().isPresent()) {
2413 switch (syncMessage
.getFetchType().get()) {
2415 getRecipientProfile(account
.getSelfRecipientId(), true);
2416 case STORAGE_MANIFEST
:
2420 if (syncMessage
.getKeys().isPresent()) {
2421 final var keysMessage
= syncMessage
.getKeys().get();
2422 if (keysMessage
.getStorageService().isPresent()) {
2423 final var storageKey
= keysMessage
.getStorageService().get();
2424 account
.setStorageKey(storageKey
);
2427 if (syncMessage
.getConfiguration().isPresent()) {
2435 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2437 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2438 } catch (IOException e
) {
2439 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2443 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2445 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2446 } catch (IOException e
) {
2447 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2451 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2453 avatarStore
.storeGroupAvatar(groupId
,
2454 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2455 } catch (IOException e
) {
2456 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2460 private void downloadProfileAvatar(
2461 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2464 avatarStore
.storeProfileAvatar(address
,
2465 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2466 } catch (Throwable e
) {
2467 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2471 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2472 return attachmentStore
.getAttachmentFile(attachmentId
);
2475 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2476 if (!attachment
.isPointer()) {
2477 logger
.warn("Invalid state, can't store an attachment stream.");
2480 var pointer
= attachment
.asPointer();
2481 if (pointer
.getPreview().isPresent()) {
2482 final var preview
= pointer
.getPreview().get();
2484 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2485 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2486 } catch (IOException e
) {
2487 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2492 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2493 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2494 } catch (IOException e
) {
2495 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2499 private void retrieveGroupV2Avatar(
2500 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2501 ) throws IOException
{
2502 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
2504 var tmpFile
= IOUtils
.createTempFile();
2505 try (InputStream input
= messageReceiver
.retrieveGroupsV2ProfileAvatar(cdnKey
,
2507 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2508 var encryptedData
= IOUtils
.readFully(input
);
2510 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2511 outputStream
.write(decryptedData
);
2514 Files
.delete(tmpFile
.toPath());
2515 } catch (IOException e
) {
2516 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2523 private void retrieveProfileAvatar(
2524 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2525 ) throws IOException
{
2526 var tmpFile
= IOUtils
.createTempFile();
2527 try (var input
= messageReceiver
.retrieveProfileAvatar(avatarPath
,
2530 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2531 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2532 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2535 Files
.delete(tmpFile
.toPath());
2536 } catch (IOException e
) {
2537 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2544 private void retrieveAttachment(
2545 final SignalServiceAttachment attachment
, final OutputStream outputStream
2546 ) throws IOException
{
2547 if (attachment
.isPointer()) {
2548 var pointer
= attachment
.asPointer();
2549 retrieveAttachmentPointer(pointer
, outputStream
);
2551 var stream
= attachment
.asStream();
2552 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2556 private void retrieveAttachmentPointer(
2557 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2558 ) throws IOException
{
2559 var tmpFile
= IOUtils
.createTempFile();
2560 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2561 IOUtils
.copyStream(input
, outputStream
);
2562 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2563 throw new IOException(e
);
2566 Files
.delete(tmpFile
.toPath());
2567 } catch (IOException e
) {
2568 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2575 private InputStream
retrieveAttachmentAsStream(
2576 SignalServiceAttachmentPointer pointer
, File tmpFile
2577 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2578 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2581 void sendGroups() throws IOException
, UntrustedIdentityException
{
2582 var groupsFile
= IOUtils
.createTempFile();
2585 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2586 var out
= new DeviceGroupsOutputStream(fos
);
2587 for (var record : getGroups()) {
2588 if (record instanceof GroupInfoV1
) {
2589 var groupInfo
= (GroupInfoV1
) record;
2590 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2591 Optional
.fromNullable(groupInfo
.name
),
2592 groupInfo
.getMembers()
2594 .map(this::resolveSignalServiceAddress
)
2595 .collect(Collectors
.toList()),
2596 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2597 groupInfo
.isMember(account
.getSelfRecipientId()),
2598 Optional
.of(groupInfo
.messageExpirationTime
),
2599 Optional
.fromNullable(groupInfo
.color
),
2602 groupInfo
.archived
));
2607 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2608 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2609 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2610 .withStream(groupsFileStream
)
2611 .withContentType("application/octet-stream")
2612 .withLength(groupsFile
.length())
2615 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2620 Files
.delete(groupsFile
.toPath());
2621 } catch (IOException e
) {
2622 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2627 public void sendContacts() throws IOException
, UntrustedIdentityException
{
2628 var contactsFile
= IOUtils
.createTempFile();
2631 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2632 var out
= new DeviceContactsOutputStream(fos
);
2633 for (var contactPair
: account
.getContactStore().getContacts()) {
2634 final var recipientId
= contactPair
.first();
2635 final var contact
= contactPair
.second();
2636 final var address
= resolveSignalServiceAddress(recipientId
);
2638 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2639 VerifiedMessage verifiedMessage
= null;
2640 if (currentIdentity
!= null) {
2641 verifiedMessage
= new VerifiedMessage(address
,
2642 currentIdentity
.getIdentityKey(),
2643 currentIdentity
.getTrustLevel().toVerifiedState(),
2644 currentIdentity
.getDateAdded().getTime());
2647 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2648 out
.write(new DeviceContact(address
,
2649 Optional
.fromNullable(contact
.getName()),
2650 createContactAvatarAttachment(address
),
2651 Optional
.fromNullable(contact
.getColor()),
2652 Optional
.fromNullable(verifiedMessage
),
2653 Optional
.fromNullable(profileKey
),
2654 contact
.isBlocked(),
2655 Optional
.of(contact
.getMessageExpirationTime()),
2657 contact
.isArchived()));
2660 if (account
.getProfileKey() != null) {
2661 // Send our own profile key as well
2662 out
.write(new DeviceContact(account
.getSelfAddress(),
2667 Optional
.of(account
.getProfileKey()),
2675 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2676 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2677 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2678 .withStream(contactsFileStream
)
2679 .withContentType("application/octet-stream")
2680 .withLength(contactsFile
.length())
2683 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
2688 Files
.delete(contactsFile
.toPath());
2689 } catch (IOException e
) {
2690 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2695 void sendBlockedList() throws IOException
, UntrustedIdentityException
{
2696 var addresses
= new ArrayList
<SignalServiceAddress
>();
2697 for (var record : account
.getContactStore().getContacts()) {
2698 if (record.second().isBlocked()) {
2699 addresses
.add(resolveSignalServiceAddress(record.first()));
2702 var groupIds
= new ArrayList
<byte[]>();
2703 for (var record : getGroups()) {
2704 if (record.isBlocked()) {
2705 groupIds
.add(record.getGroupId().serialize());
2708 sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2711 private void sendVerifiedMessage(
2712 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2713 ) throws IOException
, UntrustedIdentityException
{
2714 var verifiedMessage
= new VerifiedMessage(destination
,
2716 trustLevel
.toVerifiedState(),
2717 System
.currentTimeMillis());
2718 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2721 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2722 return account
.getContactStore().getContacts();
2725 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2726 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2727 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2728 if (recipient
== null) {
2732 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2733 return recipient
.getContact().getName();
2736 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2737 return recipient
.getProfile().getDisplayName();
2743 public GroupInfo
getGroup(GroupId groupId
) {
2744 return getGroup(groupId
, false);
2747 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2748 final var group
= account
.getGroupStore().getGroup(groupId
);
2749 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2750 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2751 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2752 account
.getGroupStore().updateGroup(group
);
2757 public List
<IdentityInfo
> getIdentities() {
2758 return account
.getIdentityKeyStore().getIdentities();
2761 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2762 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2763 return identity
== null ? List
.of() : List
.of(identity
);
2767 * Trust this the identity with this fingerprint
2769 * @param name username of the identity
2770 * @param fingerprint Fingerprint
2772 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2773 var recipientId
= canonicalizeAndResolveRecipient(name
);
2774 return trustIdentity(recipientId
,
2775 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2776 TrustLevel
.TRUSTED_VERIFIED
);
2780 * Trust this the identity with this safety number
2782 * @param name username of the identity
2783 * @param safetyNumber Safety number
2785 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2786 var recipientId
= canonicalizeAndResolveRecipient(name
);
2787 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2788 return trustIdentity(recipientId
,
2789 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2790 TrustLevel
.TRUSTED_VERIFIED
);
2794 * Trust all keys of this identity without verification
2796 * @param name username of the identity
2798 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2799 var recipientId
= canonicalizeAndResolveRecipient(name
);
2800 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2803 private boolean trustIdentity(
2804 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2806 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2807 if (identity
== null) {
2811 if (!verifier
.apply(identity
.getIdentityKey())) {
2815 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2817 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2818 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2819 } catch (IOException
| UntrustedIdentityException e
) {
2820 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2826 public String
computeSafetyNumber(
2827 SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
2829 return Utils
.computeSafetyNumber(ServiceConfig
.capabilities
.isUuid(),
2830 account
.getSelfAddress(),
2831 getIdentityKeyPair().getPublicKey(),
2837 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2838 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2840 return resolveSignalServiceAddress(address
);
2844 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2845 if (address
.matches(account
.getSelfAddress())) {
2846 return account
.getSelfAddress();
2849 return account
.getRecipientStore().resolveServiceAddress(address
);
2852 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2853 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2856 public RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2857 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
)
2859 : PhoneNumberFormatter
.formatNumber(identifier
, account
.getUsername());
2861 return resolveRecipient(canonicalizedNumber
);
2864 private RecipientId
resolveRecipient(final String identifier
) {
2865 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2867 return resolveRecipient(address
);
2870 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2871 return account
.getRecipientStore().resolveRecipient(address
);
2874 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2875 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2879 public void close() throws IOException
{
2883 void close(boolean closeAccount
) throws IOException
{
2884 executor
.shutdown();
2886 if (messagePipe
!= null) {
2887 messagePipe
.shutdown();
2891 if (unidentifiedMessagePipe
!= null) {
2892 unidentifiedMessagePipe
.shutdown();
2893 unidentifiedMessagePipe
= null;
2896 if (closeAccount
&& account
!= null) {
2902 public interface ReceiveMessageHandler
{
2904 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);