mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
The goal of the refactoring was to clean up the dependencies of IncomingBatchConnection and OutgoingBatchConnection.
45 lines
1.4 KiB
Java
45 lines
1.4 KiB
Java
package net.sf.briar.transport;
|
|
|
|
import java.io.OutputStream;
|
|
import java.security.InvalidKeyException;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.SecretKey;
|
|
|
|
import net.sf.briar.api.crypto.CryptoComponent;
|
|
import net.sf.briar.api.transport.ConnectionWriter;
|
|
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
|
|
|
import com.google.inject.Inject;
|
|
|
|
class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
|
|
|
private final CryptoComponent crypto;
|
|
|
|
@Inject
|
|
public ConnectionWriterFactoryImpl(CryptoComponent crypto) {
|
|
this.crypto = crypto;
|
|
}
|
|
|
|
public ConnectionWriter createConnectionWriter(OutputStream out,
|
|
long capacity, boolean initiator, int transportId, long connection,
|
|
byte[] secret) {
|
|
SecretKey macKey = crypto.deriveOutgoingMacKey(secret);
|
|
SecretKey ivKey = crypto.deriveOutgoingIvKey(secret);
|
|
SecretKey frameKey = crypto.deriveOutgoingFrameKey(secret);
|
|
Cipher ivCipher = crypto.getIvCipher();
|
|
Cipher frameCipher = crypto.getFrameCipher();
|
|
Mac mac = crypto.getMac();
|
|
try {
|
|
mac.init(macKey);
|
|
} catch(InvalidKeyException badKey) {
|
|
throw new IllegalArgumentException(badKey);
|
|
}
|
|
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
|
capacity, initiator, transportId, connection, ivCipher,
|
|
frameCipher, ivKey, frameKey);
|
|
return new ConnectionWriterImpl(encrypter, mac);
|
|
}
|
|
}
|