1 package org
.asamk
.signal
.manager
.util
;
3 import org
.asamk
.signal
.manager
.api
.Pair
;
4 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
5 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
6 import org
.slf4j
.Logger
;
7 import org
.slf4j
.LoggerFactory
;
8 import org
.whispersystems
.signalservice
.api
.crypto
.InvalidCiphertextException
;
9 import org
.whispersystems
.signalservice
.api
.crypto
.ProfileCipher
;
10 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
12 import java
.util
.Base64
;
13 import java
.util
.HashSet
;
15 public class ProfileUtils
{
17 private final static Logger logger
= LoggerFactory
.getLogger(ProfileUtils
.class);
19 public static Profile
decryptProfile(
20 final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
22 var profileCipher
= new ProfileCipher(profileKey
);
24 var name
= decrypt(encryptedProfile
.getName(), profileCipher
);
25 var about
= trimZeros(decrypt(encryptedProfile
.getAbout(), profileCipher
));
26 var aboutEmoji
= trimZeros(decrypt(encryptedProfile
.getAboutEmoji(), profileCipher
));
28 final var nameParts
= splitName(name
);
29 return new Profile(System
.currentTimeMillis(),
34 encryptedProfile
.getAvatar(),
35 getUnidentifiedAccessMode(encryptedProfile
, profileCipher
),
36 getCapabilities(encryptedProfile
));
37 } catch (InvalidCiphertextException e
) {
38 logger
.debug("Failed to decrypt profile for {}", encryptedProfile
.getServiceId(), e
);
43 public static Profile
.UnidentifiedAccessMode
getUnidentifiedAccessMode(
44 final SignalServiceProfile encryptedProfile
, final ProfileCipher profileCipher
46 if (encryptedProfile
.isUnrestrictedUnidentifiedAccess()) {
47 return Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
50 if (encryptedProfile
.getUnidentifiedAccess() != null && profileCipher
!= null) {
51 final var unidentifiedAccessVerifier
= Base64
.getDecoder().decode(encryptedProfile
.getUnidentifiedAccess());
52 if (profileCipher
.verifyUnidentifiedAccess(unidentifiedAccessVerifier
)) {
53 return Profile
.UnidentifiedAccessMode
.ENABLED
;
57 return Profile
.UnidentifiedAccessMode
.DISABLED
;
60 public static HashSet
<Profile
.Capability
> getCapabilities(final SignalServiceProfile encryptedProfile
) {
61 final var capabilities
= new HashSet
<Profile
.Capability
>();
62 if (encryptedProfile
.getCapabilities().isGv1Migration()) {
63 capabilities
.add(Profile
.Capability
.gv1Migration
);
65 if (encryptedProfile
.getCapabilities().isStorage()) {
66 capabilities
.add(Profile
.Capability
.storage
);
68 if (encryptedProfile
.getCapabilities().isSenderKey()) {
69 capabilities
.add(Profile
.Capability
.senderKey
);
71 if (encryptedProfile
.getCapabilities().isAnnouncementGroup()) {
72 capabilities
.add(Profile
.Capability
.announcementGroup
);
78 private static String
decrypt(
79 final String encryptedName
, final ProfileCipher profileCipher
80 ) throws InvalidCiphertextException
{
82 return encryptedName
== null
84 : new String(profileCipher
.decrypt(Base64
.getDecoder().decode(encryptedName
)));
85 } catch (IllegalArgumentException e
) {
90 private static Pair
<String
, String
> splitName(String name
) {
92 return new Pair
<>(null, null);
94 String
[] parts
= name
.split("\0");
96 return switch (parts
.length
) {
97 case 0 -> new Pair
<>(null, null);
98 case 1 -> new Pair
<>(parts
[0], null);
99 default -> new Pair
<>(parts
[0], parts
[1]);
103 static String
trimZeros(String str
) {
108 int pos
= str
.indexOf(0);
109 return pos
== -1 ? str
: str
.substring(0, pos
);