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
.Date
;
12 import java
.util
.HashSet
;
14 public class ProfileUtils
{
16 public static Profile
decryptProfile(
17 final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
19 var profileCipher
= new ProfileCipher(profileKey
);
21 var name
= decrypt(encryptedProfile
.getName(), profileCipher
);
22 var about
= decrypt(encryptedProfile
.getAbout(), profileCipher
);
23 var aboutEmoji
= decrypt(encryptedProfile
.getAboutEmoji(), profileCipher
);
25 final var nameParts
= splitName(name
);
26 return new Profile(new Date().getTime(),
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
);
69 private static String
decrypt(
70 final String encryptedName
, final ProfileCipher profileCipher
71 ) throws InvalidCiphertextException
{
73 return encryptedName
== null
75 : new String(profileCipher
.decrypt(Base64
.getDecoder().decode(encryptedName
)));
76 } catch (IllegalArgumentException e
) {
81 private static Pair
<String
, String
> splitName(String name
) {
83 return new Pair
<>(null, null);
85 String
[] parts
= name
.split("\0");
87 switch (parts
.length
) {
89 return new Pair
<>(null, null);
91 return new Pair
<>(parts
[0], null);
93 return new Pair
<>(parts
[0], parts
[1]);