]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/util/ProfileUtils.java
814bd7fa397010d059857619af0682ee58dbc303
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / ProfileUtils.java
1 package org.asamk.signal.manager.util;
2
3 import org.asamk.signal.manager.storage.recipients.Profile;
4 import org.signal.zkgroup.profiles.ProfileKey;
5 import org.whispersystems.libsignal.util.Pair;
6 import org.whispersystems.signalservice.api.crypto.InvalidCiphertextException;
7 import org.whispersystems.signalservice.api.crypto.ProfileCipher;
8 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
9
10 import java.util.Base64;
11 import java.util.HashSet;
12
13 public class ProfileUtils {
14
15 public static Profile decryptProfile(
16 final ProfileKey profileKey, final SignalServiceProfile encryptedProfile
17 ) {
18 var profileCipher = new ProfileCipher(profileKey);
19 try {
20 var name = decrypt(encryptedProfile.getName(), profileCipher);
21 var about = decrypt(encryptedProfile.getAbout(), profileCipher);
22 var aboutEmoji = decrypt(encryptedProfile.getAboutEmoji(), profileCipher);
23
24 final var nameParts = splitName(name);
25 return new Profile(System.currentTimeMillis(),
26 nameParts.first(),
27 nameParts.second(),
28 about,
29 aboutEmoji,
30 getUnidentifiedAccessMode(encryptedProfile, profileCipher),
31 getCapabilities(encryptedProfile));
32 } catch (InvalidCiphertextException e) {
33 return null;
34 }
35 }
36
37 public static Profile.UnidentifiedAccessMode getUnidentifiedAccessMode(
38 final SignalServiceProfile encryptedProfile, final ProfileCipher profileCipher
39 ) {
40 if (encryptedProfile.isUnrestrictedUnidentifiedAccess()) {
41 return Profile.UnidentifiedAccessMode.UNRESTRICTED;
42 }
43
44 if (encryptedProfile.getUnidentifiedAccess() != null && profileCipher != null) {
45 final var unidentifiedAccessVerifier = Base64.getDecoder().decode(encryptedProfile.getUnidentifiedAccess());
46 if (profileCipher.verifyUnidentifiedAccess(unidentifiedAccessVerifier)) {
47 return Profile.UnidentifiedAccessMode.ENABLED;
48 }
49 }
50
51 return Profile.UnidentifiedAccessMode.DISABLED;
52 }
53
54 public static HashSet<Profile.Capability> getCapabilities(final SignalServiceProfile encryptedProfile) {
55 final var capabilities = new HashSet<Profile.Capability>();
56 if (encryptedProfile.getCapabilities().isGv1Migration()) {
57 capabilities.add(Profile.Capability.gv1Migration);
58 }
59 if (encryptedProfile.getCapabilities().isGv2()) {
60 capabilities.add(Profile.Capability.gv2);
61 }
62 if (encryptedProfile.getCapabilities().isStorage()) {
63 capabilities.add(Profile.Capability.storage);
64 }
65 return capabilities;
66 }
67
68 private static String decrypt(
69 final String encryptedName, final ProfileCipher profileCipher
70 ) throws InvalidCiphertextException {
71 try {
72 return encryptedName == null
73 ? null
74 : new String(profileCipher.decrypt(Base64.getDecoder().decode(encryptedName)));
75 } catch (IllegalArgumentException e) {
76 return null;
77 }
78 }
79
80 private static Pair<String, String> splitName(String name) {
81 if (name == null) {
82 return new Pair<>(null, null);
83 }
84 String[] parts = name.split("\0");
85
86 switch (parts.length) {
87 case 0:
88 return new Pair<>(null, null);
89 case 1:
90 return new Pair<>(parts[0], null);
91 default:
92 return new Pair<>(parts[0], parts[1]);
93 }
94 }
95 }