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. */
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. */
Collection<BatchId> getBatchIds();
}

View File

@@ -5,12 +5,15 @@ import net.sf.briar.api.serial.Writable;
/** A pseudonymous author of messages. */
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;
/** 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;
/** 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. */
AuthorId getId();

View File

@@ -5,12 +5,6 @@ import java.util.Collection;
/** A packet containing messages. */
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. */
BatchId getId();

View File

@@ -5,12 +5,15 @@ import net.sf.briar.api.serial.Writable;
/** A group to which users may subscribe. */
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;
/** 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;
/** 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. */
GroupId getId();

View File

@@ -2,12 +2,23 @@ package net.sf.briar.api.protocol;
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;
/**
* 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. */
MessageId getId();

View File

@@ -5,12 +5,6 @@ import java.util.Collection;
/** A packet offering the recipient some messages. */
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. */
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. */
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
* responds.

View File

@@ -5,12 +5,6 @@ import java.util.Map;
/** A packet updating the sender's subscriptions. */
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. */
Map<Group, Long> getSubscriptions();

View File

@@ -5,12 +5,6 @@ import java.util.Map;
/** A packet updating the sender's transport properties. */
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. */
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 {
/** The length of a unique identifier in bytes. */
public static final int LENGTH = 32;
public static final int SERIALISED_LENGTH = LENGTH + 3;
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.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
@@ -22,7 +23,8 @@ class AckReader implements ObjectReader<Ack> {
public Ack readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(Ack.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read and digest the data
r.addConsumer(counting);
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.BatchId;
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.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
@@ -28,7 +29,8 @@ class BatchReader implements ObjectReader<Batch> {
public Batch readObject(Reader r) throws IOException {
// Initialise the consumers
Consumer counting = new CountingConsumer(Batch.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset();
// Read and digest the data

View File

@@ -41,7 +41,7 @@ class MessageReader implements ObjectReader<Message> {
public Message readObject(Reader r) throws IOException {
CopyingConsumer copying = new CopyingConsumer();
CountingConsumer counting = new CountingConsumer(Message.MAX_SIZE);
CountingConsumer counting = new CountingConsumer(Message.MAX_LENGTH);
r.addConsumer(copying);
r.addConsumer(counting);
// Read the initial tag
@@ -64,7 +64,7 @@ class MessageReader implements ObjectReader<Message> {
long timestamp = r.readInt64();
if(timestamp < 0L) throw new FormatException();
// 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
int signedByAuthor = (int) counting.getCount();
// 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.Offer;
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.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
@@ -28,7 +29,8 @@ class OfferReader implements ObjectReader<Offer> {
public Offer readObject(Reader r) throws IOException {
// Initialise the consumers
Consumer counting = new CountingConsumer(Offer.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset();
// Read the data

View File

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

View File

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

View File

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

View File

@@ -3,8 +3,8 @@ package net.sf.briar.protocol.writers;
import java.io.IOException;
import java.io.OutputStream;
import net.sf.briar.api.protocol.Ack;
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.writers.AckWriter;
import net.sf.briar.api.serial.Writer;
@@ -28,8 +28,11 @@ class AckWriterImpl implements AckWriter {
w.writeListStart();
started = true;
}
int capacity = Ack.MAX_SIZE - (int) w.getBytesWritten() - 1;
if(capacity < BatchId.SERIALISED_LENGTH) return false;
int capacity = ProtocolConstants.MAX_PACKET_LENGTH
- (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);
return true;
}

View File

@@ -5,8 +5,8 @@ import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import net.sf.briar.api.protocol.Batch;
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.writers.BatchWriter;
import net.sf.briar.api.serial.Writer;
@@ -28,7 +28,9 @@ class BatchWriterImpl implements BatchWriter {
}
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 {
@@ -38,7 +40,8 @@ class BatchWriterImpl implements BatchWriter {
w.writeListStart();
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;
// Bypass the writer and write each raw message directly
out.write(message);

View File

@@ -6,8 +6,8 @@ import java.security.DigestOutputStream;
import java.security.MessageDigest;
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.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.OfferWriter;
import net.sf.briar.api.serial.Writer;
@@ -35,8 +35,11 @@ class OfferWriterImpl implements OfferWriter {
w.writeListStart();
started = true;
}
int capacity = Offer.MAX_SIZE - (int) w.getBytesWritten() - 1;
if(capacity < MessageId.SERIALISED_LENGTH) return false;
int capacity = ProtocolConstants.MAX_PACKET_LENGTH
- (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);
return true;
}

View File

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

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.util.ArrayList;
import java.util.Collection;
@@ -31,7 +33,7 @@ class ConnectionWindowImpl implements ConnectionWindow {
private int getOffset(long connection) {
if(connection < 0L) throw new IllegalArgumentException();
if(connection > Constants.MAX_32_BIT_UNSIGNED)
if(connection > MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException();
int offset = (int) (connection - centre) + 16;
if(offset < 0 || offset > 31) throw new IllegalArgumentException();
@@ -56,11 +58,12 @@ class ConnectionWindowImpl implements ConnectionWindow {
int mask = 0x80000000 >>> i;
if((bitmap & mask) == 0) {
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)
|| centre == Constants.MAX_32_BIT_UNSIGNED + 1;
// The centre of the window should be an unseen value unless the
// maximum possible value has been seen
assert unseen.contains(centre) || centre == MAX_32_BIT_UNSIGNED + 1;
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;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
@@ -21,16 +23,16 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
private final SecretKey packetKey;
private byte[] cipherBuf, plainBuf;
private int bufOff = 0, bufLen = Constants.TAG_BYTES;
private int bufOff = 0, bufLen = TAG_LENGTH;
private boolean betweenPackets = true;
PacketDecrypterImpl(byte[] firstTag, InputStream in, Cipher tagCipher,
Cipher packetCipher, SecretKey tagKey, SecretKey packetKey) {
super(in);
if(firstTag.length != Constants.TAG_BYTES)
if(firstTag.length != TAG_LENGTH)
throw new IllegalArgumentException();
cipherBuf = Arrays.copyOf(firstTag, firstTag.length);
plainBuf = new byte[Constants.TAG_BYTES];
plainBuf = new byte[TAG_LENGTH];
this.tagCipher = tagCipher;
this.packetCipher = packetCipher;
this.packetKey = packetKey;
@@ -39,7 +41,7 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
} catch(InvalidKeyException e) {
throw new IllegalArgumentException(e);
}
if(tagCipher.getOutputSize(Constants.TAG_BYTES) != Constants.TAG_BYTES)
if(tagCipher.getOutputSize(TAG_LENGTH) != TAG_LENGTH)
throw new IllegalArgumentException();
}
@@ -48,7 +50,7 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
}
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);
int offset = bufLen;
bufOff = bufLen = 0;

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,10 +1,14 @@
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 {
static byte[] encodeTag(int transportId, long connection,
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
writeUint16(transportId, tag, 2);
// Encode the connection number as an unsigned 32-bit integer
@@ -17,7 +21,7 @@ class TagEncoder {
// Package access for testing
static void writeUint16(int i, byte[] b, int offset) {
assert i >= 0;
assert i <= Constants.MAX_16_BIT_UNSIGNED;
assert i <= MAX_16_BIT_UNSIGNED;
assert b.length >= offset + 2;
b[offset] = (byte) (i >> 8);
b[offset + 1] = (byte) (i & 0xFF);
@@ -26,7 +30,7 @@ class TagEncoder {
// Package access for testing
static void writeUint32(long i, byte[] b, int offset) {
assert i >= 0L;
assert i <= Constants.MAX_32_BIT_UNSIGNED;
assert i <= MAX_32_BIT_UNSIGNED;
assert b.length >= offset + 4;
b[offset] = (byte) (i >> 24);
b[offset + 1] = (byte) (i >> 16 & 0xFF);

View File

@@ -9,6 +9,7 @@ import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.api.protocol.Ack;
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.UniqueId;
import net.sf.briar.api.serial.FormatException;
@@ -103,7 +104,8 @@ public class AckReaderTest extends TestCase {
w.writeListStart();
byte[] b = new byte[UniqueId.LENGTH];
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);
random.nextBytes(b);
w.writeBytes(b);
@@ -114,7 +116,7 @@ public class AckReaderTest extends TestCase {
w.writeBytes(b);
}
w.writeListEnd();
assertEquals(tooBig, out.size() > Ack.MAX_SIZE);
assertEquals(tooBig, out.size() > ProtocolConstants.MAX_PACKET_LENGTH);
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.BatchId;
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.serial.FormatException;
import net.sf.briar.api.serial.ObjectReader;
@@ -54,7 +55,7 @@ public class BatchReaderTest extends TestCase {
BatchReader batchReader = new BatchReader(crypto, messageReader,
batchFactory);
byte[] b = createBatch(Batch.MAX_SIZE + 1);
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH + 1);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addObjectReader(Tags.BATCH, batchReader);
@@ -79,7 +80,7 @@ public class BatchReaderTest extends TestCase {
will(returnValue(batch));
}});
byte[] b = createBatch(Batch.MAX_SIZE);
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addObjectReader(Tags.BATCH, batchReader);
@@ -90,7 +91,7 @@ public class BatchReaderTest extends TestCase {
@Test
public void testBatchId() throws Exception {
byte[] b = createBatch(Batch.MAX_SIZE);
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
// Calculate the expected batch ID
MessageDigest messageDigest = crypto.getMessageDigest();
messageDigest.reset();

View File

@@ -6,6 +6,7 @@ import java.util.BitSet;
import junit.framework.TestCase;
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.Tags;
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,
// and five bytes for the length as an int32
int overhead = UniqueId.LENGTH + 10;
if(tooBig) w.writeBytes(new byte[Request.MAX_SIZE - overhead + 1]);
else w.writeBytes(new byte[Request.MAX_SIZE - overhead]);
assertEquals(tooBig, out.size() > Request.MAX_SIZE);
int size = ProtocolConstants.MAX_PACKET_LENGTH - overhead;
if(tooBig) size++;
w.writeBytes(new byte[size]);
assertEquals(tooBig, out.size() > ProtocolConstants.MAX_PACKET_LENGTH);
return out.toByteArray();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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