From a93cbf852bd3ae5dda127f3e4b869122c0d8a206 Mon Sep 17 00:00:00 2001 From: ameba23 Date: Thu, 6 May 2021 17:48:56 +0200 Subject: [PATCH] RemoteWipeManager builds messages using the message encoder --- .../briar/api/remotewipe/MessageEncoder.java | 3 + .../briar/api/remotewipe/MessageParser.java | 6 ++ .../briar/remotewipe/MessageEncoderImpl.java | 43 ++++++++++++- .../briar/remotewipe/MessageParserImpl.java | 22 ++++++- .../remotewipe/RemoteWipeManagerImpl.java | 12 +++- .../briar/remotewipe/RemoteWipeModule.java | 37 +++++++++++- .../briar/remotewipe/RemoteWipeValidator.java | 60 ++++++++++++++++++- 7 files changed, 175 insertions(+), 8 deletions(-) diff --git a/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageEncoder.java b/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageEncoder.java index 28adfe90f..7fb6c5b2b 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageEncoder.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageEncoder.java @@ -1,4 +1,7 @@ package org.briarproject.briar.api.remotewipe; public interface MessageEncoder { + byte[] encodeSetupMessage(); + + byte[] encodeWipeMessage(); } diff --git a/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageParser.java b/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageParser.java index a45c0cf66..1e897459c 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageParser.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/remotewipe/MessageParser.java @@ -1,4 +1,10 @@ package org.briarproject.briar.api.remotewipe; +import org.briarproject.bramble.api.FormatException; +import org.briarproject.bramble.api.data.BdfList; + public interface MessageParser { + void parseSetupMessage(BdfList body) throws FormatException; + + void parseWipeMessage(BdfList body) throws FormatException; } diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageEncoderImpl.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageEncoderImpl.java index 31bd1b8be..c9ef3ad1d 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageEncoderImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageEncoderImpl.java @@ -1,4 +1,45 @@ package org.briarproject.briar.remotewipe; -public class MessageEncoderImpl { +import org.briarproject.bramble.api.FormatException; +import org.briarproject.bramble.api.client.ClientHelper; +import org.briarproject.bramble.api.data.BdfList; +import org.briarproject.briar.api.remotewipe.MessageEncoder; + +import javax.inject.Inject; + +import static org.briarproject.briar.remotewipe.MessageType.SETUP; +import static org.briarproject.briar.remotewipe.MessageType.WIPE; + +public class MessageEncoderImpl implements MessageEncoder { + + private final ClientHelper clientHelper; + + @Inject + MessageEncoderImpl(ClientHelper clientHelper) { + this.clientHelper = clientHelper; + } + + @Override + public byte[] encodeSetupMessage() { + BdfList body = BdfList.of( + SETUP.getValue() + ); + return encodeBody(body); + } + + @Override + public byte[] encodeWipeMessage() { + BdfList body = BdfList.of( + WIPE.getValue() + ); + return encodeBody(body); + } + + private byte[] encodeBody(BdfList body) { + try { + return clientHelper.toByteArray(body); + } catch (FormatException e) { + throw new AssertionError(e); + } + } } diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageParserImpl.java index b088ae7a8..e37e92591 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/MessageParserImpl.java @@ -1,4 +1,24 @@ package org.briarproject.briar.remotewipe; -public class MessageParserImpl { +import org.briarproject.bramble.api.FormatException; +import org.briarproject.bramble.api.data.BdfList; +import org.briarproject.briar.api.remotewipe.MessageParser; + +import javax.inject.Inject; + +public class MessageParserImpl implements MessageParser { + + @Inject + MessageParserImpl () { + } + + @Override + public void parseSetupMessage(BdfList body) throws FormatException { + + } + + @Override + public void parseWipeMessage(BdfList body) throws FormatException { + + } } diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java index c10ac6e12..ce556f617 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java @@ -25,6 +25,8 @@ import org.briarproject.briar.api.attachment.AttachmentHeader; import org.briarproject.briar.api.client.MessageTracker; import org.briarproject.briar.api.conversation.ConversationMessageHeader; import org.briarproject.briar.api.conversation.DeletionResult; +import org.briarproject.briar.api.remotewipe.MessageEncoder; +import org.briarproject.briar.api.remotewipe.MessageParser; import org.briarproject.briar.api.remotewipe.RemoteWipeManager; import org.briarproject.briar.api.remotewipe.RemoteWipeMessageHeader; import org.briarproject.briar.client.ConversationClientImpl; @@ -54,6 +56,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl private final Clock clock; private final ContactGroupFactory contactGroupFactory; private final ContactManager contactManager; + private final MessageEncoder messageEncoder; + private final MessageParser messageParser; @Inject protected RemoteWipeManagerImpl( @@ -62,6 +66,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl MetadataParser metadataParser, MessageTracker messageTracker, Clock clock, + MessageEncoder messageEncoder, + MessageParser messageParser, ContactManager contactManager, ClientVersioningManager clientVersioningManager, ContactGroupFactory contactGroupFactory) { @@ -70,6 +76,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl this.contactGroupFactory = contactGroupFactory; this.contactManager = contactManager; this.clientVersioningManager = clientVersioningManager; + this.messageEncoder = messageEncoder; + this.messageParser = messageParser; localGroup = contactGroupFactory.createLocalGroup(CLIENT_ID, MAJOR_VERSION); } @@ -138,7 +146,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl if (!db.containsGroup(txn, g)) db.addGroup(txn, group); long timestamp = clock.currentTimeMillis(); - byte[] body = "setup message".getBytes(); // TODO + byte[] body = messageEncoder.encodeSetupMessage(); Message m = clientHelper.createMessage(g, timestamp, body); // TODO remote-wipe versions of MESSAGE_KEY @@ -160,7 +168,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl if (!db.containsGroup(txn, g)) db.addGroup(txn, group); long timestamp = clock.currentTimeMillis(); - byte[] body = "wipe message".getBytes(); // TODO + byte[] body = messageEncoder.encodeWipeMessage(); Message m = clientHelper.createMessage(g, timestamp, body); // TODO remote-wipe versions of MESSAGE_KEY diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeModule.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeModule.java index 64bc103d3..ba09c3eb7 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeModule.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeModule.java @@ -1,10 +1,15 @@ package org.briarproject.briar.remotewipe; +import org.briarproject.bramble.api.client.ClientHelper; import org.briarproject.bramble.api.contact.ContactManager; +import org.briarproject.bramble.api.data.MetadataEncoder; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.sync.validation.ValidationManager; +import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.versioning.ClientVersioningManager; import org.briarproject.briar.api.conversation.ConversationManager; +import org.briarproject.briar.api.remotewipe.MessageEncoder; +import org.briarproject.briar.api.remotewipe.MessageParser; import org.briarproject.briar.api.remotewipe.RemoteWipeManager; import javax.inject.Inject; @@ -13,15 +18,17 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; -import static org.briarproject.briar.api.socialbackup.SocialBackupManager.CLIENT_ID; -import static org.briarproject.briar.api.socialbackup.SocialBackupManager.MAJOR_VERSION; -import static org.briarproject.briar.api.socialbackup.SocialBackupManager.MINOR_VERSION; +import static org.briarproject.briar.api.remotewipe.RemoteWipeManager.CLIENT_ID; +import static org.briarproject.briar.api.remotewipe.RemoteWipeManager.MAJOR_VERSION; +import static org.briarproject.briar.api.remotewipe.RemoteWipeManager.MINOR_VERSION; @Module public class RemoteWipeModule { public static class EagerSingletons { @Inject RemoteWipeManager remoteWipeManager; + @Inject + RemoteWipeValidator remoteWipeValidator; } @Provides @@ -44,4 +51,28 @@ public class RemoteWipeModule { conversationManager.registerConversationClient(remoteWipeManager); return remoteWipeManager; } + + @Provides + @Singleton + RemoteWipeValidator remoteWipeValidator( + ValidationManager validationManager, + ClientHelper clientHelper, + MetadataEncoder metadataEncoder, + Clock clock) { + RemoteWipeValidator validator = + new RemoteWipeValidator(clientHelper, metadataEncoder, clock); + validationManager.registerMessageValidator(CLIENT_ID, MAJOR_VERSION, + validator); + return validator; + } + + @Provides + MessageEncoder messageEncoder(MessageEncoderImpl messageEncoder) { + return messageEncoder; + } + + @Provides + MessageParser messageParser(MessageParserImpl messageParser) { + return messageParser; + } } diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeValidator.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeValidator.java index 1b3a3a8e0..2a52c9f6f 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeValidator.java @@ -1,4 +1,62 @@ package org.briarproject.briar.remotewipe; -public class RemoteWipeValidator { +import org.briarproject.bramble.api.FormatException; +import org.briarproject.bramble.api.client.BdfMessageContext; +import org.briarproject.bramble.api.client.BdfMessageValidator; +import org.briarproject.bramble.api.client.ClientHelper; +import org.briarproject.bramble.api.data.BdfDictionary; +import org.briarproject.bramble.api.data.BdfEntry; +import org.briarproject.bramble.api.data.BdfList; +import org.briarproject.bramble.api.data.MetadataEncoder; +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; +import org.briarproject.bramble.api.sync.Group; +import org.briarproject.bramble.api.sync.Message; +import org.briarproject.bramble.api.system.Clock; + +import javax.annotation.concurrent.Immutable; +import javax.inject.Inject; + +import static org.briarproject.bramble.util.ValidationUtils.checkSize; +import static org.briarproject.briar.remotewipe.MessageType.SETUP; +import static org.briarproject.briar.remotewipe.MessageType.WIPE; +import static org.briarproject.briar.socialbackup.SocialBackupConstants.MSG_KEY_LOCAL; +import static org.briarproject.briar.socialbackup.SocialBackupConstants.MSG_KEY_MESSAGE_TYPE; + +@Immutable +@NotNullByDefault +class RemoteWipeValidator extends BdfMessageValidator { + + @Inject + RemoteWipeValidator(ClientHelper clientHelper, + MetadataEncoder metadataEncoder, Clock clock) { + super(clientHelper, metadataEncoder, clock); + } + + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList body) throws FormatException { + MessageType type = MessageType + .fromValue(body.getLong(0).intValue()); + if (type == SETUP) return validateSetupMessage(body); + else if (type == WIPE) return validateWipeMessage(body); + else throw new AssertionError(); + } + + private BdfMessageContext validateSetupMessage(BdfList body) + throws FormatException { + checkSize(body, 1); + BdfDictionary meta = BdfDictionary.of( + new BdfEntry(MSG_KEY_MESSAGE_TYPE, SETUP.getValue()), + new BdfEntry(MSG_KEY_LOCAL, false)); + return new BdfMessageContext(meta); + } + + private BdfMessageContext validateWipeMessage(BdfList body) + throws FormatException { + checkSize(body, 1); + BdfDictionary meta = BdfDictionary.of( + new BdfEntry(MSG_KEY_MESSAGE_TYPE, WIPE.getValue()), + new BdfEntry(MSG_KEY_LOCAL, false)); + return new BdfMessageContext(meta); + } }