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
.api
.Message
;
21 import org
.asamk
.signal
.manager
.api
.TypingAction
;
22 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
23 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
24 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
25 import org
.asamk
.signal
.manager
.groups
.GroupId
;
26 import org
.asamk
.signal
.manager
.groups
.GroupIdV1
;
27 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
28 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
29 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
30 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
31 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
32 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
33 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
34 import org
.asamk
.signal
.manager
.helper
.GroupV2Helper
;
35 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
36 import org
.asamk
.signal
.manager
.helper
.ProfileHelper
;
37 import org
.asamk
.signal
.manager
.helper
.SendHelper
;
38 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
39 import org
.asamk
.signal
.manager
.jobs
.Context
;
40 import org
.asamk
.signal
.manager
.jobs
.Job
;
41 import org
.asamk
.signal
.manager
.jobs
.RetrieveStickerPackJob
;
42 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
43 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
44 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
45 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
46 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
47 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
48 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
49 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
50 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
51 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
52 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
53 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
54 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
55 import org
.asamk
.signal
.manager
.util
.IOUtils
;
56 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
57 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
58 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
59 import org
.asamk
.signal
.manager
.util
.Utils
;
60 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
61 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
62 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
63 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
64 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
65 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
66 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
67 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
68 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
69 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
70 import org
.signal
.libsignal
.metadata
.SelfSendException
;
71 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
72 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
73 import org
.signal
.zkgroup
.InvalidInputException
;
74 import org
.signal
.zkgroup
.VerificationFailedException
;
75 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
76 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
77 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
78 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
79 import org
.slf4j
.Logger
;
80 import org
.slf4j
.LoggerFactory
;
81 import org
.whispersystems
.libsignal
.IdentityKey
;
82 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
83 import org
.whispersystems
.libsignal
.InvalidKeyException
;
84 import org
.whispersystems
.libsignal
.InvalidMessageException
;
85 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
86 import org
.whispersystems
.libsignal
.fingerprint
.Fingerprint
;
87 import org
.whispersystems
.libsignal
.fingerprint
.FingerprintParsingException
;
88 import org
.whispersystems
.libsignal
.fingerprint
.FingerprintVersionMismatchException
;
89 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
90 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
91 import org
.whispersystems
.libsignal
.util
.Pair
;
92 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
93 import org
.whispersystems
.signalservice
.api
.InvalidMessageStructureException
;
94 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
95 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
96 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
97 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
98 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
99 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
100 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
101 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
102 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
103 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
104 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
105 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
106 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
107 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupV2
;
108 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
109 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTypingMessage
;
110 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
111 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
112 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
113 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
114 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
117 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
118 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
119 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
120 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
121 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
122 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
123 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
124 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
125 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.ConflictException
;
126 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
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
.UuidUtil
;
131 import org
.whispersystems
.signalservice
.api
.websocket
.WebSocketUnavailableException
;
132 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
133 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
134 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
135 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
136 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
137 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
138 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
139 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
141 import java
.io
.Closeable
;
143 import java
.io
.FileInputStream
;
144 import java
.io
.FileOutputStream
;
145 import java
.io
.IOException
;
146 import java
.io
.InputStream
;
147 import java
.io
.OutputStream
;
149 import java
.net
.URISyntaxException
;
150 import java
.net
.URLEncoder
;
151 import java
.nio
.charset
.StandardCharsets
;
152 import java
.nio
.file
.Files
;
153 import java
.security
.SignatureException
;
154 import java
.util
.ArrayList
;
155 import java
.util
.Arrays
;
156 import java
.util
.Base64
;
157 import java
.util
.Collection
;
158 import java
.util
.Date
;
159 import java
.util
.HashSet
;
160 import java
.util
.List
;
161 import java
.util
.Map
;
162 import java
.util
.Set
;
163 import java
.util
.UUID
;
164 import java
.util
.concurrent
.ExecutorService
;
165 import java
.util
.concurrent
.Executors
;
166 import java
.util
.concurrent
.TimeUnit
;
167 import java
.util
.concurrent
.TimeoutException
;
168 import java
.util
.concurrent
.locks
.ReentrantLock
;
169 import java
.util
.function
.Function
;
170 import java
.util
.stream
.Collectors
;
172 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
174 public class Manager
implements Closeable
{
176 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
178 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
179 private final SignalDependencies dependencies
;
181 private SignalAccount account
;
183 private final ExecutorService executor
= Executors
.newCachedThreadPool();
185 private final ProfileHelper profileHelper
;
186 private final GroupV2Helper groupV2Helper
;
187 private final PinHelper pinHelper
;
188 private final SendHelper sendHelper
;
190 private final AvatarStore avatarStore
;
191 private final AttachmentStore attachmentStore
;
192 private final StickerPackStore stickerPackStore
;
193 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
194 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
197 public Lock
acquire() {
199 return LEGACY_LOCK
::unlock
;
204 SignalAccount account
,
205 PathConfig pathConfig
,
206 ServiceEnvironmentConfig serviceEnvironmentConfig
,
209 this.account
= account
;
210 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
212 final var credentialsProvider
= new DynamicCredentialsProvider(account
.getUuid(),
213 account
.getUsername(),
214 account
.getPassword(),
215 account
.getDeviceId());
216 this.dependencies
= new SignalDependencies(account
.getSelfAddress(),
217 serviceEnvironmentConfig
,
220 account
.getSignalProtocolStore(),
223 this.pinHelper
= new PinHelper(dependencies
.getKeyBackupService());
225 final var unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
226 account
.getProfileStore()::getProfileKey
,
227 this::getRecipientProfile
,
228 this::getSenderCertificate
);
229 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
230 unidentifiedAccessHelper
::getAccessFor
,
231 dependencies
::getProfileService
,
232 dependencies
::getMessageReceiver
,
233 this::resolveSignalServiceAddress
);
234 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
235 this::getRecipientProfile
,
236 account
::getSelfRecipientId
,
237 dependencies
.getGroupsV2Operations(),
238 dependencies
.getGroupsV2Api(),
239 this::getGroupAuthForToday
,
240 this::resolveSignalServiceAddress
);
241 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
242 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
243 this.stickerPackStore
= new StickerPackStore(pathConfig
.getStickerPacksPath());
244 this.sendHelper
= new SendHelper(account
,
246 unidentifiedAccessHelper
,
247 this::resolveSignalServiceAddress
,
248 this::resolveRecipient
,
249 this::handleIdentityFailure
,
251 this::refreshRegisteredUser
);
254 public String
getUsername() {
255 return account
.getUsername();
258 public SignalServiceAddress
getSelfAddress() {
259 return account
.getSelfAddress();
262 public RecipientId
getSelfRecipientId() {
263 return account
.getSelfRecipientId();
266 private IdentityKeyPair
getIdentityKeyPair() {
267 return account
.getIdentityKeyPair();
270 public int getDeviceId() {
271 return account
.getDeviceId();
274 public static Manager
init(
277 ServiceEnvironment serviceEnvironment
,
279 final TrustNewIdentity trustNewIdentity
280 ) throws IOException
, NotRegisteredException
{
281 var pathConfig
= PathConfig
.createDefault(settingsPath
);
283 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
284 throw new NotRegisteredException();
287 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true, trustNewIdentity
);
289 if (!account
.isRegistered()) {
290 throw new NotRegisteredException();
293 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
295 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
298 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
299 var pathConfig
= PathConfig
.createDefault(settingsPath
);
300 final var dataPath
= pathConfig
.getDataPath();
301 final var files
= dataPath
.listFiles();
307 return Arrays
.stream(files
)
308 .filter(File
::isFile
)
310 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
311 .collect(Collectors
.toList());
314 public void checkAccountState() throws IOException
{
315 if (account
.getLastReceiveTimestamp() == 0) {
316 logger
.warn("The Signal protocol expects that incoming messages are regularly received.");
318 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
319 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
322 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
326 if (dependencies
.getAccountManager().getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
329 if (account
.getUuid() == null) {
330 account
.setUuid(dependencies
.getAccountManager().getOwnUuid());
332 updateAccountAttributes();
336 * This is used for checking a set of phone numbers for registration on Signal
338 * @param numbers The set of phone number in question
339 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
340 * @throws IOException if its unable to get the contacts to check if they're registered
342 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
343 Map
<String
, String
> canonicalizedNumbers
= numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
345 return canonicalizePhoneNumber(n
);
346 } catch (InvalidNumberException e
) {
351 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
352 var contactDetails
= getRegisteredUsers(canonicalizedNumbers
.values()
354 .filter(s
-> !s
.isEmpty())
355 .collect(Collectors
.toSet()));
357 // Store numbers as recipients so we have the number/uuid association
358 contactDetails
.forEach((number
, uuid
) -> resolveRecipientTrusted(new SignalServiceAddress(uuid
, number
)));
360 return numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
361 final var number
= canonicalizedNumbers
.get(n
);
362 final var uuid
= contactDetails
.get(number
);
363 return new Pair
<>(number
.isEmpty() ?
null : number
, uuid
);
367 public void updateAccountAttributes() throws IOException
{
368 dependencies
.getAccountManager()
369 .setAccountAttributes(account
.getEncryptedDeviceName(),
371 account
.getLocalRegistrationId(),
373 // set legacy pin only if no KBS master key is set
374 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
375 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
376 account
.getSelfUnidentifiedAccessKey(),
377 account
.isUnrestrictedUnidentifiedAccess(),
379 account
.isDiscoverableByPhoneNumber());
383 * @param givenName if null, the previous givenName will be kept
384 * @param familyName if null, the previous familyName will be kept
385 * @param about if null, the previous about text will be kept
386 * @param aboutEmoji if null, the previous about emoji will be kept
387 * @param avatar if avatar is null the image from the local avatar store is used (if present),
389 public void setProfile(
390 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
391 ) throws IOException
{
392 var profile
= getRecipientProfile(account
.getSelfRecipientId());
393 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
394 if (givenName
!= null) {
395 builder
.withGivenName(givenName
);
397 if (familyName
!= null) {
398 builder
.withFamilyName(familyName
);
401 builder
.withAbout(about
);
403 if (aboutEmoji
!= null) {
404 builder
.withAboutEmoji(aboutEmoji
);
406 var newProfile
= builder
.build();
408 try (final var streamDetails
= avatar
== null
409 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
410 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
411 dependencies
.getAccountManager()
412 .setVersionedProfile(account
.getUuid(),
413 account
.getProfileKey(),
414 newProfile
.getInternalServiceName(),
415 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
416 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
421 if (avatar
!= null) {
422 if (avatar
.isPresent()) {
423 avatarStore
.storeProfileAvatar(getSelfAddress(),
424 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
426 avatarStore
.deleteProfileAvatar(getSelfAddress());
429 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
431 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
434 public void unregister() throws IOException
{
435 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
436 // If this is the master device, other users can't send messages to this number anymore.
437 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
438 dependencies
.getAccountManager().setGcmId(Optional
.absent());
440 account
.setRegistered(false);
443 public void deleteAccount() throws IOException
{
444 dependencies
.getAccountManager().deleteAccount();
446 account
.setRegistered(false);
449 public List
<Device
> getLinkedDevices() throws IOException
{
450 var devices
= dependencies
.getAccountManager().getDevices();
451 account
.setMultiDevice(devices
.size() > 1);
452 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
453 return devices
.stream().map(d
-> {
454 String deviceName
= d
.getName();
455 if (deviceName
!= null) {
457 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
458 } catch (IOException e
) {
459 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
462 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
463 }).collect(Collectors
.toList());
466 public void removeLinkedDevices(int deviceId
) throws IOException
{
467 dependencies
.getAccountManager().removeDevice(deviceId
);
468 var devices
= dependencies
.getAccountManager().getDevices();
469 account
.setMultiDevice(devices
.size() > 1);
472 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
473 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
475 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
478 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
479 var identityKeyPair
= getIdentityKeyPair();
480 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
482 dependencies
.getAccountManager()
483 .addDevice(deviceIdentifier
,
486 Optional
.of(account
.getProfileKey().serialize()),
488 account
.setMultiDevice(true);
491 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
492 if (!account
.isMasterDevice()) {
493 throw new RuntimeException("Only master device can set a PIN");
495 if (pin
.isPresent()) {
496 final var masterKey
= account
.getPinMasterKey() != null
497 ? account
.getPinMasterKey()
498 : KeyUtils
.createMasterKey();
500 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
502 account
.setRegistrationLockPin(pin
.get(), masterKey
);
505 pinHelper
.removeRegistrationLockPin();
507 account
.setRegistrationLockPin(null, null);
511 void refreshPreKeys() throws IOException
{
512 var oneTimePreKeys
= generatePreKeys();
513 final var identityKeyPair
= getIdentityKeyPair();
514 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
516 dependencies
.getAccountManager().setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
519 private List
<PreKeyRecord
> generatePreKeys() {
520 final var offset
= account
.getPreKeyIdOffset();
522 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
523 account
.addPreKeys(records
);
528 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
529 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
531 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
532 account
.addSignedPreKey(record);
537 public Profile
getRecipientProfile(
538 RecipientId recipientId
540 return getRecipientProfile(recipientId
, false);
543 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
545 Profile
getRecipientProfile(
546 RecipientId recipientId
, boolean force
548 var profile
= account
.getProfileStore().getProfile(recipientId
);
550 var now
= System
.currentTimeMillis();
551 // Profiles are cached for 24h before retrieving them again, unless forced
552 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
556 synchronized (pendingProfileRequest
) {
557 if (pendingProfileRequest
.contains(recipientId
)) {
560 pendingProfileRequest
.add(recipientId
);
562 final SignalServiceProfile encryptedProfile
;
564 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
566 synchronized (pendingProfileRequest
) {
567 pendingProfileRequest
.remove(recipientId
);
570 if (encryptedProfile
== null) {
574 profile
= decryptProfileIfKeyKnown(recipientId
, encryptedProfile
);
575 account
.getProfileStore().storeProfile(recipientId
, profile
);
580 private Profile
decryptProfileIfKeyKnown(
581 final RecipientId recipientId
, final SignalServiceProfile encryptedProfile
583 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
584 if (profileKey
== null) {
585 return new Profile(System
.currentTimeMillis(),
590 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
591 ProfileUtils
.getCapabilities(encryptedProfile
));
594 return decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
597 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
599 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
600 } catch (IOException e
) {
601 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
606 private ProfileAndCredential
retrieveProfileAndCredential(
607 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
608 ) throws IOException
{
609 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
610 final var profile
= profileAndCredential
.getProfile();
613 var newIdentity
= account
.getIdentityKeyStore()
614 .saveIdentity(recipientId
,
615 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
619 account
.getSessionStore().archiveSessions(recipientId
);
621 } catch (InvalidKeyException ignored
) {
622 logger
.warn("Got invalid identity key in profile for {}",
623 resolveSignalServiceAddress(recipientId
).getIdentifier());
625 return profileAndCredential
;
628 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
629 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
630 if (profileKeyCredential
!= null) {
631 return profileKeyCredential
;
634 ProfileAndCredential profileAndCredential
;
636 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
637 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
638 } catch (IOException e
) {
639 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
643 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
644 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
646 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
647 if (profileKey
!= null) {
648 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
650 profileAndCredential
.getProfile());
651 account
.getProfileStore().storeProfile(recipientId
, profile
);
654 return profileKeyCredential
;
657 private Profile
decryptProfileAndDownloadAvatar(
658 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
660 if (encryptedProfile
.getAvatar() != null) {
661 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
664 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
667 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
668 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
669 if (streamDetails
== null) {
670 return Optional
.absent();
673 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
676 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
677 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
678 if (streamDetails
== null) {
679 return Optional
.absent();
682 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
685 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
686 var g
= getGroup(groupId
);
688 throw new GroupNotFoundException(groupId
);
690 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
691 throw new NotAGroupMemberException(groupId
, g
.getTitle());
696 public List
<GroupInfo
> getGroups() {
697 return account
.getGroupStore().getGroups();
700 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessage(
701 Message message
, GroupId groupId
702 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
703 final var messageBuilder
= createMessageBuilder();
704 applyMessage(messageBuilder
, message
);
706 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
709 public Pair
<Long
, List
<SendMessageResult
>> sendGroupMessageReaction(
710 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, GroupId groupId
711 ) throws IOException
, InvalidNumberException
, NotAGroupMemberException
, GroupNotFoundException
{
712 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
713 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
715 resolveSignalServiceAddress(targetAuthorRecipientId
),
716 targetSentTimestamp
);
717 final var messageBuilder
= createMessageBuilder().withReaction(reaction
);
719 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
722 public Pair
<Long
, List
<SendMessageResult
>> sendQuitGroupMessage(
723 GroupId groupId
, Set
<String
> groupAdmins
724 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, InvalidNumberException
, LastGroupAdminException
{
725 var group
= getGroupForUpdating(groupId
);
726 if (group
instanceof GroupInfoV1
) {
727 return quitGroupV1((GroupInfoV1
) group
);
730 final var newAdmins
= getRecipientIds(groupAdmins
);
732 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
733 } catch (ConflictException e
) {
734 // Detected conflicting update, refreshing group and trying again
735 group
= getGroup(groupId
, true);
736 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
740 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV1(final GroupInfoV1 groupInfoV1
) throws IOException
{
741 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
742 .withId(groupInfoV1
.getGroupId().serialize())
745 var messageBuilder
= createMessageBuilder().asGroupMessage(group
);
746 groupInfoV1
.removeMember(account
.getSelfRecipientId());
747 account
.getGroupStore().updateGroup(groupInfoV1
);
748 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
749 groupInfoV1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
752 private Pair
<Long
, List
<SendMessageResult
>> quitGroupV2(
753 final GroupInfoV2 groupInfoV2
, final Set
<RecipientId
> newAdmins
754 ) throws LastGroupAdminException
, IOException
{
755 final var currentAdmins
= groupInfoV2
.getAdminMembers();
756 newAdmins
.removeAll(currentAdmins
);
757 newAdmins
.retainAll(groupInfoV2
.getMembers());
758 if (currentAdmins
.contains(getSelfRecipientId())
759 && currentAdmins
.size() == 1
760 && groupInfoV2
.getMembers().size() > 1
761 && newAdmins
.size() == 0) {
762 // Last admin can't leave the group, unless she's also the last member
763 throw new LastGroupAdminException(groupInfoV2
.getGroupId(), groupInfoV2
.getTitle());
765 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
766 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
767 var messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
768 account
.getGroupStore().updateGroup(groupInfoV2
);
769 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
770 groupInfoV2
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
773 public void deleteGroup(GroupId groupId
) throws IOException
{
774 account
.getGroupStore().deleteGroup(groupId
);
775 avatarStore
.deleteGroupAvatar(groupId
);
778 public Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
779 String name
, List
<String
> members
, File avatarFile
780 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
781 return createGroup(name
, members
== null ?
null : getRecipientIds(members
), avatarFile
);
784 private Pair
<GroupId
, List
<SendMessageResult
>> createGroup(
785 String name
, Set
<RecipientId
> members
, File avatarFile
786 ) throws IOException
, AttachmentInvalidException
{
787 final var selfRecipientId
= account
.getSelfRecipientId();
788 if (members
!= null && members
.contains(selfRecipientId
)) {
789 members
= new HashSet
<>(members
);
790 members
.remove(selfRecipientId
);
793 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
794 members
== null ? Set
.of() : members
,
797 SignalServiceDataMessage
.Builder messageBuilder
;
798 if (gv2Pair
== null) {
799 // Failed to create v2 group, creating v1 group instead
800 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
801 gv1
.addMembers(List
.of(selfRecipientId
));
802 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
803 return new Pair
<>(gv1
.getGroupId(), result
.second());
806 final var gv2
= gv2Pair
.first();
807 final var decryptedGroup
= gv2Pair
.second();
809 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
810 if (avatarFile
!= null) {
811 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
812 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
814 messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
815 account
.getGroupStore().updateGroup(gv2
);
817 final var result
= sendHelper
.sendGroupMessage(messageBuilder
.build(),
818 gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
819 return new Pair
<>(gv2
.getGroupId(), result
.second());
822 public Pair
<Long
, List
<SendMessageResult
>> updateGroup(
826 List
<String
> members
,
827 List
<String
> removeMembers
,
829 List
<String
> removeAdmins
,
830 boolean resetGroupLink
,
831 GroupLinkState groupLinkState
,
832 GroupPermission addMemberPermission
,
833 GroupPermission editDetailsPermission
,
835 Integer expirationTimer
,
836 Boolean isAnnouncementGroup
837 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, InvalidNumberException
, NotAGroupMemberException
{
838 return updateGroup(groupId
,
841 members
== null ?
null : getRecipientIds(members
),
842 removeMembers
== null ?
null : getRecipientIds(removeMembers
),
843 admins
== null ?
null : getRecipientIds(admins
),
844 removeAdmins
== null ?
null : getRecipientIds(removeAdmins
),
848 editDetailsPermission
,
851 isAnnouncementGroup
);
854 private Pair
<Long
, List
<SendMessageResult
>> updateGroup(
855 final GroupId groupId
,
857 final String description
,
858 final Set
<RecipientId
> members
,
859 final Set
<RecipientId
> removeMembers
,
860 final Set
<RecipientId
> admins
,
861 final Set
<RecipientId
> removeAdmins
,
862 final boolean resetGroupLink
,
863 final GroupLinkState groupLinkState
,
864 final GroupPermission addMemberPermission
,
865 final GroupPermission editDetailsPermission
,
866 final File avatarFile
,
867 final Integer expirationTimer
,
868 final Boolean isAnnouncementGroup
869 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
870 var group
= getGroupForUpdating(groupId
);
872 if (group
instanceof GroupInfoV2
) {
874 return updateGroupV2((GroupInfoV2
) group
,
884 editDetailsPermission
,
887 isAnnouncementGroup
);
888 } catch (ConflictException e
) {
889 // Detected conflicting update, refreshing group and trying again
890 group
= getGroup(groupId
, true);
891 return updateGroupV2((GroupInfoV2
) group
,
901 editDetailsPermission
,
904 isAnnouncementGroup
);
908 final var gv1
= (GroupInfoV1
) group
;
909 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
910 if (expirationTimer
!= null) {
911 setExpirationTimer(gv1
, expirationTimer
);
916 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV1(
917 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
918 ) throws IOException
, AttachmentInvalidException
{
919 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
920 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
922 account
.getGroupStore().updateGroup(gv1
);
924 return sendHelper
.sendGroupMessage(messageBuilder
.build(),
925 gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
928 private void updateGroupV1Details(
929 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
930 ) throws IOException
{
935 if (members
!= null) {
936 final var newMemberAddresses
= members
.stream()
937 .filter(member
-> !g
.isMember(member
))
938 .map(this::resolveSignalServiceAddress
)
939 .collect(Collectors
.toList());
940 final var newE164Members
= new HashSet
<String
>();
941 for (var member
: newMemberAddresses
) {
942 if (!member
.getNumber().isPresent()) {
945 newE164Members
.add(member
.getNumber().get());
948 final var registeredUsers
= getRegisteredUsers(newE164Members
);
949 if (registeredUsers
.size() != newE164Members
.size()) {
950 // Some of the new members are not registered on Signal
951 newE164Members
.removeAll(registeredUsers
.keySet());
952 throw new IOException("Failed to add members "
953 + String
.join(", ", newE164Members
)
954 + " to group: Not registered on Signal");
957 g
.addMembers(members
);
960 if (avatarFile
!= null) {
961 avatarStore
.storeGroupAvatar(g
.getGroupId(),
962 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
966 private Pair
<Long
, List
<SendMessageResult
>> updateGroupV2(
967 final GroupInfoV2 group
,
969 final String description
,
970 final Set
<RecipientId
> members
,
971 final Set
<RecipientId
> removeMembers
,
972 final Set
<RecipientId
> admins
,
973 final Set
<RecipientId
> removeAdmins
,
974 final boolean resetGroupLink
,
975 final GroupLinkState groupLinkState
,
976 final GroupPermission addMemberPermission
,
977 final GroupPermission editDetailsPermission
,
978 final File avatarFile
,
979 final Integer expirationTimer
,
980 final Boolean isAnnouncementGroup
981 ) throws IOException
{
982 Pair
<Long
, List
<SendMessageResult
>> result
= null;
983 if (group
.isPendingMember(account
.getSelfRecipientId())) {
984 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
985 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
988 if (members
!= null) {
989 final var newMembers
= new HashSet
<>(members
);
990 newMembers
.removeAll(group
.getMembers());
991 if (newMembers
.size() > 0) {
992 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
993 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
997 if (removeMembers
!= null) {
998 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
999 existingRemoveMembers
.retainAll(group
.getMembers());
1000 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
1001 if (existingRemoveMembers
.size() > 0) {
1002 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
1003 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1006 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
1007 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
1008 if (pendingRemoveMembers
.size() > 0) {
1009 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
1010 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1014 if (admins
!= null) {
1015 final var newAdmins
= new HashSet
<>(admins
);
1016 newAdmins
.retainAll(group
.getMembers());
1017 newAdmins
.removeAll(group
.getAdminMembers());
1018 if (newAdmins
.size() > 0) {
1019 for (var admin
: newAdmins
) {
1020 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1021 result
= sendUpdateGroupV2Message(group
,
1022 groupGroupChangePair
.first(),
1023 groupGroupChangePair
.second());
1028 if (removeAdmins
!= null) {
1029 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1030 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1031 if (existingRemoveAdmins
.size() > 0) {
1032 for (var admin
: existingRemoveAdmins
) {
1033 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1034 result
= sendUpdateGroupV2Message(group
,
1035 groupGroupChangePair
.first(),
1036 groupGroupChangePair
.second());
1041 if (resetGroupLink
) {
1042 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1043 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1046 if (groupLinkState
!= null) {
1047 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1048 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1051 if (addMemberPermission
!= null) {
1052 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1053 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1056 if (editDetailsPermission
!= null) {
1057 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1058 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1061 if (expirationTimer
!= null) {
1062 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1063 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1066 if (isAnnouncementGroup
!= null) {
1067 var groupGroupChangePair
= groupV2Helper
.setIsAnnouncementGroup(group
, isAnnouncementGroup
);
1068 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1071 if (name
!= null || description
!= null || avatarFile
!= null) {
1072 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1073 if (avatarFile
!= null) {
1074 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1075 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1077 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1083 public Pair
<GroupId
, List
<SendMessageResult
>> joinGroup(
1084 GroupInviteLinkUrl inviteLinkUrl
1085 ) throws IOException
, GroupLinkNotActiveException
{
1086 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1087 inviteLinkUrl
.getPassword());
1088 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1089 inviteLinkUrl
.getPassword(),
1091 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1092 groupJoinInfo
.getRevision() + 1,
1093 groupChange
.toByteArray());
1095 if (group
.getGroup() == null) {
1096 // Only requested member, can't send update to group members
1097 return new Pair
<>(group
.getGroupId(), List
.of());
1100 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1102 return new Pair
<>(group
.getGroupId(), result
.second());
1105 private Pair
<Long
, List
<SendMessageResult
>> sendUpdateGroupV2Message(
1106 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1107 ) throws IOException
{
1108 final var selfRecipientId
= account
.getSelfRecipientId();
1109 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1110 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1111 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1113 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1114 account
.getGroupStore().updateGroup(group
);
1115 return sendHelper
.sendGroupMessage(messageBuilder
.build(), members
);
1118 private static int currentTimeDays() {
1119 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1122 private GroupsV2AuthorizationString
getGroupAuthForToday(
1123 final GroupSecretParams groupSecretParams
1124 ) throws IOException
{
1125 final var today
= currentTimeDays();
1126 // Returns credentials for the next 7 days
1127 final var credentials
= dependencies
.getGroupsV2Api().getCredentials(today
);
1128 // TODO cache credentials until they expire
1129 var authCredentialResponse
= credentials
.get(today
);
1131 return dependencies
.getGroupsV2Api()
1132 .getGroupsV2AuthorizationString(account
.getUuid(),
1135 authCredentialResponse
);
1136 } catch (VerificationFailedException e
) {
1137 throw new IOException(e
);
1141 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoMessage(
1142 GroupIdV1 groupId
, SignalServiceAddress recipient
1143 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1145 var group
= getGroupForUpdating(groupId
);
1146 if (!(group
instanceof GroupInfoV1
)) {
1147 throw new IOException("Received an invalid group request for a v2 group!");
1149 g
= (GroupInfoV1
) group
;
1151 final var recipientId
= resolveRecipient(recipient
);
1152 if (!g
.isMember(recipientId
)) {
1153 throw new NotAGroupMemberException(groupId
, g
.name
);
1156 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1158 // Send group message only to the recipient who requested it
1159 return sendHelper
.sendGroupMessage(messageBuilder
.build(), Set
.of(recipientId
));
1162 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1163 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1164 .withId(g
.getGroupId().serialize())
1166 .withMembers(g
.getMembers()
1168 .map(this::resolveSignalServiceAddress
)
1169 .collect(Collectors
.toList()));
1172 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1173 if (attachment
.isPresent()) {
1174 group
.withAvatar(attachment
.get());
1176 } catch (IOException e
) {
1177 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1180 return createMessageBuilder().asGroupMessage(group
.build()).withExpiration(g
.getMessageExpirationTime());
1183 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1184 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1185 .withRevision(g
.getGroup().getRevision())
1186 .withSignedGroupChange(signedGroupChange
);
1187 return createMessageBuilder().asGroupMessage(group
.build()).withExpiration(g
.getMessageExpirationTime());
1190 Pair
<Long
, List
<SendMessageResult
>> sendGroupInfoRequest(
1191 GroupIdV1 groupId
, SignalServiceAddress recipient
1192 ) throws IOException
{
1193 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1195 var messageBuilder
= createMessageBuilder().asGroupMessage(group
.build());
1197 // Send group info request message to the recipient who sent us a message with this groupId
1198 return sendHelper
.sendGroupMessage(messageBuilder
.build(), Set
.of(resolveRecipient(recipient
)));
1201 public void sendReadReceipt(
1202 String sender
, List
<Long
> messageIds
1203 ) throws IOException
, UntrustedIdentityException
, InvalidNumberException
{
1204 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.READ
,
1206 System
.currentTimeMillis());
1208 sendHelper
.sendReceiptMessage(receiptMessage
, canonicalizeAndResolveRecipient(sender
));
1211 public void sendViewedReceipt(
1212 String sender
, List
<Long
> messageIds
1213 ) throws IOException
, UntrustedIdentityException
, InvalidNumberException
{
1214 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.VIEWED
,
1216 System
.currentTimeMillis());
1218 sendHelper
.sendReceiptMessage(receiptMessage
, canonicalizeAndResolveRecipient(sender
));
1221 void sendDeliveryReceipt(
1222 SignalServiceAddress remoteAddress
, List
<Long
> messageIds
1223 ) throws IOException
, UntrustedIdentityException
{
1224 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1226 System
.currentTimeMillis());
1228 sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(remoteAddress
));
1231 public Pair
<Long
, List
<SendMessageResult
>> sendMessage(
1232 Message message
, List
<String
> recipients
1233 ) throws IOException
, AttachmentInvalidException
, InvalidNumberException
{
1234 final var messageBuilder
= createMessageBuilder();
1235 applyMessage(messageBuilder
, message
);
1236 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1239 private void applyMessage(
1240 final SignalServiceDataMessage
.Builder messageBuilder
, final Message message
1241 ) throws AttachmentInvalidException
, IOException
{
1242 messageBuilder
.withBody(message
.getMessageText());
1243 if (message
.getAttachments() != null) {
1244 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(message
.getAttachments());
1246 // Upload attachments here, so we only upload once even for multiple recipients
1247 var messageSender
= dependencies
.getMessageSender();
1248 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1249 for (var attachment
: attachmentStreams
) {
1250 if (attachment
.isStream()) {
1251 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1252 } else if (attachment
.isPointer()) {
1253 attachmentPointers
.add(attachment
.asPointer());
1257 messageBuilder
.withAttachments(attachmentPointers
);
1261 public Pair
<Long
, SendMessageResult
> sendSelfMessage(final Message message
) throws IOException
, AttachmentInvalidException
{
1262 final var messageBuilder
= createMessageBuilder();
1263 applyMessage(messageBuilder
, message
);
1264 return sendHelper
.sendSelfMessage(messageBuilder
);
1267 public Pair
<Long
, List
<SendMessageResult
>> sendRemoteDeleteMessage(
1268 long targetSentTimestamp
, List
<String
> recipients
1269 ) throws IOException
, InvalidNumberException
{
1270 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1271 final var messageBuilder
= createMessageBuilder().withRemoteDelete(delete
);
1272 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1275 public Pair
<Long
, List
<SendMessageResult
>> sendGroupRemoteDeleteMessage(
1276 long targetSentTimestamp
, GroupId groupId
1277 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1278 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1279 final var messageBuilder
= createMessageBuilder().withRemoteDelete(delete
);
1280 return sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1283 public Pair
<Long
, List
<SendMessageResult
>> sendMessageReaction(
1284 String emoji
, boolean remove
, String targetAuthor
, long targetSentTimestamp
, List
<String
> recipients
1285 ) throws IOException
, InvalidNumberException
{
1286 var targetAuthorRecipientId
= canonicalizeAndResolveRecipient(targetAuthor
);
1287 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1289 resolveSignalServiceAddress(targetAuthorRecipientId
),
1290 targetSentTimestamp
);
1291 final var messageBuilder
= createMessageBuilder().withReaction(reaction
);
1292 return sendHelper
.sendMessage(messageBuilder
, getRecipientIds(recipients
));
1295 public Pair
<Long
, List
<SendMessageResult
>> sendEndSessionMessage(List
<String
> recipients
) throws IOException
, InvalidNumberException
{
1296 var messageBuilder
= createMessageBuilder().asEndSessionMessage();
1298 final var recipientIds
= getRecipientIds(recipients
);
1300 return sendHelper
.sendMessage(messageBuilder
, recipientIds
);
1302 for (var recipientId
: recipientIds
) {
1303 handleEndSession(recipientId
);
1308 void renewSession(RecipientId recipientId
) throws IOException
{
1309 account
.getSessionStore().archiveSessions(recipientId
);
1310 if (!recipientId
.equals(getSelfRecipientId())) {
1311 sendHelper
.sendNullMessage(recipientId
);
1315 public void setContactName(String number
, String name
) throws InvalidNumberException
, NotMasterDeviceException
{
1316 if (!account
.isMasterDevice()) {
1317 throw new NotMasterDeviceException();
1319 final var recipientId
= canonicalizeAndResolveRecipient(number
);
1320 var contact
= account
.getContactStore().getContact(recipientId
);
1321 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1322 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1325 public void setContactBlocked(
1326 String number
, boolean blocked
1327 ) throws InvalidNumberException
, NotMasterDeviceException
{
1328 if (!account
.isMasterDevice()) {
1329 throw new NotMasterDeviceException();
1331 setContactBlocked(canonicalizeAndResolveRecipient(number
), blocked
);
1334 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1335 var contact
= account
.getContactStore().getContact(recipientId
);
1336 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1337 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1340 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1341 var group
= getGroup(groupId
);
1342 if (group
== null) {
1343 throw new GroupNotFoundException(groupId
);
1346 group
.setBlocked(blocked
);
1347 account
.getGroupStore().updateGroup(group
);
1350 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1351 var contact
= account
.getContactStore().getContact(recipientId
);
1352 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1355 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1356 account
.getContactStore()
1357 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1360 private void sendExpirationTimerUpdate(RecipientId recipientId
) throws IOException
{
1361 final var messageBuilder
= createMessageBuilder().asExpirationUpdate();
1362 sendHelper
.sendMessage(messageBuilder
, Set
.of(recipientId
));
1366 * Change the expiration timer for a contact
1368 public void setExpirationTimer(
1369 String number
, int messageExpirationTimer
1370 ) throws IOException
, InvalidNumberException
{
1371 var recipientId
= canonicalizeAndResolveRecipient(number
);
1372 setExpirationTimer(recipientId
, messageExpirationTimer
);
1373 sendExpirationTimerUpdate(recipientId
);
1377 * Change the expiration timer for a group
1379 private void setExpirationTimer(
1380 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1381 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1382 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1383 account
.getGroupStore().updateGroup(groupInfoV1
);
1384 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1387 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1388 final var messageBuilder
= createMessageBuilder().asExpirationUpdate();
1389 sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1393 * Upload the sticker pack from path.
1395 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1396 * @return if successful, returns the URL to install the sticker pack in the signal app
1398 public String
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1399 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1401 var messageSender
= dependencies
.getMessageSender();
1403 var packKey
= KeyUtils
.createStickerUploadKey();
1404 var packIdString
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1405 var packId
= StickerPackId
.deserialize(Hex
.fromStringCondensed(packIdString
));
1407 var sticker
= new Sticker(packId
, packKey
);
1408 account
.getStickerStore().updateSticker(sticker
);
1411 return new URI("https",
1415 + URLEncoder
.encode(Hex
.toStringCondensed(packId
.serialize()), StandardCharsets
.UTF_8
)
1417 + URLEncoder
.encode(Hex
.toStringCondensed(packKey
), StandardCharsets
.UTF_8
)).toString();
1418 } catch (URISyntaxException e
) {
1419 throw new AssertionError(e
);
1423 public void requestAllSyncData() throws IOException
{
1424 requestSyncGroups();
1425 requestSyncContacts();
1426 requestSyncBlocked();
1427 requestSyncConfiguration();
1431 private void requestSyncGroups() throws IOException
{
1432 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1433 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1435 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1436 sendHelper
.sendSyncMessage(message
);
1439 private void requestSyncContacts() throws IOException
{
1440 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1441 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1443 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1444 sendHelper
.sendSyncMessage(message
);
1447 private void requestSyncBlocked() throws IOException
{
1448 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1449 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1451 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1452 sendHelper
.sendSyncMessage(message
);
1455 private void requestSyncConfiguration() throws IOException
{
1456 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1457 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1459 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1460 sendHelper
.sendSyncMessage(message
);
1463 private void requestSyncKeys() throws IOException
{
1464 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1465 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1467 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1468 sendHelper
.sendSyncMessage(message
);
1471 private byte[] getSenderCertificate() {
1474 if (account
.isPhoneNumberShared()) {
1475 certificate
= dependencies
.getAccountManager().getSenderCertificate();
1477 certificate
= dependencies
.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
1479 } catch (IOException e
) {
1480 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1483 // TODO cache for a day
1487 private Set
<RecipientId
> getRecipientIds(Collection
<String
> numbers
) throws InvalidNumberException
{
1488 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(numbers
.size());
1489 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1491 for (var number
: numbers
) {
1492 final var resolvedAddress
= resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number
));
1493 if (resolvedAddress
.getUuid().isPresent()) {
1494 signalServiceAddresses
.add(resolvedAddress
);
1496 addressesMissingUuid
.add(resolvedAddress
);
1500 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1501 .map(a
-> a
.getNumber().get())
1502 .collect(Collectors
.toSet());
1503 Map
<String
, UUID
> registeredUsers
;
1505 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1506 } catch (IOException e
) {
1507 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1508 registeredUsers
= Map
.of();
1511 for (var address
: addressesMissingUuid
) {
1512 final var number
= address
.getNumber().get();
1513 if (registeredUsers
.containsKey(number
)) {
1514 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1515 registeredUsers
.get(number
),
1517 signalServiceAddresses
.add(newAddress
);
1519 signalServiceAddresses
.add(address
);
1523 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1526 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1527 final var address
= resolveSignalServiceAddress(recipientId
);
1528 if (!address
.getNumber().isPresent()) {
1531 final var number
= address
.getNumber().get();
1532 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1533 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1536 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1538 return dependencies
.getAccountManager()
1539 .getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1541 serviceEnvironmentConfig
.getCdsMrenclave());
1542 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1543 throw new IOException(e
);
1547 public void sendTypingMessage(
1548 TypingAction action
, Set
<String
> recipients
1549 ) throws IOException
, UntrustedIdentityException
, InvalidNumberException
{
1550 final var timestamp
= System
.currentTimeMillis();
1551 var message
= new SignalServiceTypingMessage(action
.toSignalService(), timestamp
, Optional
.absent());
1552 sendHelper
.sendTypingMessage(message
, getRecipientIds(recipients
));
1555 public void sendGroupTypingMessage(
1556 TypingAction action
, GroupId groupId
1557 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1558 final var timestamp
= System
.currentTimeMillis();
1559 final var message
= new SignalServiceTypingMessage(action
.toSignalService(),
1561 Optional
.of(groupId
.serialize()));
1562 sendHelper
.sendGroupTypingMessage(message
, groupId
);
1565 private SignalServiceDataMessage
.Builder
createMessageBuilder() {
1566 final var timestamp
= System
.currentTimeMillis();
1568 var messageBuilder
= SignalServiceDataMessage
.newBuilder();
1569 messageBuilder
.withTimestamp(timestamp
);
1570 return messageBuilder
;
1573 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
, InvalidMessageStructureException
{
1574 return dependencies
.getCipher().decrypt(envelope
);
1577 private void handleEndSession(RecipientId recipientId
) {
1578 account
.getSessionStore().deleteAllSessions(recipientId
);
1581 private List
<HandleAction
> handleSignalServiceDataMessage(
1582 SignalServiceDataMessage message
,
1584 SignalServiceAddress source
,
1585 SignalServiceAddress destination
,
1586 boolean ignoreAttachments
1588 var actions
= new ArrayList
<HandleAction
>();
1589 if (message
.getGroupContext().isPresent()) {
1590 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1591 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1592 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1593 var group
= getGroup(groupId
);
1594 if (group
== null || group
instanceof GroupInfoV1
) {
1595 var groupV1
= (GroupInfoV1
) group
;
1596 switch (groupInfo
.getType()) {
1598 if (groupV1
== null) {
1599 groupV1
= new GroupInfoV1(groupId
);
1602 if (groupInfo
.getAvatar().isPresent()) {
1603 var avatar
= groupInfo
.getAvatar().get();
1604 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1607 if (groupInfo
.getName().isPresent()) {
1608 groupV1
.name
= groupInfo
.getName().get();
1611 if (groupInfo
.getMembers().isPresent()) {
1612 groupV1
.addMembers(groupInfo
.getMembers()
1615 .map(this::resolveRecipient
)
1616 .collect(Collectors
.toSet()));
1619 account
.getGroupStore().updateGroup(groupV1
);
1623 if (groupV1
== null && !isSync
) {
1624 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1628 if (groupV1
!= null) {
1629 groupV1
.removeMember(resolveRecipient(source
));
1630 account
.getGroupStore().updateGroup(groupV1
);
1635 if (groupV1
!= null && !isSync
) {
1636 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1641 // Received a group v1 message for a v2 group
1644 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1645 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1646 final var groupMasterKey
= groupContext
.getMasterKey();
1648 getOrMigrateGroup(groupMasterKey
,
1649 groupContext
.getRevision(),
1650 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1654 final var conversationPartnerAddress
= isSync ? destination
: source
;
1655 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1656 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1658 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1659 if (message
.getGroupContext().isPresent()) {
1660 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1661 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1662 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1663 if (group
!= null) {
1664 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1665 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1666 account
.getGroupStore().updateGroup(group
);
1669 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1670 // disappearing message timer already stored in the DecryptedGroup
1672 } else if (conversationPartnerAddress
!= null) {
1673 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1676 if (!ignoreAttachments
) {
1677 if (message
.getAttachments().isPresent()) {
1678 for (var attachment
: message
.getAttachments().get()) {
1679 downloadAttachment(attachment
);
1682 if (message
.getSharedContacts().isPresent()) {
1683 for (var contact
: message
.getSharedContacts().get()) {
1684 if (contact
.getAvatar().isPresent()) {
1685 downloadAttachment(contact
.getAvatar().get().getAttachment());
1690 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1691 final ProfileKey profileKey
;
1693 profileKey
= new ProfileKey(message
.getProfileKey().get());
1694 } catch (InvalidInputException e
) {
1695 throw new AssertionError(e
);
1697 if (source
.matches(account
.getSelfAddress())) {
1698 this.account
.setProfileKey(profileKey
);
1700 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1702 if (message
.getPreviews().isPresent()) {
1703 final var previews
= message
.getPreviews().get();
1704 for (var preview
: previews
) {
1705 if (preview
.getImage().isPresent()) {
1706 downloadAttachment(preview
.getImage().get());
1710 if (message
.getQuote().isPresent()) {
1711 final var quote
= message
.getQuote().get();
1713 for (var quotedAttachment
: quote
.getAttachments()) {
1714 final var thumbnail
= quotedAttachment
.getThumbnail();
1715 if (thumbnail
!= null) {
1716 downloadAttachment(thumbnail
);
1720 if (message
.getSticker().isPresent()) {
1721 final var messageSticker
= message
.getSticker().get();
1722 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1723 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1724 if (sticker
== null) {
1725 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1726 account
.getStickerStore().updateSticker(sticker
);
1728 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, messageSticker
.getPackKey()));
1733 private GroupInfoV2
getOrMigrateGroup(
1734 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1736 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1738 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1739 var groupInfo
= getGroup(groupId
);
1740 final GroupInfoV2 groupInfoV2
;
1741 if (groupInfo
instanceof GroupInfoV1
) {
1742 // Received a v2 group message for a v1 group, we need to locally migrate the group
1743 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1744 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1745 logger
.info("Locally migrated group {} to group v2, id: {}",
1746 groupInfo
.getGroupId().toBase64(),
1747 groupInfoV2
.getGroupId().toBase64());
1748 } else if (groupInfo
instanceof GroupInfoV2
) {
1749 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1751 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1754 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1755 DecryptedGroup group
= null;
1756 if (signedGroupChange
!= null
1757 && groupInfoV2
.getGroup() != null
1758 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1759 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1763 if (group
== null) {
1764 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1766 if (group
!= null) {
1767 storeProfileKeysFromMembers(group
);
1768 final var avatar
= group
.getAvatar();
1769 if (avatar
!= null && !avatar
.isEmpty()) {
1770 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1773 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1774 account
.getGroupStore().updateGroup(groupInfoV2
);
1780 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1781 for (var member
: group
.getMembersList()) {
1782 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1783 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1785 account
.getProfileStore()
1786 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1787 } catch (InvalidInputException ignored
) {
1792 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1793 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1794 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1795 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1796 if (actions
!= null) {
1797 queuedActions
.addAll(actions
);
1800 handleQueuedActions(queuedActions
);
1803 private List
<HandleAction
> retryFailedReceivedMessage(
1804 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1806 var envelope
= cachedMessage
.loadEnvelope();
1807 if (envelope
== null) {
1810 SignalServiceContent content
= null;
1811 List
<HandleAction
> actions
= null;
1812 if (!envelope
.isReceipt()) {
1814 content
= decryptMessage(envelope
);
1815 } catch (ProtocolUntrustedIdentityException e
) {
1816 if (!envelope
.hasSource()) {
1817 final var identifier
= e
.getSender();
1818 final var recipientId
= resolveRecipient(identifier
);
1820 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1821 } catch (IOException ioException
) {
1822 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1826 } catch (Exception er
) {
1827 // All other errors are not recoverable, so delete the cached message
1828 cachedMessage
.delete();
1831 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1833 handler
.handleMessage(envelope
, content
, null);
1834 cachedMessage
.delete();
1838 public void receiveMessages(
1841 boolean returnOnTimeout
,
1842 boolean ignoreAttachments
,
1843 ReceiveMessageHandler handler
1844 ) throws IOException
{
1845 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1847 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1849 final var signalWebSocket
= dependencies
.getSignalWebSocket();
1850 signalWebSocket
.connect();
1852 var hasCaughtUpWithOldMessages
= false;
1854 while (!Thread
.interrupted()) {
1855 SignalServiceEnvelope envelope
;
1856 SignalServiceContent content
= null;
1857 Exception exception
= null;
1858 final CachedMessage
[] cachedMessage
= {null};
1859 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
1860 logger
.debug("Checking for new message from server");
1862 var result
= signalWebSocket
.readOrEmpty(unit
.toMillis(timeout
), envelope1
-> {
1863 final var recipientId
= envelope1
.hasSource()
1864 ?
resolveRecipient(envelope1
.getSourceIdentifier())
1866 // store message on disk, before acknowledging receipt to the server
1867 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1869 logger
.debug("New message received from server");
1870 if (result
.isPresent()) {
1871 envelope
= result
.get();
1873 // Received indicator that server queue is empty
1874 hasCaughtUpWithOldMessages
= true;
1876 handleQueuedActions(queuedActions
);
1877 queuedActions
.clear();
1879 // Continue to wait another timeout for new messages
1882 } catch (AssertionError e
) {
1883 if (e
.getCause() instanceof InterruptedException
) {
1884 Thread
.currentThread().interrupt();
1889 } catch (WebSocketUnavailableException e
) {
1890 logger
.debug("Pipe unexpectedly unavailable, connecting");
1891 signalWebSocket
.connect();
1893 } catch (TimeoutException e
) {
1894 if (returnOnTimeout
) return;
1898 if (envelope
.hasSource()) {
1899 // Store uuid if we don't have it already
1900 // address/uuid in envelope is sent by server
1901 resolveRecipientTrusted(envelope
.getSourceAddress());
1903 if (!envelope
.isReceipt()) {
1905 content
= decryptMessage(envelope
);
1906 } catch (Exception e
) {
1909 if (!envelope
.hasSource() && content
!= null) {
1910 // Store uuid if we don't have it already
1911 // address/uuid is validated by unidentified sender certificate
1912 resolveRecipientTrusted(content
.getSender());
1914 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1915 if (exception
instanceof ProtocolInvalidMessageException
) {
1916 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
1917 logger
.debug("Received invalid message, queuing renew session action.");
1918 actions
.add(new RenewSessionAction(sender
));
1920 if (hasCaughtUpWithOldMessages
) {
1921 for (var action
: actions
) {
1923 action
.execute(this);
1924 } catch (Throwable e
) {
1925 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1926 Thread
.currentThread().interrupt();
1928 logger
.warn("Message action failed.", e
);
1932 queuedActions
.addAll(actions
);
1935 final var notAllowedToSendToGroup
= isNotAllowedToSendToGroup(envelope
, content
);
1936 if (isMessageBlocked(envelope
, content
)) {
1937 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
1938 } else if (notAllowedToSendToGroup
) {
1939 logger
.info("Ignoring a group message from an unauthorized sender (no member or admin): {} {}",
1940 (envelope
.hasSource() ? envelope
.getSourceAddress() : content
.getSender()).getIdentifier(),
1941 envelope
.getTimestamp());
1943 handler
.handleMessage(envelope
, content
, exception
);
1945 if (cachedMessage
[0] != null) {
1946 if (exception
instanceof ProtocolUntrustedIdentityException
) {
1947 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
1948 final var recipientId
= resolveRecipient(identifier
);
1949 queuedActions
.add(new RetrieveProfileAction(recipientId
));
1950 if (!envelope
.hasSource()) {
1952 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
1953 } catch (IOException ioException
) {
1954 logger
.warn("Failed to move cached message to recipient folder: {}",
1955 ioException
.getMessage());
1959 cachedMessage
[0].delete();
1963 handleQueuedActions(queuedActions
);
1966 private void handleQueuedActions(final Set
<HandleAction
> queuedActions
) {
1967 for (var action
: queuedActions
) {
1969 action
.execute(this);
1970 } catch (Throwable e
) {
1971 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1972 Thread
.currentThread().interrupt();
1974 logger
.warn("Message action failed.", e
);
1979 private boolean isMessageBlocked(
1980 SignalServiceEnvelope envelope
, SignalServiceContent content
1982 SignalServiceAddress source
;
1983 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
1984 source
= envelope
.getSourceAddress();
1985 } else if (content
!= null) {
1986 source
= content
.getSender();
1990 final var recipientId
= resolveRecipient(source
);
1991 if (isContactBlocked(recipientId
)) {
1995 if (content
!= null && content
.getDataMessage().isPresent()) {
1996 var message
= content
.getDataMessage().get();
1997 if (message
.getGroupContext().isPresent()) {
1998 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
1999 var group
= getGroup(groupId
);
2000 if (group
!= null && group
.isBlocked()) {
2008 public boolean isContactBlocked(final String identifier
) throws InvalidNumberException
{
2009 final var recipientId
= canonicalizeAndResolveRecipient(identifier
);
2010 return isContactBlocked(recipientId
);
2013 private boolean isContactBlocked(final RecipientId recipientId
) {
2014 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2015 return sourceContact
!= null && sourceContact
.isBlocked();
2018 private boolean isNotAllowedToSendToGroup(
2019 SignalServiceEnvelope envelope
, SignalServiceContent content
2021 SignalServiceAddress source
;
2022 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2023 source
= envelope
.getSourceAddress();
2024 } else if (content
!= null) {
2025 source
= content
.getSender();
2030 if (content
== null || !content
.getDataMessage().isPresent()) {
2034 var message
= content
.getDataMessage().get();
2035 if (!message
.getGroupContext().isPresent()) {
2039 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2040 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2041 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2046 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2047 var group
= getGroup(groupId
);
2048 if (group
== null) {
2052 final var recipientId
= resolveRecipient(source
);
2053 return !group
.isMember(recipientId
) || (
2054 group
.isAnnouncementGroup() && !group
.isAdmin(recipientId
)
2058 private List
<HandleAction
> handleMessage(
2059 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2061 var actions
= new ArrayList
<HandleAction
>();
2062 if (content
!= null) {
2063 final SignalServiceAddress sender
;
2064 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2065 sender
= envelope
.getSourceAddress();
2067 sender
= content
.getSender();
2070 if (content
.getDataMessage().isPresent()) {
2071 var message
= content
.getDataMessage().get();
2073 if (content
.isNeedsReceipt()) {
2074 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2077 actions
.addAll(handleSignalServiceDataMessage(message
,
2080 account
.getSelfAddress(),
2081 ignoreAttachments
));
2083 if (content
.getSyncMessage().isPresent()) {
2084 account
.setMultiDevice(true);
2085 var syncMessage
= content
.getSyncMessage().get();
2086 if (syncMessage
.getSent().isPresent()) {
2087 var message
= syncMessage
.getSent().get();
2088 final var destination
= message
.getDestination().orNull();
2089 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2093 ignoreAttachments
));
2095 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2096 var rm
= syncMessage
.getRequest().get();
2097 if (rm
.isContactsRequest()) {
2098 actions
.add(SendSyncContactsAction
.create());
2100 if (rm
.isGroupsRequest()) {
2101 actions
.add(SendSyncGroupsAction
.create());
2103 if (rm
.isBlockedListRequest()) {
2104 actions
.add(SendSyncBlockedListAction
.create());
2106 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2108 if (syncMessage
.getGroups().isPresent()) {
2109 File tmpFile
= null;
2111 tmpFile
= IOUtils
.createTempFile();
2112 final var groupsMessage
= syncMessage
.getGroups().get();
2113 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2114 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2119 } catch (IOException e
) {
2120 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2126 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2127 if (syncGroup
!= null) {
2128 if (g
.getName().isPresent()) {
2129 syncGroup
.name
= g
.getName().get();
2131 syncGroup
.addMembers(g
.getMembers()
2133 .map(this::resolveRecipient
)
2134 .collect(Collectors
.toSet()));
2135 if (!g
.isActive()) {
2136 syncGroup
.removeMember(account
.getSelfRecipientId());
2138 // Add ourself to the member set as it's marked as active
2139 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2141 syncGroup
.blocked
= g
.isBlocked();
2142 if (g
.getColor().isPresent()) {
2143 syncGroup
.color
= g
.getColor().get();
2146 if (g
.getAvatar().isPresent()) {
2147 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2149 syncGroup
.archived
= g
.isArchived();
2150 account
.getGroupStore().updateGroup(syncGroup
);
2154 } catch (Exception e
) {
2155 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2159 if (tmpFile
!= null) {
2161 Files
.delete(tmpFile
.toPath());
2162 } catch (IOException e
) {
2163 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2170 if (syncMessage
.getBlockedList().isPresent()) {
2171 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2172 for (var address
: blockedListMessage
.getAddresses()) {
2173 setContactBlocked(resolveRecipient(address
), true);
2175 for (var groupId
: blockedListMessage
.getGroupIds()
2177 .map(GroupId
::unknownVersion
)
2178 .collect(Collectors
.toSet())) {
2180 setGroupBlocked(groupId
, true);
2181 } catch (GroupNotFoundException e
) {
2182 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2183 groupId
.toBase64());
2187 if (syncMessage
.getContacts().isPresent()) {
2188 File tmpFile
= null;
2190 tmpFile
= IOUtils
.createTempFile();
2191 final var contactsMessage
= syncMessage
.getContacts().get();
2192 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2193 .asPointer(), tmpFile
)) {
2194 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2199 } catch (IOException e
) {
2200 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2207 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2208 account
.setProfileKey(c
.getProfileKey().get());
2210 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2211 var contact
= account
.getContactStore().getContact(recipientId
);
2212 final var builder
= contact
== null
2213 ? Contact
.newBuilder()
2214 : Contact
.newBuilder(contact
);
2215 if (c
.getName().isPresent()) {
2216 builder
.withName(c
.getName().get());
2218 if (c
.getColor().isPresent()) {
2219 builder
.withColor(c
.getColor().get());
2221 if (c
.getProfileKey().isPresent()) {
2222 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2224 if (c
.getVerified().isPresent()) {
2225 final var verifiedMessage
= c
.getVerified().get();
2226 account
.getIdentityKeyStore()
2227 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2228 verifiedMessage
.getIdentityKey(),
2229 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2231 if (c
.getExpirationTimer().isPresent()) {
2232 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2234 builder
.withBlocked(c
.isBlocked());
2235 builder
.withArchived(c
.isArchived());
2236 account
.getContactStore().storeContact(recipientId
, builder
.build());
2238 if (c
.getAvatar().isPresent()) {
2239 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2243 } catch (Exception e
) {
2244 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2248 if (tmpFile
!= null) {
2250 Files
.delete(tmpFile
.toPath());
2251 } catch (IOException e
) {
2252 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2259 if (syncMessage
.getVerified().isPresent()) {
2260 final var verifiedMessage
= syncMessage
.getVerified().get();
2261 account
.getIdentityKeyStore()
2262 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2263 verifiedMessage
.getIdentityKey(),
2264 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2266 if (syncMessage
.getStickerPackOperations().isPresent()) {
2267 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2268 for (var m
: stickerPackOperationMessages
) {
2269 if (!m
.getPackId().isPresent()) {
2272 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2273 final var installed
= !m
.getType().isPresent()
2274 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
;
2276 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2277 if (m
.getPackKey().isPresent()) {
2278 if (sticker
== null) {
2279 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2282 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, m
.getPackKey().get()));
2286 if (sticker
!= null) {
2287 sticker
.setInstalled(installed
);
2288 account
.getStickerStore().updateSticker(sticker
);
2292 if (syncMessage
.getFetchType().isPresent()) {
2293 switch (syncMessage
.getFetchType().get()) {
2295 getRecipientProfile(account
.getSelfRecipientId(), true);
2296 case STORAGE_MANIFEST
:
2300 if (syncMessage
.getKeys().isPresent()) {
2301 final var keysMessage
= syncMessage
.getKeys().get();
2302 if (keysMessage
.getStorageService().isPresent()) {
2303 final var storageKey
= keysMessage
.getStorageService().get();
2304 account
.setStorageKey(storageKey
);
2307 if (syncMessage
.getConfiguration().isPresent()) {
2315 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2317 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2318 } catch (IOException e
) {
2319 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2323 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2325 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2326 } catch (IOException e
) {
2327 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2331 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2333 avatarStore
.storeGroupAvatar(groupId
,
2334 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2335 } catch (IOException e
) {
2336 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2340 private void downloadProfileAvatar(
2341 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2344 avatarStore
.storeProfileAvatar(address
,
2345 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2346 } catch (Throwable e
) {
2347 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2348 Thread
.currentThread().interrupt();
2350 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2354 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2355 return attachmentStore
.getAttachmentFile(attachmentId
);
2358 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2359 if (!attachment
.isPointer()) {
2360 logger
.warn("Invalid state, can't store an attachment stream.");
2363 var pointer
= attachment
.asPointer();
2364 if (pointer
.getPreview().isPresent()) {
2365 final var preview
= pointer
.getPreview().get();
2367 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2368 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2369 } catch (IOException e
) {
2370 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2375 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2376 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2377 } catch (IOException e
) {
2378 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2382 private void retrieveGroupV2Avatar(
2383 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2384 ) throws IOException
{
2385 var groupOperations
= dependencies
.getGroupsV2Operations().forGroup(groupSecretParams
);
2387 var tmpFile
= IOUtils
.createTempFile();
2388 try (InputStream input
= dependencies
.getMessageReceiver()
2389 .retrieveGroupsV2ProfileAvatar(cdnKey
, tmpFile
, ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2390 var encryptedData
= IOUtils
.readFully(input
);
2392 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2393 outputStream
.write(decryptedData
);
2396 Files
.delete(tmpFile
.toPath());
2397 } catch (IOException e
) {
2398 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2405 private void retrieveProfileAvatar(
2406 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2407 ) throws IOException
{
2408 var tmpFile
= IOUtils
.createTempFile();
2409 try (var input
= dependencies
.getMessageReceiver()
2410 .retrieveProfileAvatar(avatarPath
,
2413 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2414 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2415 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2418 Files
.delete(tmpFile
.toPath());
2419 } catch (IOException e
) {
2420 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2427 private void retrieveAttachment(
2428 final SignalServiceAttachment attachment
, final OutputStream outputStream
2429 ) throws IOException
{
2430 if (attachment
.isPointer()) {
2431 var pointer
= attachment
.asPointer();
2432 retrieveAttachmentPointer(pointer
, outputStream
);
2434 var stream
= attachment
.asStream();
2435 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2439 private void retrieveAttachmentPointer(
2440 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2441 ) throws IOException
{
2442 var tmpFile
= IOUtils
.createTempFile();
2443 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2444 IOUtils
.copyStream(input
, outputStream
);
2445 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2446 throw new IOException(e
);
2449 Files
.delete(tmpFile
.toPath());
2450 } catch (IOException e
) {
2451 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2458 private InputStream
retrieveAttachmentAsStream(
2459 SignalServiceAttachmentPointer pointer
, File tmpFile
2460 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2461 return dependencies
.getMessageReceiver()
2462 .retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2465 void sendGroups() throws IOException
{
2466 var groupsFile
= IOUtils
.createTempFile();
2469 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2470 var out
= new DeviceGroupsOutputStream(fos
);
2471 for (var record : getGroups()) {
2472 if (record instanceof GroupInfoV1
) {
2473 var groupInfo
= (GroupInfoV1
) record;
2474 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2475 Optional
.fromNullable(groupInfo
.name
),
2476 groupInfo
.getMembers()
2478 .map(this::resolveSignalServiceAddress
)
2479 .collect(Collectors
.toList()),
2480 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2481 groupInfo
.isMember(account
.getSelfRecipientId()),
2482 Optional
.of(groupInfo
.messageExpirationTime
),
2483 Optional
.fromNullable(groupInfo
.color
),
2486 groupInfo
.archived
));
2491 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2492 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2493 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2494 .withStream(groupsFileStream
)
2495 .withContentType("application/octet-stream")
2496 .withLength(groupsFile
.length())
2499 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2504 Files
.delete(groupsFile
.toPath());
2505 } catch (IOException e
) {
2506 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2511 public void sendContacts() throws IOException
{
2512 var contactsFile
= IOUtils
.createTempFile();
2515 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2516 var out
= new DeviceContactsOutputStream(fos
);
2517 for (var contactPair
: account
.getContactStore().getContacts()) {
2518 final var recipientId
= contactPair
.first();
2519 final var contact
= contactPair
.second();
2520 final var address
= resolveSignalServiceAddress(recipientId
);
2522 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2523 VerifiedMessage verifiedMessage
= null;
2524 if (currentIdentity
!= null) {
2525 verifiedMessage
= new VerifiedMessage(address
,
2526 currentIdentity
.getIdentityKey(),
2527 currentIdentity
.getTrustLevel().toVerifiedState(),
2528 currentIdentity
.getDateAdded().getTime());
2531 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2532 out
.write(new DeviceContact(address
,
2533 Optional
.fromNullable(contact
.getName()),
2534 createContactAvatarAttachment(address
),
2535 Optional
.fromNullable(contact
.getColor()),
2536 Optional
.fromNullable(verifiedMessage
),
2537 Optional
.fromNullable(profileKey
),
2538 contact
.isBlocked(),
2539 Optional
.of(contact
.getMessageExpirationTime()),
2541 contact
.isArchived()));
2544 if (account
.getProfileKey() != null) {
2545 // Send our own profile key as well
2546 out
.write(new DeviceContact(account
.getSelfAddress(),
2551 Optional
.of(account
.getProfileKey()),
2559 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2560 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2561 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2562 .withStream(contactsFileStream
)
2563 .withContentType("application/octet-stream")
2564 .withLength(contactsFile
.length())
2567 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
,
2573 Files
.delete(contactsFile
.toPath());
2574 } catch (IOException e
) {
2575 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2580 void sendBlockedList() throws IOException
{
2581 var addresses
= new ArrayList
<SignalServiceAddress
>();
2582 for (var record : account
.getContactStore().getContacts()) {
2583 if (record.second().isBlocked()) {
2584 addresses
.add(resolveSignalServiceAddress(record.first()));
2587 var groupIds
= new ArrayList
<byte[]>();
2588 for (var record : getGroups()) {
2589 if (record.isBlocked()) {
2590 groupIds
.add(record.getGroupId().serialize());
2593 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2596 private void sendVerifiedMessage(
2597 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2598 ) throws IOException
{
2599 var verifiedMessage
= new VerifiedMessage(destination
,
2601 trustLevel
.toVerifiedState(),
2602 System
.currentTimeMillis());
2603 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2606 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2607 return account
.getContactStore().getContacts();
2610 public String
getContactOrProfileName(String number
) throws InvalidNumberException
{
2611 final var recipientId
= canonicalizeAndResolveRecipient(number
);
2612 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2613 if (recipient
== null) {
2617 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2618 return recipient
.getContact().getName();
2621 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2622 return recipient
.getProfile().getDisplayName();
2628 public GroupInfo
getGroup(GroupId groupId
) {
2629 return getGroup(groupId
, false);
2632 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2633 final var group
= account
.getGroupStore().getGroup(groupId
);
2634 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2635 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2636 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2637 account
.getGroupStore().updateGroup(group
);
2642 public List
<IdentityInfo
> getIdentities() {
2643 return account
.getIdentityKeyStore().getIdentities();
2646 public List
<IdentityInfo
> getIdentities(String number
) throws InvalidNumberException
{
2647 final var identity
= account
.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number
));
2648 return identity
== null ? List
.of() : List
.of(identity
);
2652 * Trust this the identity with this fingerprint
2654 * @param name username of the identity
2655 * @param fingerprint Fingerprint
2657 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) throws InvalidNumberException
{
2658 var recipientId
= canonicalizeAndResolveRecipient(name
);
2659 return trustIdentity(recipientId
,
2660 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2661 TrustLevel
.TRUSTED_VERIFIED
);
2665 * Trust this the identity with this safety number
2667 * @param name username of the identity
2668 * @param safetyNumber Safety number
2670 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) throws InvalidNumberException
{
2671 var recipientId
= canonicalizeAndResolveRecipient(name
);
2672 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2673 return trustIdentity(recipientId
,
2674 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2675 TrustLevel
.TRUSTED_VERIFIED
);
2679 * Trust this the identity with this scannable safety number
2681 * @param name username of the identity
2682 * @param safetyNumber Scannable safety number
2684 public boolean trustIdentityVerifiedSafetyNumber(String name
, byte[] safetyNumber
) throws InvalidNumberException
{
2685 var recipientId
= canonicalizeAndResolveRecipient(name
);
2686 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2687 return trustIdentity(recipientId
, identityKey
-> {
2688 final var fingerprint
= computeSafetyNumberFingerprint(address
, identityKey
);
2690 return fingerprint
!= null && fingerprint
.getScannableFingerprint().compareTo(safetyNumber
);
2691 } catch (FingerprintVersionMismatchException
| FingerprintParsingException e
) {
2694 }, TrustLevel
.TRUSTED_VERIFIED
);
2698 * Trust all keys of this identity without verification
2700 * @param name username of the identity
2702 public boolean trustIdentityAllKeys(String name
) throws InvalidNumberException
{
2703 var recipientId
= canonicalizeAndResolveRecipient(name
);
2704 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2707 private boolean trustIdentity(
2708 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2710 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2711 if (identity
== null) {
2715 if (!verifier
.apply(identity
.getIdentityKey())) {
2719 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2721 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2722 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2723 } catch (IOException e
) {
2724 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2730 private void handleIdentityFailure(
2731 final RecipientId recipientId
, final SendMessageResult
.IdentityFailure identityFailure
2733 final var identityKey
= identityFailure
.getIdentityKey();
2734 if (identityKey
!= null) {
2735 final var newIdentity
= account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
2737 account
.getSessionStore().archiveSessions(recipientId
);
2740 // Retrieve profile to get the current identity key from the server
2741 retrieveEncryptedProfile(recipientId
);
2745 public String
computeSafetyNumber(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2746 final Fingerprint fingerprint
= computeSafetyNumberFingerprint(theirAddress
, theirIdentityKey
);
2747 return fingerprint
== null ?
null : fingerprint
.getDisplayableFingerprint().getDisplayText();
2750 public byte[] computeSafetyNumberForScanning(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2751 final Fingerprint fingerprint
= computeSafetyNumberFingerprint(theirAddress
, theirIdentityKey
);
2752 return fingerprint
== null ?
null : fingerprint
.getScannableFingerprint().getSerialized();
2755 private Fingerprint
computeSafetyNumberFingerprint(
2756 final SignalServiceAddress theirAddress
, final IdentityKey theirIdentityKey
2758 return Utils
.computeSafetyNumber(capabilities
.isUuid(),
2759 account
.getSelfAddress(),
2760 getIdentityKeyPair().getPublicKey(),
2766 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2767 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2769 return resolveSignalServiceAddress(address
);
2773 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2774 if (address
.matches(account
.getSelfAddress())) {
2775 return account
.getSelfAddress();
2778 return account
.getRecipientStore().resolveServiceAddress(address
);
2781 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2782 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2785 private RecipientId
canonicalizeAndResolveRecipient(String identifier
) throws InvalidNumberException
{
2786 var canonicalizedNumber
= UuidUtil
.isUuid(identifier
) ? identifier
: canonicalizePhoneNumber(identifier
);
2788 return resolveRecipient(canonicalizedNumber
);
2791 private String
canonicalizePhoneNumber(final String number
) throws InvalidNumberException
{
2792 return PhoneNumberFormatter
.formatNumber(number
, account
.getUsername());
2795 private RecipientId
resolveRecipient(final String identifier
) {
2796 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2798 return resolveRecipient(address
);
2801 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2802 return account
.getRecipientStore().resolveRecipient(address
);
2805 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2806 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2809 private void enqueueJob(Job job
) {
2810 var context
= new Context(account
,
2811 dependencies
.getAccountManager(),
2812 dependencies
.getMessageReceiver(),
2818 public void close() throws IOException
{
2822 void close(boolean closeAccount
) throws IOException
{
2823 executor
.shutdown();
2825 dependencies
.getSignalWebSocket().disconnect();
2827 if (closeAccount
&& account
!= null) {
2833 public interface ReceiveMessageHandler
{
2835 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);