mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Defined some user-defined tags for protocol elements. Currently they're just adding redundancy, but in future they'll be used for parsing nested elements.
This commit is contained in:
@@ -20,8 +20,10 @@ 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.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 {
|
||||
@@ -59,23 +61,36 @@ class BundleReaderImpl implements BundleReader {
|
||||
// Read the signed data
|
||||
reader.addConsumer(counting);
|
||||
reader.addConsumer(signing);
|
||||
reader.readUserDefinedTag(Tags.HEADER);
|
||||
// Acks
|
||||
Set<BatchId> acks = new HashSet<BatchId>();
|
||||
for(Raw raw : reader.readList(Raw.class)) {
|
||||
byte[] b = raw.getBytes();
|
||||
reader.readListStart();
|
||||
while(!reader.hasListEnd()) {
|
||||
reader.readUserDefinedTag(Tags.BATCH_ID);
|
||||
byte[] b = reader.readRaw();
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
acks.add(new BatchId(b));
|
||||
}
|
||||
reader.readListEnd();
|
||||
// Subs
|
||||
Set<GroupId> subs = new HashSet<GroupId>();
|
||||
for(Raw raw : reader.readList(Raw.class)) {
|
||||
byte[] b = raw.getBytes();
|
||||
reader.readListStart();
|
||||
while(!reader.hasListEnd()) {
|
||||
reader.readUserDefinedTag(Tags.GROUP_ID);
|
||||
byte[] b = reader.readRaw();
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
subs.add(new GroupId(b));
|
||||
}
|
||||
reader.readListEnd();
|
||||
// Transports
|
||||
Map<String, String> transports =
|
||||
reader.readMap(String.class, String.class);
|
||||
// Timestamp
|
||||
reader.readUserDefinedTag(Tags.TIMESTAMP);
|
||||
long timestamp = reader.readInt64();
|
||||
reader.removeConsumer(signing);
|
||||
// Read and verify the signature
|
||||
reader.readUserDefinedTag(Tags.SIGNATURE);
|
||||
byte[] sig = reader.readRaw();
|
||||
reader.removeConsumer(counting);
|
||||
if(!signature.verify(sig)) throw new SignatureException();
|
||||
@@ -106,9 +121,17 @@ class BundleReaderImpl implements BundleReader {
|
||||
reader.addConsumer(counting);
|
||||
reader.addConsumer(digesting);
|
||||
reader.addConsumer(signing);
|
||||
List<Raw> rawMessages = reader.readList(Raw.class);
|
||||
reader.readUserDefinedTag(Tags.BATCH);
|
||||
List<Raw> rawMessages = new ArrayList<Raw>();
|
||||
reader.readListStart();
|
||||
while(!reader.hasListEnd()) {
|
||||
reader.readUserDefinedTag(Tags.MESSAGE);
|
||||
rawMessages.add(new RawByteArray(reader.readRaw()));
|
||||
}
|
||||
reader.readListEnd();
|
||||
reader.removeConsumer(signing);
|
||||
// Read and verify the signature
|
||||
reader.readUserDefinedTag(Tags.SIGNATURE);
|
||||
byte[] sig = reader.readRaw();
|
||||
reader.removeConsumer(digesting);
|
||||
reader.removeConsumer(counting);
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.Map;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.BundleWriter;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.Raw;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
@@ -20,7 +21,7 @@ class BundleWriterImpl implements BundleWriter {
|
||||
private static enum State { START, FIRST_BATCH, MORE_BATCHES, END };
|
||||
|
||||
private final SigningDigestingOutputStream out;
|
||||
private final Writer w;
|
||||
private final Writer writer;
|
||||
private final PrivateKey privateKey;
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
@@ -32,7 +33,7 @@ class BundleWriterImpl implements BundleWriter {
|
||||
MessageDigest messageDigest, long capacity) {
|
||||
this.out =
|
||||
new SigningDigestingOutputStream(out, signature, messageDigest);
|
||||
w = writerFactory.createWriter(this.out);
|
||||
writer = writerFactory.createWriter(this.out);
|
||||
this.privateKey = privateKey;
|
||||
this.signature = signature;
|
||||
this.messageDigest = messageDigest;
|
||||
@@ -40,7 +41,7 @@ class BundleWriterImpl implements BundleWriter {
|
||||
}
|
||||
|
||||
public long getRemainingCapacity() {
|
||||
return capacity - w.getBytesWritten();
|
||||
return capacity - writer.getBytesWritten();
|
||||
}
|
||||
|
||||
public void addHeader(Iterable<BatchId> acks, Iterable<GroupId> subs,
|
||||
@@ -51,18 +52,21 @@ class BundleWriterImpl implements BundleWriter {
|
||||
signature.initSign(privateKey);
|
||||
// Write the data to be signed
|
||||
out.setSigning(true);
|
||||
w.writeListStart();
|
||||
for(BatchId ack : acks) w.writeRaw(ack);
|
||||
w.writeListEnd();
|
||||
w.writeListStart();
|
||||
for(GroupId sub : subs) w.writeRaw(sub);
|
||||
w.writeListEnd();
|
||||
w.writeMap(transports);
|
||||
w.writeInt64(System.currentTimeMillis());
|
||||
writer.writeUserDefinedTag(Tags.HEADER);
|
||||
writer.writeListStart();
|
||||
for(BatchId ack : acks) ack.writeTo(writer);
|
||||
writer.writeListEnd();
|
||||
writer.writeListStart();
|
||||
for(GroupId sub : subs) sub.writeTo(writer);
|
||||
writer.writeListEnd();
|
||||
writer.writeMap(transports);
|
||||
writer.writeUserDefinedTag(Tags.TIMESTAMP);
|
||||
writer.writeInt64(System.currentTimeMillis());
|
||||
out.setSigning(false);
|
||||
// Create and write the signature
|
||||
byte[] sig = signature.sign();
|
||||
w.writeRaw(sig);
|
||||
writer.writeUserDefinedTag(Tags.SIGNATURE);
|
||||
writer.writeRaw(sig);
|
||||
// Expect a (possibly empty) list of batches
|
||||
state = State.FIRST_BATCH;
|
||||
}
|
||||
@@ -70,7 +74,7 @@ class BundleWriterImpl implements BundleWriter {
|
||||
public BatchId addBatch(Iterable<Raw> messages) throws IOException,
|
||||
GeneralSecurityException {
|
||||
if(state == State.FIRST_BATCH) {
|
||||
w.writeListStart();
|
||||
writer.writeListStart();
|
||||
state = State.MORE_BATCHES;
|
||||
}
|
||||
if(state != State.MORE_BATCHES) throw new IllegalStateException();
|
||||
@@ -80,13 +84,18 @@ class BundleWriterImpl implements BundleWriter {
|
||||
// Write the data to be signed
|
||||
out.setDigesting(true);
|
||||
out.setSigning(true);
|
||||
w.writeListStart();
|
||||
for(Raw message : messages) w.writeRaw(message);
|
||||
w.writeListEnd();
|
||||
writer.writeUserDefinedTag(Tags.BATCH);
|
||||
writer.writeListStart();
|
||||
for(Raw message : messages) {
|
||||
writer.writeUserDefinedTag(Tags.MESSAGE);
|
||||
writer.writeRaw(message);
|
||||
}
|
||||
writer.writeListEnd();
|
||||
out.setSigning(false);
|
||||
// Create and write the signature
|
||||
byte[] sig = signature.sign();
|
||||
w.writeRaw(sig);
|
||||
writer.writeUserDefinedTag(Tags.SIGNATURE);
|
||||
writer.writeRaw(sig);
|
||||
out.setDigesting(false);
|
||||
// Calculate and return the ID
|
||||
return new BatchId(messageDigest.digest());
|
||||
@@ -94,12 +103,12 @@ class BundleWriterImpl implements BundleWriter {
|
||||
|
||||
public void finish() throws IOException {
|
||||
if(state == State.FIRST_BATCH) {
|
||||
w.writeListStart();
|
||||
writer.writeListStart();
|
||||
state = State.MORE_BATCHES;
|
||||
}
|
||||
if(state != State.MORE_BATCHES) throw new IllegalStateException();
|
||||
w.writeListEnd();
|
||||
w.close();
|
||||
writer.writeListEnd();
|
||||
writer.close();
|
||||
state = State.END;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -478,4 +478,8 @@ class ReaderImpl implements Reader {
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
|
||||
public void readUserDefinedTag(int i) throws IOException {
|
||||
if(readUserDefinedTag() != i) throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user