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
.RecipientIdentifier
;
22 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
23 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
24 import org
.asamk
.signal
.manager
.api
.TypingAction
;
25 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
26 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
27 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
28 import org
.asamk
.signal
.manager
.groups
.GroupId
;
29 import org
.asamk
.signal
.manager
.groups
.GroupIdV1
;
30 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
31 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
32 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
33 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
34 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
35 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
36 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
37 import org
.asamk
.signal
.manager
.helper
.GroupV2Helper
;
38 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
39 import org
.asamk
.signal
.manager
.helper
.ProfileHelper
;
40 import org
.asamk
.signal
.manager
.helper
.SendHelper
;
41 import org
.asamk
.signal
.manager
.helper
.UnidentifiedAccessHelper
;
42 import org
.asamk
.signal
.manager
.jobs
.Context
;
43 import org
.asamk
.signal
.manager
.jobs
.Job
;
44 import org
.asamk
.signal
.manager
.jobs
.RetrieveStickerPackJob
;
45 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
46 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
47 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
48 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
49 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
50 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
51 import org
.asamk
.signal
.manager
.storage
.messageCache
.CachedMessage
;
52 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
53 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
54 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
55 import org
.asamk
.signal
.manager
.storage
.stickers
.Sticker
;
56 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
57 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
58 import org
.asamk
.signal
.manager
.util
.IOUtils
;
59 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
60 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
61 import org
.asamk
.signal
.manager
.util
.StickerUtils
;
62 import org
.asamk
.signal
.manager
.util
.Utils
;
63 import org
.signal
.libsignal
.metadata
.InvalidMetadataMessageException
;
64 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
65 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
66 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
67 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
68 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
69 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
70 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
71 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
72 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
73 import org
.signal
.libsignal
.metadata
.SelfSendException
;
74 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
75 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
76 import org
.signal
.zkgroup
.InvalidInputException
;
77 import org
.signal
.zkgroup
.VerificationFailedException
;
78 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
79 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
80 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
81 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
82 import org
.slf4j
.Logger
;
83 import org
.slf4j
.LoggerFactory
;
84 import org
.whispersystems
.libsignal
.IdentityKey
;
85 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
86 import org
.whispersystems
.libsignal
.InvalidKeyException
;
87 import org
.whispersystems
.libsignal
.InvalidMessageException
;
88 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
89 import org
.whispersystems
.libsignal
.fingerprint
.Fingerprint
;
90 import org
.whispersystems
.libsignal
.fingerprint
.FingerprintParsingException
;
91 import org
.whispersystems
.libsignal
.fingerprint
.FingerprintVersionMismatchException
;
92 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
93 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
94 import org
.whispersystems
.libsignal
.util
.Pair
;
95 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
96 import org
.whispersystems
.signalservice
.api
.InvalidMessageStructureException
;
97 import org
.whispersystems
.signalservice
.api
.SignalSessionLock
;
98 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
99 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
100 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
101 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
102 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
103 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
104 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
105 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
106 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
107 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
108 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
109 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
110 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupV2
;
111 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
112 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTypingMessage
;
113 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
114 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
115 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
116 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
117 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
118 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
119 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
120 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
121 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
122 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
123 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
124 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
125 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
126 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
127 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
128 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.ConflictException
;
129 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
130 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
131 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
132 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
133 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
134 import org
.whispersystems
.signalservice
.api
.websocket
.WebSocketUnavailableException
;
135 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.Quote
;
136 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedQuoteException
;
137 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
138 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
139 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
140 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
141 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
142 import org
.whispersystems
.signalservice
.internal
.util
.Util
;
144 import java
.io
.Closeable
;
146 import java
.io
.FileInputStream
;
147 import java
.io
.FileOutputStream
;
148 import java
.io
.IOException
;
149 import java
.io
.InputStream
;
150 import java
.io
.OutputStream
;
152 import java
.net
.URISyntaxException
;
153 import java
.net
.URLEncoder
;
154 import java
.nio
.charset
.StandardCharsets
;
155 import java
.nio
.file
.Files
;
156 import java
.security
.SignatureException
;
157 import java
.util
.ArrayList
;
158 import java
.util
.Arrays
;
159 import java
.util
.Base64
;
160 import java
.util
.Collection
;
161 import java
.util
.Date
;
162 import java
.util
.HashMap
;
163 import java
.util
.HashSet
;
164 import java
.util
.List
;
165 import java
.util
.Map
;
166 import java
.util
.Set
;
167 import java
.util
.UUID
;
168 import java
.util
.concurrent
.ExecutorService
;
169 import java
.util
.concurrent
.Executors
;
170 import java
.util
.concurrent
.TimeUnit
;
171 import java
.util
.concurrent
.TimeoutException
;
172 import java
.util
.concurrent
.locks
.ReentrantLock
;
173 import java
.util
.function
.Function
;
174 import java
.util
.stream
.Collectors
;
176 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
178 public class Manager
implements Closeable
{
180 private final static Logger logger
= LoggerFactory
.getLogger(Manager
.class);
182 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
183 private final SignalDependencies dependencies
;
185 private SignalAccount account
;
187 private final ExecutorService executor
= Executors
.newCachedThreadPool();
189 private final ProfileHelper profileHelper
;
190 private final GroupV2Helper groupV2Helper
;
191 private final PinHelper pinHelper
;
192 private final SendHelper sendHelper
;
194 private final AvatarStore avatarStore
;
195 private final AttachmentStore attachmentStore
;
196 private final StickerPackStore stickerPackStore
;
197 private final SignalSessionLock sessionLock
= new SignalSessionLock() {
198 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
201 public Lock
acquire() {
203 return LEGACY_LOCK
::unlock
;
208 SignalAccount account
,
209 PathConfig pathConfig
,
210 ServiceEnvironmentConfig serviceEnvironmentConfig
,
213 this.account
= account
;
214 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
216 final var credentialsProvider
= new DynamicCredentialsProvider(account
.getUuid(),
217 account
.getUsername(),
218 account
.getPassword(),
219 account
.getDeviceId());
220 this.dependencies
= new SignalDependencies(account
.getSelfAddress(),
221 serviceEnvironmentConfig
,
224 account
.getSignalProtocolStore(),
227 this.pinHelper
= new PinHelper(dependencies
.getKeyBackupService());
229 final var unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
::getProfileKey
,
230 account
.getProfileStore()::getProfileKey
,
231 this::getRecipientProfile
,
232 this::getSenderCertificate
);
233 this.profileHelper
= new ProfileHelper(account
.getProfileStore()::getProfileKey
,
234 unidentifiedAccessHelper
::getAccessFor
,
235 dependencies
::getProfileService
,
236 dependencies
::getMessageReceiver
,
237 this::resolveSignalServiceAddress
);
238 this.groupV2Helper
= new GroupV2Helper(this::getRecipientProfileKeyCredential
,
239 this::getRecipientProfile
,
240 account
::getSelfRecipientId
,
241 dependencies
.getGroupsV2Operations(),
242 dependencies
.getGroupsV2Api(),
243 this::getGroupAuthForToday
,
244 this::resolveSignalServiceAddress
);
245 this.avatarStore
= new AvatarStore(pathConfig
.getAvatarsPath());
246 this.attachmentStore
= new AttachmentStore(pathConfig
.getAttachmentsPath());
247 this.stickerPackStore
= new StickerPackStore(pathConfig
.getStickerPacksPath());
248 this.sendHelper
= new SendHelper(account
,
250 unidentifiedAccessHelper
,
251 this::resolveSignalServiceAddress
,
252 this::resolveRecipient
,
253 this::handleIdentityFailure
,
255 this::refreshRegisteredUser
);
258 public String
getUsername() {
259 return account
.getUsername();
262 public SignalServiceAddress
getSelfAddress() {
263 return account
.getSelfAddress();
266 public RecipientId
getSelfRecipientId() {
267 return account
.getSelfRecipientId();
270 private IdentityKeyPair
getIdentityKeyPair() {
271 return account
.getIdentityKeyPair();
274 public int getDeviceId() {
275 return account
.getDeviceId();
278 public static Manager
init(
281 ServiceEnvironment serviceEnvironment
,
283 final TrustNewIdentity trustNewIdentity
284 ) throws IOException
, NotRegisteredException
{
285 var pathConfig
= PathConfig
.createDefault(settingsPath
);
287 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
288 throw new NotRegisteredException();
291 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true, trustNewIdentity
);
293 if (!account
.isRegistered()) {
294 throw new NotRegisteredException();
297 final var serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
299 return new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
302 public static List
<String
> getAllLocalUsernames(File settingsPath
) {
303 var pathConfig
= PathConfig
.createDefault(settingsPath
);
304 final var dataPath
= pathConfig
.getDataPath();
305 final var files
= dataPath
.listFiles();
311 return Arrays
.stream(files
)
312 .filter(File
::isFile
)
314 .filter(file
-> PhoneNumberFormatter
.isValidNumber(file
, null))
315 .collect(Collectors
.toList());
318 public void checkAccountState() throws IOException
{
319 if (account
.getLastReceiveTimestamp() == 0) {
320 logger
.info("The Signal protocol expects that incoming messages are regularly received.");
322 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
323 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
326 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
330 if (dependencies
.getAccountManager().getPreKeysCount() < ServiceConfig
.PREKEY_MINIMUM_COUNT
) {
333 if (account
.getUuid() == null) {
334 account
.setUuid(dependencies
.getAccountManager().getOwnUuid());
336 updateAccountAttributes();
340 * This is used for checking a set of phone numbers for registration on Signal
342 * @param numbers The set of phone number in question
343 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
344 * @throws IOException if its unable to get the contacts to check if they're registered
346 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
347 Map
<String
, String
> canonicalizedNumbers
= numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
349 return canonicalizePhoneNumber(n
);
350 } catch (InvalidNumberException e
) {
355 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
356 var contactDetails
= getRegisteredUsers(canonicalizedNumbers
.values()
358 .filter(s
-> !s
.isEmpty())
359 .collect(Collectors
.toSet()));
361 return numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
362 final var number
= canonicalizedNumbers
.get(n
);
363 final var uuid
= contactDetails
.get(number
);
364 return new Pair
<>(number
.isEmpty() ?
null : number
, uuid
);
368 public void updateAccountAttributes() throws IOException
{
369 dependencies
.getAccountManager()
370 .setAccountAttributes(account
.getEncryptedDeviceName(),
372 account
.getLocalRegistrationId(),
374 // set legacy pin only if no KBS master key is set
375 account
.getPinMasterKey() == null ? account
.getRegistrationLockPin() : null,
376 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
377 account
.getSelfUnidentifiedAccessKey(),
378 account
.isUnrestrictedUnidentifiedAccess(),
380 account
.isDiscoverableByPhoneNumber());
384 * @param givenName if null, the previous givenName will be kept
385 * @param familyName if null, the previous familyName will be kept
386 * @param about if null, the previous about text will be kept
387 * @param aboutEmoji if null, the previous about emoji will be kept
388 * @param avatar if avatar is null the image from the local avatar store is used (if present),
390 public void setProfile(
391 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
392 ) throws IOException
{
393 var profile
= getRecipientProfile(account
.getSelfRecipientId());
394 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
395 if (givenName
!= null) {
396 builder
.withGivenName(givenName
);
398 if (familyName
!= null) {
399 builder
.withFamilyName(familyName
);
402 builder
.withAbout(about
);
404 if (aboutEmoji
!= null) {
405 builder
.withAboutEmoji(aboutEmoji
);
407 var newProfile
= builder
.build();
409 try (final var streamDetails
= avatar
== null
410 ? avatarStore
.retrieveProfileAvatar(getSelfAddress())
411 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
412 dependencies
.getAccountManager()
413 .setVersionedProfile(account
.getUuid(),
414 account
.getProfileKey(),
415 newProfile
.getInternalServiceName(),
416 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
417 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
422 if (avatar
!= null) {
423 if (avatar
.isPresent()) {
424 avatarStore
.storeProfileAvatar(getSelfAddress(),
425 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
427 avatarStore
.deleteProfileAvatar(getSelfAddress());
430 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
432 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
435 public void unregister() throws IOException
{
436 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
437 // If this is the master device, other users can't send messages to this number anymore.
438 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
439 dependencies
.getAccountManager().setGcmId(Optional
.absent());
441 account
.setRegistered(false);
444 public void deleteAccount() throws IOException
{
445 dependencies
.getAccountManager().deleteAccount();
447 account
.setRegistered(false);
450 public List
<Device
> getLinkedDevices() throws IOException
{
451 var devices
= dependencies
.getAccountManager().getDevices();
452 account
.setMultiDevice(devices
.size() > 1);
453 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
454 return devices
.stream().map(d
-> {
455 String deviceName
= d
.getName();
456 if (deviceName
!= null) {
458 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
459 } catch (IOException e
) {
460 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
463 return new Device(d
.getId(), deviceName
, d
.getCreated(), d
.getLastSeen());
464 }).collect(Collectors
.toList());
467 public void removeLinkedDevices(int deviceId
) throws IOException
{
468 dependencies
.getAccountManager().removeDevice(deviceId
);
469 var devices
= dependencies
.getAccountManager().getDevices();
470 account
.setMultiDevice(devices
.size() > 1);
473 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
474 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
476 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
479 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
480 var identityKeyPair
= getIdentityKeyPair();
481 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
483 dependencies
.getAccountManager()
484 .addDevice(deviceIdentifier
,
487 Optional
.of(account
.getProfileKey().serialize()),
489 account
.setMultiDevice(true);
492 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
493 if (!account
.isMasterDevice()) {
494 throw new RuntimeException("Only master device can set a PIN");
496 if (pin
.isPresent()) {
497 final var masterKey
= account
.getPinMasterKey() != null
498 ? account
.getPinMasterKey()
499 : KeyUtils
.createMasterKey();
501 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
503 account
.setRegistrationLockPin(pin
.get(), masterKey
);
506 pinHelper
.removeRegistrationLockPin();
508 account
.setRegistrationLockPin(null, null);
512 void refreshPreKeys() throws IOException
{
513 var oneTimePreKeys
= generatePreKeys();
514 final var identityKeyPair
= getIdentityKeyPair();
515 var signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
517 dependencies
.getAccountManager().setPreKeys(identityKeyPair
.getPublicKey(), signedPreKeyRecord
, oneTimePreKeys
);
520 private List
<PreKeyRecord
> generatePreKeys() {
521 final var offset
= account
.getPreKeyIdOffset();
523 var records
= KeyUtils
.generatePreKeyRecords(offset
, ServiceConfig
.PREKEY_BATCH_SIZE
);
524 account
.addPreKeys(records
);
529 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
530 final var signedPreKeyId
= account
.getNextSignedPreKeyId();
532 var record = KeyUtils
.generateSignedPreKeyRecord(identityKeyPair
, signedPreKeyId
);
533 account
.addSignedPreKey(record);
538 public Profile
getRecipientProfile(
539 RecipientId recipientId
541 return getRecipientProfile(recipientId
, false);
544 private final Set
<RecipientId
> pendingProfileRequest
= new HashSet
<>();
546 Profile
getRecipientProfile(
547 RecipientId recipientId
, boolean force
549 var profile
= account
.getProfileStore().getProfile(recipientId
);
551 var now
= System
.currentTimeMillis();
552 // Profiles are cached for 24h before retrieving them again, unless forced
553 if (!force
&& profile
!= null && now
- profile
.getLastUpdateTimestamp() < 24 * 60 * 60 * 1000) {
557 synchronized (pendingProfileRequest
) {
558 if (pendingProfileRequest
.contains(recipientId
)) {
561 pendingProfileRequest
.add(recipientId
);
563 final SignalServiceProfile encryptedProfile
;
565 encryptedProfile
= retrieveEncryptedProfile(recipientId
);
567 synchronized (pendingProfileRequest
) {
568 pendingProfileRequest
.remove(recipientId
);
571 if (encryptedProfile
== null) {
575 profile
= decryptProfileIfKeyKnown(recipientId
, encryptedProfile
);
576 account
.getProfileStore().storeProfile(recipientId
, profile
);
581 private Profile
decryptProfileIfKeyKnown(
582 final RecipientId recipientId
, final SignalServiceProfile encryptedProfile
584 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
585 if (profileKey
== null) {
586 return new Profile(System
.currentTimeMillis(),
591 ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null),
592 ProfileUtils
.getCapabilities(encryptedProfile
));
595 return decryptProfileAndDownloadAvatar(recipientId
, profileKey
, encryptedProfile
);
598 private SignalServiceProfile
retrieveEncryptedProfile(RecipientId recipientId
) {
600 return retrieveProfileAndCredential(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).getProfile();
601 } catch (IOException e
) {
602 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
607 private ProfileAndCredential
retrieveProfileAndCredential(
608 final RecipientId recipientId
, final SignalServiceProfile
.RequestType requestType
609 ) throws IOException
{
610 final var profileAndCredential
= profileHelper
.retrieveProfileSync(recipientId
, requestType
);
611 final var profile
= profileAndCredential
.getProfile();
614 var newIdentity
= account
.getIdentityKeyStore()
615 .saveIdentity(recipientId
,
616 new IdentityKey(Base64
.getDecoder().decode(profile
.getIdentityKey())),
620 account
.getSessionStore().archiveSessions(recipientId
);
622 } catch (InvalidKeyException ignored
) {
623 logger
.warn("Got invalid identity key in profile for {}",
624 resolveSignalServiceAddress(recipientId
).getIdentifier());
626 return profileAndCredential
;
629 private ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
630 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
631 if (profileKeyCredential
!= null) {
632 return profileKeyCredential
;
635 ProfileAndCredential profileAndCredential
;
637 profileAndCredential
= retrieveProfileAndCredential(recipientId
,
638 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
);
639 } catch (IOException e
) {
640 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
644 profileKeyCredential
= profileAndCredential
.getProfileKeyCredential().orNull();
645 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
647 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
648 if (profileKey
!= null) {
649 final var profile
= decryptProfileAndDownloadAvatar(recipientId
,
651 profileAndCredential
.getProfile());
652 account
.getProfileStore().storeProfile(recipientId
, profile
);
655 return profileKeyCredential
;
658 private Profile
decryptProfileAndDownloadAvatar(
659 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
661 if (encryptedProfile
.getAvatar() != null) {
662 downloadProfileAvatar(resolveSignalServiceAddress(recipientId
), encryptedProfile
.getAvatar(), profileKey
);
665 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
668 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(GroupId groupId
) throws IOException
{
669 final var streamDetails
= avatarStore
.retrieveGroupAvatar(groupId
);
670 if (streamDetails
== null) {
671 return Optional
.absent();
674 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
677 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(SignalServiceAddress address
) throws IOException
{
678 final var streamDetails
= avatarStore
.retrieveContactAvatar(address
);
679 if (streamDetails
== null) {
680 return Optional
.absent();
683 return Optional
.of(AttachmentUtils
.createAttachment(streamDetails
, Optional
.absent()));
686 private GroupInfo
getGroupForUpdating(GroupId groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
687 var g
= getGroup(groupId
);
689 throw new GroupNotFoundException(groupId
);
691 if (!g
.isMember(account
.getSelfRecipientId()) && !g
.isPendingMember(account
.getSelfRecipientId())) {
692 throw new NotAGroupMemberException(groupId
, g
.getTitle());
697 public List
<GroupInfo
> getGroups() {
698 return account
.getGroupStore().getGroups();
701 public SendGroupMessageResults
sendQuitGroupMessage(
702 GroupId groupId
, Set
<RecipientIdentifier
.Single
> groupAdmins
703 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
704 var group
= getGroupForUpdating(groupId
);
705 if (group
instanceof GroupInfoV1
) {
706 return quitGroupV1((GroupInfoV1
) group
);
709 final var newAdmins
= getRecipientIds(groupAdmins
);
711 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
712 } catch (ConflictException e
) {
713 // Detected conflicting update, refreshing group and trying again
714 group
= getGroup(groupId
, true);
715 return quitGroupV2((GroupInfoV2
) group
, newAdmins
);
719 private SendGroupMessageResults
quitGroupV1(final GroupInfoV1 groupInfoV1
) throws IOException
{
720 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
721 .withId(groupInfoV1
.getGroupId().serialize())
724 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
);
725 groupInfoV1
.removeMember(account
.getSelfRecipientId());
726 account
.getGroupStore().updateGroup(groupInfoV1
);
727 return sendGroupMessage(messageBuilder
,
728 groupInfoV1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
731 private SendGroupMessageResults
quitGroupV2(
732 final GroupInfoV2 groupInfoV2
, final Set
<RecipientId
> newAdmins
733 ) throws LastGroupAdminException
, IOException
{
734 final var currentAdmins
= groupInfoV2
.getAdminMembers();
735 newAdmins
.removeAll(currentAdmins
);
736 newAdmins
.retainAll(groupInfoV2
.getMembers());
737 if (currentAdmins
.contains(getSelfRecipientId())
738 && currentAdmins
.size() == 1
739 && groupInfoV2
.getMembers().size() > 1
740 && newAdmins
.size() == 0) {
741 // Last admin can't leave the group, unless she's also the last member
742 throw new LastGroupAdminException(groupInfoV2
.getGroupId(), groupInfoV2
.getTitle());
744 final var groupGroupChangePair
= groupV2Helper
.leaveGroup(groupInfoV2
, newAdmins
);
745 groupInfoV2
.setGroup(groupGroupChangePair
.first(), this::resolveRecipient
);
746 account
.getGroupStore().updateGroup(groupInfoV2
);
748 var messageBuilder
= getGroupUpdateMessageBuilder(groupInfoV2
, groupGroupChangePair
.second().toByteArray());
749 return sendGroupMessage(messageBuilder
,
750 groupInfoV2
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
753 public void deleteGroup(GroupId groupId
) throws IOException
{
754 account
.getGroupStore().deleteGroup(groupId
);
755 avatarStore
.deleteGroupAvatar(groupId
);
758 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
759 String name
, Set
<RecipientIdentifier
.Single
> members
, File avatarFile
760 ) throws IOException
, AttachmentInvalidException
{
761 return createGroupInternal(name
, members
== null ?
null : getRecipientIds(members
), avatarFile
);
764 private Pair
<GroupId
, SendGroupMessageResults
> createGroupInternal(
765 String name
, Set
<RecipientId
> members
, File avatarFile
766 ) throws IOException
, AttachmentInvalidException
{
767 final var selfRecipientId
= account
.getSelfRecipientId();
768 if (members
!= null && members
.contains(selfRecipientId
)) {
769 members
= new HashSet
<>(members
);
770 members
.remove(selfRecipientId
);
773 var gv2Pair
= groupV2Helper
.createGroup(name
== null ?
"" : name
,
774 members
== null ? Set
.of() : members
,
777 if (gv2Pair
== null) {
778 // Failed to create v2 group, creating v1 group instead
779 var gv1
= new GroupInfoV1(GroupIdV1
.createRandom());
780 gv1
.addMembers(List
.of(selfRecipientId
));
781 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
782 return new Pair
<>(gv1
.getGroupId(), result
);
785 final var gv2
= gv2Pair
.first();
786 final var decryptedGroup
= gv2Pair
.second();
788 gv2
.setGroup(decryptedGroup
, this::resolveRecipient
);
789 if (avatarFile
!= null) {
790 avatarStore
.storeGroupAvatar(gv2
.getGroupId(),
791 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
794 account
.getGroupStore().updateGroup(gv2
);
796 final var messageBuilder
= getGroupUpdateMessageBuilder(gv2
, null);
798 final var result
= sendGroupMessage(messageBuilder
, gv2
.getMembersIncludingPendingWithout(selfRecipientId
));
799 return new Pair
<>(gv2
.getGroupId(), result
);
802 public SendGroupMessageResults
updateGroup(
806 Set
<RecipientIdentifier
.Single
> members
,
807 Set
<RecipientIdentifier
.Single
> removeMembers
,
808 Set
<RecipientIdentifier
.Single
> admins
,
809 Set
<RecipientIdentifier
.Single
> removeAdmins
,
810 boolean resetGroupLink
,
811 GroupLinkState groupLinkState
,
812 GroupPermission addMemberPermission
,
813 GroupPermission editDetailsPermission
,
815 Integer expirationTimer
,
816 Boolean isAnnouncementGroup
817 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
818 return updateGroupInternal(groupId
,
821 members
== null ?
null : getRecipientIds(members
),
822 removeMembers
== null ?
null : getRecipientIds(removeMembers
),
823 admins
== null ?
null : getRecipientIds(admins
),
824 removeAdmins
== null ?
null : getRecipientIds(removeAdmins
),
828 editDetailsPermission
,
831 isAnnouncementGroup
);
834 private SendGroupMessageResults
updateGroupInternal(
835 final GroupId groupId
,
837 final String description
,
838 final Set
<RecipientId
> members
,
839 final Set
<RecipientId
> removeMembers
,
840 final Set
<RecipientId
> admins
,
841 final Set
<RecipientId
> removeAdmins
,
842 final boolean resetGroupLink
,
843 final GroupLinkState groupLinkState
,
844 final GroupPermission addMemberPermission
,
845 final GroupPermission editDetailsPermission
,
846 final File avatarFile
,
847 final Integer expirationTimer
,
848 final Boolean isAnnouncementGroup
849 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
{
850 var group
= getGroupForUpdating(groupId
);
852 if (group
instanceof GroupInfoV2
) {
854 return updateGroupV2((GroupInfoV2
) group
,
864 editDetailsPermission
,
867 isAnnouncementGroup
);
868 } catch (ConflictException e
) {
869 // Detected conflicting update, refreshing group and trying again
870 group
= getGroup(groupId
, true);
871 return updateGroupV2((GroupInfoV2
) group
,
881 editDetailsPermission
,
884 isAnnouncementGroup
);
888 final var gv1
= (GroupInfoV1
) group
;
889 final var result
= updateGroupV1(gv1
, name
, members
, avatarFile
);
890 if (expirationTimer
!= null) {
891 setExpirationTimer(gv1
, expirationTimer
);
896 private SendGroupMessageResults
updateGroupV1(
897 final GroupInfoV1 gv1
, final String name
, final Set
<RecipientId
> members
, final File avatarFile
898 ) throws IOException
, AttachmentInvalidException
{
899 updateGroupV1Details(gv1
, name
, members
, avatarFile
);
901 account
.getGroupStore().updateGroup(gv1
);
903 var messageBuilder
= getGroupUpdateMessageBuilder(gv1
);
904 return sendGroupMessage(messageBuilder
, gv1
.getMembersIncludingPendingWithout(account
.getSelfRecipientId()));
907 private void updateGroupV1Details(
908 final GroupInfoV1 g
, final String name
, final Collection
<RecipientId
> members
, final File avatarFile
909 ) throws IOException
{
914 if (members
!= null) {
915 final var newMemberAddresses
= members
.stream()
916 .filter(member
-> !g
.isMember(member
))
917 .map(this::resolveSignalServiceAddress
)
918 .collect(Collectors
.toList());
919 final var newE164Members
= new HashSet
<String
>();
920 for (var member
: newMemberAddresses
) {
921 if (!member
.getNumber().isPresent()) {
924 newE164Members
.add(member
.getNumber().get());
927 final var registeredUsers
= getRegisteredUsers(newE164Members
);
928 if (registeredUsers
.size() != newE164Members
.size()) {
929 // Some of the new members are not registered on Signal
930 newE164Members
.removeAll(registeredUsers
.keySet());
931 throw new IOException("Failed to add members "
932 + String
.join(", ", newE164Members
)
933 + " to group: Not registered on Signal");
936 g
.addMembers(members
);
939 if (avatarFile
!= null) {
940 avatarStore
.storeGroupAvatar(g
.getGroupId(),
941 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
945 private SendGroupMessageResults
updateGroupV2(
946 final GroupInfoV2 group
,
948 final String description
,
949 final Set
<RecipientId
> members
,
950 final Set
<RecipientId
> removeMembers
,
951 final Set
<RecipientId
> admins
,
952 final Set
<RecipientId
> removeAdmins
,
953 final boolean resetGroupLink
,
954 final GroupLinkState groupLinkState
,
955 final GroupPermission addMemberPermission
,
956 final GroupPermission editDetailsPermission
,
957 final File avatarFile
,
958 final Integer expirationTimer
,
959 final Boolean isAnnouncementGroup
960 ) throws IOException
{
961 SendGroupMessageResults result
= null;
962 if (group
.isPendingMember(account
.getSelfRecipientId())) {
963 var groupGroupChangePair
= groupV2Helper
.acceptInvite(group
);
964 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
967 if (members
!= null) {
968 final var newMembers
= new HashSet
<>(members
);
969 newMembers
.removeAll(group
.getMembers());
970 if (newMembers
.size() > 0) {
971 var groupGroupChangePair
= groupV2Helper
.addMembers(group
, newMembers
);
972 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
976 if (removeMembers
!= null) {
977 var existingRemoveMembers
= new HashSet
<>(removeMembers
);
978 existingRemoveMembers
.retainAll(group
.getMembers());
979 existingRemoveMembers
.remove(getSelfRecipientId());// self can be removed with sendQuitGroupMessage
980 if (existingRemoveMembers
.size() > 0) {
981 var groupGroupChangePair
= groupV2Helper
.removeMembers(group
, existingRemoveMembers
);
982 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
985 var pendingRemoveMembers
= new HashSet
<>(removeMembers
);
986 pendingRemoveMembers
.retainAll(group
.getPendingMembers());
987 if (pendingRemoveMembers
.size() > 0) {
988 var groupGroupChangePair
= groupV2Helper
.revokeInvitedMembers(group
, pendingRemoveMembers
);
989 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
993 if (admins
!= null) {
994 final var newAdmins
= new HashSet
<>(admins
);
995 newAdmins
.retainAll(group
.getMembers());
996 newAdmins
.removeAll(group
.getAdminMembers());
997 if (newAdmins
.size() > 0) {
998 for (var admin
: newAdmins
) {
999 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, true);
1000 result
= sendUpdateGroupV2Message(group
,
1001 groupGroupChangePair
.first(),
1002 groupGroupChangePair
.second());
1007 if (removeAdmins
!= null) {
1008 final var existingRemoveAdmins
= new HashSet
<>(removeAdmins
);
1009 existingRemoveAdmins
.retainAll(group
.getAdminMembers());
1010 if (existingRemoveAdmins
.size() > 0) {
1011 for (var admin
: existingRemoveAdmins
) {
1012 var groupGroupChangePair
= groupV2Helper
.setMemberAdmin(group
, admin
, false);
1013 result
= sendUpdateGroupV2Message(group
,
1014 groupGroupChangePair
.first(),
1015 groupGroupChangePair
.second());
1020 if (resetGroupLink
) {
1021 var groupGroupChangePair
= groupV2Helper
.resetGroupLinkPassword(group
);
1022 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1025 if (groupLinkState
!= null) {
1026 var groupGroupChangePair
= groupV2Helper
.setGroupLinkState(group
, groupLinkState
);
1027 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1030 if (addMemberPermission
!= null) {
1031 var groupGroupChangePair
= groupV2Helper
.setAddMemberPermission(group
, addMemberPermission
);
1032 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1035 if (editDetailsPermission
!= null) {
1036 var groupGroupChangePair
= groupV2Helper
.setEditDetailsPermission(group
, editDetailsPermission
);
1037 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1040 if (expirationTimer
!= null) {
1041 var groupGroupChangePair
= groupV2Helper
.setMessageExpirationTimer(group
, expirationTimer
);
1042 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1045 if (isAnnouncementGroup
!= null) {
1046 var groupGroupChangePair
= groupV2Helper
.setIsAnnouncementGroup(group
, isAnnouncementGroup
);
1047 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1050 if (name
!= null || description
!= null || avatarFile
!= null) {
1051 var groupGroupChangePair
= groupV2Helper
.updateGroup(group
, name
, description
, avatarFile
);
1052 if (avatarFile
!= null) {
1053 avatarStore
.storeGroupAvatar(group
.getGroupId(),
1054 outputStream
-> IOUtils
.copyFileToStream(avatarFile
, outputStream
));
1056 result
= sendUpdateGroupV2Message(group
, groupGroupChangePair
.first(), groupGroupChangePair
.second());
1062 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(
1063 GroupInviteLinkUrl inviteLinkUrl
1064 ) throws IOException
, GroupLinkNotActiveException
{
1065 final var groupJoinInfo
= groupV2Helper
.getDecryptedGroupJoinInfo(inviteLinkUrl
.getGroupMasterKey(),
1066 inviteLinkUrl
.getPassword());
1067 final var groupChange
= groupV2Helper
.joinGroup(inviteLinkUrl
.getGroupMasterKey(),
1068 inviteLinkUrl
.getPassword(),
1070 final var group
= getOrMigrateGroup(inviteLinkUrl
.getGroupMasterKey(),
1071 groupJoinInfo
.getRevision() + 1,
1072 groupChange
.toByteArray());
1074 if (group
.getGroup() == null) {
1075 // Only requested member, can't send update to group members
1076 return new Pair
<>(group
.getGroupId(), new SendGroupMessageResults(0, List
.of()));
1079 final var result
= sendUpdateGroupV2Message(group
, group
.getGroup(), groupChange
);
1081 return new Pair
<>(group
.getGroupId(), result
);
1084 private SendGroupMessageResults
sendUpdateGroupV2Message(
1085 GroupInfoV2 group
, DecryptedGroup newDecryptedGroup
, GroupChange groupChange
1086 ) throws IOException
{
1087 final var selfRecipientId
= account
.getSelfRecipientId();
1088 final var members
= group
.getMembersIncludingPendingWithout(selfRecipientId
);
1089 group
.setGroup(newDecryptedGroup
, this::resolveRecipient
);
1090 members
.addAll(group
.getMembersIncludingPendingWithout(selfRecipientId
));
1091 account
.getGroupStore().updateGroup(group
);
1093 final var messageBuilder
= getGroupUpdateMessageBuilder(group
, groupChange
.toByteArray());
1094 return sendGroupMessage(messageBuilder
, members
);
1097 public SendMessageResults
sendMessage(
1098 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientIdentifier
> recipients
1099 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1100 var results
= new HashMap
<RecipientIdentifier
, List
<SendMessageResult
>>();
1101 long timestamp
= System
.currentTimeMillis();
1102 messageBuilder
.withTimestamp(timestamp
);
1103 for (final var recipient
: recipients
) {
1104 if (recipient
instanceof RecipientIdentifier
.Single
) {
1105 final var recipientId
= resolveRecipient((RecipientIdentifier
.Single
) recipient
);
1106 final var result
= sendHelper
.sendMessage(messageBuilder
, recipientId
);
1107 results
.put(recipient
, List
.of(result
));
1108 } else if (recipient
instanceof RecipientIdentifier
.NoteToSelf
) {
1109 final var result
= sendHelper
.sendSelfMessage(messageBuilder
);
1110 results
.put(recipient
, List
.of(result
));
1111 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
1112 final var groupId
= ((RecipientIdentifier
.Group
) recipient
).groupId
;
1113 final var result
= sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1114 results
.put(recipient
, result
);
1117 return new SendMessageResults(timestamp
, results
);
1120 public void sendTypingMessage(
1121 SignalServiceTypingMessage
.Action action
, Set
<RecipientIdentifier
> recipients
1122 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
{
1123 final var timestamp
= System
.currentTimeMillis();
1124 for (var recipient
: recipients
) {
1125 if (recipient
instanceof RecipientIdentifier
.Single
) {
1126 final var message
= new SignalServiceTypingMessage(action
, timestamp
, Optional
.absent());
1127 final var recipientId
= resolveRecipient((RecipientIdentifier
.Single
) recipient
);
1128 sendHelper
.sendTypingMessage(message
, recipientId
);
1129 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
1130 final var groupId
= ((RecipientIdentifier
.Group
) recipient
).groupId
;
1131 final var message
= new SignalServiceTypingMessage(action
, timestamp
, Optional
.of(groupId
.serialize()));
1132 sendHelper
.sendGroupTypingMessage(message
, groupId
);
1137 private SendGroupMessageResults
sendGroupMessage(
1138 final SignalServiceDataMessage
.Builder messageBuilder
, final Set
<RecipientId
> members
1139 ) throws IOException
{
1140 final var timestamp
= System
.currentTimeMillis();
1141 messageBuilder
.withTimestamp(timestamp
);
1142 final var results
= sendHelper
.sendGroupMessage(messageBuilder
.build(), members
);
1143 return new SendGroupMessageResults(timestamp
, results
);
1146 private static int currentTimeDays() {
1147 return (int) TimeUnit
.MILLISECONDS
.toDays(System
.currentTimeMillis());
1150 private GroupsV2AuthorizationString
getGroupAuthForToday(
1151 final GroupSecretParams groupSecretParams
1152 ) throws IOException
{
1153 final var today
= currentTimeDays();
1154 // Returns credentials for the next 7 days
1155 final var credentials
= dependencies
.getGroupsV2Api().getCredentials(today
);
1156 // TODO cache credentials until they expire
1157 var authCredentialResponse
= credentials
.get(today
);
1159 return dependencies
.getGroupsV2Api()
1160 .getGroupsV2AuthorizationString(account
.getUuid(),
1163 authCredentialResponse
);
1164 } catch (VerificationFailedException e
) {
1165 throw new IOException(e
);
1169 SendGroupMessageResults
sendGroupInfoMessage(
1170 GroupIdV1 groupId
, SignalServiceAddress recipient
1171 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, AttachmentInvalidException
{
1173 var group
= getGroupForUpdating(groupId
);
1174 if (!(group
instanceof GroupInfoV1
)) {
1175 throw new IOException("Received an invalid group request for a v2 group!");
1177 g
= (GroupInfoV1
) group
;
1179 final var recipientId
= resolveRecipient(recipient
);
1180 if (!g
.isMember(recipientId
)) {
1181 throw new NotAGroupMemberException(groupId
, g
.name
);
1184 var messageBuilder
= getGroupUpdateMessageBuilder(g
);
1186 // Send group message only to the recipient who requested it
1187 return sendGroupMessage(messageBuilder
, Set
.of(recipientId
));
1190 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV1 g
) throws AttachmentInvalidException
{
1191 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
1192 .withId(g
.getGroupId().serialize())
1194 .withMembers(g
.getMembers()
1196 .map(this::resolveSignalServiceAddress
)
1197 .collect(Collectors
.toList()));
1200 final var attachment
= createGroupAvatarAttachment(g
.getGroupId());
1201 if (attachment
.isPresent()) {
1202 group
.withAvatar(attachment
.get());
1204 } catch (IOException e
) {
1205 throw new AttachmentInvalidException(g
.getGroupId().toBase64(), e
);
1208 return SignalServiceDataMessage
.newBuilder()
1209 .asGroupMessage(group
.build())
1210 .withExpiration(g
.getMessageExpirationTime());
1213 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfoV2 g
, byte[] signedGroupChange
) {
1214 var group
= SignalServiceGroupV2
.newBuilder(g
.getMasterKey())
1215 .withRevision(g
.getGroup().getRevision())
1216 .withSignedGroupChange(signedGroupChange
);
1217 return SignalServiceDataMessage
.newBuilder()
1218 .asGroupMessage(group
.build())
1219 .withExpiration(g
.getMessageExpirationTime());
1222 SendGroupMessageResults
sendGroupInfoRequest(
1223 GroupIdV1 groupId
, SignalServiceAddress recipient
1224 ) throws IOException
{
1225 var group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
).withId(groupId
.serialize());
1227 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asGroupMessage(group
.build());
1229 // Send group info request message to the recipient who sent us a message with this groupId
1230 return sendGroupMessage(messageBuilder
, Set
.of(resolveRecipient(recipient
)));
1233 public void sendReadReceipt(
1234 RecipientIdentifier
.Single sender
, List
<Long
> messageIds
1235 ) throws IOException
, UntrustedIdentityException
{
1236 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.READ
,
1238 System
.currentTimeMillis());
1240 sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(sender
));
1243 public void sendViewedReceipt(
1244 RecipientIdentifier
.Single sender
, List
<Long
> messageIds
1245 ) throws IOException
, UntrustedIdentityException
{
1246 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.VIEWED
,
1248 System
.currentTimeMillis());
1250 sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(sender
));
1253 void sendDeliveryReceipt(
1254 SignalServiceAddress remoteAddress
, List
<Long
> messageIds
1255 ) throws IOException
, UntrustedIdentityException
{
1256 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.DELIVERY
,
1258 System
.currentTimeMillis());
1260 sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(remoteAddress
));
1263 public SendMessageResults
sendMessage(
1264 Message message
, Set
<RecipientIdentifier
> recipients
1265 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
{
1266 final var messageBuilder
= SignalServiceDataMessage
.newBuilder();
1267 applyMessage(messageBuilder
, message
);
1268 return sendMessage(messageBuilder
, recipients
);
1271 private void applyMessage(
1272 final SignalServiceDataMessage
.Builder messageBuilder
, final Message message
1273 ) throws AttachmentInvalidException
, IOException
{
1274 messageBuilder
.withBody(message
.getMessageText());
1275 if (message
.getAttachments() != null) {
1276 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(message
.getAttachments());
1278 // Upload attachments here, so we only upload once even for multiple recipients
1279 var messageSender
= dependencies
.getMessageSender();
1280 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
1281 for (var attachment
: attachmentStreams
) {
1282 if (attachment
.isStream()) {
1283 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
1284 } else if (attachment
.isPointer()) {
1285 attachmentPointers
.add(attachment
.asPointer());
1289 messageBuilder
.withAttachments(attachmentPointers
);
1293 public SendMessageResults
sendRemoteDeleteMessage(
1294 long targetSentTimestamp
, Set
<RecipientIdentifier
> recipients
1295 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1296 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
1297 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
1298 return sendMessage(messageBuilder
, recipients
);
1301 public SendMessageResults
sendMessageReaction(
1304 RecipientIdentifier
.Single targetAuthor
,
1305 long targetSentTimestamp
,
1306 Set
<RecipientIdentifier
> recipients
1307 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1308 var targetAuthorRecipientId
= resolveRecipient(targetAuthor
);
1309 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
1311 resolveSignalServiceAddress(targetAuthorRecipientId
),
1312 targetSentTimestamp
);
1313 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
1314 return sendMessage(messageBuilder
, recipients
);
1317 public SendMessageResults
sendEndSessionMessage(Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
1318 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
1321 return sendMessage(messageBuilder
,
1322 recipients
.stream().map(RecipientIdentifier
.class::cast
).collect(Collectors
.toSet()));
1323 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
1324 throw new AssertionError(e
);
1326 for (var recipient
: recipients
) {
1327 final var recipientId
= resolveRecipient((RecipientIdentifier
.Single
) recipient
);
1328 handleEndSession(recipientId
);
1333 void renewSession(RecipientId recipientId
) throws IOException
{
1334 account
.getSessionStore().archiveSessions(recipientId
);
1335 if (!recipientId
.equals(getSelfRecipientId())) {
1336 sendHelper
.sendNullMessage(recipientId
);
1340 public void setContactName(
1341 RecipientIdentifier
.Single recipient
, String name
1342 ) throws NotMasterDeviceException
{
1343 if (!account
.isMasterDevice()) {
1344 throw new NotMasterDeviceException();
1346 final var recipientId
= resolveRecipient(recipient
);
1347 var contact
= account
.getContactStore().getContact(recipientId
);
1348 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1349 account
.getContactStore().storeContact(recipientId
, builder
.withName(name
).build());
1352 public void setContactBlocked(
1353 RecipientIdentifier
.Single recipient
, boolean blocked
1354 ) throws NotMasterDeviceException
{
1355 if (!account
.isMasterDevice()) {
1356 throw new NotMasterDeviceException();
1358 setContactBlocked(resolveRecipient(recipient
), blocked
);
1361 private void setContactBlocked(RecipientId recipientId
, boolean blocked
) {
1362 var contact
= account
.getContactStore().getContact(recipientId
);
1363 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1364 account
.getContactStore().storeContact(recipientId
, builder
.withBlocked(blocked
).build());
1367 public void setGroupBlocked(final GroupId groupId
, final boolean blocked
) throws GroupNotFoundException
{
1368 var group
= getGroup(groupId
);
1369 if (group
== null) {
1370 throw new GroupNotFoundException(groupId
);
1373 group
.setBlocked(blocked
);
1374 account
.getGroupStore().updateGroup(group
);
1377 private void setExpirationTimer(RecipientId recipientId
, int messageExpirationTimer
) {
1378 var contact
= account
.getContactStore().getContact(recipientId
);
1379 if (contact
!= null && contact
.getMessageExpirationTime() == messageExpirationTimer
) {
1382 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
1383 account
.getContactStore()
1384 .storeContact(recipientId
, builder
.withMessageExpirationTime(messageExpirationTimer
).build());
1388 * Change the expiration timer for a contact
1390 public void setExpirationTimer(
1391 RecipientIdentifier
.Single recipient
, int messageExpirationTimer
1392 ) throws IOException
{
1393 var recipientId
= resolveRecipient(recipient
);
1394 setExpirationTimer(recipientId
, messageExpirationTimer
);
1395 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1397 sendMessage(messageBuilder
, Set
.of(recipient
));
1398 } catch (NotAGroupMemberException
| GroupNotFoundException e
) {
1399 throw new AssertionError(e
);
1404 * Change the expiration timer for a group
1406 private void setExpirationTimer(
1407 GroupInfoV1 groupInfoV1
, int messageExpirationTimer
1408 ) throws NotAGroupMemberException
, GroupNotFoundException
, IOException
{
1409 groupInfoV1
.messageExpirationTime
= messageExpirationTimer
;
1410 account
.getGroupStore().updateGroup(groupInfoV1
);
1411 sendExpirationTimerUpdate(groupInfoV1
.getGroupId());
1414 private void sendExpirationTimerUpdate(GroupIdV1 groupId
) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
{
1415 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
1416 sendHelper
.sendAsGroupMessage(messageBuilder
, groupId
);
1420 * Upload the sticker pack from path.
1422 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
1423 * @return if successful, returns the URL to install the sticker pack in the signal app
1425 public URI
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
1426 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
1428 var messageSender
= dependencies
.getMessageSender();
1430 var packKey
= KeyUtils
.createStickerUploadKey();
1431 var packIdString
= messageSender
.uploadStickerManifest(manifest
, packKey
);
1432 var packId
= StickerPackId
.deserialize(Hex
.fromStringCondensed(packIdString
));
1434 var sticker
= new Sticker(packId
, packKey
);
1435 account
.getStickerStore().updateSticker(sticker
);
1438 return new URI("https",
1442 + URLEncoder
.encode(Hex
.toStringCondensed(packId
.serialize()), StandardCharsets
.UTF_8
)
1444 + URLEncoder
.encode(Hex
.toStringCondensed(packKey
), StandardCharsets
.UTF_8
));
1445 } catch (URISyntaxException e
) {
1446 throw new AssertionError(e
);
1450 public void requestAllSyncData() throws IOException
{
1451 requestSyncGroups();
1452 requestSyncContacts();
1453 requestSyncBlocked();
1454 requestSyncConfiguration();
1458 private void requestSyncGroups() throws IOException
{
1459 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1460 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
)
1462 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1463 sendHelper
.sendSyncMessage(message
);
1466 private void requestSyncContacts() throws IOException
{
1467 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1468 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
)
1470 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1471 sendHelper
.sendSyncMessage(message
);
1474 private void requestSyncBlocked() throws IOException
{
1475 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1476 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
)
1478 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1479 sendHelper
.sendSyncMessage(message
);
1482 private void requestSyncConfiguration() throws IOException
{
1483 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1484 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
)
1486 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1487 sendHelper
.sendSyncMessage(message
);
1490 private void requestSyncKeys() throws IOException
{
1491 var r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder()
1492 .setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.KEYS
)
1494 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
1495 sendHelper
.sendSyncMessage(message
);
1498 private byte[] getSenderCertificate() {
1501 if (account
.isPhoneNumberShared()) {
1502 certificate
= dependencies
.getAccountManager().getSenderCertificate();
1504 certificate
= dependencies
.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
1506 } catch (IOException e
) {
1507 logger
.warn("Failed to get sender certificate, ignoring: {}", e
.getMessage());
1510 // TODO cache for a day
1514 private Set
<RecipientId
> getRecipientIds(Collection
<RecipientIdentifier
.Single
> recipients
) {
1515 final var signalServiceAddresses
= new HashSet
<SignalServiceAddress
>(recipients
.size());
1516 final var addressesMissingUuid
= new HashSet
<SignalServiceAddress
>();
1518 for (var number
: recipients
) {
1519 final var resolvedAddress
= resolveSignalServiceAddress(resolveRecipient(number
));
1520 if (resolvedAddress
.getUuid().isPresent()) {
1521 signalServiceAddresses
.add(resolvedAddress
);
1523 addressesMissingUuid
.add(resolvedAddress
);
1527 final var numbersMissingUuid
= addressesMissingUuid
.stream()
1528 .map(a
-> a
.getNumber().get())
1529 .collect(Collectors
.toSet());
1530 Map
<String
, UUID
> registeredUsers
;
1532 registeredUsers
= getRegisteredUsers(numbersMissingUuid
);
1533 } catch (IOException e
) {
1534 logger
.warn("Failed to resolve uuids from server, ignoring: {}", e
.getMessage());
1535 registeredUsers
= Map
.of();
1538 for (var address
: addressesMissingUuid
) {
1539 final var number
= address
.getNumber().get();
1540 if (registeredUsers
.containsKey(number
)) {
1541 final var newAddress
= resolveSignalServiceAddress(resolveRecipientTrusted(new SignalServiceAddress(
1542 registeredUsers
.get(number
),
1544 signalServiceAddresses
.add(newAddress
);
1546 signalServiceAddresses
.add(address
);
1550 return signalServiceAddresses
.stream().map(this::resolveRecipient
).collect(Collectors
.toSet());
1553 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
1554 final var address
= resolveSignalServiceAddress(recipientId
);
1555 if (!address
.getNumber().isPresent()) {
1558 final var number
= address
.getNumber().get();
1559 final var uuidMap
= getRegisteredUsers(Set
.of(number
));
1560 return resolveRecipientTrusted(new SignalServiceAddress(uuidMap
.getOrDefault(number
, null), number
));
1563 private Map
<String
, UUID
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
1564 final Map
<String
, UUID
> registeredUsers
;
1566 registeredUsers
= dependencies
.getAccountManager()
1567 .getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
1569 serviceEnvironmentConfig
.getCdsMrenclave());
1570 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
1571 throw new IOException(e
);
1574 // Store numbers as recipients so we have the number/uuid association
1575 registeredUsers
.forEach((number
, uuid
) -> resolveRecipientTrusted(new SignalServiceAddress(uuid
, number
)));
1577 return registeredUsers
;
1580 public void sendTypingMessage(
1581 TypingAction action
, Set
<RecipientIdentifier
> recipients
1582 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
{
1583 sendTypingMessage(action
.toSignalService(), recipients
);
1586 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, SelfSendException
, UnsupportedDataMessageException
, ProtocolUntrustedIdentityException
, InvalidMessageStructureException
{
1587 return dependencies
.getCipher().decrypt(envelope
);
1590 private void handleEndSession(RecipientId recipientId
) {
1591 account
.getSessionStore().deleteAllSessions(recipientId
);
1594 private List
<HandleAction
> handleSignalServiceDataMessage(
1595 SignalServiceDataMessage message
,
1597 SignalServiceAddress source
,
1598 SignalServiceAddress destination
,
1599 boolean ignoreAttachments
1601 var actions
= new ArrayList
<HandleAction
>();
1602 if (message
.getGroupContext().isPresent()) {
1603 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1604 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1605 var groupId
= GroupId
.v1(groupInfo
.getGroupId());
1606 var group
= getGroup(groupId
);
1607 if (group
== null || group
instanceof GroupInfoV1
) {
1608 var groupV1
= (GroupInfoV1
) group
;
1609 switch (groupInfo
.getType()) {
1611 if (groupV1
== null) {
1612 groupV1
= new GroupInfoV1(groupId
);
1615 if (groupInfo
.getAvatar().isPresent()) {
1616 var avatar
= groupInfo
.getAvatar().get();
1617 downloadGroupAvatar(avatar
, groupV1
.getGroupId());
1620 if (groupInfo
.getName().isPresent()) {
1621 groupV1
.name
= groupInfo
.getName().get();
1624 if (groupInfo
.getMembers().isPresent()) {
1625 groupV1
.addMembers(groupInfo
.getMembers()
1628 .map(this::resolveRecipient
)
1629 .collect(Collectors
.toSet()));
1632 account
.getGroupStore().updateGroup(groupV1
);
1636 if (groupV1
== null && !isSync
) {
1637 actions
.add(new SendGroupInfoRequestAction(source
, groupId
));
1641 if (groupV1
!= null) {
1642 groupV1
.removeMember(resolveRecipient(source
));
1643 account
.getGroupStore().updateGroup(groupV1
);
1648 if (groupV1
!= null && !isSync
) {
1649 actions
.add(new SendGroupInfoAction(source
, groupV1
.getGroupId()));
1654 // Received a group v1 message for a v2 group
1657 if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1658 final var groupContext
= message
.getGroupContext().get().getGroupV2().get();
1659 final var groupMasterKey
= groupContext
.getMasterKey();
1661 getOrMigrateGroup(groupMasterKey
,
1662 groupContext
.getRevision(),
1663 groupContext
.hasSignedGroupChange() ? groupContext
.getSignedGroupChange() : null);
1667 final var conversationPartnerAddress
= isSync ? destination
: source
;
1668 if (conversationPartnerAddress
!= null && message
.isEndSession()) {
1669 handleEndSession(resolveRecipient(conversationPartnerAddress
));
1671 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1672 if (message
.getGroupContext().isPresent()) {
1673 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
1674 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
1675 var group
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(groupInfo
.getGroupId()));
1676 if (group
!= null) {
1677 if (group
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1678 group
.messageExpirationTime
= message
.getExpiresInSeconds();
1679 account
.getGroupStore().updateGroup(group
);
1682 } else if (message
.getGroupContext().get().getGroupV2().isPresent()) {
1683 // disappearing message timer already stored in the DecryptedGroup
1685 } else if (conversationPartnerAddress
!= null) {
1686 setExpirationTimer(resolveRecipient(conversationPartnerAddress
), message
.getExpiresInSeconds());
1689 if (!ignoreAttachments
) {
1690 if (message
.getAttachments().isPresent()) {
1691 for (var attachment
: message
.getAttachments().get()) {
1692 downloadAttachment(attachment
);
1695 if (message
.getSharedContacts().isPresent()) {
1696 for (var contact
: message
.getSharedContacts().get()) {
1697 if (contact
.getAvatar().isPresent()) {
1698 downloadAttachment(contact
.getAvatar().get().getAttachment());
1703 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1704 final ProfileKey profileKey
;
1706 profileKey
= new ProfileKey(message
.getProfileKey().get());
1707 } catch (InvalidInputException e
) {
1708 throw new AssertionError(e
);
1710 if (source
.matches(account
.getSelfAddress())) {
1711 this.account
.setProfileKey(profileKey
);
1713 this.account
.getProfileStore().storeProfileKey(resolveRecipient(source
), profileKey
);
1715 if (message
.getPreviews().isPresent()) {
1716 final var previews
= message
.getPreviews().get();
1717 for (var preview
: previews
) {
1718 if (preview
.getImage().isPresent()) {
1719 downloadAttachment(preview
.getImage().get());
1723 if (message
.getQuote().isPresent()) {
1724 final var quote
= message
.getQuote().get();
1726 for (var quotedAttachment
: quote
.getAttachments()) {
1727 final var thumbnail
= quotedAttachment
.getThumbnail();
1728 if (thumbnail
!= null) {
1729 downloadAttachment(thumbnail
);
1733 if (message
.getSticker().isPresent()) {
1734 final var messageSticker
= message
.getSticker().get();
1735 final var stickerPackId
= StickerPackId
.deserialize(messageSticker
.getPackId());
1736 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
1737 if (sticker
== null) {
1738 sticker
= new Sticker(stickerPackId
, messageSticker
.getPackKey());
1739 account
.getStickerStore().updateSticker(sticker
);
1741 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, messageSticker
.getPackKey()));
1746 private GroupInfoV2
getOrMigrateGroup(
1747 final GroupMasterKey groupMasterKey
, final int revision
, final byte[] signedGroupChange
1749 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
1751 var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
1752 var groupInfo
= getGroup(groupId
);
1753 final GroupInfoV2 groupInfoV2
;
1754 if (groupInfo
instanceof GroupInfoV1
) {
1755 // Received a v2 group message for a v1 group, we need to locally migrate the group
1756 account
.getGroupStore().deleteGroupV1(((GroupInfoV1
) groupInfo
).getGroupId());
1757 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1758 logger
.info("Locally migrated group {} to group v2, id: {}",
1759 groupInfo
.getGroupId().toBase64(),
1760 groupInfoV2
.getGroupId().toBase64());
1761 } else if (groupInfo
instanceof GroupInfoV2
) {
1762 groupInfoV2
= (GroupInfoV2
) groupInfo
;
1764 groupInfoV2
= new GroupInfoV2(groupId
, groupMasterKey
);
1767 if (groupInfoV2
.getGroup() == null || groupInfoV2
.getGroup().getRevision() < revision
) {
1768 DecryptedGroup group
= null;
1769 if (signedGroupChange
!= null
1770 && groupInfoV2
.getGroup() != null
1771 && groupInfoV2
.getGroup().getRevision() + 1 == revision
) {
1772 group
= groupV2Helper
.getUpdatedDecryptedGroup(groupInfoV2
.getGroup(),
1776 if (group
== null) {
1777 group
= groupV2Helper
.getDecryptedGroup(groupSecretParams
);
1779 if (group
!= null) {
1780 storeProfileKeysFromMembers(group
);
1781 final var avatar
= group
.getAvatar();
1782 if (avatar
!= null && !avatar
.isEmpty()) {
1783 downloadGroupAvatar(groupId
, groupSecretParams
, avatar
);
1786 groupInfoV2
.setGroup(group
, this::resolveRecipient
);
1787 account
.getGroupStore().updateGroup(groupInfoV2
);
1793 private void storeProfileKeysFromMembers(final DecryptedGroup group
) {
1794 for (var member
: group
.getMembersList()) {
1795 final var uuid
= UuidUtil
.parseOrThrow(member
.getUuid().toByteArray());
1796 final var recipientId
= account
.getRecipientStore().resolveRecipient(uuid
);
1798 account
.getProfileStore()
1799 .storeProfileKey(recipientId
, new ProfileKey(member
.getProfileKey().toByteArray()));
1800 } catch (InvalidInputException ignored
) {
1805 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1806 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1807 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
1808 var actions
= retryFailedReceivedMessage(handler
, ignoreAttachments
, cachedMessage
);
1809 if (actions
!= null) {
1810 queuedActions
.addAll(actions
);
1813 handleQueuedActions(queuedActions
);
1816 private List
<HandleAction
> retryFailedReceivedMessage(
1817 final ReceiveMessageHandler handler
, final boolean ignoreAttachments
, final CachedMessage cachedMessage
1819 var envelope
= cachedMessage
.loadEnvelope();
1820 if (envelope
== null) {
1823 SignalServiceContent content
= null;
1824 List
<HandleAction
> actions
= null;
1825 if (!envelope
.isReceipt()) {
1827 content
= decryptMessage(envelope
);
1828 } catch (ProtocolUntrustedIdentityException e
) {
1829 if (!envelope
.hasSource()) {
1830 final var identifier
= e
.getSender();
1831 final var recipientId
= resolveRecipient(identifier
);
1833 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
1834 } catch (IOException ioException
) {
1835 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
1839 } catch (Exception er
) {
1840 // All other errors are not recoverable, so delete the cached message
1841 cachedMessage
.delete();
1844 actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1846 handler
.handleMessage(envelope
, content
, null);
1847 cachedMessage
.delete();
1851 public void receiveMessages(
1854 boolean returnOnTimeout
,
1855 boolean ignoreAttachments
,
1856 ReceiveMessageHandler handler
1857 ) throws IOException
{
1858 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1860 Set
<HandleAction
> queuedActions
= new HashSet
<>();
1862 final var signalWebSocket
= dependencies
.getSignalWebSocket();
1863 signalWebSocket
.connect();
1865 var hasCaughtUpWithOldMessages
= false;
1867 while (!Thread
.interrupted()) {
1868 SignalServiceEnvelope envelope
;
1869 SignalServiceContent content
= null;
1870 Exception exception
= null;
1871 final CachedMessage
[] cachedMessage
= {null};
1872 account
.setLastReceiveTimestamp(System
.currentTimeMillis());
1873 logger
.debug("Checking for new message from server");
1875 var result
= signalWebSocket
.readOrEmpty(unit
.toMillis(timeout
), envelope1
-> {
1876 final var recipientId
= envelope1
.hasSource()
1877 ?
resolveRecipient(envelope1
.getSourceIdentifier())
1879 // store message on disk, before acknowledging receipt to the server
1880 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1882 logger
.debug("New message received from server");
1883 if (result
.isPresent()) {
1884 envelope
= result
.get();
1886 // Received indicator that server queue is empty
1887 hasCaughtUpWithOldMessages
= true;
1889 handleQueuedActions(queuedActions
);
1890 queuedActions
.clear();
1892 // Continue to wait another timeout for new messages
1895 } catch (AssertionError e
) {
1896 if (e
.getCause() instanceof InterruptedException
) {
1897 Thread
.currentThread().interrupt();
1902 } catch (WebSocketUnavailableException e
) {
1903 logger
.debug("Pipe unexpectedly unavailable, connecting");
1904 signalWebSocket
.connect();
1906 } catch (TimeoutException e
) {
1907 if (returnOnTimeout
) return;
1911 if (envelope
.hasSource()) {
1912 // Store uuid if we don't have it already
1913 // address/uuid in envelope is sent by server
1914 resolveRecipientTrusted(envelope
.getSourceAddress());
1916 if (!envelope
.isReceipt()) {
1918 content
= decryptMessage(envelope
);
1919 } catch (Exception e
) {
1922 if (!envelope
.hasSource() && content
!= null) {
1923 // Store uuid if we don't have it already
1924 // address/uuid is validated by unidentified sender certificate
1925 resolveRecipientTrusted(content
.getSender());
1927 var actions
= handleMessage(envelope
, content
, ignoreAttachments
);
1928 if (exception
instanceof ProtocolInvalidMessageException
) {
1929 final var sender
= resolveRecipient(((ProtocolInvalidMessageException
) exception
).getSender());
1930 logger
.debug("Received invalid message, queuing renew session action.");
1931 actions
.add(new RenewSessionAction(sender
));
1933 if (hasCaughtUpWithOldMessages
) {
1934 for (var action
: actions
) {
1936 action
.execute(this);
1937 } catch (Throwable e
) {
1938 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1939 Thread
.currentThread().interrupt();
1941 logger
.warn("Message action failed.", e
);
1945 queuedActions
.addAll(actions
);
1948 final var notAllowedToSendToGroup
= isNotAllowedToSendToGroup(envelope
, content
);
1949 if (isMessageBlocked(envelope
, content
)) {
1950 logger
.info("Ignoring a message from blocked user/group: {}", envelope
.getTimestamp());
1951 } else if (notAllowedToSendToGroup
) {
1952 logger
.info("Ignoring a group message from an unauthorized sender (no member or admin): {} {}",
1953 (envelope
.hasSource() ? envelope
.getSourceAddress() : content
.getSender()).getIdentifier(),
1954 envelope
.getTimestamp());
1956 handler
.handleMessage(envelope
, content
, exception
);
1958 if (cachedMessage
[0] != null) {
1959 if (exception
instanceof ProtocolUntrustedIdentityException
) {
1960 final var identifier
= ((ProtocolUntrustedIdentityException
) exception
).getSender();
1961 final var recipientId
= resolveRecipient(identifier
);
1962 queuedActions
.add(new RetrieveProfileAction(recipientId
));
1963 if (!envelope
.hasSource()) {
1965 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
1966 } catch (IOException ioException
) {
1967 logger
.warn("Failed to move cached message to recipient folder: {}",
1968 ioException
.getMessage());
1972 cachedMessage
[0].delete();
1976 handleQueuedActions(queuedActions
);
1979 private void handleQueuedActions(final Set
<HandleAction
> queuedActions
) {
1980 for (var action
: queuedActions
) {
1982 action
.execute(this);
1983 } catch (Throwable e
) {
1984 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
1985 Thread
.currentThread().interrupt();
1987 logger
.warn("Message action failed.", e
);
1992 private boolean isMessageBlocked(
1993 SignalServiceEnvelope envelope
, SignalServiceContent content
1995 SignalServiceAddress source
;
1996 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
1997 source
= envelope
.getSourceAddress();
1998 } else if (content
!= null) {
1999 source
= content
.getSender();
2003 final var recipientId
= resolveRecipient(source
);
2004 if (isContactBlocked(recipientId
)) {
2008 if (content
!= null && content
.getDataMessage().isPresent()) {
2009 var message
= content
.getDataMessage().get();
2010 if (message
.getGroupContext().isPresent()) {
2011 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2012 var group
= getGroup(groupId
);
2013 if (group
!= null && group
.isBlocked()) {
2021 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
2022 final var recipientId
= resolveRecipient(recipient
);
2023 return isContactBlocked(recipientId
);
2026 private boolean isContactBlocked(final RecipientId recipientId
) {
2027 var sourceContact
= account
.getContactStore().getContact(recipientId
);
2028 return sourceContact
!= null && sourceContact
.isBlocked();
2031 private boolean isNotAllowedToSendToGroup(
2032 SignalServiceEnvelope envelope
, SignalServiceContent content
2034 SignalServiceAddress source
;
2035 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2036 source
= envelope
.getSourceAddress();
2037 } else if (content
!= null) {
2038 source
= content
.getSender();
2043 if (content
== null || !content
.getDataMessage().isPresent()) {
2047 var message
= content
.getDataMessage().get();
2048 if (!message
.getGroupContext().isPresent()) {
2052 if (message
.getGroupContext().get().getGroupV1().isPresent()) {
2053 var groupInfo
= message
.getGroupContext().get().getGroupV1().get();
2054 if (groupInfo
.getType() == SignalServiceGroup
.Type
.QUIT
) {
2059 var groupId
= GroupUtils
.getGroupId(message
.getGroupContext().get());
2060 var group
= getGroup(groupId
);
2061 if (group
== null) {
2065 final var recipientId
= resolveRecipient(source
);
2066 return !group
.isMember(recipientId
) || (
2067 group
.isAnnouncementGroup() && !group
.isAdmin(recipientId
)
2071 private List
<HandleAction
> handleMessage(
2072 SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
2074 var actions
= new ArrayList
<HandleAction
>();
2075 if (content
!= null) {
2076 final SignalServiceAddress sender
;
2077 if (!envelope
.isUnidentifiedSender() && envelope
.hasSource()) {
2078 sender
= envelope
.getSourceAddress();
2080 sender
= content
.getSender();
2083 if (content
.getDataMessage().isPresent()) {
2084 var message
= content
.getDataMessage().get();
2086 if (content
.isNeedsReceipt()) {
2087 actions
.add(new SendReceiptAction(sender
, message
.getTimestamp()));
2090 actions
.addAll(handleSignalServiceDataMessage(message
,
2093 account
.getSelfAddress(),
2094 ignoreAttachments
));
2096 if (content
.getSyncMessage().isPresent()) {
2097 account
.setMultiDevice(true);
2098 var syncMessage
= content
.getSyncMessage().get();
2099 if (syncMessage
.getSent().isPresent()) {
2100 var message
= syncMessage
.getSent().get();
2101 final var destination
= message
.getDestination().orNull();
2102 actions
.addAll(handleSignalServiceDataMessage(message
.getMessage(),
2106 ignoreAttachments
));
2108 if (syncMessage
.getRequest().isPresent() && account
.isMasterDevice()) {
2109 var rm
= syncMessage
.getRequest().get();
2110 if (rm
.isContactsRequest()) {
2111 actions
.add(SendSyncContactsAction
.create());
2113 if (rm
.isGroupsRequest()) {
2114 actions
.add(SendSyncGroupsAction
.create());
2116 if (rm
.isBlockedListRequest()) {
2117 actions
.add(SendSyncBlockedListAction
.create());
2119 // TODO Handle rm.isConfigurationRequest(); rm.isKeysRequest();
2121 if (syncMessage
.getGroups().isPresent()) {
2122 File tmpFile
= null;
2124 tmpFile
= IOUtils
.createTempFile();
2125 final var groupsMessage
= syncMessage
.getGroups().get();
2126 try (var attachmentAsStream
= retrieveAttachmentAsStream(groupsMessage
.asPointer(), tmpFile
)) {
2127 var s
= new DeviceGroupsInputStream(attachmentAsStream
);
2132 } catch (IOException e
) {
2133 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
2139 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
2140 if (syncGroup
!= null) {
2141 if (g
.getName().isPresent()) {
2142 syncGroup
.name
= g
.getName().get();
2144 syncGroup
.addMembers(g
.getMembers()
2146 .map(this::resolveRecipient
)
2147 .collect(Collectors
.toSet()));
2148 if (!g
.isActive()) {
2149 syncGroup
.removeMember(account
.getSelfRecipientId());
2151 // Add ourself to the member set as it's marked as active
2152 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
2154 syncGroup
.blocked
= g
.isBlocked();
2155 if (g
.getColor().isPresent()) {
2156 syncGroup
.color
= g
.getColor().get();
2159 if (g
.getAvatar().isPresent()) {
2160 downloadGroupAvatar(g
.getAvatar().get(), syncGroup
.getGroupId());
2162 syncGroup
.archived
= g
.isArchived();
2163 account
.getGroupStore().updateGroup(syncGroup
);
2167 } catch (Exception e
) {
2168 logger
.warn("Failed to handle received sync groups “{}”, ignoring: {}",
2172 if (tmpFile
!= null) {
2174 Files
.delete(tmpFile
.toPath());
2175 } catch (IOException e
) {
2176 logger
.warn("Failed to delete received groups temp file “{}”, ignoring: {}",
2183 if (syncMessage
.getBlockedList().isPresent()) {
2184 final var blockedListMessage
= syncMessage
.getBlockedList().get();
2185 for (var address
: blockedListMessage
.getAddresses()) {
2186 setContactBlocked(resolveRecipient(address
), true);
2188 for (var groupId
: blockedListMessage
.getGroupIds()
2190 .map(GroupId
::unknownVersion
)
2191 .collect(Collectors
.toSet())) {
2193 setGroupBlocked(groupId
, true);
2194 } catch (GroupNotFoundException e
) {
2195 logger
.warn("BlockedListMessage contained groupID that was not found in GroupStore: {}",
2196 groupId
.toBase64());
2200 if (syncMessage
.getContacts().isPresent()) {
2201 File tmpFile
= null;
2203 tmpFile
= IOUtils
.createTempFile();
2204 final var contactsMessage
= syncMessage
.getContacts().get();
2205 try (var attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream()
2206 .asPointer(), tmpFile
)) {
2207 var s
= new DeviceContactsInputStream(attachmentAsStream
);
2212 } catch (IOException e
) {
2213 logger
.warn("Sync contacts contained invalid contact, ignoring: {}",
2220 if (c
.getAddress().matches(account
.getSelfAddress()) && c
.getProfileKey().isPresent()) {
2221 account
.setProfileKey(c
.getProfileKey().get());
2223 final var recipientId
= resolveRecipientTrusted(c
.getAddress());
2224 var contact
= account
.getContactStore().getContact(recipientId
);
2225 final var builder
= contact
== null
2226 ? Contact
.newBuilder()
2227 : Contact
.newBuilder(contact
);
2228 if (c
.getName().isPresent()) {
2229 builder
.withName(c
.getName().get());
2231 if (c
.getColor().isPresent()) {
2232 builder
.withColor(c
.getColor().get());
2234 if (c
.getProfileKey().isPresent()) {
2235 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
2237 if (c
.getVerified().isPresent()) {
2238 final var verifiedMessage
= c
.getVerified().get();
2239 account
.getIdentityKeyStore()
2240 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2241 verifiedMessage
.getIdentityKey(),
2242 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2244 if (c
.getExpirationTimer().isPresent()) {
2245 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
2247 builder
.withBlocked(c
.isBlocked());
2248 builder
.withArchived(c
.isArchived());
2249 account
.getContactStore().storeContact(recipientId
, builder
.build());
2251 if (c
.getAvatar().isPresent()) {
2252 downloadContactAvatar(c
.getAvatar().get(), c
.getAddress());
2256 } catch (Exception e
) {
2257 logger
.warn("Failed to handle received sync contacts “{}”, ignoring: {}",
2261 if (tmpFile
!= null) {
2263 Files
.delete(tmpFile
.toPath());
2264 } catch (IOException e
) {
2265 logger
.warn("Failed to delete received contacts temp file “{}”, ignoring: {}",
2272 if (syncMessage
.getVerified().isPresent()) {
2273 final var verifiedMessage
= syncMessage
.getVerified().get();
2274 account
.getIdentityKeyStore()
2275 .setIdentityTrustLevel(resolveRecipientTrusted(verifiedMessage
.getDestination()),
2276 verifiedMessage
.getIdentityKey(),
2277 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
2279 if (syncMessage
.getStickerPackOperations().isPresent()) {
2280 final var stickerPackOperationMessages
= syncMessage
.getStickerPackOperations().get();
2281 for (var m
: stickerPackOperationMessages
) {
2282 if (!m
.getPackId().isPresent()) {
2285 final var stickerPackId
= StickerPackId
.deserialize(m
.getPackId().get());
2286 final var installed
= !m
.getType().isPresent()
2287 || m
.getType().get() == StickerPackOperationMessage
.Type
.INSTALL
;
2289 var sticker
= account
.getStickerStore().getSticker(stickerPackId
);
2290 if (m
.getPackKey().isPresent()) {
2291 if (sticker
== null) {
2292 sticker
= new Sticker(stickerPackId
, m
.getPackKey().get());
2295 enqueueJob(new RetrieveStickerPackJob(stickerPackId
, m
.getPackKey().get()));
2299 if (sticker
!= null) {
2300 sticker
.setInstalled(installed
);
2301 account
.getStickerStore().updateSticker(sticker
);
2305 if (syncMessage
.getFetchType().isPresent()) {
2306 switch (syncMessage
.getFetchType().get()) {
2308 getRecipientProfile(account
.getSelfRecipientId(), true);
2309 case STORAGE_MANIFEST
:
2313 if (syncMessage
.getKeys().isPresent()) {
2314 final var keysMessage
= syncMessage
.getKeys().get();
2315 if (keysMessage
.getStorageService().isPresent()) {
2316 final var storageKey
= keysMessage
.getStorageService().get();
2317 account
.setStorageKey(storageKey
);
2320 if (syncMessage
.getConfiguration().isPresent()) {
2328 private void downloadContactAvatar(SignalServiceAttachment avatar
, SignalServiceAddress address
) {
2330 avatarStore
.storeContactAvatar(address
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2331 } catch (IOException e
) {
2332 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
2336 private void downloadGroupAvatar(SignalServiceAttachment avatar
, GroupId groupId
) {
2338 avatarStore
.storeGroupAvatar(groupId
, outputStream
-> retrieveAttachment(avatar
, outputStream
));
2339 } catch (IOException e
) {
2340 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2344 private void downloadGroupAvatar(GroupId groupId
, GroupSecretParams groupSecretParams
, String cdnKey
) {
2346 avatarStore
.storeGroupAvatar(groupId
,
2347 outputStream
-> retrieveGroupV2Avatar(groupSecretParams
, cdnKey
, outputStream
));
2348 } catch (IOException e
) {
2349 logger
.warn("Failed to download avatar for group {}, ignoring: {}", groupId
.toBase64(), e
.getMessage());
2353 private void downloadProfileAvatar(
2354 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
2357 avatarStore
.storeProfileAvatar(address
,
2358 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
2359 } catch (Throwable e
) {
2360 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
2361 Thread
.currentThread().interrupt();
2363 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
2367 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
2368 return attachmentStore
.getAttachmentFile(attachmentId
);
2371 private void downloadAttachment(final SignalServiceAttachment attachment
) {
2372 if (!attachment
.isPointer()) {
2373 logger
.warn("Invalid state, can't store an attachment stream.");
2376 var pointer
= attachment
.asPointer();
2377 if (pointer
.getPreview().isPresent()) {
2378 final var preview
= pointer
.getPreview().get();
2380 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
2381 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
2382 } catch (IOException e
) {
2383 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
2388 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
2389 outputStream
-> retrieveAttachmentPointer(pointer
, outputStream
));
2390 } catch (IOException e
) {
2391 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
2395 private void retrieveGroupV2Avatar(
2396 GroupSecretParams groupSecretParams
, String cdnKey
, OutputStream outputStream
2397 ) throws IOException
{
2398 var groupOperations
= dependencies
.getGroupsV2Operations().forGroup(groupSecretParams
);
2400 var tmpFile
= IOUtils
.createTempFile();
2401 try (InputStream input
= dependencies
.getMessageReceiver()
2402 .retrieveGroupsV2ProfileAvatar(cdnKey
, tmpFile
, ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2403 var encryptedData
= IOUtils
.readFully(input
);
2405 var decryptedData
= groupOperations
.decryptAvatar(encryptedData
);
2406 outputStream
.write(decryptedData
);
2409 Files
.delete(tmpFile
.toPath());
2410 } catch (IOException e
) {
2411 logger
.warn("Failed to delete received group avatar temp file “{}”, ignoring: {}",
2418 private void retrieveProfileAvatar(
2419 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
2420 ) throws IOException
{
2421 var tmpFile
= IOUtils
.createTempFile();
2422 try (var input
= dependencies
.getMessageReceiver()
2423 .retrieveProfileAvatar(avatarPath
,
2426 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
2427 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
2428 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
2431 Files
.delete(tmpFile
.toPath());
2432 } catch (IOException e
) {
2433 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
2440 private void retrieveAttachment(
2441 final SignalServiceAttachment attachment
, final OutputStream outputStream
2442 ) throws IOException
{
2443 if (attachment
.isPointer()) {
2444 var pointer
= attachment
.asPointer();
2445 retrieveAttachmentPointer(pointer
, outputStream
);
2447 var stream
= attachment
.asStream();
2448 IOUtils
.copyStream(stream
.getInputStream(), outputStream
);
2452 private void retrieveAttachmentPointer(
2453 SignalServiceAttachmentPointer pointer
, OutputStream outputStream
2454 ) throws IOException
{
2455 var tmpFile
= IOUtils
.createTempFile();
2456 try (var input
= retrieveAttachmentAsStream(pointer
, tmpFile
)) {
2457 IOUtils
.copyStream(input
, outputStream
);
2458 } catch (MissingConfigurationException
| InvalidMessageException e
) {
2459 throw new IOException(e
);
2462 Files
.delete(tmpFile
.toPath());
2463 } catch (IOException e
) {
2464 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
2471 private InputStream
retrieveAttachmentAsStream(
2472 SignalServiceAttachmentPointer pointer
, File tmpFile
2473 ) throws IOException
, InvalidMessageException
, MissingConfigurationException
{
2474 return dependencies
.getMessageReceiver()
2475 .retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
2478 void sendGroups() throws IOException
{
2479 var groupsFile
= IOUtils
.createTempFile();
2482 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
2483 var out
= new DeviceGroupsOutputStream(fos
);
2484 for (var record : getGroups()) {
2485 if (record instanceof GroupInfoV1
) {
2486 var groupInfo
= (GroupInfoV1
) record;
2487 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
2488 Optional
.fromNullable(groupInfo
.name
),
2489 groupInfo
.getMembers()
2491 .map(this::resolveSignalServiceAddress
)
2492 .collect(Collectors
.toList()),
2493 createGroupAvatarAttachment(groupInfo
.getGroupId()),
2494 groupInfo
.isMember(account
.getSelfRecipientId()),
2495 Optional
.of(groupInfo
.messageExpirationTime
),
2496 Optional
.fromNullable(groupInfo
.color
),
2499 groupInfo
.archived
));
2504 if (groupsFile
.exists() && groupsFile
.length() > 0) {
2505 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
2506 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2507 .withStream(groupsFileStream
)
2508 .withContentType("application/octet-stream")
2509 .withLength(groupsFile
.length())
2512 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
2517 Files
.delete(groupsFile
.toPath());
2518 } catch (IOException e
) {
2519 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
2524 public void sendContacts() throws IOException
{
2525 var contactsFile
= IOUtils
.createTempFile();
2528 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
2529 var out
= new DeviceContactsOutputStream(fos
);
2530 for (var contactPair
: account
.getContactStore().getContacts()) {
2531 final var recipientId
= contactPair
.first();
2532 final var contact
= contactPair
.second();
2533 final var address
= resolveSignalServiceAddress(recipientId
);
2535 var currentIdentity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2536 VerifiedMessage verifiedMessage
= null;
2537 if (currentIdentity
!= null) {
2538 verifiedMessage
= new VerifiedMessage(address
,
2539 currentIdentity
.getIdentityKey(),
2540 currentIdentity
.getTrustLevel().toVerifiedState(),
2541 currentIdentity
.getDateAdded().getTime());
2544 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
2545 out
.write(new DeviceContact(address
,
2546 Optional
.fromNullable(contact
.getName()),
2547 createContactAvatarAttachment(address
),
2548 Optional
.fromNullable(contact
.getColor()),
2549 Optional
.fromNullable(verifiedMessage
),
2550 Optional
.fromNullable(profileKey
),
2551 contact
.isBlocked(),
2552 Optional
.of(contact
.getMessageExpirationTime()),
2554 contact
.isArchived()));
2557 if (account
.getProfileKey() != null) {
2558 // Send our own profile key as well
2559 out
.write(new DeviceContact(account
.getSelfAddress(),
2564 Optional
.of(account
.getProfileKey()),
2572 if (contactsFile
.exists() && contactsFile
.length() > 0) {
2573 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
2574 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
2575 .withStream(contactsFileStream
)
2576 .withContentType("application/octet-stream")
2577 .withLength(contactsFile
.length())
2580 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
,
2586 Files
.delete(contactsFile
.toPath());
2587 } catch (IOException e
) {
2588 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
2593 void sendBlockedList() throws IOException
{
2594 var addresses
= new ArrayList
<SignalServiceAddress
>();
2595 for (var record : account
.getContactStore().getContacts()) {
2596 if (record.second().isBlocked()) {
2597 addresses
.add(resolveSignalServiceAddress(record.first()));
2600 var groupIds
= new ArrayList
<byte[]>();
2601 for (var record : getGroups()) {
2602 if (record.isBlocked()) {
2603 groupIds
.add(record.getGroupId().serialize());
2606 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
2609 private void sendVerifiedMessage(
2610 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
2611 ) throws IOException
{
2612 var verifiedMessage
= new VerifiedMessage(destination
,
2614 trustLevel
.toVerifiedState(),
2615 System
.currentTimeMillis());
2616 sendHelper
.sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
2619 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
2620 return account
.getContactStore().getContacts();
2623 public String
getContactOrProfileName(RecipientIdentifier
.Single recipientIdentifier
) {
2624 final var recipientId
= resolveRecipient(recipientIdentifier
);
2625 final var recipient
= account
.getRecipientStore().getRecipient(recipientId
);
2626 if (recipient
== null) {
2630 if (recipient
.getContact() != null && !Util
.isEmpty(recipient
.getContact().getName())) {
2631 return recipient
.getContact().getName();
2634 if (recipient
.getProfile() != null && recipient
.getProfile() != null) {
2635 return recipient
.getProfile().getDisplayName();
2641 public GroupInfo
getGroup(GroupId groupId
) {
2642 return getGroup(groupId
, false);
2645 public GroupInfo
getGroup(GroupId groupId
, boolean forceUpdate
) {
2646 final var group
= account
.getGroupStore().getGroup(groupId
);
2647 if (group
instanceof GroupInfoV2
&& (forceUpdate
|| ((GroupInfoV2
) group
).getGroup() == null)) {
2648 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(((GroupInfoV2
) group
).getMasterKey());
2649 ((GroupInfoV2
) group
).setGroup(groupV2Helper
.getDecryptedGroup(groupSecretParams
), this::resolveRecipient
);
2650 account
.getGroupStore().updateGroup(group
);
2655 public List
<IdentityInfo
> getIdentities() {
2656 return account
.getIdentityKeyStore().getIdentities();
2659 public List
<IdentityInfo
> getIdentities(RecipientIdentifier
.Single recipient
) {
2660 final var identity
= account
.getIdentityKeyStore().getIdentity(resolveRecipient(recipient
));
2661 return identity
== null ? List
.of() : List
.of(identity
);
2665 * Trust this the identity with this fingerprint
2667 * @param recipient username of the identity
2668 * @param fingerprint Fingerprint
2670 public boolean trustIdentityVerified(RecipientIdentifier
.Single recipient
, byte[] fingerprint
) {
2671 var recipientId
= resolveRecipient(recipient
);
2672 return trustIdentity(recipientId
,
2673 identityKey
-> Arrays
.equals(identityKey
.serialize(), fingerprint
),
2674 TrustLevel
.TRUSTED_VERIFIED
);
2678 * Trust this the identity with this safety number
2680 * @param recipient username of the identity
2681 * @param safetyNumber Safety number
2683 public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier
.Single recipient
, String safetyNumber
) {
2684 var recipientId
= resolveRecipient(recipient
);
2685 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2686 return trustIdentity(recipientId
,
2687 identityKey
-> safetyNumber
.equals(computeSafetyNumber(address
, identityKey
)),
2688 TrustLevel
.TRUSTED_VERIFIED
);
2692 * Trust this the identity with this scannable safety number
2694 * @param recipient username of the identity
2695 * @param safetyNumber Scannable safety number
2697 public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier
.Single recipient
, byte[] safetyNumber
) {
2698 var recipientId
= resolveRecipient(recipient
);
2699 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2700 return trustIdentity(recipientId
, identityKey
-> {
2701 final var fingerprint
= computeSafetyNumberFingerprint(address
, identityKey
);
2703 return fingerprint
!= null && fingerprint
.getScannableFingerprint().compareTo(safetyNumber
);
2704 } catch (FingerprintVersionMismatchException
| FingerprintParsingException e
) {
2707 }, TrustLevel
.TRUSTED_VERIFIED
);
2711 * Trust all keys of this identity without verification
2713 * @param recipient username of the identity
2715 public boolean trustIdentityAllKeys(RecipientIdentifier
.Single recipient
) {
2716 var recipientId
= resolveRecipient(recipient
);
2717 return trustIdentity(recipientId
, identityKey
-> true, TrustLevel
.TRUSTED_UNVERIFIED
);
2720 private boolean trustIdentity(
2721 RecipientId recipientId
, Function
<IdentityKey
, Boolean
> verifier
, TrustLevel trustLevel
2723 var identity
= account
.getIdentityKeyStore().getIdentity(recipientId
);
2724 if (identity
== null) {
2728 if (!verifier
.apply(identity
.getIdentityKey())) {
2732 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identity
.getIdentityKey(), trustLevel
);
2734 var address
= account
.getRecipientStore().resolveServiceAddress(recipientId
);
2735 sendVerifiedMessage(address
, identity
.getIdentityKey(), trustLevel
);
2736 } catch (IOException e
) {
2737 logger
.warn("Failed to send verification sync message: {}", e
.getMessage());
2743 private void handleIdentityFailure(
2744 final RecipientId recipientId
, final SendMessageResult
.IdentityFailure identityFailure
2746 final var identityKey
= identityFailure
.getIdentityKey();
2747 if (identityKey
!= null) {
2748 final var newIdentity
= account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
2750 account
.getSessionStore().archiveSessions(recipientId
);
2753 // Retrieve profile to get the current identity key from the server
2754 retrieveEncryptedProfile(recipientId
);
2758 public String
computeSafetyNumber(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2759 final Fingerprint fingerprint
= computeSafetyNumberFingerprint(theirAddress
, theirIdentityKey
);
2760 return fingerprint
== null ?
null : fingerprint
.getDisplayableFingerprint().getDisplayText();
2763 public byte[] computeSafetyNumberForScanning(SignalServiceAddress theirAddress
, IdentityKey theirIdentityKey
) {
2764 final Fingerprint fingerprint
= computeSafetyNumberFingerprint(theirAddress
, theirIdentityKey
);
2765 return fingerprint
== null ?
null : fingerprint
.getScannableFingerprint().getSerialized();
2768 private Fingerprint
computeSafetyNumberFingerprint(
2769 final SignalServiceAddress theirAddress
, final IdentityKey theirIdentityKey
2771 return Utils
.computeSafetyNumber(capabilities
.isUuid(),
2772 account
.getSelfAddress(),
2773 getIdentityKeyPair().getPublicKey(),
2779 public SignalServiceAddress
resolveSignalServiceAddress(String identifier
) {
2780 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2782 return resolveSignalServiceAddress(address
);
2786 public SignalServiceAddress
resolveSignalServiceAddress(SignalServiceAddress address
) {
2787 if (address
.matches(account
.getSelfAddress())) {
2788 return account
.getSelfAddress();
2791 return account
.getRecipientStore().resolveServiceAddress(address
);
2794 public SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
2795 return account
.getRecipientStore().resolveServiceAddress(recipientId
);
2798 private String
canonicalizePhoneNumber(final String number
) throws InvalidNumberException
{
2799 return PhoneNumberFormatter
.formatNumber(number
, account
.getUsername());
2802 private RecipientId
resolveRecipient(final String identifier
) {
2803 var address
= Utils
.getSignalServiceAddressFromIdentifier(identifier
);
2805 return resolveRecipient(address
);
2808 private RecipientId
resolveRecipient(final RecipientIdentifier
.Single recipient
) {
2809 final SignalServiceAddress address
;
2810 if (recipient
instanceof RecipientIdentifier
.Uuid
) {
2811 address
= new SignalServiceAddress(((RecipientIdentifier
.Uuid
) recipient
).uuid
, null);
2813 address
= new SignalServiceAddress(null, ((RecipientIdentifier
.Number
) recipient
).number
);
2816 return resolveRecipient(address
);
2819 public RecipientId
resolveRecipient(SignalServiceAddress address
) {
2820 return account
.getRecipientStore().resolveRecipient(address
);
2823 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
2824 return account
.getRecipientStore().resolveRecipientTrusted(address
);
2827 private void enqueueJob(Job job
) {
2828 var context
= new Context(account
,
2829 dependencies
.getAccountManager(),
2830 dependencies
.getMessageReceiver(),
2836 public void close() throws IOException
{
2840 void close(boolean closeAccount
) throws IOException
{
2841 executor
.shutdown();
2843 dependencies
.getSignalWebSocket().disconnect();
2845 if (closeAccount
&& account
!= null) {
2851 public interface ReceiveMessageHandler
{
2853 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);