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