Implement support for sending/receiving Group V2 messages

Requires libzkgroup to work, which is currently only included for x86_64 Linux

Related #354
This commit is contained in:
AsamK 2020-11-22 19:47:10 +01:00
parent 6d016bcfc9
commit 6a1b7dc597
12 changed files with 657 additions and 331 deletions

View file

@ -61,16 +61,17 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
} else if (content.getDataMessage().isPresent()) { } else if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
byte[] groupId = getGroupId(m, message);
if (!message.isEndSession() && if (!message.isEndSession() &&
!(message.getGroupContext().isPresent() && (groupId == null
message.getGroupContext().get().getGroupV1Type() != SignalServiceGroup.Type.DELIVER)) { || message.getGroupContext().get().getGroupV1Type() == null
|| message.getGroupContext().get().getGroupV1Type() == SignalServiceGroup.Type.DELIVER)) {
try { try {
conn.sendMessage(new Signal.MessageReceived( conn.sendMessage(new Signal.MessageReceived(
objectPath, objectPath,
message.getTimestamp(), message.getTimestamp(),
sender.getLegacyIdentifier(), sender.getLegacyIdentifier(),
message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent() groupId != null ? groupId : new byte[0],
? message.getGroupContext().get().getGroupV1().get().getGroupId() : new byte[0],
message.getBody().isPresent() ? message.getBody().get() : "", message.getBody().isPresent() ? message.getBody().get() : "",
JsonDbusReceiveMessageHandler.getAttachments(message, m))); JsonDbusReceiveMessageHandler.getAttachments(message, m)));
} catch (DBusException e) { } catch (DBusException e) {
@ -84,6 +85,7 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
if (transcript.getDestination().isPresent() || transcript.getMessage().getGroupContext().isPresent()) { if (transcript.getDestination().isPresent() || transcript.getMessage().getGroupContext().isPresent()) {
SignalServiceDataMessage message = transcript.getMessage(); SignalServiceDataMessage message = transcript.getMessage();
byte[] groupId = getGroupId(m, message);
try { try {
conn.sendMessage(new Signal.SyncMessageReceived( conn.sendMessage(new Signal.SyncMessageReceived(
@ -91,8 +93,7 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
transcript.getTimestamp(), transcript.getTimestamp(),
sender.getLegacyIdentifier(), sender.getLegacyIdentifier(),
transcript.getDestination().isPresent() ? transcript.getDestination().get().getLegacyIdentifier() : "", transcript.getDestination().isPresent() ? transcript.getDestination().get().getLegacyIdentifier() : "",
message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent() groupId != null ? groupId : new byte[0],
? message.getGroupContext().get().getGroupV1().get().getGroupId() : new byte[0],
message.getBody().isPresent() ? message.getBody().get() : "", message.getBody().isPresent() ? message.getBody().get() : "",
JsonDbusReceiveMessageHandler.getAttachments(message, m))); JsonDbusReceiveMessageHandler.getAttachments(message, m)));
} catch (DBusException e) { } catch (DBusException e) {
@ -104,6 +105,22 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
} }
} }
private static byte[] getGroupId(final Manager m, final SignalServiceDataMessage message) {
byte[] groupId;
if (message.getGroupContext().isPresent()) {
if (message.getGroupContext().get().getGroupV1().isPresent()) {
groupId = message.getGroupContext().get().getGroupV1().get().getGroupId();
} else if (message.getGroupContext().get().getGroupV2().isPresent()) {
groupId = m.getGroupId(message.getGroupContext().get().getGroupV2().get().getMasterKey());
} else {
groupId = null;
}
} else {
groupId = null;
}
return groupId;
}
static private List<String> getAttachments(SignalServiceDataMessage message, Manager m) { static private List<String> getAttachments(SignalServiceDataMessage message, Manager m) {
List<String> attachments = new ArrayList<>(); List<String> attachments = new ArrayList<>();
if (message.getAttachments().isPresent()) { if (message.getAttachments().isPresent()) {

View file

@ -275,11 +275,13 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
System.out.println(" - Action: " + typingMessage.getAction()); System.out.println(" - Action: " + typingMessage.getAction());
System.out.println(" - Timestamp: " + DateUtils.formatTimestamp(typingMessage.getTimestamp())); System.out.println(" - Timestamp: " + DateUtils.formatTimestamp(typingMessage.getTimestamp()));
if (typingMessage.getGroupId().isPresent()) { if (typingMessage.getGroupId().isPresent()) {
System.out.println(" - Group Info:");
System.out.println(" Id: " + Base64.encodeBytes(typingMessage.getGroupId().get()));
GroupInfo group = m.getGroup(typingMessage.getGroupId().get()); GroupInfo group = m.getGroup(typingMessage.getGroupId().get());
if (group != null) { if (group != null) {
System.out.println(" Name: " + group.name); System.out.println(" Name: " + group.getTitle());
} else { } else {
System.out.println(" Name: <Unknown group>"); System.out.println(" Name: <Unknown group>");
} }
} }
} }
@ -310,7 +312,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
} else { } else {
GroupInfo group = m.getGroup(groupInfo.getGroupId()); GroupInfo group = m.getGroup(groupInfo.getGroupId());
if (group != null) { if (group != null) {
System.out.println(" Name: " + group.name); System.out.println(" Name: " + group.getTitle());
} else { } else {
System.out.println(" Name: <Unknown group>"); System.out.println(" Name: <Unknown group>");
} }
@ -327,6 +329,14 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
} }
} else if (groupContext.getGroupV2().isPresent()) { } else if (groupContext.getGroupV2().isPresent()) {
final SignalServiceGroupV2 groupInfo = groupContext.getGroupV2().get(); final SignalServiceGroupV2 groupInfo = groupContext.getGroupV2().get();
byte[] groupId = m.getGroupId(groupInfo.getMasterKey());
System.out.println(" Id: " + Base64.encodeBytes(groupId));
GroupInfo group = m.getGroup(groupId);
if (group != null) {
System.out.println(" Name: " + group.getTitle());
} else {
System.out.println(" Name: <Unknown group>");
}
System.out.println(" Revision: " + groupInfo.getRevision()); System.out.println(" Revision: " + groupInfo.getRevision());
System.out.println(" Master key length: " + groupInfo.getMasterKey().serialize().length); System.out.println(" Master key length: " + groupInfo.getMasterKey().serialize().length);
System.out.println(" Has signed group change: " + groupInfo.hasSignedGroupChange()); System.out.println(" Has signed group change: " + groupInfo.hasSignedGroupChange());
@ -376,7 +386,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
final SignalServiceDataMessage.Reaction reaction = message.getReaction().get(); final SignalServiceDataMessage.Reaction reaction = message.getReaction().get();
System.out.println("Reaction:"); System.out.println("Reaction:");
System.out.println(" - Emoji: " + reaction.getEmoji()); System.out.println(" - Emoji: " + reaction.getEmoji());
System.out.println(" - Target author: " + reaction.getTargetAuthor().getLegacyIdentifier()); // todo resolve System.out.println(" - Target author: " + m.resolveSignalServiceAddress(reaction.getTargetAuthor()).getLegacyIdentifier());
System.out.println(" - Target timestamp: " + reaction.getTargetSentTimestamp()); System.out.println(" - Target timestamp: " + reaction.getTargetSentTimestamp());
System.out.println(" - Is remove: " + reaction.isRemove()); System.out.println(" - Is remove: " + reaction.isRemove());
} }

View file

@ -10,16 +10,23 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.util.Base64; import org.whispersystems.util.Base64;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ListGroupsCommand implements LocalCommand { public class ListGroupsCommand implements LocalCommand {
private static void printGroup(GroupInfo group, boolean detailed, SignalServiceAddress address) { private static void printGroup(Manager m, GroupInfo group, boolean detailed) {
if (detailed) { if (detailed) {
Set<String> members = group.getMembers()
.stream()
.map(m::resolveSignalServiceAddress)
.map(SignalServiceAddress::getLegacyIdentifier)
.collect(Collectors.toSet());
System.out.println(String.format("Id: %s Name: %s Active: %s Blocked: %b Members: %s", System.out.println(String.format("Id: %s Name: %s Active: %s Blocked: %b Members: %s",
Base64.encodeBytes(group.groupId), group.name, group.isMember(address), group.blocked, group.getMembersE164())); Base64.encodeBytes(group.groupId), group.getTitle(), group.isMember(m.getSelfAddress()), group.isBlocked(), members));
} else { } else {
System.out.println(String.format("Id: %s Name: %s Active: %s Blocked: %b", System.out.println(String.format("Id: %s Name: %s Active: %s Blocked: %b",
Base64.encodeBytes(group.groupId), group.name, group.isMember(address), group.blocked)); Base64.encodeBytes(group.groupId), group.getTitle(), group.isMember(m.getSelfAddress()), group.isBlocked()));
} }
} }
@ -41,7 +48,7 @@ public class ListGroupsCommand implements LocalCommand {
boolean detailed = ns.getBoolean("detailed"); boolean detailed = ns.getBoolean("detailed");
for (GroupInfo group : groups) { for (GroupInfo group : groups) {
printGroup(group, detailed, m.getSelfAddress()); printGroup(m, group, detailed);
} }
return 0; return 0;
} }

View file

@ -10,12 +10,14 @@ import org.asamk.signal.util.ErrorUtils;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.libsignal.util.Pair; import org.whispersystems.libsignal.util.Pair;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.messages.SendMessageResult;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class DbusSignalImpl implements Signal { public class DbusSignalImpl implements Signal {
@ -152,7 +154,7 @@ public class DbusSignalImpl implements Signal {
if (group == null) { if (group == null) {
return ""; return "";
} else { } else {
return group.name; return group.getTitle();
} }
} }
@ -162,7 +164,7 @@ public class DbusSignalImpl implements Signal {
if (group == null) { if (group == null) {
return Collections.emptyList(); return Collections.emptyList();
} else { } else {
return new ArrayList<>(group.getMembersE164()); return group.getMembers().stream().map(m::resolveSignalServiceAddress).map(SignalServiceAddress::getLegacyIdentifier).collect(Collectors.toList());
} }
} }

View file

@ -21,7 +21,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.storage.SignalAccount; import org.asamk.signal.storage.SignalAccount;
import org.asamk.signal.storage.contacts.ContactInfo; import org.asamk.signal.storage.contacts.ContactInfo;
import org.asamk.signal.storage.groups.GroupInfo; import org.asamk.signal.storage.groups.GroupInfo;
import org.asamk.signal.storage.groups.JsonGroupStore; import org.asamk.signal.storage.groups.GroupInfoV1;
import org.asamk.signal.storage.groups.GroupInfoV2;
import org.asamk.signal.storage.profiles.SignalProfile; import org.asamk.signal.storage.profiles.SignalProfile;
import org.asamk.signal.storage.profiles.SignalProfileEntry; import org.asamk.signal.storage.profiles.SignalProfileEntry;
import org.asamk.signal.storage.protocol.JsonIdentityKeyStore; import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
@ -39,7 +40,13 @@ import org.signal.libsignal.metadata.ProtocolNoSessionException;
import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException; import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
import org.signal.libsignal.metadata.SelfSendException; import org.signal.libsignal.metadata.SelfSendException;
import org.signal.libsignal.metadata.certificate.InvalidCertificateException; import org.signal.libsignal.metadata.certificate.InvalidCertificateException;
import org.signal.storageservice.protos.groups.local.DecryptedGroup;
import org.signal.storageservice.protos.groups.local.DecryptedMember;
import org.signal.zkgroup.InvalidInputException; import org.signal.zkgroup.InvalidInputException;
import org.signal.zkgroup.VerificationFailedException;
import org.signal.zkgroup.auth.AuthCredentialResponse;
import org.signal.zkgroup.groups.GroupMasterKey;
import org.signal.zkgroup.groups.GroupSecretParams;
import org.signal.zkgroup.profiles.ClientZkProfileOperations; import org.signal.zkgroup.profiles.ClientZkProfileOperations;
import org.signal.zkgroup.profiles.ProfileKey; import org.signal.zkgroup.profiles.ProfileKey;
import org.whispersystems.libsignal.IdentityKey; import org.whispersystems.libsignal.IdentityKey;
@ -67,7 +74,10 @@ import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair; import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations; import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations; import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
import org.whispersystems.signalservice.api.groupsv2.InvalidGroupStateException;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.messages.SendMessageResult;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment; import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer; import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
@ -77,6 +87,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup; import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2;
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage; import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload; import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload;
import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload.StickerInfo; import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload.StickerInfo;
@ -130,6 +141,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -155,6 +167,7 @@ public class Manager implements Closeable {
private final SignalAccount account; private final SignalAccount account;
private final PathConfig pathConfig; private final PathConfig pathConfig;
private SignalServiceAccountManager accountManager; private SignalServiceAccountManager accountManager;
private GroupsV2Api groupsV2Api;
private SignalServiceMessagePipe messagePipe = null; private SignalServiceMessagePipe messagePipe = null;
private SignalServiceMessagePipe unidentifiedMessagePipe = null; private SignalServiceMessagePipe unidentifiedMessagePipe = null;
private final boolean discoverableByPhoneNumber = true; private final boolean discoverableByPhoneNumber = true;
@ -165,6 +178,7 @@ public class Manager implements Closeable {
this.serviceConfiguration = serviceConfiguration; this.serviceConfiguration = serviceConfiguration;
this.userAgent = userAgent; this.userAgent = userAgent;
this.accountManager = createSignalServiceAccountManager(); this.accountManager = createSignalServiceAccountManager();
this.groupsV2Api = accountManager.getGroupsV2Api();
this.account.setResolver(this::resolveSignalServiceAddress); this.account.setResolver(this::resolveSignalServiceAddress);
} }
@ -178,12 +192,10 @@ public class Manager implements Closeable {
} }
private SignalServiceAccountManager createSignalServiceAccountManager() { private SignalServiceAccountManager createSignalServiceAccountManager() {
GroupsV2Operations groupsV2Operations; GroupsV2Operations groupsV2Operations = capabilities.isGv2()
try { ? new GroupsV2Operations(ClientZkOperations.create(serviceConfiguration))
groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceConfiguration)); : null;
} catch (Throwable ignored) {
groupsV2Operations = null;
}
return new SignalServiceAccountManager(serviceConfiguration, return new SignalServiceAccountManager(serviceConfiguration,
new DynamicCredentialsProvider(account.getUuid(), account.getUsername(), account.getPassword(), null, account.getDeviceId()), new DynamicCredentialsProvider(account.getUuid(), account.getUsername(), account.getPassword(), null, account.getDeviceId()),
userAgent, userAgent,
@ -236,29 +248,12 @@ public class Manager implements Closeable {
Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent); Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent);
m.migrateLegacyConfigs(); m.migrateLegacyConfigs();
m.updateAccountAttributes();
return m; return m;
} }
private void migrateLegacyConfigs() { private void migrateLegacyConfigs() {
// Copy group avatars that were previously stored in the attachments folder
// to the new avatar folder
if (JsonGroupStore.groupsWithLegacyAvatarId.size() > 0) {
for (GroupInfo g : JsonGroupStore.groupsWithLegacyAvatarId) {
File avatarFile = getGroupAvatarFile(g.groupId);
File attachmentFile = getAttachmentFile(new SignalServiceAttachmentRemoteId(g.getAvatarId()));
if (!avatarFile.exists() && attachmentFile.exists()) {
try {
IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
Files.copy(attachmentFile.toPath(), avatarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
// Ignore
}
}
}
JsonGroupStore.groupsWithLegacyAvatarId.clear();
account.save();
}
if (account.getProfileKey() == null) { if (account.getProfileKey() == null) {
// Old config file, creating new profile key // Old config file, creating new profile key
account.setProfileKey(KeyUtils.createProfileKey()); account.setProfileKey(KeyUtils.createProfileKey());
@ -304,6 +299,7 @@ public class Manager implements Closeable {
// Resetting UUID, because registering doesn't work otherwise // Resetting UUID, because registering doesn't work otherwise
account.setUuid(null); account.setUuid(null);
accountManager = createSignalServiceAccountManager(); accountManager = createSignalServiceAccountManager();
this.groupsV2Api = accountManager.getGroupsV2Api();
if (voiceVerification) { if (voiceVerification) {
accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.fromNullable(captcha), Optional.absent()); accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.fromNullable(captcha), Optional.absent());
@ -435,14 +431,16 @@ public class Manager implements Closeable {
} }
private SignalServiceMessageReceiver getMessageReceiver() { private SignalServiceMessageReceiver getMessageReceiver() {
// TODO implement ZkGroup support final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2()
final ClientZkProfileOperations clientZkProfileOperations = null; ? ClientZkOperations.create(serviceConfiguration).getProfileOperations()
: null;
return new SignalServiceMessageReceiver(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), account.getSignalingKey(), userAgent, null, timer, clientZkProfileOperations); return new SignalServiceMessageReceiver(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), account.getSignalingKey(), userAgent, null, timer, clientZkProfileOperations);
} }
private SignalServiceMessageSender getMessageSender() { private SignalServiceMessageSender getMessageSender() {
// TODO implement ZkGroup support final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2()
final ClientZkProfileOperations clientZkProfileOperations = null; ? ClientZkOperations.create(serviceConfiguration).getProfileOperations()
: null;
final ExecutorService executor = null; final ExecutorService executor = null;
return new SignalServiceMessageSender(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), return new SignalServiceMessageSender(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(),
account.getDeviceId(), account.getSignalProtocolStore(), userAgent, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent(), clientZkProfileOperations, executor, ServiceConfig.MAX_ENVELOPE_SIZE); account.getDeviceId(), account.getSignalProtocolStore(), userAgent, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent(), clientZkProfileOperations, executor, ServiceConfig.MAX_ENVELOPE_SIZE);
@ -527,7 +525,7 @@ public class Manager implements Closeable {
throw new GroupNotFoundException(groupId); throw new GroupNotFoundException(groupId);
} }
if (!g.isMember(account.getSelfAddress())) { if (!g.isMember(account.getSelfAddress())) {
throw new NotAGroupMemberException(groupId, g.name); throw new NotAGroupMemberException(groupId, g.getTitle());
} }
return g; return g;
} }
@ -546,33 +544,38 @@ public class Manager implements Closeable {
if (attachments != null) { if (attachments != null) {
messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments)); messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
} }
if (groupId != null) {
SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
.withId(groupId)
.build();
messageBuilder.asGroupMessage(group);
}
final GroupInfo g = getGroupForSending(groupId); final GroupInfo g = getGroupForSending(groupId);
messageBuilder.withExpiration(g.messageExpirationTime); setGroupContext(messageBuilder, g);
messageBuilder.withExpiration(g.getMessageExpirationTime());
return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress())); return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
} }
private void setGroupContext(final SignalServiceDataMessage.Builder messageBuilder, final GroupInfo groupInfo) {
if (groupInfo instanceof GroupInfoV1) {
SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
.withId(groupInfo.groupId)
.build();
messageBuilder.asGroupMessage(group);
} else {
final GroupInfoV2 groupInfoV2 = (GroupInfoV2) groupInfo;
SignalServiceGroupV2 group = SignalServiceGroupV2.newBuilder(groupInfoV2.getMasterKey())
.withRevision(groupInfoV2.getGroup() == null ? 0 : groupInfoV2.getGroup().getRevision())
.build();
messageBuilder.asGroupMessage(group);
}
}
public Pair<Long, List<SendMessageResult>> sendGroupMessageReaction(String emoji, boolean remove, String targetAuthor, public Pair<Long, List<SendMessageResult>> sendGroupMessageReaction(String emoji, boolean remove, String targetAuthor,
long targetSentTimestamp, byte[] groupId) long targetSentTimestamp, byte[] groupId)
throws IOException, InvalidNumberException, NotAGroupMemberException, GroupNotFoundException { throws IOException, InvalidNumberException, NotAGroupMemberException, GroupNotFoundException {
SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, canonicalizeAndResolveSignalServiceAddress(targetAuthor), targetSentTimestamp); SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, canonicalizeAndResolveSignalServiceAddress(targetAuthor), targetSentTimestamp);
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder() final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
.withReaction(reaction); .withReaction(reaction);
if (groupId != null) {
SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
.withId(groupId)
.build();
messageBuilder.asGroupMessage(group);
}
final GroupInfo g = getGroupForSending(groupId); final GroupInfo g = getGroupForSending(groupId);
setGroupContext(messageBuilder, g);
return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress())); return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
} }
@ -585,20 +588,29 @@ public class Manager implements Closeable {
.asGroupMessage(group); .asGroupMessage(group);
final GroupInfo g = getGroupForSending(groupId); final GroupInfo g = getGroupForSending(groupId);
g.removeMember(account.getSelfAddress()); if (g instanceof GroupInfoV1) {
account.getGroupStore().updateGroup(g); GroupInfoV1 groupInfoV1 = (GroupInfoV1) g;
groupInfoV1.removeMember(account.getSelfAddress());
account.getGroupStore().updateGroup(groupInfoV1);
} else {
throw new RuntimeException("TODO Not implemented!");
}
return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress())); return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
} }
private Pair<byte[], List<SendMessageResult>> sendUpdateGroupMessage(byte[] groupId, String name, Collection<SignalServiceAddress> members, String avatarFile) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException { private Pair<byte[], List<SendMessageResult>> sendUpdateGroupMessage(byte[] groupId, String name, Collection<SignalServiceAddress> members, String avatarFile) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
GroupInfo g; GroupInfoV1 g;
if (groupId == null) { if (groupId == null) {
// Create new group // Create new group
g = new GroupInfo(KeyUtils.createGroupId()); g = new GroupInfoV1(KeyUtils.createGroupId());
g.addMembers(Collections.singleton(account.getSelfAddress())); g.addMembers(Collections.singleton(account.getSelfAddress()));
} else { } else {
g = getGroupForSending(groupId); GroupInfo group = getGroupForSending(groupId);
if (!(group instanceof GroupInfoV1)) {
throw new RuntimeException("TODO Not implemented!");
}
g = (GroupInfoV1) group;
} }
if (name != null) { if (name != null) {
@ -641,7 +653,12 @@ public class Manager implements Closeable {
} }
Pair<Long, List<SendMessageResult>> sendUpdateGroupMessage(byte[] groupId, SignalServiceAddress recipient) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException { Pair<Long, List<SendMessageResult>> sendUpdateGroupMessage(byte[] groupId, SignalServiceAddress recipient) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
GroupInfo g = getGroupForSending(groupId); GroupInfoV1 g;
GroupInfo group = getGroupForSending(groupId);
if (!(group instanceof GroupInfoV1)) {
throw new RuntimeException("TODO Not implemented!");
}
g = (GroupInfoV1) group;
if (!g.isMember(recipient)) { if (!g.isMember(recipient)) {
throw new NotAGroupMemberException(groupId, g.name); throw new NotAGroupMemberException(groupId, g.name);
@ -653,7 +670,7 @@ public class Manager implements Closeable {
return sendMessage(messageBuilder, Collections.singleton(recipient)); return sendMessage(messageBuilder, Collections.singleton(recipient));
} }
private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) throws AttachmentInvalidException { private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV1 g) throws AttachmentInvalidException {
SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE) SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
.withId(g.groupId) .withId(g.groupId)
.withName(g.name) .withName(g.name)
@ -780,7 +797,7 @@ public class Manager implements Closeable {
throw new GroupNotFoundException(groupId); throw new GroupNotFoundException(groupId);
} }
group.blocked = blocked; group.setBlocked(blocked);
account.getGroupStore().updateGroup(group); account.getGroupStore().updateGroup(group);
account.save(); account.save();
} }
@ -831,8 +848,13 @@ public class Manager implements Closeable {
*/ */
public void setExpirationTimer(byte[] groupId, int messageExpirationTimer) { public void setExpirationTimer(byte[] groupId, int messageExpirationTimer) {
GroupInfo g = account.getGroupStore().getGroup(groupId); GroupInfo g = account.getGroupStore().getGroup(groupId);
g.messageExpirationTime = messageExpirationTimer; if (g instanceof GroupInfoV1) {
account.getGroupStore().updateGroup(g); GroupInfoV1 groupInfoV1 = (GroupInfoV1) g;
groupInfoV1.messageExpirationTime = messageExpirationTimer;
account.getGroupStore().updateGroup(groupInfoV1);
} else {
throw new RuntimeException("TODO Not implemented!");
}
} }
/** /**
@ -1101,6 +1123,7 @@ public class Manager implements Closeable {
private Pair<Long, List<SendMessageResult>> sendMessage(SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients) private Pair<Long, List<SendMessageResult>> sendMessage(SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients)
throws IOException { throws IOException {
recipients = recipients.stream().map(this::resolveSignalServiceAddress).collect(Collectors.toSet());
final long timestamp = System.currentTimeMillis(); final long timestamp = System.currentTimeMillis();
messageBuilder.withTimestamp(timestamp); messageBuilder.withTimestamp(timestamp);
if (messagePipe == null) { if (messagePipe == null) {
@ -1211,57 +1234,114 @@ public class Manager implements Closeable {
account.getSignalProtocolStore().deleteAllSessions(source); account.getSignalProtocolStore().deleteAllSessions(source);
} }
private static int currentTimeDays() {
return (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
}
private GroupsV2AuthorizationString getGroupAuthForToday(final GroupSecretParams groupSecretParams) throws IOException, VerificationFailedException {
final int today = currentTimeDays();
// Returns credentials for the next 7 days
final HashMap<Integer, AuthCredentialResponse> credentials = groupsV2Api.getCredentials(today);
// TODO cache credentials until they expire
AuthCredentialResponse authCredentialResponse = credentials.get(today);
return groupsV2Api.getGroupsV2AuthorizationString(account.getUuid(), today, groupSecretParams, authCredentialResponse);
}
private List<HandleAction> handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, SignalServiceAddress source, SignalServiceAddress destination, boolean ignoreAttachments) { private List<HandleAction> handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, SignalServiceAddress source, SignalServiceAddress destination, boolean ignoreAttachments) {
List<HandleAction> actions = new ArrayList<>(); List<HandleAction> actions = new ArrayList<>();
if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) { if (message.getGroupContext().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get(); if (message.getGroupContext().get().getGroupV1().isPresent()) {
GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId()); SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
switch (groupInfo.getType()) { GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId());
case UPDATE: if (group == null || group instanceof GroupInfoV1) {
if (group == null) { GroupInfoV1 groupV1 = (GroupInfoV1) group;
group = new GroupInfo(groupInfo.getGroupId()); switch (groupInfo.getType()) {
} case UPDATE: {
if (groupV1 == null) {
if (groupInfo.getAvatar().isPresent()) { groupV1 = new GroupInfoV1(groupInfo.getGroupId());
SignalServiceAttachment avatar = groupInfo.getAvatar().get();
if (avatar.isPointer()) {
try {
retrieveGroupAvatarAttachment(avatar.asPointer(), group.groupId);
} catch (IOException | InvalidMessageException | MissingConfigurationException e) {
System.err.println("Failed to retrieve group avatar (" + avatar.asPointer().getRemoteId() + "): " + e.getMessage());
} }
if (groupInfo.getAvatar().isPresent()) {
SignalServiceAttachment avatar = groupInfo.getAvatar().get();
if (avatar.isPointer()) {
try {
retrieveGroupAvatarAttachment(avatar.asPointer(), groupV1.groupId);
} catch (IOException | InvalidMessageException | MissingConfigurationException e) {
System.err.println("Failed to retrieve group avatar (" + avatar.asPointer().getRemoteId() + "): " + e.getMessage());
}
}
}
if (groupInfo.getName().isPresent()) {
groupV1.name = groupInfo.getName().get();
}
if (groupInfo.getMembers().isPresent()) {
groupV1.addMembers(groupInfo.getMembers().get()
.stream()
.map(this::resolveSignalServiceAddress)
.collect(Collectors.toSet()));
}
account.getGroupStore().updateGroup(groupV1);
break;
} }
case DELIVER:
if (groupV1 == null && !isSync) {
actions.add(new SendGroupInfoRequestAction(source, groupInfo.getGroupId()));
}
break;
case QUIT: {
if (groupV1 != null) {
groupV1.removeMember(source);
account.getGroupStore().updateGroup(groupV1);
}
break;
}
case REQUEST_INFO:
if (groupV1 != null && !isSync) {
actions.add(new SendGroupUpdateAction(source, groupV1.groupId));
}
break;
} }
} else {
System.err.println("Received a group v1 message for a v2 group: " + group.getTitle());
}
}
if (message.getGroupContext().get().getGroupV2().isPresent()) {
final SignalServiceGroupV2 groupContext = message.getGroupContext().get().getGroupV2().get();
final GroupMasterKey groupMasterKey = groupContext.getMasterKey();
if (groupInfo.getName().isPresent()) { final GroupSecretParams groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
group.name = groupInfo.getName().get();
}
if (groupInfo.getMembers().isPresent()) { byte[] groupId = groupSecretParams.getPublicParams().getGroupIdentifier().serialize();
group.addMembers(groupInfo.getMembers().get() GroupInfo groupInfo = account.getGroupStore().getGroup(groupId);
.stream() if (groupInfo instanceof GroupInfoV1) {
.map(this::resolveSignalServiceAddress) // TODO upgrade group
.collect(Collectors.toSet())); } else if (groupInfo == null || groupInfo instanceof GroupInfoV2) {
} GroupInfoV2 groupInfoV2 = groupInfo == null
? new GroupInfoV2(groupId, groupMasterKey)
: (GroupInfoV2) groupInfo;
account.getGroupStore().updateGroup(group); if (groupInfoV2.getGroup() == null || groupInfoV2.getGroup().getRevision() < groupContext.getRevision()) {
break; // TODO check if revision is only 1 behind and a signedGroupChange is available
case DELIVER: try {
if (group == null && !isSync) { final GroupsV2AuthorizationString groupsV2AuthorizationString = getGroupAuthForToday(groupSecretParams);
actions.add(new SendGroupInfoRequestAction(source, groupInfo.getGroupId())); final DecryptedGroup group = groupsV2Api.getGroup(groupSecretParams, groupsV2AuthorizationString);
groupInfoV2.setGroup(group);
for (DecryptedMember member : group.getMembersList()) {
final SignalServiceAddress address = resolveSignalServiceAddress(new SignalServiceAddress(UuidUtil.parseOrThrow(member.getUuid().toByteArray()), null));
try {
account.getProfileStore().storeProfileKey(address, new ProfileKey(member.getProfileKey().toByteArray()));
} catch (InvalidInputException ignored) {
}
}
} catch (IOException | VerificationFailedException | InvalidGroupStateException e) {
System.err.println("Failed to retrieve Group V2 info, ignoring ...");
}
account.getGroupStore().updateGroup(groupInfoV2);
} }
break; }
case QUIT:
if (group != null) {
group.removeMember(source);
account.getGroupStore().updateGroup(group);
}
break;
case REQUEST_INFO:
if (group != null && !isSync) {
actions.add(new SendGroupUpdateAction(source, group.groupId));
}
break;
} }
} }
final SignalServiceAddress conversationPartnerAddress = isSync ? destination : source; final SignalServiceAddress conversationPartnerAddress = isSync ? destination : source;
@ -1269,15 +1349,18 @@ public class Manager implements Closeable {
handleEndSession(conversationPartnerAddress); handleEndSession(conversationPartnerAddress);
} }
if (message.isExpirationUpdate() || message.getBody().isPresent()) { if (message.isExpirationUpdate() || message.getBody().isPresent()) {
if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) { if (message.getGroupContext().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get(); if (message.getGroupContext().get().getGroupV1().isPresent()) {
GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId()); SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
if (group == null) { GroupInfoV1 group = account.getGroupStore().getOrCreateGroupV1(groupInfo.getGroupId());
group = new GroupInfo(groupInfo.getGroupId()); if (group != null) {
} if (group.messageExpirationTime != message.getExpiresInSeconds()) {
if (group.messageExpirationTime != message.getExpiresInSeconds()) { group.messageExpirationTime = message.getExpiresInSeconds();
group.messageExpirationTime = message.getExpiresInSeconds(); account.getGroupStore().updateGroup(group);
account.getGroupStore().updateGroup(group); }
}
} else if (message.getGroupContext().get().getGroupV2().isPresent()) {
// disappearing message timer already stored in the DecryptedGroup
} }
} else { } else {
ContactInfo contact = account.getContactStore().getContact(conversationPartnerAddress); ContactInfo contact = account.getContactStore().getContact(conversationPartnerAddress);
@ -1519,7 +1602,7 @@ public class Manager implements Closeable {
if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) { if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get(); SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
GroupInfo group = getGroup(groupInfo.getGroupId()); GroupInfo group = getGroup(groupInfo.getGroupId());
return groupInfo.getType() == SignalServiceGroup.Type.DELIVER && group != null && group.blocked; return groupInfo.getType() == SignalServiceGroup.Type.DELIVER && group != null && group.isBlocked();
} }
} }
return false; return false;
@ -1574,34 +1657,33 @@ public class Manager implements Closeable {
DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream); DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream);
DeviceGroup g; DeviceGroup g;
while ((g = s.read()) != null) { while ((g = s.read()) != null) {
GroupInfo syncGroup = account.getGroupStore().getGroup(g.getId()); GroupInfoV1 syncGroup = account.getGroupStore().getOrCreateGroupV1(g.getId());
if (syncGroup == null) { if (syncGroup != null) {
syncGroup = new GroupInfo(g.getId()); if (g.getName().isPresent()) {
} syncGroup.name = g.getName().get();
if (g.getName().isPresent()) { }
syncGroup.name = g.getName().get(); syncGroup.addMembers(g.getMembers()
} .stream()
syncGroup.addMembers(g.getMembers() .map(this::resolveSignalServiceAddress)
.stream() .collect(Collectors.toSet()));
.map(this::resolveSignalServiceAddress) if (!g.isActive()) {
.collect(Collectors.toSet())); syncGroup.removeMember(account.getSelfAddress());
if (!g.isActive()) { } else {
syncGroup.removeMember(account.getSelfAddress()); // Add ourself to the member set as it's marked as active
} else { syncGroup.addMembers(Collections.singleton(account.getSelfAddress()));
// Add ourself to the member set as it's marked as active }
syncGroup.addMembers(Collections.singleton(account.getSelfAddress())); syncGroup.blocked = g.isBlocked();
} if (g.getColor().isPresent()) {
syncGroup.blocked = g.isBlocked(); syncGroup.color = g.getColor().get();
if (g.getColor().isPresent()) { }
syncGroup.color = g.getColor().get();
}
if (g.getAvatar().isPresent()) { if (g.getAvatar().isPresent()) {
retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId); retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId);
}
syncGroup.inboxPosition = g.getInboxPosition().orNull();
syncGroup.archived = g.isArchived();
account.getGroupStore().updateGroup(syncGroup);
} }
syncGroup.inboxPosition = g.getInboxPosition().orNull();
syncGroup.archived = g.isArchived();
account.getGroupStore().updateGroup(syncGroup);
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -1800,10 +1882,13 @@ public class Manager implements Closeable {
try (OutputStream fos = new FileOutputStream(groupsFile)) { try (OutputStream fos = new FileOutputStream(groupsFile)) {
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos); DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
for (GroupInfo record : account.getGroupStore().getGroups()) { for (GroupInfo record : account.getGroupStore().getGroups()) {
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name), if (record instanceof GroupInfoV1) {
new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId), GroupInfoV1 groupInfo = (GroupInfoV1) record;
record.isMember(account.getSelfAddress()), Optional.of(record.messageExpirationTime), out.write(new DeviceGroup(groupInfo.groupId, Optional.fromNullable(groupInfo.name),
Optional.fromNullable(record.color), record.blocked, Optional.fromNullable(record.inboxPosition), record.archived)); new ArrayList<>(groupInfo.getMembers()), createGroupAvatarAttachment(groupInfo.groupId),
groupInfo.isMember(account.getSelfAddress()), Optional.of(groupInfo.messageExpirationTime),
Optional.fromNullable(groupInfo.color), groupInfo.blocked, Optional.fromNullable(groupInfo.inboxPosition), groupInfo.archived));
}
} }
} }
@ -1887,7 +1972,7 @@ public class Manager implements Closeable {
} }
List<byte[]> groupIds = new ArrayList<>(); List<byte[]> groupIds = new ArrayList<>();
for (GroupInfo record : account.getGroupStore().getGroups()) { for (GroupInfo record : account.getGroupStore().getGroups()) {
if (record.blocked) { if (record.isBlocked()) {
groupIds.add(record.groupId); groupIds.add(record.groupId);
} }
} }
@ -1911,6 +1996,11 @@ public class Manager implements Closeable {
return account.getGroupStore().getGroup(groupId); return account.getGroupStore().getGroup(groupId);
} }
public byte[] getGroupId(GroupMasterKey groupMasterKey) {
final GroupSecretParams groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
return groupSecretParams.getPublicParams().getGroupIdentifier().serialize();
}
public List<JsonIdentityKeyStore.Identity> getIdentities() { public List<JsonIdentityKeyStore.Identity> getIdentities() {
return account.getSignalProtocolStore().getIdentities(); return account.getSignalProtocolStore().getIdentities();
} }

View file

@ -1,5 +1,6 @@
package org.asamk.signal.manager; package org.asamk.signal.manager;
import org.signal.zkgroup.ServerPublicParams;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.account.AccountAttributes; import org.whispersystems.signalservice.api.account.AccountAttributes;
import org.whispersystems.signalservice.api.push.TrustStore; import org.whispersystems.signalservice.api.push.TrustStore;
@ -13,7 +14,6 @@ import org.whispersystems.util.Base64;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -39,8 +39,26 @@ public class ServiceConfig {
private final static Optional<Dns> dns = Optional.absent(); private final static Optional<Dns> dns = Optional.absent();
private final static String zkGroupServerPublicParamsHex = "AMhf5ywVwITZMsff/eCyudZx9JDmkkkbV6PInzG4p8x3VqVJSFiMvnvlEKWuRob/1eaIetR31IYeAbm0NdOuHH8Qi+Rexi1wLlpzIo1gstHWBfZzy1+qHRV5A4TqPp15YzBPm0WSggW6PbSn+F4lf57VCnHF7p8SvzAA2ZZJPYJURt8X7bbg+H3i+PEjH9DXItNEqs2sNcug37xZQDLm7X0="; private final static String zkGroupServerPublicParamsHex = "AMhf5ywVwITZMsff/eCyudZx9JDmkkkbV6PInzG4p8x3VqVJSFiMvnvlEKWuRob/1eaIetR31IYeAbm0NdOuHH8Qi+Rexi1wLlpzIo1gstHWBfZzy1+qHRV5A4TqPp15YzBPm0WSggW6PbSn+F4lf57VCnHF7p8SvzAA2ZZJPYJURt8X7bbg+H3i+PEjH9DXItNEqs2sNcug37xZQDLm7X0=";
private final static byte[] zkGroupServerPublicParams;
static final AccountAttributes.Capabilities capabilities = new AccountAttributes.Capabilities(false, false, false, false); static final AccountAttributes.Capabilities capabilities;
static {
try {
zkGroupServerPublicParams = Base64.decode(zkGroupServerPublicParamsHex);
} catch (IOException e) {
throw new AssertionError(e);
}
boolean zkGroupAvailable;
try {
new ServerPublicParams(zkGroupServerPublicParams);
zkGroupAvailable = true;
} catch (Throwable ignored) {
zkGroupAvailable = false;
}
capabilities = new AccountAttributes.Capabilities(false, zkGroupAvailable, false, false);
}
public static SignalServiceConfiguration createDefaultServiceConfiguration(String userAgent) { public static SignalServiceConfiguration createDefaultServiceConfiguration(String userAgent) {
final Interceptor userAgentInterceptor = chain -> final Interceptor userAgentInterceptor = chain ->
@ -50,13 +68,6 @@ public class ServiceConfig {
final List<Interceptor> interceptors = Collections.singletonList(userAgentInterceptor); final List<Interceptor> interceptors = Collections.singletonList(userAgentInterceptor);
final byte[] zkGroupServerPublicParams;
try {
zkGroupServerPublicParams = Base64.decode(zkGroupServerPublicParamsHex);
} catch (IOException e) {
throw new AssertionError(e);
}
return new SignalServiceConfiguration( return new SignalServiceConfiguration(
new SignalServiceUrl[]{new SignalServiceUrl(URL, TRUST_STORE)}, new SignalServiceUrl[]{new SignalServiceUrl(URL, TRUST_STORE)},
makeSignalCdnUrlMapFor(new SignalCdnUrl[]{new SignalCdnUrl(CDN_URL, TRUST_STORE)}, new SignalCdnUrl[]{new SignalCdnUrl(CDN2_URL, TRUST_STORE)}), makeSignalCdnUrlMapFor(new SignalCdnUrl[]{new SignalCdnUrl(CDN_URL, TRUST_STORE)}, new SignalCdnUrl[]{new SignalCdnUrl(CDN2_URL, TRUST_STORE)}),
@ -70,10 +81,7 @@ public class ServiceConfig {
} }
private static Map<Integer, SignalCdnUrl[]> makeSignalCdnUrlMapFor(SignalCdnUrl[] cdn0Urls, SignalCdnUrl[] cdn2Urls) { private static Map<Integer, SignalCdnUrl[]> makeSignalCdnUrlMapFor(SignalCdnUrl[] cdn0Urls, SignalCdnUrl[] cdn2Urls) {
Map<Integer, SignalCdnUrl[]> result = new HashMap<>(); return Map.of(0, cdn0Urls, 2, cdn2Urls);
result.put(0, cdn0Urls);
result.put(2, cdn2Urls);
return Collections.unmodifiableMap(result);
} }
private ServiceConfig() { private ServiceConfig() {

View file

@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.asamk.signal.storage.contacts.ContactInfo; import org.asamk.signal.storage.contacts.ContactInfo;
import org.asamk.signal.storage.contacts.JsonContactsStore; import org.asamk.signal.storage.contacts.JsonContactsStore;
import org.asamk.signal.storage.groups.GroupInfo; import org.asamk.signal.storage.groups.GroupInfo;
import org.asamk.signal.storage.groups.GroupInfoV1;
import org.asamk.signal.storage.groups.JsonGroupStore; import org.asamk.signal.storage.groups.JsonGroupStore;
import org.asamk.signal.storage.profiles.ProfileStore; import org.asamk.signal.storage.profiles.ProfileStore;
import org.asamk.signal.storage.protocol.JsonIdentityKeyStore; import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
@ -87,7 +88,7 @@ public class SignalAccount implements Closeable {
final Pair<FileChannel, FileLock> pair = openFileChannel(fileName); final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
try { try {
SignalAccount account = new SignalAccount(pair.first(), pair.second()); SignalAccount account = new SignalAccount(pair.first(), pair.second());
account.load(); account.load(dataPath);
return account; return account;
} catch (Throwable e) { } catch (Throwable e) {
pair.second().close(); pair.second().close();
@ -109,7 +110,7 @@ public class SignalAccount implements Closeable {
account.username = username; account.username = username;
account.profileKey = profileKey; account.profileKey = profileKey;
account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId); account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
account.groupStore = new JsonGroupStore(); account.groupStore = new JsonGroupStore(getGroupCachePath(dataPath, username));
account.contactStore = new JsonContactsStore(); account.contactStore = new JsonContactsStore();
account.recipientStore = new RecipientStore(); account.recipientStore = new RecipientStore();
account.profileStore = new ProfileStore(); account.profileStore = new ProfileStore();
@ -135,7 +136,7 @@ public class SignalAccount implements Closeable {
account.deviceId = deviceId; account.deviceId = deviceId;
account.signalingKey = signalingKey; account.signalingKey = signalingKey;
account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId); account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
account.groupStore = new JsonGroupStore(); account.groupStore = new JsonGroupStore(getGroupCachePath(dataPath, username));
account.contactStore = new JsonContactsStore(); account.contactStore = new JsonContactsStore();
account.recipientStore = new RecipientStore(); account.recipientStore = new RecipientStore();
account.profileStore = new ProfileStore(); account.profileStore = new ProfileStore();
@ -149,6 +150,10 @@ public class SignalAccount implements Closeable {
return dataPath + "/" + username; return dataPath + "/" + username;
} }
private static File getGroupCachePath(String dataPath, String username) {
return new File(new File(dataPath, username + ".d"), "group-cache");
}
public static boolean userExists(String dataPath, String username) { public static boolean userExists(String dataPath, String username) {
if (username == null) { if (username == null) {
return false; return false;
@ -157,7 +162,7 @@ public class SignalAccount implements Closeable {
return !(!f.exists() || f.isDirectory()); return !(!f.exists() || f.isDirectory());
} }
private void load() throws IOException { private void load(String dataPath) throws IOException {
JsonNode rootNode; JsonNode rootNode;
synchronized (fileChannel) { synchronized (fileChannel) {
fileChannel.position(0); fileChannel.position(0);
@ -209,9 +214,10 @@ public class SignalAccount implements Closeable {
JsonNode groupStoreNode = rootNode.get("groupStore"); JsonNode groupStoreNode = rootNode.get("groupStore");
if (groupStoreNode != null) { if (groupStoreNode != null) {
groupStore = jsonProcessor.convertValue(groupStoreNode, JsonGroupStore.class); groupStore = jsonProcessor.convertValue(groupStoreNode, JsonGroupStore.class);
groupStore.groupCachePath = getGroupCachePath(dataPath, username);
} }
if (groupStore == null) { if (groupStore == null) {
groupStore = new JsonGroupStore(); groupStore = new JsonGroupStore(getGroupCachePath(dataPath, username));
} }
JsonNode contactStoreNode = rootNode.get("contactStore"); JsonNode contactStoreNode = rootNode.get("contactStore");
@ -236,9 +242,12 @@ public class SignalAccount implements Closeable {
} }
for (GroupInfo group : groupStore.getGroups()) { for (GroupInfo group : groupStore.getGroups()) {
group.members = group.members.stream() if (group instanceof GroupInfoV1) {
.map(m -> recipientStore.resolveServiceAddress(m)) GroupInfoV1 groupInfoV1 = (GroupInfoV1) group;
.collect(Collectors.toSet()); groupInfoV1.members = groupInfoV1.members.stream()
.map(m -> recipientStore.resolveServiceAddress(m))
.collect(Collectors.toSet());
}
} }
for (SessionInfo session : signalProtocolStore.getSessions()) { for (SessionInfo session : signalProtocolStore.getSessions()) {
@ -273,8 +282,8 @@ public class SignalAccount implements Closeable {
contactStore.updateContact(contactInfo); contactStore.updateContact(contactInfo);
} else { } else {
GroupInfo groupInfo = groupStore.getGroup(Base64.decode(thread.id)); GroupInfo groupInfo = groupStore.getGroup(Base64.decode(thread.id));
if (groupInfo != null) { if (groupInfo instanceof GroupInfoV1) {
groupInfo.messageExpirationTime = thread.messageExpirationTime; ((GroupInfoV1) groupInfo).messageExpirationTime = thread.messageExpirationTime;
groupStore.updateGroup(groupInfo); groupStore.updateGroup(groupInfo);
} }
} }

View file

@ -2,98 +2,40 @@ package org.asamk.signal.storage.groups;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.UUID;
public class GroupInfo { public abstract class GroupInfo {
private static final ObjectMapper jsonProcessor = new ObjectMapper();
@JsonProperty @JsonProperty
public final byte[] groupId; public final byte[] groupId;
@JsonProperty
public String name;
@JsonProperty
@JsonDeserialize(using = MembersDeserializer.class)
@JsonSerialize(using = MembersSerializer.class)
public Set<SignalServiceAddress> members = new HashSet<>();
@JsonProperty
public String color;
@JsonProperty(defaultValue = "0")
public int messageExpirationTime;
@JsonProperty(defaultValue = "false")
public boolean blocked;
@JsonProperty
public Integer inboxPosition;
@JsonProperty(defaultValue = "false")
public boolean archived;
private long avatarId;
@JsonProperty
@JsonIgnore
private boolean active;
public GroupInfo(byte[] groupId) { public GroupInfo(byte[] groupId) {
this.groupId = groupId; this.groupId = groupId;
} }
public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<SignalServiceAddress> members, @JsonProperty("avatarId") long avatarId, @JsonProperty("color") String color, @JsonProperty("blocked") boolean blocked, @JsonProperty("inboxPosition") Integer inboxPosition, @JsonProperty("archived") boolean archived, @JsonProperty("messageExpirationTime") int messageExpirationTime) { @JsonIgnore
this.groupId = groupId; public abstract String getTitle();
this.name = name;
this.members.addAll(members);
this.avatarId = avatarId;
this.color = color;
this.blocked = blocked;
this.inboxPosition = inboxPosition;
this.archived = archived;
this.messageExpirationTime = messageExpirationTime;
}
@JsonIgnore @JsonIgnore
public long getAvatarId() { public abstract Set<SignalServiceAddress> getMembers();
return avatarId;
}
@JsonIgnore @JsonIgnore
public Set<SignalServiceAddress> getMembers() { public abstract boolean isBlocked();
return members;
}
@JsonIgnore @JsonIgnore
public Set<String> getMembersE164() { public abstract void setBlocked(boolean blocked);
Set<String> membersE164 = new HashSet<>();
for (SignalServiceAddress member : members) { @JsonIgnore
if (!member.getNumber().isPresent()) { public abstract int getMessageExpirationTime();
continue;
}
membersE164.add(member.getNumber().get());
}
return membersE164;
}
@JsonIgnore @JsonIgnore
public Set<SignalServiceAddress> getMembersWithout(SignalServiceAddress address) { public Set<SignalServiceAddress> getMembersWithout(SignalServiceAddress address) {
Set<SignalServiceAddress> members = new HashSet<>(this.members.size()); Set<SignalServiceAddress> members = new HashSet<>();
for (SignalServiceAddress member : this.members) { for (SignalServiceAddress member : getMembers()) {
if (!member.matches(address)) { if (!member.matches(address)) {
members.add(member); members.add(member);
} }
@ -101,85 +43,13 @@ public class GroupInfo {
return members; return members;
} }
public void addMembers(Collection<SignalServiceAddress> addresses) {
for (SignalServiceAddress address : addresses) {
if (this.members.contains(address)) {
continue;
}
removeMember(address);
this.members.add(address);
}
}
public void removeMember(SignalServiceAddress address) {
this.members.removeIf(member -> member.matches(address));
}
@JsonIgnore @JsonIgnore
public boolean isMember(SignalServiceAddress address) { public boolean isMember(SignalServiceAddress address) {
for (SignalServiceAddress member : this.members) { for (SignalServiceAddress member : getMembers()) {
if (member.matches(address)) { if (member.matches(address)) {
return true; return true;
} }
} }
return false; return false;
} }
private static final class JsonSignalServiceAddress {
@JsonProperty
private UUID uuid;
@JsonProperty
private String number;
JsonSignalServiceAddress(@JsonProperty("uuid") final UUID uuid, @JsonProperty("number") final String number) {
this.uuid = uuid;
this.number = number;
}
JsonSignalServiceAddress(SignalServiceAddress address) {
this.uuid = address.getUuid().orNull();
this.number = address.getNumber().orNull();
}
SignalServiceAddress toSignalServiceAddress() {
return new SignalServiceAddress(uuid, number);
}
}
private static class MembersSerializer extends JsonSerializer<Set<SignalServiceAddress>> {
@Override
public void serialize(final Set<SignalServiceAddress> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
jgen.writeStartArray(value.size());
for (SignalServiceAddress address : value) {
if (address.getUuid().isPresent()) {
jgen.writeObject(new JsonSignalServiceAddress(address));
} else {
jgen.writeString(address.getNumber().get());
}
}
jgen.writeEndArray();
}
}
private static class MembersDeserializer extends JsonDeserializer<Set<SignalServiceAddress>> {
@Override
public Set<SignalServiceAddress> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
Set<SignalServiceAddress> addresses = new HashSet<>();
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
for (JsonNode n : node) {
if (n.isTextual()) {
addresses.add(new SignalServiceAddress(null, n.textValue()));
} else {
JsonSignalServiceAddress address = jsonProcessor.treeToValue(n, JsonSignalServiceAddress.class);
addresses.add(address.toSignalServiceAddress());
}
}
return addresses;
}
}
} }

View file

@ -0,0 +1,157 @@
package org.asamk.signal.storage.groups;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class GroupInfoV1 extends GroupInfo {
private static final ObjectMapper jsonProcessor = new ObjectMapper();
@JsonProperty
public String name;
@JsonProperty
@JsonDeserialize(using = MembersDeserializer.class)
@JsonSerialize(using = MembersSerializer.class)
public Set<SignalServiceAddress> members = new HashSet<>();
@JsonProperty
public String color;
@JsonProperty(defaultValue = "0")
public int messageExpirationTime;
@JsonProperty(defaultValue = "false")
public boolean blocked;
@JsonProperty
public Integer inboxPosition;
@JsonProperty(defaultValue = "false")
public boolean archived;
public GroupInfoV1(byte[] groupId) {
super(groupId);
}
@Override
public String getTitle() {
return name;
}
public GroupInfoV1(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<SignalServiceAddress> members, @JsonProperty("avatarId") long _ignored_avatarId, @JsonProperty("color") String color, @JsonProperty("blocked") boolean blocked, @JsonProperty("inboxPosition") Integer inboxPosition, @JsonProperty("archived") boolean archived, @JsonProperty("messageExpirationTime") int messageExpirationTime, @JsonProperty("active") boolean _ignored_active) {
super(groupId);
this.name = name;
this.members.addAll(members);
this.color = color;
this.blocked = blocked;
this.inboxPosition = inboxPosition;
this.archived = archived;
this.messageExpirationTime = messageExpirationTime;
}
@JsonIgnore
public Set<SignalServiceAddress> getMembers() {
return members;
}
@Override
public boolean isBlocked() {
return blocked;
}
@Override
public void setBlocked(final boolean blocked) {
this.blocked = blocked;
}
@Override
public int getMessageExpirationTime() {
return messageExpirationTime;
}
public void addMembers(Collection<SignalServiceAddress> addresses) {
for (SignalServiceAddress address : addresses) {
if (this.members.contains(address)) {
continue;
}
removeMember(address);
this.members.add(address);
}
}
public void removeMember(SignalServiceAddress address) {
this.members.removeIf(member -> member.matches(address));
}
private static final class JsonSignalServiceAddress {
@JsonProperty
private UUID uuid;
@JsonProperty
private String number;
JsonSignalServiceAddress(@JsonProperty("uuid") final UUID uuid, @JsonProperty("number") final String number) {
this.uuid = uuid;
this.number = number;
}
JsonSignalServiceAddress(SignalServiceAddress address) {
this.uuid = address.getUuid().orNull();
this.number = address.getNumber().orNull();
}
SignalServiceAddress toSignalServiceAddress() {
return new SignalServiceAddress(uuid, number);
}
}
private static class MembersSerializer extends JsonSerializer<Set<SignalServiceAddress>> {
@Override
public void serialize(final Set<SignalServiceAddress> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
jgen.writeStartArray(value.size());
for (SignalServiceAddress address : value) {
if (address.getUuid().isPresent()) {
jgen.writeObject(new JsonSignalServiceAddress(address));
} else {
jgen.writeString(address.getNumber().get());
}
}
jgen.writeEndArray();
}
}
private static class MembersDeserializer extends JsonDeserializer<Set<SignalServiceAddress>> {
@Override
public Set<SignalServiceAddress> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
Set<SignalServiceAddress> addresses = new HashSet<>();
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
for (JsonNode n : node) {
if (n.isTextual()) {
addresses.add(new SignalServiceAddress(null, n.textValue()));
} else {
JsonSignalServiceAddress address = jsonProcessor.treeToValue(n, JsonSignalServiceAddress.class);
addresses.add(address.toSignalServiceAddress());
}
}
return addresses;
}
}
}

View file

@ -0,0 +1,70 @@
package org.asamk.signal.storage.groups;
import org.signal.storageservice.protos.groups.local.DecryptedGroup;
import org.signal.zkgroup.groups.GroupMasterKey;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
public class GroupInfoV2 extends GroupInfo {
private final GroupMasterKey masterKey;
private boolean blocked;
private DecryptedGroup group; // stored as a file with hexadecimal groupId as name
public GroupInfoV2(final byte[] groupId, final GroupMasterKey masterKey) {
super(groupId);
this.masterKey = masterKey;
}
public GroupMasterKey getMasterKey() {
return masterKey;
}
public void setGroup(final DecryptedGroup group) {
this.group = group;
}
public DecryptedGroup getGroup() {
return group;
}
@Override
public String getTitle() {
if (this.group == null) {
return null;
}
return this.group.getTitle();
}
@Override
public Set<SignalServiceAddress> getMembers() {
if (this.group == null) {
return Collections.emptySet();
}
return group.getMembersList().stream()
.map(m -> new SignalServiceAddress(UuidUtil.parseOrThrow(m.getUuid().toByteArray()), null))
.collect(Collectors.toSet());
}
@Override
public boolean isBlocked() {
return blocked;
}
@Override
public void setBlocked(final boolean blocked) {
this.blocked = blocked;
}
@Override
public int getMessageExpirationTime() {
return this.group != null && this.group.hasDisappearingMessagesTimer()
? this.group.getDisappearingMessagesTimer().getDuration()
: 0;
}
}

View file

@ -12,10 +12,19 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.asamk.signal.util.Hex;
import org.asamk.signal.util.IOUtils;
import org.signal.storageservice.protos.groups.local.DecryptedGroup;
import org.signal.zkgroup.InvalidInputException;
import org.signal.zkgroup.groups.GroupMasterKey;
import org.whispersystems.util.Base64; import org.whispersystems.util.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -23,31 +32,95 @@ import java.util.Map;
public class JsonGroupStore { public class JsonGroupStore {
private static final ObjectMapper jsonProcessor = new ObjectMapper(); private static final ObjectMapper jsonProcessor = new ObjectMapper();
public File groupCachePath;
public static List<GroupInfo> groupsWithLegacyAvatarId = new ArrayList<>();
@JsonProperty("groups") @JsonProperty("groups")
@JsonSerialize(using = JsonGroupStore.MapToListSerializer.class) @JsonSerialize(using = GroupsSerializer.class)
@JsonDeserialize(using = JsonGroupStore.GroupsDeserializer.class) @JsonDeserialize(using = GroupsDeserializer.class)
private Map<String, GroupInfo> groups = new HashMap<>(); private final Map<String, GroupInfo> groups = new HashMap<>();
private JsonGroupStore() {
}
public JsonGroupStore(final File groupCachePath) {
this.groupCachePath = groupCachePath;
}
public void updateGroup(GroupInfo group) { public void updateGroup(GroupInfo group) {
groups.put(Base64.encodeBytes(group.groupId), group); groups.put(Base64.encodeBytes(group.groupId), group);
if (group instanceof GroupInfoV2) {
try {
IOUtils.createPrivateDirectories(groupCachePath);
try (FileOutputStream stream = new FileOutputStream(getGroupFile(group.groupId))) {
((GroupInfoV2) group).getGroup().writeTo(stream);
}
} catch (IOException e) {
System.err.println("Failed to cache group, ignoring ...");
}
}
} }
public GroupInfo getGroup(byte[] groupId) { public GroupInfo getGroup(byte[] groupId) {
return groups.get(Base64.encodeBytes(groupId)); final GroupInfo group = groups.get(Base64.encodeBytes(groupId));
loadDecryptedGroup(group);
return group;
}
private void loadDecryptedGroup(final GroupInfo group) {
if (group instanceof GroupInfoV2 && ((GroupInfoV2) group).getGroup() == null) {
try (FileInputStream stream = new FileInputStream(getGroupFile(group.groupId))) {
((GroupInfoV2) group).setGroup(DecryptedGroup.parseFrom(stream));
} catch (IOException ignored) {
}
}
}
private File getGroupFile(final byte[] groupId) {
return new File(groupCachePath, Hex.toStringCondensed(groupId));
}
public GroupInfoV1 getOrCreateGroupV1(byte[] groupId) {
GroupInfo group = groups.get(Base64.encodeBytes(groupId));
if (group instanceof GroupInfoV1) {
return (GroupInfoV1) group;
}
if (group == null) {
return new GroupInfoV1(groupId);
}
return null;
} }
public List<GroupInfo> getGroups() { public List<GroupInfo> getGroups() {
return new ArrayList<>(groups.values()); final Collection<GroupInfo> groups = this.groups.values();
for (GroupInfo group : groups) {
loadDecryptedGroup(group);
}
return new ArrayList<>(groups);
} }
private static class MapToListSerializer extends JsonSerializer<Map<?, ?>> { private static class GroupsSerializer extends JsonSerializer<Map<String, GroupInfo>> {
@Override @Override
public void serialize(final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException { public void serialize(final Map<String, GroupInfo> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
jgen.writeObject(value.values()); final Collection<GroupInfo> groups = value.values();
jgen.writeStartArray(groups.size());
for (GroupInfo group : groups) {
if (group instanceof GroupInfoV1) {
jgen.writeObject(group);
} else if (group instanceof GroupInfoV2) {
final GroupInfoV2 groupV2 = (GroupInfoV2) group;
jgen.writeStartObject();
jgen.writeStringField("groupId", Base64.encodeBytes(groupV2.groupId));
jgen.writeStringField("masterKey", Base64.encodeBytes(groupV2.getMasterKey().serialize()));
jgen.writeBooleanField("blocked", groupV2.isBlocked());
jgen.writeEndObject();
} else {
throw new AssertionError("Unknown group version");
}
}
jgen.writeEndArray();
} }
} }
@ -58,10 +131,19 @@ public class JsonGroupStore {
Map<String, GroupInfo> groups = new HashMap<>(); Map<String, GroupInfo> groups = new HashMap<>();
JsonNode node = jsonParser.getCodec().readTree(jsonParser); JsonNode node = jsonParser.getCodec().readTree(jsonParser);
for (JsonNode n : node) { for (JsonNode n : node) {
GroupInfo g = jsonProcessor.treeToValue(n, GroupInfo.class); GroupInfo g;
// Check if a legacy avatarId exists if (n.has("masterKey")) {
if (g.getAvatarId() != 0) { // a v2 group
groupsWithLegacyAvatarId.add(g); byte[] groupId = Base64.decode(n.get("groupId").asText());
try {
GroupMasterKey masterKey = new GroupMasterKey(Base64.decode(n.get("masterKey").asText()));
g = new GroupInfoV2(groupId, masterKey);
} catch (InvalidInputException e) {
throw new AssertionError("Invalid master key for group " + Base64.encodeBytes(groupId));
}
g.setBlocked(n.get("blocked").asBoolean(false));
} else {
g = jsonProcessor.treeToValue(n, GroupInfoV1.class);
} }
groups.put(Base64.encodeBytes(g.groupId), g); groups.put(Base64.encodeBytes(g.groupId), g);
} }

View file

@ -48,6 +48,10 @@ public class IOUtils {
public static void createPrivateDirectories(String directoryPath) throws IOException { public static void createPrivateDirectories(String directoryPath) throws IOException {
final File file = new File(directoryPath); final File file = new File(directoryPath);
createPrivateDirectories(file);
}
public static void createPrivateDirectories(File file) throws IOException {
if (file.exists()) { if (file.exists()) {
return; return;
} }