Return key set IDs when adding unbound keys.

This commit is contained in:
akwizgran
2018-03-27 16:11:18 +01:00
parent cb8f89db53
commit 5bd2092a03
6 changed files with 42 additions and 39 deletions

View File

@@ -6,6 +6,8 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import java.util.Collection;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
@@ -26,10 +28,11 @@ public interface KeyManager {
long timestamp, boolean alice) throws DbException; long timestamp, boolean alice) throws DbException;
/** /**
* Derives and stores a set of unbound transport keys for each transport. * Derives and stores a set of unbound transport keys for each transport
* and returns the key set IDs.
*/ */
void addUnboundKeys(Transaction txn, SecretKey master, long timestamp, Collection<KeySetId> addUnboundKeys(Transaction txn, SecretKey master,
boolean alice) throws DbException; long timestamp, boolean alice) throws DbException;
/** /**
* Returns a {@link StreamContext} for sending a stream to the given * Returns a {@link StreamContext} for sending a stream to the given

View File

@@ -19,8 +19,11 @@ import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory; import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory; import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
import org.briarproject.bramble.api.transport.KeyManager; import org.briarproject.bramble.api.transport.KeyManager;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.StreamContext; import org.briarproject.bramble.api.transport.StreamContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@@ -105,10 +108,13 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
} }
@Override @Override
public void addUnboundKeys(Transaction txn, SecretKey master, public Collection<KeySetId> addUnboundKeys(Transaction txn,
long timestamp, boolean alice) throws DbException { SecretKey master, long timestamp, boolean alice)
throws DbException {
Collection<KeySetId> ids = new ArrayList<>();
for (TransportKeyManager m : managers.values()) for (TransportKeyManager m : managers.values())
m.addUnboundKeys(txn, master, timestamp, alice); ids.add(m.addUnboundKeys(txn, master, timestamp, alice));
return ids;
} }
@Override @Override
@@ -121,7 +127,7 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t); if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
return null; return null;
} }
StreamContext ctx = null; StreamContext ctx;
Transaction txn = db.startTransaction(false); Transaction txn = db.startTransaction(false);
try { try {
ctx = m.getStreamContext(txn, c); ctx = m.getStreamContext(txn, c);
@@ -140,7 +146,7 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t); if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
return null; return null;
} }
StreamContext ctx = null; StreamContext ctx;
Transaction txn = db.startTransaction(false); Transaction txn = db.startTransaction(false);
try { try {
ctx = m.getStreamContext(txn, tag); ctx = m.getStreamContext(txn, tag);

View File

@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.StreamContext; import org.briarproject.bramble.api.transport.StreamContext;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@@ -17,7 +18,7 @@ interface TransportKeyManager {
void addContact(Transaction txn, ContactId c, SecretKey master, void addContact(Transaction txn, ContactId c, SecretKey master,
long timestamp, boolean alice) throws DbException; long timestamp, boolean alice) throws DbException;
void addUnboundKeys(Transaction txn, SecretKey master, long timestamp, KeySetId addUnboundKeys(Transaction txn, SecretKey master, long timestamp,
boolean alice) throws DbException; boolean alice) throws DbException;
void removeContact(ContactId c); void removeContact(ContactId c);

View File

@@ -176,12 +176,12 @@ class TransportKeyManagerImpl implements TransportKeyManager {
} }
@Override @Override
public void addUnboundKeys(Transaction txn, SecretKey master, public KeySetId addUnboundKeys(Transaction txn, SecretKey master,
long timestamp, boolean alice) throws DbException { long timestamp, boolean alice) throws DbException {
deriveAndAddKeys(txn, null, master, timestamp, alice); return deriveAndAddKeys(txn, null, master, timestamp, alice);
} }
private void deriveAndAddKeys(Transaction txn, @Nullable ContactId c, private KeySetId deriveAndAddKeys(Transaction txn, @Nullable ContactId c,
SecretKey master, long timestamp, boolean alice) SecretKey master, long timestamp, boolean alice)
throws DbException { throws DbException {
lock.lock(); lock.lock();
@@ -198,6 +198,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
KeySetId keySetId = db.addTransportKeys(txn, c, k); KeySetId keySetId = db.addTransportKeys(txn, c, k);
// Initialise mutable state for the contact // Initialise mutable state for the contact
addKeys(keySetId, c, new MutableTransportKeys(k)); addKeys(keySetId, c, new MutableTransportKeys(k));
return keySetId;
} finally { } finally {
lock.unlock(); lock.unlock();
} }

View File

@@ -12,19 +12,19 @@ import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.plugin.PluginConfig; import org.briarproject.bramble.api.plugin.PluginConfig;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory; import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.StreamContext; import org.briarproject.bramble.api.transport.StreamContext;
import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.concurrent.DeterministicExecutor; import org.jmock.lib.concurrent.DeterministicExecutor;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Random; import java.util.Random;
import static java.util.Collections.singletonList;
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getAuthor; import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes; import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
@@ -32,31 +32,29 @@ import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getSecretKey; import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class KeyManagerImplTest extends BrambleTestCase { public class KeyManagerImplTest extends BrambleMockTestCase {
private final Mockery context = new Mockery();
private final KeyManagerImpl keyManager;
private final DatabaseComponent db = context.mock(DatabaseComponent.class); private final DatabaseComponent db = context.mock(DatabaseComponent.class);
private final PluginConfig pluginConfig = context.mock(PluginConfig.class); private final PluginConfig pluginConfig = context.mock(PluginConfig.class);
private final TransportKeyManagerFactory transportKeyManagerFactory = private final TransportKeyManagerFactory transportKeyManagerFactory =
context.mock(TransportKeyManagerFactory.class); context.mock(TransportKeyManagerFactory.class);
private final TransportKeyManager transportKeyManager = private final TransportKeyManager transportKeyManager =
context.mock(TransportKeyManager.class); context.mock(TransportKeyManager.class);
private final DeterministicExecutor executor = new DeterministicExecutor(); private final DeterministicExecutor executor = new DeterministicExecutor();
private final Transaction txn = new Transaction(null, false); private final Transaction txn = new Transaction(null, false);
private final ContactId contactId = new ContactId(42); private final ContactId contactId = new ContactId(123);
private final ContactId inactiveContactId = new ContactId(43); private final ContactId inactiveContactId = new ContactId(234);
private final TransportId transportId = new TransportId("tId"); private final KeySetId keySetId = new KeySetId(345);
private final TransportId unknownTransportId = new TransportId("id"); private final TransportId transportId = new TransportId("known");
private final TransportId unknownTransportId = new TransportId("unknown");
private final StreamContext streamContext = private final StreamContext streamContext =
new StreamContext(contactId, transportId, getSecretKey(), new StreamContext(contactId, transportId, getSecretKey(),
getSecretKey(), 1); getSecretKey(), 1);
private final byte[] tag = getRandomBytes(TAG_LENGTH); private final byte[] tag = getRandomBytes(TAG_LENGTH);
public KeyManagerImplTest() { private final KeyManagerImpl keyManager = new KeyManagerImpl(db, executor,
keyManager = new KeyManagerImpl(db, executor, pluginConfig, pluginConfig, transportKeyManagerFactory);
transportKeyManagerFactory);
}
@Before @Before
public void testStartService() throws Exception { public void testStartService() throws Exception {
@@ -70,8 +68,8 @@ public class KeyManagerImplTest extends BrambleTestCase {
true, false)); true, false));
SimplexPluginFactory pluginFactory = SimplexPluginFactory pluginFactory =
context.mock(SimplexPluginFactory.class); context.mock(SimplexPluginFactory.class);
Collection<SimplexPluginFactory> factories = Collections Collection<SimplexPluginFactory> factories =
.singletonList(pluginFactory); singletonList(pluginFactory);
int maxLatency = 1337; int maxLatency = 1337;
context.checking(new Expectations() {{ context.checking(new Expectations() {{
@@ -110,7 +108,6 @@ public class KeyManagerImplTest extends BrambleTestCase {
}}); }});
keyManager.addContact(txn, contactId, secretKey, timestamp, alice); keyManager.addContact(txn, contactId, secretKey, timestamp, alice);
context.assertIsSatisfied();
} }
@Test @Test
@@ -122,10 +119,11 @@ public class KeyManagerImplTest extends BrambleTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(transportKeyManager).addUnboundKeys(txn, secretKey, oneOf(transportKeyManager).addUnboundKeys(txn, secretKey,
timestamp, alice); timestamp, alice);
will(returnValue(keySetId));
}}); }});
keyManager.addUnboundKeys(txn, secretKey, timestamp, alice); assertEquals(singletonList(keySetId),
context.assertIsSatisfied(); keyManager.addUnboundKeys(txn, secretKey, timestamp, alice));
} }
@Test @Test
@@ -153,7 +151,6 @@ public class KeyManagerImplTest extends BrambleTestCase {
assertEquals(streamContext, assertEquals(streamContext,
keyManager.getStreamContext(contactId, transportId)); keyManager.getStreamContext(contactId, transportId));
context.assertIsSatisfied();
} }
@Test @Test
@@ -176,7 +173,6 @@ public class KeyManagerImplTest extends BrambleTestCase {
assertEquals(streamContext, assertEquals(streamContext,
keyManager.getStreamContext(transportId, tag)); keyManager.getStreamContext(transportId, tag));
context.assertIsSatisfied();
} }
@Test @Test
@@ -190,8 +186,6 @@ public class KeyManagerImplTest extends BrambleTestCase {
keyManager.eventOccurred(event); keyManager.eventOccurred(event);
executor.runUntilIdle(); executor.runUntilIdle();
assertEquals(null, keyManager.getStreamContext(contactId, transportId)); assertEquals(null, keyManager.getStreamContext(contactId, transportId));
context.assertIsSatisfied();
} }
@Test @Test
@@ -211,8 +205,5 @@ public class KeyManagerImplTest extends BrambleTestCase {
keyManager.eventOccurred(event); keyManager.eventOccurred(event);
assertEquals(streamContext, assertEquals(streamContext,
keyManager.getStreamContext(inactiveContactId, transportId)); keyManager.getStreamContext(inactiveContactId, transportId));
context.assertIsSatisfied();
} }
} }

View File

@@ -179,7 +179,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
maxLatency); maxLatency);
// The timestamp is 1 ms before the start of rotation period 1000 // The timestamp is 1 ms before the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000 - 1; long timestamp = rotationPeriodLength * 1000 - 1;
transportKeyManager.addUnboundKeys(txn, masterKey, timestamp, alice); assertEquals(keySetId, transportKeyManager.addUnboundKeys(txn,
masterKey, timestamp, alice));
} }
@Test @Test