From 9cc8d4477842997e37db1c2869ff616a13383b35 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 19 May 2021 10:42:47 +0100 Subject: [PATCH 1/3] Add a key manager method for adding a single set of transport keys. --- .../bramble/api/transport/KeyManager.java | 19 +++++++++++++++++-- .../bramble/transport/KeyManagerImpl.java | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java index 50f7d8aa0..086e46416 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java @@ -22,8 +22,23 @@ public interface KeyManager { /** * Derives and stores a set of rotation mode transport keys for - * communicating with the given contact over each transport and returns the - * key set IDs. + * communicating with the given contact over the given transport and + * returns the key set ID. + *

+ * {@link StreamContext StreamContexts} for the contact can be created + * after this method has returned. + * + * @param alice True if the local party is Alice + * @param active Whether the derived keys can be used for outgoing streams + */ + KeySetId addRotationKeys(Transaction txn, ContactId c, TransportId t, + SecretKey rootKey, long timestamp, boolean alice, + boolean active) throws DbException; + + /** + * Derives and stores a set of rotation mode transport keys for + * communicating with the given contact over each supported transport and + * returns the key set IDs. *

* {@link StreamContext StreamContexts} for the contact can be created * after this method has returned. diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java index 58aeb7cea..ded31ab50 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java @@ -101,9 +101,17 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { } @Override - public Map addRotationKeys( - Transaction txn, ContactId c, SecretKey rootKey, long timestamp, - boolean alice, boolean active) throws DbException { + public KeySetId addRotationKeys(Transaction txn, ContactId c, + TransportId t, SecretKey rootKey, long timestamp, boolean alice, + boolean active) throws DbException { + return withManager(t, m -> + m.addRotationKeys(txn, c, rootKey, timestamp, alice, active)); + } + + @Override + public Map addRotationKeys(Transaction txn, + ContactId c, SecretKey rootKey, long timestamp, boolean alice, + boolean active) throws DbException { Map ids = new HashMap<>(); for (Entry e : managers.entrySet()) { TransportId t = e.getKey(); @@ -137,7 +145,7 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { PendingContactId p, PublicKey theirPublicKey, KeyPair ourKeyPair) throws DbException, GeneralSecurityException { SecretKey staticMasterKey = transportCrypto - .deriveStaticMasterKey(theirPublicKey, ourKeyPair); + .deriveStaticMasterKey(theirPublicKey, ourKeyPair); SecretKey rootKey = transportCrypto.deriveHandshakeRootKey(staticMasterKey, true); boolean alice = transportCrypto.isAlice(theirPublicKey, ourKeyPair); From 6e6cadd3adb561dec5b85dc5c2f075f6140700ee Mon Sep 17 00:00:00 2001 From: akwizgran Date: Fri, 21 May 2021 13:18:59 +0100 Subject: [PATCH 2/3] Refactor KeyManager startup so managers are created earlier. --- .../bramble/api/transport/KeyManager.java | 3 +- .../bramble/transport/KeyManagerImpl.java | 38 ++++++++++--------- .../bramble/transport/KeyManagerImplTest.java | 23 +++++++---- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java index 086e46416..db906e649 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/transport/KeyManager.java @@ -23,7 +23,7 @@ public interface KeyManager { /** * Derives and stores a set of rotation mode transport keys for * communicating with the given contact over the given transport and - * returns the key set ID. + * returns the key set ID, or null if the transport is not supported. *

* {@link StreamContext StreamContexts} for the contact can be created * after this method has returned. @@ -31,6 +31,7 @@ public interface KeyManager { * @param alice True if the local party is Alice * @param active Whether the derived keys can be used for outgoing streams */ + @Nullable KeySetId addRotationKeys(Transaction txn, ContactId c, TransportId t, SecretKey rootKey, long timestamp, boolean alice, boolean active) throws DbException; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java index ded31ab50..8d6ce998c 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/KeyManagerImpl.java @@ -51,7 +51,6 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { private final DatabaseComponent db; private final Executor dbExecutor; private final PluginConfig pluginConfig; - private final TransportKeyManagerFactory transportKeyManagerFactory; private final TransportCrypto transportCrypto; private final ConcurrentHashMap managers; @@ -61,34 +60,39 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { KeyManagerImpl(DatabaseComponent db, @DatabaseExecutor Executor dbExecutor, PluginConfig pluginConfig, - TransportKeyManagerFactory transportKeyManagerFactory, - TransportCrypto transportCrypto) { + TransportCrypto transportCrypto, + TransportKeyManagerFactory transportKeyManagerFactory) { this.db = db; this.dbExecutor = dbExecutor; this.pluginConfig = pluginConfig; - this.transportKeyManagerFactory = transportKeyManagerFactory; this.transportCrypto = transportCrypto; managers = new ConcurrentHashMap<>(); + for (SimplexPluginFactory f : pluginConfig.getSimplexFactories()) { + TransportKeyManager m = transportKeyManagerFactory. + createTransportKeyManager(f.getId(), f.getMaxLatency()); + managers.put(f.getId(), m); + } + for (DuplexPluginFactory f : pluginConfig.getDuplexFactories()) { + TransportKeyManager m = transportKeyManagerFactory. + createTransportKeyManager(f.getId(), f.getMaxLatency()); + managers.put(f.getId(), m); + } } @Override public void startService() throws ServiceException { if (used.getAndSet(true)) throw new IllegalStateException(); - Map transports = new HashMap<>(); - for (SimplexPluginFactory f : pluginConfig.getSimplexFactories()) - transports.put(f.getId(), f.getMaxLatency()); - for (DuplexPluginFactory f : pluginConfig.getDuplexFactories()) - transports.put(f.getId(), f.getMaxLatency()); try { db.transaction(false, txn -> { - for (Entry e : transports.entrySet()) - db.addTransport(txn, e.getKey(), e.getValue()); - for (Entry e : transports.entrySet()) { - TransportKeyManager m = transportKeyManagerFactory - .createTransportKeyManager(e.getKey(), - e.getValue()); - managers.put(e.getKey(), m); - m.start(txn); + for (SimplexPluginFactory f : + pluginConfig.getSimplexFactories()) { + db.addTransport(txn, f.getId(), f.getMaxLatency()); + managers.get(f.getId()).start(txn); + } + for (DuplexPluginFactory f : + pluginConfig.getDuplexFactories()) { + db.addTransport(txn, f.getId(), f.getMaxLatency()); + managers.get(f.getId()).start(txn); } }); } catch (DbException e) { diff --git a/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java index 142f06538..02bb20244 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.Map; import java.util.Random; +import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH; @@ -71,8 +72,7 @@ public class KeyManagerImplTest extends BrambleMockTestCase { private final SecretKey rootKey = getSecretKey(); private final Random random = new Random(); - private final KeyManagerImpl keyManager = new KeyManagerImpl(db, executor, - pluginConfig, transportKeyManagerFactory, transportCrypto); + private KeyManagerImpl keyManager; @Before public void testStartService() throws Exception { @@ -83,18 +83,25 @@ public class KeyManagerImplTest extends BrambleMockTestCase { singletonList(pluginFactory); int maxLatency = 1337; - context.checking(new DbExpectations() {{ - oneOf(pluginConfig).getSimplexFactories(); + context.checking(new Expectations() {{ + allowing(pluginConfig).getSimplexFactories(); will(returnValue(factories)); - oneOf(pluginFactory).getId(); + allowing(pluginFactory).getId(); will(returnValue(transportId)); - oneOf(pluginFactory).getMaxLatency(); + allowing(pluginFactory).getMaxLatency(); will(returnValue(maxLatency)); - oneOf(db).addTransport(txn, transportId, maxLatency); + allowing(pluginConfig).getDuplexFactories(); + will(returnValue(emptyList())); oneOf(transportKeyManagerFactory) .createTransportKeyManager(transportId, maxLatency); will(returnValue(transportKeyManager)); - oneOf(pluginConfig).getDuplexFactories(); + }}); + + keyManager = new KeyManagerImpl(db, executor, + pluginConfig, transportCrypto, transportKeyManagerFactory); + + context.checking(new DbExpectations() {{ + oneOf(db).addTransport(txn, transportId, maxLatency); oneOf(db).transaction(with(false), withDbRunnable(txn)); oneOf(transportKeyManager).start(txn); }}); From e05575b9562efea2328a6e5f12b15a38e8744c20 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 8 Jun 2021 15:51:29 +0100 Subject: [PATCH 3/3] Add unit tests for addRotationKeys() methods. --- .../bramble/transport/KeyManagerImplTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java index 02bb20244..5936b89e1 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/transport/KeyManagerImplTest.java @@ -242,4 +242,37 @@ public class KeyManagerImplTest extends BrambleMockTestCase { keyManager.eventOccurred(event); executor.runUntilIdle(); } + + @Test + public void testAddMultipleRotationKeySets() throws Exception { + long timestamp = System.currentTimeMillis(); + boolean alice = random.nextBoolean(); + boolean active = random.nextBoolean(); + + context.checking(new Expectations() {{ + oneOf(transportKeyManager).addRotationKeys(txn, contactId, + rootKey, timestamp, alice, active); + will(returnValue(keySetId)); + }}); + + assertEquals(singletonMap(transportId, keySetId), + keyManager.addRotationKeys(txn, contactId, rootKey, timestamp, + alice, active)); + } + + @Test + public void testAddSingleRotationKeySet() throws Exception { + long timestamp = System.currentTimeMillis(); + boolean alice = random.nextBoolean(); + boolean active = random.nextBoolean(); + + context.checking(new Expectations() {{ + oneOf(transportKeyManager).addRotationKeys(txn, contactId, + rootKey, timestamp, alice, active); + will(returnValue(keySetId)); + }}); + + assertEquals(keySetId, keyManager.addRotationKeys(txn, contactId, + transportId, rootKey, timestamp, alice, active)); + } }