mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Removed redundant frame number from header.
This commit is contained in:
@@ -31,8 +31,8 @@ implements ConnectionReader {
|
|||||||
super(decrypter.getInputStream());
|
super(decrypter.getInputStream());
|
||||||
this.decrypter = decrypter;
|
this.decrypter = decrypter;
|
||||||
this.mac = mac;
|
this.mac = mac;
|
||||||
maxPayloadLength = MAX_FRAME_LENGTH - 8 - mac.getMacLength();
|
maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength();
|
||||||
header = new byte[8];
|
header = new byte[4];
|
||||||
payload = new byte[maxPayloadLength];
|
payload = new byte[maxPayloadLength];
|
||||||
footer = new byte[mac.getMacLength()];
|
footer = new byte[mac.getMacLength()];
|
||||||
}
|
}
|
||||||
@@ -69,8 +69,10 @@ implements ConnectionReader {
|
|||||||
|
|
||||||
private boolean readFrame() throws IOException {
|
private boolean readFrame() throws IOException {
|
||||||
assert betweenFrames;
|
assert betweenFrames;
|
||||||
// Read the header
|
// Don't allow more than 2^32 frames to be read
|
||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
|
frame++;
|
||||||
|
// Read the header
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
while(offset < header.length) {
|
while(offset < header.length) {
|
||||||
int read = in.read(header, offset, header.length - offset);
|
int read = in.read(header, offset, header.length - offset);
|
||||||
@@ -80,16 +82,12 @@ implements ConnectionReader {
|
|||||||
if(offset == 0) return false; // EOF between frames
|
if(offset == 0) return false; // EOF between frames
|
||||||
if(offset < header.length) throw new EOFException(); // Unexpected EOF
|
if(offset < header.length) throw new EOFException(); // Unexpected EOF
|
||||||
mac.update(header);
|
mac.update(header);
|
||||||
// Check that the frame has the expected frame number
|
|
||||||
if(ByteUtils.readUint32(header, 0) != frame)
|
|
||||||
throw new FormatException();
|
|
||||||
// Check that the payload and padding lengths are legal
|
// Check that the payload and padding lengths are legal
|
||||||
payloadLen = ByteUtils.readUint16(header, 4);
|
payloadLen = ByteUtils.readUint16(header, 0);
|
||||||
int paddingLen = ByteUtils.readUint16(header, 6);
|
int paddingLen = ByteUtils.readUint16(header, 2);
|
||||||
if(payloadLen + paddingLen == 0) throw new FormatException();
|
if(payloadLen + paddingLen == 0) throw new FormatException();
|
||||||
if(payloadLen + paddingLen > maxPayloadLength)
|
if(payloadLen + paddingLen > maxPayloadLength)
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
frame++;
|
|
||||||
// Read the payload
|
// Read the payload
|
||||||
offset = 0;
|
offset = 0;
|
||||||
while(offset < payloadLen) {
|
while(offset < payloadLen) {
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ implements ConnectionWriter {
|
|||||||
super(encrypter.getOutputStream());
|
super(encrypter.getOutputStream());
|
||||||
this.encrypter = encrypter;
|
this.encrypter = encrypter;
|
||||||
this.mac = mac;
|
this.mac = mac;
|
||||||
maxPayloadLength = MAX_FRAME_LENGTH - 8 - mac.getMacLength();
|
maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength();
|
||||||
buf = new ByteArrayOutputStream(maxPayloadLength);
|
buf = new ByteArrayOutputStream(maxPayloadLength);
|
||||||
header = new byte[8];
|
header = new byte[4];
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getOutputStream() {
|
public OutputStream getOutputStream() {
|
||||||
@@ -75,8 +75,7 @@ implements ConnectionWriter {
|
|||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
byte[] payload = buf.toByteArray();
|
byte[] payload = buf.toByteArray();
|
||||||
if(payload.length > maxPayloadLength) throw new IllegalStateException();
|
if(payload.length > maxPayloadLength) throw new IllegalStateException();
|
||||||
ByteUtils.writeUint32(frame, header, 0);
|
ByteUtils.writeUint16(payload.length, header, 0);
|
||||||
ByteUtils.writeUint16(payload.length, header, 4);
|
|
||||||
out.write(header);
|
out.write(header);
|
||||||
mac.update(header);
|
mac.update(header);
|
||||||
out.write(payload);
|
out.write(payload);
|
||||||
|
|||||||
@@ -36,9 +36,9 @@ implements ConnectionWriter {
|
|||||||
super(encrypter.getOutputStream());
|
super(encrypter.getOutputStream());
|
||||||
this.encrypter = encrypter;
|
this.encrypter = encrypter;
|
||||||
this.mac = mac;
|
this.mac = mac;
|
||||||
maxPayloadLength = MAX_FRAME_LENGTH - 8 - mac.getMacLength();
|
maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength();
|
||||||
buf = new ByteArrayOutputStream(maxPayloadLength);
|
buf = new ByteArrayOutputStream(maxPayloadLength);
|
||||||
header = new byte[8];
|
header = new byte[4];
|
||||||
padding = new byte[maxPayloadLength];
|
padding = new byte[maxPayloadLength];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,9 +109,8 @@ implements ConnectionWriter {
|
|||||||
byte[] payload = buf.toByteArray();
|
byte[] payload = buf.toByteArray();
|
||||||
if(payload.length > maxPayloadLength) throw new IllegalStateException();
|
if(payload.length > maxPayloadLength) throw new IllegalStateException();
|
||||||
int paddingLength = pad ? maxPayloadLength - payload.length : 0;
|
int paddingLength = pad ? maxPayloadLength - payload.length : 0;
|
||||||
ByteUtils.writeUint32(frame, header, 0);
|
ByteUtils.writeUint16(payload.length, header, 0);
|
||||||
ByteUtils.writeUint16(payload.length, header, 4);
|
ByteUtils.writeUint16(paddingLength, header, 2);
|
||||||
ByteUtils.writeUint16(paddingLength, header, 6);
|
|
||||||
out.write(header);
|
out.write(header);
|
||||||
mac.update(header);
|
mac.update(header);
|
||||||
out.write(payload);
|
out.write(payload);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testLengthZero() throws Exception {
|
public void testLengthZero() throws Exception {
|
||||||
int payloadLength = 0;
|
int payloadLength = 0;
|
||||||
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
||||||
writeHeader(frame, 0L, payloadLength, 0);
|
writeHeader(frame, payloadLength, 0);
|
||||||
// Calculate the MAC
|
// Calculate the MAC
|
||||||
mac.update(frame, 0, headerLength + payloadLength);
|
mac.update(frame, 0, headerLength + payloadLength);
|
||||||
mac.doFinal(frame, headerLength + payloadLength);
|
mac.doFinal(frame, headerLength + payloadLength);
|
||||||
@@ -40,7 +40,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testLengthOne() throws Exception {
|
public void testLengthOne() throws Exception {
|
||||||
int payloadLength = 1;
|
int payloadLength = 1;
|
||||||
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
||||||
writeHeader(frame, 0L, payloadLength, 0);
|
writeHeader(frame, payloadLength, 0);
|
||||||
// Calculate the MAC
|
// Calculate the MAC
|
||||||
mac.update(frame, 0, headerLength + payloadLength);
|
mac.update(frame, 0, headerLength + payloadLength);
|
||||||
mac.doFinal(frame, headerLength + payloadLength);
|
mac.doFinal(frame, headerLength + payloadLength);
|
||||||
@@ -57,12 +57,12 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testMaxLength() throws Exception {
|
public void testMaxLength() throws Exception {
|
||||||
// First frame: max payload length
|
// First frame: max payload length
|
||||||
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||||
writeHeader(frame, 0L, maxPayloadLength, 0);
|
writeHeader(frame, maxPayloadLength, 0);
|
||||||
mac.update(frame, 0, headerLength + maxPayloadLength);
|
mac.update(frame, 0, headerLength + maxPayloadLength);
|
||||||
mac.doFinal(frame, headerLength + maxPayloadLength);
|
mac.doFinal(frame, headerLength + maxPayloadLength);
|
||||||
// Second frame: max payload length plus one
|
// Second frame: max payload length plus one
|
||||||
byte[] frame1 = new byte[MAX_FRAME_LENGTH + 1];
|
byte[] frame1 = new byte[MAX_FRAME_LENGTH + 1];
|
||||||
writeHeader(frame1, 1L, maxPayloadLength + 1, 0);
|
writeHeader(frame1, maxPayloadLength + 1, 0);
|
||||||
mac.update(frame1, 0, headerLength + maxPayloadLength + 1);
|
mac.update(frame1, 0, headerLength + maxPayloadLength + 1);
|
||||||
mac.doFinal(frame1, headerLength + maxPayloadLength + 1);
|
mac.doFinal(frame1, headerLength + maxPayloadLength + 1);
|
||||||
// Concatenate the frames
|
// Concatenate the frames
|
||||||
@@ -88,12 +88,12 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
int paddingLength = 10;
|
int paddingLength = 10;
|
||||||
// First frame: max payload length, including padding
|
// First frame: max payload length, including padding
|
||||||
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||||
writeHeader(frame, 0L, maxPayloadLength - paddingLength, paddingLength);
|
writeHeader(frame, maxPayloadLength - paddingLength, paddingLength);
|
||||||
mac.update(frame, 0, headerLength + maxPayloadLength);
|
mac.update(frame, 0, headerLength + maxPayloadLength);
|
||||||
mac.doFinal(frame, headerLength + maxPayloadLength);
|
mac.doFinal(frame, headerLength + maxPayloadLength);
|
||||||
// Second frame: max payload length plus one, including padding
|
// Second frame: max payload length plus one, including padding
|
||||||
byte[] frame1 = new byte[MAX_FRAME_LENGTH + 1];
|
byte[] frame1 = new byte[MAX_FRAME_LENGTH + 1];
|
||||||
writeHeader(frame1, 1L, maxPayloadLength + 1 - paddingLength,
|
writeHeader(frame1, maxPayloadLength + 1 - paddingLength,
|
||||||
paddingLength);
|
paddingLength);
|
||||||
mac.update(frame1, 0, headerLength + maxPayloadLength + 1);
|
mac.update(frame1, 0, headerLength + maxPayloadLength + 1);
|
||||||
mac.doFinal(frame1, headerLength + maxPayloadLength + 1);
|
mac.doFinal(frame1, headerLength + maxPayloadLength + 1);
|
||||||
@@ -118,15 +118,15 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMultipleFrames() throws Exception {
|
public void testMultipleFrames() throws Exception {
|
||||||
// First frame: 123-byte payload
|
// First frame: 123-byte payload
|
||||||
byte[] frame = new byte[8 + 123 + mac.getMacLength()];
|
byte[] frame = new byte[headerLength + 123 + mac.getMacLength()];
|
||||||
writeHeader(frame, 0L, 123, 0);
|
writeHeader(frame, 123, 0);
|
||||||
mac.update(frame, 0, 8 + 123);
|
mac.update(frame, 0, headerLength + 123);
|
||||||
mac.doFinal(frame, 8 + 123);
|
mac.doFinal(frame, headerLength + 123);
|
||||||
// Second frame: 1234-byte payload
|
// Second frame: 1234-byte payload
|
||||||
byte[] frame1 = new byte[8 + 1234 + mac.getMacLength()];
|
byte[] frame1 = new byte[headerLength + 1234 + mac.getMacLength()];
|
||||||
writeHeader(frame1, 1L, 1234, 0);
|
writeHeader(frame1, 1234, 0);
|
||||||
mac.update(frame1, 0, 8 + 1234);
|
mac.update(frame1, 0, headerLength + 1234);
|
||||||
mac.doFinal(frame1, 8 + 1234);
|
mac.doFinal(frame1, headerLength + 1234);
|
||||||
// Concatenate the frames
|
// Concatenate the frames
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
out.write(frame);
|
out.write(frame);
|
||||||
@@ -147,7 +147,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testCorruptPayload() throws Exception {
|
public void testCorruptPayload() throws Exception {
|
||||||
int payloadLength = 8;
|
int payloadLength = 8;
|
||||||
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
||||||
writeHeader(frame, 0L, payloadLength, 0);
|
writeHeader(frame, payloadLength, 0);
|
||||||
// Calculate the MAC
|
// Calculate the MAC
|
||||||
mac.update(frame, 0, headerLength + payloadLength);
|
mac.update(frame, 0, headerLength + payloadLength);
|
||||||
mac.doFinal(frame, headerLength + payloadLength);
|
mac.doFinal(frame, headerLength + payloadLength);
|
||||||
@@ -167,7 +167,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testCorruptMac() throws Exception {
|
public void testCorruptMac() throws Exception {
|
||||||
int payloadLength = 8;
|
int payloadLength = 8;
|
||||||
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
||||||
writeHeader(frame, 0L, payloadLength, 0);
|
writeHeader(frame, payloadLength, 0);
|
||||||
// Calculate the MAC
|
// Calculate the MAC
|
||||||
mac.update(frame, 0, headerLength + payloadLength);
|
mac.update(frame, 0, headerLength + payloadLength);
|
||||||
mac.doFinal(frame, headerLength + payloadLength);
|
mac.doFinal(frame, headerLength + payloadLength);
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class ConnectionWriterImplTest extends TransportTest {
|
|||||||
public void testSingleByteFrame() throws Exception {
|
public void testSingleByteFrame() throws Exception {
|
||||||
int payloadLength = 1;
|
int payloadLength = 1;
|
||||||
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
||||||
writeHeader(frame, 0L, payloadLength, 0);
|
writeHeader(frame, payloadLength, 0);
|
||||||
// Calculate the MAC
|
// Calculate the MAC
|
||||||
mac.update(frame, 0, headerLength + payloadLength);
|
mac.update(frame, 0, headerLength + payloadLength);
|
||||||
mac.doFinal(frame, headerLength + payloadLength);
|
mac.doFinal(frame, headerLength + payloadLength);
|
||||||
@@ -76,12 +76,12 @@ public class ConnectionWriterImplTest extends TransportTest {
|
|||||||
public void testMultipleFrames() throws Exception {
|
public void testMultipleFrames() throws Exception {
|
||||||
// First frame: 123-byte payload
|
// First frame: 123-byte payload
|
||||||
byte[] frame = new byte[headerLength + 123 + macLength];
|
byte[] frame = new byte[headerLength + 123 + macLength];
|
||||||
writeHeader(frame, 0L, 123, 0);
|
writeHeader(frame, 123, 0);
|
||||||
mac.update(frame, 0, headerLength + 123);
|
mac.update(frame, 0, headerLength + 123);
|
||||||
mac.doFinal(frame, headerLength + 123);
|
mac.doFinal(frame, headerLength + 123);
|
||||||
// Second frame: 1234-byte payload
|
// Second frame: 1234-byte payload
|
||||||
byte[] frame1 = new byte[headerLength + 1234 + macLength];
|
byte[] frame1 = new byte[headerLength + 1234 + macLength];
|
||||||
writeHeader(frame1, 1L, 1234, 0);
|
writeHeader(frame1, 1234, 0);
|
||||||
mac.update(frame1, 0, headerLength + 1234);
|
mac.update(frame1, 0, headerLength + 1234);
|
||||||
mac.doFinal(frame1, headerLength + 1234);
|
mac.doFinal(frame1, headerLength + 1234);
|
||||||
// Concatenate the frames
|
// Concatenate the frames
|
||||||
|
|||||||
@@ -157,8 +157,7 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
|||||||
assertEquals(MAX_FRAME_LENGTH, out.size());
|
assertEquals(MAX_FRAME_LENGTH, out.size());
|
||||||
// The frame should have a payload length of 1 and padding for the rest
|
// The frame should have a payload length of 1 and padding for the rest
|
||||||
byte[] frame = out.toByteArray();
|
byte[] frame = out.toByteArray();
|
||||||
assertEquals(0L, ByteUtils.readUint32(frame, 0)); // Frame number
|
assertEquals(1, ByteUtils.readUint16(frame, 0)); // Payload length
|
||||||
assertEquals(1, ByteUtils.readUint16(frame, 4)); // Payload length
|
assertEquals(maxPayloadLength - 1, ByteUtils.readUint16(frame, 2));
|
||||||
assertEquals(maxPayloadLength - 1, ByteUtils.readUint16(frame, 6));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import junit.framework.TestCase;
|
|||||||
public abstract class TransportTest extends TestCase {
|
public abstract class TransportTest extends TestCase {
|
||||||
|
|
||||||
protected final Mac mac;
|
protected final Mac mac;
|
||||||
protected final int headerLength = 8, macLength, maxPayloadLength;
|
protected final int headerLength = 4, macLength, maxPayloadLength;
|
||||||
|
|
||||||
public TransportTest() throws Exception {
|
public TransportTest() throws Exception {
|
||||||
super();
|
super();
|
||||||
@@ -27,9 +27,8 @@ public abstract class TransportTest extends TestCase {
|
|||||||
maxPayloadLength = MAX_FRAME_LENGTH - headerLength - macLength;
|
maxPayloadLength = MAX_FRAME_LENGTH - headerLength - macLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void writeHeader(byte[] b, long frame, int payload, int padding) {
|
static void writeHeader(byte[] b, int payload, int padding) {
|
||||||
ByteUtils.writeUint32(frame, b, 0);
|
ByteUtils.writeUint16(payload, b, 0);
|
||||||
ByteUtils.writeUint16(payload, b, 4);
|
ByteUtils.writeUint16(padding, b, 2);
|
||||||
ByteUtils.writeUint16(padding, b, 6);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user