mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Merge branch 'merge-handshake-and-transport-keys' into 'master'
Add support for handshake keys to KeyManager See merge request briar/briar!1088
This commit is contained in:
@@ -1,167 +0,0 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.crypto.TransportCrypto;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.transport.HandshakeKeys;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.briarproject.bramble.crypto.KeyDerivationTestUtils.assertAllDifferent;
|
||||
import static org.briarproject.bramble.crypto.KeyDerivationTestUtils.assertMatches;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
public class HandshakeKeyDerivationTest extends BrambleTestCase {
|
||||
|
||||
private final CryptoComponent crypto =
|
||||
new CryptoComponentImpl(new TestSecureRandomProvider(), null);
|
||||
private final TransportCrypto transportCrypto =
|
||||
new TransportCryptoImpl(crypto);
|
||||
private final TransportId transportId = getTransportId();
|
||||
private final SecretKey rootKey = getSecretKey();
|
||||
|
||||
@Test
|
||||
public void testKeysAreDistinct() {
|
||||
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
assertAllDifferent(kA);
|
||||
assertAllDifferent(kB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreNotUpdatedToPreviousPeriod() {
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 122);
|
||||
assertSame(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreNotUpdatedToCurrentPeriod() {
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 123);
|
||||
assertSame(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreUpdatedByOnePeriod() {
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 124);
|
||||
assertSame(k.getCurrentIncomingKeys(), k1.getPreviousIncomingKeys());
|
||||
assertSame(k.getNextIncomingKeys(), k1.getCurrentIncomingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreUpdatedByTwoPeriods() {
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 125);
|
||||
assertSame(k.getNextIncomingKeys(), k1.getPreviousIncomingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreUpdatedByThreePeriods() {
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 126);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrentKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
// Alice's incoming keys should equal Bob's outgoing keys
|
||||
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
// Bob's incoming keys should equal Alice's outgoing keys
|
||||
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
// Update into the future
|
||||
kA = transportCrypto.updateHandshakeKeys(kA, 456);
|
||||
kB = transportCrypto.updateHandshakeKeys(kB, 456);
|
||||
// Alice's incoming keys should equal Bob's outgoing keys
|
||||
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
// Bob's incoming keys should equal Alice's outgoing keys
|
||||
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
// Compare Alice's previous keys in period 456 with Bob's current keys
|
||||
// in period 455
|
||||
kA = transportCrypto.updateHandshakeKeys(kA, 456);
|
||||
kB = transportCrypto.updateHandshakeKeys(kB, 455);
|
||||
// Alice's previous incoming keys should equal Bob's current
|
||||
// outgoing keys
|
||||
assertMatches(kA.getPreviousIncomingKeys(),
|
||||
kB.getCurrentOutgoingKeys());
|
||||
// Compare Alice's current keys in period 456 with Bob's previous keys
|
||||
// in period 457
|
||||
kB = transportCrypto.updateHandshakeKeys(kB, 457);
|
||||
// Bob's previous incoming keys should equal Alice's current
|
||||
// outgoing keys
|
||||
assertMatches(kB.getPreviousIncomingKeys(),
|
||||
kA.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
// Compare Alice's current keys in period 456 with Bob's next keys in
|
||||
// period 455
|
||||
kA = transportCrypto.updateHandshakeKeys(kA, 456);
|
||||
kB = transportCrypto.updateHandshakeKeys(kB, 455);
|
||||
// Bob's next incoming keys should equal Alice's current outgoing keys
|
||||
assertMatches(kB.getNextIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
// Compare Alice's next keys in period 456 with Bob's current keys
|
||||
// in period 457
|
||||
kB = transportCrypto.updateHandshakeKeys(kB, 457);
|
||||
// Alice's next incoming keys should equal Bob's current outgoing keys
|
||||
assertMatches(kA.getNextIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRootKeyAffectsOutput() {
|
||||
SecretKey rootKey1 = getSecretKey();
|
||||
assertFalse(Arrays.equals(rootKey.getBytes(), rootKey1.getBytes()));
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey1, 123, true);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransportIdAffectsOutput() {
|
||||
TransportId transportId1 = getTransportId();
|
||||
assertNotEquals(transportId.getString(), transportId1.getString());
|
||||
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
HandshakeKeys k1 = transportCrypto.deriveHandshakeKeys(transportId1,
|
||||
rootKey, 123, true);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.transport.AbstractTransportKeys;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
class KeyDerivationTestUtils {
|
||||
|
||||
static void assertAllDifferent(AbstractTransportKeys... transportKeys) {
|
||||
List<SecretKey> secretKeys = new ArrayList<>();
|
||||
for (AbstractTransportKeys k : transportKeys) {
|
||||
secretKeys.add(k.getPreviousIncomingKeys().getTagKey());
|
||||
secretKeys.add(k.getPreviousIncomingKeys().getHeaderKey());
|
||||
secretKeys.add(k.getCurrentIncomingKeys().getTagKey());
|
||||
secretKeys.add(k.getCurrentIncomingKeys().getHeaderKey());
|
||||
secretKeys.add(k.getNextIncomingKeys().getTagKey());
|
||||
secretKeys.add(k.getNextIncomingKeys().getHeaderKey());
|
||||
secretKeys.add(k.getCurrentOutgoingKeys().getTagKey());
|
||||
secretKeys.add(k.getCurrentOutgoingKeys().getHeaderKey());
|
||||
}
|
||||
assertAllDifferent(secretKeys);
|
||||
}
|
||||
|
||||
static void assertAllDifferent(List<SecretKey> keys) {
|
||||
Set<Bytes> set = new HashSet<>();
|
||||
for (SecretKey k : keys) assertTrue(set.add(new Bytes(k.getBytes())));
|
||||
}
|
||||
|
||||
static void assertMatches(IncomingKeys in, OutgoingKeys out) {
|
||||
assertArrayEquals(in.getTagKey().getBytes(),
|
||||
out.getTagKey().getBytes());
|
||||
assertArrayEquals(in.getHeaderKey().getBytes(),
|
||||
out.getHeaderKey().getBytes());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,30 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.crypto.TransportCrypto;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.briarproject.bramble.crypto.KeyDerivationTestUtils.assertAllDifferent;
|
||||
import static org.briarproject.bramble.crypto.KeyDerivationTestUtils.assertMatches;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TransportKeyDerivationTest extends BrambleTestCase {
|
||||
|
||||
@@ -29,70 +36,70 @@ public class TransportKeyDerivationTest extends BrambleTestCase {
|
||||
private final SecretKey rootKey = getSecretKey();
|
||||
|
||||
@Test
|
||||
public void testKeysAreDistinct() {
|
||||
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
|
||||
public void testRotationKeysAreDistinct() {
|
||||
TransportKeys kA = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kB = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, false, true);
|
||||
assertAllDifferent(kA);
|
||||
assertAllDifferent(kB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreNotRotatedToPreviousPeriod() {
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
public void testRotationKeysAreNotRotatedToPreviousPeriod() {
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.rotateTransportKeys(k, 122);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 122);
|
||||
assertSame(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreNotRotatedToCurrentPeriod() {
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
public void testRotationKeysAreNotRotatedToCurrentPeriod() {
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.rotateTransportKeys(k, 123);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 123);
|
||||
assertSame(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreRotatedByOnePeriod() {
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
public void testRotationKeysAreRotatedByOnePeriod() {
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.rotateTransportKeys(k, 124);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 124);
|
||||
assertSame(k.getCurrentIncomingKeys(), k1.getPreviousIncomingKeys());
|
||||
assertSame(k.getNextIncomingKeys(), k1.getCurrentIncomingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreRotatedByTwoPeriods() {
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
public void testRotationKeysAreRotatedByTwoPeriods() {
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.rotateTransportKeys(k, 125);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 125);
|
||||
assertSame(k.getNextIncomingKeys(), k1.getPreviousIncomingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreRotatedByThreePeriods() {
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
public void testRotationKeysAreRotatedByThreePeriods() {
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.rotateTransportKeys(k, 126);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 126);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrentKeysMatchContact() {
|
||||
public void testCurrentRotationKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kA = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kB = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, false, true);
|
||||
// Alice's incoming keys should equal Bob's outgoing keys
|
||||
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
// Bob's incoming keys should equal Alice's outgoing keys
|
||||
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
// Rotate into the future
|
||||
kA = transportCrypto.rotateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.rotateTransportKeys(kB, 456);
|
||||
kA = transportCrypto.updateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 456);
|
||||
// Alice's incoming keys should equal Bob's outgoing keys
|
||||
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
// Bob's incoming keys should equal Alice's outgoing keys
|
||||
@@ -100,23 +107,23 @@ public class TransportKeyDerivationTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousKeysMatchContact() {
|
||||
public void testPreviousRotationKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kA = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kB = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, false, true);
|
||||
// Compare Alice's previous keys in period 456 with Bob's current keys
|
||||
// in period 455
|
||||
kA = transportCrypto.rotateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.rotateTransportKeys(kB, 455);
|
||||
kA = transportCrypto.updateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 455);
|
||||
// Alice's previous incoming keys should equal Bob's current
|
||||
// outgoing keys
|
||||
assertMatches(kA.getPreviousIncomingKeys(),
|
||||
kB.getCurrentOutgoingKeys());
|
||||
// Compare Alice's current keys in period 456 with Bob's previous keys
|
||||
// in period 457
|
||||
kB = transportCrypto.rotateTransportKeys(kB, 457);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 457);
|
||||
// Bob's previous incoming keys should equal Alice's current
|
||||
// outgoing keys
|
||||
assertMatches(kB.getPreviousIncomingKeys(),
|
||||
@@ -124,44 +131,208 @@ public class TransportKeyDerivationTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextKeysMatchContact() {
|
||||
public void testNextRotationKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kA = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys kB = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, false, true);
|
||||
// Compare Alice's current keys in period 456 with Bob's next keys in
|
||||
// period 455
|
||||
kA = transportCrypto.rotateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.rotateTransportKeys(kB, 455);
|
||||
kA = transportCrypto.updateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 455);
|
||||
// Bob's next incoming keys should equal Alice's current outgoing keys
|
||||
assertMatches(kB.getNextIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
// Compare Alice's next keys in period 456 with Bob's current keys
|
||||
// in period 457
|
||||
kB = transportCrypto.rotateTransportKeys(kB, 457);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 457);
|
||||
// Alice's next incoming keys should equal Bob's current outgoing keys
|
||||
assertMatches(kA.getNextIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRootKeyAffectsOutput() {
|
||||
public void testRootKeyAffectsRotationKeyDerivation() {
|
||||
SecretKey rootKey1 = getSecretKey();
|
||||
assertFalse(Arrays.equals(rootKey.getBytes(), rootKey1.getBytes()));
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys k1 = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey1, 123, true, true);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransportIdAffectsOutput() {
|
||||
public void testTransportIdAffectsRotationKeyDerivation() {
|
||||
TransportId transportId1 = getTransportId();
|
||||
assertNotEquals(transportId.getString(), transportId1.getString());
|
||||
TransportKeys k = transportCrypto.deriveTransportKeys(transportId,
|
||||
TransportKeys k = transportCrypto.deriveRotationKeys(transportId,
|
||||
rootKey, 123, true, true);
|
||||
TransportKeys k1 = transportCrypto.deriveTransportKeys(transportId1,
|
||||
TransportKeys k1 = transportCrypto.deriveRotationKeys(transportId1,
|
||||
rootKey, 123, true, true);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysAreDistinct() {
|
||||
TransportKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
assertAllDifferent(kA);
|
||||
assertAllDifferent(kB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysAreNotUpdatedToPreviousPeriod() {
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 122);
|
||||
assertSame(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysAreNotUpdatedToCurrentPeriod() {
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 123);
|
||||
assertSame(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysAreUpdatedByOnePeriod() {
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 124);
|
||||
assertSame(k.getCurrentIncomingKeys(), k1.getPreviousIncomingKeys());
|
||||
assertSame(k.getNextIncomingKeys(), k1.getCurrentIncomingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysAreUpdatedByTwoPeriods() {
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 125);
|
||||
assertSame(k.getNextIncomingKeys(), k1.getPreviousIncomingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysAreUpdatedByThreePeriods() {
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.updateTransportKeys(k, 126);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrentHandshakeKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
TransportKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
// Alice's incoming keys should equal Bob's outgoing keys
|
||||
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
// Bob's incoming keys should equal Alice's outgoing keys
|
||||
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
// Update into the future
|
||||
kA = transportCrypto.updateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 456);
|
||||
// Alice's incoming keys should equal Bob's outgoing keys
|
||||
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
// Bob's incoming keys should equal Alice's outgoing keys
|
||||
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousHandshakeKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
TransportKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
// Compare Alice's previous keys in period 456 with Bob's current keys
|
||||
// in period 455
|
||||
kA = transportCrypto.updateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 455);
|
||||
// Alice's previous incoming keys should equal Bob's current
|
||||
// outgoing keys
|
||||
assertMatches(kA.getPreviousIncomingKeys(),
|
||||
kB.getCurrentOutgoingKeys());
|
||||
// Compare Alice's current keys in period 456 with Bob's previous keys
|
||||
// in period 457
|
||||
kB = transportCrypto.updateTransportKeys(kB, 457);
|
||||
// Bob's previous incoming keys should equal Alice's current
|
||||
// outgoing keys
|
||||
assertMatches(kB.getPreviousIncomingKeys(),
|
||||
kA.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextHandshakeKeysMatchContact() {
|
||||
// Start in time period 123
|
||||
TransportKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, false);
|
||||
// Compare Alice's current keys in period 456 with Bob's next keys in
|
||||
// period 455
|
||||
kA = transportCrypto.updateTransportKeys(kA, 456);
|
||||
kB = transportCrypto.updateTransportKeys(kB, 455);
|
||||
// Bob's next incoming keys should equal Alice's current outgoing keys
|
||||
assertMatches(kB.getNextIncomingKeys(), kA.getCurrentOutgoingKeys());
|
||||
// Compare Alice's next keys in period 456 with Bob's current keys
|
||||
// in period 457
|
||||
kB = transportCrypto.updateTransportKeys(kB, 457);
|
||||
// Alice's next incoming keys should equal Bob's current outgoing keys
|
||||
assertMatches(kA.getNextIncomingKeys(), kB.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRootKeyAffectsHandshakeKeyDerivation() {
|
||||
SecretKey rootKey1 = getSecretKey();
|
||||
assertFalse(Arrays.equals(rootKey.getBytes(), rootKey1.getBytes()));
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey1, 123, true);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransportIdAffectsHandshakeKeyDerivation() {
|
||||
TransportId transportId1 = getTransportId();
|
||||
assertNotEquals(transportId.getString(), transportId1.getString());
|
||||
TransportKeys k = transportCrypto.deriveHandshakeKeys(transportId,
|
||||
rootKey, 123, true);
|
||||
TransportKeys k1 = transportCrypto.deriveHandshakeKeys(transportId1,
|
||||
rootKey, 123, true);
|
||||
assertAllDifferent(k, k1);
|
||||
}
|
||||
|
||||
private void assertAllDifferent(TransportKeys... transportKeys) {
|
||||
List<SecretKey> secretKeys = new ArrayList<>();
|
||||
for (TransportKeys k : transportKeys) {
|
||||
secretKeys.add(k.getPreviousIncomingKeys().getTagKey());
|
||||
secretKeys.add(k.getPreviousIncomingKeys().getHeaderKey());
|
||||
secretKeys.add(k.getCurrentIncomingKeys().getTagKey());
|
||||
secretKeys.add(k.getCurrentIncomingKeys().getHeaderKey());
|
||||
secretKeys.add(k.getNextIncomingKeys().getTagKey());
|
||||
secretKeys.add(k.getNextIncomingKeys().getHeaderKey());
|
||||
secretKeys.add(k.getCurrentOutgoingKeys().getTagKey());
|
||||
secretKeys.add(k.getCurrentOutgoingKeys().getHeaderKey());
|
||||
}
|
||||
assertAllDifferent(secretKeys);
|
||||
}
|
||||
|
||||
private void assertAllDifferent(List<SecretKey> keys) {
|
||||
Set<Bytes> set = new HashSet<>();
|
||||
for (SecretKey k : keys) assertTrue(set.add(new Bytes(k.getBytes())));
|
||||
}
|
||||
|
||||
private void assertMatches(IncomingKeys in, OutgoingKeys out) {
|
||||
assertArrayEquals(in.getTagKey().getBytes(),
|
||||
out.getTagKey().getBytes());
|
||||
assertArrayEquals(in.getHeaderKey().getBytes(),
|
||||
out.getHeaderKey().getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,11 +48,10 @@ import org.briarproject.bramble.api.sync.event.MessageToAckEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessageToRequestEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
|
||||
import org.briarproject.bramble.api.transport.HandshakeKeys;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySetId;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.CaptureArgumentAction;
|
||||
@@ -119,7 +118,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
private final int maxLatency;
|
||||
private final ContactId contactId;
|
||||
private final Contact contact;
|
||||
private final TransportKeySetId keySetId;
|
||||
private final KeySetId keySetId;
|
||||
private final PendingContactId pendingContactId;
|
||||
|
||||
public DatabaseComponentImplTest() {
|
||||
@@ -141,7 +140,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
contact = getContact(author, localAuthor.getId(), true);
|
||||
contactId = contact.getId();
|
||||
alias = contact.getAlias();
|
||||
keySetId = new TransportKeySetId(345);
|
||||
keySetId = new KeySetId(345);
|
||||
pendingContactId = new PendingContactId(getRandomId());
|
||||
}
|
||||
|
||||
@@ -286,24 +285,15 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
// Check whether the contact is in the DB (which it's not)
|
||||
exactly(17).of(database).startTransaction();
|
||||
exactly(16).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(17).of(database).containsContact(txn, contactId);
|
||||
exactly(16).of(database).containsContact(txn, contactId);
|
||||
will(returnValue(false));
|
||||
exactly(17).of(database).abortTransaction(txn);
|
||||
exactly(16).of(database).abortTransaction(txn);
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addHandshakeKeys(transaction, contactId,
|
||||
createHandshakeKeys()));
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addTransportKeys(transaction, contactId,
|
||||
@@ -499,8 +489,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
exactly(8).of(database).containsGroup(txn, groupId);
|
||||
will(returnValue(false));
|
||||
exactly(8).of(database).abortTransaction(txn);
|
||||
// This is needed for getMessageStatus() and setGroupVisibility()
|
||||
exactly(2).of(database).containsContact(txn, contactId);
|
||||
// Allow other checks to pass
|
||||
allowing(database).containsContact(txn, contactId);
|
||||
will(returnValue(true));
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
@@ -583,8 +573,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
exactly(11).of(database).containsMessage(txn, messageId);
|
||||
will(returnValue(false));
|
||||
exactly(11).of(database).abortTransaction(txn);
|
||||
// This is needed for getMessageStatus() to proceed
|
||||
exactly(1).of(database).containsContact(txn, contactId);
|
||||
// Allow other checks to pass
|
||||
allowing(database).containsContact(txn, contactId);
|
||||
will(returnValue(true));
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
@@ -684,15 +674,38 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
// Check whether the transport is in the DB (which it's not)
|
||||
exactly(5).of(database).startTransaction();
|
||||
exactly(8).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(5).of(database).containsTransport(txn, transportId);
|
||||
exactly(8).of(database).containsTransport(txn, transportId);
|
||||
will(returnValue(false));
|
||||
exactly(5).of(database).abortTransaction(txn);
|
||||
exactly(8).of(database).abortTransaction(txn);
|
||||
// Allow other checks to pass
|
||||
allowing(database).containsContact(txn, contactId);
|
||||
will(returnValue(true));
|
||||
allowing(database).containsPendingContact(txn, pendingContactId);
|
||||
will(returnValue(true));
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addTransportKeys(transaction, contactId,
|
||||
createHandshakeKeys()));
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addTransportKeys(transaction, pendingContactId,
|
||||
createHandshakeKeys()));
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.getTransportKeys(transaction, transportId));
|
||||
@@ -712,7 +725,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.removeTransport(transaction, transportId));
|
||||
db.removeTransportKeys(transaction, transportId, keySetId));
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
@@ -720,7 +733,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.removeTransportKeys(transaction, transportId, keySetId));
|
||||
db.removeTransport(transaction, transportId));
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
@@ -734,6 +747,15 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.setTransportKeysActive(transaction, transportId,
|
||||
keySetId));
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -753,7 +775,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addHandshakeKeys(transaction, pendingContactId,
|
||||
db.addTransportKeys(transaction, pendingContactId,
|
||||
createHandshakeKeys()));
|
||||
fail();
|
||||
} catch (NoSuchPendingContactException expected) {
|
||||
@@ -1169,7 +1191,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
public void testTransportKeys() throws Exception {
|
||||
TransportKeys transportKeys = createTransportKeys();
|
||||
TransportKeySet ks =
|
||||
new TransportKeySet(keySetId, contactId, transportKeys);
|
||||
new TransportKeySet(keySetId, contactId, null, transportKeys);
|
||||
Collection<TransportKeySet> keys = singletonList(ks);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -1297,7 +1319,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
});
|
||||
}
|
||||
|
||||
private HandshakeKeys createHandshakeKeys() {
|
||||
private TransportKeys createHandshakeKeys() {
|
||||
SecretKey inPrevTagKey = getSecretKey();
|
||||
SecretKey inPrevHeaderKey = getSecretKey();
|
||||
IncomingKeys inPrev = new IncomingKeys(inPrevTagKey, inPrevHeaderKey,
|
||||
@@ -1314,7 +1336,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
SecretKey outCurrHeaderKey = getSecretKey();
|
||||
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
|
||||
2, 456, true);
|
||||
return new HandshakeKeys(transportId, inPrev, inCurr, inNext, outCurr,
|
||||
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr,
|
||||
getSecretKey(), true);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,13 +24,10 @@ import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.MessageStatus;
|
||||
import org.briarproject.bramble.api.sync.validation.MessageState;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.transport.HandshakeKeySet;
|
||||
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
|
||||
import org.briarproject.bramble.api.transport.HandshakeKeys;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySetId;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.system.SystemClock;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
@@ -114,8 +111,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
private final MessageId messageId;
|
||||
private final TransportId transportId;
|
||||
private final ContactId contactId;
|
||||
private final TransportKeySetId keySetId, keySetId1;
|
||||
private final HandshakeKeySetId handshakeKeySetId, handshakeKeySetId1;
|
||||
private final KeySetId keySetId, keySetId1;
|
||||
private final PendingContact pendingContact;
|
||||
private final Random random = new Random();
|
||||
|
||||
@@ -131,10 +127,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
messageId = message.getId();
|
||||
transportId = getTransportId();
|
||||
contactId = new ContactId(1);
|
||||
keySetId = new TransportKeySetId(1);
|
||||
keySetId1 = new TransportKeySetId(2);
|
||||
handshakeKeySetId = new HandshakeKeySetId(1);
|
||||
handshakeKeySetId1 = new HandshakeKeySetId(2);
|
||||
keySetId = new KeySetId(1);
|
||||
keySetId1 = new KeySetId(2);
|
||||
pendingContact = getPendingContact();
|
||||
}
|
||||
|
||||
@@ -703,14 +697,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// Rotate the transport keys
|
||||
TransportKeys rotated = createTransportKeys(timePeriod + 1, active);
|
||||
TransportKeys rotated1 =
|
||||
// Update the transport keys
|
||||
TransportKeys updated = createTransportKeys(timePeriod + 1, active);
|
||||
TransportKeys updated1 =
|
||||
createTransportKeys(timePeriod1 + 1, active);
|
||||
db.updateTransportKeys(txn, new TransportKeySet(keySetId, contactId,
|
||||
rotated));
|
||||
null, updated));
|
||||
db.updateTransportKeys(txn, new TransportKeySet(keySetId1, contactId,
|
||||
rotated1));
|
||||
null, updated1));
|
||||
|
||||
// Retrieve the transport keys again
|
||||
allKeys = db.getTransportKeys(txn, transportId);
|
||||
@@ -718,10 +712,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
for (TransportKeySet ks : allKeys) {
|
||||
assertEquals(contactId, ks.getContactId());
|
||||
if (ks.getKeySetId().equals(keySetId)) {
|
||||
assertKeysEquals(rotated, ks.getKeys());
|
||||
assertKeysEquals(updated, ks.getKeys());
|
||||
} else {
|
||||
assertEquals(keySetId1, ks.getKeySetId());
|
||||
assertKeysEquals(rotated1, ks.getKeys());
|
||||
assertKeysEquals(updated1, ks.getKeys());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -745,6 +739,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
actual.getNextIncomingKeys());
|
||||
assertKeysEquals(expected.getCurrentOutgoingKeys(),
|
||||
actual.getCurrentOutgoingKeys());
|
||||
if (expected.isHandshakeMode()) {
|
||||
assertTrue(actual.isHandshakeMode());
|
||||
assertArrayEquals(expected.getRootKey().getBytes(),
|
||||
actual.getRootKey().getBytes());
|
||||
assertEquals(expected.isAlice(), actual.isAlice());
|
||||
} else {
|
||||
assertFalse(actual.isHandshakeMode());
|
||||
}
|
||||
}
|
||||
|
||||
private void assertKeysEquals(IncomingKeys expected, IncomingKeys actual) {
|
||||
@@ -773,154 +775,135 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
boolean alice = random.nextBoolean();
|
||||
SecretKey rootKey = getSecretKey();
|
||||
SecretKey rootKey1 = getSecretKey();
|
||||
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
HandshakeKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
|
||||
TransportKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
TransportKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Initially there should be no handshake keys in the database
|
||||
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||
|
||||
// Add the contact, the transport and the handshake keys
|
||||
db.addIdentity(txn, identity);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
assertEquals(handshakeKeySetId,
|
||||
db.addHandshakeKeys(txn, contactId, keys));
|
||||
assertEquals(handshakeKeySetId1,
|
||||
db.addHandshakeKeys(txn, contactId, keys1));
|
||||
assertEquals(keySetId, db.addTransportKeys(txn, contactId, keys));
|
||||
assertEquals(keySetId1, db.addTransportKeys(txn, contactId, keys1));
|
||||
|
||||
// Retrieve the handshake keys
|
||||
Collection<HandshakeKeySet> allKeys =
|
||||
db.getHandshakeKeys(txn, transportId);
|
||||
Collection<TransportKeySet> allKeys =
|
||||
db.getTransportKeys(txn, transportId);
|
||||
assertEquals(2, allKeys.size());
|
||||
for (HandshakeKeySet ks : allKeys) {
|
||||
for (TransportKeySet ks : allKeys) {
|
||||
assertEquals(contactId, ks.getContactId());
|
||||
assertNull(ks.getPendingContactId());
|
||||
if (ks.getKeySetId().equals(handshakeKeySetId)) {
|
||||
if (ks.getKeySetId().equals(keySetId)) {
|
||||
assertKeysEquals(keys, ks.getKeys());
|
||||
} else {
|
||||
assertEquals(handshakeKeySetId1, ks.getKeySetId());
|
||||
assertEquals(keySetId1, ks.getKeySetId());
|
||||
assertKeysEquals(keys1, ks.getKeys());
|
||||
}
|
||||
}
|
||||
|
||||
// Update the handshake keys
|
||||
HandshakeKeys updated =
|
||||
TransportKeys updated =
|
||||
createHandshakeKeys(timePeriod + 1, rootKey, alice);
|
||||
HandshakeKeys updated1 =
|
||||
TransportKeys updated1 =
|
||||
createHandshakeKeys(timePeriod1 + 1, rootKey1, alice);
|
||||
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId,
|
||||
contactId, updated));
|
||||
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId1,
|
||||
contactId, updated1));
|
||||
db.updateTransportKeys(txn, new TransportKeySet(keySetId, contactId,
|
||||
null, updated));
|
||||
db.updateTransportKeys(txn, new TransportKeySet(keySetId1, contactId,
|
||||
null, updated1));
|
||||
|
||||
// Retrieve the handshake keys again
|
||||
allKeys = db.getHandshakeKeys(txn, transportId);
|
||||
allKeys = db.getTransportKeys(txn, transportId);
|
||||
assertEquals(2, allKeys.size());
|
||||
for (HandshakeKeySet ks : allKeys) {
|
||||
for (TransportKeySet ks : allKeys) {
|
||||
assertEquals(contactId, ks.getContactId());
|
||||
assertNull(ks.getPendingContactId());
|
||||
if (ks.getKeySetId().equals(handshakeKeySetId)) {
|
||||
if (ks.getKeySetId().equals(keySetId)) {
|
||||
assertKeysEquals(updated, ks.getKeys());
|
||||
} else {
|
||||
assertEquals(handshakeKeySetId1, ks.getKeySetId());
|
||||
assertEquals(keySetId1, ks.getKeySetId());
|
||||
assertKeysEquals(updated1, ks.getKeys());
|
||||
}
|
||||
}
|
||||
|
||||
// Removing the contact should remove the handshake keys
|
||||
db.removeContact(txn, contactId);
|
||||
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
}
|
||||
|
||||
private void assertKeysEquals(HandshakeKeys expected,
|
||||
HandshakeKeys actual) {
|
||||
assertEquals(expected.getTransportId(), actual.getTransportId());
|
||||
assertEquals(expected.getTimePeriod(), actual.getTimePeriod());
|
||||
assertArrayEquals(expected.getRootKey().getBytes(),
|
||||
actual.getRootKey().getBytes());
|
||||
assertEquals(expected.isAlice(), actual.isAlice());
|
||||
assertKeysEquals(expected.getPreviousIncomingKeys(),
|
||||
actual.getPreviousIncomingKeys());
|
||||
assertKeysEquals(expected.getCurrentIncomingKeys(),
|
||||
actual.getCurrentIncomingKeys());
|
||||
assertKeysEquals(expected.getNextIncomingKeys(),
|
||||
actual.getNextIncomingKeys());
|
||||
assertKeysEquals(expected.getCurrentOutgoingKeys(),
|
||||
actual.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandshakeKeysForPendingContact() throws Exception {
|
||||
long timePeriod = 123, timePeriod1 = 234;
|
||||
boolean alice = random.nextBoolean();
|
||||
SecretKey rootKey = getSecretKey();
|
||||
SecretKey rootKey1 = getSecretKey();
|
||||
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
HandshakeKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
|
||||
TransportKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
TransportKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Initially there should be no handshake keys in the database
|
||||
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||
|
||||
// Add the pending contact, the transport and the handshake keys
|
||||
db.addPendingContact(txn, pendingContact);
|
||||
db.addTransport(txn, transportId, 123);
|
||||
assertEquals(handshakeKeySetId, db.addHandshakeKeys(txn,
|
||||
pendingContact.getId(), keys));
|
||||
assertEquals(handshakeKeySetId1, db.addHandshakeKeys(txn,
|
||||
pendingContact.getId(), keys1));
|
||||
assertEquals(keySetId,
|
||||
db.addTransportKeys(txn, pendingContact.getId(), keys));
|
||||
assertEquals(keySetId1,
|
||||
db.addTransportKeys(txn, pendingContact.getId(), keys1));
|
||||
|
||||
// Retrieve the handshake keys
|
||||
Collection<HandshakeKeySet> allKeys =
|
||||
db.getHandshakeKeys(txn, transportId);
|
||||
Collection<TransportKeySet> allKeys =
|
||||
db.getTransportKeys(txn, transportId);
|
||||
assertEquals(2, allKeys.size());
|
||||
for (HandshakeKeySet ks : allKeys) {
|
||||
for (TransportKeySet ks : allKeys) {
|
||||
assertNull(ks.getContactId());
|
||||
assertEquals(pendingContact.getId(), ks.getPendingContactId());
|
||||
if (ks.getKeySetId().equals(handshakeKeySetId)) {
|
||||
if (ks.getKeySetId().equals(keySetId)) {
|
||||
assertKeysEquals(keys, ks.getKeys());
|
||||
} else {
|
||||
assertEquals(handshakeKeySetId1, ks.getKeySetId());
|
||||
assertEquals(keySetId1, ks.getKeySetId());
|
||||
assertKeysEquals(keys1, ks.getKeys());
|
||||
}
|
||||
}
|
||||
|
||||
// Update the handshake keys
|
||||
HandshakeKeys updated =
|
||||
TransportKeys updated =
|
||||
createHandshakeKeys(timePeriod + 1, rootKey, alice);
|
||||
HandshakeKeys updated1 =
|
||||
TransportKeys updated1 =
|
||||
createHandshakeKeys(timePeriod1 + 1, rootKey1, alice);
|
||||
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId,
|
||||
db.updateTransportKeys(txn, new TransportKeySet(keySetId, null,
|
||||
pendingContact.getId(), updated));
|
||||
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId1,
|
||||
db.updateTransportKeys(txn, new TransportKeySet(keySetId1, null,
|
||||
pendingContact.getId(), updated1));
|
||||
|
||||
// Retrieve the handshake keys again
|
||||
allKeys = db.getHandshakeKeys(txn, transportId);
|
||||
allKeys = db.getTransportKeys(txn, transportId);
|
||||
assertEquals(2, allKeys.size());
|
||||
for (HandshakeKeySet ks : allKeys) {
|
||||
for (TransportKeySet ks : allKeys) {
|
||||
assertNull(ks.getContactId());
|
||||
assertEquals(pendingContact.getId(), ks.getPendingContactId());
|
||||
if (ks.getKeySetId().equals(handshakeKeySetId)) {
|
||||
if (ks.getKeySetId().equals(keySetId)) {
|
||||
assertKeysEquals(updated, ks.getKeys());
|
||||
} else {
|
||||
assertEquals(handshakeKeySetId1, ks.getKeySetId());
|
||||
assertEquals(keySetId1, ks.getKeySetId());
|
||||
assertKeysEquals(updated1, ks.getKeys());
|
||||
}
|
||||
}
|
||||
|
||||
// Removing the pending contact should remove the handshake keys
|
||||
db.removePendingContact(txn, pendingContact.getId());
|
||||
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -973,7 +956,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
long timePeriod = 123;
|
||||
SecretKey rootKey = getSecretKey();
|
||||
boolean alice = random.nextBoolean();
|
||||
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
TransportKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
long streamCounter = keys.getCurrentOutgoingKeys().getStreamCounter();
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
@@ -984,20 +967,20 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
assertEquals(handshakeKeySetId,
|
||||
db.addHandshakeKeys(txn, contactId, keys));
|
||||
assertEquals(keySetId, db.addTransportKeys(txn, contactId, keys));
|
||||
|
||||
// Increment the stream counter twice and retrieve the handshake keys
|
||||
db.incrementStreamCounter(txn, transportId, handshakeKeySetId);
|
||||
db.incrementStreamCounter(txn, transportId, handshakeKeySetId);
|
||||
Collection<HandshakeKeySet> newKeys =
|
||||
db.getHandshakeKeys(txn, transportId);
|
||||
db.incrementStreamCounter(txn, transportId, keySetId);
|
||||
db.incrementStreamCounter(txn, transportId, keySetId);
|
||||
Collection<TransportKeySet> newKeys =
|
||||
db.getTransportKeys(txn, transportId);
|
||||
assertEquals(1, newKeys.size());
|
||||
HandshakeKeySet ks = newKeys.iterator().next();
|
||||
assertEquals(handshakeKeySetId, ks.getKeySetId());
|
||||
TransportKeySet ks = newKeys.iterator().next();
|
||||
assertEquals(keySetId, ks.getKeySetId());
|
||||
assertEquals(contactId, ks.getContactId());
|
||||
HandshakeKeys k = ks.getKeys();
|
||||
TransportKeys k = ks.getKeys();
|
||||
assertEquals(transportId, k.getTransportId());
|
||||
assertNotNull(k.getRootKey());
|
||||
assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes());
|
||||
assertEquals(alice, k.isAlice());
|
||||
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
|
||||
@@ -1066,7 +1049,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
long timePeriod = 123;
|
||||
SecretKey rootKey = getSecretKey();
|
||||
boolean alice = random.nextBoolean();
|
||||
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
TransportKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
|
||||
long base = keys.getCurrentIncomingKeys().getWindowBase();
|
||||
byte[] bitmap = keys.getCurrentIncomingKeys().getWindowBitmap();
|
||||
|
||||
@@ -1078,21 +1061,21 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
assertEquals(handshakeKeySetId,
|
||||
db.addHandshakeKeys(txn, contactId, keys));
|
||||
assertEquals(keySetId, db.addTransportKeys(txn, contactId, keys));
|
||||
|
||||
// Update the reordering window and retrieve the handshake keys
|
||||
random.nextBytes(bitmap);
|
||||
db.setReorderingWindow(txn, handshakeKeySetId, transportId, timePeriod,
|
||||
db.setReorderingWindow(txn, keySetId, transportId, timePeriod,
|
||||
base + 1, bitmap);
|
||||
Collection<HandshakeKeySet> newKeys =
|
||||
db.getHandshakeKeys(txn, transportId);
|
||||
Collection<TransportKeySet> newKeys =
|
||||
db.getTransportKeys(txn, transportId);
|
||||
assertEquals(1, newKeys.size());
|
||||
HandshakeKeySet ks = newKeys.iterator().next();
|
||||
assertEquals(handshakeKeySetId, ks.getKeySetId());
|
||||
TransportKeySet ks = newKeys.iterator().next();
|
||||
assertEquals(keySetId, ks.getKeySetId());
|
||||
assertEquals(contactId, ks.getContactId());
|
||||
HandshakeKeys k = ks.getKeys();
|
||||
TransportKeys k = ks.getKeys();
|
||||
assertEquals(transportId, k.getTransportId());
|
||||
assertNotNull(k.getRootKey());
|
||||
assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes());
|
||||
assertEquals(alice, k.isAlice());
|
||||
IncomingKeys inCurr = k.getCurrentIncomingKeys();
|
||||
@@ -2308,7 +2291,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);
|
||||
}
|
||||
|
||||
private HandshakeKeys createHandshakeKeys(long timePeriod,
|
||||
private TransportKeys createHandshakeKeys(long timePeriod,
|
||||
SecretKey rootKey, boolean alice) {
|
||||
SecretKey inPrevTagKey = getSecretKey();
|
||||
SecretKey inPrevHeaderKey = getSecretKey();
|
||||
@@ -2326,7 +2309,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
SecretKey outCurrHeaderKey = getSecretKey();
|
||||
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
|
||||
timePeriod, 456, true);
|
||||
return new HandshakeKeys(transportId, inPrev, inCurr, inNext, outCurr,
|
||||
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr,
|
||||
rootKey, alice);
|
||||
}
|
||||
|
||||
|
||||
@@ -101,8 +101,8 @@ public class SyncIntegrationTest extends BrambleTestCase {
|
||||
|
||||
private byte[] write() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamContext ctx = new StreamContext(contactId, transportId, tagKey,
|
||||
headerKey, streamNumber);
|
||||
StreamContext ctx = new StreamContext(contactId, null, transportId,
|
||||
tagKey, headerKey, streamNumber, false);
|
||||
StreamWriter streamWriter = streamWriterFactory.createStreamWriter(out,
|
||||
ctx);
|
||||
SyncRecordWriter recordWriter = recordWriterFactory.createRecordWriter(
|
||||
@@ -131,8 +131,8 @@ public class SyncIntegrationTest extends BrambleTestCase {
|
||||
assertArrayEquals(expectedTag, tag);
|
||||
|
||||
// Create the readers
|
||||
StreamContext ctx = new StreamContext(contactId, transportId, tagKey,
|
||||
headerKey, streamNumber);
|
||||
StreamContext ctx = new StreamContext(contactId, null, transportId,
|
||||
tagKey, headerKey, streamNumber, false);
|
||||
InputStream streamReader = streamReaderFactory.createStreamReader(in,
|
||||
ctx);
|
||||
SyncRecordReader recordReader = recordReaderFactory.createRecordReader(
|
||||
|
||||
@@ -8,8 +8,8 @@ import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.plugin.PluginConfig;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
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.TransportKeySetId;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.DbExpectations;
|
||||
import org.jmock.Expectations;
|
||||
@@ -43,11 +43,11 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
||||
private final DeterministicExecutor executor = new DeterministicExecutor();
|
||||
private final Transaction txn = new Transaction(null, false);
|
||||
private final ContactId contactId = getContactId();
|
||||
private final TransportKeySetId keySetId = new TransportKeySetId(345);
|
||||
private final KeySetId keySetId = new KeySetId(345);
|
||||
private final TransportId transportId = getTransportId();
|
||||
private final TransportId unknownTransportId = getTransportId();
|
||||
private final StreamContext streamContext = new StreamContext(contactId,
|
||||
transportId, getSecretKey(), getSecretKey(), 1);
|
||||
null, transportId, getSecretKey(), getSecretKey(), 1, false);
|
||||
private final byte[] tag = getRandomBytes(TAG_LENGTH);
|
||||
private final Random random = new Random();
|
||||
|
||||
@@ -95,8 +95,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(keySetId));
|
||||
}});
|
||||
|
||||
Map<TransportId, TransportKeySetId> ids = keyManager.addContact(txn,
|
||||
contactId, secretKey, timestamp, alice, active);
|
||||
Map<TransportId, KeySetId> ids = keyManager.addContact(txn, contactId,
|
||||
secretKey, timestamp, alice, active);
|
||||
assertEquals(singletonMap(transportId, keySetId), ids);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
import org.briarproject.bramble.api.transport.StreamContext;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySetId;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.DbExpectations;
|
||||
@@ -61,22 +61,23 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
private final long timePeriodLength = maxLatency + MAX_CLOCK_DIFFERENCE;
|
||||
private final ContactId contactId = getContactId();
|
||||
private final ContactId contactId1 = getContactId();
|
||||
private final TransportKeySetId keySetId = new TransportKeySetId(345);
|
||||
private final TransportKeySetId keySetId1 = new TransportKeySetId(456);
|
||||
private final KeySetId keySetId = new KeySetId(345);
|
||||
private final KeySetId keySetId1 = new KeySetId(456);
|
||||
private final SecretKey tagKey = getSecretKey();
|
||||
private final SecretKey headerKey = getSecretKey();
|
||||
private final SecretKey rootKey = getSecretKey();
|
||||
private final Random random = new Random();
|
||||
|
||||
@Test
|
||||
public void testKeysAreRotatedAtStartup() throws Exception {
|
||||
TransportKeys shouldRotate = createTransportKeys(900, 0, true);
|
||||
TransportKeys shouldNotRotate = createTransportKeys(1000, 0, true);
|
||||
public void testKeysAreUpdatedAtStartup() throws Exception {
|
||||
TransportKeys shouldUpdate = createTransportKeys(900, 0, true);
|
||||
TransportKeys shouldNotUpdate = createTransportKeys(1000, 0, true);
|
||||
Collection<TransportKeySet> loaded = asList(
|
||||
new TransportKeySet(keySetId, contactId, shouldRotate),
|
||||
new TransportKeySet(keySetId1, contactId1, shouldNotRotate)
|
||||
new TransportKeySet(keySetId, contactId, null, shouldUpdate),
|
||||
new TransportKeySet(keySetId1, contactId1, null,
|
||||
shouldNotUpdate)
|
||||
);
|
||||
TransportKeys rotated = createTransportKeys(1000, 0, true);
|
||||
TransportKeys updated = createTransportKeys(1000, 0, true);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -86,11 +87,11 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
// Load the transport keys
|
||||
oneOf(db).getTransportKeys(txn, transportId);
|
||||
will(returnValue(loaded));
|
||||
// Rotate the transport keys
|
||||
oneOf(transportCrypto).rotateTransportKeys(shouldRotate, 1000);
|
||||
will(returnValue(rotated));
|
||||
oneOf(transportCrypto).rotateTransportKeys(shouldNotRotate, 1000);
|
||||
will(returnValue(shouldNotRotate));
|
||||
// Update the transport keys
|
||||
oneOf(transportCrypto).updateTransportKeys(shouldUpdate, 1000);
|
||||
will(returnValue(updated));
|
||||
oneOf(transportCrypto).updateTransportKeys(shouldNotUpdate, 1000);
|
||||
will(returnValue(shouldNotUpdate));
|
||||
// Encode the tags (3 sets per contact)
|
||||
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
|
||||
exactly(6).of(transportCrypto).encodeTag(
|
||||
@@ -98,10 +99,10 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
with(PROTOCOL_VERSION), with(i));
|
||||
will(new EncodeTagAction());
|
||||
}
|
||||
// Save the keys that were rotated
|
||||
// Save the keys that were updated
|
||||
oneOf(db).updateTransportKeys(txn, singletonList(
|
||||
new TransportKeySet(keySetId, contactId, rotated)));
|
||||
// Schedule key rotation at the start of the next time period
|
||||
new TransportKeySet(keySetId, contactId, null, updated)));
|
||||
// Schedule a key update at the start of the next time period
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with(timePeriodLength - 1), with(MILLISECONDS));
|
||||
}});
|
||||
@@ -114,22 +115,22 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreRotatedWhenAddingContact() throws Exception {
|
||||
public void testKeysAreUpdatedWhenAddingContact() throws Exception {
|
||||
boolean alice = random.nextBoolean();
|
||||
TransportKeys transportKeys = createTransportKeys(999, 0, true);
|
||||
TransportKeys rotated = createTransportKeys(1000, 0, true);
|
||||
TransportKeys updated = createTransportKeys(1000, 0, true);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(transportCrypto).deriveTransportKeys(transportId, rootKey,
|
||||
oneOf(transportCrypto).deriveRotationKeys(transportId, rootKey,
|
||||
999, alice, true);
|
||||
will(returnValue(transportKeys));
|
||||
// Get the current time (1 ms after start of time period 1000)
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(timePeriodLength * 1000 + 1));
|
||||
// Rotate the transport keys
|
||||
oneOf(transportCrypto).rotateTransportKeys(transportKeys, 1000);
|
||||
will(returnValue(rotated));
|
||||
// Update the transport keys
|
||||
oneOf(transportCrypto).updateTransportKeys(transportKeys, 1000);
|
||||
will(returnValue(updated));
|
||||
// Encode the tags (3 sets)
|
||||
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
|
||||
exactly(3).of(transportCrypto).encodeTag(
|
||||
@@ -138,7 +139,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
will(new EncodeTagAction());
|
||||
}
|
||||
// Save the keys
|
||||
oneOf(db).addTransportKeys(txn, contactId, rotated);
|
||||
oneOf(db).addTransportKeys(txn, contactId, updated);
|
||||
will(returnValue(keySetId));
|
||||
}});
|
||||
|
||||
@@ -173,7 +174,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
MAX_32_BIT_UNSIGNED + 1, true);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
expectAddContactNoRotation(alice, true, transportKeys, txn);
|
||||
expectAddContactKeysNotUpdated(alice, true, transportKeys, txn);
|
||||
|
||||
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
|
||||
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
|
||||
@@ -194,7 +195,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
MAX_32_BIT_UNSIGNED, true);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
expectAddContactNoRotation(alice, true, transportKeys, txn);
|
||||
expectAddContactKeysNotUpdated(alice, true, transportKeys, txn);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// Increment the stream counter
|
||||
@@ -231,7 +232,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
TransportKeys transportKeys = createTransportKeys(1000, 0, active);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
expectAddContactNoRotation(alice, active, transportKeys, txn);
|
||||
expectAddContactKeysNotUpdated(alice, active, transportKeys, txn);
|
||||
|
||||
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
|
||||
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
|
||||
@@ -257,7 +258,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
List<byte[]> tags = new ArrayList<>();
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(transportCrypto).deriveTransportKeys(transportId, rootKey,
|
||||
oneOf(transportCrypto).deriveRotationKeys(transportId, rootKey,
|
||||
1000, alice, true);
|
||||
will(returnValue(transportKeys));
|
||||
// Get the current time (the start of time period 1000)
|
||||
@@ -270,8 +271,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
with(PROTOCOL_VERSION), with(i));
|
||||
will(new EncodeTagAction(tags));
|
||||
}
|
||||
// Rotate the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).rotateTransportKeys(transportKeys, 1000);
|
||||
// Updated the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).updateTransportKeys(transportKeys, 1000);
|
||||
will(returnValue(transportKeys));
|
||||
// Save the keys
|
||||
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
|
||||
@@ -312,11 +313,11 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeysAreRotatedToCurrentPeriod() throws Exception {
|
||||
public void testKeysAreUpdatedToCurrentPeriod() throws Exception {
|
||||
TransportKeys transportKeys = createTransportKeys(1000, 0, true);
|
||||
Collection<TransportKeySet> loaded = singletonList(
|
||||
new TransportKeySet(keySetId, contactId, transportKeys));
|
||||
TransportKeys rotated = createTransportKeys(1001, 0, true);
|
||||
new TransportKeySet(keySetId, contactId, null, transportKeys));
|
||||
TransportKeys updated = createTransportKeys(1001, 0, true);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Transaction txn1 = new Transaction(null, false);
|
||||
|
||||
@@ -327,8 +328,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
// Load the transport keys
|
||||
oneOf(db).getTransportKeys(txn, transportId);
|
||||
will(returnValue(loaded));
|
||||
// Rotate the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).rotateTransportKeys(transportKeys, 1000);
|
||||
// Update the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).updateTransportKeys(transportKeys, 1000);
|
||||
will(returnValue(transportKeys));
|
||||
// Encode the tags (3 sets)
|
||||
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
|
||||
@@ -337,21 +338,21 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
with(PROTOCOL_VERSION), with(i));
|
||||
will(new EncodeTagAction());
|
||||
}
|
||||
// Schedule key rotation at the start of the next time period
|
||||
// Schedule a key update at the start of the next time period
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with(timePeriodLength), with(MILLISECONDS));
|
||||
will(new RunAction());
|
||||
oneOf(dbExecutor).execute(with(any(Runnable.class)));
|
||||
will(new RunAction());
|
||||
// Start a transaction for key rotation
|
||||
// Start a transaction for updating keys
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn1));
|
||||
// Get the current time (the start of time period 1001)
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(timePeriodLength * 1001));
|
||||
// Rotate the transport keys
|
||||
oneOf(transportCrypto).rotateTransportKeys(
|
||||
// Update the transport keys
|
||||
oneOf(transportCrypto).updateTransportKeys(
|
||||
with(any(TransportKeys.class)), with(1001L));
|
||||
will(returnValue(rotated));
|
||||
will(returnValue(updated));
|
||||
// Encode the tags (3 sets)
|
||||
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
|
||||
exactly(3).of(transportCrypto).encodeTag(
|
||||
@@ -359,10 +360,10 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
with(PROTOCOL_VERSION), with(i));
|
||||
will(new EncodeTagAction());
|
||||
}
|
||||
// Save the keys that were rotated
|
||||
// Save the keys that were updated
|
||||
oneOf(db).updateTransportKeys(txn1, singletonList(
|
||||
new TransportKeySet(keySetId, contactId, rotated)));
|
||||
// Schedule key rotation at the start of the next time period
|
||||
new TransportKeySet(keySetId, contactId, null, updated)));
|
||||
// Schedule a key update at the start of the next time period
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with(timePeriodLength), with(MILLISECONDS));
|
||||
}});
|
||||
@@ -380,7 +381,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
TransportKeys transportKeys = createTransportKeys(1000, 0, false);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
expectAddContactNoRotation(alice, false, transportKeys, txn);
|
||||
expectAddContactKeysNotUpdated(alice, false, transportKeys, txn);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// Activate the keys
|
||||
@@ -422,7 +423,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
List<byte[]> tags = new ArrayList<>();
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(transportCrypto).deriveTransportKeys(transportId, rootKey,
|
||||
oneOf(transportCrypto).deriveRotationKeys(transportId, rootKey,
|
||||
1000, alice, false);
|
||||
will(returnValue(transportKeys));
|
||||
// Get the current time (the start of time period 1000)
|
||||
@@ -435,8 +436,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
with(PROTOCOL_VERSION), with(i));
|
||||
will(new EncodeTagAction(tags));
|
||||
}
|
||||
// Rotate the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).rotateTransportKeys(transportKeys, 1000);
|
||||
// Update the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).updateTransportKeys(transportKeys, 1000);
|
||||
will(returnValue(transportKeys));
|
||||
// Save the keys
|
||||
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
|
||||
@@ -486,10 +487,10 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(0L, ctx.getStreamNumber());
|
||||
}
|
||||
|
||||
private void expectAddContactNoRotation(boolean alice, boolean active,
|
||||
private void expectAddContactKeysNotUpdated(boolean alice, boolean active,
|
||||
TransportKeys transportKeys, Transaction txn) throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(transportCrypto).deriveTransportKeys(transportId, rootKey,
|
||||
oneOf(transportCrypto).deriveRotationKeys(transportId, rootKey,
|
||||
1000, alice, active);
|
||||
will(returnValue(transportKeys));
|
||||
// Get the current time (the start of time period 1000)
|
||||
@@ -502,8 +503,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
with(PROTOCOL_VERSION), with(i));
|
||||
will(new EncodeTagAction());
|
||||
}
|
||||
// Rotate the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).rotateTransportKeys(transportKeys, 1000);
|
||||
// Upate the transport keys (the keys are unaffected)
|
||||
oneOf(transportCrypto).updateTransportKeys(transportKeys, 1000);
|
||||
will(returnValue(transportKeys));
|
||||
// Save the keys
|
||||
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
|
||||
|
||||
Reference in New Issue
Block a user