Forward secrecy.

Each connection's keys are derived from a secret that is erased after
deriving the keys and the secret for the next connection.
This commit is contained in:
akwizgran
2011-11-16 15:35:16 +00:00
parent d02a68edfc
commit f6ae4734ce
45 changed files with 506 additions and 430 deletions

View File

@@ -16,6 +16,7 @@ import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Author;
@@ -36,7 +37,6 @@ import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.protocol.writers.AckWriter;
import net.sf.briar.api.protocol.writers.BatchWriter;
import net.sf.briar.api.protocol.writers.OfferWriter;
@@ -44,6 +44,8 @@ import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
import net.sf.briar.api.protocol.writers.RequestWriter;
import net.sf.briar.api.protocol.writers.SubscriptionUpdateWriter;
import net.sf.briar.api.protocol.writers.TransportUpdateWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionWriter;
@@ -68,12 +70,14 @@ public class ProtocolIntegrationTest extends TestCase {
private final BatchId ack = new BatchId(TestUtils.getRandomId());
private final long timestamp = System.currentTimeMillis();
private final ConnectionContextFactory connectionContextFactory;
private final ConnectionReaderFactory connectionReaderFactory;
private final ConnectionWriterFactory connectionWriterFactory;
private final ProtocolReaderFactory protocolReaderFactory;
private final ProtocolWriterFactory protocolWriterFactory;
private final CryptoComponent crypto;
private final byte[] aliceToBobSecret;
private final byte[] secret;
private final ContactId contactId = new ContactId(13);
private final TransportIndex transportIndex = new TransportIndex(13);
private final long connection = 12345L;
private final Author author;
@@ -91,16 +95,17 @@ public class ProtocolIntegrationTest extends TestCase {
new ProtocolWritersModule(), new SerialModule(),
new TestDatabaseModule(), new TransportBatchModule(),
new TransportModule(), new TransportStreamModule());
connectionContextFactory =
i.getInstance(ConnectionContextFactory.class);
connectionReaderFactory = i.getInstance(ConnectionReaderFactory.class);
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
protocolReaderFactory = i.getInstance(ProtocolReaderFactory.class);
protocolWriterFactory = i.getInstance(ProtocolWriterFactory.class);
crypto = i.getInstance(CryptoComponent.class);
assertEquals(crypto.getMessageDigest().getDigestLength(),
UniqueId.LENGTH);
// Create a shared secret
Random r = new Random();
aliceToBobSecret = new byte[32];
r.nextBytes(aliceToBobSecret);
secret = new byte[32];
r.nextBytes(secret);
// Create two groups: one restricted, one unrestricted
GroupFactory groupFactory = i.getInstance(GroupFactory.class);
group = groupFactory.createGroup("Unrestricted group", null);
@@ -139,9 +144,11 @@ public class ProtocolIntegrationTest extends TestCase {
private byte[] write() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] copyOfSecret = Arrays.clone(aliceToBobSecret);
ConnectionContext ctx =
connectionContextFactory.createConnectionContext(contactId,
transportIndex, connection, Arrays.clone(secret));
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
Long.MAX_VALUE, transportIndex, connection, copyOfSecret);
Long.MAX_VALUE, ctx);
OutputStream out1 = w.getOutputStream();
AckWriter a = protocolWriterFactory.createAckWriter(out1);
@@ -184,19 +191,15 @@ public class ProtocolIntegrationTest extends TestCase {
return out.toByteArray();
}
private void read(byte[] connection) throws Exception {
InputStream in = new ByteArrayInputStream(connection);
private void read(byte[] connectionData) throws Exception {
InputStream in = new ByteArrayInputStream(connectionData);
byte[] encryptedIv = new byte[16];
int offset = 0;
while(offset < 16) {
int read = in.read(encryptedIv, offset, 16 - offset);
if(read == -1) break;
offset += read;
}
assertEquals(16, offset);
byte[] copyOfSecret = Arrays.clone(aliceToBobSecret);
assertEquals(16, in.read(encryptedIv, 0, 16));
ConnectionContext ctx =
connectionContextFactory.createConnectionContext(contactId,
transportIndex, connection, Arrays.clone(secret));
ConnectionReader r = connectionReaderFactory.createConnectionReader(in,
transportIndex, encryptedIv, copyOfSecret);
ctx, encryptedIv);
in = r.getInputStream();
ProtocolReader protocolReader =
protocolReaderFactory.createProtocolReader(in);

View File

@@ -47,7 +47,6 @@ import net.sf.briar.api.transport.ConnectionWindow;
import org.jmock.Expectations;
import org.jmock.Mockery;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public abstract class DatabaseComponentTest extends TestCase {
@@ -107,9 +106,9 @@ public abstract class DatabaseComponentTest extends TestCase {
Database<T> database, DatabaseCleaner cleaner);
@Test
@SuppressWarnings("unchecked")
public void testSimpleCalls() throws Exception {
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ConnectionWindow connectionWindow =
@@ -138,7 +137,8 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).setRating(txn, authorId, Rating.GOOD);
will(returnValue(Rating.GOOD));
// addContact()
oneOf(database).addContact(txn, inSecret, outSecret);
oneOf(database).addContact(with(txn), with(inSecret),
with(outSecret), with(any(Collection.class)));
will(returnValue(contactId));
oneOf(listener).eventOccurred(with(any(ContactAddedEvent.class)));
// getContacts()
@@ -149,16 +149,6 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(true));
oneOf(database).getConnectionWindow(txn, contactId, remoteIndex);
will(returnValue(connectionWindow));
// getSharedSecret(contactId, true)
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getSharedSecret(txn, contactId, true);
will(returnValue(inSecret));
// getSharedSecret(contactId, false)
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getSharedSecret(txn, contactId, false);
will(returnValue(outSecret));
// getTransportProperties(transportId)
oneOf(database).getRemoteProperties(txn, transportId);
will(returnValue(remoteProperties));
@@ -213,8 +203,6 @@ public abstract class DatabaseComponentTest extends TestCase {
assertEquals(Collections.singletonList(contactId), db.getContacts());
assertEquals(connectionWindow,
db.getConnectionWindow(contactId, remoteIndex));
assertArrayEquals(inSecret, db.getSharedSecret(contactId, true));
assertArrayEquals(outSecret, db.getSharedSecret(contactId, false));
assertEquals(remoteProperties, db.getRemoteProperties(transportId));
db.subscribe(group); // First time - listeners called
db.subscribe(group); // Second time - not called
@@ -516,11 +504,11 @@ public abstract class DatabaseComponentTest extends TestCase {
context.mock(TransportUpdate.class);
context.checking(new Expectations() {{
// Check whether the contact is still in the DB (which it's not)
exactly(20).of(database).startTransaction();
exactly(19).of(database).startTransaction();
will(returnValue(txn));
exactly(20).of(database).containsContact(txn, contactId);
exactly(19).of(database).containsContact(txn, contactId);
will(returnValue(false));
exactly(20).of(database).commitTransaction(txn);
exactly(19).of(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
@@ -575,11 +563,6 @@ public abstract class DatabaseComponentTest extends TestCase {
fail();
} catch(NoSuchContactException expected) {}
try {
db.getSharedSecret(contactId, true);
fail();
} catch(NoSuchContactException expected) {}
try {
db.hasSendableMessages(contactId);
fail();

View File

@@ -4,6 +4,7 @@ import static org.junit.Assert.assertArrayEquals;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -88,6 +89,7 @@ public class H2DatabaseTest extends TestCase {
private final Collection<Transport> remoteTransports;
private final Map<Group, Long> subscriptions;
private final byte[] inSecret, outSecret;
private final Collection<byte[]> erase;
public H2DatabaseTest() throws Exception {
super();
@@ -131,6 +133,7 @@ public class H2DatabaseTest extends TestCase {
r.nextBytes(inSecret);
outSecret = new byte[32];
r.nextBytes(outSecret);
erase = new ArrayList<byte[]>();
}
@Before
@@ -144,8 +147,7 @@ public class H2DatabaseTest extends TestCase {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
assertFalse(db.containsContact(txn, contactId));
assertEquals(contactId,
db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
assertTrue(db.containsContact(txn, contactId));
assertFalse(db.containsSubscription(txn, groupId));
db.addSubscription(txn, group);
@@ -201,20 +203,23 @@ public class H2DatabaseTest extends TestCase {
// Create three contacts
assertFalse(db.containsContact(txn, contactId));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
assertTrue(db.containsContact(txn, contactId));
assertFalse(db.containsContact(txn, contactId1));
assertEquals(contactId1, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId1,
db.addContact(txn, inSecret, outSecret, erase));
assertTrue(db.containsContact(txn, contactId1));
assertFalse(db.containsContact(txn, contactId2));
assertEquals(contactId2, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId2,
db.addContact(txn, inSecret, outSecret, erase));
assertTrue(db.containsContact(txn, contactId2));
// Delete the contact with the highest ID
db.removeContact(txn, contactId2);
assertFalse(db.containsContact(txn, contactId2));
// Add another contact - a new ID should be created
assertFalse(db.containsContact(txn, contactId3));
assertEquals(contactId3, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId3,
db.addContact(txn, inSecret, outSecret, erase));
assertTrue(db.containsContact(txn, contactId3));
db.commitTransaction(txn);
@@ -261,7 +266,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and store a private message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addPrivateMessage(txn, privateMessage, contactId);
// Removing the contact should remove the message
@@ -280,7 +285,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and store a private message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addPrivateMessage(txn, privateMessage, contactId);
// The message has no status yet, so it should not be sendable
@@ -319,7 +324,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and store a private message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addPrivateMessage(txn, privateMessage, contactId);
db.setStatus(txn, contactId, privateMessageId, Status.NEW);
@@ -347,7 +352,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -385,7 +390,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -427,7 +432,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.addGroupMessage(txn, message);
@@ -466,7 +471,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.addGroupMessage(txn, message);
@@ -501,7 +506,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -532,7 +537,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, subscriptions, 1);
db.addGroupMessage(txn, message);
@@ -565,7 +570,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and some batches to ack
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addBatchToAck(txn, contactId, batchId);
db.addBatchToAck(txn, contactId, batchId1);
@@ -592,7 +597,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and receive the same batch twice
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addBatchToAck(txn, contactId, batchId);
db.addBatchToAck(txn, contactId, batchId);
@@ -618,7 +623,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
@@ -643,8 +648,8 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add two contacts, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
ContactId contactId1 = db.addContact(txn, inSecret, outSecret);
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
ContactId contactId1 = db.addContact(txn, inSecret, outSecret, erase);
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
@@ -666,7 +671,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -705,7 +710,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -750,7 +755,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
// Add some outstanding batches, a few ms apart
for(int i = 0; i < ids.length; i++) {
@@ -790,7 +795,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
// Add some outstanding batches, a few ms apart
for(int i = 0; i < ids.length; i++) {
@@ -1010,7 +1015,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with a transport
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.setTransports(txn, contactId, remoteTransports, 1);
assertEquals(remoteProperties,
db.getRemoteProperties(txn, transportId));
@@ -1103,7 +1108,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with a transport
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.setTransports(txn, contactId, remoteTransports, 1);
assertEquals(remoteProperties,
db.getRemoteProperties(txn, transportId));
@@ -1147,7 +1152,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with some subscriptions
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.setSubscriptions(txn, contactId, subscriptions, 1);
assertEquals(Collections.singletonList(group),
db.getSubscriptions(txn, contactId));
@@ -1172,7 +1177,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with some subscriptions
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.setSubscriptions(txn, contactId, subscriptions, 2);
assertEquals(Collections.singletonList(group),
db.getSubscriptions(txn, contactId));
@@ -1196,7 +1201,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -1214,7 +1219,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, subscriptions, 1);
db.addGroupMessage(txn, message);
@@ -1237,7 +1242,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, subscriptions, 1);
db.addGroupMessage(txn, message);
@@ -1260,7 +1265,7 @@ public class H2DatabaseTest extends TestCase {
// Add a contact, subscribe to a group and store a message -
// the message is older than the contact's subscription
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
Map<Group, Long> subs = Collections.singletonMap(group, timestamp + 1);
@@ -1284,7 +1289,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -1309,7 +1314,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -1328,7 +1333,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with a subscription
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.setSubscriptions(txn, contactId, subscriptions, 1);
// There's no local subscription for the group
@@ -1345,7 +1350,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -1364,7 +1369,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -1384,7 +1389,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -1406,7 +1411,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
db.setSubscriptions(txn, contactId, subscriptions, 1);
@@ -1427,7 +1432,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
// The group should not be visible to the contact
assertEquals(Collections.emptyList(), db.getVisibility(txn, groupId));
@@ -1450,7 +1455,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
// Get the connection window for a new index
ConnectionWindow w = db.getConnectionWindow(txn, contactId,
remoteIndex);
@@ -1469,18 +1474,18 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
// Get the connection window for a new index
ConnectionWindow w = db.getConnectionWindow(txn, contactId,
remoteIndex);
// The connection window should exist and be in the initial state
assertNotNull(w);
Collection<Long> unseen = w.getUnseen();
Map<Long, byte[]> unseen = w.getUnseen();
long top = ProtocolConstants.CONNECTION_WINDOW_SIZE / 2 - 1;
assertEquals(top + 1, unseen.size());
for(long l = 0; l <= top; l++) {
assertFalse(w.isSeen(l));
assertTrue(unseen.contains(l));
assertTrue(unseen.containsKey(l));
}
// Update the connection window and store it
w.setSeen(5);
@@ -1573,7 +1578,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
// A message with a private parent should return null
@@ -1622,7 +1627,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
// The subscription and transport timestamps should be initialised to 0
assertEquals(0L, db.getSubscriptionsModified(txn, contactId));
@@ -1653,7 +1658,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, inSecret, outSecret));
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
db.addSubscription(txn, group);
// Store a couple of messages
@@ -1897,6 +1902,7 @@ public class H2DatabaseTest extends TestCase {
@After
public void tearDown() {
erase.clear();
TestUtils.deleteTestDirectory(testDir);
}

View File

@@ -52,7 +52,8 @@ public class ConnectionDecrypterImplTest extends TestCase {
private void testDecryption(boolean initiator) throws Exception {
// Calculate the plaintext and ciphertext for the IV
byte[] iv = IvEncoder.encodeIv(initiator, transportIndex, connection);
byte[] iv = IvEncoder.encodeIv(initiator, transportIndex.getInt(),
connection);
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
byte[] encryptedIv = ivCipher.doFinal(iv);
assertEquals(IV_LENGTH, encryptedIv.length);
@@ -85,8 +86,8 @@ public class ConnectionDecrypterImplTest extends TestCase {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use a ConnectionDecrypter to decrypt the ciphertext
ConnectionDecrypter d = new ConnectionDecrypterImpl(in,
IvEncoder.encodeIv(initiator, transportIndex, connection),
frameCipher, frameKey);
IvEncoder.encodeIv(initiator, transportIndex.getInt(),
connection), frameCipher, frameKey);
// First frame
byte[] decrypted = new byte[ciphertext.length];
TestUtils.readFully(d.getInputStream(), decrypted);

View File

@@ -50,7 +50,8 @@ public class ConnectionEncrypterImplTest extends TestCase {
private void testEncryption(boolean initiator) throws Exception {
// Calculate the expected ciphertext for the IV
byte[] iv = IvEncoder.encodeIv(initiator, transportIndex, connection);
byte[] iv = IvEncoder.encodeIv(initiator, transportIndex.getInt(),
connection);
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
byte[] encryptedIv = ivCipher.doFinal(iv);
assertEquals(IV_LENGTH, encryptedIv.length);
@@ -82,7 +83,7 @@ public class ConnectionEncrypterImplTest extends TestCase {
byte[] expected = out.toByteArray();
// Use a ConnectionEncrypter to encrypt the plaintext
out.reset();
iv = IvEncoder.encodeIv(initiator, transportIndex, connection);
iv = IvEncoder.encodeIv(initiator, transportIndex.getInt(), connection);
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
iv, ivCipher, frameCipher, ivKey, frameKey);
e.getOutputStream().write(plaintext);

View File

@@ -4,6 +4,7 @@ import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Random;
import javax.crypto.Cipher;
@@ -51,7 +52,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
Transport transport = new Transport(transportId, localIndex,
Collections.singletonMap("foo", "bar"));
transports = Collections.singletonList(transport);
connectionWindow = new ConnectionWindowImpl();
connectionWindow = new ConnectionWindowImpl(crypto, remoteIndex,
inSecret);
}
@Test
@@ -65,8 +67,6 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(transports));
oneOf(db).getContacts();
will(returnValue(Collections.singletonList(contactId)));
oneOf(db).getSharedSecret(contactId, true);
will(returnValue(inSecret));
oneOf(db).getRemoteIndex(contactId, transportId);
will(returnValue(remoteIndex));
oneOf(db).getConnectionWindow(contactId, remoteIndex);
@@ -80,11 +80,16 @@ public class ConnectionRecogniserImplTest extends TestCase {
@Test
public void testExpectedIv() throws Exception {
// Calculate the shared secret for connection number 3
byte[] secret = inSecret;
for(int i = 0; i < 4; i++) {
secret = crypto.deriveNextSecret(secret, remoteIndex.getInt(), i);
}
// Calculate the expected IV for connection number 3
ErasableKey ivKey = crypto.deriveIvKey(inSecret, true);
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
Cipher ivCipher = crypto.getIvCipher();
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
byte[] iv = IvEncoder.encodeIv(true, remoteIndex, 3L);
byte[] iv = IvEncoder.encodeIv(true, remoteIndex.getInt(), 3);
byte[] encryptedIv = ivCipher.doFinal(iv);
Mockery context = new Mockery();
@@ -96,8 +101,6 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(transports));
oneOf(db).getContacts();
will(returnValue(Collections.singletonList(contactId)));
oneOf(db).getSharedSecret(contactId, true);
will(returnValue(inSecret));
oneOf(db).getRemoteIndex(contactId, transportId);
will(returnValue(remoteIndex));
oneOf(db).getConnectionWindow(contactId, remoteIndex);
@@ -107,8 +110,6 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(connectionWindow));
oneOf(db).setConnectionWindow(contactId, remoteIndex,
connectionWindow);
oneOf(db).getSharedSecret(contactId, true);
will(returnValue(inSecret));
}});
final ConnectionRecogniserImpl c =
new ConnectionRecogniserImpl(crypto, db);
@@ -121,11 +122,11 @@ public class ConnectionRecogniserImplTest extends TestCase {
// Second time - the IV should no longer be expected
assertNull(c.acceptConnection(encryptedIv));
// The window should have advanced
Collection<Long> unseen = connectionWindow.getUnseen();
Map<Long, byte[]> unseen = connectionWindow.getUnseen();
assertEquals(19, unseen.size());
for(int i = 0; i < 19; i++) {
if(i == 3) continue;
assertTrue(unseen.contains(Long.valueOf(i)));
assertTrue(unseen.containsKey(Long.valueOf(i)));
}
context.assertIsSatisfied();
}

View File

@@ -1,18 +1,39 @@
package net.sf.briar.transport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.util.ByteUtils;
import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionWindowImplTest extends TestCase {
private final CryptoComponent crypto;
private final byte[] secret;
private final TransportIndex transportIndex = new TransportIndex(13);
public ConnectionWindowImplTest(String name) {
super(name);
Injector i = Guice.createInjector(new CryptoModule());
crypto = i.getInstance(CryptoComponent.class);
secret = new byte[32];
new Random().nextBytes(secret);
}
@Test
public void testWindowSliding() {
ConnectionWindowImpl w = new ConnectionWindowImpl();
ConnectionWindow w = new ConnectionWindowImpl(crypto,
transportIndex, secret);
for(int i = 0; i < 100; i++) {
assertFalse(w.isSeen(i));
w.setSeen(i);
@@ -22,7 +43,8 @@ public class ConnectionWindowImplTest extends TestCase {
@Test
public void testWindowJumping() {
ConnectionWindowImpl w = new ConnectionWindowImpl();
ConnectionWindow w = new ConnectionWindowImpl(crypto, transportIndex,
secret);
for(int i = 0; i < 100; i += 13) {
assertFalse(w.isSeen(i));
w.setSeen(i);
@@ -32,7 +54,8 @@ public class ConnectionWindowImplTest extends TestCase {
@Test
public void testWindowUpperLimit() {
ConnectionWindowImpl w = new ConnectionWindowImpl();
ConnectionWindow w = new ConnectionWindowImpl(crypto, transportIndex,
secret);
// Centre is 0, highest value in window is 15
w.setSeen(15);
// Centre is 16, highest value in window is 31
@@ -43,11 +66,11 @@ public class ConnectionWindowImplTest extends TestCase {
fail();
} catch(IllegalArgumentException expected) {}
// Values greater than 2^32 - 1 should never be allowed
Collection<Long> unseen = new ArrayList<Long>();
Map<Long, byte[]> unseen = new HashMap<Long, byte[]>();
for(int i = 0; i < 32; i++) {
unseen.add(ByteUtils.MAX_32_BIT_UNSIGNED - i);
unseen.put(ByteUtils.MAX_32_BIT_UNSIGNED - i, secret);
}
w = new ConnectionWindowImpl(unseen);
w = new ConnectionWindowImpl(crypto, transportIndex, unseen);
w.setSeen(ByteUtils.MAX_32_BIT_UNSIGNED);
try {
w.setSeen(ByteUtils.MAX_32_BIT_UNSIGNED + 1);
@@ -57,7 +80,8 @@ public class ConnectionWindowImplTest extends TestCase {
@Test
public void testWindowLowerLimit() {
ConnectionWindowImpl w = new ConnectionWindowImpl();
ConnectionWindow w = new ConnectionWindowImpl(crypto, transportIndex,
secret);
// Centre is 0, negative values should never be allowed
try {
w.setSeen(-1);
@@ -87,7 +111,8 @@ public class ConnectionWindowImplTest extends TestCase {
@Test
public void testCannotSetSeenTwice() {
ConnectionWindowImpl w = new ConnectionWindowImpl();
ConnectionWindow w = new ConnectionWindowImpl(crypto, transportIndex,
secret);
w.setSeen(15);
try {
w.setSeen(15);
@@ -97,12 +122,13 @@ public class ConnectionWindowImplTest extends TestCase {
@Test
public void testGetUnseenConnectionNumbers() {
ConnectionWindowImpl w = new ConnectionWindowImpl();
ConnectionWindow w = new ConnectionWindowImpl(crypto, transportIndex,
secret);
// Centre is 0; window should cover 0 to 15, inclusive, with none seen
Collection<Long> unseen = w.getUnseen();
Map<Long, byte[]> unseen = w.getUnseen();
assertEquals(16, unseen.size());
for(int i = 0; i < 16; i++) {
assertTrue(unseen.contains(Long.valueOf(i)));
assertTrue(unseen.containsKey(Long.valueOf(i)));
assertFalse(w.isSeen(i));
}
w.setSeen(3);
@@ -112,10 +138,10 @@ public class ConnectionWindowImplTest extends TestCase {
assertEquals(19, unseen.size());
for(int i = 0; i < 21; i++) {
if(i == 3 || i == 4) {
assertFalse(unseen.contains(Long.valueOf(i)));
assertFalse(unseen.containsKey(Long.valueOf(i)));
assertTrue(w.isSeen(i));
} else {
assertTrue(unseen.contains(Long.valueOf(i)));
assertTrue(unseen.containsKey(Long.valueOf(i)));
assertFalse(w.isSeen(i));
}
}
@@ -125,10 +151,10 @@ public class ConnectionWindowImplTest extends TestCase {
assertEquals(30, unseen.size());
for(int i = 4; i < 36; i++) {
if(i == 4 || i == 19) {
assertFalse(unseen.contains(Long.valueOf(i)));
assertFalse(unseen.containsKey(Long.valueOf(i)));
assertTrue(w.isSeen(i));
} else {
assertTrue(unseen.contains(Long.valueOf(i)));
assertTrue(unseen.containsKey(Long.valueOf(i)));
assertFalse(w.isSeen(i));
}
}

View File

@@ -8,7 +8,10 @@ import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.crypto.CryptoModule;
@@ -26,8 +29,10 @@ import com.google.inject.Injector;
public class ConnectionWriterTest extends TestCase {
private final ConnectionContextFactory connectionContextFactory;
private final ConnectionWriterFactory connectionWriterFactory;
private final byte[] outSecret;
private final byte[] secret;
private final ContactId contactId = new ContactId(13);
private final TransportIndex transportIndex = new TransportIndex(13);
private final long connection = 12345L;
@@ -38,17 +43,22 @@ public class ConnectionWriterTest extends TestCase {
new ProtocolWritersModule(), new SerialModule(),
new TestDatabaseModule(), new TransportBatchModule(),
new TransportModule(), new TransportStreamModule());
connectionContextFactory =
i.getInstance(ConnectionContextFactory.class);
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
outSecret = new byte[32];
new Random().nextBytes(outSecret);
secret = new byte[32];
new Random().nextBytes(secret);
}
@Test
public void testOverhead() throws Exception {
ByteArrayOutputStream out =
new ByteArrayOutputStream(MIN_CONNECTION_LENGTH);
ConnectionContext ctx =
connectionContextFactory.createConnectionContext(contactId,
transportIndex, connection, secret);
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
MIN_CONNECTION_LENGTH, transportIndex, connection, outSecret);
MIN_CONNECTION_LENGTH, ctx);
// Check that the connection writer thinks there's room for a packet
long capacity = w.getRemainingCapacity();
assertTrue(capacity >= MAX_PACKET_LENGTH);

View File

@@ -64,7 +64,8 @@ public class FrameReadWriteTest extends TestCase {
private void testWriteAndRead(boolean initiator) throws Exception {
// Create and encrypt the IV
byte[] iv = IvEncoder.encodeIv(initiator, transportIndex, connection);
byte[] iv = IvEncoder.encodeIv(initiator, transportIndex.getInt(),
connection);
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
byte[] encryptedIv = ivCipher.doFinal(iv);
assertEquals(IV_LENGTH, encryptedIv.length);
@@ -92,7 +93,7 @@ public class FrameReadWriteTest extends TestCase {
// Decrypt the IV
ivCipher.init(Cipher.DECRYPT_MODE, ivKey);
byte[] recoveredIv = ivCipher.doFinal(recoveredEncryptedIv);
iv = IvEncoder.encodeIv(initiator, transportIndex, connection);
iv = IvEncoder.encodeIv(initiator, transportIndex.getInt(), connection);
assertArrayEquals(iv, recoveredIv);
// Read the frames back
ConnectionDecrypter decrypter = new ConnectionDecrypterImpl(in, iv,

View File

@@ -119,7 +119,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
alice.getInstance(ProtocolWriterFactory.class);
BatchTransportWriter writer = new TestBatchTransportWriter(out);
OutgoingBatchConnection batchOut = new OutgoingBatchConnection(
connFactory, db, protoFactory, transportIndex, contactId,
connFactory, db, protoFactory, contactId, transportIndex,
writer);
// Write whatever needs to be written
batchOut.write();
@@ -170,8 +170,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
bob.getInstance(ProtocolReaderFactory.class);
BatchTransportReader reader = new TestBatchTransportReader(in);
IncomingBatchConnection batchIn = new IncomingBatchConnection(
connFactory, db, protoFactory, transportIndex, contactId,
reader, encryptedIv);
connFactory, db, protoFactory, ctx, reader, encryptedIv);
// No messages should have been added yet
assertFalse(listener.messagesAdded);
// Read whatever needs to be read