mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Updated FileReadWriteTest to use the transport component for
encrypting and decrypting packets. Moved the test to the main package since it's an integration test for several components.
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.Author;
|
||||
import net.sf.briar.api.protocol.AuthorFactory;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.GroupFactory;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
@@ -29,12 +35,24 @@ public class ProtocolModule extends AbstractModule {
|
||||
bind(MessageEncoder.class).to(MessageEncoderImpl.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Ack> getAckReader(ObjectReader<BatchId> batchIdReader,
|
||||
AckFactory ackFactory) {
|
||||
return new AckReader(batchIdReader, ackFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Author> getAuthorReader(CryptoComponent crypto,
|
||||
AuthorFactory authorFactory) {
|
||||
return new AuthorReader(crypto, authorFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Batch> getBatchReader(CryptoComponent crypto,
|
||||
ObjectReader<Message> messageReader, BatchFactory batchFactory) {
|
||||
return new BatchReader(crypto, messageReader, batchFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<BatchId> getBatchIdReader() {
|
||||
return new BatchIdReader();
|
||||
@@ -59,4 +77,28 @@ public class ProtocolModule extends AbstractModule {
|
||||
return new MessageReader(crypto, messageIdReader, groupReader,
|
||||
authorReader);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Offer> getOfferReader(ObjectReader<MessageId> messageIdReader,
|
||||
OfferFactory offerFactory) {
|
||||
return new OfferReader(messageIdReader, offerFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Request> getRequestReader(RequestFactory requestFactory) {
|
||||
return new RequestReader(requestFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<SubscriptionUpdate> getSubscriptionReader(
|
||||
ObjectReader<Group> groupReader,
|
||||
SubscriptionFactory subscriptionFactory) {
|
||||
return new SubscriptionReader(groupReader, subscriptionFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<TransportUpdate> getTransportReader(
|
||||
TransportFactory transportFactory) {
|
||||
return new TransportReader(transportFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.ShortBufferException;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
class PacketEncrypterImpl extends FilterOutputStream
|
||||
@@ -68,35 +67,19 @@ implements PacketEncrypter {
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
byte[] buf = new byte[] {(byte) b};
|
||||
try {
|
||||
int i = packetCipher.update(buf, 0, buf.length, buf);
|
||||
assert i <= 1;
|
||||
if(i == 1) out.write(b);
|
||||
} catch(ShortBufferException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
byte[] ciphertext = packetCipher.update(new byte[] {(byte) b});
|
||||
if(ciphertext != null) out.write(ciphertext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
try {
|
||||
int i = packetCipher.update(b, 0, b.length, b);
|
||||
assert i <= b.length;
|
||||
out.write(b, 0, i);
|
||||
} catch(ShortBufferException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
byte[] ciphertext = packetCipher.update(b);
|
||||
if(ciphertext != null) out.write(ciphertext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
try {
|
||||
int i = packetCipher.update(b, off, len, b, off);
|
||||
assert i <= len;
|
||||
out.write(b, off, i);
|
||||
} catch(ShortBufferException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
byte[] ciphertext = packetCipher.update(b, off, len);
|
||||
if(ciphertext != null) out.write(ciphertext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import java.io.InputStream;
|
||||
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.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
import net.sf.briar.api.transport.PacketReader;
|
||||
import net.sf.briar.api.transport.PacketReaderFactory;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
class PacketReaderFactoryImpl implements PacketReaderFactory {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final ReaderFactory readerFactory;
|
||||
private final Provider<ObjectReader<Ack>> ackProvider;
|
||||
private final Provider<ObjectReader<Batch>> batchProvider;
|
||||
private final Provider<ObjectReader<Offer>> offerProvider;
|
||||
private final Provider<ObjectReader<Request>> requestProvider;
|
||||
private final Provider<ObjectReader<SubscriptionUpdate>> subscriptionProvider;
|
||||
private final Provider<ObjectReader<TransportUpdate>> transportProvider;
|
||||
|
||||
@Inject
|
||||
PacketReaderFactoryImpl(CryptoComponent crypto, ReaderFactory readerFactory,
|
||||
Provider<ObjectReader<Ack>> ackProvider,
|
||||
Provider<ObjectReader<Batch>> batchProvider,
|
||||
Provider<ObjectReader<Offer>> offerProvider,
|
||||
Provider<ObjectReader<Request>> requestProvider,
|
||||
Provider<ObjectReader<SubscriptionUpdate>> subscriptionProvider,
|
||||
Provider<ObjectReader<TransportUpdate>> transportProvider) {
|
||||
this.crypto = crypto;
|
||||
this.readerFactory = readerFactory;
|
||||
this.ackProvider = ackProvider;
|
||||
this.batchProvider = batchProvider;
|
||||
this.offerProvider = offerProvider;
|
||||
this.requestProvider = requestProvider;
|
||||
this.subscriptionProvider = subscriptionProvider;
|
||||
this.transportProvider = transportProvider;
|
||||
}
|
||||
|
||||
public PacketReader createPacketReader(byte[] firstTag, InputStream in,
|
||||
int transportId, long connection, byte[] secret) {
|
||||
SecretKey macKey = crypto.deriveMacKey(secret);
|
||||
SecretKey tagKey = crypto.deriveTagKey(secret);
|
||||
SecretKey packetKey = crypto.derivePacketKey(secret);
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher packetCipher = crypto.getPacketCipher();
|
||||
Mac mac = crypto.getMac();
|
||||
try {
|
||||
mac.init(macKey);
|
||||
} catch(InvalidKeyException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
PacketDecrypter decrypter = new PacketDecrypterImpl(firstTag, in,
|
||||
tagCipher, packetCipher, tagKey, packetKey);
|
||||
return new PacketReaderImpl(firstTag, readerFactory, ackProvider.get(),
|
||||
batchProvider.get(), offerProvider.get(), requestProvider.get(),
|
||||
subscriptionProvider.get(), transportProvider.get(),
|
||||
decrypter, mac, transportId, connection);
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.writers.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
import net.sf.briar.api.transport.PacketReader;
|
||||
@@ -31,18 +31,21 @@ class PacketReaderImpl implements PacketReader {
|
||||
private boolean betweenPackets = true;
|
||||
|
||||
PacketReaderImpl(byte[] firstTag, ReaderFactory readerFactory,
|
||||
ProtocolReaderFactory protocol, PacketDecrypter decrypter, Mac mac,
|
||||
int transportId, long connection) {
|
||||
ObjectReader<Ack> ackReader, ObjectReader<Batch> batchReader,
|
||||
ObjectReader<Offer> offerReader,
|
||||
ObjectReader<Request> requestReader,
|
||||
ObjectReader<SubscriptionUpdate> subscriptionReader,
|
||||
ObjectReader<TransportUpdate> transportReader,
|
||||
PacketDecrypter decrypter, Mac mac, int transportId,
|
||||
long connection) {
|
||||
InputStream in = decrypter.getInputStream();
|
||||
reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Tags.ACK, protocol.createAckReader(in));
|
||||
reader.addObjectReader(Tags.BATCH, protocol.createBatchReader(in));
|
||||
reader.addObjectReader(Tags.OFFER, protocol.createOfferReader(in));
|
||||
reader.addObjectReader(Tags.REQUEST, protocol.createRequestReader(in));
|
||||
reader.addObjectReader(Tags.SUBSCRIPTIONS,
|
||||
protocol.createSubscriptionReader(in));
|
||||
reader.addObjectReader(Tags.TRANSPORTS,
|
||||
protocol.createTransportReader(in));
|
||||
reader.addObjectReader(Tags.ACK, ackReader);
|
||||
reader.addObjectReader(Tags.BATCH, batchReader);
|
||||
reader.addObjectReader(Tags.OFFER, offerReader);
|
||||
reader.addObjectReader(Tags.REQUEST, requestReader);
|
||||
reader.addObjectReader(Tags.SUBSCRIPTIONS, subscriptionReader);
|
||||
reader.addObjectReader(Tags.TRANSPORTS, transportReader);
|
||||
reader.addConsumer(new MacConsumer(mac));
|
||||
this.decrypter = decrypter;
|
||||
this.mac = mac;
|
||||
@@ -51,10 +54,6 @@ class PacketReaderImpl implements PacketReader {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public boolean eof() throws IOException {
|
||||
return reader.eof();
|
||||
}
|
||||
|
||||
public boolean hasAck() throws IOException {
|
||||
if(betweenPackets) readTag();
|
||||
return reader.hasUserDefined(Tags.ACK);
|
||||
|
||||
@@ -3,6 +3,7 @@ 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;
|
||||
|
||||
@@ -22,17 +23,20 @@ class PacketWriterFactoryImpl implements PacketWriterFactory {
|
||||
}
|
||||
|
||||
public PacketWriter createPacketWriter(OutputStream out, int transportId,
|
||||
long connection, SecretKey macKey, SecretKey tagKey,
|
||||
SecretKey packetKey) {
|
||||
long connection, byte[] secret) {
|
||||
SecretKey macKey = crypto.deriveMacKey(secret);
|
||||
SecretKey tagKey = crypto.deriveTagKey(secret);
|
||||
SecretKey packetKey = crypto.derivePacketKey(secret);
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher packetCipher = crypto.getPacketCipher();
|
||||
Mac mac = crypto.getMac();
|
||||
try {
|
||||
mac.init(macKey);
|
||||
} catch(InvalidKeyException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
PacketEncrypter e = new PacketEncrypterImpl(out, crypto.getTagCipher(),
|
||||
crypto.getPacketCipher(), tagKey, packetKey);
|
||||
return new PacketWriterImpl(e, mac, transportId,
|
||||
connection);
|
||||
PacketEncrypter encrypter = new PacketEncrypterImpl(out, tagCipher,
|
||||
packetCipher, tagKey, packetKey);
|
||||
return new PacketWriterImpl(encrypter, mac, transportId, connection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +56,10 @@ class PacketWriterImpl extends FilterOutputStream implements PacketWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int len, int off) throws IOException {
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
if(betweenPackets) writeTag();
|
||||
out.write(b, len, off);
|
||||
mac.update(b, len, off);
|
||||
out.write(b, off, len);
|
||||
mac.update(b, off, len);
|
||||
}
|
||||
|
||||
private void writeMac() throws IOException {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
import net.sf.briar.api.transport.PacketReaderFactory;
|
||||
import net.sf.briar.api.transport.PacketWriterFactory;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
@@ -10,5 +12,7 @@ public class TransportModule extends AbstractModule {
|
||||
protected void configure() {
|
||||
bind(ConnectionWindowFactory.class).to(
|
||||
ConnectionWindowFactoryImpl.class);
|
||||
bind(PacketReaderFactory.class).to(PacketReaderFactoryImpl.class);
|
||||
bind(PacketWriterFactory.class).to(PacketWriterFactoryImpl.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user