Changed maximum packet and message sizes in preparation for new

transport format.
This commit is contained in:
akwizgran
2011-08-18 15:14:48 +02:00
parent 5e0aadd373
commit 4dd303d9e1
40 changed files with 210 additions and 146 deletions

View File

@@ -5,12 +5,6 @@ import java.util.Collection;
/** A packet acknowledging receipt of one or more batches. */ /** A packet acknowledging receipt of one or more batches. */
public interface Ack { public interface Ack {
/**
* The maximum size of a serialised ack, excluding encryption and
* authentication.
*/
static final int MAX_SIZE = (1024 * 1024) - 100;
/** Returns the IDs of the acknowledged batches. */ /** Returns the IDs of the acknowledged batches. */
Collection<BatchId> getBatchIds(); Collection<BatchId> getBatchIds();
} }

View File

@@ -5,12 +5,15 @@ import net.sf.briar.api.serial.Writable;
/** A pseudonymous author of messages. */ /** A pseudonymous author of messages. */
public interface Author extends Writable { public interface Author extends Writable {
/** The maximum length of an author's name, in UTF-8 bytes. */ /** The maximum length of an author's name in UTF-8 bytes. */
static final int MAX_NAME_LENGTH = 50; static final int MAX_NAME_LENGTH = 50;
/** The maximum length of an author's public key, in bytes. */ /** The maximum length of an author's public key in bytes. */
static final int MAX_PUBLIC_KEY_LENGTH = 100; static final int MAX_PUBLIC_KEY_LENGTH = 100;
/** The maximum length of a serialised author in bytes. */
static final int MAX_LENGTH = MAX_NAME_LENGTH + MAX_PUBLIC_KEY_LENGTH + 5;
/** Returns the author's unique identifier. */ /** Returns the author's unique identifier. */
AuthorId getId(); AuthorId getId();

View File

@@ -5,12 +5,6 @@ import java.util.Collection;
/** A packet containing messages. */ /** A packet containing messages. */
public interface Batch { public interface Batch {
/**
* The maximum size of a serialised batch, excluding encryption and
* authentication.
*/
static final int MAX_SIZE = (1024 * 1024) - 100;
/** Returns the batch's unique identifier. */ /** Returns the batch's unique identifier. */
BatchId getId(); BatchId getId();

View File

@@ -5,12 +5,15 @@ import net.sf.briar.api.serial.Writable;
/** A group to which users may subscribe. */ /** A group to which users may subscribe. */
public interface Group extends Writable { public interface Group extends Writable {
/** The maximum length of a group's name, in UTF-8 bytes. */ /** The maximum length of a group's name in UTF-8 bytes. */
static final int MAX_NAME_LENGTH = 50; static final int MAX_NAME_LENGTH = 50;
/** The maximum length of a group's public key, in bytes. */ /** The maximum length of a group's public key in bytes. */
static final int MAX_PUBLIC_KEY_LENGTH = 100; static final int MAX_PUBLIC_KEY_LENGTH = 100;
/** The maximum length of a serialised group in bytes. */
static final int MAX_LENGTH = MAX_NAME_LENGTH + MAX_PUBLIC_KEY_LENGTH + 5;
/** Returns the group's unique identifier. */ /** Returns the group's unique identifier. */
GroupId getId(); GroupId getId();

View File

@@ -2,12 +2,23 @@ package net.sf.briar.api.protocol;
public interface Message { public interface Message {
/** The maximum size of a serialised message, in bytes. */ /**
static final int MAX_SIZE = (1024 * 1024) - 200; * The maximum length of a serialised message in bytes. To allow for future
* changes in the batch format, this is smaller than the amount of data
* that can fit in a batch using the current format.
*/
static final int MAX_LENGTH = ProtocolConstants.MAX_PACKET_LENGTH - 1024;
/** The maximum size of a signature, in bytes. */ /** The maximum length of a signature in bytes. */
static final int MAX_SIGNATURE_LENGTH = 100; static final int MAX_SIGNATURE_LENGTH = 100;
/**
* The maximum length of a message body in bytes. To allow for future
* changes in the message format, this is smaller than the amount of data
* that can fit in a message using the current format.
*/
static final int MAX_BODY_LENGTH = MAX_LENGTH - 1024;
/** Returns the message's unique identifier. */ /** Returns the message's unique identifier. */
MessageId getId(); MessageId getId();

View File

@@ -5,12 +5,6 @@ import java.util.Collection;
/** A packet offering the recipient some messages. */ /** A packet offering the recipient some messages. */
public interface Offer { public interface Offer {
/**
* The maximum size of a serialised offer, excluding encryption and
* authentication.
*/
static final int MAX_SIZE = (1024 * 1024) - 100;
/** Returns the offer's unique identifier. */ /** Returns the offer's unique identifier. */
OfferId getId(); OfferId getId();

View File

@@ -0,0 +1,14 @@
package net.sf.briar.api.protocol;
import net.sf.briar.api.transport.TransportConstants;
public interface ProtocolConstants {
/**
* The maximum length of a serialised packet in bytes. To allow for future
* changes in the frame format, this is smaller than the amount of data
* that can fit in a frame using the current format.
*/
static final int MAX_PACKET_LENGTH =
TransportConstants.MAX_FRAME_LENGTH - 1024;
}

View File

@@ -5,12 +5,6 @@ import java.util.BitSet;
/** A packet requesting some or all of the messages from an offer. */ /** A packet requesting some or all of the messages from an offer. */
public interface Request { public interface Request {
/**
* The maximum size of a serialised request, exlcuding encryption and
* authentication.
*/
static final int MAX_SIZE = (1024 * 1024) - 100;
/** /**
* Returns the unique identifier of the offer to which this request * Returns the unique identifier of the offer to which this request
* responds. * responds.

View File

@@ -5,12 +5,6 @@ import java.util.Map;
/** A packet updating the sender's subscriptions. */ /** A packet updating the sender's subscriptions. */
public interface SubscriptionUpdate { public interface SubscriptionUpdate {
/**
* The maximum size of a serialized subscription update, excluding
* encryption and authentication.
*/
static final int MAX_SIZE = (1024 * 1024) - 100;
/** Returns the subscriptions contained in the update. */ /** Returns the subscriptions contained in the update. */
Map<Group, Long> getSubscriptions(); Map<Group, Long> getSubscriptions();

View File

@@ -5,12 +5,6 @@ import java.util.Map;
/** A packet updating the sender's transport properties. */ /** A packet updating the sender's transport properties. */
public interface TransportUpdate { public interface TransportUpdate {
/**
* The maximum size of a serialised transport update, excluding
* encryption and authentication.
*/
static final int MAX_SIZE = (1024 * 1024) - 100;
/** Returns the transport properties contained in the update. */ /** Returns the transport properties contained in the update. */
Map<String, Map<String, String>> getTransports(); Map<String, Map<String, String>> getTransports();

View File

@@ -6,8 +6,8 @@ import net.sf.briar.api.serial.Writable;
public abstract class UniqueId implements Writable { public abstract class UniqueId implements Writable {
/** The length of a unique identifier in bytes. */
public static final int LENGTH = 32; public static final int LENGTH = 32;
public static final int SERIALISED_LENGTH = LENGTH + 3;
protected final byte[] id; protected final byte[] id;

View File

@@ -0,0 +1,22 @@
package net.sf.briar.api.transport;
public interface TransportConstants {
/**
* The maximum length of a frame in bytes, including the header and footer.
*/
static final int MAX_FRAME_LENGTH = 65536; // 2^16
/** The length in bytes of the tag that uniquely identifies a connection. */
static final int TAG_LENGTH = 16;
/**
* The maximum value that can be represented as an unsigned 16-bit integer.
*/
static final int MAX_16_BIT_UNSIGNED = 65535; // 2^16 - 1
/**
* The maximum value that can be represented as an unsigned 32-bit integer.
*/
static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
}

View File

@@ -5,6 +5,7 @@ import java.util.Collection;
import net.sf.briar.api.protocol.Ack; import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader; import net.sf.briar.api.serial.ObjectReader;
@@ -22,7 +23,8 @@ class AckReader implements ObjectReader<Ack> {
public Ack readObject(Reader r) throws IOException { public Ack readObject(Reader r) throws IOException {
// Initialise the consumer // Initialise the consumer
Consumer counting = new CountingConsumer(Ack.MAX_SIZE); Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read and digest the data // Read and digest the data
r.addConsumer(counting); r.addConsumer(counting);
r.readUserDefinedTag(Tags.ACK); r.readUserDefinedTag(Tags.ACK);

View File

@@ -8,6 +8,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Batch; import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Message; import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader; import net.sf.briar.api.serial.ObjectReader;
@@ -28,7 +29,8 @@ class BatchReader implements ObjectReader<Batch> {
public Batch readObject(Reader r) throws IOException { public Batch readObject(Reader r) throws IOException {
// Initialise the consumers // Initialise the consumers
Consumer counting = new CountingConsumer(Batch.MAX_SIZE); Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
DigestingConsumer digesting = new DigestingConsumer(messageDigest); DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset(); messageDigest.reset();
// Read and digest the data // Read and digest the data

View File

@@ -41,7 +41,7 @@ class MessageReader implements ObjectReader<Message> {
public Message readObject(Reader r) throws IOException { public Message readObject(Reader r) throws IOException {
CopyingConsumer copying = new CopyingConsumer(); CopyingConsumer copying = new CopyingConsumer();
CountingConsumer counting = new CountingConsumer(Message.MAX_SIZE); CountingConsumer counting = new CountingConsumer(Message.MAX_LENGTH);
r.addConsumer(copying); r.addConsumer(copying);
r.addConsumer(counting); r.addConsumer(counting);
// Read the initial tag // Read the initial tag
@@ -64,7 +64,7 @@ class MessageReader implements ObjectReader<Message> {
long timestamp = r.readInt64(); long timestamp = r.readInt64();
if(timestamp < 0L) throw new FormatException(); if(timestamp < 0L) throw new FormatException();
// Skip the message body // Skip the message body
r.readBytes(Message.MAX_SIZE); r.readBytes(Message.MAX_LENGTH);
// Record the length of the data covered by the author's signature // Record the length of the data covered by the author's signature
int signedByAuthor = (int) counting.getCount(); int signedByAuthor = (int) counting.getCount();
// Read the author's signature, if there is one // Read the author's signature, if there is one

View File

@@ -8,6 +8,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.MessageId; import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer; import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.OfferId; import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader; import net.sf.briar.api.serial.ObjectReader;
@@ -28,7 +29,8 @@ class OfferReader implements ObjectReader<Offer> {
public Offer readObject(Reader r) throws IOException { public Offer readObject(Reader r) throws IOException {
// Initialise the consumers // Initialise the consumers
Consumer counting = new CountingConsumer(Offer.MAX_SIZE); Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
DigestingConsumer digesting = new DigestingConsumer(messageDigest); DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset(); messageDigest.reset();
// Read the data // Read the data

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.BitSet; import java.util.BitSet;
import net.sf.briar.api.protocol.OfferId; import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Request; import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
@@ -23,13 +24,14 @@ class RequestReader implements ObjectReader<Request> {
public Request readObject(Reader r) throws IOException { public Request readObject(Reader r) throws IOException {
// Initialise the consumer // Initialise the consumer
Consumer counting = new CountingConsumer(Request.MAX_SIZE); Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data // Read the data
r.addConsumer(counting); r.addConsumer(counting);
r.readUserDefinedTag(Tags.REQUEST); r.readUserDefinedTag(Tags.REQUEST);
r.addObjectReader(Tags.OFFER_ID, offerIdReader); r.addObjectReader(Tags.OFFER_ID, offerIdReader);
OfferId offerId = r.readUserDefined(Tags.OFFER_ID, OfferId.class); OfferId offerId = r.readUserDefined(Tags.OFFER_ID, OfferId.class);
byte[] bitmap = r.readBytes(Request.MAX_SIZE); byte[] bitmap = r.readBytes(ProtocolConstants.MAX_PACKET_LENGTH);
r.removeConsumer(counting); r.removeConsumer(counting);
// Convert the bitmap into a BitSet // Convert the bitmap into a BitSet
BitSet b = new BitSet(bitmap.length * 8); BitSet b = new BitSet(bitmap.length * 8);

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.protocol.Group; import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.SubscriptionUpdate; import net.sf.briar.api.protocol.SubscriptionUpdate;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
@@ -23,7 +24,8 @@ class SubscriptionReader implements ObjectReader<SubscriptionUpdate> {
public SubscriptionUpdate readObject(Reader r) throws IOException { public SubscriptionUpdate readObject(Reader r) throws IOException {
// Initialise the consumer // Initialise the consumer
Consumer counting = new CountingConsumer(SubscriptionUpdate.MAX_SIZE); Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data // Read the data
r.addConsumer(counting); r.addConsumer(counting);
r.readUserDefinedTag(Tags.SUBSCRIPTION_UPDATE); r.readUserDefinedTag(Tags.SUBSCRIPTION_UPDATE);

View File

@@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.TransportUpdate; import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
@@ -23,12 +24,13 @@ class TransportReader implements ObjectReader<TransportUpdate> {
public TransportUpdate readObject(Reader r) throws IOException { public TransportUpdate readObject(Reader r) throws IOException {
// Initialise the consumer // Initialise the consumer
Consumer counting = new CountingConsumer(TransportUpdate.MAX_SIZE); Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data // Read the data
r.addConsumer(counting); r.addConsumer(counting);
r.readUserDefinedTag(Tags.TRANSPORT_UPDATE); r.readUserDefinedTag(Tags.TRANSPORT_UPDATE);
r.addObjectReader(Tags.TRANSPORT_PROPERTIES, propertiesReader); r.addObjectReader(Tags.TRANSPORT_PROPERTIES, propertiesReader);
r.setMaxStringLength(TransportUpdate.MAX_SIZE); r.setMaxStringLength(ProtocolConstants.MAX_PACKET_LENGTH);
List<TransportProperties> l = r.readList(TransportProperties.class); List<TransportProperties> l = r.readList(TransportProperties.class);
r.resetMaxStringLength(); r.resetMaxStringLength();
r.removeObjectReader(Tags.TRANSPORT_PROPERTIES); r.removeObjectReader(Tags.TRANSPORT_PROPERTIES);

View File

@@ -3,8 +3,8 @@ package net.sf.briar.protocol.writers;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.AckWriter; import net.sf.briar.api.protocol.writers.AckWriter;
import net.sf.briar.api.serial.Writer; import net.sf.briar.api.serial.Writer;
@@ -28,8 +28,11 @@ class AckWriterImpl implements AckWriter {
w.writeListStart(); w.writeListStart();
started = true; started = true;
} }
int capacity = Ack.MAX_SIZE - (int) w.getBytesWritten() - 1; int capacity = ProtocolConstants.MAX_PACKET_LENGTH
if(capacity < BatchId.SERIALISED_LENGTH) return false; - (int) w.getBytesWritten() - 1;
// Allow one byte for the BATCH_ID tag, one byte for the BYTES tag and
// one byte for the length as a uint7
if(capacity < BatchId.LENGTH + 3) return false;
b.writeTo(w); b.writeTo(w);
return true; return true;
} }

View File

@@ -5,8 +5,8 @@ import java.io.OutputStream;
import java.security.DigestOutputStream; import java.security.DigestOutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.BatchWriter; import net.sf.briar.api.protocol.writers.BatchWriter;
import net.sf.briar.api.serial.Writer; import net.sf.briar.api.serial.Writer;
@@ -28,7 +28,9 @@ class BatchWriterImpl implements BatchWriter {
} }
public int getCapacity() { public int getCapacity() {
return Batch.MAX_SIZE - 3; // Allow one byte for the batch tag, one for the list start tag and
// one for the list end tag
return ProtocolConstants.MAX_PACKET_LENGTH - 3;
} }
public boolean writeMessage(byte[] message) throws IOException { public boolean writeMessage(byte[] message) throws IOException {
@@ -38,7 +40,8 @@ class BatchWriterImpl implements BatchWriter {
w.writeListStart(); w.writeListStart();
started = true; started = true;
} }
int capacity = Batch.MAX_SIZE - (int) w.getBytesWritten() - 1; int capacity = ProtocolConstants.MAX_PACKET_LENGTH
- (int) w.getBytesWritten() - 1;
if(capacity < message.length) return false; if(capacity < message.length) return false;
// Bypass the writer and write each raw message directly // Bypass the writer and write each raw message directly
out.write(message); out.write(message);

View File

@@ -6,8 +6,8 @@ import java.security.DigestOutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import net.sf.briar.api.protocol.MessageId; import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.OfferId; import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.OfferWriter; import net.sf.briar.api.protocol.writers.OfferWriter;
import net.sf.briar.api.serial.Writer; import net.sf.briar.api.serial.Writer;
@@ -35,8 +35,11 @@ class OfferWriterImpl implements OfferWriter {
w.writeListStart(); w.writeListStart();
started = true; started = true;
} }
int capacity = Offer.MAX_SIZE - (int) w.getBytesWritten() - 1; int capacity = ProtocolConstants.MAX_PACKET_LENGTH
if(capacity < MessageId.SERIALISED_LENGTH) return false; - (int) w.getBytesWritten() - 1;
// Allow one byte for the MESSAGE_ID tag, one byte for the BYTES tag
// and one byte for the length as a uint7
if(capacity < MessageId.LENGTH + 3) return false;
m.writeTo(w); m.writeTo(w);
return true; return true;
} }

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -92,7 +94,7 @@ DatabaseListener {
public synchronized ContactId acceptConnection(byte[] tag) public synchronized ContactId acceptConnection(byte[] tag)
throws DbException { throws DbException {
if(tag.length != Constants.TAG_BYTES) if(tag.length != TAG_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(!initialised) initialise(); if(!initialised) initialise();
Bytes b = new Bytes(tag); Bytes b = new Bytes(tag);

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@@ -31,7 +33,7 @@ class ConnectionWindowImpl implements ConnectionWindow {
private int getOffset(long connection) { private int getOffset(long connection) {
if(connection < 0L) throw new IllegalArgumentException(); if(connection < 0L) throw new IllegalArgumentException();
if(connection > Constants.MAX_32_BIT_UNSIGNED) if(connection > MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
int offset = (int) (connection - centre) + 16; int offset = (int) (connection - centre) + 16;
if(offset < 0 || offset > 31) throw new IllegalArgumentException(); if(offset < 0 || offset > 31) throw new IllegalArgumentException();
@@ -56,11 +58,12 @@ class ConnectionWindowImpl implements ConnectionWindow {
int mask = 0x80000000 >>> i; int mask = 0x80000000 >>> i;
if((bitmap & mask) == 0) { if((bitmap & mask) == 0) {
long c = centre - 16 + i; long c = centre - 16 + i;
if(c >= 0L && c <= Constants.MAX_32_BIT_UNSIGNED) unseen.add(c); if(c >= 0L && c <= MAX_32_BIT_UNSIGNED) unseen.add(c);
} }
} }
assert unseen.contains(centre) // The centre of the window should be an unseen value unless the
|| centre == Constants.MAX_32_BIT_UNSIGNED + 1; // maximum possible value has been seen
assert unseen.contains(centre) || centre == MAX_32_BIT_UNSIGNED + 1;
return unseen; return unseen;
} }
} }

View File

@@ -1,8 +0,0 @@
package net.sf.briar.transport;
interface Constants {
static final int TAG_BYTES = 16;
static final int MAX_16_BIT_UNSIGNED = 65535; // 2^16 - 1
static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
}

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.EOFException; import java.io.EOFException;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
@@ -21,16 +23,16 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
private final SecretKey packetKey; private final SecretKey packetKey;
private byte[] cipherBuf, plainBuf; private byte[] cipherBuf, plainBuf;
private int bufOff = 0, bufLen = Constants.TAG_BYTES; private int bufOff = 0, bufLen = TAG_LENGTH;
private boolean betweenPackets = true; private boolean betweenPackets = true;
PacketDecrypterImpl(byte[] firstTag, InputStream in, Cipher tagCipher, PacketDecrypterImpl(byte[] firstTag, InputStream in, Cipher tagCipher,
Cipher packetCipher, SecretKey tagKey, SecretKey packetKey) { Cipher packetCipher, SecretKey tagKey, SecretKey packetKey) {
super(in); super(in);
if(firstTag.length != Constants.TAG_BYTES) if(firstTag.length != TAG_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
cipherBuf = Arrays.copyOf(firstTag, firstTag.length); cipherBuf = Arrays.copyOf(firstTag, firstTag.length);
plainBuf = new byte[Constants.TAG_BYTES]; plainBuf = new byte[TAG_LENGTH];
this.tagCipher = tagCipher; this.tagCipher = tagCipher;
this.packetCipher = packetCipher; this.packetCipher = packetCipher;
this.packetKey = packetKey; this.packetKey = packetKey;
@@ -39,7 +41,7 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
} catch(InvalidKeyException e) { } catch(InvalidKeyException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
if(tagCipher.getOutputSize(Constants.TAG_BYTES) != Constants.TAG_BYTES) if(tagCipher.getOutputSize(TAG_LENGTH) != TAG_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@@ -48,7 +50,7 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
} }
public byte[] readTag() throws IOException { public byte[] readTag() throws IOException {
byte[] tag = new byte[Constants.TAG_BYTES]; byte[] tag = new byte[TAG_LENGTH];
System.arraycopy(cipherBuf, bufOff, tag, 0, bufLen); System.arraycopy(cipherBuf, bufOff, tag, 0, bufLen);
int offset = bufLen; int offset = bufLen;
bufOff = bufLen = 0; bufOff = bufLen = 0;

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.FilterOutputStream; import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@@ -29,7 +31,7 @@ implements PacketEncrypter {
} catch(InvalidKeyException e) { } catch(InvalidKeyException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
if(tagCipher.getOutputSize(Constants.TAG_BYTES) != Constants.TAG_BYTES) if(tagCipher.getOutputSize(TAG_LENGTH) != TAG_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@@ -38,8 +40,7 @@ implements PacketEncrypter {
} }
public void writeTag(byte[] tag) throws IOException { public void writeTag(byte[] tag) throws IOException {
if(tag.length != Constants.TAG_BYTES) if(tag.length != TAG_LENGTH) throw new IllegalArgumentException();
throw new IllegalArgumentException();
IvParameterSpec iv = new IvParameterSpec(tag); IvParameterSpec iv = new IvParameterSpec(tag);
try { try {
out.write(tagCipher.doFinal(tag)); out.write(tagCipher.doFinal(tag));

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -78,8 +80,7 @@ class PacketReaderImpl extends FilterInputStream implements PacketReader {
private void readTag() throws IOException { private void readTag() throws IOException {
assert betweenPackets; assert betweenPackets;
if(packet > Constants.MAX_32_BIT_UNSIGNED) if(packet > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
throw new IllegalStateException();
byte[] tag = decrypter.readTag(); byte[] tag = decrypter.readTag();
if(tag == null) return; // EOF if(tag == null) return; // EOF
if(!TagDecoder.decodeTag(tag, transportId, connection, packet)) if(!TagDecoder.decodeTag(tag, transportId, connection, packet))

View File

@@ -1,5 +1,8 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_16_BIT_UNSIGNED;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.io.FilterOutputStream; import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@@ -24,11 +27,11 @@ class PacketWriterImpl extends FilterOutputStream implements PacketWriter {
this.encrypter = encrypter; this.encrypter = encrypter;
this.mac = mac; this.mac = mac;
if(transportId < 0) throw new IllegalArgumentException(); if(transportId < 0) throw new IllegalArgumentException();
if(transportId > Constants.MAX_16_BIT_UNSIGNED) if(transportId > MAX_16_BIT_UNSIGNED)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.transportId = transportId; this.transportId = transportId;
if(connection < 0L) throw new IllegalArgumentException(); if(connection < 0L) throw new IllegalArgumentException();
if(connection > Constants.MAX_32_BIT_UNSIGNED) if(connection > MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.connection = connection; this.connection = connection;
} }
@@ -68,8 +71,7 @@ class PacketWriterImpl extends FilterOutputStream implements PacketWriter {
private void writeTag() throws IOException { private void writeTag() throws IOException {
assert betweenPackets; assert betweenPackets;
if(packet > Constants.MAX_32_BIT_UNSIGNED) if(packet > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
throw new IllegalStateException();
byte[] tag = TagEncoder.encodeTag(transportId, connection, byte[] tag = TagEncoder.encodeTag(transportId, connection,
packet); packet);
// Write the tag to the encrypter and start calculating the MAC // Write the tag to the encrypter and start calculating the MAC

View File

@@ -1,10 +1,12 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
class TagDecoder { class TagDecoder {
static boolean decodeTag(byte[] tag, int transportId, long connection, static boolean decodeTag(byte[] tag, int transportId, long connection,
long packet) { long packet) {
if(tag.length != Constants.TAG_BYTES) return false; if(tag.length != TAG_LENGTH) return false;
// First 16 bits must be zero // First 16 bits must be zero
if(readUint16(tag, 0) != 0) return false; if(readUint16(tag, 0) != 0) return false;
// Transport identifier is encoded as an unsigned 16-bit integer // Transport identifier is encoded as an unsigned 16-bit integer

View File

@@ -1,10 +1,14 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_16_BIT_UNSIGNED;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
class TagEncoder { class TagEncoder {
static byte[] encodeTag(int transportId, long connection, static byte[] encodeTag(int transportId, long connection,
long packet) { long packet) {
byte[] tag = new byte[Constants.TAG_BYTES]; byte[] tag = new byte[TAG_LENGTH];
// Encode the transport identifier as an unsigned 16-bit integer // Encode the transport identifier as an unsigned 16-bit integer
writeUint16(transportId, tag, 2); writeUint16(transportId, tag, 2);
// Encode the connection number as an unsigned 32-bit integer // Encode the connection number as an unsigned 32-bit integer
@@ -17,7 +21,7 @@ class TagEncoder {
// Package access for testing // Package access for testing
static void writeUint16(int i, byte[] b, int offset) { static void writeUint16(int i, byte[] b, int offset) {
assert i >= 0; assert i >= 0;
assert i <= Constants.MAX_16_BIT_UNSIGNED; assert i <= MAX_16_BIT_UNSIGNED;
assert b.length >= offset + 2; assert b.length >= offset + 2;
b[offset] = (byte) (i >> 8); b[offset] = (byte) (i >> 8);
b[offset + 1] = (byte) (i & 0xFF); b[offset + 1] = (byte) (i & 0xFF);
@@ -26,7 +30,7 @@ class TagEncoder {
// Package access for testing // Package access for testing
static void writeUint32(long i, byte[] b, int offset) { static void writeUint32(long i, byte[] b, int offset) {
assert i >= 0L; assert i >= 0L;
assert i <= Constants.MAX_32_BIT_UNSIGNED; assert i <= MAX_32_BIT_UNSIGNED;
assert b.length >= offset + 4; assert b.length >= offset + 4;
b[offset] = (byte) (i >> 24); b[offset] = (byte) (i >> 24);
b[offset + 1] = (byte) (i >> 16 & 0xFF); b[offset + 1] = (byte) (i >> 16 & 0xFF);

View File

@@ -9,6 +9,7 @@ import java.util.Random;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.api.protocol.Ack; import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.UniqueId; import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.serial.FormatException; import net.sf.briar.api.serial.FormatException;
@@ -103,7 +104,8 @@ public class AckReaderTest extends TestCase {
w.writeListStart(); w.writeListStart();
byte[] b = new byte[UniqueId.LENGTH]; byte[] b = new byte[UniqueId.LENGTH];
Random random = new Random(); Random random = new Random();
while(out.size() < Ack.MAX_SIZE - BatchId.SERIALISED_LENGTH) { while(out.size() + BatchId.LENGTH + 3
< ProtocolConstants.MAX_PACKET_LENGTH) {
w.writeUserDefinedTag(Tags.BATCH_ID); w.writeUserDefinedTag(Tags.BATCH_ID);
random.nextBytes(b); random.nextBytes(b);
w.writeBytes(b); w.writeBytes(b);
@@ -114,7 +116,7 @@ public class AckReaderTest extends TestCase {
w.writeBytes(b); w.writeBytes(b);
} }
w.writeListEnd(); w.writeListEnd();
assertEquals(tooBig, out.size() > Ack.MAX_SIZE); assertEquals(tooBig, out.size() > ProtocolConstants.MAX_PACKET_LENGTH);
return out.toByteArray(); return out.toByteArray();
} }

View File

@@ -11,6 +11,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Batch; import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Message; import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.FormatException; import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.ObjectReader; import net.sf.briar.api.serial.ObjectReader;
@@ -54,7 +55,7 @@ public class BatchReaderTest extends TestCase {
BatchReader batchReader = new BatchReader(crypto, messageReader, BatchReader batchReader = new BatchReader(crypto, messageReader,
batchFactory); batchFactory);
byte[] b = createBatch(Batch.MAX_SIZE + 1); byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH + 1);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in); Reader reader = readerFactory.createReader(in);
reader.addObjectReader(Tags.BATCH, batchReader); reader.addObjectReader(Tags.BATCH, batchReader);
@@ -79,7 +80,7 @@ public class BatchReaderTest extends TestCase {
will(returnValue(batch)); will(returnValue(batch));
}}); }});
byte[] b = createBatch(Batch.MAX_SIZE); byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in); Reader reader = readerFactory.createReader(in);
reader.addObjectReader(Tags.BATCH, batchReader); reader.addObjectReader(Tags.BATCH, batchReader);
@@ -90,7 +91,7 @@ public class BatchReaderTest extends TestCase {
@Test @Test
public void testBatchId() throws Exception { public void testBatchId() throws Exception {
byte[] b = createBatch(Batch.MAX_SIZE); byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
// Calculate the expected batch ID // Calculate the expected batch ID
MessageDigest messageDigest = crypto.getMessageDigest(); MessageDigest messageDigest = crypto.getMessageDigest();
messageDigest.reset(); messageDigest.reset();

View File

@@ -6,6 +6,7 @@ import java.util.BitSet;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.api.protocol.OfferId; import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Request; import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.Tags; import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.UniqueId; import net.sf.briar.api.protocol.UniqueId;
@@ -127,9 +128,10 @@ public class RequestReaderTest extends TestCase {
// UniqueID.LENGTH bytes for the offer ID, one byte for the BYTES tag, // UniqueID.LENGTH bytes for the offer ID, one byte for the BYTES tag,
// and five bytes for the length as an int32 // and five bytes for the length as an int32
int overhead = UniqueId.LENGTH + 10; int overhead = UniqueId.LENGTH + 10;
if(tooBig) w.writeBytes(new byte[Request.MAX_SIZE - overhead + 1]); int size = ProtocolConstants.MAX_PACKET_LENGTH - overhead;
else w.writeBytes(new byte[Request.MAX_SIZE - overhead]); if(tooBig) size++;
assertEquals(tooBig, out.size() > Request.MAX_SIZE); w.writeBytes(new byte[size]);
assertEquals(tooBig, out.size() > ProtocolConstants.MAX_PACKET_LENGTH);
return out.toByteArray(); return out.toByteArray();
} }

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@@ -53,7 +55,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
}}); }});
final ConnectionRecogniserImpl c = final ConnectionRecogniserImpl c =
new ConnectionRecogniserImpl(transportId, crypto, db); new ConnectionRecogniserImpl(transportId, crypto, db);
assertNull(c.acceptConnection(new byte[Constants.TAG_BYTES])); assertNull(c.acceptConnection(new byte[TAG_LENGTH]));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.Arrays; import java.util.Arrays;
@@ -33,25 +35,25 @@ public class PacketDecrypterImplTest extends TestCase {
@Test @Test
public void testSingleBytePackets() throws Exception { public void testSingleBytePackets() throws Exception {
byte[] ciphertext = new byte[(Constants.TAG_BYTES + 1) * 2]; byte[] ciphertext = new byte[(TAG_LENGTH + 1) * 2];
ByteArrayInputStream in = new ByteArrayInputStream(ciphertext); ByteArrayInputStream in = new ByteArrayInputStream(ciphertext);
byte[] firstTag = new byte[Constants.TAG_BYTES]; byte[] firstTag = new byte[TAG_LENGTH];
assertEquals(Constants.TAG_BYTES, in.read(firstTag)); assertEquals(TAG_LENGTH, in.read(firstTag));
PacketDecrypter p = new PacketDecrypterImpl(firstTag, in, tagCipher, PacketDecrypter p = new PacketDecrypterImpl(firstTag, in, tagCipher,
packetCipher, tagKey, packetKey); packetCipher, tagKey, packetKey);
byte[] decryptedTag = p.readTag(); byte[] decryptedTag = p.readTag();
assertEquals(Constants.TAG_BYTES, decryptedTag.length); assertEquals(TAG_LENGTH, decryptedTag.length);
assertTrue(p.getInputStream().read() > -1); assertTrue(p.getInputStream().read() > -1);
byte[] decryptedTag1 = p.readTag(); byte[] decryptedTag1 = p.readTag();
assertEquals(Constants.TAG_BYTES, decryptedTag1.length); assertEquals(TAG_LENGTH, decryptedTag1.length);
assertTrue(p.getInputStream().read() > -1); assertTrue(p.getInputStream().read() > -1);
} }
@Test @Test
public void testDecryption() throws Exception { public void testDecryption() throws Exception {
byte[] tag = new byte[Constants.TAG_BYTES]; byte[] tag = new byte[TAG_LENGTH];
byte[] packet = new byte[123]; byte[] packet = new byte[123];
byte[] tag1 = new byte[Constants.TAG_BYTES]; byte[] tag1 = new byte[TAG_LENGTH];
byte[] packet1 = new byte[234]; byte[] packet1 = new byte[234];
// Calculate the first expected decrypted tag // Calculate the first expected decrypted tag
tagCipher.init(Cipher.DECRYPT_MODE, tagKey); tagCipher.init(Cipher.DECRYPT_MODE, tagKey);

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays; import java.util.Arrays;
@@ -36,15 +38,15 @@ public class PacketEncrypterImplTest extends TestCase {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
PacketEncrypter p = new PacketEncrypterImpl(out, tagCipher, PacketEncrypter p = new PacketEncrypterImpl(out, tagCipher,
packetCipher, tagKey, packetKey); packetCipher, tagKey, packetKey);
p.writeTag(new byte[Constants.TAG_BYTES]); p.writeTag(new byte[TAG_LENGTH]);
p.getOutputStream().write((byte) 0); p.getOutputStream().write((byte) 0);
p.finishPacket(); p.finishPacket();
assertEquals(Constants.TAG_BYTES + 1, out.toByteArray().length); assertEquals(TAG_LENGTH + 1, out.toByteArray().length);
} }
@Test @Test
public void testEncryption() throws Exception { public void testEncryption() throws Exception {
byte[] tag = new byte[Constants.TAG_BYTES]; byte[] tag = new byte[TAG_LENGTH];
byte[] packet = new byte[123]; byte[] packet = new byte[123];
// Calculate the expected encrypted tag // Calculate the expected encrypted tag
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey); tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
@@ -63,14 +65,14 @@ public class PacketEncrypterImplTest extends TestCase {
p.getOutputStream().write(packet); p.getOutputStream().write(packet);
p.finishPacket(); p.finishPacket();
byte[] ciphertext = out.toByteArray(); byte[] ciphertext = out.toByteArray();
assertEquals(Constants.TAG_BYTES + packet.length, ciphertext.length); assertEquals(TAG_LENGTH + packet.length, ciphertext.length);
// Check the tag // Check the tag
byte[] actualTag = new byte[Constants.TAG_BYTES]; byte[] actualTag = new byte[TAG_LENGTH];
System.arraycopy(ciphertext, 0, actualTag, 0, Constants.TAG_BYTES); System.arraycopy(ciphertext, 0, actualTag, 0, TAG_LENGTH);
assertTrue(Arrays.equals(expectedTag, actualTag)); assertTrue(Arrays.equals(expectedTag, actualTag));
// Check the packet // Check the packet
byte[] actualPacket = new byte[packet.length]; byte[] actualPacket = new byte[packet.length];
System.arraycopy(ciphertext, Constants.TAG_BYTES, actualPacket, 0, System.arraycopy(ciphertext, TAG_LENGTH, actualPacket, 0,
actualPacket.length); actualPacket.length);
assertTrue(Arrays.equals(expectedPacket, actualPacket)); assertTrue(Arrays.equals(expectedPacket, actualPacket));
} }

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.InputStream; import java.io.InputStream;
@@ -68,8 +70,8 @@ public class PacketReadWriteTest extends TestCase {
writer.finishPacket(); writer.finishPacket();
// Read the packets back // Read the packets back
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
byte[] firstTag = new byte[Constants.TAG_BYTES]; byte[] firstTag = new byte[TAG_LENGTH];
assertEquals(Constants.TAG_BYTES, in.read(firstTag)); assertEquals(TAG_LENGTH, in.read(firstTag));
PacketDecrypter decrypter = new PacketDecrypterImpl(firstTag, in, PacketDecrypter decrypter = new PacketDecrypterImpl(firstTag, in,
tagCipher, packetCipher, tagKey, packetKey); tagCipher, packetCipher, tagKey, packetKey);
PacketReader reader = new PacketReaderImpl(decrypter, mac, transportId, PacketReader reader = new PacketReaderImpl(decrypter, mac, transportId,

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
@@ -35,7 +37,7 @@ public class PacketReaderImplTest extends TestCase {
@Test @Test
public void testFirstReadTriggersTag() throws Exception { public void testFirstReadTriggersTag() throws Exception {
// TAG_BYTES for the tag, 1 byte for the packet // TAG_BYTES for the tag, 1 byte for the packet
byte[] b = new byte[Constants.TAG_BYTES + 1]; byte[] b = new byte[TAG_LENGTH + 1];
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
PacketDecrypter d = new NullPacketDecrypter(in); PacketDecrypter d = new NullPacketDecrypter(in);
PacketReader p = new PacketReaderImpl(d, mac, 0, 0L); PacketReader p = new PacketReaderImpl(d, mac, 0, 0L);
@@ -47,7 +49,7 @@ public class PacketReaderImplTest extends TestCase {
@Test @Test
public void testFinishPacketAfterReadTriggersMac() throws Exception { public void testFinishPacketAfterReadTriggersMac() throws Exception {
// TAG_BYTES for the tag, 1 byte for the packet // TAG_BYTES for the tag, 1 byte for the packet
byte[] b = new byte[Constants.TAG_BYTES + 1]; byte[] b = new byte[TAG_LENGTH + 1];
// Calculate the MAC and append it to the packet // Calculate the MAC and append it to the packet
mac.update(b); mac.update(b);
byte[] macBytes = mac.doFinal(); byte[] macBytes = mac.doFinal();
@@ -66,14 +68,14 @@ public class PacketReaderImplTest extends TestCase {
@Test @Test
public void testModifyingPacketInvalidatesMac() throws Exception { public void testModifyingPacketInvalidatesMac() throws Exception {
// TAG_BYTES for the tag, 1 byte for the packet // TAG_BYTES for the tag, 1 byte for the packet
byte[] b = new byte[Constants.TAG_BYTES + 1]; byte[] b = new byte[TAG_LENGTH + 1];
// Calculate the MAC and append it to the packet // Calculate the MAC and append it to the packet
mac.update(b); mac.update(b);
byte[] macBytes = mac.doFinal(); byte[] macBytes = mac.doFinal();
byte[] b1 = Arrays.copyOf(b, b.length + macBytes.length); byte[] b1 = Arrays.copyOf(b, b.length + macBytes.length);
System.arraycopy(macBytes, 0, b1, b.length, macBytes.length); System.arraycopy(macBytes, 0, b1, b.length, macBytes.length);
// Modify the packet // Modify the packet
b1[Constants.TAG_BYTES] = (byte) 1; b1[TAG_LENGTH] = (byte) 1;
// Check that the PacketReader reads and fails to verify the MAC // Check that the PacketReader reads and fails to verify the MAC
ByteArrayInputStream in = new ByteArrayInputStream(b1); ByteArrayInputStream in = new ByteArrayInputStream(b1);
PacketDecrypter d = new NullPacketDecrypter(in); PacketDecrypter d = new NullPacketDecrypter(in);
@@ -88,7 +90,7 @@ public class PacketReaderImplTest extends TestCase {
@Test @Test
public void testExtraCallsToFinishPacketDoNothing() throws Exception { public void testExtraCallsToFinishPacketDoNothing() throws Exception {
// TAG_BYTES for the tag, 1 byte for the packet // TAG_BYTES for the tag, 1 byte for the packet
byte[] b = new byte[Constants.TAG_BYTES + 1]; byte[] b = new byte[TAG_LENGTH + 1];
// Calculate the MAC and append it to the packet // Calculate the MAC and append it to the packet
mac.update(b); mac.update(b);
byte[] macBytes = mac.doFinal(); byte[] macBytes = mac.doFinal();
@@ -121,7 +123,7 @@ public class PacketReaderImplTest extends TestCase {
+ "00000000" // 32 bits for the packet number + "00000000" // 32 bits for the packet number
+ "00000000" // 32 bits for the block number + "00000000" // 32 bits for the block number
); );
assertEquals(Constants.TAG_BYTES, tag.length); assertEquals(TAG_LENGTH, tag.length);
byte[] tag1 = StringUtils.fromHexString( byte[] tag1 = StringUtils.fromHexString(
"0000" // 16 bits reserved "0000" // 16 bits reserved
+ "F00D" // 16 bits for the transport ID + "F00D" // 16 bits for the transport ID
@@ -129,7 +131,7 @@ public class PacketReaderImplTest extends TestCase {
+ "00000001" // 32 bits for the packet number + "00000001" // 32 bits for the packet number
+ "00000000" // 32 bits for the block number + "00000000" // 32 bits for the block number
); );
assertEquals(Constants.TAG_BYTES, tag1.length); assertEquals(TAG_LENGTH, tag1.length);
// Calculate the MAC on the first packet and append it to the packet // Calculate the MAC on the first packet and append it to the packet
mac.update(tag); mac.update(tag);
mac.update((byte) 0); mac.update((byte) 0);
@@ -172,7 +174,7 @@ public class PacketReaderImplTest extends TestCase {
} }
public byte[] readTag() throws IOException { public byte[] readTag() throws IOException {
byte[] tag = new byte[Constants.TAG_BYTES]; byte[] tag = new byte[TAG_LENGTH];
int offset = 0; int offset = 0;
while(offset < tag.length) { while(offset < tag.length) {
int read = in.read(tag, offset, tag.length - offset); int read = in.read(tag, offset, tag.length - offset);

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@@ -37,14 +39,14 @@ public class PacketWriterImplTest extends TestCase {
PacketWriter p = new PacketWriterImpl(e, mac, 0, 0L); PacketWriter p = new PacketWriterImpl(e, mac, 0, 0L);
p.getOutputStream().write(0); p.getOutputStream().write(0);
// There should be TAG_BYTES bytes for the tag, 1 byte for the packet // There should be TAG_BYTES bytes for the tag, 1 byte for the packet
assertTrue(Arrays.equals(new byte[Constants.TAG_BYTES + 1], assertTrue(Arrays.equals(new byte[TAG_LENGTH + 1],
out.toByteArray())); out.toByteArray()));
} }
@Test @Test
public void testFinishPacketAfterWriteTriggersMac() throws Exception { public void testFinishPacketAfterWriteTriggersMac() throws Exception {
// Calculate what the MAC should be // Calculate what the MAC should be
mac.update(new byte[Constants.TAG_BYTES + 1]); mac.update(new byte[TAG_LENGTH + 1]);
byte[] expectedMac = mac.doFinal(); byte[] expectedMac = mac.doFinal();
// Check that the PacketWriter calculates and writes the correct MAC // Check that the PacketWriter calculates and writes the correct MAC
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -53,10 +55,10 @@ public class PacketWriterImplTest extends TestCase {
p.getOutputStream().write(0); p.getOutputStream().write(0);
p.finishPacket(); p.finishPacket();
byte[] written = out.toByteArray(); byte[] written = out.toByteArray();
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length, assertEquals(TAG_LENGTH + 1 + expectedMac.length,
written.length); written.length);
byte[] actualMac = new byte[expectedMac.length]; byte[] actualMac = new byte[expectedMac.length];
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0, System.arraycopy(written, TAG_LENGTH + 1, actualMac, 0,
actualMac.length); actualMac.length);
assertTrue(Arrays.equals(expectedMac, actualMac)); assertTrue(Arrays.equals(expectedMac, actualMac));
} }
@@ -64,7 +66,7 @@ public class PacketWriterImplTest extends TestCase {
@Test @Test
public void testExtraCallsToFinishPacketDoNothing() throws Exception { public void testExtraCallsToFinishPacketDoNothing() throws Exception {
// Calculate what the MAC should be // Calculate what the MAC should be
mac.update(new byte[Constants.TAG_BYTES + 1]); mac.update(new byte[TAG_LENGTH + 1]);
byte[] expectedMac = mac.doFinal(); byte[] expectedMac = mac.doFinal();
// Check that the PacketWriter calculates and writes the correct MAC // Check that the PacketWriter calculates and writes the correct MAC
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -81,10 +83,10 @@ public class PacketWriterImplTest extends TestCase {
p.finishPacket(); p.finishPacket();
p.finishPacket(); p.finishPacket();
byte[] written = out.toByteArray(); byte[] written = out.toByteArray();
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length, assertEquals(TAG_LENGTH + 1 + expectedMac.length,
written.length); written.length);
byte[] actualMac = new byte[expectedMac.length]; byte[] actualMac = new byte[expectedMac.length];
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0, System.arraycopy(written, TAG_LENGTH + 1, actualMac, 0,
actualMac.length); actualMac.length);
assertTrue(Arrays.equals(expectedMac, actualMac)); assertTrue(Arrays.equals(expectedMac, actualMac));
} }
@@ -98,7 +100,7 @@ public class PacketWriterImplTest extends TestCase {
+ "00000000" // 32 bits for the packet number + "00000000" // 32 bits for the packet number
+ "00000000" // 32 bits for the block number + "00000000" // 32 bits for the block number
); );
assertEquals(Constants.TAG_BYTES, expectedTag.length); assertEquals(TAG_LENGTH, expectedTag.length);
byte[] expectedTag1 = StringUtils.fromHexString( byte[] expectedTag1 = StringUtils.fromHexString(
"0000" // 16 bits reserved "0000" // 16 bits reserved
+ "F00D" // 16 bits for the transport ID + "F00D" // 16 bits for the transport ID
@@ -106,7 +108,7 @@ public class PacketWriterImplTest extends TestCase {
+ "00000001" // 32 bits for the packet number + "00000001" // 32 bits for the packet number
+ "00000000" // 32 bits for the block number + "00000000" // 32 bits for the block number
); );
assertEquals(Constants.TAG_BYTES, expectedTag1.length); assertEquals(TAG_LENGTH, expectedTag1.length);
// Calculate what the MAC on the first packet should be // Calculate what the MAC on the first packet should be
mac.update(expectedTag); mac.update(expectedTag);
mac.update((byte) 0); mac.update((byte) 0);
@@ -126,27 +128,27 @@ public class PacketWriterImplTest extends TestCase {
p.getOutputStream().write(0); p.getOutputStream().write(0);
p.finishPacket(); p.finishPacket();
byte[] written = out.toByteArray(); byte[] written = out.toByteArray();
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length assertEquals(TAG_LENGTH + 1 + expectedMac.length
+ Constants.TAG_BYTES + 1 + expectedMac1.length, + TAG_LENGTH + 1 + expectedMac1.length,
written.length); written.length);
// Check the first packet's tag // Check the first packet's tag
byte[] actualTag = new byte[Constants.TAG_BYTES]; byte[] actualTag = new byte[TAG_LENGTH];
System.arraycopy(written, 0, actualTag, 0, Constants.TAG_BYTES); System.arraycopy(written, 0, actualTag, 0, TAG_LENGTH);
assertTrue(Arrays.equals(expectedTag, actualTag)); assertTrue(Arrays.equals(expectedTag, actualTag));
// Check the first packet's MAC // Check the first packet's MAC
byte[] actualMac = new byte[expectedMac.length]; byte[] actualMac = new byte[expectedMac.length];
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0, System.arraycopy(written, TAG_LENGTH + 1, actualMac, 0,
actualMac.length); actualMac.length);
assertTrue(Arrays.equals(expectedMac, actualMac)); assertTrue(Arrays.equals(expectedMac, actualMac));
// Check the second packet's tag // Check the second packet's tag
byte[] actualTag1 = new byte[Constants.TAG_BYTES]; byte[] actualTag1 = new byte[TAG_LENGTH];
System.arraycopy(written, Constants.TAG_BYTES + 1 + expectedMac.length, System.arraycopy(written, TAG_LENGTH + 1 + expectedMac.length,
actualTag1, 0, Constants.TAG_BYTES); actualTag1, 0, TAG_LENGTH);
assertTrue(Arrays.equals(expectedTag1, actualTag1)); assertTrue(Arrays.equals(expectedTag1, actualTag1));
// Check the second packet's MAC // Check the second packet's MAC
byte[] actualMac1 = new byte[expectedMac1.length]; byte[] actualMac1 = new byte[expectedMac1.length];
System.arraycopy(written, Constants.TAG_BYTES + 1 + expectedMac.length System.arraycopy(written, TAG_LENGTH + 1 + expectedMac.length
+ Constants.TAG_BYTES + 1, actualMac1, 0, actualMac1.length); + TAG_LENGTH + 1, actualMac1, 0, actualMac1.length);
assertTrue(Arrays.equals(expectedMac1, actualMac1)); assertTrue(Arrays.equals(expectedMac1, actualMac1));
} }