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

@@ -69,7 +69,7 @@ class DroidtoothPlugin implements DuplexPlugin {
private final SecureRandom secureRandom; private final SecureRandom secureRandom;
private final Clock clock; private final Clock clock;
private final DuplexPluginCallback callback; private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency, pollingInterval; private final int maxLatency, pollingInterval;
private volatile boolean running = false; private volatile boolean running = false;
private volatile boolean wasDisabled = false; private volatile boolean wasDisabled = false;
@@ -81,7 +81,7 @@ class DroidtoothPlugin implements DuplexPlugin {
DroidtoothPlugin(Executor ioExecutor, AndroidExecutor androidExecutor, DroidtoothPlugin(Executor ioExecutor, AndroidExecutor androidExecutor,
Context appContext, SecureRandom secureRandom, Clock clock, Context appContext, SecureRandom secureRandom, Clock clock,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency, DuplexPluginCallback callback, int maxLatency,
int pollingInterval) { int pollingInterval) {
this.ioExecutor = ioExecutor; this.ioExecutor = ioExecutor;
this.androidExecutor = androidExecutor; this.androidExecutor = androidExecutor;
@@ -89,7 +89,6 @@ class DroidtoothPlugin implements DuplexPlugin {
this.secureRandom = secureRandom; this.secureRandom = secureRandom;
this.clock = clock; this.clock = clock;
this.callback = callback; this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency; this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval; this.pollingInterval = pollingInterval;
} }
@@ -98,10 +97,6 @@ class DroidtoothPlugin implements DuplexPlugin {
return ID; return ID;
} }
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() { public int getMaxLatency() {
return maxLatency; return maxLatency;
} }

View File

@@ -15,7 +15,6 @@ import android.content.Context;
public class DroidtoothPluginFactory implements DuplexPluginFactory { public class DroidtoothPluginFactory 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_LATENCY = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -41,7 +40,6 @@ public class DroidtoothPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) { public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new DroidtoothPlugin(ioExecutor, androidExecutor, appContext, return new DroidtoothPlugin(ioExecutor, androidExecutor, appContext,
secureRandom, clock, callback, MAX_FRAME_LENGTH, MAX_LATENCY, secureRandom, clock, callback, MAX_LATENCY, POLLING_INTERVAL);
POLLING_INTERVAL);
} }
} }

View File

@@ -39,10 +39,6 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader { private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() { public long getMaxLatency() {
return plugin.getMaxLatency(); return plugin.getMaxLatency();
} }
@@ -60,10 +56,6 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter { private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() { public int getMaxLatency() {
return plugin.getMaxLatency(); return plugin.getMaxLatency();
} }

View File

@@ -26,10 +26,9 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
private volatile BroadcastReceiver networkStateReceiver = null; private volatile BroadcastReceiver networkStateReceiver = null;
AndroidLanTcpPlugin(Executor ioExecutor, Context appContext, AndroidLanTcpPlugin(Executor ioExecutor, Context appContext,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency, DuplexPluginCallback callback, int maxLatency,
int maxIdleTime, int pollingInterval) { int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxFrameLength, maxLatency, maxIdleTime, super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
pollingInterval);
this.appContext = appContext; this.appContext = appContext;
} }

View File

@@ -11,7 +11,6 @@ import android.content.Context;
public class AndroidLanTcpPluginFactory implements DuplexPluginFactory { public class AndroidLanTcpPluginFactory 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_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 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 private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -30,6 +29,6 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) { public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new AndroidLanTcpPlugin(ioExecutor, appContext, callback, return new AndroidLanTcpPlugin(ioExecutor, appContext, callback,
MAX_FRAME_LENGTH, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL); MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
} }
} }

View File

@@ -75,8 +75,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
private final Context appContext; private final Context appContext;
private final LocationUtils locationUtils; private final LocationUtils locationUtils;
private final DuplexPluginCallback callback; private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency, maxIdleTime, pollingInterval; private final int maxLatency, maxIdleTime, pollingInterval, socketTimeout;
private final int socketTimeout;
private final File torDirectory, torFile, geoIpFile, configFile, doneFile; private final File torDirectory, torFile, geoIpFile, configFile, doneFile;
private final File cookieFile, hostnameFile; private final File cookieFile, hostnameFile;
private final AtomicBoolean circuitBuilt; private final AtomicBoolean circuitBuilt;
@@ -90,13 +89,11 @@ class TorPlugin implements DuplexPlugin, EventHandler {
TorPlugin(Executor ioExecutor, Context appContext, TorPlugin(Executor ioExecutor, Context appContext,
LocationUtils locationUtils, DuplexPluginCallback callback, LocationUtils locationUtils, DuplexPluginCallback callback,
int maxFrameLength, int maxLatency, int maxIdleTime, int maxLatency, int maxIdleTime, int pollingInterval) {
int pollingInterval) {
this.ioExecutor = ioExecutor; this.ioExecutor = ioExecutor;
this.appContext = appContext; this.appContext = appContext;
this.locationUtils = locationUtils; this.locationUtils = locationUtils;
this.callback = callback; this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency; this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime; this.maxIdleTime = maxIdleTime;
this.pollingInterval = pollingInterval; this.pollingInterval = pollingInterval;
@@ -117,10 +114,6 @@ class TorPlugin implements DuplexPlugin, EventHandler {
return ID; return ID;
} }
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() { public int getMaxLatency() {
return maxLatency; return maxLatency;
} }

View File

@@ -17,7 +17,6 @@ public class TorPluginFactory implements DuplexPluginFactory {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(TorPluginFactory.class.getName()); Logger.getLogger(TorPluginFactory.class.getName());
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds 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 MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -44,6 +43,6 @@ public class TorPluginFactory implements DuplexPluginFactory {
return null; return null;
} }
return new TorPlugin(ioExecutor,appContext, locationUtils, callback, return new TorPlugin(ioExecutor,appContext, locationUtils, callback,
MAX_FRAME_LENGTH, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL); MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
} }
} }

View File

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

View File

@@ -2,7 +2,7 @@ package org.briarproject.api.crypto;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
/** An authenticated cipher that support additional authenticated data. */ /** An authenticated cipher that supports additional authenticated data. */
public interface AuthenticatedCipher { public interface AuthenticatedCipher {
/** /**

View File

@@ -7,12 +7,11 @@ import org.briarproject.api.transport.StreamContext;
public interface StreamDecrypterFactory { public interface StreamDecrypterFactory {
/** Creates a {@link StreamDecrypter} for decrypting a transport stream. */ /** Creates a {@link StreamDecrypter} for decrypting a transport stream. */
StreamDecrypter createStreamDecrypter(InputStream in, int maxFrameLength, StreamDecrypter createStreamDecrypter(InputStream in, StreamContext ctx);
StreamContext ctx);
/** /**
* Creates a {@link StreamDecrypter} for decrypting an invitation stream. * Creates a {@link StreamDecrypter} for decrypting an invitation stream.
*/ */
StreamDecrypter createInvitationStreamDecrypter(InputStream in, StreamDecrypter createInvitationStreamDecrypter(InputStream in,
int maxFrameLength, byte[] secret, boolean alice); byte[] secret, boolean alice);
} }

View File

@@ -7,12 +7,11 @@ import org.briarproject.api.transport.StreamContext;
public interface StreamEncrypterFactory { public interface StreamEncrypterFactory {
/** Creates a {@link StreamEncrypter} for encrypting a transport stream. */ /** Creates a {@link StreamEncrypter} for encrypting a transport stream. */
StreamEncrypter createStreamEncrypter(OutputStream out, StreamEncrypter createStreamEncrypter(OutputStream out, StreamContext ctx);
int maxFrameLength, StreamContext ctx);
/** /**
* Creates a {@link StreamEncrypter} for encrypting an invitation stream. * Creates a {@link StreamEncrypter} for encrypting an invitation stream.
*/ */
StreamEncrypter createInvitationStreamEncrypter(OutputStream out, StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice); byte[] secret, boolean alice);
} }

View File

@@ -11,9 +11,6 @@ public interface Plugin {
/** Returns the plugin's transport identifier. */ /** Returns the plugin's transport identifier. */
TransportId getId(); TransportId getId();
/** Returns the transport's maximum frame length in bytes. */
int getMaxFrameLength();
/** Returns the transport's maximum latency in milliseconds. */ /** Returns the transport's maximum latency in milliseconds. */
int getMaxLatency(); int getMaxLatency();

View File

@@ -9,9 +9,6 @@ import java.io.InputStream;
*/ */
public interface TransportConnectionReader { public interface TransportConnectionReader {
/** Returns the maximum frame length of the transport in bytes. */
int getMaxFrameLength();
/** Returns the maximum latency of the transport in milliseconds. */ /** Returns the maximum latency of the transport in milliseconds. */
long getMaxLatency(); long getMaxLatency();

View File

@@ -9,9 +9,6 @@ import java.io.OutputStream;
*/ */
public interface TransportConnectionWriter { public interface TransportConnectionWriter {
/** Returns the maximum frame length of the transport in bytes. */
int getMaxFrameLength();
/** Returns the maximum latency of the transport in milliseconds. */ /** Returns the maximum latency of the transport in milliseconds. */
int getMaxLatency(); int getMaxLatency();

View File

@@ -8,13 +8,12 @@ public interface StreamReaderFactory {
* Creates an {@link java.io.InputStream InputStream} for reading from a * Creates an {@link java.io.InputStream InputStream} for reading from a
* transport stream. * transport stream.
*/ */
InputStream createStreamReader(InputStream in, int maxFrameLength, InputStream createStreamReader(InputStream in, StreamContext ctx);
StreamContext ctx);
/** /**
* Creates an {@link java.io.InputStream InputStream} for reading from an * Creates an {@link java.io.InputStream InputStream} for reading from an
* invitation stream. * invitation stream.
*/ */
InputStream createInvitationStreamReader(InputStream in, InputStream createInvitationStreamReader(InputStream in,
int maxFrameLength, byte[] secret, boolean alice); byte[] secret, boolean alice);
} }

View File

@@ -8,13 +8,12 @@ public interface StreamWriterFactory {
* Creates an {@link java.io.OutputStream OutputStream} for writing to a * Creates an {@link java.io.OutputStream OutputStream} for writing to a
* transport stream * transport stream
*/ */
OutputStream createStreamWriter(OutputStream out, int maxFrameLength, OutputStream createStreamWriter(OutputStream out, StreamContext ctx);
StreamContext ctx);
/** /**
* Creates an {@link java.io.OutputStream OutputStream} for writing to an * Creates an {@link java.io.OutputStream OutputStream} for writing to an
* invitation stream. * invitation stream.
*/ */
OutputStream createInvitationStreamWriter(OutputStream out, OutputStream createInvitationStreamWriter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice); byte[] secret, boolean alice);
} }

View File

@@ -6,7 +6,7 @@ public interface TransportConstants {
int TAG_LENGTH = 16; int TAG_LENGTH = 16;
/** The maximum length of a frame in bytes, including the header and MAC. */ /** The maximum length of a frame in bytes, including the header and MAC. */
int MAX_FRAME_LENGTH = 32768; // 2^15, 32 KiB int MAX_FRAME_LENGTH = 1024;
/** The length of the initalisation vector (IV) in bytes. */ /** The length of the initalisation vector (IV) in bytes. */
int IV_LENGTH = 12; int IV_LENGTH = 12;

View File

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

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

View File

@@ -22,7 +22,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
} }
public StreamEncrypter createStreamEncrypter(OutputStream out, public StreamEncrypter createStreamEncrypter(OutputStream out,
int maxFrameLength, StreamContext ctx) { StreamContext ctx) {
byte[] secret = ctx.getSecret(); byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber(); long streamNumber = ctx.getStreamNumber();
boolean alice = ctx.getAlice(); boolean alice = ctx.getAlice();
@@ -34,15 +34,15 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice); SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the encrypter // Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey, return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, tag); tag);
} }
public StreamEncrypter createInvitationStreamEncrypter(OutputStream out, public StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice) { byte[] secret, boolean alice) {
// Derive the frame key // Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice); SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the encrypter // Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey, 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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_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.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED; import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import java.io.IOException; import java.io.IOException;
@@ -20,22 +21,20 @@ class StreamEncrypterImpl implements StreamEncrypter {
private final AuthenticatedCipher frameCipher; private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey; private final SecretKey frameKey;
private final byte[] tag, iv, aad, plaintext, ciphertext; private final byte[] tag, iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber; private long frameNumber;
private boolean writeTag; private boolean writeTag;
StreamEncrypterImpl(OutputStream out, AuthenticatedCipher frameCipher, StreamEncrypterImpl(OutputStream out, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength, byte[] tag) { SecretKey frameKey, byte[] tag) {
this.out = out; this.out = out;
this.frameCipher = frameCipher; this.frameCipher = frameCipher;
this.frameKey = frameKey; this.frameKey = frameKey;
this.frameLength = frameLength;
this.tag = tag; this.tag = tag;
iv = new byte[IV_LENGTH]; iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH]; aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH]; plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[frameLength]; ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0; frameNumber = 0;
writeTag = (tag != null); writeTag = (tag != null);
} }
@@ -54,8 +53,8 @@ class StreamEncrypterImpl implements StreamEncrypter {
plaintextLength = HEADER_LENGTH + payloadLength; plaintextLength = HEADER_LENGTH + payloadLength;
ciphertextLength = plaintextLength + MAC_LENGTH; ciphertextLength = plaintextLength + MAC_LENGTH;
} else { } else {
plaintextLength = frameLength - MAC_LENGTH; plaintextLength = MAX_FRAME_LENGTH - MAC_LENGTH;
ciphertextLength = frameLength; ciphertextLength = MAX_FRAME_LENGTH;
} }
// Encode the header // Encode the header
FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength); FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,7 +9,6 @@ import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
public class LanTcpPluginFactory implements 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_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 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 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) { public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new LanTcpPlugin(ioExecutor, callback, MAX_FRAME_LENGTH, return new LanTcpPlugin(ioExecutor, callback, MAX_LATENCY,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL); MAX_IDLE_TIME, POLLING_INTERVAL);
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

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

View File

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

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

View File

@@ -46,7 +46,7 @@ class BluetoothPlugin implements DuplexPlugin {
private final Clock clock; private final Clock clock;
private final SecureRandom secureRandom; private final SecureRandom secureRandom;
private final DuplexPluginCallback callback; private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency, pollingInterval; private final int maxLatency, pollingInterval;
private final Semaphore discoverySemaphore = new Semaphore(1); private final Semaphore discoverySemaphore = new Semaphore(1);
private volatile boolean running = false; private volatile boolean running = false;
@@ -54,13 +54,12 @@ class BluetoothPlugin implements DuplexPlugin {
private volatile LocalDevice localDevice = null; private volatile LocalDevice localDevice = null;
BluetoothPlugin(Executor ioExecutor, Clock clock, SecureRandom secureRandom, BluetoothPlugin(Executor ioExecutor, Clock clock, SecureRandom secureRandom,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency, DuplexPluginCallback callback, int maxLatency,
int pollingInterval) { int pollingInterval) {
this.ioExecutor = ioExecutor; this.ioExecutor = ioExecutor;
this.clock = clock; this.clock = clock;
this.secureRandom = secureRandom; this.secureRandom = secureRandom;
this.callback = callback; this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency; this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval; this.pollingInterval = pollingInterval;
} }
@@ -69,10 +68,6 @@ class BluetoothPlugin implements DuplexPlugin {
return ID; return ID;
} }
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() { public int getMaxLatency() {
return maxLatency; return maxLatency;
} }

View File

@@ -12,7 +12,6 @@ import org.briarproject.system.SystemClock;
public class BluetoothPluginFactory implements DuplexPluginFactory { public class BluetoothPluginFactory 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_LATENCY = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -33,6 +32,6 @@ public class BluetoothPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) { public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new BluetoothPlugin(ioExecutor, clock, secureRandom, callback, return new BluetoothPlugin(ioExecutor, clock, secureRandom, callback,
MAX_FRAME_LENGTH, MAX_LATENCY, POLLING_INTERVAL); MAX_LATENCY, POLLING_INTERVAL);
} }
} }

View File

@@ -39,10 +39,6 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader { private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() { public long getMaxLatency() {
return plugin.getMaxLatency(); return plugin.getMaxLatency();
} }
@@ -60,10 +56,6 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter { private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() { public int getMaxLatency() {
return plugin.getMaxLatency(); return plugin.getMaxLatency();
} }

View File

@@ -29,8 +29,8 @@ implements RemovableDriveMonitor.Callback {
RemovableDrivePlugin(Executor ioExecutor, FileUtils fileUtils, RemovableDrivePlugin(Executor ioExecutor, FileUtils fileUtils,
SimplexPluginCallback callback, RemovableDriveFinder finder, SimplexPluginCallback callback, RemovableDriveFinder finder,
RemovableDriveMonitor monitor, int maxFrameLength, int maxLatency) { RemovableDriveMonitor monitor, int maxLatency) {
super(ioExecutor, fileUtils, callback, maxFrameLength, maxLatency); super(ioExecutor, fileUtils, callback, maxLatency);
this.finder = finder; this.finder = finder;
this.monitor = monitor; this.monitor = monitor;
} }

View File

@@ -1,7 +1,5 @@
package org.briarproject.plugins.file; package org.briarproject.plugins.file;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import org.briarproject.api.TransportId; import org.briarproject.api.TransportId;
@@ -52,6 +50,6 @@ public class RemovableDrivePluginFactory implements SimplexPluginFactory {
return null; return null;
} }
return new RemovableDrivePlugin(ioExecutor, fileUtils, callback, return new RemovableDrivePlugin(ioExecutor, fileUtils, callback,
finder, monitor, MAX_FRAME_LENGTH, MAX_LATENCY); finder, monitor, MAX_LATENCY);
} }
} }

View File

@@ -32,17 +32,16 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private final ModemFactory modemFactory; private final ModemFactory modemFactory;
private final SerialPortList serialPortList; private final SerialPortList serialPortList;
private final DuplexPluginCallback callback; private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency; private final int maxLatency;
private volatile boolean running = false; private volatile boolean running = false;
private volatile Modem modem = null; private volatile Modem modem = null;
ModemPlugin(ModemFactory modemFactory, SerialPortList serialPortList, ModemPlugin(ModemFactory modemFactory, SerialPortList serialPortList,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency) { DuplexPluginCallback callback, int maxLatency) {
this.modemFactory = modemFactory; this.modemFactory = modemFactory;
this.serialPortList = serialPortList; this.serialPortList = serialPortList;
this.callback = callback; this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency; this.maxLatency = maxLatency;
} }
@@ -50,10 +49,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
return ID; return ID;
} }
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() { public int getMaxLatency() {
return maxLatency; return maxLatency;
} }
@@ -199,10 +194,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private class Reader implements TransportConnectionReader { private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return maxFrameLength;
}
public long getMaxLatency() { public long getMaxLatency() {
return maxLatency; return maxLatency;
} }
@@ -219,10 +210,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private class Writer implements TransportConnectionWriter { private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return getMaxFrameLength();
}
public int getMaxLatency() { public int getMaxLatency() {
return getMaxLatency(); return getMaxLatency();
} }

View File

@@ -11,7 +11,6 @@ import org.briarproject.util.StringUtils;
public class ModemPluginFactory implements DuplexPluginFactory { public class ModemPluginFactory 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_LATENCY = 30 * 1000; // 30 seconds
private final ModemFactory modemFactory; private final ModemFactory modemFactory;
@@ -32,6 +31,6 @@ public class ModemPluginFactory implements DuplexPluginFactory {
String enabled = callback.getConfig().get("enabled"); String enabled = callback.getConfig().get("enabled");
if(StringUtils.isNullOrEmpty(enabled)) return null; if(StringUtils.isNullOrEmpty(enabled)) return null;
return new ModemPlugin(modemFactory, serialPortList, callback, return new ModemPlugin(modemFactory, serialPortList, callback,
MAX_FRAME_LENGTH, MAX_LATENCY); MAX_LATENCY);
} }
} }

View File

@@ -1,6 +1,5 @@
package org.briarproject; package org.briarproject;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
@@ -118,8 +117,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamContext ctx = new StreamContext(contactId, transportId, secret, StreamContext ctx = new StreamContext(contactId, transportId, secret,
0, true); 0, true);
OutputStream streamWriter = streamWriterFactory.createStreamWriter(out, OutputStream streamWriter =
MAX_FRAME_LENGTH, ctx); streamWriterFactory.createStreamWriter(out, ctx);
PacketWriter packetWriter = packetWriterFactory.createPacketWriter( PacketWriter packetWriter = packetWriterFactory.createPacketWriter(
streamWriter); streamWriter);
@@ -150,8 +149,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
// FIXME: Check that the expected tag was received // FIXME: Check that the expected tag was received
StreamContext ctx = new StreamContext(contactId, transportId, secret, StreamContext ctx = new StreamContext(contactId, transportId, secret,
0, false); 0, false);
InputStream streamReader = streamReaderFactory.createStreamReader(in, InputStream streamReader =
MAX_FRAME_LENGTH, ctx); streamReaderFactory.createStreamReader(in, ctx);
PacketReader packetReader = packetReaderFactory.createPacketReader( PacketReader packetReader = packetReaderFactory.createPacketReader(
streamReader); streamReader);

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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_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.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -14,6 +15,7 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.crypto.AuthenticatedCipher; import org.briarproject.api.crypto.AuthenticatedCipher;
import org.briarproject.api.crypto.CryptoComponent; import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.crypto.SecretKey;
import org.briarproject.util.ByteUtils;
import org.junit.Test; import org.junit.Test;
import com.google.inject.Guice; import com.google.inject.Guice;
@@ -23,9 +25,8 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// FIXME: This is an integration test, not a unit test // FIXME: This is an integration test, not a unit test
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH = private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH; MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
private final CryptoComponent crypto; private final CryptoComponent crypto;
private final AuthenticatedCipher frameCipher; private final AuthenticatedCipher frameCipher;
@@ -42,16 +43,16 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testReadValidFrames() throws Exception { public void testReadValidFrames() throws Exception {
// Generate two valid frames // Generate two valid frames
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false); byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
byte[] frame1 = generateFrame(1, FRAME_LENGTH, 123, false, false); byte[] frame1 = generateFrame(1, MAX_FRAME_LENGTH, 123, false, false);
// Concatenate the frames // Concatenate the frames
byte[] valid = new byte[FRAME_LENGTH * 2]; byte[] valid = new byte[MAX_FRAME_LENGTH * 2];
System.arraycopy(frame, 0, valid, 0, FRAME_LENGTH); System.arraycopy(frame, 0, valid, 0, MAX_FRAME_LENGTH);
System.arraycopy(frame1, 0, valid, FRAME_LENGTH, FRAME_LENGTH); System.arraycopy(frame1, 0, valid, MAX_FRAME_LENGTH, MAX_FRAME_LENGTH);
// Read the frames // Read the frames
ByteArrayInputStream in = new ByteArrayInputStream(valid); ByteArrayInputStream in = new ByteArrayInputStream(valid);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
byte[] payload = new byte[MAX_PAYLOAD_LENGTH]; byte[] payload = new byte[MAX_PAYLOAD_LENGTH];
assertEquals(123, i.readFrame(payload)); assertEquals(123, i.readFrame(payload));
assertEquals(123, i.readFrame(payload)); assertEquals(123, i.readFrame(payload));
@@ -60,14 +61,14 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testTruncatedFrameThrowsException() throws Exception { public void testTruncatedFrameThrowsException() throws Exception {
// Generate a valid frame // Generate a valid frame
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false); byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
// Chop off the last byte // Chop off the last byte
byte[] truncated = new byte[FRAME_LENGTH - 1]; byte[] truncated = new byte[MAX_FRAME_LENGTH - 1];
System.arraycopy(frame, 0, truncated, 0, FRAME_LENGTH - 1); System.arraycopy(frame, 0, truncated, 0, MAX_FRAME_LENGTH - 1);
// Try to read the frame, which should fail due to truncation // Try to read the frame, which should fail due to truncation
ByteArrayInputStream in = new ByteArrayInputStream(truncated); ByteArrayInputStream in = new ByteArrayInputStream(truncated);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
try { try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]); i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail(); fail();
@@ -77,13 +78,13 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testModifiedFrameThrowsException() throws Exception { public void testModifiedFrameThrowsException() throws Exception {
// Generate a valid frame // Generate a valid frame
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false); byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
// Modify a randomly chosen byte of the frame // Modify a randomly chosen byte of the frame
frame[(int) (Math.random() * FRAME_LENGTH)] ^= 1; frame[(int) (Math.random() * MAX_FRAME_LENGTH)] ^= 1;
// Try to read the frame, which should fail due to modification // Try to read the frame, which should fail due to modification
ByteArrayInputStream in = new ByteArrayInputStream(frame); ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
try { try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]); i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail(); fail();
@@ -93,11 +94,12 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testShortNonFinalFrameThrowsException() throws Exception { public void testShortNonFinalFrameThrowsException() throws Exception {
// Generate a short non-final frame // Generate a short non-final frame
byte[] frame = generateFrame(0, FRAME_LENGTH - 1, 123, false, false); byte[] frame = generateFrame(0, MAX_FRAME_LENGTH - 1, 123, false,
false);
// Try to read the frame, which should fail due to invalid length // Try to read the frame, which should fail due to invalid length
ByteArrayInputStream in = new ByteArrayInputStream(frame); ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
try { try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]); i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail(); fail();
@@ -107,11 +109,11 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testShortFinalFrameDoesNotThrowException() throws Exception { public void testShortFinalFrameDoesNotThrowException() throws Exception {
// Generate a short final frame // Generate a short final frame
byte[] frame = generateFrame(0, FRAME_LENGTH - 1, 123, true, false); byte[] frame = generateFrame(0, MAX_FRAME_LENGTH - 1, 123, true, false);
// Read the frame // Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame); ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
int length = i.readFrame(new byte[MAX_PAYLOAD_LENGTH]); int length = i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
assertEquals(123, length); assertEquals(123, length);
} }
@@ -119,12 +121,12 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testInvalidPayloadLengthThrowsException() throws Exception { public void testInvalidPayloadLengthThrowsException() throws Exception {
// Generate a frame with an invalid payload length // Generate a frame with an invalid payload length
byte[] frame = generateFrame(0, FRAME_LENGTH, MAX_PAYLOAD_LENGTH + 1, byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
false, false); ByteUtils.writeUint16(MAX_PAYLOAD_LENGTH + 1, frame, 0);
// Try to read the frame, which should fail due to invalid length // Try to read the frame, which should fail due to invalid length
ByteArrayInputStream in = new ByteArrayInputStream(frame); ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
try { try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]); i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail(); fail();
@@ -134,11 +136,11 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testNonZeroPaddingThrowsException() throws Exception { public void testNonZeroPaddingThrowsException() throws Exception {
// Generate a frame with bad padding // Generate a frame with bad padding
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, true); byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, true);
// Try to read the frame, which should fail due to bad padding // Try to read the frame, which should fail due to bad padding
ByteArrayInputStream in = new ByteArrayInputStream(frame); ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
try { try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]); i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail(); fail();
@@ -148,17 +150,18 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test @Test
public void testCannotReadBeyondFinalFrame() throws Exception { public void testCannotReadBeyondFinalFrame() throws Exception {
// Generate a valid final frame and another valid final frame after it // Generate a valid final frame and another valid final frame after it
byte[] frame = generateFrame(0, FRAME_LENGTH, MAX_PAYLOAD_LENGTH, true, byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, MAX_PAYLOAD_LENGTH,
false); true, false);
byte[] frame1 = generateFrame(1, FRAME_LENGTH, 123, true, false); byte[] frame1 = generateFrame(1, MAX_FRAME_LENGTH, 123, true, false);
// Concatenate the frames // Concatenate the frames
byte[] extraFrame = new byte[FRAME_LENGTH * 2]; byte[] extraFrame = new byte[MAX_FRAME_LENGTH * 2];
System.arraycopy(frame, 0, extraFrame, 0, FRAME_LENGTH); System.arraycopy(frame, 0, extraFrame, 0, MAX_FRAME_LENGTH);
System.arraycopy(frame1, 0, extraFrame, FRAME_LENGTH, FRAME_LENGTH); System.arraycopy(frame1, 0, extraFrame, MAX_FRAME_LENGTH,
MAX_FRAME_LENGTH);
// Read the final frame, which should first read the tag // Read the final frame, which should first read the tag
ByteArrayInputStream in = new ByteArrayInputStream(extraFrame); ByteArrayInputStream in = new ByteArrayInputStream(extraFrame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher, StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH); frameKey);
byte[] payload = new byte[MAX_PAYLOAD_LENGTH]; byte[] payload = new byte[MAX_PAYLOAD_LENGTH];
assertEquals(MAX_PAYLOAD_LENGTH, i.readFrame(payload)); assertEquals(MAX_PAYLOAD_LENGTH, i.readFrame(payload));
// The frame after the final frame should not be read // The frame after the final frame should not be read

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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_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.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -24,8 +25,6 @@ public class StreamEncrypterImplTest extends BriarTestCase {
// FIXME: This is an integration test, not a unit test // FIXME: This is an integration test, not a unit test
private static final int FRAME_LENGTH = 1024;
private final CryptoComponent crypto; private final CryptoComponent crypto;
private final AuthenticatedCipher frameCipher; private final AuthenticatedCipher frameCipher;
@@ -40,8 +39,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
public void testEncryptionWithoutTag() throws Exception { public void testEncryptionWithoutTag() throws Exception {
int payloadLength = 123; int payloadLength = 123;
byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH]; byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH];
byte[] plaintext = new byte[FRAME_LENGTH - MAC_LENGTH]; byte[] plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[FRAME_LENGTH]; byte[] ciphertext = new byte[MAX_FRAME_LENGTH];
SecretKey frameKey = crypto.generateSecretKey(); SecretKey frameKey = crypto.generateSecretKey();
// Calculate the expected ciphertext // Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0); FrameEncoder.encodeIv(iv, 0);
@@ -51,12 +50,12 @@ public class StreamEncrypterImplTest extends BriarTestCase {
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0); frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual ciphertext matches what's expected // Check that the actual ciphertext matches what's expected
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypterImpl o = new StreamEncrypterImpl(out, StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameCipher, frameKey, FRAME_LENGTH, null); frameKey, null);
o.writeFrame(new byte[payloadLength], payloadLength, false); o.writeFrame(new byte[payloadLength], payloadLength, false);
byte[] actual = out.toByteArray(); byte[] actual = out.toByteArray();
assertEquals(FRAME_LENGTH, actual.length); assertEquals(MAX_FRAME_LENGTH, actual.length);
for(int i = 0; i < FRAME_LENGTH; i++) for(int i = 0; i < MAX_FRAME_LENGTH; i++)
assertEquals(ciphertext[i], actual[i]); assertEquals(ciphertext[i], actual[i]);
} }
@@ -66,8 +65,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
new Random().nextBytes(tag); new Random().nextBytes(tag);
int payloadLength = 123; int payloadLength = 123;
byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH]; byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH];
byte[] plaintext = new byte[FRAME_LENGTH - MAC_LENGTH]; byte[] plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[FRAME_LENGTH]; byte[] ciphertext = new byte[MAX_FRAME_LENGTH];
SecretKey frameKey = crypto.generateSecretKey(); SecretKey frameKey = crypto.generateSecretKey();
// Calculate the expected ciphertext // Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0); FrameEncoder.encodeIv(iv, 0);
@@ -77,13 +76,13 @@ public class StreamEncrypterImplTest extends BriarTestCase {
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0); frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual tag and ciphertext match what's expected // Check that the actual tag and ciphertext match what's expected
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypterImpl o = new StreamEncrypterImpl(out, StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameCipher, frameKey, FRAME_LENGTH, tag); frameKey, tag);
o.writeFrame(new byte[payloadLength], payloadLength, false); o.writeFrame(new byte[payloadLength], payloadLength, false);
byte[] actual = out.toByteArray(); byte[] actual = out.toByteArray();
assertEquals(TAG_LENGTH + FRAME_LENGTH, actual.length); assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH, actual.length);
for(int i = 0; i < TAG_LENGTH; i++) assertEquals(tag[i], actual[i]); for(int i = 0; i < TAG_LENGTH; i++) assertEquals(tag[i], actual[i]);
for(int i = 0; i < FRAME_LENGTH; i++) for(int i = 0; i < MAX_FRAME_LENGTH; i++)
assertEquals(ciphertext[i], actual[TAG_LENGTH + i]); assertEquals(ciphertext[i], actual[TAG_LENGTH + i]);
} }
@@ -93,10 +92,10 @@ public class StreamEncrypterImplTest extends BriarTestCase {
new Random().nextBytes(tag); new Random().nextBytes(tag);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
// Initiator's constructor // Initiator's constructor
StreamEncrypterImpl o = new StreamEncrypterImpl(out, StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameCipher, crypto.generateSecretKey(), FRAME_LENGTH, tag); crypto.generateSecretKey(), tag);
// Write an empty final frame without having written any other frames // Write an empty final frame without having written any other frames
o.writeFrame(new byte[FRAME_LENGTH - MAC_LENGTH], 0, true); o.writeFrame(new byte[MAX_FRAME_LENGTH - MAC_LENGTH], 0, true);
// The tag and the empty frame should be written to the output stream // The tag and the empty frame should be written to the output stream
assertEquals(TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH, out.size()); assertEquals(TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH, out.size());
} }

View File

@@ -3,7 +3,6 @@ package org.briarproject.messaging;
import static org.briarproject.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH; import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -143,8 +142,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamWriterFactory streamWriterFactory = StreamWriterFactory streamWriterFactory =
alice.getInstance(StreamWriterFactory.class); alice.getInstance(StreamWriterFactory.class);
OutputStream streamWriter = streamWriterFactory.createStreamWriter(out, OutputStream streamWriter =
MAX_FRAME_LENGTH, ctx); streamWriterFactory.createStreamWriter(out, ctx);
// Create an outgoing messaging session // Create an outgoing messaging session
EventBus eventBus = alice.getInstance(EventBus.class); EventBus eventBus = alice.getInstance(EventBus.class);
PacketWriterFactory packetWriterFactory = PacketWriterFactory packetWriterFactory =
@@ -205,8 +204,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Create a stream reader // Create a stream reader
StreamReaderFactory streamReaderFactory = StreamReaderFactory streamReaderFactory =
bob.getInstance(StreamReaderFactory.class); bob.getInstance(StreamReaderFactory.class);
InputStream streamReader = streamReaderFactory.createStreamReader(in, InputStream streamReader =
MAX_FRAME_LENGTH, ctx); streamReaderFactory.createStreamReader(in, ctx);
// Create an incoming messaging session // Create an incoming messaging session
EventBus eventBus = bob.getInstance(EventBus.class); EventBus eventBus = bob.getInstance(EventBus.class);
MessageVerifier messageVerifier = MessageVerifier messageVerifier =

View File

@@ -23,12 +23,12 @@ public class BluetoothClientTest extends DuplexClientTest {
p.put("address", serverAddress); p.put("address", serverAddress);
p.put("uuid", BluetoothTest.EMPTY_UUID); p.put("uuid", BluetoothTest.EMPTY_UUID);
Map<ContactId, TransportProperties> remote = Map<ContactId, TransportProperties> remote =
Collections.singletonMap(contactId, p); Collections.singletonMap(contactId, p);
// Create the plugin // Create the plugin
callback = new ClientCallback(new TransportConfig(), callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote); new TransportProperties(), remote);
plugin = new BluetoothPlugin(executor, new SystemClock(), plugin = new BluetoothPlugin(executor, new SystemClock(),
new SecureRandom(), callback, 0, 0, 0); new SecureRandom(), callback, 0, 0);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@@ -23,7 +23,7 @@ public class BluetoothServerTest extends DuplexServerTest {
callback = new ServerCallback(new TransportConfig(), local, callback = new ServerCallback(new TransportConfig(), local,
Collections.singletonMap(contactId, new TransportProperties())); Collections.singletonMap(contactId, new TransportProperties()));
plugin = new BluetoothPlugin(executor, new SystemClock(), plugin = new BluetoothPlugin(executor, new SystemClock(),
new SecureRandom(), callback, 0, 0, 0); new SecureRandom(), callback, 0, 0);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@@ -1,6 +1,5 @@
package org.briarproject.plugins.file; package org.briarproject.plugins.file;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MIN_STREAM_LENGTH; import static org.briarproject.api.transport.TransportConstants.MIN_STREAM_LENGTH;
import java.io.File; import java.io.File;
@@ -58,7 +57,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -93,7 +92,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -130,7 +129,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -169,7 +168,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -208,7 +207,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
assertNotNull(plugin.createWriter(contactId)); assertNotNull(plugin.createWriter(contactId));
@@ -251,7 +250,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
TransportConnectionWriter writer = plugin.createWriter(contactId); TransportConnectionWriter writer = plugin.createWriter(contactId);
@@ -290,7 +289,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
plugin.start(); plugin.start();
plugin.driveInserted(testDir); plugin.driveInserted(testDir);
@@ -310,7 +309,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
context.mock(RemovableDriveMonitor.class); context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0); fileUtils, callback, finder, monitor, 0);
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat")); assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat")); assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
@@ -339,7 +338,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin( RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), fileUtils, callback, finder, monitor, new ImmediateExecutor(), fileUtils, callback, finder, monitor,
MAX_FRAME_LENGTH, 0); 0);
plugin.start(); plugin.start();
File f = new File(testDir, "abcdefgh.dat"); File f = new File(testDir, "abcdefgh.dat");

View File

@@ -24,7 +24,7 @@ public class ModemPluginTest extends BriarTestCase {
final SerialPortList serialPortList = final SerialPortList serialPortList =
context.mock(SerialPortList.class); context.mock(SerialPortList.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory, final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, null, 0, 0); serialPortList, null, 0);
final Modem modem = context.mock(Modem.class); final Modem modem = context.mock(Modem.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(serialPortList).getPortNames(); oneOf(serialPortList).getPortNames();
@@ -58,7 +58,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback = final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class); context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory, final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, callback, 0, 0); serialPortList, callback, 0);
final Modem modem = context.mock(Modem.class); final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties(); final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336); local.put("iso3166", ISO_1336);
@@ -99,7 +99,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback = final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class); context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory, final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, callback, 0, 0); serialPortList, callback, 0);
final Modem modem = context.mock(Modem.class); final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties(); final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336); local.put("iso3166", ISO_1336);
@@ -140,7 +140,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback = final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class); context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory, final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, callback, 0, 0); serialPortList, callback, 0);
final Modem modem = context.mock(Modem.class); final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties(); final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336); local.put("iso3166", ISO_1336);

View File

@@ -15,7 +15,6 @@ import org.briarproject.plugins.DuplexClientTest;
// is running on another machine // is running on another machine
public class LanTcpClientTest extends DuplexClientTest { public class LanTcpClientTest extends DuplexClientTest {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 60 * 1000; private static final int MAX_LATENCY = 60 * 1000;
private static final int MAX_IDLE_TIME = 30 * 1000; private static final int MAX_IDLE_TIME = 30 * 1000;
private static final int POLLING_INTERVAL = 60 * 1000; private static final int POLLING_INTERVAL = 60 * 1000;
@@ -31,8 +30,8 @@ public class LanTcpClientTest extends DuplexClientTest {
// Create the plugin // Create the plugin
callback = new ClientCallback(new TransportConfig(), callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote); new TransportProperties(), remote);
plugin = new LanTcpPlugin(executor, callback, MAX_FRAME_LENGTH, plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL); MAX_IDLE_TIME, POLLING_INTERVAL);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@@ -32,7 +32,7 @@ public class LanTcpPluginTest extends BriarTestCase {
@Test @Test
public void testAddressesAreOnSameLan() { public void testAddressesAreOnSameLan() {
LanTcpPlugin plugin = new LanTcpPlugin(null, null, 0, 0, 0, 0); LanTcpPlugin plugin = new LanTcpPlugin(null, null, 0, 0, 0);
// Local and remote in 10.0.0.0/8 should return true // Local and remote in 10.0.0.0/8 should return true
assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0), assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
makeAddress(10, 255, 255, 255))); makeAddress(10, 255, 255, 255)));
@@ -81,7 +81,7 @@ public class LanTcpPluginTest extends BriarTestCase {
} }
Callback callback = new Callback(); Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool(); Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0, 0); DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0);
plugin.start(); plugin.start();
// The plugin should have bound a socket and stored the port number // The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS)); assertTrue(callback.propertiesLatch.await(5, SECONDS));
@@ -113,7 +113,7 @@ public class LanTcpPluginTest extends BriarTestCase {
} }
Callback callback = new Callback(); Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool(); Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0, 0); DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0);
plugin.start(); plugin.start();
// The plugin should have bound a socket and stored the port number // The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS)); assertTrue(callback.propertiesLatch.await(5, SECONDS));

View File

@@ -13,7 +13,6 @@ import org.briarproject.plugins.DuplexServerTest;
// is running on another machine // is running on another machine
public class LanTcpServerTest extends DuplexServerTest { public class LanTcpServerTest extends DuplexServerTest {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 60 * 1000; private static final int MAX_LATENCY = 60 * 1000;
private static final int MAX_IDLE_TIME = 30 * 1000; private static final int MAX_IDLE_TIME = 30 * 1000;
private static final int POLLING_INTERVAL = 60 * 1000; private static final int POLLING_INTERVAL = 60 * 1000;
@@ -22,8 +21,8 @@ public class LanTcpServerTest extends DuplexServerTest {
callback = new ServerCallback(new TransportConfig(), callback = new ServerCallback(new TransportConfig(),
new TransportProperties(), new TransportProperties(),
Collections.singletonMap(contactId, new TransportProperties())); Collections.singletonMap(contactId, new TransportProperties()));
plugin = new LanTcpPlugin(executor, callback, MAX_FRAME_LENGTH, plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL); MAX_IDLE_TIME, POLLING_INTERVAL);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH; import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import org.briarproject.BriarTestCase; import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamDecrypter; import org.briarproject.api.crypto.StreamDecrypter;
@@ -11,9 +12,8 @@ import org.junit.Test;
public class StreamReaderImplTest extends BriarTestCase { public class StreamReaderImplTest extends BriarTestCase {
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH = private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH; MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
@Test @Test
public void testEmptyFramesAreSkipped() throws Exception { public void testEmptyFramesAreSkipped() throws Exception {
@@ -29,7 +29,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class))); oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames will(returnValue(-1)); // No more frames
}}); }});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH); StreamReaderImpl r = new StreamReaderImpl(decrypter);
assertEquals(0, r.read()); // Skip the first empty frame, read a byte assertEquals(0, r.read()); // Skip the first empty frame, read a byte
assertEquals(0, r.read()); // Read another byte assertEquals(0, r.read()); // Read another byte
assertEquals(-1, r.read()); // Skip the second empty frame, reach EOF assertEquals(-1, r.read()); // Skip the second empty frame, reach EOF
@@ -52,7 +52,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class))); oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames will(returnValue(-1)); // No more frames
}}); }});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH); StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH]; byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Skip the first empty frame, read the two payload bytes // Skip the first empty frame, read the two payload bytes
assertEquals(2, r.read(buf)); assertEquals(2, r.read(buf));
@@ -74,7 +74,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class))); oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames will(returnValue(-1)); // No more frames
}}); }});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH); StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH / 2]; byte[] buf = new byte[MAX_PAYLOAD_LENGTH / 2];
// Read the first half of the payload // Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf)); assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf));
@@ -96,7 +96,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class))); oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames will(returnValue(-1)); // No more frames
}}); }});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH); StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH]; byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Read the first half of the payload // Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf, MAX_PAYLOAD_LENGTH / 2, assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf, MAX_PAYLOAD_LENGTH / 2,

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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH; import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import org.briarproject.BriarTestCase; import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamEncrypter; import org.briarproject.api.crypto.StreamEncrypter;
@@ -11,9 +12,8 @@ import org.junit.Test;
public class StreamWriterImplTest extends BriarTestCase { public class StreamWriterImplTest extends BriarTestCase {
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH = private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH; MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
@Test @Test
public void testCloseWithoutWritingWritesFinalFrame() throws Exception { public void testCloseWithoutWritingWritesFinalFrame() throws Exception {
@@ -26,7 +26,7 @@ public class StreamWriterImplTest extends BriarTestCase {
// Flush the stream // Flush the stream
oneOf(encrypter).flush(); oneOf(encrypter).flush();
}}); }});
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH); StreamWriterImpl w = new StreamWriterImpl(encrypter);
w.close(); w.close();
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -36,7 +36,7 @@ public class StreamWriterImplTest extends BriarTestCase {
throws Exception { throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class); final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH); StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Write a non-final frame with an empty payload // Write a non-final frame with an empty payload
oneOf(encrypter).writeFrame(with(any(byte[].class)), with(0), oneOf(encrypter).writeFrame(with(any(byte[].class)), with(0),
@@ -63,7 +63,7 @@ public class StreamWriterImplTest extends BriarTestCase {
throws Exception { throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class); final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH); StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Write a non-final frame with one payload byte // Write a non-final frame with one payload byte
oneOf(encrypter).writeFrame(with(any(byte[].class)), with(1), oneOf(encrypter).writeFrame(with(any(byte[].class)), with(1),
@@ -90,7 +90,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testSingleByteWritesWriteFullFrame() throws Exception { public void testSingleByteWritesWriteFullFrame() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class); final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH); StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Write a full non-final frame // Write a full non-final frame
oneOf(encrypter).writeFrame(with(any(byte[].class)), oneOf(encrypter).writeFrame(with(any(byte[].class)),
@@ -116,7 +116,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testMultiByteWritesWriteFullFrames() throws Exception { public void testMultiByteWritesWriteFullFrames() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class); final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH); StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Write two full non-final frames // Write two full non-final frames
exactly(2).of(encrypter).writeFrame(with(any(byte[].class)), exactly(2).of(encrypter).writeFrame(with(any(byte[].class)),
@@ -147,7 +147,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testLargeMultiByteWriteWritesFullFrames() throws Exception { public void testLargeMultiByteWriteWritesFullFrames() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class); final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH); StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Write two full non-final frames // Write two full non-final frames
exactly(2).of(encrypter).writeFrame(with(any(byte[].class)), exactly(2).of(encrypter).writeFrame(with(any(byte[].class)),

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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_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.EOFException;
import java.io.IOException; import java.io.IOException;
@@ -16,9 +17,9 @@ class TestStreamDecrypter implements StreamDecrypter {
private final InputStream in; private final InputStream in;
private final byte[] frame; private final byte[] frame;
TestStreamDecrypter(InputStream in, int frameLength) { TestStreamDecrypter(InputStream in) {
this.in = in; this.in = in;
frame = new byte[frameLength]; frame = new byte[MAX_FRAME_LENGTH];
} }
public int readFrame(byte[] payload) throws IOException { public int readFrame(byte[] payload) throws IOException {

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.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_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.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@@ -16,10 +17,10 @@ class TestStreamEncrypter implements StreamEncrypter {
private boolean writeTag = true; private boolean writeTag = true;
TestStreamEncrypter(OutputStream out, int frameLength, byte[] tag) { TestStreamEncrypter(OutputStream out, byte[] tag) {
this.out = out; this.out = out;
this.tag = tag; this.tag = tag;
frame = new byte[frameLength]; frame = new byte[MAX_FRAME_LENGTH];
} }
public void writeFrame(byte[] payload, int payloadLength, public void writeFrame(byte[] payload, int payloadLength,

View File

@@ -1,5 +1,6 @@
package org.briarproject.transport; package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
@@ -17,8 +18,6 @@ import org.junit.Test;
public class TransportIntegrationTest extends BriarTestCase { public class TransportIntegrationTest extends BriarTestCase {
private final int FRAME_LENGTH = 2048;
private final Random random; private final Random random;
public TransportIntegrationTest() { public TransportIntegrationTest() {
@@ -40,31 +39,28 @@ public class TransportIntegrationTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH]; byte[] tag = new byte[TAG_LENGTH];
random.nextBytes(tag); random.nextBytes(tag);
// Generate two frames with random payloads // Generate two frames with random payloads
byte[] payload1 = new byte[1234]; byte[] payload1 = new byte[123];
random.nextBytes(payload1); random.nextBytes(payload1);
byte[] payload2 = new byte[321]; byte[] payload2 = new byte[321];
random.nextBytes(payload2); random.nextBytes(payload2);
// Write the tag and the frames // Write the tag and the frames
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypter encrypter = new TestStreamEncrypter(out, FRAME_LENGTH, StreamEncrypter encrypter = new TestStreamEncrypter(out, tag);
tag); OutputStream streamWriter = new StreamWriterImpl(encrypter);
OutputStream streamWriter = new StreamWriterImpl(encrypter,
FRAME_LENGTH);
streamWriter.write(payload1); streamWriter.write(payload1);
streamWriter.flush(); streamWriter.flush();
streamWriter.write(payload2); streamWriter.write(payload2);
streamWriter.flush(); streamWriter.flush();
byte[] output = out.toByteArray(); byte[] output = out.toByteArray();
assertEquals(TAG_LENGTH + FRAME_LENGTH * 2, output.length); assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH * 2, output.length);
// Read the tag back // Read the tag back
ByteArrayInputStream in = new ByteArrayInputStream(output); ByteArrayInputStream in = new ByteArrayInputStream(output);
byte[] recoveredTag = new byte[tag.length]; byte[] recoveredTag = new byte[tag.length];
read(in, recoveredTag); read(in, recoveredTag);
assertArrayEquals(tag, recoveredTag); assertArrayEquals(tag, recoveredTag);
// Read the frames back // Read the frames back
StreamDecrypter decrypter = new TestStreamDecrypter(in, FRAME_LENGTH); StreamDecrypter decrypter = new TestStreamDecrypter(in);
InputStream streamReader = new StreamReaderImpl(decrypter, InputStream streamReader = new StreamReaderImpl(decrypter);
FRAME_LENGTH);
byte[] recoveredPayload1 = new byte[payload1.length]; byte[] recoveredPayload1 = new byte[payload1.length];
read(streamReader, recoveredPayload1); read(streamReader, recoveredPayload1);
assertArrayEquals(payload1, recoveredPayload1); assertArrayEquals(payload1, recoveredPayload1);