1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.api
.GroupNotFoundException
;
4 import org
.asamk
.signal
.manager
.api
.NotAGroupMemberException
;
5 import org
.asamk
.signal
.manager
.api
.PhoneNumberSharingMode
;
6 import org
.asamk
.signal
.manager
.api
.Profile
;
7 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
8 import org
.asamk
.signal
.manager
.internal
.SignalDependencies
;
9 import org
.asamk
.signal
.manager
.jobs
.SyncStorageJob
;
10 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
11 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
12 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
13 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
14 import org
.asamk
.signal
.manager
.util
.IOUtils
;
15 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
16 import org
.asamk
.signal
.manager
.util
.PaymentUtils
;
17 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
18 import org
.asamk
.signal
.manager
.util
.Utils
;
19 import org
.jetbrains
.annotations
.Nullable
;
20 import org
.signal
.libsignal
.protocol
.IdentityKey
;
21 import org
.signal
.libsignal
.protocol
.InvalidKeyException
;
22 import org
.signal
.libsignal
.zkgroup
.profiles
.ExpiringProfileKeyCredential
;
23 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
24 import org
.slf4j
.Logger
;
25 import org
.slf4j
.LoggerFactory
;
26 import org
.whispersystems
.signalservice
.api
.crypto
.SealedSenderAccess
;
27 import org
.whispersystems
.signalservice
.api
.profiles
.AvatarUploadParams
;
28 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
29 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
30 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
31 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NotFoundException
;
32 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.PushNetworkException
;
33 import org
.whispersystems
.signalservice
.api
.services
.ProfileService
;
34 import org
.whispersystems
.signalservice
.api
.util
.ExpiringProfileCredentialUtil
;
36 import java
.io
.IOException
;
37 import java
.io
.OutputStream
;
38 import java
.nio
.file
.Files
;
39 import java
.util
.Base64
;
40 import java
.util
.Collection
;
41 import java
.util
.List
;
42 import java
.util
.Locale
;
43 import java
.util
.Objects
;
44 import java
.util
.Optional
;
47 import io
.reactivex
.rxjava3
.core
.Flowable
;
48 import io
.reactivex
.rxjava3
.core
.Maybe
;
49 import io
.reactivex
.rxjava3
.core
.Single
;
51 public final class ProfileHelper
{
53 private static final Logger logger
= LoggerFactory
.getLogger(ProfileHelper
.class);
55 private final SignalAccount account
;
56 private final SignalDependencies dependencies
;
57 private final Context context
;
59 public ProfileHelper(final Context context
) {
60 this.account
= context
.getAccount();
61 this.dependencies
= context
.getDependencies();
62 this.context
= context
;
65 public void rotateProfileKey() throws IOException
{
66 // refresh our profile, before creating a new profile key
68 var profileKey
= KeyUtils
.createProfileKey();
69 account
.setProfileKey(profileKey
);
70 context
.getAccountHelper().updateAccountAttributes();
71 setProfile(true, true, null, null, null, null, null, null);
72 account
.getRecipientStore().rotateSelfStorageId();
73 context
.getJobExecutor().enqueueJob(new SyncStorageJob());
75 final var recipientIds
= account
.getRecipientStore().getRecipientIdsWithEnabledProfileSharing();
76 for (final var recipientId
: recipientIds
) {
77 context
.getSendHelper().sendProfileKey(recipientId
);
80 final var selfRecipientId
= account
.getSelfRecipientId();
81 final var activeGroupIds
= account
.getGroupStore()
84 .filter(g
-> g
instanceof GroupInfoV2
&& g
.isMember(selfRecipientId
) && g
.isProfileSharingEnabled())
85 .map(g
-> (GroupInfoV2
) g
)
86 .map(GroupInfoV2
::getGroupId
)
88 for (final var groupId
: activeGroupIds
) {
90 context
.getGroupHelper().updateGroupProfileKey(groupId
);
91 } catch (GroupNotFoundException
| NotAGroupMemberException
| IOException e
) {
92 logger
.warn("Failed to update group profile key: {}", e
.getMessage());
97 public Profile
getRecipientProfile(RecipientId recipientId
) {
98 return getRecipientProfile(recipientId
, false);
101 public List
<Profile
> getRecipientProfiles(Collection
<RecipientId
> recipientIds
) {
102 return getRecipientProfiles(recipientIds
, false);
105 public void refreshRecipientProfile(RecipientId recipientId
) {
106 getRecipientProfile(recipientId
, true);
109 public void refreshRecipientProfiles(Collection
<RecipientId
> recipientIds
) {
110 getRecipientProfiles(recipientIds
, true);
113 public List
<ExpiringProfileKeyCredential
> getExpiringProfileKeyCredential(List
<RecipientId
> recipientIds
) {
114 final var profileFetches
= Flowable
.fromIterable(recipientIds
)
115 .filter(recipientId
-> !ExpiringProfileCredentialUtil
.isValid(account
.getProfileStore()
116 .getExpiringProfileKeyCredential(recipientId
)))
117 .map(recipientId
-> retrieveProfile(recipientId
,
118 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
).onErrorComplete());
119 Maybe
.merge(profileFetches
, 10).blockingSubscribe();
121 return recipientIds
.stream().map(r
-> account
.getProfileStore().getExpiringProfileKeyCredential(r
)).toList();
124 public ExpiringProfileKeyCredential
getExpiringProfileKeyCredential(RecipientId recipientId
) {
125 var profileKeyCredential
= account
.getProfileStore().getExpiringProfileKeyCredential(recipientId
);
126 if (ExpiringProfileCredentialUtil
.isValid(profileKeyCredential
)) {
127 return profileKeyCredential
;
131 blockingGetProfile(retrieveProfile(recipientId
, SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
));
132 } catch (IOException e
) {
133 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
137 return account
.getProfileStore().getExpiringProfileKeyCredential(recipientId
);
141 * @param givenName if null, the previous givenName will be kept
142 * @param familyName if null, the previous familyName will be kept
143 * @param about if null, the previous about text will be kept
144 * @param aboutEmoji if null, the previous about emoji will be kept
145 * @param avatar if avatar is null the image from the local avatar store is used (if present),
147 public void setProfile(
149 final String familyName
,
152 Optional
<String
> avatar
,
153 byte[] mobileCoinAddress
154 ) throws IOException
{
155 setProfile(true, false, givenName
, familyName
, about
, aboutEmoji
, avatar
, mobileCoinAddress
);
158 public void setProfile(
159 boolean uploadProfile
,
160 boolean forceUploadAvatar
,
162 final String familyName
,
165 Optional
<String
> avatar
,
166 byte[] mobileCoinAddress
167 ) throws IOException
{
168 var profile
= getSelfProfile();
169 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
170 if (givenName
!= null) {
171 builder
.withGivenName(givenName
);
173 if (familyName
!= null) {
174 builder
.withFamilyName(familyName
);
177 builder
.withAbout(about
);
179 if (aboutEmoji
!= null) {
180 builder
.withAboutEmoji(aboutEmoji
);
182 if (mobileCoinAddress
!= null) {
183 builder
.withMobileCoinAddress(mobileCoinAddress
);
185 var newProfile
= builder
.build();
188 final var streamDetails
= avatar
!= null && avatar
.isPresent()
189 ? Utils
.createStreamDetails(avatar
.get())
191 : forceUploadAvatar
&& avatar
== null ? context
.getAvatarStore()
192 .retrieveProfileAvatar(account
.getSelfRecipientAddress()) : null;
193 try (streamDetails
) {
194 final var avatarUploadParams
= streamDetails
!= null
195 ? AvatarUploadParams
.forAvatar(streamDetails
)
196 : avatar
== null ? AvatarUploadParams
.unchanged(true) : AvatarUploadParams
.unchanged(false);
197 final var paymentsAddress
= Optional
.ofNullable(newProfile
.getMobileCoinAddress())
198 .map(address
-> PaymentUtils
.signPaymentsAddress(address
,
199 account
.getAciIdentityKeyPair().getPrivateKey()));
200 logger
.debug("Uploading new profile");
201 final var avatarPath
= dependencies
.getAccountManager()
202 .setVersionedProfile(account
.getAci(),
203 account
.getProfileKey(),
204 newProfile
.getInternalServiceName(),
205 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
206 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
209 List
.of(/* TODO implement support for badges */),
210 account
.getConfigurationStore().getPhoneNumberSharingMode()
211 == PhoneNumberSharingMode
.EVERYBODY
);
212 if (!avatarUploadParams
.keepTheSame
) {
213 builder
.withAvatarUrlPath(avatarPath
.orElse(null));
215 newProfile
= builder
.build();
219 if (avatar
!= null) {
220 if (avatar
.isPresent()) {
221 try (final var streamDetails
= Utils
.createStreamDetails(avatar
.get()).first()) {
222 context
.getAvatarStore()
223 .storeProfileAvatar(account
.getSelfRecipientAddress(),
224 outputStream
-> IOUtils
.copyStream(streamDetails
.getStream(), outputStream
));
227 context
.getAvatarStore().deleteProfileAvatar(account
.getSelfRecipientAddress());
230 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
233 public Profile
getSelfProfile() {
234 return getRecipientProfile(account
.getSelfRecipientId());
237 private List
<Profile
> getRecipientProfiles(Collection
<RecipientId
> recipientIds
, boolean force
) {
238 final var profileStore
= account
.getProfileStore();
239 final var profileFetches
= Flowable
.fromIterable(recipientIds
)
240 .filter(recipientId
-> force
|| isProfileRefreshRequired(profileStore
.getProfile(recipientId
)))
241 .map(recipientId
-> retrieveProfile(recipientId
,
242 SignalServiceProfile
.RequestType
.PROFILE
).onErrorComplete());
243 Maybe
.merge(profileFetches
, 10).blockingSubscribe();
245 return recipientIds
.stream().map(profileStore
::getProfile
).toList();
248 private Profile
getRecipientProfile(RecipientId recipientId
, boolean force
) {
249 var profile
= account
.getProfileStore().getProfile(recipientId
);
251 if (!force
&& !isProfileRefreshRequired(profile
)) {
256 blockingGetProfile(retrieveProfile(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
));
257 } catch (IOException e
) {
258 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
261 return account
.getProfileStore().getProfile(recipientId
);
264 private boolean isProfileRefreshRequired(final Profile profile
) {
265 if (profile
== null) {
268 // Profiles are cached for 6h before retrieving them again, unless forced
269 final var now
= System
.currentTimeMillis();
270 return now
- profile
.getLastUpdateTimestamp() >= 6 * 60 * 60 * 1000;
273 private Profile
decryptProfileAndDownloadAvatar(
274 final RecipientId recipientId
,
275 final ProfileKey profileKey
,
276 final SignalServiceProfile encryptedProfile
278 final var avatarPath
= encryptedProfile
.getAvatar();
279 downloadProfileAvatar(recipientId
, avatarPath
, profileKey
);
281 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
284 public void downloadProfileAvatar(
285 final RecipientId recipientId
,
286 final String avatarPath
,
287 final ProfileKey profileKey
289 var profile
= account
.getProfileStore().getProfile(recipientId
);
290 if (profile
== null || !Objects
.equals(avatarPath
, profile
.getAvatarUrlPath())) {
291 logger
.trace("Downloading profile avatar for {}", recipientId
);
292 downloadProfileAvatar(account
.getRecipientAddressResolver().resolveRecipientAddress(recipientId
),
295 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
296 account
.getProfileStore().storeProfile(recipientId
, builder
.withAvatarUrlPath(avatarPath
).build());
300 private ProfileAndCredential
blockingGetProfile(Single
<ProfileAndCredential
> profile
) throws IOException
{
302 return profile
.blockingGet();
303 } catch (RuntimeException e
) {
304 if (e
.getCause() instanceof PushNetworkException
) {
305 throw (PushNetworkException
) e
.getCause();
306 } else if (e
.getCause() instanceof NotFoundException
) {
307 throw (NotFoundException
) e
.getCause();
309 throw new IOException(e
);
314 private Single
<ProfileAndCredential
> retrieveProfile(
315 RecipientId recipientId
,
316 SignalServiceProfile
.RequestType requestType
318 var unidentifiedAccess
= getUnidentifiedAccess(recipientId
);
319 var profileKey
= Optional
.ofNullable(account
.getProfileStore().getProfileKey(recipientId
));
321 logger
.trace("Retrieving profile for {} {}",
323 profileKey
.isPresent() ?
"with profile key" : "without profile key");
324 final var address
= context
.getRecipientHelper().resolveSignalServiceAddress(recipientId
);
325 return retrieveProfile(address
, profileKey
, unidentifiedAccess
, requestType
).doOnSuccess(p
-> {
326 logger
.trace("Got new profile for {}", recipientId
);
327 final var encryptedProfile
= p
.getProfile();
329 if (requestType
== SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
330 || !ExpiringProfileCredentialUtil
.isValid(account
.getProfileStore()
331 .getExpiringProfileKeyCredential(recipientId
))) {
332 logger
.trace("Storing profile credential");
333 final var profileKeyCredential
= p
.getExpiringProfileKeyCredential().orElse(null);
334 account
.getProfileStore().storeExpiringProfileKeyCredential(recipientId
, profileKeyCredential
);
337 final var profile
= account
.getProfileStore().getProfile(recipientId
);
339 if (recipientId
.equals(account
.getSelfRecipientId())) {
340 final var isUnrestricted
= encryptedProfile
.isUnrestrictedUnidentifiedAccess();
341 if (account
.isUnrestrictedUnidentifiedAccess() != isUnrestricted
) {
342 account
.setUnrestrictedUnidentifiedAccess(isUnrestricted
);
346 Profile newProfile
= null;
347 if (profileKey
.isPresent()) {
348 logger
.trace("Decrypting profile");
349 newProfile
= decryptProfileAndDownloadAvatar(recipientId
, profileKey
.get(), encryptedProfile
);
352 if (newProfile
== null) {
354 profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
)
355 ).withLastUpdateTimestamp(System
.currentTimeMillis())
356 .withUnidentifiedAccessMode(ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null))
357 .withCapabilities(ProfileUtils
.getCapabilities(encryptedProfile
))
362 logger
.trace("Storing identity");
363 final var identityKey
= new IdentityKey(Base64
.getDecoder().decode(encryptedProfile
.getIdentityKey()));
364 account
.getIdentityKeyStore().saveIdentity(p
.getProfile().getServiceId(), identityKey
);
365 } catch (InvalidKeyException ignored
) {
366 logger
.warn("Got invalid identity key in profile for {}",
367 context
.getRecipientHelper().resolveSignalServiceAddress(recipientId
).getIdentifier());
370 logger
.trace("Storing profile");
371 account
.getProfileStore().storeProfile(recipientId
, newProfile
);
372 account
.getRecipientStore().markRegistered(recipientId
, true);
374 logger
.trace("Done handling retrieved profile");
376 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
377 final var profile
= account
.getProfileStore().getProfile(recipientId
);
378 final var newProfile
= (
379 profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
)
380 ).withLastUpdateTimestamp(System
.currentTimeMillis())
381 .withUnidentifiedAccessMode(Profile
.UnidentifiedAccessMode
.UNKNOWN
)
382 .withCapabilities(Set
.of())
384 if (e
instanceof NotFoundException
) {
385 logger
.debug("Marking recipient {} as unregistered after 404 profile fetch.", recipientId
);
386 account
.getRecipientStore().markRegistered(recipientId
, false);
389 account
.getProfileStore().storeProfile(recipientId
, newProfile
);
393 private Single
<ProfileAndCredential
> retrieveProfile(
394 SignalServiceAddress address
,
395 Optional
<ProfileKey
> profileKey
,
396 @Nullable SealedSenderAccess unidentifiedAccess
,
397 SignalServiceProfile
.RequestType requestType
399 final var profileService
= dependencies
.getProfileService();
400 final var locale
= Utils
.getDefaultLocale(Locale
.US
);
402 return profileService
.getProfile(address
, profileKey
, unidentifiedAccess
, requestType
, locale
).map(pair
-> {
403 var processor
= new ProfileService
.ProfileResponseProcessor(pair
);
404 if (processor
.hasResult()) {
405 return processor
.getResult();
406 } else if (processor
.notFound()) {
407 throw new NotFoundException("Profile not found");
409 throw pair
.getExecutionError()
410 .or(pair
::getApplicationError
)
411 .orElseThrow(() -> new IOException("Unknown error while retrieving profile"));
416 private void downloadProfileAvatar(RecipientAddress address
, String avatarPath
, ProfileKey profileKey
) {
417 if (avatarPath
== null) {
419 context
.getAvatarStore().deleteProfileAvatar(address
);
420 } catch (IOException e
) {
421 logger
.warn("Failed to delete local profile avatar, ignoring: {}", e
.getMessage());
427 context
.getAvatarStore()
428 .storeProfileAvatar(address
,
429 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
430 } catch (Throwable e
) {
431 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
435 private void retrieveProfileAvatar(
437 ProfileKey profileKey
,
438 OutputStream outputStream
439 ) throws IOException
{
440 var tmpFile
= IOUtils
.createTempFile();
441 try (var input
= dependencies
.getMessageReceiver()
442 .retrieveProfileAvatar(avatarPath
,
445 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
446 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
447 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
450 Files
.delete(tmpFile
.toPath());
451 } catch (IOException e
) {
452 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
459 private @Nullable SealedSenderAccess
getUnidentifiedAccess(RecipientId recipientId
) {
460 return context
.getUnidentifiedAccessHelper().getSealedSenderAccessFor(recipientId
, true);