1 package org
.asamk
.signal
.manager
.storage
.groups
;
3 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
4 import com
.fasterxml
.jackson
.core
.JsonParser
;
5 import com
.fasterxml
.jackson
.databind
.DeserializationContext
;
6 import com
.fasterxml
.jackson
.databind
.JsonDeserializer
;
7 import com
.fasterxml
.jackson
.databind
.JsonNode
;
8 import com
.fasterxml
.jackson
.databind
.JsonSerializer
;
9 import com
.fasterxml
.jackson
.databind
.SerializerProvider
;
10 import com
.fasterxml
.jackson
.databind
.annotation
.JsonDeserialize
;
11 import com
.fasterxml
.jackson
.databind
.annotation
.JsonSerialize
;
13 import org
.asamk
.signal
.manager
.groups
.GroupId
;
14 import org
.asamk
.signal
.manager
.groups
.GroupIdV1
;
15 import org
.asamk
.signal
.manager
.groups
.GroupIdV2
;
16 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
17 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
18 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
19 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
20 import org
.asamk
.signal
.manager
.util
.IOUtils
;
21 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
22 import org
.signal
.zkgroup
.InvalidInputException
;
23 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
24 import org
.slf4j
.Logger
;
25 import org
.slf4j
.LoggerFactory
;
26 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
27 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
30 import java
.io
.FileInputStream
;
31 import java
.io
.FileOutputStream
;
32 import java
.io
.IOException
;
33 import java
.nio
.file
.Files
;
34 import java
.util
.ArrayList
;
35 import java
.util
.Base64
;
36 import java
.util
.HashMap
;
37 import java
.util
.List
;
39 import java
.util
.stream
.Collectors
;
41 public class GroupStore
{
43 private final static Logger logger
= LoggerFactory
.getLogger(GroupStore
.class);
45 private final File groupCachePath
;
46 private final Map
<GroupId
, GroupInfo
> groups
;
47 private final RecipientResolver recipientResolver
;
48 private final Saver saver
;
51 final File groupCachePath
,
52 final Map
<GroupId
, GroupInfo
> groups
,
53 final RecipientResolver recipientResolver
,
56 this.groupCachePath
= groupCachePath
;
58 this.recipientResolver
= recipientResolver
;
63 final File groupCachePath
, final RecipientResolver recipientResolver
, final Saver saver
65 this.groups
= new HashMap
<>();
66 this.groupCachePath
= groupCachePath
;
67 this.recipientResolver
= recipientResolver
;
71 public static GroupStore
fromStorage(
72 final Storage storage
,
73 final File groupCachePath
,
74 final RecipientResolver recipientResolver
,
77 final var groups
= storage
.groups
.stream().map(g
-> {
78 if (g
instanceof Storage
.GroupV1 g1
) {
79 final var members
= g1
.members
.stream().map(m
-> {
80 if (m
.recipientId
== null) {
81 return recipientResolver
.resolveRecipient(new RecipientAddress(UuidUtil
.parseOrNull(m
.uuid
),
85 return RecipientId
.of(m
.recipientId
);
86 }).collect(Collectors
.toSet());
88 return new GroupInfoV1(GroupIdV1
.fromBase64(g1
.groupId
),
89 g1
.expectedV2Id
== null ?
null : GroupIdV2
.fromBase64(g1
.expectedV2Id
),
93 g1
.messageExpirationTime
,
98 final var g2
= (Storage
.GroupV2
) g
;
99 var groupId
= GroupIdV2
.fromBase64(g2
.groupId
);
100 GroupMasterKey masterKey
;
102 masterKey
= new GroupMasterKey(Base64
.getDecoder().decode(g2
.masterKey
));
103 } catch (InvalidInputException
| IllegalArgumentException e
) {
104 throw new AssertionError("Invalid master key for group " + groupId
.toBase64());
107 return new GroupInfoV2(groupId
, masterKey
, g2
.blocked
, g2
.permissionDenied
);
108 }).collect(Collectors
.toMap(GroupInfo
::getGroupId
, g
-> g
));
110 return new GroupStore(groupCachePath
, groups
, recipientResolver
, saver
);
113 public void updateGroup(GroupInfo group
) {
114 final Storage storage
;
115 synchronized (groups
) {
116 groups
.put(group
.getGroupId(), group
);
117 if (group
instanceof GroupInfoV2
&& ((GroupInfoV2
) group
).getGroup() != null) {
119 IOUtils
.createPrivateDirectories(groupCachePath
);
120 try (var stream
= new FileOutputStream(getGroupV2File(group
.getGroupId()))) {
121 ((GroupInfoV2
) group
).getGroup().writeTo(stream
);
123 final var groupFileLegacy
= getGroupV2FileLegacy(group
.getGroupId());
124 if (groupFileLegacy
.exists()) {
126 Files
.delete(groupFileLegacy
.toPath());
127 } catch (IOException e
) {
128 logger
.error("Failed to delete legacy group file {}: {}", groupFileLegacy
, e
.getMessage());
131 } catch (IOException e
) {
132 logger
.warn("Failed to cache group, ignoring: {}", e
.getMessage());
135 storage
= toStorageLocked();
140 public void deleteGroupV1(GroupIdV1 groupIdV1
) {
141 deleteGroup(groupIdV1
);
144 public void deleteGroup(GroupId groupId
) {
145 final Storage storage
;
146 synchronized (groups
) {
147 groups
.remove(groupId
);
148 storage
= toStorageLocked();
153 public GroupInfo
getGroup(GroupId groupId
) {
154 synchronized (groups
) {
155 return getGroupLocked(groupId
);
159 public GroupInfoV1
getOrCreateGroupV1(GroupIdV1 groupId
) {
160 synchronized (groups
) {
161 var group
= getGroupLocked(groupId
);
162 if (group
instanceof GroupInfoV1
) {
163 return (GroupInfoV1
) group
;
167 return new GroupInfoV1(groupId
);
174 public List
<GroupInfo
> getGroups() {
175 synchronized (groups
) {
176 final var groups
= this.groups
.values();
177 for (var group
: groups
) {
178 loadDecryptedGroupLocked(group
);
180 return new ArrayList
<>(groups
);
184 public void mergeRecipients(final RecipientId recipientId
, final RecipientId toBeMergedRecipientId
) {
185 synchronized (groups
) {
186 var modified
= false;
187 for (var group
: this.groups
.values()) {
188 if (group
instanceof GroupInfoV1 groupV1
) {
189 if (groupV1
.isMember(toBeMergedRecipientId
)) {
190 groupV1
.removeMember(toBeMergedRecipientId
);
191 groupV1
.addMembers(List
.of(recipientId
));
197 saver
.save(toStorageLocked());
202 private GroupInfo
getGroupLocked(final GroupId groupId
) {
203 var group
= groups
.get(groupId
);
205 if (groupId
instanceof GroupIdV1
) {
206 group
= getGroupByV1IdLocked((GroupIdV1
) groupId
);
207 } else if (groupId
instanceof GroupIdV2
) {
208 group
= getGroupV1ByV2IdLocked((GroupIdV2
) groupId
);
211 loadDecryptedGroupLocked(group
);
215 private GroupInfo
getGroupByV1IdLocked(final GroupIdV1 groupId
) {
216 return groups
.get(GroupUtils
.getGroupIdV2(groupId
));
219 private GroupInfoV1
getGroupV1ByV2IdLocked(GroupIdV2 groupIdV2
) {
220 for (var g
: groups
.values()) {
221 if (g
instanceof GroupInfoV1 gv1
) {
222 if (groupIdV2
.equals(gv1
.getExpectedV2Id())) {
230 private void loadDecryptedGroupLocked(final GroupInfo group
) {
231 if (group
instanceof GroupInfoV2
&& ((GroupInfoV2
) group
).getGroup() == null) {
232 var groupFile
= getGroupV2File(group
.getGroupId());
233 if (!groupFile
.exists()) {
234 groupFile
= getGroupV2FileLegacy(group
.getGroupId());
236 if (!groupFile
.exists()) {
239 try (var stream
= new FileInputStream(groupFile
)) {
240 ((GroupInfoV2
) group
).setGroup(DecryptedGroup
.parseFrom(stream
), recipientResolver
);
241 } catch (IOException ignored
) {
246 private File
getGroupV2FileLegacy(final GroupId groupId
) {
247 return new File(groupCachePath
, Hex
.toStringCondensed(groupId
.serialize()));
250 private File
getGroupV2File(final GroupId groupId
) {
251 return new File(groupCachePath
, groupId
.toBase64().replace("/", "_"));
254 private Storage
toStorageLocked() {
255 return new Storage(groups
.values().stream().map(g
-> {
256 if (g
instanceof GroupInfoV1 g1
) {
257 return new Storage
.GroupV1(g1
.getGroupId().toBase64(),
258 g1
.getExpectedV2Id().toBase64(),
261 g1
.messageExpirationTime
,
265 .map(m
-> new Storage
.GroupV1
.Member(m
.id(), null, null))
266 .collect(Collectors
.toList()));
269 final var g2
= (GroupInfoV2
) g
;
270 return new Storage
.GroupV2(g2
.getGroupId().toBase64(),
271 Base64
.getEncoder().encodeToString(g2
.getMasterKey().serialize()),
273 g2
.isPermissionDenied());
274 }).collect(Collectors
.toList()));
277 public record Storage(@JsonDeserialize(using
= GroupsDeserializer
.class) List
<Object
> groups
) {
279 private record GroupV1(
284 int messageExpirationTime
,
287 @JsonDeserialize(using
= MembersDeserializer
.class) @JsonSerialize(using
= MembersSerializer
.class) List
<Member
> members
290 private record Member(Long recipientId
, String uuid
, String number
) {}
292 private record JsonRecipientAddress(String uuid
, String number
) {}
294 private static class MembersSerializer
extends JsonSerializer
<List
<Member
>> {
297 public void serialize(
298 final List
<Member
> value
, final JsonGenerator jgen
, final SerializerProvider provider
299 ) throws IOException
{
300 jgen
.writeStartArray(null, value
.size());
301 for (var address
: value
) {
302 if (address
.recipientId
!= null) {
303 jgen
.writeNumber(address
.recipientId
);
304 } else if (address
.uuid
!= null) {
305 jgen
.writeObject(new JsonRecipientAddress(address
.uuid
, address
.number
));
307 jgen
.writeString(address
.number
);
310 jgen
.writeEndArray();
314 private static class MembersDeserializer
extends JsonDeserializer
<List
<Member
>> {
317 public List
<Member
> deserialize(
318 JsonParser jsonParser
, DeserializationContext deserializationContext
319 ) throws IOException
{
320 var addresses
= new ArrayList
<Member
>();
321 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
324 addresses
.add(new Member(null, null, n
.textValue()));
325 } else if (n
.isNumber()) {
326 addresses
.add(new Member(n
.numberValue().longValue(), null, null));
328 var address
= jsonParser
.getCodec().treeToValue(n
, JsonRecipientAddress
.class);
329 addresses
.add(new Member(null, address
.uuid
, address
.number
));
338 private record GroupV2(String groupId
, String masterKey
, boolean blocked
, boolean permissionDenied
) {}
341 private static class GroupsDeserializer
extends JsonDeserializer
<List
<Object
>> {
344 public List
<Object
> deserialize(
345 JsonParser jsonParser
, DeserializationContext deserializationContext
346 ) throws IOException
{
347 var groups
= new ArrayList
<>();
348 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
351 if (n
.hasNonNull("masterKey")) {
353 g
= jsonParser
.getCodec().treeToValue(n
, Storage
.GroupV2
.class);
355 g
= jsonParser
.getCodec().treeToValue(n
, Storage
.GroupV1
.class);
364 public interface Saver
{
366 void save(Storage storage
);