mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Messages are no longer encoded as raw byte arrays.
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
package net.sf.briar.api.protocol;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
public interface MessageParser {
|
||||
|
||||
Message parseMessage(byte[] raw) throws IOException,
|
||||
GeneralSecurityException;
|
||||
}
|
||||
@@ -59,5 +59,5 @@ public interface Reader {
|
||||
|
||||
boolean hasUserDefinedTag() throws IOException;
|
||||
int readUserDefinedTag() throws IOException;
|
||||
void readUserDefinedTag(int i) throws IOException;
|
||||
void readUserDefinedTag(int tag) throws IOException;
|
||||
}
|
||||
|
||||
@@ -18,12 +18,9 @@ import net.sf.briar.api.protocol.BundleReader;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Header;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageParser;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.Raw;
|
||||
import net.sf.briar.api.serial.RawByteArray;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class BundleReaderImpl implements BundleReader {
|
||||
@@ -34,19 +31,19 @@ class BundleReaderImpl implements BundleReader {
|
||||
private final PublicKey publicKey;
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
private final MessageParser messageParser;
|
||||
private final MessageReader messageReader;
|
||||
private final HeaderFactory headerFactory;
|
||||
private final BatchFactory batchFactory;
|
||||
private State state = State.START;
|
||||
|
||||
BundleReaderImpl(Reader reader, PublicKey publicKey, Signature signature,
|
||||
MessageDigest messageDigest, MessageParser messageParser,
|
||||
MessageDigest messageDigest, MessageReader messageParser,
|
||||
HeaderFactory headerFactory, BatchFactory batchFactory) {
|
||||
this.reader = reader;
|
||||
this.publicKey = publicKey;
|
||||
this.signature = signature;
|
||||
this.messageDigest = messageDigest;
|
||||
this.messageParser = messageParser;
|
||||
this.messageReader = messageParser;
|
||||
this.headerFactory = headerFactory;
|
||||
this.batchFactory = batchFactory;
|
||||
}
|
||||
@@ -88,6 +85,7 @@ class BundleReaderImpl implements BundleReader {
|
||||
// Timestamp
|
||||
reader.readUserDefinedTag(Tags.TIMESTAMP);
|
||||
long timestamp = reader.readInt64();
|
||||
if(timestamp < 0L) throw new FormatException();
|
||||
reader.removeConsumer(signing);
|
||||
// Read and verify the signature
|
||||
reader.readUserDefinedTag(Tags.SIGNATURE);
|
||||
@@ -122,11 +120,11 @@ class BundleReaderImpl implements BundleReader {
|
||||
reader.addConsumer(digesting);
|
||||
reader.addConsumer(signing);
|
||||
reader.readUserDefinedTag(Tags.BATCH);
|
||||
List<Raw> rawMessages = new ArrayList<Raw>();
|
||||
List<Message> messages = new ArrayList<Message>();
|
||||
reader.readListStart();
|
||||
while(!reader.hasListEnd()) {
|
||||
reader.readUserDefinedTag(Tags.MESSAGE);
|
||||
rawMessages.add(new RawByteArray(reader.readRaw()));
|
||||
messages.add(messageReader.readMessage(reader));
|
||||
}
|
||||
reader.readListEnd();
|
||||
reader.removeConsumer(signing);
|
||||
@@ -136,12 +134,6 @@ class BundleReaderImpl implements BundleReader {
|
||||
reader.removeConsumer(digesting);
|
||||
reader.removeConsumer(counting);
|
||||
if(!signature.verify(sig)) throw new SignatureException();
|
||||
// Parse the messages
|
||||
List<Message> messages = new ArrayList<Message>(rawMessages.size());
|
||||
for(Raw r : rawMessages) {
|
||||
Message m = messageParser.parseMessage(r.getBytes());
|
||||
messages.add(m);
|
||||
}
|
||||
// Build and return the batch
|
||||
BatchId id = new BatchId(messageDigest.digest());
|
||||
return batchFactory.createBatch(id, messages);
|
||||
|
||||
@@ -53,13 +53,17 @@ class BundleWriterImpl implements BundleWriter {
|
||||
// Write the data to be signed
|
||||
out.setSigning(true);
|
||||
writer.writeUserDefinedTag(Tags.HEADER);
|
||||
// Acks
|
||||
writer.writeListStart();
|
||||
for(BatchId ack : acks) ack.writeTo(writer);
|
||||
writer.writeListEnd();
|
||||
// Subs
|
||||
writer.writeListStart();
|
||||
for(GroupId sub : subs) sub.writeTo(writer);
|
||||
writer.writeListEnd();
|
||||
// Transports
|
||||
writer.writeMap(transports);
|
||||
// Timestamp
|
||||
writer.writeUserDefinedTag(Tags.TIMESTAMP);
|
||||
writer.writeInt64(System.currentTimeMillis());
|
||||
out.setSigning(false);
|
||||
@@ -88,7 +92,8 @@ class BundleWriterImpl implements BundleWriter {
|
||||
writer.writeListStart();
|
||||
for(Raw message : messages) {
|
||||
writer.writeUserDefinedTag(Tags.MESSAGE);
|
||||
writer.writeRaw(message);
|
||||
// Bypass the writer and write the raw message directly
|
||||
out.write(message.getBytes());
|
||||
}
|
||||
writer.writeListEnd();
|
||||
out.setSigning(false);
|
||||
|
||||
27
components/net/sf/briar/protocol/CopyingConsumer.java
Normal file
27
components/net/sf/briar/protocol/CopyingConsumer.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
|
||||
public class CopyingConsumer implements Consumer {
|
||||
|
||||
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
byte[] getCopy() {
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public void write(byte b) throws IOException {
|
||||
out.write(b);
|
||||
}
|
||||
|
||||
public void write(byte[] b) throws IOException {
|
||||
out.write(b);
|
||||
}
|
||||
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
out.write(b, off, len);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
|
||||
@@ -35,16 +36,20 @@ class MessageEncoderImpl implements MessageEncoder {
|
||||
byte[] encodedKey = keyPair.getPublic().getEncoded();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeRaw(parent);
|
||||
w.writeRaw(group);
|
||||
parent.writeTo(w);
|
||||
group.writeTo(w);
|
||||
w.writeUserDefinedTag(Tags.TIMESTAMP);
|
||||
w.writeInt64(timestamp);
|
||||
w.writeUserDefinedTag(Tags.AUTHOR);
|
||||
w.writeString(nick);
|
||||
w.writeRaw(encodedKey);
|
||||
w.writeUserDefinedTag(Tags.MESSAGE_BODY);
|
||||
w.writeRaw(body);
|
||||
byte[] signable = out.toByteArray();
|
||||
signature.initSign(keyPair.getPrivate());
|
||||
signature.update(signable);
|
||||
byte[] sig = signature.sign();
|
||||
w.writeUserDefinedTag(Tags.SIGNATURE);
|
||||
w.writeRaw(sig);
|
||||
byte[] raw = out.toByteArray();
|
||||
w.close();
|
||||
@@ -53,10 +58,13 @@ class MessageEncoderImpl implements MessageEncoder {
|
||||
messageDigest.update(raw);
|
||||
MessageId id = new MessageId(messageDigest.digest());
|
||||
// The author ID is the hash of the author's nick and public key
|
||||
out.reset();
|
||||
w = writerFactory.createWriter(out);
|
||||
w.writeString(nick);
|
||||
w.writeRaw(encodedKey);
|
||||
w.close();
|
||||
messageDigest.reset();
|
||||
messageDigest.update(nick.getBytes("UTF-8"));
|
||||
messageDigest.update((byte) 0); // Null separator
|
||||
messageDigest.update(encodedKey);
|
||||
messageDigest.update(out.toByteArray());
|
||||
AuthorId author = new AuthorId(messageDigest.digest());
|
||||
return new MessageImpl(id, parent, group, author, timestamp, raw);
|
||||
}
|
||||
|
||||
13
components/net/sf/briar/protocol/MessageReader.java
Normal file
13
components/net/sf/briar/protocol/MessageReader.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
interface MessageReader {
|
||||
|
||||
Message readMessage(Reader r) throws IOException,
|
||||
GeneralSecurityException;
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.MessageDigest;
|
||||
@@ -14,61 +13,63 @@ import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.MessageParser;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
|
||||
class MessageParserImpl implements MessageParser {
|
||||
class MessageReaderImpl implements MessageReader {
|
||||
|
||||
private final KeyParser keyParser;
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
private final ReaderFactory readerFactory;
|
||||
|
||||
MessageParserImpl(KeyParser keyParser, Signature signature,
|
||||
MessageDigest messageDigest, ReaderFactory readerFactory) {
|
||||
MessageReaderImpl(KeyParser keyParser, Signature signature,
|
||||
MessageDigest messageDigest) {
|
||||
this.keyParser = keyParser;
|
||||
this.signature = signature;
|
||||
this.messageDigest = messageDigest;
|
||||
this.readerFactory = readerFactory;
|
||||
}
|
||||
|
||||
public Message parseMessage(byte[] raw) throws IOException,
|
||||
public Message readMessage(Reader reader) throws IOException,
|
||||
GeneralSecurityException {
|
||||
if(raw.length > Message.MAX_SIZE) throw new FormatException();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(raw);
|
||||
Reader r = readerFactory.createReader(in);
|
||||
CopyingConsumer copying = new CopyingConsumer();
|
||||
CountingConsumer counting = new CountingConsumer(Message.MAX_SIZE);
|
||||
r.addConsumer(counting);
|
||||
// Read the parent message ID
|
||||
byte[] idBytes = r.readRaw();
|
||||
if(idBytes.length != UniqueId.LENGTH) throw new FormatException();
|
||||
MessageId parent = new MessageId(idBytes);
|
||||
// Read the group ID
|
||||
idBytes = r.readRaw();
|
||||
if(idBytes.length != UniqueId.LENGTH) throw new FormatException();
|
||||
GroupId group = new GroupId(idBytes);
|
||||
// Read the timestamp
|
||||
long timestamp = r.readInt64();
|
||||
// Hash the author's nick and public key to get the author ID
|
||||
String nick = r.readString();
|
||||
byte[] encodedKey = r.readRaw();
|
||||
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
|
||||
messageDigest.reset();
|
||||
messageDigest.update(nick.getBytes("UTF-8"));
|
||||
messageDigest.update((byte) 0); // Null separator
|
||||
messageDigest.update(encodedKey);
|
||||
reader.addConsumer(copying);
|
||||
reader.addConsumer(counting);
|
||||
// Read the parent message ID
|
||||
reader.readUserDefinedTag(Tags.MESSAGE_ID);
|
||||
byte[] b = reader.readRaw();
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
MessageId parent = new MessageId(b);
|
||||
// Read the group ID
|
||||
reader.readUserDefinedTag(Tags.GROUP_ID);
|
||||
b = reader.readRaw();
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
GroupId group = new GroupId(b);
|
||||
// Read the timestamp
|
||||
reader.readUserDefinedTag(Tags.TIMESTAMP);
|
||||
long timestamp = reader.readInt64();
|
||||
if(timestamp < 0L) throw new FormatException();
|
||||
// Hash the author's nick and public key to get the author ID
|
||||
reader.readUserDefinedTag(Tags.AUTHOR);
|
||||
reader.addConsumer(digesting);
|
||||
reader.readString();
|
||||
byte[] encodedKey = reader.readRaw();
|
||||
reader.removeConsumer(digesting);
|
||||
AuthorId author = new AuthorId(messageDigest.digest());
|
||||
// Skip the message body
|
||||
r.readRaw();
|
||||
reader.readUserDefinedTag(Tags.MESSAGE_BODY);
|
||||
reader.readRaw();
|
||||
// Record the length of the signed data
|
||||
int messageLength = (int) counting.getCount();
|
||||
r.removeConsumer(counting);
|
||||
// Read the signature
|
||||
byte[] sig = r.readRaw();
|
||||
// That should be all
|
||||
if(!r.eof()) throw new FormatException();
|
||||
reader.readUserDefinedTag(Tags.SIGNATURE);
|
||||
byte[] sig = reader.readRaw();
|
||||
reader.removeConsumer(counting);
|
||||
reader.removeConsumer(copying);
|
||||
// Verify the signature
|
||||
PublicKey publicKey;
|
||||
try {
|
||||
@@ -76,6 +77,7 @@ class MessageParserImpl implements MessageParser {
|
||||
} catch(InvalidKeySpecException e) {
|
||||
throw new FormatException();
|
||||
}
|
||||
byte[] raw = copying.getCopy();
|
||||
signature.initVerify(publicKey);
|
||||
signature.update(raw, 0, messageLength);
|
||||
if(!signature.verify(sig)) throw new SignatureException();
|
||||
@@ -3,7 +3,6 @@ package net.sf.briar.protocol;
|
||||
import net.sf.briar.api.protocol.BundleReader;
|
||||
import net.sf.briar.api.protocol.BundleWriter;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageParser;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
@@ -16,6 +15,6 @@ public class ProtocolModule extends AbstractModule {
|
||||
bind(BundleWriter.class).to(BundleWriterImpl.class);
|
||||
bind(HeaderFactory.class).to(HeaderFactoryImpl.class);
|
||||
bind(MessageEncoder.class).to(MessageEncoderImpl.class);
|
||||
bind(MessageParser.class).to(MessageParserImpl.class);
|
||||
bind(MessageReader.class).to(MessageReaderImpl.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -479,7 +479,7 @@ class ReaderImpl implements Reader {
|
||||
}
|
||||
}
|
||||
|
||||
public void readUserDefinedTag(int i) throws IOException {
|
||||
if(readUserDefinedTag() != i) throw new FormatException();
|
||||
public void readUserDefinedTag(int tag) throws IOException {
|
||||
if(readUserDefinedTag() != tag) throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ import net.sf.briar.api.protocol.Header;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.MessageParser;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.Raw;
|
||||
import net.sf.briar.api.serial.RawByteArray;
|
||||
@@ -71,8 +70,8 @@ public class BundleReadWriteTest extends TestCase {
|
||||
private final WriterFactory wf;
|
||||
|
||||
private final KeyPair keyPair;
|
||||
private final Signature sig;
|
||||
private final MessageDigest dig;
|
||||
private final Signature sig, sig1;
|
||||
private final MessageDigest dig, dig1;
|
||||
private final KeyParser keyParser;
|
||||
private final Message message;
|
||||
|
||||
@@ -83,7 +82,9 @@ public class BundleReadWriteTest extends TestCase {
|
||||
wf = i.getInstance(WriterFactory.class);
|
||||
keyPair = KeyPairGenerator.getInstance(KEY_PAIR_ALGO).generateKeyPair();
|
||||
sig = Signature.getInstance(SIGNATURE_ALGO);
|
||||
sig1 = Signature.getInstance(SIGNATURE_ALGO);
|
||||
dig = MessageDigest.getInstance(DIGEST_ALGO);
|
||||
dig1 = MessageDigest.getInstance(DIGEST_ALGO);
|
||||
final KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIR_ALGO);
|
||||
keyParser = new KeyParser() {
|
||||
public PublicKey parsePublicKey(byte[] encodedKey)
|
||||
@@ -123,12 +124,12 @@ public class BundleReadWriteTest extends TestCase {
|
||||
|
||||
testWriteBundle();
|
||||
|
||||
MessageParser messageParser =
|
||||
new MessageParserImpl(keyParser, sig, dig, rf);
|
||||
MessageReader messageReader =
|
||||
new MessageReaderImpl(keyParser, sig1, dig1);
|
||||
FileInputStream in = new FileInputStream(bundle);
|
||||
Reader reader = rf.createReader(in);
|
||||
BundleReader r = new BundleReaderImpl(reader, keyPair.getPublic(), sig,
|
||||
dig, messageParser, new HeaderFactoryImpl(),
|
||||
dig, messageReader, new HeaderFactoryImpl(),
|
||||
new BatchFactoryImpl());
|
||||
|
||||
Header h = r.getHeader();
|
||||
@@ -163,12 +164,12 @@ public class BundleReadWriteTest extends TestCase {
|
||||
f.writeByte(b + 1);
|
||||
f.close();
|
||||
|
||||
MessageParser messageParser =
|
||||
new MessageParserImpl(keyParser, sig, dig, rf);
|
||||
MessageReader messageReader =
|
||||
new MessageReaderImpl(keyParser, sig1, dig1);
|
||||
FileInputStream in = new FileInputStream(bundle);
|
||||
Reader reader = rf.createReader(in);
|
||||
BundleReader r = new BundleReaderImpl(reader, keyPair.getPublic(), sig,
|
||||
dig, messageParser, new HeaderFactoryImpl(),
|
||||
dig, messageReader, new HeaderFactoryImpl(),
|
||||
new BatchFactoryImpl());
|
||||
|
||||
Header h = r.getHeader();
|
||||
|
||||
Reference in New Issue
Block a user