Generate message and group IDs in a forward-compatible way.

This commit is contained in:
akwizgran
2018-04-18 16:34:02 +01:00
parent 8decc73f4d
commit 9196169561
7 changed files with 46 additions and 33 deletions

View File

@@ -1,8 +1,8 @@
package org.briarproject.briar.client;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.ValidationManager;
import org.briarproject.briar.api.client.MessageQueueManager;
import org.briarproject.briar.api.client.MessageTracker;
@@ -26,8 +26,9 @@ public class BriarClientModule {
}
@Provides
QueueMessageFactory provideQueueMessageFactory(CryptoComponent crypto) {
return new QueueMessageFactoryImpl(crypto);
QueueMessageFactory provideQueueMessageFactory(
MessageFactory messageFactory) {
return new QueueMessageFactoryImpl(messageFactory);
}
@Provides

View File

@@ -1,9 +1,10 @@
package org.briarproject.briar.client;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.util.ByteUtils;
import org.briarproject.briar.api.client.QueueMessage;
@@ -12,10 +13,8 @@ import org.briarproject.briar.api.client.QueueMessageFactory;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import static org.briarproject.bramble.api.sync.MessageId.LABEL;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES;
import static org.briarproject.briar.api.client.QueueMessage.MAX_QUEUE_MESSAGE_BODY_LENGTH;
import static org.briarproject.briar.api.client.QueueMessage.QUEUE_MESSAGE_HEADER_LENGTH;
@@ -24,11 +23,11 @@ import static org.briarproject.briar.api.client.QueueMessage.QUEUE_MESSAGE_HEADE
@NotNullByDefault
class QueueMessageFactoryImpl implements QueueMessageFactory {
private final CryptoComponent crypto;
private final MessageFactory messageFactory;
@Inject
QueueMessageFactoryImpl(CryptoComponent crypto) {
this.crypto = crypto;
QueueMessageFactoryImpl(MessageFactory messageFactory) {
this.messageFactory = messageFactory;
}
@Override
@@ -36,21 +35,13 @@ class QueueMessageFactoryImpl implements QueueMessageFactory {
long queuePosition, byte[] body) {
if (body.length > MAX_QUEUE_MESSAGE_BODY_LENGTH)
throw new IllegalArgumentException();
byte[] raw = new byte[QUEUE_MESSAGE_HEADER_LENGTH + body.length];
System.arraycopy(groupId.getBytes(), 0, raw, 0, UniqueId.LENGTH);
ByteUtils.writeUint64(timestamp, raw, UniqueId.LENGTH);
ByteUtils.writeUint64(queuePosition, raw, MESSAGE_HEADER_LENGTH);
System.arraycopy(body, 0, raw, QUEUE_MESSAGE_HEADER_LENGTH,
body.length);
byte[] timeBytes = new byte[INT_64_BYTES];
ByteUtils.writeUint64(timestamp, timeBytes, 0);
byte[] bodyBytes = new byte[body.length + INT_64_BYTES];
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, bodyBytes, 0,
body.length + INT_64_BYTES);
byte[] hash = crypto.hash(LABEL, new byte[] {PROTOCOL_VERSION},
groupId.getBytes(), timeBytes, bodyBytes);
MessageId id = new MessageId(hash);
return new QueueMessage(id, groupId, timestamp, queuePosition, raw);
byte[] messageBody = new byte[INT_64_BYTES + body.length];
ByteUtils.writeUint64(queuePosition, messageBody, 0);
System.arraycopy(body, 0, messageBody, INT_64_BYTES, body.length);
Message m = messageFactory.createMessage(groupId, timestamp,
messageBody);
return new QueueMessage(m.getId(), groupId, timestamp, queuePosition,
m.getRaw());
}
@Override