Connection readers and writers don't need the connection context.

This commit is contained in:
akwizgran
2011-12-02 14:23:45 +00:00
parent 51d58fadad
commit c8338f9866
10 changed files with 35 additions and 64 deletions

View File

@@ -7,7 +7,6 @@ import javax.crypto.Mac;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.util.ByteUtils;
@@ -24,25 +23,24 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
}
public ConnectionReader createConnectionReader(InputStream in,
ConnectionContext ctx, byte[] tag) {
byte[] secret, byte[] tag) {
// Validate the tag
Cipher tagCipher = crypto.getTagCipher();
ErasableKey tagKey = crypto.deriveTagKey(ctx.getSecret(), true);
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
boolean valid = TagEncoder.validateTag(tag, 0, tagCipher, tagKey);
tagKey.erase();
if(!valid) throw new IllegalArgumentException();
return createConnectionReader(in, true, ctx);
return createConnectionReader(in, true, secret);
}
public ConnectionReader createConnectionReader(InputStream in,
ConnectionContext ctx) {
return createConnectionReader(in, false, ctx);
byte[] secret) {
return createConnectionReader(in, false, secret);
}
private ConnectionReader createConnectionReader(InputStream in,
boolean initiator, ConnectionContext ctx) {
boolean initiator, byte[] secret) {
// Derive the keys and erase the secret
byte[] secret = ctx.getSecret();
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
ByteUtils.erase(secret);

View File

@@ -7,7 +7,6 @@ import javax.crypto.Mac;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.util.ByteUtils;
@@ -24,25 +23,24 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
}
public ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, ConnectionContext ctx) {
return createConnectionWriter(out, capacity, true, ctx);
long capacity, byte[] secret) {
return createConnectionWriter(out, capacity, true, secret);
}
public ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, ConnectionContext ctx, byte[] tag) {
long capacity, byte[] secret, byte[] tag) {
// Decrypt the tag
Cipher tagCipher = crypto.getTagCipher();
ErasableKey tagKey = crypto.deriveTagKey(ctx.getSecret(), true);
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
boolean valid = TagEncoder.validateTag(tag, 0, tagCipher, tagKey);
tagKey.erase();
if(!valid) throw new IllegalArgumentException();
return createConnectionWriter(out, capacity, false, ctx);
return createConnectionWriter(out, capacity, false, secret);
}
private ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, boolean initiator, ConnectionContext ctx) {
long capacity, boolean initiator, byte[] secret) {
// Derive the keys and erase the secret
byte[] secret = ctx.getSecret();
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);

View File

@@ -45,7 +45,7 @@ class IncomingBatchConnection {
void read() {
try {
ConnectionReader conn = connFactory.createConnectionReader(
reader.getInputStream(), ctx, tag);
reader.getInputStream(), ctx.getSecret(), tag);
ProtocolReader proto = protoFactory.createProtocolReader(
conn.getInputStream());
ContactId c = ctx.getContactId();

View File

@@ -50,7 +50,8 @@ class OutgoingBatchConnection {
ConnectionContext ctx = db.getConnectionContext(contactId,
transportIndex);
ConnectionWriter conn = connFactory.createConnectionWriter(
writer.getOutputStream(), writer.getCapacity(), ctx);
writer.getOutputStream(), writer.getCapacity(),
ctx.getSecret());
OutputStream out = conn.getOutputStream();
// There should be enough space for a packet
long capacity = conn.getRemainingCapacity();

View File

@@ -34,13 +34,14 @@ class IncomingStreamConnection extends StreamConnection {
protected ConnectionReader createConnectionReader() throws DbException,
IOException {
return connReaderFactory.createConnectionReader(
connection.getInputStream(), ctx, tag);
connection.getInputStream(), ctx.getSecret(), tag);
}
@Override
protected ConnectionWriter createConnectionWriter() throws DbException,
IOException {
return connWriterFactory.createConnectionWriter(
connection.getOutputStream(), Long.MAX_VALUE, ctx, tag);
connection.getOutputStream(), Long.MAX_VALUE, ctx.getSecret(),
tag);
}
}

View File

@@ -40,7 +40,7 @@ class OutgoingStreamConnection extends StreamConnection {
ctx = db.getConnectionContext(contactId, transportIndex);
}
return connReaderFactory.createConnectionReader(
connection.getInputStream(), ctx);
connection.getInputStream(), ctx.getSecret());
}
@Override
@@ -51,6 +51,6 @@ class OutgoingStreamConnection extends StreamConnection {
ctx = db.getConnectionContext(contactId, transportIndex);
}
return connWriterFactory.createConnectionWriter(
connection.getOutputStream(), Long.MAX_VALUE, ctx);
connection.getOutputStream(), Long.MAX_VALUE, ctx.getSecret());
}
}