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
.RecipientId
;
18 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
19 import org
.asamk
.signal
.manager
.util
.IOUtils
;
20 import org
.signal
.storageservice
.protos
.groups
.local
.DecryptedGroup
;
21 import org
.signal
.zkgroup
.InvalidInputException
;
22 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
23 import org
.slf4j
.Logger
;
24 import org
.slf4j
.LoggerFactory
;
25 import org
.whispersystems
.libsignal
.util
.Hex
;
26 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
27 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
30 import java
.io
.FileInputStream
;
31 import java
.io
.FileOutputStream
;
32 import java
.io
.IOException
;
33 import java
.util
.ArrayList
;
34 import java
.util
.Base64
;
35 import java
.util
.HashMap
;
36 import java
.util
.List
;
38 import java
.util
.stream
.Collectors
;
40 public class GroupStore
{
42 private final static Logger logger
= LoggerFactory
.getLogger(GroupStore
.class);
44 private final File groupCachePath
;
45 private final Map
<GroupId
, GroupInfo
> groups
;
46 private final RecipientResolver recipientResolver
;
47 private final Saver saver
;
50 final File groupCachePath
,
51 final Map
<GroupId
, GroupInfo
> groups
,
52 final RecipientResolver recipientResolver
,
55 this.groupCachePath
= groupCachePath
;
57 this.recipientResolver
= recipientResolver
;
62 final File groupCachePath
, final RecipientResolver recipientResolver
, final Saver saver
64 this.groups
= new HashMap
<>();
65 this.groupCachePath
= groupCachePath
;
66 this.recipientResolver
= recipientResolver
;
70 public static GroupStore
fromStorage(
71 final Storage storage
,
72 final File groupCachePath
,
73 final RecipientResolver recipientResolver
,
76 final var groups
= storage
.groups
.stream().map(g
-> {
77 if (g
instanceof Storage
.GroupV1
) {
78 final var g1
= (Storage
.GroupV1
) g
;
79 final var members
= g1
.members
.stream().map(m
-> {
80 if (m
.recipientId
== null) {
81 return recipientResolver
.resolveRecipient(new SignalServiceAddress(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
);
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()) {
125 groupFileLegacy
.delete();
127 } catch (IOException e
) {
128 logger
.warn("Failed to cache group, ignoring: {}", e
.getMessage());
131 storage
= toStorageLocked();
136 public void deleteGroupV1(GroupIdV1 groupId
) {
137 final Storage storage
;
138 synchronized (groups
) {
139 groups
.remove(groupId
);
140 storage
= toStorageLocked();
145 public GroupInfo
getGroup(GroupId groupId
) {
146 synchronized (groups
) {
147 return getGroupLocked(groupId
);
151 public GroupInfoV1
getOrCreateGroupV1(GroupIdV1 groupId
) {
152 synchronized (groups
) {
153 var group
= getGroupLocked(groupId
);
154 if (group
instanceof GroupInfoV1
) {
155 return (GroupInfoV1
) group
;
159 return new GroupInfoV1(groupId
);
166 public List
<GroupInfo
> getGroups() {
167 synchronized (groups
) {
168 final var groups
= this.groups
.values();
169 for (var group
: groups
) {
170 loadDecryptedGroupLocked(group
);
172 return new ArrayList
<>(groups
);
176 public void mergeRecipients(final RecipientId recipientId
, final RecipientId toBeMergedRecipientId
) {
177 synchronized (groups
) {
178 var modified
= false;
179 for (var group
: this.groups
.values()) {
180 if (group
instanceof GroupInfoV1
) {
181 var groupV1
= (GroupInfoV1
) group
;
182 if (groupV1
.isMember(toBeMergedRecipientId
)) {
183 groupV1
.removeMember(toBeMergedRecipientId
);
184 groupV1
.addMembers(List
.of(recipientId
));
190 saver
.save(toStorageLocked());
195 private GroupInfo
getGroupLocked(final GroupId groupId
) {
196 var group
= groups
.get(groupId
);
198 if (groupId
instanceof GroupIdV1
) {
199 group
= getGroupByV1IdLocked((GroupIdV1
) groupId
);
200 } else if (groupId
instanceof GroupIdV2
) {
201 group
= getGroupV1ByV2IdLocked((GroupIdV2
) groupId
);
204 loadDecryptedGroupLocked(group
);
208 private GroupInfo
getGroupByV1IdLocked(final GroupIdV1 groupId
) {
209 return groups
.get(GroupUtils
.getGroupIdV2(groupId
));
212 private GroupInfoV1
getGroupV1ByV2IdLocked(GroupIdV2 groupIdV2
) {
213 for (var g
: groups
.values()) {
214 if (g
instanceof GroupInfoV1
) {
215 final var gv1
= (GroupInfoV1
) g
;
216 if (groupIdV2
.equals(gv1
.getExpectedV2Id())) {
224 private void loadDecryptedGroupLocked(final GroupInfo group
) {
225 if (group
instanceof GroupInfoV2
&& ((GroupInfoV2
) group
).getGroup() == null) {
226 var groupFile
= getGroupV2File(group
.getGroupId());
227 if (!groupFile
.exists()) {
228 groupFile
= getGroupV2FileLegacy(group
.getGroupId());
230 if (!groupFile
.exists()) {
233 try (var stream
= new FileInputStream(groupFile
)) {
234 ((GroupInfoV2
) group
).setGroup(DecryptedGroup
.parseFrom(stream
), recipientResolver
);
235 } catch (IOException ignored
) {
240 private File
getGroupV2FileLegacy(final GroupId groupId
) {
241 return new File(groupCachePath
, Hex
.toStringCondensed(groupId
.serialize()));
244 private File
getGroupV2File(final GroupId groupId
) {
245 return new File(groupCachePath
, groupId
.toBase64().replace("/", "_"));
248 private Storage
toStorageLocked() {
249 return new Storage(groups
.values().stream().map(g
-> {
250 if (g
instanceof GroupInfoV1
) {
251 final var g1
= (GroupInfoV1
) g
;
252 return new Storage
.GroupV1(g1
.getGroupId().toBase64(),
253 g1
.getExpectedV2Id().toBase64(),
256 g1
.messageExpirationTime
,
260 .map(m
-> new Storage
.GroupV1
.Member(m
.getId(), null, null))
261 .collect(Collectors
.toList()));
264 final var g2
= (GroupInfoV2
) g
;
265 return new Storage
.GroupV2(g2
.getGroupId().toBase64(),
266 Base64
.getEncoder().encodeToString(g2
.getMasterKey().serialize()),
268 }).collect(Collectors
.toList()));
271 public static class Storage
{
273 // @JsonSerialize(using = GroupsSerializer.class)
274 @JsonDeserialize(using
= GroupsDeserializer
.class)
275 public List
<Storage
.Group
> groups
;
277 // For deserialization
281 public Storage(final List
<Storage
.Group
> groups
) {
282 this.groups
= groups
;
285 private abstract static class Group
{
289 private static class GroupV1
extends Group
{
291 public String groupId
;
292 public String expectedV2Id
;
295 public int messageExpirationTime
;
296 public boolean blocked
;
297 public boolean archived
;
299 @JsonDeserialize(using
= MembersDeserializer
.class)
300 @JsonSerialize(using
= MembersSerializer
.class)
301 public List
<Member
> members
;
303 // For deserialization
308 final String groupId
,
309 final String expectedV2Id
,
312 final int messageExpirationTime
,
313 final boolean blocked
,
314 final boolean archived
,
315 final List
<Member
> members
317 this.groupId
= groupId
;
318 this.expectedV2Id
= expectedV2Id
;
321 this.messageExpirationTime
= messageExpirationTime
;
322 this.blocked
= blocked
;
323 this.archived
= archived
;
324 this.members
= members
;
327 private static final class Member
{
329 public Long recipientId
;
333 public String number
;
335 Member(Long recipientId
, final String uuid
, final String number
) {
336 this.recipientId
= recipientId
;
338 this.number
= number
;
342 private static final class JsonSignalServiceAddress
{
346 public String number
;
348 // For deserialization
349 public JsonSignalServiceAddress() {
352 JsonSignalServiceAddress(final String uuid
, final String number
) {
354 this.number
= number
;
358 private static class MembersSerializer
extends JsonSerializer
<List
<Member
>> {
361 public void serialize(
362 final List
<Member
> value
, final JsonGenerator jgen
, final SerializerProvider provider
363 ) throws IOException
{
364 jgen
.writeStartArray(value
.size());
365 for (var address
: value
) {
366 if (address
.recipientId
!= null) {
367 jgen
.writeNumber(address
.recipientId
);
368 } else if (address
.uuid
!= null) {
369 jgen
.writeObject(new JsonSignalServiceAddress(address
.uuid
, address
.number
));
371 jgen
.writeString(address
.number
);
374 jgen
.writeEndArray();
378 private static class MembersDeserializer
extends JsonDeserializer
<List
<Member
>> {
381 public List
<Member
> deserialize(
382 JsonParser jsonParser
, DeserializationContext deserializationContext
383 ) throws IOException
{
384 var addresses
= new ArrayList
<Member
>();
385 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
388 addresses
.add(new Member(null, null, n
.textValue()));
389 } else if (n
.isNumber()) {
390 addresses
.add(new Member(n
.numberValue().longValue(), null, null));
392 var address
= jsonParser
.getCodec().treeToValue(n
, JsonSignalServiceAddress
.class);
393 addresses
.add(new Member(null, address
.uuid
, address
.number
));
402 private static class GroupV2
extends Group
{
404 public String groupId
;
405 public String masterKey
;
406 public boolean blocked
;
408 // For deserialization
412 public GroupV2(final String groupId
, final String masterKey
, final boolean blocked
) {
413 this.groupId
= groupId
;
414 this.masterKey
= masterKey
;
415 this.blocked
= blocked
;
421 // private static class GroupsSerializer extends JsonSerializer<List<Storage.Group>> {
424 // public void serialize(
425 // final List<Storage.Group> groups, final JsonGenerator jgen, final SerializerProvider provider
426 // ) throws IOException {
427 // jgen.writeStartArray(groups.size());
428 // for (var group : groups) {
429 // if (group instanceof GroupInfoV1) {
430 // jgen.writeObject(group);
431 // } else if (group instanceof GroupInfoV2) {
432 // final var groupV2 = (GroupInfoV2) group;
433 // jgen.writeStartObject();
434 // jgen.writeStringField("groupId", groupV2.getGroupId().toBase64());
435 // jgen.writeStringField("masterKey",
436 // Base64.getEncoder().encodeToString(groupV2.getMasterKey().serialize()));
437 // jgen.writeBooleanField("blocked", groupV2.isBlocked());
438 // jgen.writeEndObject();
440 // throw new AssertionError("Unknown group version");
443 // jgen.writeEndArray();
447 private static class GroupsDeserializer
extends JsonDeserializer
<List
<Storage
.Group
>> {
450 public List
<Storage
.Group
> deserialize(
451 JsonParser jsonParser
, DeserializationContext deserializationContext
452 ) throws IOException
{
453 var groups
= new ArrayList
<Storage
.Group
>();
454 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
457 if (n
.hasNonNull("masterKey")) {
459 g
= jsonParser
.getCodec().treeToValue(n
, Storage
.GroupV2
.class);
461 g
= jsonParser
.getCodec().treeToValue(n
, Storage
.GroupV1
.class);
470 public interface Saver
{
472 void save(Storage storage
);