From 3f19aeb2d70e9f21adc00a8abd801803be705883 Mon Sep 17 00:00:00 2001 From: ameba23 Date: Mon, 21 Feb 2022 09:09:10 +0100 Subject: [PATCH] Implement CONFIRM remote wipe message --- .../briar/remotewipe/MessageEncoderImpl.java | 9 ++++ .../remotewipe/RemoteWipeManagerImpl.java | 43 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) 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 cdfab49f8..26f9a1005 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 @@ -7,6 +7,7 @@ import org.briarproject.briar.api.remotewipe.MessageEncoder; import javax.inject.Inject; +import static org.briarproject.briar.api.remotewipe.MessageType.CONFIRM; import static org.briarproject.briar.api.remotewipe.MessageType.SETUP; import static org.briarproject.briar.api.remotewipe.MessageType.WIPE; import static org.briarproject.briar.api.remotewipe.MessageType.REVOKE; @@ -44,6 +45,14 @@ public class MessageEncoderImpl implements MessageEncoder { return encodeBody(body); } + @Override + public byte[] encodeConfirmMessage() { + BdfList body = BdfList.of( + CONFIRM.getValue() + ); + return encodeBody(body); + } + private byte[] encodeBody(BdfList body) { try { return clientHelper.toByteArray(body); 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 997f8e467..437953847 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 @@ -47,6 +47,7 @@ import javax.annotation.Nullable; import javax.inject.Inject; import static java.util.logging.Logger.getLogger; +import static org.briarproject.briar.api.remotewipe.MessageType.CONFIRM; import static org.briarproject.briar.api.remotewipe.MessageType.REVOKE; import static org.briarproject.briar.api.remotewipe.MessageType.SETUP; import static org.briarproject.briar.api.remotewipe.MessageType.WIPE; @@ -191,7 +192,11 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl } if (receivedWipeMessages.size() + 1 == THRESHOLD) { - LOG.warning("Threshold reached - panic!"); + LOG.warning("Threshold number of remote wipe signals reached - panic!"); + + // Send a CONFIRM message to each wiper + sendConfirmMessages(txn); + if (observer != null) { observer.onPanic(); } @@ -227,6 +232,14 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl db.addGroup(txn, localGroup); clientHelper .mergeGroupMetadata(txn, localGroup.getId(), localRecord); + } else if (type == CONFIRM) { + messageTracker.trackIncomingMessage(txn, m); + ContactId contactId = getContactId(txn, m.getGroupId()); + + MessageStatus status = db.getMessageStatus(txn, contactId, + m.getId()); + txn.attach(new RemoteWipeReceivedEvent( + createMessageHeader(m, meta, status, type), contactId)); } return false; @@ -314,6 +327,34 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl messageTracker.trackOutgoingMessage(txn, m); } + private void sendConfirmMessages(Transaction txn) + throws DbException, FormatException { + List wipers = getWiperContactIds(txn); + for (ContactId c : wipers) { + Contact contact = contactManager.getContact(txn, c); + sendConfirmMessage(txn, contact); + } + } + + private void sendConfirmMessage(Transaction txn, Contact contact) + throws DbException, FormatException { + Group group = getContactGroup(contact); + GroupId g = group.getId(); + if (!db.containsGroup(txn, g)) db.addGroup(txn, group); + long timestamp = clock.currentTimeMillis(); + + byte[] body = messageEncoder.encodeConfirmMessage(); + + Message m = clientHelper.createMessage(g, timestamp, body); + BdfDictionary meta = BdfDictionary.of( + new BdfEntry(MSG_KEY_MESSAGE_TYPE, CONFIRM.getValue()), + new BdfEntry(MSG_KEY_LOCAL, true), + new BdfEntry(MSG_KEY_TIMESTAMP, timestamp) + ); + clientHelper.addLocalMessage(txn, m, meta, true, false); + messageTracker.trackOutgoingMessage(txn, m); + } + public void wipe(Transaction txn, Contact contact) throws DbException, FormatException { // Check that we have wiper status