Pass segments rather than frames to/from segmented plugins.

This commit is contained in:
akwizgran
2012-01-13 15:05:42 +00:00
parent d0e402062a
commit ac136d3732
23 changed files with 155 additions and 61 deletions

View File

@@ -14,7 +14,6 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
class ConnectionDecrypter implements FrameSource {
@@ -49,6 +48,8 @@ class ConnectionDecrypter implements FrameSource {
} catch(GeneralSecurityException badIvOrKey) {
throw new RuntimeException(badIvOrKey);
}
// Clear the buffer before exposing it to the transport plugin
for(int i = 0; i < b.length; i++) b[i] = 0;
try {
// Read the first block
int offset = 0;
@@ -64,7 +65,7 @@ class ConnectionDecrypter implements FrameSource {
// Decrypt the first block
try {
int decrypted = frameCipher.update(b, 0, blockSize, b);
assert decrypted == blockSize;
if(decrypted != blockSize) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}
@@ -86,7 +87,8 @@ class ConnectionDecrypter implements FrameSource {
try {
int decrypted = frameCipher.doFinal(b, blockSize,
length - blockSize, b, blockSize);
assert decrypted == length - blockSize;
if(decrypted != length - blockSize)
throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}

View File

@@ -2,7 +2,6 @@ package net.sf.briar.transport;
import java.io.IOException;
import net.sf.briar.api.plugins.FrameSink;
/** Encrypts authenticated data to be sent over a connection. */
interface ConnectionEncrypter extends FrameSink {

View File

@@ -50,7 +50,7 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter {
try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
int encrypted = frameCipher.doFinal(b, 0, len, b);
assert encrypted == len;
if(encrypted != len) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}

View File

@@ -7,7 +7,6 @@ import javax.crypto.Mac;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.util.ByteUtils;

View File

@@ -12,7 +12,6 @@ import javax.crypto.Mac;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader;
class ConnectionReaderImpl extends InputStream implements ConnectionReader {

View File

@@ -0,0 +1,9 @@
package net.sf.briar.transport;
import java.io.IOException;
interface FrameSink {
/** Writes the given frame. */
void writeFrame(byte[] b, int len) throws IOException;
}

View File

@@ -0,0 +1,12 @@
package net.sf.briar.transport;
import java.io.IOException;
interface FrameSource {
/**
* Reads a frame into the given buffer and returns its length, or -1 if no
* more frames can be read.
*/
int readFrame(byte[] b) throws IOException;
}

View File

@@ -0,0 +1,45 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import net.sf.briar.api.plugins.Segment;
import net.sf.briar.util.ByteUtils;
class SegmentImpl implements Segment {
private final byte[] buf = new byte[MAX_FRAME_LENGTH];
private int length = -1;
private long transmission = -1;
public void clear() {
for(int i = 0; i < buf.length; i++) buf[i] = 0;
length = -1;
transmission = -1;
}
public byte[] getBuffer() {
return buf;
}
public int getLength() {
if(length == -1) throw new IllegalStateException();
return length;
}
public long getTransmissionNumber() {
if(transmission == -1) throw new IllegalStateException();
return transmission;
}
public void setLength(int length) {
if(length < 0 || length > buf.length)
throw new IllegalArgumentException();
this.length = length;
}
public void setTransmissionNumber(int transmission) {
if(transmission < 0 || transmission > ByteUtils.MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException();
this.transmission = transmission;
}
}

View File

@@ -12,19 +12,21 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.plugins.Segment;
import net.sf.briar.api.plugins.SegmentSource;
class SegmentedConnectionDecrypter implements FrameSource {
private final FrameSource in;
private final SegmentSource in;
private final Cipher frameCipher;
private final ErasableKey frameKey;
private final int macLength, blockSize;
private final byte[] iv;
private final Segment segment;
private long frame = 0L;
SegmentedConnectionDecrypter(FrameSource in, Cipher frameCipher,
SegmentedConnectionDecrypter(SegmentSource in, Cipher frameCipher,
ErasableKey frameKey, int macLength) {
this.in = in;
this.frameCipher = frameCipher;
@@ -34,6 +36,7 @@ class SegmentedConnectionDecrypter implements FrameSource {
if(blockSize < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
iv = IvEncoder.encodeIv(0, blockSize);
segment = new SegmentImpl();
}
public int readFrame(byte[] b) throws IOException {
@@ -47,17 +50,22 @@ class SegmentedConnectionDecrypter implements FrameSource {
} catch(GeneralSecurityException badIvOrKey) {
throw new RuntimeException(badIvOrKey);
}
// Clear the buffer before exposing it to the transport plugin
segment.clear();
try {
// Read the frame
int length = in.readFrame(b);
if(length == -1) return -1;
if(!in.readSegment(segment)) return -1;
if(segment.getTransmissionNumber() != frame)
throw new FormatException();
int length = segment.getLength();
if(length > MAX_FRAME_LENGTH) throw new FormatException();
if(length < FRAME_HEADER_LENGTH + macLength)
throw new FormatException();
// Decrypt the frame
try {
int decrypted = frameCipher.doFinal(b, 0, length, b);
assert decrypted == length;
int decrypted = frameCipher.doFinal(segment.getBuffer(), 0,
length, b);
if(decrypted != length) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}

View File

@@ -1,6 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.security.GeneralSecurityException;
@@ -9,20 +10,23 @@ import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSink;
import net.sf.briar.api.plugins.Segment;
import net.sf.briar.api.plugins.SegmentSink;
class SegmentedConnectionEncrypter implements ConnectionEncrypter {
private final FrameSink out;
private final SegmentSink out;
private final Cipher frameCipher;
private final ErasableKey frameKey;
private final byte[] iv, tag;
private final Segment segment;
private long capacity, frame = 0L;
private boolean tagWritten = false;
SegmentedConnectionEncrypter(FrameSink out, long capacity, Cipher tagCipher,
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey) {
SegmentedConnectionEncrypter(SegmentSink out, long capacity,
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
ErasableKey frameKey) {
this.out = out;
this.capacity = capacity;
this.frameCipher = frameCipher;
@@ -31,16 +35,16 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
// Encrypt the tag
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
tagKey.erase();
segment = new SegmentImpl();
}
public void writeFrame(byte[] b, int len) throws IOException {
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
int offset = 0;
if(!tagWritten) {
if(tag.length + len > b.length)
if(tag.length + len > MAX_FRAME_LENGTH)
throw new IllegalArgumentException();
System.arraycopy(b, 0, b, tag.length, len);
System.arraycopy(tag, 0, b, 0, tag.length);
System.arraycopy(tag, 0, segment.getBuffer(), 0, tag.length);
capacity -= tag.length;
tagWritten = true;
offset = tag.length;
@@ -49,13 +53,15 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
IvParameterSpec ivSpec = new IvParameterSpec(iv);
try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
int encrypted = frameCipher.doFinal(b, offset, len, b, offset);
assert encrypted == len;
int encrypted = frameCipher.doFinal(b, 0, len, segment.getBuffer(),
offset);
if(encrypted != len) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}
segment.setLength(offset + len);
try {
out.writeFrame(b, offset + len);
out.writeSegment(segment);
} catch(IOException e) {
frameKey.erase();
throw e;