]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java
8113000b182912e365f4086d669e21a9e3e7c252
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / ProfileHelper.java
1 package org.asamk.signal.manager.helper;
2
3 import org.asamk.signal.manager.SignalDependencies;
4 import org.asamk.signal.manager.config.ServiceConfig;
5 import org.asamk.signal.manager.groups.GroupNotFoundException;
6 import org.asamk.signal.manager.groups.NotAGroupMemberException;
7 import org.asamk.signal.manager.storage.SignalAccount;
8 import org.asamk.signal.manager.storage.groups.GroupInfoV2;
9 import org.asamk.signal.manager.storage.recipients.Profile;
10 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
11 import org.asamk.signal.manager.storage.recipients.RecipientId;
12 import org.asamk.signal.manager.util.IOUtils;
13 import org.asamk.signal.manager.util.KeyUtils;
14 import org.asamk.signal.manager.util.PaymentUtils;
15 import org.asamk.signal.manager.util.ProfileUtils;
16 import org.asamk.signal.manager.util.Utils;
17 import org.signal.libsignal.protocol.IdentityKey;
18 import org.signal.libsignal.protocol.InvalidKeyException;
19 import org.signal.libsignal.zkgroup.profiles.ExpiringProfileKeyCredential;
20 import org.signal.libsignal.zkgroup.profiles.ProfileKey;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
24 import org.whispersystems.signalservice.api.profiles.AvatarUploadParams;
25 import org.whispersystems.signalservice.api.profiles.ProfileAndCredential;
26 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
27 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
28 import org.whispersystems.signalservice.api.push.exceptions.NotFoundException;
29 import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
30 import org.whispersystems.signalservice.api.services.ProfileService;
31 import org.whispersystems.signalservice.api.util.ExpiringProfileCredentialUtil;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.nio.file.Files;
37 import java.util.Base64;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.Set;
44
45 import io.reactivex.rxjava3.core.Flowable;
46 import io.reactivex.rxjava3.core.Maybe;
47 import io.reactivex.rxjava3.core.Single;
48
49 public final class ProfileHelper {
50
51 private final static Logger logger = LoggerFactory.getLogger(ProfileHelper.class);
52
53 private final SignalAccount account;
54 private final SignalDependencies dependencies;
55 private final Context context;
56
57 public ProfileHelper(final Context context) {
58 this.account = context.getAccount();
59 this.dependencies = context.getDependencies();
60 this.context = context;
61 }
62
63 public void rotateProfileKey() throws IOException {
64 // refresh our profile, before creating a new profile key
65 getSelfProfile();
66 var profileKey = KeyUtils.createProfileKey();
67 account.setProfileKey(profileKey);
68 context.getAccountHelper().updateAccountAttributes();
69 setProfile(true, true, null, null, null, null, null, null);
70 // TODO update profile key in storage
71
72 final var recipientIds = account.getRecipientStore().getRecipientIdsWithEnabledProfileSharing();
73 for (final var recipientId : recipientIds) {
74 context.getSendHelper().sendProfileKey(recipientId);
75 }
76
77 final var selfRecipientId = account.getSelfRecipientId();
78 final var activeGroupIds = account.getGroupStore()
79 .getGroups()
80 .stream()
81 .filter(g -> g instanceof GroupInfoV2 && g.isMember(selfRecipientId))
82 .map(g -> (GroupInfoV2) g)
83 .map(GroupInfoV2::getGroupId)
84 .toList();
85 for (final var groupId : activeGroupIds) {
86 try {
87 context.getGroupHelper().updateGroupProfileKey(groupId);
88 } catch (GroupNotFoundException | NotAGroupMemberException | IOException e) {
89 logger.warn("Failed to update group profile key: {}", e.getMessage());
90 }
91 }
92 }
93
94 public Profile getRecipientProfile(RecipientId recipientId) {
95 return getRecipientProfile(recipientId, false);
96 }
97
98 public List<Profile> getRecipientProfiles(Collection<RecipientId> recipientIds) {
99 return getRecipientProfiles(recipientIds, false);
100 }
101
102 public void refreshRecipientProfile(RecipientId recipientId) {
103 getRecipientProfile(recipientId, true);
104 }
105
106 public void refreshRecipientProfiles(Collection<RecipientId> recipientIds) {
107 getRecipientProfiles(recipientIds, true);
108 }
109
110 public List<ExpiringProfileKeyCredential> getExpiringProfileKeyCredential(List<RecipientId> recipientIds) {
111 final var profileFetches = Flowable.fromIterable(recipientIds)
112 .filter(recipientId -> !ExpiringProfileCredentialUtil.isValid(account.getProfileStore()
113 .getExpiringProfileKeyCredential(recipientId)))
114 .map(recipientId -> retrieveProfile(recipientId,
115 SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL).onErrorComplete());
116 Maybe.merge(profileFetches, 10).blockingSubscribe();
117
118 return recipientIds.stream().map(r -> account.getProfileStore().getExpiringProfileKeyCredential(r)).toList();
119 }
120
121 public ExpiringProfileKeyCredential getExpiringProfileKeyCredential(RecipientId recipientId) {
122 var profileKeyCredential = account.getProfileStore().getExpiringProfileKeyCredential(recipientId);
123 if (ExpiringProfileCredentialUtil.isValid(profileKeyCredential)) {
124 return profileKeyCredential;
125 }
126
127 try {
128 blockingGetProfile(retrieveProfile(recipientId, SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL));
129 } catch (IOException e) {
130 logger.warn("Failed to retrieve profile key credential, ignoring: {}", e.getMessage());
131 return null;
132 }
133
134 return account.getProfileStore().getExpiringProfileKeyCredential(recipientId);
135 }
136
137 /**
138 * @param givenName if null, the previous givenName will be kept
139 * @param familyName if null, the previous familyName will be kept
140 * @param about if null, the previous about text will be kept
141 * @param aboutEmoji if null, the previous about emoji will be kept
142 * @param avatar if avatar is null the image from the local avatar store is used (if present),
143 */
144 public void setProfile(
145 String givenName,
146 final String familyName,
147 String about,
148 String aboutEmoji,
149 Optional<File> avatar,
150 byte[] mobileCoinAddress
151 ) throws IOException {
152 setProfile(true, false, givenName, familyName, about, aboutEmoji, avatar, mobileCoinAddress);
153 }
154
155 public void setProfile(
156 boolean uploadProfile,
157 boolean forceUploadAvatar,
158 String givenName,
159 final String familyName,
160 String about,
161 String aboutEmoji,
162 Optional<File> avatar,
163 byte[] mobileCoinAddress
164 ) throws IOException {
165 var profile = getSelfProfile();
166 var builder = profile == null ? Profile.newBuilder() : Profile.newBuilder(profile);
167 if (givenName != null) {
168 builder.withGivenName(givenName);
169 }
170 if (familyName != null) {
171 builder.withFamilyName(familyName);
172 }
173 if (about != null) {
174 builder.withAbout(about);
175 }
176 if (aboutEmoji != null) {
177 builder.withAboutEmoji(aboutEmoji);
178 }
179 if (mobileCoinAddress != null) {
180 builder.withMobileCoinAddress(mobileCoinAddress);
181 }
182 var newProfile = builder.build();
183
184 if (uploadProfile) {
185 final var streamDetails = avatar != null && avatar.isPresent()
186 ? Utils.createStreamDetailsFromFile(avatar.get())
187 : forceUploadAvatar && avatar == null ? context.getAvatarStore()
188 .retrieveProfileAvatar(account.getSelfRecipientAddress()) : null;
189 try (streamDetails) {
190 final var avatarUploadParams = streamDetails != null
191 ? AvatarUploadParams.forAvatar(streamDetails)
192 : avatar == null ? AvatarUploadParams.unchanged(true) : AvatarUploadParams.unchanged(false);
193 final var paymentsAddress = Optional.ofNullable(newProfile.getMobileCoinAddress())
194 .map(address -> PaymentUtils.signPaymentsAddress(address,
195 account.getAciIdentityKeyPair().getPrivateKey()));
196 logger.debug("Uploading new profile");
197 final var avatarPath = dependencies.getAccountManager()
198 .setVersionedProfile(account.getAci(),
199 account.getProfileKey(),
200 newProfile.getInternalServiceName(),
201 newProfile.getAbout() == null ? "" : newProfile.getAbout(),
202 newProfile.getAboutEmoji() == null ? "" : newProfile.getAboutEmoji(),
203 paymentsAddress,
204 avatarUploadParams,
205 List.of(/* TODO implement support for badges */));
206 if (!avatarUploadParams.keepTheSame) {
207 builder.withAvatarUrlPath(avatarPath.orElse(null));
208 }
209 newProfile = builder.build();
210 }
211 }
212
213 if (avatar != null) {
214 if (avatar.isPresent()) {
215 context.getAvatarStore()
216 .storeProfileAvatar(account.getSelfRecipientAddress(),
217 outputStream -> IOUtils.copyFileToStream(avatar.get(), outputStream));
218 } else {
219 context.getAvatarStore().deleteProfileAvatar(account.getSelfRecipientAddress());
220 }
221 }
222 account.getProfileStore().storeProfile(account.getSelfRecipientId(), newProfile);
223 }
224
225 public Profile getSelfProfile() {
226 return getRecipientProfile(account.getSelfRecipientId());
227 }
228
229 private List<Profile> getRecipientProfiles(Collection<RecipientId> recipientIds, boolean force) {
230 final var profileStore = account.getProfileStore();
231 final var profileFetches = Flowable.fromIterable(recipientIds)
232 .filter(recipientId -> force || isProfileRefreshRequired(profileStore.getProfile(recipientId)))
233 .map(recipientId -> retrieveProfile(recipientId,
234 SignalServiceProfile.RequestType.PROFILE).onErrorComplete());
235 Maybe.merge(profileFetches, 10).blockingSubscribe();
236
237 return recipientIds.stream().map(profileStore::getProfile).toList();
238 }
239
240 private Profile getRecipientProfile(RecipientId recipientId, boolean force) {
241 var profile = account.getProfileStore().getProfile(recipientId);
242
243 if (!force && !isProfileRefreshRequired(profile)) {
244 return profile;
245 }
246
247 try {
248 blockingGetProfile(retrieveProfile(recipientId, SignalServiceProfile.RequestType.PROFILE));
249 } catch (IOException e) {
250 logger.warn("Failed to retrieve profile, ignoring: {}", e.getMessage());
251 }
252
253 return account.getProfileStore().getProfile(recipientId);
254 }
255
256 private boolean isProfileRefreshRequired(final Profile profile) {
257 if (profile == null) {
258 return true;
259 }
260 // Profiles are cached for 6h before retrieving them again, unless forced
261 final var now = System.currentTimeMillis();
262 return now - profile.getLastUpdateTimestamp() >= 6 * 60 * 60 * 1000;
263 }
264
265 private Profile decryptProfileAndDownloadAvatar(
266 final RecipientId recipientId, final ProfileKey profileKey, final SignalServiceProfile encryptedProfile
267 ) {
268 final var avatarPath = encryptedProfile.getAvatar();
269 downloadProfileAvatar(recipientId, avatarPath, profileKey);
270
271 return ProfileUtils.decryptProfile(profileKey, encryptedProfile);
272 }
273
274 public void downloadProfileAvatar(
275 final RecipientId recipientId, final String avatarPath, final ProfileKey profileKey
276 ) {
277 var profile = account.getProfileStore().getProfile(recipientId);
278 if (profile == null || !Objects.equals(avatarPath, profile.getAvatarUrlPath())) {
279 logger.trace("Downloading profile avatar for {}", recipientId);
280 downloadProfileAvatar(account.getRecipientAddressResolver().resolveRecipientAddress(recipientId),
281 avatarPath,
282 profileKey);
283 var builder = profile == null ? Profile.newBuilder() : Profile.newBuilder(profile);
284 account.getProfileStore().storeProfile(recipientId, builder.withAvatarUrlPath(avatarPath).build());
285 }
286 }
287
288 private ProfileAndCredential blockingGetProfile(Single<ProfileAndCredential> profile) throws IOException {
289 try {
290 return profile.blockingGet();
291 } catch (RuntimeException e) {
292 if (e.getCause() instanceof PushNetworkException) {
293 throw (PushNetworkException) e.getCause();
294 } else if (e.getCause() instanceof NotFoundException) {
295 throw (NotFoundException) e.getCause();
296 } else {
297 throw new IOException(e);
298 }
299 }
300 }
301
302 private Single<ProfileAndCredential> retrieveProfile(
303 RecipientId recipientId, SignalServiceProfile.RequestType requestType
304 ) {
305 var unidentifiedAccess = getUnidentifiedAccess(recipientId);
306 var profileKey = Optional.ofNullable(account.getProfileStore().getProfileKey(recipientId));
307
308 logger.trace("Retrieving profile for {} {}",
309 recipientId,
310 profileKey.isPresent() ? "with profile key" : "without profile key");
311 final var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
312 return retrieveProfile(address, profileKey, unidentifiedAccess, requestType).doOnSuccess(p -> {
313 logger.trace("Got new profile for {}", recipientId);
314 final var encryptedProfile = p.getProfile();
315
316 if (requestType == SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL
317 || !ExpiringProfileCredentialUtil.isValid(account.getProfileStore()
318 .getExpiringProfileKeyCredential(recipientId))) {
319 logger.trace("Storing profile credential");
320 final var profileKeyCredential = p.getExpiringProfileKeyCredential().orElse(null);
321 account.getProfileStore().storeExpiringProfileKeyCredential(recipientId, profileKeyCredential);
322 }
323
324 final var profile = account.getProfileStore().getProfile(recipientId);
325
326 Profile newProfile = null;
327 if (profileKey.isPresent()) {
328 logger.trace("Decrypting profile");
329 newProfile = decryptProfileAndDownloadAvatar(recipientId, profileKey.get(), encryptedProfile);
330 }
331
332 if (newProfile == null) {
333 newProfile = (
334 profile == null ? Profile.newBuilder() : Profile.newBuilder(profile)
335 ).withLastUpdateTimestamp(System.currentTimeMillis())
336 .withUnidentifiedAccessMode(ProfileUtils.getUnidentifiedAccessMode(encryptedProfile, null))
337 .withCapabilities(ProfileUtils.getCapabilities(encryptedProfile))
338 .build();
339 }
340
341 try {
342 logger.trace("Storing identity");
343 final var identityKey = new IdentityKey(Base64.getDecoder().decode(encryptedProfile.getIdentityKey()));
344 account.getIdentityKeyStore().saveIdentity(p.getProfile().getServiceId(), identityKey);
345 } catch (InvalidKeyException ignored) {
346 logger.warn("Got invalid identity key in profile for {}",
347 context.getRecipientHelper().resolveSignalServiceAddress(recipientId).getIdentifier());
348 }
349
350 logger.trace("Storing profile");
351 account.getProfileStore().storeProfile(recipientId, newProfile);
352
353 logger.trace("Done handling retrieved profile");
354 }).doOnError(e -> {
355 logger.warn("Failed to retrieve profile, ignoring: {}", e.getMessage());
356 final var profile = account.getProfileStore().getProfile(recipientId);
357 final var newProfile = (
358 profile == null ? Profile.newBuilder() : Profile.newBuilder(profile)
359 ).withLastUpdateTimestamp(System.currentTimeMillis())
360 .withUnidentifiedAccessMode(Profile.UnidentifiedAccessMode.UNKNOWN)
361 .withCapabilities(Set.of())
362 .build();
363
364 account.getProfileStore().storeProfile(recipientId, newProfile);
365 });
366 }
367
368 private Single<ProfileAndCredential> retrieveProfile(
369 SignalServiceAddress address,
370 Optional<ProfileKey> profileKey,
371 Optional<UnidentifiedAccess> unidentifiedAccess,
372 SignalServiceProfile.RequestType requestType
373 ) {
374 final var profileService = dependencies.getProfileService();
375 final var locale = Utils.getDefaultLocale(Locale.US);
376
377 return profileService.getProfile(address, profileKey, unidentifiedAccess, requestType, locale).map(pair -> {
378 var processor = new ProfileService.ProfileResponseProcessor(pair);
379 if (processor.hasResult()) {
380 return processor.getResult();
381 } else if (processor.notFound()) {
382 throw new NotFoundException("Profile not found");
383 } else {
384 throw pair.getExecutionError()
385 .or(pair::getApplicationError)
386 .orElseThrow(() -> new IOException("Unknown error while retrieving profile"));
387 }
388 });
389 }
390
391 private void downloadProfileAvatar(
392 RecipientAddress address, String avatarPath, ProfileKey profileKey
393 ) {
394 if (avatarPath == null) {
395 try {
396 context.getAvatarStore().deleteProfileAvatar(address);
397 } catch (IOException e) {
398 logger.warn("Failed to delete local profile avatar, ignoring: {}", e.getMessage());
399 }
400 return;
401 }
402
403 try {
404 context.getAvatarStore()
405 .storeProfileAvatar(address,
406 outputStream -> retrieveProfileAvatar(avatarPath, profileKey, outputStream));
407 } catch (Throwable e) {
408 logger.warn("Failed to download profile avatar, ignoring: {}", e.getMessage());
409 }
410 }
411
412 private void retrieveProfileAvatar(
413 String avatarPath, ProfileKey profileKey, OutputStream outputStream
414 ) throws IOException {
415 var tmpFile = IOUtils.createTempFile();
416 try (var input = dependencies.getMessageReceiver()
417 .retrieveProfileAvatar(avatarPath,
418 tmpFile,
419 profileKey,
420 ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
421 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
422 IOUtils.copyStream(input, outputStream, (int) ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
423 } finally {
424 try {
425 Files.delete(tmpFile.toPath());
426 } catch (IOException e) {
427 logger.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
428 tmpFile,
429 e.getMessage());
430 }
431 }
432 }
433
434 private Optional<UnidentifiedAccess> getUnidentifiedAccess(RecipientId recipientId) {
435 var unidentifiedAccess = context.getUnidentifiedAccessHelper().getAccessFor(recipientId, true);
436
437 if (unidentifiedAccess.isPresent()) {
438 return unidentifiedAccess.get().getTargetUnidentifiedAccess();
439 }
440
441 return Optional.empty();
442 }
443 }