mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Code cleanup for the transport protocol.
This commit is contained in:
@@ -34,7 +34,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
||||
Cipher framePeekingCipher = crypto.getFramePeekingCipher();
|
||||
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
|
||||
IvEncoder framePeekingIvEncoder = crypto.getFramePeekingIvEncoder();
|
||||
FrameReader encryption = new IncomingEncryptionLayerImpl(in, tagCipher,
|
||||
FrameReader encryption = new IncomingEncryptionLayer(in, tagCipher,
|
||||
frameCipher, framePeekingCipher, frameIvEncoder,
|
||||
framePeekingIvEncoder, tagKey, frameKey, !initiator);
|
||||
return new ConnectionReaderImpl(encryption);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -11,14 +12,14 @@ import net.sf.briar.api.transport.ConnectionReader;
|
||||
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
|
||||
private final FrameReader in;
|
||||
private final Frame frame;
|
||||
private final byte[] frame;
|
||||
|
||||
private int offset = 0, length = 0;
|
||||
|
||||
ConnectionReaderImpl(FrameReader in) {
|
||||
this.in = in;
|
||||
frame = new Frame();
|
||||
offset = FRAME_HEADER_LENGTH;
|
||||
frame = new byte[MAX_FRAME_LENGTH];
|
||||
offset = HEADER_LENGTH;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
@@ -29,7 +30,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
public int read() throws IOException {
|
||||
if(length == -1) return -1;
|
||||
while(length == 0) if(!readFrame()) return -1;
|
||||
int b = frame.getBuffer()[offset] & 0xff;
|
||||
int b = frame[offset] & 0xff;
|
||||
offset++;
|
||||
length--;
|
||||
return b;
|
||||
@@ -45,7 +46,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
if(length == -1) return -1;
|
||||
while(length == 0) if(!readFrame()) return -1;
|
||||
len = Math.min(len, length);
|
||||
System.arraycopy(frame.getBuffer(), offset, b, off, len);
|
||||
System.arraycopy(frame, offset, b, off, len);
|
||||
offset += len;
|
||||
length -= len;
|
||||
return len;
|
||||
@@ -53,19 +54,17 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
|
||||
private boolean readFrame() throws IOException {
|
||||
assert length == 0;
|
||||
byte[] buf = frame.getBuffer();
|
||||
if(HeaderEncoder.isLastFrame(buf)) {
|
||||
if(HeaderEncoder.isLastFrame(frame)) {
|
||||
length = -1;
|
||||
return false;
|
||||
}
|
||||
frame.reset();
|
||||
if(!in.readFrame(frame)) throw new FormatException();
|
||||
offset = FRAME_HEADER_LENGTH;
|
||||
length = HeaderEncoder.getPayloadLength(buf);
|
||||
offset = HEADER_LENGTH;
|
||||
length = HeaderEncoder.getPayloadLength(frame);
|
||||
// The padding must be all zeroes
|
||||
int padding = HeaderEncoder.getPaddingLength(buf);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
for(int i = offset + length; i < offset + length + padding; i++) {
|
||||
if(buf[i] != 0) throw new FormatException();
|
||||
if(frame[i] != 0) throw new FormatException();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
|
||||
FrameWriter encryption = new OutgoingEncryptionLayerImpl(
|
||||
FrameWriter encryption = new OutgoingEncryptionLayer(
|
||||
out, capacity, tagCipher, frameCipher, frameIvEncoder, tagKey,
|
||||
frameKey);
|
||||
return new ConnectionWriterImpl(encryption);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
@@ -19,15 +19,15 @@ import net.sf.briar.api.transport.ConnectionWriter;
|
||||
class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
|
||||
private final FrameWriter out;
|
||||
private final Frame frame;
|
||||
private final byte[] frame;
|
||||
|
||||
private int offset;
|
||||
private long frameNumber;
|
||||
|
||||
ConnectionWriterImpl(FrameWriter out) {
|
||||
this.out = out;
|
||||
frame = new Frame();
|
||||
offset = FRAME_HEADER_LENGTH;
|
||||
frame = new byte[MAX_FRAME_LENGTH];
|
||||
offset = HEADER_LENGTH;
|
||||
frameNumber = 0L;
|
||||
}
|
||||
|
||||
@@ -38,29 +38,29 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
public long getRemainingCapacity() {
|
||||
long capacity = out.getRemainingCapacity();
|
||||
// If there's any data buffered, subtract it and its overhead
|
||||
if(offset > FRAME_HEADER_LENGTH) capacity -= offset + MAC_LENGTH;
|
||||
if(offset > HEADER_LENGTH) capacity -= offset + MAC_LENGTH;
|
||||
// Subtract the overhead from the remaining capacity
|
||||
long frames = (long) Math.ceil((double) capacity / MAX_FRAME_LENGTH);
|
||||
int overheadPerFrame = FRAME_HEADER_LENGTH + MAC_LENGTH;
|
||||
int overheadPerFrame = HEADER_LENGTH + MAC_LENGTH;
|
||||
return Math.max(0L, capacity - frames * overheadPerFrame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if(offset > FRAME_HEADER_LENGTH || frameNumber > 0L) writeFrame(true);
|
||||
if(offset > HEADER_LENGTH || frameNumber > 0L) writeFrame(true);
|
||||
out.flush();
|
||||
super.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
if(offset > FRAME_HEADER_LENGTH) writeFrame(false);
|
||||
if(offset > HEADER_LENGTH) writeFrame(false);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
frame.getBuffer()[offset++] = (byte) b;
|
||||
frame[offset++] = (byte) b;
|
||||
if(offset + MAC_LENGTH == MAX_FRAME_LENGTH) writeFrame(false);
|
||||
}
|
||||
|
||||
@@ -71,30 +71,26 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
byte[] buf = frame.getBuffer();
|
||||
int available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
|
||||
while(available <= len) {
|
||||
System.arraycopy(b, off, buf, offset, available);
|
||||
System.arraycopy(b, off, frame, offset, available);
|
||||
offset += available;
|
||||
writeFrame(false);
|
||||
off += available;
|
||||
len -= available;
|
||||
available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
|
||||
}
|
||||
System.arraycopy(b, off, buf, offset, len);
|
||||
System.arraycopy(b, off, frame, offset, len);
|
||||
offset += len;
|
||||
}
|
||||
|
||||
private void writeFrame(boolean lastFrame) throws IOException {
|
||||
if(frameNumber > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||
int payload = offset - FRAME_HEADER_LENGTH;
|
||||
int payload = offset - HEADER_LENGTH;
|
||||
assert payload >= 0;
|
||||
HeaderEncoder.encodeHeader(frame.getBuffer(), frameNumber, payload, 0,
|
||||
lastFrame);
|
||||
frame.setLength(offset);
|
||||
HeaderEncoder.encodeHeader(frame, frameNumber, payload, 0, lastFrame);
|
||||
out.writeFrame(frame);
|
||||
frame.reset();
|
||||
offset = FRAME_HEADER_LENGTH;
|
||||
offset = HEADER_LENGTH;
|
||||
frameNumber++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
class Frame {
|
||||
|
||||
private final byte[] buf;
|
||||
|
||||
private int length = -1;
|
||||
|
||||
Frame() {
|
||||
buf = new byte[MAX_FRAME_LENGTH];
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
return buf;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
if(length == -1) throw new IllegalStateException();
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
if(length < FRAME_HEADER_LENGTH || length > buf.length)
|
||||
throw new IllegalArgumentException();
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
length = -1;
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,5 @@ interface FrameReader {
|
||||
* Reads a frame into the given buffer. Returns false if no more frames can
|
||||
* be read from the connection.
|
||||
*/
|
||||
boolean readFrame(Frame f) throws IOException;
|
||||
boolean readFrame(byte[] frame) throws IOException;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.io.IOException;
|
||||
interface FrameWriter {
|
||||
|
||||
/** Writes the given frame. */
|
||||
void writeFrame(Frame f) throws IOException;
|
||||
void writeFrame(byte[] frame) throws IOException;
|
||||
|
||||
/** Flushes the stack. */
|
||||
void flush() throws IOException;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import net.sf.briar.util.ByteUtils;
|
||||
|
||||
@@ -8,7 +8,7 @@ class HeaderEncoder {
|
||||
|
||||
static void encodeHeader(byte[] header, long frameNumber, int payload,
|
||||
int padding, boolean lastFrame) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
if(header.length < HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(frameNumber < 0 || frameNumber > ByteUtils.MAX_32_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
@@ -23,37 +23,32 @@ class HeaderEncoder {
|
||||
}
|
||||
|
||||
static boolean checkHeader(byte[] header, int length) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||
int payload = getPayloadLength(header);
|
||||
int padding = getPaddingLength(header);
|
||||
if(FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH != length)
|
||||
if(HEADER_LENGTH + payload + padding + MAC_LENGTH != length)
|
||||
return false;
|
||||
if(header[8] != 0 && header[8] != 1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static long getFrameNumber(byte[] header) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||
return ByteUtils.readUint32(header, 0);
|
||||
}
|
||||
|
||||
static int getPayloadLength(byte[] header) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||
return ByteUtils.readUint16(header, 4);
|
||||
}
|
||||
|
||||
static int getPaddingLength(byte[] header) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||
return ByteUtils.readUint16(header, 6);
|
||||
}
|
||||
|
||||
static boolean isLastFrame(byte[] header) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||
return header[8] == 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
@@ -17,7 +17,7 @@ import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.crypto.IvEncoder;
|
||||
|
||||
class IncomingEncryptionLayerImpl implements FrameReader {
|
||||
class IncomingEncryptionLayer implements FrameReader {
|
||||
|
||||
private final InputStream in;
|
||||
private final Cipher tagCipher, frameCipher, framePeekingCipher;
|
||||
@@ -29,7 +29,7 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
||||
private boolean readTag;
|
||||
private long frameNumber;
|
||||
|
||||
IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher,
|
||||
IncomingEncryptionLayer(InputStream in, Cipher tagCipher,
|
||||
Cipher frameCipher, Cipher framePeekingCipher,
|
||||
IvEncoder frameIvEncoder, IvEncoder framePeekingIvEncoder,
|
||||
ErasableKey tagKey, ErasableKey frameKey, boolean readTag) {
|
||||
@@ -43,15 +43,14 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
||||
this.frameKey = frameKey;
|
||||
this.readTag = readTag;
|
||||
blockSize = frameCipher.getBlockSize();
|
||||
if(blockSize < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(blockSize < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||
frameIv = frameIvEncoder.encodeIv(0L);
|
||||
framePeekingIv = framePeekingIvEncoder.encodeIv(0L);
|
||||
ciphertext = new byte[MAX_FRAME_LENGTH];
|
||||
frameNumber = 0L;
|
||||
}
|
||||
|
||||
public boolean readFrame(Frame f) throws IOException {
|
||||
public boolean readFrame(byte[] frame) throws IOException {
|
||||
try {
|
||||
// Read the tag if it hasn't already been read
|
||||
if(readTag) {
|
||||
@@ -79,19 +78,18 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
||||
// Decrypt the first block of the frame to peek at the header
|
||||
framePeekingIvEncoder.updateIv(framePeekingIv, frameNumber);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(framePeekingIv);
|
||||
byte[] plaintext = f.getBuffer();
|
||||
try {
|
||||
framePeekingCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||
int decrypted = framePeekingCipher.update(ciphertext, 0,
|
||||
blockSize, plaintext);
|
||||
blockSize, frame);
|
||||
if(decrypted != blockSize) throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
// Parse the frame header
|
||||
int payload = HeaderEncoder.getPayloadLength(plaintext);
|
||||
int padding = HeaderEncoder.getPaddingLength(plaintext);
|
||||
int length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
int length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||
// Read the remainder of the frame
|
||||
while(offset < length) {
|
||||
@@ -105,13 +103,12 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
||||
try {
|
||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||
int decrypted = frameCipher.doFinal(ciphertext, 0, length,
|
||||
plaintext);
|
||||
frame);
|
||||
if(decrypted != length - MAC_LENGTH)
|
||||
throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
f.setLength(length - MAC_LENGTH);
|
||||
frameNumber++;
|
||||
return true;
|
||||
} catch(IOException e) {
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
@@ -14,7 +15,7 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.crypto.IvEncoder;
|
||||
|
||||
class OutgoingEncryptionLayerImpl implements FrameWriter {
|
||||
class OutgoingEncryptionLayer implements FrameWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
@@ -24,9 +25,9 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
|
||||
|
||||
private long capacity, frameNumber;
|
||||
|
||||
OutgoingEncryptionLayerImpl(OutputStream out, long capacity,
|
||||
Cipher tagCipher, Cipher frameCipher, IvEncoder frameIvEncoder,
|
||||
ErasableKey tagKey, ErasableKey frameKey) {
|
||||
OutgoingEncryptionLayer(OutputStream out, long capacity, Cipher tagCipher,
|
||||
Cipher frameCipher, IvEncoder frameIvEncoder, ErasableKey tagKey,
|
||||
ErasableKey frameKey) {
|
||||
this.out = out;
|
||||
this.capacity = capacity;
|
||||
this.tagCipher = tagCipher;
|
||||
@@ -39,9 +40,10 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
|
||||
frameNumber = 0L;
|
||||
}
|
||||
|
||||
public void writeFrame(Frame f) throws IOException {
|
||||
byte[] plaintext = f.getBuffer();
|
||||
int offset = 0, length = f.getLength();
|
||||
public void writeFrame(byte[] frame) throws IOException {
|
||||
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
int offset = 0, length = HEADER_LENGTH + payload + padding;
|
||||
if(frameNumber == 0) {
|
||||
TagEncoder.encodeTag(ciphertext, tagCipher, tagKey);
|
||||
offset = TAG_LENGTH;
|
||||
@@ -50,8 +52,8 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(frameIv);
|
||||
try {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
int encrypted = frameCipher.doFinal(plaintext, 0, length,
|
||||
ciphertext, offset);
|
||||
int encrypted = frameCipher.doFinal(frame, 0, length, ciphertext,
|
||||
offset);
|
||||
if(encrypted != length + MAC_LENGTH) throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
Reference in New Issue
Block a user