Factory methods for segmented connection writers.

This commit is contained in:
akwizgran
2012-01-17 20:29:30 +00:00
parent dbeb7a207e
commit 6085b70b85
7 changed files with 38 additions and 30 deletions

View File

@@ -48,6 +48,6 @@ class IncomingDuplexConnection extends DuplexConnection {
protected ConnectionWriter createConnectionWriter() throws IOException {
return connWriterFactory.createConnectionWriter(
transport.getOutputStream(), Long.MAX_VALUE, ctx.getSecret(),
tag);
false);
}
}

View File

@@ -60,6 +60,7 @@ class OutgoingDuplexConnection extends DuplexConnection {
ctx = db.getConnectionContext(contactId, transportIndex);
}
return connWriterFactory.createConnectionWriter(
transport.getOutputStream(), Long.MAX_VALUE, ctx.getSecret());
transport.getOutputStream(), Long.MAX_VALUE, ctx.getSecret(),
true);
}
}

View File

@@ -62,7 +62,7 @@ class OutgoingSimplexConnection {
transportIndex);
ConnectionWriter conn = connFactory.createConnectionWriter(
transport.getOutputStream(), transport.getCapacity(),
ctx.getSecret());
ctx.getSecret(), true);
OutputStream out = conn.getOutputStream();
ProtocolWriter writer = protoFactory.createProtocolWriter(out,
transport.shouldFlush());

View File

@@ -7,6 +7,7 @@ import javax.crypto.Mac;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.SegmentSink;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.util.ByteUtils;
@@ -23,23 +24,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
}
public ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, byte[] secret) {
return createConnectionWriter(out, capacity, true, secret);
}
public ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, byte[] secret, byte[] tag) {
// Validate the tag
Cipher tagCipher = crypto.getTagCipher();
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
long segmentNumber = TagEncoder.decodeTag(tag, tagCipher, tagKey);
tagKey.erase();
if(segmentNumber != 0) throw new IllegalArgumentException();
return createConnectionWriter(out, capacity, false, secret);
}
private ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, boolean initiator, byte[] secret) {
long capacity, byte[] secret, boolean initiator) {
// Derive the keys and erase the secret
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
@@ -57,4 +42,25 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
Mac mac = crypto.getMac();
return new ConnectionWriterImpl(correcter, mac, macKey);
}
public ConnectionWriter createConnectionWriter(SegmentSink out,
long capacity, byte[] secret, boolean initiator) {
// Derive the keys and erase the secret
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
ByteUtils.erase(secret);
// Create the encrypter
Cipher tagCipher = crypto.getTagCipher();
Cipher frameCipher = crypto.getFrameCipher();
OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(out, capacity, tagCipher,
frameCipher, tagKey, frameKey, false);
// No error correction
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
// Create the writer
Mac mac = crypto.getMac();
return new ConnectionWriterImpl(correcter, mac, macKey);
}
}