Use the same maximum frame length for all transports.

This commit is contained in:
akwizgran
2015-01-05 16:24:44 +00:00
parent 358166bc12
commit d3bf2d59a1
60 changed files with 194 additions and 321 deletions

View File

@@ -20,23 +20,21 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
}
public StreamDecrypter createStreamDecrypter(InputStream in,
int maxFrameLength, StreamContext ctx) {
StreamContext ctx) {
byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber();
boolean alice = !ctx.getAlice();
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the decrypter
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey,
maxFrameLength);
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
}
public StreamDecrypter createInvitationStreamDecrypter(InputStream in,
int maxFrameLength, byte[] secret, boolean alice) {
byte[] secret, boolean alice) {
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the decrypter
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey,
maxFrameLength);
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
}
}

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.EOFException;
import java.io.IOException;
@@ -21,21 +22,19 @@ class StreamDecrypterImpl implements StreamDecrypter {
private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey;
private final byte[] iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber;
private boolean finalFrame;
StreamDecrypterImpl(InputStream in, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength) {
SecretKey frameKey) {
this.in = in;
this.frameCipher = frameCipher;
this.frameKey = frameKey;
this.frameLength = frameLength;
iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH];
ciphertext = new byte[frameLength];
plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0;
finalFrame = false;
}
@@ -44,9 +43,9 @@ class StreamDecrypterImpl implements StreamDecrypter {
if(finalFrame) return -1;
// Read the frame
int ciphertextLength = 0;
while(ciphertextLength < frameLength) {
while(ciphertextLength < MAX_FRAME_LENGTH) {
int read = in.read(ciphertext, ciphertextLength,
frameLength - ciphertextLength);
MAX_FRAME_LENGTH - ciphertextLength);
if(read == -1) break; // We'll check the length later
ciphertextLength += read;
}
@@ -65,7 +64,7 @@ class StreamDecrypterImpl implements StreamDecrypter {
}
// Decode and validate the header
finalFrame = FrameEncoder.isFinalFrame(plaintext);
if(!finalFrame && ciphertextLength < frameLength)
if(!finalFrame && ciphertextLength < MAX_FRAME_LENGTH)
throw new FormatException();
int payloadLength = FrameEncoder.getPayloadLength(plaintext);
if(payloadLength > plaintextLength - HEADER_LENGTH)

View File

@@ -22,7 +22,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
}
public StreamEncrypter createStreamEncrypter(OutputStream out,
int maxFrameLength, StreamContext ctx) {
StreamContext ctx) {
byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber();
boolean alice = ctx.getAlice();
@@ -34,15 +34,15 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, tag);
tag);
}
public StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice) {
byte[] secret, boolean alice) {
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, null);
null);
}
}

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import java.io.IOException;
@@ -20,22 +21,20 @@ class StreamEncrypterImpl implements StreamEncrypter {
private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey;
private final byte[] tag, iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber;
private boolean writeTag;
StreamEncrypterImpl(OutputStream out, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength, byte[] tag) {
SecretKey frameKey, byte[] tag) {
this.out = out;
this.frameCipher = frameCipher;
this.frameKey = frameKey;
this.frameLength = frameLength;
this.tag = tag;
iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH];
ciphertext = new byte[frameLength];
plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0;
writeTag = (tag != null);
}
@@ -54,8 +53,8 @@ class StreamEncrypterImpl implements StreamEncrypter {
plaintextLength = HEADER_LENGTH + payloadLength;
ciphertextLength = plaintextLength + MAC_LENGTH;
} else {
plaintextLength = frameLength - MAC_LENGTH;
ciphertextLength = frameLength;
plaintextLength = MAX_FRAME_LENGTH - MAC_LENGTH;
ciphertextLength = MAX_FRAME_LENGTH;
}
// Encode the header
FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength);

View File

@@ -128,16 +128,15 @@ class AliceConnector extends Connector {
// Confirmation succeeded - upgrade to a secure connection
if(LOG.isLoggable(INFO))
LOG.info(pluginName + " confirmation succeeded");
int maxFrameLength = conn.getReader().getMaxFrameLength();
// Create the readers
InputStream streamReader =
streamReaderFactory.createInvitationStreamReader(in,
maxFrameLength, secret, false); // Bob's stream
secret, false); // Bob's stream
r = readerFactory.createReader(streamReader);
// Create the writers
OutputStream streamWriter =
streamWriterFactory.createInvitationStreamWriter(out,
maxFrameLength, secret, true); // Alice's stream
secret, true); // Alice's stream
w = writerFactory.createWriter(streamWriter);
// Derive the invitation nonces
byte[][] nonces = crypto.deriveInvitationNonces(secret);

View File

@@ -128,16 +128,15 @@ class BobConnector extends Connector {
// Confirmation succeeded - upgrade to a secure connection
if(LOG.isLoggable(INFO))
LOG.info(pluginName + " confirmation succeeded");
int maxFrameLength = conn.getReader().getMaxFrameLength();
// Create the readers
InputStream streamReader =
streamReaderFactory.createInvitationStreamReader(in,
maxFrameLength, secret, true); // Alice's stream
secret, true); // Alice's stream
r = readerFactory.createReader(streamReader);
// Create the writers
OutputStream streamWriter =
streamWriterFactory.createInvitationStreamWriter(out,
maxFrameLength, secret, false); // Bob's stream
secret, false); // Bob's stream
w = writerFactory.createWriter(streamWriter);
// Derive the nonces
byte[][] nonces = crypto.deriveInvitationNonces(secret);

View File

@@ -95,7 +95,7 @@ class ConnectionManagerImpl implements ConnectionManager {
private MessagingSession createIncomingSession(StreamContext ctx,
TransportConnectionReader r) throws IOException {
InputStream streamReader = streamReaderFactory.createStreamReader(
r.getInputStream(), r.getMaxFrameLength(), ctx);
r.getInputStream(), ctx);
return messagingSessionFactory.createIncomingSession(
ctx.getContactId(), ctx.getTransportId(), streamReader);
}
@@ -103,7 +103,7 @@ class ConnectionManagerImpl implements ConnectionManager {
private MessagingSession createSimplexOutgoingSession(StreamContext ctx,
TransportConnectionWriter w) throws IOException {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), w.getMaxFrameLength(), ctx);
w.getOutputStream(), ctx);
return messagingSessionFactory.createSimplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
streamWriter);
@@ -112,7 +112,7 @@ class ConnectionManagerImpl implements ConnectionManager {
private MessagingSession createDuplexOutgoingSession(StreamContext ctx,
TransportConnectionWriter w) throws IOException {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), w.getMaxFrameLength(), ctx);
w.getOutputStream(), ctx);
return messagingSessionFactory.createDuplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
w.getMaxIdleTime(), streamWriter);

View File

@@ -28,7 +28,7 @@ public abstract class FilePlugin implements SimplexPlugin {
protected final Executor ioExecutor;
protected final FileUtils fileUtils;
protected final SimplexPluginCallback callback;
protected final int maxFrameLength, maxLatency;
protected final int maxLatency;
protected volatile boolean running = false;
@@ -38,19 +38,13 @@ public abstract class FilePlugin implements SimplexPlugin {
protected abstract void readerFinished(File f);
protected FilePlugin(Executor ioExecutor, FileUtils fileUtils,
SimplexPluginCallback callback, int maxFrameLength,
int maxLatency) {
SimplexPluginCallback callback, int maxLatency) {
this.ioExecutor = ioExecutor;
this.fileUtils = fileUtils;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -24,10 +24,6 @@ class FileTransportReader implements TransportConnectionReader {
this.plugin = plugin;
}
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -27,10 +27,6 @@ class FileTransportWriter implements TransportConnectionWriter {
this.plugin = plugin;
}
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -17,10 +17,8 @@ class LanTcpPlugin extends TcpPlugin {
static final TransportId ID = new TransportId("lan");
LanTcpPlugin(Executor ioExecutor, DuplexPluginCallback callback,
int maxFrameLength, int maxLatency, int maxIdleTime,
int pollingInterval) {
super(ioExecutor, callback, maxFrameLength, maxLatency, maxIdleTime,
pollingInterval);
int maxLatency, int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
}
public TransportId getId() {

View File

@@ -9,7 +9,6 @@ import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
public class LanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -25,7 +24,7 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new LanTcpPlugin(ioExecutor, callback, MAX_FRAME_LENGTH,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
return new LanTcpPlugin(ioExecutor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
}
}

View File

@@ -37,8 +37,7 @@ abstract class TcpPlugin implements DuplexPlugin {
protected final Executor ioExecutor;
protected final DuplexPluginCallback callback;
protected final int maxFrameLength, maxLatency, maxIdleTime;
protected final int pollingInterval, socketTimeout;
protected final int maxLatency, maxIdleTime, pollingInterval, socketTimeout;
protected volatile boolean running = false;
protected volatile ServerSocket socket = null;
@@ -53,11 +52,9 @@ abstract class TcpPlugin implements DuplexPlugin {
protected abstract boolean isConnectable(InetSocketAddress remote);
protected TcpPlugin(Executor ioExecutor, DuplexPluginCallback callback,
int maxFrameLength, int maxLatency, int maxIdleTime,
int pollingInterval) {
int maxLatency, int maxIdleTime, int pollingInterval) {
this.ioExecutor = ioExecutor;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime;
this.pollingInterval = pollingInterval;
@@ -66,10 +63,6 @@ abstract class TcpPlugin implements DuplexPlugin {
else socketTimeout = maxIdleTime * 2;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -38,10 +38,6 @@ class TcpTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
@@ -59,10 +55,6 @@ class TcpTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -21,10 +21,9 @@ class WanTcpPlugin extends TcpPlugin {
private volatile MappingResult mappingResult;
WanTcpPlugin(Executor ioExecutor, PortMapper portMapper,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency,
int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxFrameLength, maxLatency, maxIdleTime,
pollingInterval);
DuplexPluginCallback callback, int maxLatency, int maxIdleTime,
int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
this.portMapper = portMapper;
}

View File

@@ -10,7 +10,6 @@ import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
public class WanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 5 * 60 * 1000; // 5 minutes
@@ -30,7 +29,6 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new WanTcpPlugin(ioExecutor, new PortMapperImpl(shutdownManager),
callback, MAX_FRAME_LENGTH, MAX_LATENCY, MAX_IDLE_TIME,
POLLING_INTERVAL);
callback, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
}
}

View File

@@ -4,7 +4,6 @@ import java.io.InputStream;
import javax.inject.Inject;
import org.briarproject.api.crypto.StreamDecrypter;
import org.briarproject.api.crypto.StreamDecrypterFactory;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReaderFactory;
@@ -18,18 +17,15 @@ class StreamReaderFactoryImpl implements StreamReaderFactory {
this.streamDecrypterFactory = streamDecrypterFactory;
}
public InputStream createStreamReader(InputStream in, int maxFrameLength,
StreamContext ctx) {
StreamDecrypter s = streamDecrypterFactory.createStreamDecrypter(in,
maxFrameLength, ctx);
return new StreamReaderImpl(s, maxFrameLength);
public InputStream createStreamReader(InputStream in, StreamContext ctx) {
return new StreamReaderImpl(
streamDecrypterFactory.createStreamDecrypter(in, ctx));
}
public InputStream createInvitationStreamReader(InputStream in,
int maxFrameLength, byte[] secret, boolean alice) {
StreamDecrypter s =
byte[] secret, boolean alice) {
return new StreamReaderImpl(
streamDecrypterFactory.createInvitationStreamDecrypter(in,
maxFrameLength, secret, alice);
return new StreamReaderImpl(s, maxFrameLength);
secret, alice));
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.InputStream;
@@ -15,9 +16,9 @@ class StreamReaderImpl extends InputStream {
private int offset = 0, length = 0;
StreamReaderImpl(StreamDecrypter decrypter, int frameLength) {
StreamReaderImpl(StreamDecrypter decrypter) {
this.decrypter = decrypter;
payload = new byte[frameLength - HEADER_LENGTH - MAC_LENGTH];
payload = new byte[MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH];
}
@Override

View File

@@ -4,7 +4,6 @@ import java.io.OutputStream;
import javax.inject.Inject;
import org.briarproject.api.crypto.StreamEncrypter;
import org.briarproject.api.crypto.StreamEncrypterFactory;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamWriterFactory;
@@ -18,18 +17,16 @@ class StreamWriterFactoryImpl implements StreamWriterFactory {
this.streamEncrypterFactory = streamEncrypterFactory;
}
public OutputStream createStreamWriter(OutputStream out, int maxFrameLength,
public OutputStream createStreamWriter(OutputStream out,
StreamContext ctx) {
StreamEncrypter s = streamEncrypterFactory.createStreamEncrypter(out,
maxFrameLength, ctx);
return new StreamWriterImpl(s, maxFrameLength);
return new StreamWriterImpl(
streamEncrypterFactory.createStreamEncrypter(out, ctx));
}
public OutputStream createInvitationStreamWriter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice) {
StreamEncrypter s =
byte[] secret, boolean alice) {
return new StreamWriterImpl(
streamEncrypterFactory.createInvitationStreamEncrypter(out,
maxFrameLength, secret, alice);
return new StreamWriterImpl(s, maxFrameLength);
secret, alice));
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.OutputStream;
@@ -22,9 +23,9 @@ class StreamWriterImpl extends OutputStream {
private int length = 0;
StreamWriterImpl(StreamEncrypter encrypter, int maxFrameLength) {
StreamWriterImpl(StreamEncrypter encrypter) {
this.encrypter = encrypter;
payload = new byte[maxFrameLength - HEADER_LENGTH - MAC_LENGTH];
payload = new byte[MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH];
}
@Override