1 package org
.asamk
.signal
.manager
.helper
;
3 import com
.google
.protobuf
.InvalidProtocolBufferException
;
5 import org
.asamk
.signal
.manager
.groups
.GroupLinkPassword
;
6 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
7 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV2
;
8 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
9 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
10 import org
.asamk
.signal
.manager
.util
.IOUtils
;
11 import org
.signal
.storageservice
.protos
.groups
.AccessControl
;
12 import org
.signal
.storageservice
.protos
.groups
.GroupChange
;
13 import org
.signal
.storageservice
.protos
.groups
.Member
;
14 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
15 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroupChange
;
16 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroupJoinInfo
;
17 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedPendingMember
;
18 import org
.signal
.zkgroup
.InvalidInputException
;
19 import org
.signal
.zkgroup
.VerificationFailedException
;
20 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
21 import org
.signal
.zkgroup
.groups
.GroupSecretParams
;
22 import org
.signal
.zkgroup
.groups
.UuidCiphertext
;
23 import org
.slf4j
.Logger
;
24 import org
.slf4j
.LoggerFactory
;
25 import org
.whispersystems
.libsignal
.util
.Pair
;
26 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
27 import org
.whispersystems
.signalservice
.api
.groupsv2
.DecryptedGroupUtil
;
28 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupCandidate
;
29 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
30 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Api
;
31 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2AuthorizationString
;
32 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
33 import org
.whispersystems
.signalservice
.api
.groupsv2
.InvalidGroupStateException
;
34 import org
.whispersystems
.signalservice
.api
.groupsv2
.NotAbleToApplyGroupV2ChangeException
;
35 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
36 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
39 import java
.io
.FileInputStream
;
40 import java
.io
.IOException
;
41 import java
.io
.InputStream
;
43 import java
.util
.UUID
;
44 import java
.util
.stream
.Collectors
;
46 public class GroupHelper
{
48 private final static Logger logger
= LoggerFactory
.getLogger(GroupHelper
.class);
50 private final ProfileKeyCredentialProvider profileKeyCredentialProvider
;
52 private final ProfileProvider profileProvider
;
54 private final SelfRecipientIdProvider selfRecipientIdProvider
;
56 private final GroupsV2Operations groupsV2Operations
;
58 private final GroupsV2Api groupsV2Api
;
60 private final GroupAuthorizationProvider groupAuthorizationProvider
;
62 private final SignalServiceAddressResolver addressResolver
;
65 final ProfileKeyCredentialProvider profileKeyCredentialProvider
,
66 final ProfileProvider profileProvider
,
67 final SelfRecipientIdProvider selfRecipientIdProvider
,
68 final GroupsV2Operations groupsV2Operations
,
69 final GroupsV2Api groupsV2Api
,
70 final GroupAuthorizationProvider groupAuthorizationProvider
,
71 final SignalServiceAddressResolver addressResolver
73 this.profileKeyCredentialProvider
= profileKeyCredentialProvider
;
74 this.profileProvider
= profileProvider
;
75 this.selfRecipientIdProvider
= selfRecipientIdProvider
;
76 this.groupsV2Operations
= groupsV2Operations
;
77 this.groupsV2Api
= groupsV2Api
;
78 this.groupAuthorizationProvider
= groupAuthorizationProvider
;
79 this.addressResolver
= addressResolver
;
82 public DecryptedGroup
getDecryptedGroup(final GroupSecretParams groupSecretParams
) {
84 final var groupsV2AuthorizationString
= groupAuthorizationProvider
.getAuthorizationForToday(
86 return groupsV2Api
.getGroup(groupSecretParams
, groupsV2AuthorizationString
);
87 } catch (IOException
| VerificationFailedException
| InvalidGroupStateException e
) {
88 logger
.warn("Failed to retrieve Group V2 info, ignoring: {}", e
.getMessage());
93 public DecryptedGroupJoinInfo
getDecryptedGroupJoinInfo(
94 GroupMasterKey groupMasterKey
, GroupLinkPassword password
95 ) throws IOException
, GroupLinkNotActiveException
{
96 var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
98 return groupsV2Api
.getGroupJoinInfo(groupSecretParams
,
99 Optional
.fromNullable(password
).transform(GroupLinkPassword
::serialize
),
100 groupAuthorizationProvider
.getAuthorizationForToday(groupSecretParams
));
103 public GroupInfoV2
createGroupV2(
104 String name
, Set
<RecipientId
> members
, File avatarFile
105 ) throws IOException
{
106 final var avatarBytes
= readAvatarBytes(avatarFile
);
107 final var newGroup
= buildNewGroupV2(name
, members
, avatarBytes
);
108 if (newGroup
== null) {
112 final var groupSecretParams
= newGroup
.getGroupSecretParams();
114 final GroupsV2AuthorizationString groupAuthForToday
;
115 final DecryptedGroup decryptedGroup
;
117 groupAuthForToday
= groupAuthorizationProvider
.getAuthorizationForToday(groupSecretParams
);
118 groupsV2Api
.putNewGroup(newGroup
, groupAuthForToday
);
119 decryptedGroup
= groupsV2Api
.getGroup(groupSecretParams
, groupAuthForToday
);
120 } catch (IOException
| VerificationFailedException
| InvalidGroupStateException e
) {
121 logger
.warn("Failed to create V2 group: {}", e
.getMessage());
124 if (decryptedGroup
== null) {
125 logger
.warn("Failed to create V2 group, unknown error!");
129 final var groupId
= GroupUtils
.getGroupIdV2(groupSecretParams
);
130 final var masterKey
= groupSecretParams
.getMasterKey();
131 var g
= new GroupInfoV2(groupId
, masterKey
);
132 g
.setGroup(decryptedGroup
);
137 private byte[] readAvatarBytes(final File avatarFile
) throws IOException
{
138 final byte[] avatarBytes
;
139 try (InputStream avatar
= avatarFile
== null ?
null : new FileInputStream(avatarFile
)) {
140 avatarBytes
= avatar
== null ?
null : IOUtils
.readFully(avatar
);
145 private GroupsV2Operations
.NewGroup
buildNewGroupV2(
146 String name
, Set
<RecipientId
> members
, byte[] avatar
148 final var profileKeyCredential
= profileKeyCredentialProvider
.getProfileKeyCredential(selfRecipientIdProvider
.getSelfRecipientId());
149 if (profileKeyCredential
== null) {
150 logger
.warn("Cannot create a V2 group as self does not have a versioned profile");
154 if (!areMembersValid(members
)) return null;
156 var self
= new GroupCandidate(addressResolver
.resolveSignalServiceAddress(selfRecipientIdProvider
.getSelfRecipientId())
158 .orNull(), Optional
.fromNullable(profileKeyCredential
));
159 var candidates
= members
.stream()
160 .map(member
-> new GroupCandidate(addressResolver
.resolveSignalServiceAddress(member
).getUuid().get(),
161 Optional
.fromNullable(profileKeyCredentialProvider
.getProfileKeyCredential(member
))))
162 .collect(Collectors
.toSet());
164 final var groupSecretParams
= GroupSecretParams
.generate();
165 return groupsV2Operations
.createNewGroup(groupSecretParams
,
167 Optional
.fromNullable(avatar
),
174 private boolean areMembersValid(final Set
<RecipientId
> members
) {
175 final var noUuidCapability
= members
.stream()
176 .map(addressResolver
::resolveSignalServiceAddress
)
177 .filter(address
-> !address
.getUuid().isPresent())
178 .map(SignalServiceAddress
::getLegacyIdentifier
)
179 .collect(Collectors
.toSet());
180 if (noUuidCapability
.size() > 0) {
181 logger
.warn("Cannot create a V2 group as some members don't have a UUID: {}",
182 String
.join(", ", noUuidCapability
));
186 final var noGv2Capability
= members
.stream()
187 .map(profileProvider
::getProfile
)
188 .filter(profile
-> profile
!= null && !profile
.getCapabilities().contains(Profile
.Capability
.gv2
))
189 .collect(Collectors
.toSet());
190 if (noGv2Capability
.size() > 0) {
191 logger
.warn("Cannot create a V2 group as some members don't support Groups V2: {}",
192 noGv2Capability
.stream().map(Profile
::getDisplayName
).collect(Collectors
.joining(", ")));
199 public Pair
<DecryptedGroup
, GroupChange
> updateGroupV2(
200 GroupInfoV2 groupInfoV2
, String name
, File avatarFile
201 ) throws IOException
{
202 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupInfoV2
.getMasterKey());
203 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
205 var change
= name
!= null ? groupOperations
.createModifyGroupTitle(name
) : GroupChange
.Actions
.newBuilder();
207 if (avatarFile
!= null) {
208 final var avatarBytes
= readAvatarBytes(avatarFile
);
209 var avatarCdnKey
= groupsV2Api
.uploadAvatar(avatarBytes
,
211 groupAuthorizationProvider
.getAuthorizationForToday(groupSecretParams
));
212 change
.setModifyAvatar(GroupChange
.Actions
.ModifyAvatarAction
.newBuilder().setAvatar(avatarCdnKey
));
215 final var uuid
= addressResolver
.resolveSignalServiceAddress(this.selfRecipientIdProvider
.getSelfRecipientId())
217 if (uuid
.isPresent()) {
218 change
.setSourceUuid(UuidUtil
.toByteString(uuid
.get()));
221 return commitChange(groupInfoV2
, change
);
224 public Pair
<DecryptedGroup
, GroupChange
> updateGroupV2(
225 GroupInfoV2 groupInfoV2
, Set
<RecipientId
> newMembers
226 ) throws IOException
{
227 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupInfoV2
.getMasterKey());
228 var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
230 if (!areMembersValid(newMembers
)) {
231 throw new IOException("Failed to update group");
234 var candidates
= newMembers
.stream()
235 .map(member
-> new GroupCandidate(addressResolver
.resolveSignalServiceAddress(member
).getUuid().get(),
236 Optional
.fromNullable(profileKeyCredentialProvider
.getProfileKeyCredential(member
))))
237 .collect(Collectors
.toSet());
239 final var uuid
= addressResolver
.resolveSignalServiceAddress(selfRecipientIdProvider
.getSelfRecipientId())
242 final var change
= groupOperations
.createModifyGroupMembershipChange(candidates
, uuid
);
244 change
.setSourceUuid(UuidUtil
.toByteString(uuid
));
246 return commitChange(groupInfoV2
, change
);
249 public Pair
<DecryptedGroup
, GroupChange
> leaveGroup(GroupInfoV2 groupInfoV2
) throws IOException
{
250 var pendingMembersList
= groupInfoV2
.getGroup().getPendingMembersList();
251 final var selfUuid
= addressResolver
.resolveSignalServiceAddress(selfRecipientIdProvider
.getSelfRecipientId())
254 var selfPendingMember
= DecryptedGroupUtil
.findPendingByUuid(pendingMembersList
, selfUuid
);
256 if (selfPendingMember
.isPresent()) {
257 return revokeInvites(groupInfoV2
, Set
.of(selfPendingMember
.get()));
259 return ejectMembers(groupInfoV2
, Set
.of(selfUuid
));
263 public GroupChange
joinGroup(
264 GroupMasterKey groupMasterKey
,
265 GroupLinkPassword groupLinkPassword
,
266 DecryptedGroupJoinInfo decryptedGroupJoinInfo
267 ) throws IOException
{
268 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupMasterKey
);
269 final var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
271 final var selfRecipientId
= this.selfRecipientIdProvider
.getSelfRecipientId();
272 final var profileKeyCredential
= profileKeyCredentialProvider
.getProfileKeyCredential(selfRecipientId
);
273 if (profileKeyCredential
== null) {
274 throw new IOException("Cannot join a V2 group as self does not have a versioned profile");
277 var requestToJoin
= decryptedGroupJoinInfo
.getAddFromInviteLink() == AccessControl
.AccessRequired
.ADMINISTRATOR
;
278 var change
= requestToJoin
279 ? groupOperations
.createGroupJoinRequest(profileKeyCredential
)
280 : groupOperations
.createGroupJoinDirect(profileKeyCredential
);
282 change
.setSourceUuid(UuidUtil
.toByteString(addressResolver
.resolveSignalServiceAddress(selfRecipientId
)
286 return commitChange(groupSecretParams
, decryptedGroupJoinInfo
.getRevision(), change
, groupLinkPassword
);
289 public Pair
<DecryptedGroup
, GroupChange
> acceptInvite(GroupInfoV2 groupInfoV2
) throws IOException
{
290 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupInfoV2
.getMasterKey());
291 final var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
293 final var selfRecipientId
= this.selfRecipientIdProvider
.getSelfRecipientId();
294 final var profileKeyCredential
= profileKeyCredentialProvider
.getProfileKeyCredential(selfRecipientId
);
295 if (profileKeyCredential
== null) {
296 throw new IOException("Cannot join a V2 group as self does not have a versioned profile");
299 final var change
= groupOperations
.createAcceptInviteChange(profileKeyCredential
);
301 final var uuid
= addressResolver
.resolveSignalServiceAddress(selfRecipientId
).getUuid();
302 if (uuid
.isPresent()) {
303 change
.setSourceUuid(UuidUtil
.toByteString(uuid
.get()));
306 return commitChange(groupInfoV2
, change
);
309 public Pair
<DecryptedGroup
, GroupChange
> revokeInvites(
310 GroupInfoV2 groupInfoV2
, Set
<DecryptedPendingMember
> pendingMembers
311 ) throws IOException
{
312 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupInfoV2
.getMasterKey());
313 final var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
314 final var uuidCipherTexts
= pendingMembers
.stream().map(member
-> {
316 return new UuidCiphertext(member
.getUuidCipherText().toByteArray());
317 } catch (InvalidInputException e
) {
318 throw new AssertionError(e
);
320 }).collect(Collectors
.toSet());
321 return commitChange(groupInfoV2
, groupOperations
.createRemoveInvitationChange(uuidCipherTexts
));
324 public Pair
<DecryptedGroup
, GroupChange
> ejectMembers(GroupInfoV2 groupInfoV2
, Set
<UUID
> uuids
) throws IOException
{
325 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupInfoV2
.getMasterKey());
326 final var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
327 return commitChange(groupInfoV2
, groupOperations
.createRemoveMembersChange(uuids
));
330 private Pair
<DecryptedGroup
, GroupChange
> commitChange(
331 GroupInfoV2 groupInfoV2
, GroupChange
.Actions
.Builder change
332 ) throws IOException
{
333 final var groupSecretParams
= GroupSecretParams
.deriveFromMasterKey(groupInfoV2
.getMasterKey());
334 final var groupOperations
= groupsV2Operations
.forGroup(groupSecretParams
);
335 final var previousGroupState
= groupInfoV2
.getGroup();
336 final var nextRevision
= previousGroupState
.getRevision() + 1;
337 final var changeActions
= change
.setRevision(nextRevision
).build();
338 final DecryptedGroupChange decryptedChange
;
339 final DecryptedGroup decryptedGroupState
;
342 decryptedChange
= groupOperations
.decryptChange(changeActions
,
343 addressResolver
.resolveSignalServiceAddress(selfRecipientIdProvider
.getSelfRecipientId())
346 decryptedGroupState
= DecryptedGroupUtil
.apply(previousGroupState
, decryptedChange
);
347 } catch (VerificationFailedException
| InvalidGroupStateException
| NotAbleToApplyGroupV2ChangeException e
) {
348 throw new IOException(e
);
351 var signedGroupChange
= groupsV2Api
.patchGroup(changeActions
,
352 groupAuthorizationProvider
.getAuthorizationForToday(groupSecretParams
),
355 return new Pair
<>(decryptedGroupState
, signedGroupChange
);
358 private GroupChange
commitChange(
359 GroupSecretParams groupSecretParams
,
361 GroupChange
.Actions
.Builder change
,
362 GroupLinkPassword password
363 ) throws IOException
{
364 final var nextRevision
= currentRevision
+ 1;
365 final var changeActions
= change
.setRevision(nextRevision
).build();
367 return groupsV2Api
.patchGroup(changeActions
,
368 groupAuthorizationProvider
.getAuthorizationForToday(groupSecretParams
),
369 Optional
.fromNullable(password
).transform(GroupLinkPassword
::serialize
));
372 public DecryptedGroup
getUpdatedDecryptedGroup(
373 DecryptedGroup group
, byte[] signedGroupChange
, GroupMasterKey groupMasterKey
376 final var decryptedGroupChange
= getDecryptedGroupChange(signedGroupChange
, groupMasterKey
);
377 if (decryptedGroupChange
== null) {
380 return DecryptedGroupUtil
.apply(group
, decryptedGroupChange
);
381 } catch (NotAbleToApplyGroupV2ChangeException e
) {
386 private DecryptedGroupChange
getDecryptedGroupChange(byte[] signedGroupChange
, GroupMasterKey groupMasterKey
) {
387 if (signedGroupChange
!= null) {
388 var groupOperations
= groupsV2Operations
.forGroup(GroupSecretParams
.deriveFromMasterKey(groupMasterKey
));
391 return groupOperations
.decryptChange(GroupChange
.parseFrom(signedGroupChange
), true).orNull();
392 } catch (VerificationFailedException
| InvalidGroupStateException
| InvalidProtocolBufferException e
) {