1 package org
.asamk
.signal
.manager
.util
;
3 import com
.google
.protobuf
.InvalidProtocolBufferException
;
5 import org
.asamk
.signal
.manager
.api
.Pair
;
6 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
7 import org
.signal
.libsignal
.protocol
.IdentityKey
;
8 import org
.signal
.libsignal
.protocol
.InvalidKeyException
;
9 import org
.signal
.libsignal
.protocol
.ecc
.ECPublicKey
;
10 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
11 import org
.slf4j
.Logger
;
12 import org
.slf4j
.LoggerFactory
;
13 import org
.whispersystems
.signalservice
.api
.crypto
.InvalidCiphertextException
;
14 import org
.whispersystems
.signalservice
.api
.crypto
.ProfileCipher
;
15 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
16 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
18 import java
.io
.IOException
;
19 import java
.util
.Base64
;
20 import java
.util
.HashSet
;
22 public class ProfileUtils
{
24 private final static Logger logger
= LoggerFactory
.getLogger(ProfileUtils
.class);
26 public static Profile
decryptProfile(
27 final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
29 var profileCipher
= new ProfileCipher(profileKey
);
30 IdentityKey identityKey
= null;
32 identityKey
= new IdentityKey(Base64
.getDecoder().decode(encryptedProfile
.getIdentityKey()), 0);
33 } catch (InvalidKeyException ignored
) {
37 var name
= decrypt(encryptedProfile
.getName(), profileCipher
);
38 var about
= trimZeros(decrypt(encryptedProfile
.getAbout(), profileCipher
));
39 var aboutEmoji
= trimZeros(decrypt(encryptedProfile
.getAboutEmoji(), profileCipher
));
41 final var nameParts
= splitName(name
);
42 return new Profile(System
.currentTimeMillis(),
47 encryptedProfile
.getAvatar(),
48 identityKey
== null || encryptedProfile
.getPaymentAddress() == null
50 : decryptAndVerifyMobileCoinAddress(encryptedProfile
.getPaymentAddress(),
52 identityKey
.getPublicKey()),
53 getUnidentifiedAccessMode(encryptedProfile
, profileCipher
),
54 getCapabilities(encryptedProfile
));
55 } catch (InvalidCiphertextException e
) {
56 logger
.debug("Failed to decrypt profile for {}", encryptedProfile
.getServiceId(), e
);
61 public static Profile
.UnidentifiedAccessMode
getUnidentifiedAccessMode(
62 final SignalServiceProfile encryptedProfile
, final ProfileCipher profileCipher
64 if (encryptedProfile
.isUnrestrictedUnidentifiedAccess()) {
65 return Profile
.UnidentifiedAccessMode
.UNRESTRICTED
;
68 if (encryptedProfile
.getUnidentifiedAccess() != null && profileCipher
!= null) {
69 final var unidentifiedAccessVerifier
= Base64
.getDecoder().decode(encryptedProfile
.getUnidentifiedAccess());
70 if (profileCipher
.verifyUnidentifiedAccess(unidentifiedAccessVerifier
)) {
71 return Profile
.UnidentifiedAccessMode
.ENABLED
;
75 return Profile
.UnidentifiedAccessMode
.DISABLED
;
78 public static HashSet
<Profile
.Capability
> getCapabilities(final SignalServiceProfile encryptedProfile
) {
79 final var capabilities
= new HashSet
<Profile
.Capability
>();
80 if (encryptedProfile
.getCapabilities().isGv1Migration()) {
81 capabilities
.add(Profile
.Capability
.gv1Migration
);
83 if (encryptedProfile
.getCapabilities().isStorage()) {
84 capabilities
.add(Profile
.Capability
.storage
);
86 if (encryptedProfile
.getCapabilities().isSenderKey()) {
87 capabilities
.add(Profile
.Capability
.senderKey
);
89 if (encryptedProfile
.getCapabilities().isAnnouncementGroup()) {
90 capabilities
.add(Profile
.Capability
.announcementGroup
);
96 private static String
decrypt(
97 final String encryptedName
, final ProfileCipher profileCipher
98 ) throws InvalidCiphertextException
{
100 return encryptedName
== null
102 : new String(profileCipher
.decrypt(Base64
.getDecoder().decode(encryptedName
)));
103 } catch (IllegalArgumentException e
) {
108 private static byte[] decryptAndVerifyMobileCoinAddress(
109 final byte[] encryptedPaymentAddress
, final ProfileCipher profileCipher
, final ECPublicKey publicKey
110 ) throws InvalidCiphertextException
{
113 decrypted
= profileCipher
.decryptWithLength(encryptedPaymentAddress
);
114 } catch (IOException e
) {
118 SignalServiceProtos
.PaymentAddress paymentAddress
;
120 paymentAddress
= SignalServiceProtos
.PaymentAddress
.parseFrom(decrypted
);
121 } catch (InvalidProtocolBufferException e
) {
125 return PaymentUtils
.verifyPaymentsAddress(paymentAddress
, publicKey
);
128 private static Pair
<String
, String
> splitName(String name
) {
130 return new Pair
<>(null, null);
132 String
[] parts
= name
.split("\0");
134 return switch (parts
.length
) {
135 case 0 -> new Pair
<>(null, null);
136 case 1 -> new Pair
<>(parts
[0], null);
137 default -> new Pair
<>(parts
[0], parts
[1]);
141 static String
trimZeros(String str
) {
146 int pos
= str
.indexOf(0);
147 return pos
== -1 ? str
: str
.substring(0, pos
);