1 package org
.asamk
.signal
.manager
.util
;
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
;
10 import java
.util
.Base64
;
11 import java
.util
.HashSet
;
13 public class ProfileUtils
{
15 public static Profile
decryptProfile(
16 final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
18 var profileCipher
= new ProfileCipher(profileKey
);
20 var name
= decrypt(encryptedProfile
.getName(), profileCipher
);
21 var about
= trimZeros(decrypt(encryptedProfile
.getAbout(), profileCipher
));
22 var aboutEmoji
= trimZeros(decrypt(encryptedProfile
.getAboutEmoji(), profileCipher
));
24 final var nameParts
= splitName(name
);
25 return new Profile(System
.currentTimeMillis(),
30 encryptedProfile
.getAvatar(),
31 getUnidentifiedAccessMode(encryptedProfile
, profileCipher
),
32 getCapabilities(encryptedProfile
));
33 } catch (InvalidCiphertextException e
) {
38 public static Profile
.UnidentifiedAccessMode
getUnidentifiedAccessMode(
39 final SignalServiceProfile encryptedProfile
, final ProfileCipher profileCipher
41 if (encryptedProfile
.isUnrestrictedUnidentifiedAccess()) {
42 return Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
45 if (encryptedProfile
.getUnidentifiedAccess() != null && profileCipher
!= null) {
46 final var unidentifiedAccessVerifier
= Base64
.getDecoder().decode(encryptedProfile
.getUnidentifiedAccess());
47 if (profileCipher
.verifyUnidentifiedAccess(unidentifiedAccessVerifier
)) {
48 return Profile
.UnidentifiedAccessMode
.ENABLED
;
52 return Profile
.UnidentifiedAccessMode
.DISABLED
;
55 public static HashSet
<Profile
.Capability
> getCapabilities(final SignalServiceProfile encryptedProfile
) {
56 final var capabilities
= new HashSet
<Profile
.Capability
>();
57 if (encryptedProfile
.getCapabilities().isGv1Migration()) {
58 capabilities
.add(Profile
.Capability
.gv1Migration
);
60 if (encryptedProfile
.getCapabilities().isGv2()) {
61 capabilities
.add(Profile
.Capability
.gv2
);
63 if (encryptedProfile
.getCapabilities().isStorage()) {
64 capabilities
.add(Profile
.Capability
.storage
);
66 if (encryptedProfile
.getCapabilities().isSenderKey()) {
67 capabilities
.add(Profile
.Capability
.senderKey
);
69 if (encryptedProfile
.getCapabilities().isAnnouncementGroup()) {
70 capabilities
.add(Profile
.Capability
.announcementGroup
);
76 private static String
decrypt(
77 final String encryptedName
, final ProfileCipher profileCipher
78 ) throws InvalidCiphertextException
{
80 return encryptedName
== null
82 : new String(profileCipher
.decrypt(Base64
.getDecoder().decode(encryptedName
)));
83 } catch (IllegalArgumentException e
) {
88 private static Pair
<String
, String
> splitName(String name
) {
90 return new Pair
<>(null, null);
92 String
[] parts
= name
.split("\0");
94 switch (parts
.length
) {
96 return new Pair
<>(null, null);
98 return new Pair
<>(parts
[0], null);
100 return new Pair
<>(parts
[0], parts
[1]);
104 static String
trimZeros(String str
) {
109 int pos
= str
.indexOf(0);
110 return pos
== -1 ? str
: str
.substring(0, pos
);