Rename static transport keys to handshake keys.

This commit is contained in:
akwizgran
2019-04-17 14:52:52 +01:00
parent e91a7c64d8
commit fafd0c7ff9
16 changed files with 585 additions and 603 deletions

View File

@@ -1,7 +1,7 @@
package org.briarproject.bramble.api.crypto; package org.briarproject.bramble.api.crypto;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.transport.StaticTransportKeys; import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
/** /**
@@ -27,21 +27,19 @@ public interface TransportCrypto {
TransportKeys rotateTransportKeys(TransportKeys k, long timePeriod); TransportKeys rotateTransportKeys(TransportKeys k, long timePeriod);
/** /**
* Derives static transport keys for the given transport in the given time * Derives handshake keys for the given transport in the given time period
* period from the given root key. * from the given root key.
* *
* @param alice whether the keys are for use by Alice or Bob. * @param alice whether the keys are for use by Alice or Bob.
*/ */
StaticTransportKeys deriveStaticTransportKeys(TransportId t, HandshakeKeys deriveHandshakeKeys(TransportId t, SecretKey rootKey,
SecretKey rootKey, long timePeriod, boolean alice); long timePeriod, boolean alice);
/** /**
* Updates the given static transport keys to the given time period. If * Updates the given handshake keys to the given time period. If the keys
* the keys are for the given period or any later period they are not * are for the given period or any later period they are not updated.
* updated.
*/ */
StaticTransportKeys updateStaticTransportKeys(StaticTransportKeys k, HandshakeKeys updateHandshakeKeys(HandshakeKeys k, long timePeriod);
long timePeriod);
/** /**
* Encodes the pseudo-random tag that is used to recognise a stream. * Encodes the pseudo-random tag that is used to recognise a stream.

View File

@@ -21,9 +21,9 @@ import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.Offer; import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.Request; import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.validation.MessageState; import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.transport.StaticTransportKeySet; import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId; import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys; import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet; import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId; import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
@@ -112,6 +112,20 @@ public interface DatabaseComponent {
*/ */
void addGroup(Transaction txn, Group g) throws DbException; void addGroup(Transaction txn, Group g) throws DbException;
/**
* Stores the given handshake keys for the given contact and returns a
* key set ID.
*/
HandshakeKeySetId addHandshakeKeys(Transaction txn, ContactId c,
HandshakeKeys k) throws DbException;
/**
* Stores the given handshake keys for the given pending contact and
* returns a key set ID.
*/
HandshakeKeySetId addHandshakeKeys(Transaction txn, PendingContactId p,
HandshakeKeys k) throws DbException;
/** /**
* Stores a local pseudonym. * Stores a local pseudonym.
*/ */
@@ -123,20 +137,6 @@ public interface DatabaseComponent {
void addLocalMessage(Transaction txn, Message m, Metadata meta, void addLocalMessage(Transaction txn, Message m, Metadata meta,
boolean shared) throws DbException; boolean shared) throws DbException;
/**
* Stores the given static transport keys for the given contact and returns
* a key set ID.
*/
StaticTransportKeySetId addStaticTransportKeys(Transaction txn, ContactId c,
StaticTransportKeys k) throws DbException;
/**
* Stores the given static transport keys for the given pending contact and
* returns a key set ID.
*/
StaticTransportKeySetId addStaticTransportKeys(Transaction txn,
PendingContactId p, StaticTransportKeys k) throws DbException;
/** /**
* Stores a transport. * Stores a transport.
*/ */
@@ -287,6 +287,14 @@ public interface DatabaseComponent {
Visibility getGroupVisibility(Transaction txn, ContactId c, GroupId g) Visibility getGroupVisibility(Transaction txn, ContactId c, GroupId g)
throws DbException; throws DbException;
/**
* Returns all handshake keys for the given transport.
* <p/>
* Read-only.
*/
Collection<HandshakeKeySet> getHandshakeKeys(Transaction txn,
TransportId t) throws DbException;
/** /**
* Returns the local pseudonym with the given ID. * Returns the local pseudonym with the given ID.
* <p/> * <p/>
@@ -442,14 +450,6 @@ public interface DatabaseComponent {
*/ */
Settings getSettings(Transaction txn, String namespace) throws DbException; Settings getSettings(Transaction txn, String namespace) throws DbException;
/**
* Returns all static transport keys for the given transport.
* <p/>
* Read-only.
*/
Collection<StaticTransportKeySet> getStaticTransportKeys(Transaction txn,
TransportId t) throws DbException;
/** /**
* Returns all transport keys for the given transport. * Returns all transport keys for the given transport.
* <p/> * <p/>
@@ -459,11 +459,10 @@ public interface DatabaseComponent {
throws DbException; throws DbException;
/** /**
* Increments the outgoing stream counter for the given static transport * Increments the outgoing stream counter for the given handshake keys.
* keys.
*/ */
void incrementStreamCounter(Transaction txn, TransportId t, void incrementStreamCounter(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException; HandshakeKeySetId k) throws DbException;
/** /**
* Increments the outgoing stream counter for the given transport keys. * Increments the outgoing stream counter for the given transport keys.
@@ -524,6 +523,12 @@ public interface DatabaseComponent {
*/ */
void removeGroup(Transaction txn, Group g) throws DbException; void removeGroup(Transaction txn, Group g) throws DbException;
/**
* Removes the given handshake keys from the database.
*/
void removeHandshakeKeys(Transaction txn, TransportId t,
HandshakeKeySetId k) throws DbException;
/** /**
* Removes a local pseudonym (and all associated state) from the database. * Removes a local pseudonym (and all associated state) from the database.
*/ */
@@ -534,12 +539,6 @@ public interface DatabaseComponent {
*/ */
void removeMessage(Transaction txn, MessageId m) throws DbException; void removeMessage(Transaction txn, MessageId m) throws DbException;
/**
* Removes the given static transport keys from the database.
*/
void removeStaticTransportKeys(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException;
/** /**
* Removes a transport (and all associated state) from the database. * Removes a transport (and all associated state) from the database.
*/ */
@@ -606,11 +605,10 @@ public interface DatabaseComponent {
TransportKeySetId k) throws DbException; TransportKeySetId k) throws DbException;
/** /**
* Stores the given static transport keys, deleting any keys they have * Stores the given handshake keys, deleting any keys they have replaced.
* replaced.
*/ */
void updateStaticTransportKeys(Transaction txn, void updateHandshakeKeys(Transaction txn, Collection<HandshakeKeySet> keys)
Collection<StaticTransportKeySet> keys) throws DbException; throws DbException;
/** /**
* Stores the given transport keys, deleting any keys they have replaced. * Stores the given transport keys, deleting any keys they have replaced.

View File

@@ -6,8 +6,7 @@ import org.briarproject.bramble.api.plugin.TransportId;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
/** /**
* Abstract superclass for {@link TransportKeys} and * Abstract superclass for {@link TransportKeys} and {@link HandshakeKeys}.
* {@link StaticTransportKeys}.
*/ */
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault

View File

@@ -8,37 +8,38 @@ import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
/** /**
* A set of transport keys for communicating with a contact or pending contact. * A set of keys for handshaking with a given contact or pending contact over a
* Unlike a {@link TransportKeySet} these keys do not provide forward secrecy. * given transport. Unlike a {@link TransportKeySet} these keys do not provide
* forward secrecy.
*/ */
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
public class StaticTransportKeySet { public class HandshakeKeySet {
private final StaticTransportKeySetId keySetId; private final HandshakeKeySetId keySetId;
@Nullable @Nullable
private final ContactId contactId; private final ContactId contactId;
@Nullable @Nullable
private final PendingContactId pendingContactId; private final PendingContactId pendingContactId;
private final StaticTransportKeys keys; private final HandshakeKeys keys;
public StaticTransportKeySet(StaticTransportKeySetId keySetId, public HandshakeKeySet(HandshakeKeySetId keySetId, ContactId contactId,
ContactId contactId, StaticTransportKeys keys) { HandshakeKeys keys) {
this.keySetId = keySetId; this.keySetId = keySetId;
this.contactId = contactId; this.contactId = contactId;
this.keys = keys; this.keys = keys;
pendingContactId = null; pendingContactId = null;
} }
public StaticTransportKeySet(StaticTransportKeySetId keySetId, public HandshakeKeySet(HandshakeKeySetId keySetId,
PendingContactId pendingContactId, StaticTransportKeys keys) { PendingContactId pendingContactId, HandshakeKeys keys) {
this.keySetId = keySetId; this.keySetId = keySetId;
this.pendingContactId = pendingContactId; this.pendingContactId = pendingContactId;
this.keys = keys; this.keys = keys;
contactId = null; contactId = null;
} }
public StaticTransportKeySetId getKeySetId() { public HandshakeKeySetId getKeySetId() {
return keySetId; return keySetId;
} }
@@ -52,7 +53,7 @@ public class StaticTransportKeySet {
return pendingContactId; return pendingContactId;
} }
public StaticTransportKeys getKeys() { public HandshakeKeys getKeys() {
return keys; return keys;
} }
@@ -63,7 +64,7 @@ public class StaticTransportKeySet {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return o instanceof StaticTransportKeySet && return o instanceof HandshakeKeySet &&
keySetId.equals(((StaticTransportKeySet) o).keySetId); keySetId.equals(((HandshakeKeySet) o).keySetId);
} }
} }

View File

@@ -6,16 +6,16 @@ import javax.annotation.concurrent.Immutable;
/** /**
* Type-safe wrapper for an integer that uniquely identifies a * Type-safe wrapper for an integer that uniquely identifies a
* {@link StaticTransportKeySet set of static transport keys} within the scope * {@link HandshakeKeySet set of handshake keys} within the scope of the local
* of the local device. * device.
*/ */
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
public class StaticTransportKeySetId { public class HandshakeKeySetId {
private final int id; private final int id;
public StaticTransportKeySetId(int id) { public HandshakeKeySetId(int id) {
this.id = id; this.id = id;
} }
@@ -30,7 +30,7 @@ public class StaticTransportKeySetId {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return o instanceof StaticTransportKeySetId && return o instanceof HandshakeKeySetId &&
id == ((StaticTransportKeySetId) o).id; id == ((HandshakeKeySetId) o).id;
} }
} }

View File

@@ -7,17 +7,18 @@ import org.briarproject.bramble.api.plugin.TransportId;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
/** /**
* Keys for communicating with a given contact or pending contact over a given * Keys for handshaking with a given contact or pending contact over a given
* transport. Unlike {@link TransportKeys} these do not provide forward secrecy. * transport. Unlike {@link TransportKeys} these keys do not provide forward
* secrecy.
*/ */
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
public class StaticTransportKeys extends AbstractTransportKeys { public class HandshakeKeys extends AbstractTransportKeys {
private final SecretKey rootKey; private final SecretKey rootKey;
private final boolean alice; private final boolean alice;
public StaticTransportKeys(TransportId transportId, IncomingKeys inPrev, public HandshakeKeys(TransportId transportId, IncomingKeys inPrev,
IncomingKeys inCurr, IncomingKeys inNext, OutgoingKeys outCurr, IncomingKeys inCurr, IncomingKeys inNext, OutgoingKeys outCurr,
SecretKey rootKey, boolean alice) { SecretKey rootKey, boolean alice) {
super(transportId, inPrev, inCurr, inNext, outCurr); super(transportId, inPrev, inCurr, inNext, outCurr);

View File

@@ -109,26 +109,26 @@ public interface TransportConstants {
String ROTATE_LABEL = "org.briarproject.bramble.transport/ROTATE"; String ROTATE_LABEL = "org.briarproject.bramble.transport/ROTATE";
/** /**
* Label for deriving Alice's static tag key from the root key. * Label for deriving Alice's handshake tag key from the root key.
*/ */
String ALICE_STATIC_TAG_LABEL = String ALICE_HANDSHAKE_TAG_LABEL =
"org.briarproject.bramble.transport/ALICE_STATIC_TAG_KEY"; "org.briarproject.bramble.transport/ALICE_STATIC_TAG_KEY";
/** /**
* Label for deriving Bob's static tag key from the root key. * Label for deriving Bob's handshake tag key from the root key.
*/ */
String BOB_STATIC_TAG_LABEL = String BOB_HANDSHAKE_TAG_LABEL =
"org.briarproject.bramble.transport/BOB_STATIC_TAG_KEY"; "org.briarproject.bramble.transport/BOB_STATIC_TAG_KEY";
/** /**
* Label for deriving Alice's static header key from the root key. * Label for deriving Alice's handshake header key from the root key.
*/ */
String ALICE_STATIC_HEADER_LABEL = String ALICE_HANDSHAKE_HEADER_LABEL =
"org.briarproject.bramble.transport/ALICE_STATIC_HEADER_KEY"; "org.briarproject.bramble.transport/ALICE_STATIC_HEADER_KEY";
/** /**
* Label for deriving Bob's static header key from the root key. * Label for deriving Bob's handshake header key from the root key.
*/ */
String BOB_STATIC_HEADER_LABEL = String BOB_HANDSHAKE_HEADER_LABEL =
"org.briarproject.bramble.transport/BOB_STATIC_HEADER_KEY"; "org.briarproject.bramble.transport/BOB_STATIC_HEADER_KEY";
} }

View File

@@ -6,7 +6,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
/** /**
* A set of transport keys for communicating with a contact. * A set of keys for communicating with a given contact over a given transport.
* Unlike a {@link HandshakeKeySet} these keys provide forward secrecy.
*/ */
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault

View File

@@ -7,7 +7,7 @@ import javax.annotation.concurrent.Immutable;
/** /**
* Keys for communicating with a given contact over a given transport. Unlike * Keys for communicating with a given contact over a given transport. Unlike
* {@link StaticTransportKeys}, these keys provide forward secrecy. * {@link HandshakeKeys} these keys provide forward secrecy.
*/ */
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault

View File

@@ -4,9 +4,9 @@ import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.crypto.TransportCrypto; import org.briarproject.bramble.api.crypto.TransportCrypto;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.IncomingKeys; import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.OutgoingKeys; import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
import org.spongycastle.crypto.Digest; import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.Blake2bDigest; import org.spongycastle.crypto.digests.Blake2bDigest;
@@ -14,13 +14,13 @@ import org.spongycastle.crypto.digests.Blake2bDigest;
import javax.inject.Inject; import javax.inject.Inject;
import static java.lang.System.arraycopy; import static java.lang.System.arraycopy;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HANDSHAKE_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HANDSHAKE_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HEADER_LABEL; import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_STATIC_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_STATIC_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_TAG_LABEL; import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HANDSHAKE_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HANDSHAKE_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HEADER_LABEL; import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_STATIC_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_STATIC_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_TAG_LABEL; import static org.briarproject.bramble.api.transport.TransportConstants.BOB_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ROTATE_LABEL; import static org.briarproject.bramble.api.transport.TransportConstants.ROTATE_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
@@ -115,49 +115,51 @@ class TransportCryptoImpl implements TransportCrypto {
} }
@Override @Override
public StaticTransportKeys deriveStaticTransportKeys(TransportId t, public HandshakeKeys deriveHandshakeKeys(TransportId t, SecretKey rootKey,
SecretKey rootKey, long timePeriod, boolean alice) { long timePeriod, boolean alice) {
if (timePeriod < 1) throw new IllegalArgumentException(); if (timePeriod < 1) throw new IllegalArgumentException();
IncomingKeys inPrev = deriveStaticIncomingKeys(t, rootKey, alice, IncomingKeys inPrev = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod - 1); timePeriod - 1);
IncomingKeys inCurr = deriveStaticIncomingKeys(t, rootKey, alice, IncomingKeys inCurr = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod); timePeriod);
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice, IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod + 1); timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice, OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey, alice,
timePeriod); timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr, return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr, rootKey,
rootKey, alice); alice);
} }
private IncomingKeys deriveStaticIncomingKeys(TransportId t, private IncomingKeys deriveIncomingHandshakeKeys(TransportId t,
SecretKey rootKey, boolean alice, long timePeriod) { SecretKey rootKey, boolean alice, long timePeriod) {
SecretKey tag = deriveStaticTagKey(t, rootKey, !alice, timePeriod); SecretKey tag = deriveHandshakeTagKey(t, rootKey, !alice, timePeriod);
SecretKey header = deriveStaticHeaderKey(t, rootKey, !alice, SecretKey header = deriveHandshakeHeaderKey(t, rootKey, !alice,
timePeriod); timePeriod);
return new IncomingKeys(tag, header, timePeriod); return new IncomingKeys(tag, header, timePeriod);
} }
private OutgoingKeys deriveStaticOutgoingKeys(TransportId t, private OutgoingKeys deriveOutgoingHandshakeKeys(TransportId t,
SecretKey rootKey, boolean alice, long timePeriod) { SecretKey rootKey, boolean alice, long timePeriod) {
SecretKey tag = deriveStaticTagKey(t, rootKey, alice, timePeriod); SecretKey tag = deriveHandshakeTagKey(t, rootKey, alice, timePeriod);
SecretKey header = deriveStaticHeaderKey(t, rootKey, alice, timePeriod); SecretKey header = deriveHandshakeHeaderKey(t, rootKey, alice,
timePeriod);
return new OutgoingKeys(tag, header, timePeriod, true); return new OutgoingKeys(tag, header, timePeriod, true);
} }
private SecretKey deriveStaticTagKey(TransportId t, SecretKey rootKey, private SecretKey deriveHandshakeTagKey(TransportId t, SecretKey rootKey,
boolean alice, long timePeriod) { boolean alice, long timePeriod) {
String label = alice ? ALICE_STATIC_TAG_LABEL : BOB_STATIC_TAG_LABEL; String label = alice ? ALICE_HANDSHAKE_TAG_LABEL :
BOB_HANDSHAKE_TAG_LABEL;
byte[] id = toUtf8(t.getString()); byte[] id = toUtf8(t.getString());
byte[] period = new byte[INT_64_BYTES]; byte[] period = new byte[INT_64_BYTES];
writeUint64(timePeriod, period, 0); writeUint64(timePeriod, period, 0);
return crypto.deriveKey(label, rootKey, id, period); return crypto.deriveKey(label, rootKey, id, period);
} }
private SecretKey deriveStaticHeaderKey(TransportId t, SecretKey rootKey, private SecretKey deriveHandshakeHeaderKey(TransportId t, SecretKey rootKey,
boolean alice, long timePeriod) { boolean alice, long timePeriod) {
String label = String label = alice ? ALICE_HANDSHAKE_HEADER_LABEL :
alice ? ALICE_STATIC_HEADER_LABEL : BOB_STATIC_HEADER_LABEL; BOB_HANDSHAKE_HEADER_LABEL;
byte[] id = toUtf8(t.getString()); byte[] id = toUtf8(t.getString());
byte[] period = new byte[INT_64_BYTES]; byte[] period = new byte[INT_64_BYTES];
writeUint64(timePeriod, period, 0); writeUint64(timePeriod, period, 0);
@@ -165,8 +167,7 @@ class TransportCryptoImpl implements TransportCrypto {
} }
@Override @Override
public StaticTransportKeys updateStaticTransportKeys(StaticTransportKeys k, public HandshakeKeys updateHandshakeKeys(HandshakeKeys k, long timePeriod) {
long timePeriod) {
long elapsed = timePeriod - k.getTimePeriod(); long elapsed = timePeriod - k.getTimePeriod();
TransportId t = k.getTransportId(); TransportId t = k.getTransportId();
SecretKey rootKey = k.getRootKey(); SecretKey rootKey = k.getRootKey();
@@ -178,26 +179,26 @@ class TransportCryptoImpl implements TransportCrypto {
// The keys are one period old - shift by one period // The keys are one period old - shift by one period
IncomingKeys inPrev = k.getCurrentIncomingKeys(); IncomingKeys inPrev = k.getCurrentIncomingKeys();
IncomingKeys inCurr = k.getNextIncomingKeys(); IncomingKeys inCurr = k.getNextIncomingKeys();
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice, IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey,
timePeriod + 1); alice, timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice, OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey,
timePeriod); alice, timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr, return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice); rootKey, alice);
} else if (elapsed == 2) { } else if (elapsed == 2) {
// The keys are two periods old - shift by two periods // The keys are two periods old - shift by two periods
IncomingKeys inPrev = k.getNextIncomingKeys(); IncomingKeys inPrev = k.getNextIncomingKeys();
IncomingKeys inCurr = deriveStaticIncomingKeys(t, rootKey, alice, IncomingKeys inCurr = deriveIncomingHandshakeKeys(t, rootKey,
timePeriod); alice, timePeriod);
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice, IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey,
timePeriod + 1); alice, timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice, OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey,
timePeriod); alice, timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr, return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice); rootKey, alice);
} else { } else {
// The keys are more than two periods old - derive fresh keys // The keys are more than two periods old - derive fresh keys
return deriveStaticTransportKeys(t, rootKey, timePeriod, alice); return deriveHandshakeKeys(t, rootKey, timePeriod, alice);
} }
} }

View File

@@ -27,9 +27,9 @@ import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.MessageStatus; import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.validation.MessageState; import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.transport.StaticTransportKeySet; import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId; import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys; import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet; import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId; import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
@@ -105,6 +105,20 @@ interface Database<T> {
void addGroupVisibility(T txn, ContactId c, GroupId g, boolean shared) void addGroupVisibility(T txn, ContactId c, GroupId g, boolean shared)
throws DbException; throws DbException;
/**
* Stores the given handshake keys for the given contact and returns a
* key set ID.
*/
HandshakeKeySetId addHandshakeKeys(T txn, ContactId c, HandshakeKeys k)
throws DbException;
/**
* Stores the given handshake keys for the given pending contact and
* returns a key set ID.
*/
HandshakeKeySetId addHandshakeKeys(T txn, PendingContactId p,
HandshakeKeys k) throws DbException;
/** /**
* Stores a local pseudonym. * Stores a local pseudonym.
*/ */
@@ -136,20 +150,6 @@ interface Database<T> {
*/ */
void addPendingContact(T txn, PendingContact p) throws DbException; void addPendingContact(T txn, PendingContact p) throws DbException;
/**
* Stores the given static transport keys for the given contact and returns
* a key set ID.
*/
StaticTransportKeySetId addStaticTransportKeys(T txn, ContactId c,
StaticTransportKeys k) throws DbException;
/**
* Stores the given static transport keys for the given pending contact and
* returns a key set ID.
*/
StaticTransportKeySetId addStaticTransportKeys(T txn, PendingContactId p,
StaticTransportKeys k) throws DbException;
/** /**
* Stores a transport. * Stores a transport.
*/ */
@@ -314,6 +314,14 @@ interface Database<T> {
Map<ContactId, Boolean> getGroupVisibility(T txn, GroupId g) Map<ContactId, Boolean> getGroupVisibility(T txn, GroupId g)
throws DbException; throws DbException;
/**
* Returns all handshake keys for the given transport.
* <p/>
* Read-only.
*/
Collection<HandshakeKeySet> getHandshakeKeys(T txn, TransportId t)
throws DbException;
/** /**
* Returns the local pseudonym with the given ID. * Returns the local pseudonym with the given ID.
* <p/> * <p/>
@@ -528,14 +536,6 @@ interface Database<T> {
*/ */
Settings getSettings(T txn, String namespace) throws DbException; Settings getSettings(T txn, String namespace) throws DbException;
/**
* Returns all static transport keys for the given transport.
* <p/>
* Read-only.
*/
Collection<StaticTransportKeySet> getStaticTransportKeys(T txn,
TransportId t) throws DbException;
/** /**
* Returns all transport keys for the given transport. * Returns all transport keys for the given transport.
* <p/> * <p/>
@@ -545,10 +545,9 @@ interface Database<T> {
throws DbException; throws DbException;
/** /**
* Increments the outgoing stream counter for the given static transport * Increments the outgoing stream counter for the given handshake keys.
* keys.
*/ */
void incrementStreamCounter(T txn, TransportId t, StaticTransportKeySetId k) void incrementStreamCounter(T txn, TransportId t, HandshakeKeySetId k)
throws DbException; throws DbException;
/** /**
@@ -623,6 +622,12 @@ interface Database<T> {
void removeGroupVisibility(T txn, ContactId c, GroupId g) void removeGroupVisibility(T txn, ContactId c, GroupId g)
throws DbException; throws DbException;
/**
* Removes the given handshake keys from the database.
*/
void removeHandshakeKeys(T txn, TransportId t, HandshakeKeySetId k)
throws DbException;
/** /**
* Removes a local pseudonym (and all associated state) from the database. * Removes a local pseudonym (and all associated state) from the database.
*/ */
@@ -645,12 +650,6 @@ interface Database<T> {
*/ */
void removePendingContact(T txn, PendingContactId p) throws DbException; void removePendingContact(T txn, PendingContactId p) throws DbException;
/**
* Removes the given static transport keys from the database.
*/
void removeStaticTransportKeys(T txn, TransportId t,
StaticTransportKeySetId k) throws DbException;
/** /**
* Removes a transport (and all associated state) from the database. * Removes a transport (and all associated state) from the database.
*/ */
@@ -710,19 +709,18 @@ interface Database<T> {
PendingContactState state) throws DbException; PendingContactState state) throws DbException;
/** /**
* Sets the reordering window for the given key set and transport in the * Sets the reordering window for the given transport key set in the given
* given time period. * time period.
*/ */
void setReorderingWindow(T txn, TransportKeySetId k, TransportId t, void setReorderingWindow(T txn, TransportKeySetId k, TransportId t,
long timePeriod, long base, byte[] bitmap) throws DbException; long timePeriod, long base, byte[] bitmap) throws DbException;
/** /**
* Sets the reordering window for the given static key set and transport in * Sets the reordering window for the given handshake key set in the given
* the given time period. * time period.
*/ */
void setStaticReorderingWindow(T txn, StaticTransportKeySetId k, void setReorderingWindow(T txn, HandshakeKeySetId k, TransportId t,
TransportId t, long timePeriod, long base, byte[] bitmap) long timePeriod, long base, byte[] bitmap) throws DbException;
throws DbException;
/** /**
* Marks the given transport keys as usable for outgoing streams. * Marks the given transport keys as usable for outgoing streams.
@@ -739,10 +737,9 @@ interface Database<T> {
throws DbException; throws DbException;
/** /**
* Updates the given static transport keys following key rotation. * Updates the given handshake keys.
*/ */
void updateStaticTransportKeys(T txn, StaticTransportKeySet ks) void updateHandshakeKeys(T txn, HandshakeKeySet ks) throws DbException;
throws DbException;
/** /**
* Updates the given transport keys following key rotation. * Updates the given transport keys following key rotation.

View File

@@ -60,9 +60,9 @@ import org.briarproject.bramble.api.sync.event.MessageToRequestEvent;
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent; import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
import org.briarproject.bramble.api.sync.event.MessagesSentEvent; import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
import org.briarproject.bramble.api.sync.validation.MessageState; import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.transport.StaticTransportKeySet; import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId; import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys; import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet; import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId; import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
@@ -257,6 +257,30 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
} }
} }
@Override
public HandshakeKeySetId addHandshakeKeys(Transaction transaction,
ContactId c, HandshakeKeys k) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
if (!db.containsTransport(txn, k.getTransportId()))
throw new NoSuchTransportException();
return db.addHandshakeKeys(txn, c, k);
}
@Override
public HandshakeKeySetId addHandshakeKeys(Transaction transaction,
PendingContactId p, HandshakeKeys k) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsPendingContact(txn, p))
throw new NoSuchContactException();
if (!db.containsTransport(txn, k.getTransportId()))
throw new NoSuchTransportException();
return db.addHandshakeKeys(txn, p, k);
}
@Override @Override
public void addLocalAuthor(Transaction transaction, LocalAuthor a) public void addLocalAuthor(Transaction transaction, LocalAuthor a)
throws DbException { throws DbException {
@@ -285,32 +309,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
db.mergeMessageMetadata(txn, m.getId(), meta); db.mergeMessageMetadata(txn, m.getId(), meta);
} }
@Override
public StaticTransportKeySetId addStaticTransportKeys(
Transaction transaction, ContactId c, StaticTransportKeys k)
throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
if (!db.containsTransport(txn, k.getTransportId()))
throw new NoSuchTransportException();
return db.addStaticTransportKeys(txn, c, k);
}
@Override
public StaticTransportKeySetId addStaticTransportKeys(
Transaction transaction, PendingContactId p,
StaticTransportKeys k) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsPendingContact(txn, p))
throw new NoSuchContactException();
if (!db.containsTransport(txn, k.getTransportId()))
throw new NoSuchTransportException();
return db.addStaticTransportKeys(txn, p, k);
}
@Override @Override
public void addTransport(Transaction transaction, TransportId t, public void addTransport(Transaction transaction, TransportId t,
int maxLatency) throws DbException { int maxLatency) throws DbException {
@@ -528,6 +526,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return db.getGroupVisibility(txn, c, g); return db.getGroupVisibility(txn, c, g);
} }
@Override
public Collection<HandshakeKeySet> getHandshakeKeys(Transaction transaction,
TransportId t) throws DbException {
T txn = unbox(transaction);
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
return db.getHandshakeKeys(txn, t);
}
@Override @Override
public LocalAuthor getLocalAuthor(Transaction transaction, AuthorId a) public LocalAuthor getLocalAuthor(Transaction transaction, AuthorId a)
throws DbException { throws DbException {
@@ -692,15 +699,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return db.getSettings(txn, namespace); return db.getSettings(txn, namespace);
} }
@Override
public Collection<StaticTransportKeySet> getStaticTransportKeys(
Transaction transaction, TransportId t) throws DbException {
T txn = unbox(transaction);
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
return db.getStaticTransportKeys(txn, t);
}
@Override @Override
public Collection<TransportKeySet> getTransportKeys(Transaction transaction, public Collection<TransportKeySet> getTransportKeys(Transaction transaction,
TransportId t) throws DbException { TransportId t) throws DbException {
@@ -712,8 +710,8 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
@Override @Override
public void incrementStreamCounter(Transaction txn, TransportId t, public void incrementStreamCounter(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException { HandshakeKeySetId k) throws DbException {
// TODO
} }
@Override @Override
@@ -867,6 +865,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
transaction.attach(new GroupVisibilityUpdatedEvent(affected)); transaction.attach(new GroupVisibilityUpdatedEvent(affected));
} }
@Override
public void removeHandshakeKeys(Transaction transaction,
TransportId t, HandshakeKeySetId k) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
db.removeHandshakeKeys(txn, t, k);
}
@Override @Override
public void removeLocalAuthor(Transaction transaction, AuthorId a) public void removeLocalAuthor(Transaction transaction, AuthorId a)
throws DbException { throws DbException {
@@ -889,16 +897,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
db.removeMessage(txn, m); db.removeMessage(txn, m);
} }
@Override
public void removeStaticTransportKeys(Transaction transaction,
TransportId t, StaticTransportKeySetId k) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
db.removeStaticTransportKeys(txn, t, k);
}
@Override @Override
public void removeTransport(Transaction transaction, TransportId t) public void removeTransport(Transaction transaction, TransportId t)
throws DbException { throws DbException {
@@ -1031,14 +1029,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
} }
@Override @Override
public void updateStaticTransportKeys(Transaction transaction, public void updateHandshakeKeys(Transaction transaction,
Collection<StaticTransportKeySet> keys) throws DbException { Collection<HandshakeKeySet> keys) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException(); if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction); T txn = unbox(transaction);
for (StaticTransportKeySet ks : keys) { for (HandshakeKeySet ks : keys) {
TransportId t = ks.getKeys().getTransportId(); TransportId t = ks.getKeys().getTransportId();
if (db.containsTransport(txn, t)) if (db.containsTransport(txn, t))
db.updateStaticTransportKeys(txn, ks); db.updateHandshakeKeys(txn, ks);
} }
} }

View File

@@ -29,11 +29,11 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.MessageStatus; import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.validation.MessageState; import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.system.Clock; 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.IncomingKeys;
import org.briarproject.bramble.api.transport.OutgoingKeys; import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.StaticTransportKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.TransportKeySet; import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId; import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
@@ -296,8 +296,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " timestamp BIGINT NOT NULL," + " timestamp BIGINT NOT NULL,"
+ " PRIMARY KEY (pendingContactId))"; + " PRIMARY KEY (pendingContactId))";
private static final String CREATE_OUTGOING_STATIC_KEYS = private static final String CREATE_OUTGOING_HANDSHAKE_KEYS =
"CREATE TABLE outgoingStaticKeys" "CREATE TABLE outgoingHandshakeKeys"
+ " (transportId _STRING NOT NULL," + " (transportId _STRING NOT NULL,"
+ " keySetId _COUNTER," + " keySetId _COUNTER,"
+ " timePeriod BIGINT NOT NULL," + " timePeriod BIGINT NOT NULL,"
@@ -320,8 +320,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " REFERENCES pendingContacts (pendingContactId)" + " REFERENCES pendingContacts (pendingContactId)"
+ " ON DELETE CASCADE)"; + " ON DELETE CASCADE)";
private static final String CREATE_INCOMING_STATIC_KEYS = private static final String CREATE_INCOMING_HANDSHAKE_KEYS =
"CREATE TABLE incomingStaticKeys" "CREATE TABLE incomingHandshakeKeys"
+ " (transportId _STRING NOT NULL," + " (transportId _STRING NOT NULL,"
+ " keySetId INT NOT NULL," + " keySetId INT NOT NULL,"
+ " timePeriod BIGINT NOT NULL," + " timePeriod BIGINT NOT NULL,"
@@ -335,7 +335,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " REFERENCES transports (transportId)" + " REFERENCES transports (transportId)"
+ " ON DELETE CASCADE," + " ON DELETE CASCADE,"
+ " FOREIGN KEY (keySetId)" + " FOREIGN KEY (keySetId)"
+ " REFERENCES outgoingStaticKeys (keySetId)" + " REFERENCES outgoingHandshakeKeys (keySetId)"
+ " ON DELETE CASCADE)"; + " ON DELETE CASCADE)";
private static final String INDEX_CONTACTS_BY_AUTHOR_ID = private static final String INDEX_CONTACTS_BY_AUTHOR_ID =
@@ -533,8 +533,10 @@ abstract class JdbcDatabase implements Database<Connection> {
s.executeUpdate(dbTypes.replaceTypes(CREATE_OUTGOING_KEYS)); s.executeUpdate(dbTypes.replaceTypes(CREATE_OUTGOING_KEYS));
s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_KEYS)); s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_KEYS));
s.executeUpdate(dbTypes.replaceTypes(CREATE_PENDING_CONTACTS)); s.executeUpdate(dbTypes.replaceTypes(CREATE_PENDING_CONTACTS));
s.executeUpdate(dbTypes.replaceTypes(CREATE_OUTGOING_STATIC_KEYS)); s.executeUpdate(dbTypes.replaceTypes(
s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_STATIC_KEYS)); CREATE_OUTGOING_HANDSHAKE_KEYS));
s.executeUpdate(dbTypes.replaceTypes(
CREATE_INCOMING_HANDSHAKE_KEYS));
s.close(); s.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s, LOG, WARNING); tryToClose(s, LOG, WARNING);
@@ -772,6 +774,103 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
@Override
public HandshakeKeySetId addHandshakeKeys(Connection txn, ContactId c,
HandshakeKeys k) throws DbException {
return addHandshakeKeys(txn, c, null, k);
}
@Override
public HandshakeKeySetId addHandshakeKeys(Connection txn,
PendingContactId p, HandshakeKeys k) throws DbException {
return addHandshakeKeys(txn, null, p, k);
}
private HandshakeKeySetId addHandshakeKeys(Connection txn,
@Nullable ContactId c, @Nullable PendingContactId p,
HandshakeKeys k) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Store the outgoing keys
String sql = "INSERT INTO outgoingHandshakeKeys (contactId,"
+ " pendingContactId, transportId, rootKey, alice,"
+ " timePeriod, tagKey, headerKey, stream)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
if (c == null) ps.setNull(1, INTEGER);
else ps.setInt(1, c.getInt());
if (p == null) ps.setNull(2, BINARY);
else ps.setBytes(2, p.getBytes());
ps.setString(3, k.getTransportId().getString());
ps.setBytes(4, k.getRootKey().getBytes());
ps.setBoolean(5, k.isAlice());
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
ps.setLong(6, outCurr.getTimePeriod());
ps.setBytes(7, outCurr.getTagKey().getBytes());
ps.setBytes(8, outCurr.getHeaderKey().getBytes());
ps.setLong(9, outCurr.getStreamCounter());
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
// Get the new (highest) key set ID
sql = "SELECT keySetId FROM outgoingHandshakeKeys"
+ " ORDER BY keySetId DESC LIMIT 1";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
if (!rs.next()) throw new DbStateException();
HandshakeKeySetId keySetId = new HandshakeKeySetId(rs.getInt(1));
if (rs.next()) throw new DbStateException();
rs.close();
ps.close();
// Store the incoming keys
sql = "INSERT INTO incomingHandshakeKeys (keySetId, transportId,"
+ " timePeriod, tagKey, headerKey, base, bitmap,"
+ " periodOffset)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setInt(1, keySetId.getInt());
ps.setString(2, k.getTransportId().getString());
// Previous time period
IncomingKeys inPrev = k.getPreviousIncomingKeys();
ps.setLong(3, inPrev.getTimePeriod());
ps.setBytes(4, inPrev.getTagKey().getBytes());
ps.setBytes(5, inPrev.getHeaderKey().getBytes());
ps.setLong(6, inPrev.getWindowBase());
ps.setBytes(7, inPrev.getWindowBitmap());
ps.setInt(8, OFFSET_PREV);
ps.addBatch();
// Current time period
IncomingKeys inCurr = k.getCurrentIncomingKeys();
ps.setLong(3, inCurr.getTimePeriod());
ps.setBytes(4, inCurr.getTagKey().getBytes());
ps.setBytes(5, inCurr.getHeaderKey().getBytes());
ps.setLong(6, inCurr.getWindowBase());
ps.setBytes(7, inCurr.getWindowBitmap());
ps.setInt(8, OFFSET_CURR);
ps.addBatch();
// Next time period
IncomingKeys inNext = k.getNextIncomingKeys();
ps.setLong(3, inNext.getTimePeriod());
ps.setBytes(4, inNext.getTagKey().getBytes());
ps.setBytes(5, inNext.getHeaderKey().getBytes());
ps.setLong(6, inNext.getWindowBase());
ps.setBytes(7, inNext.getWindowBitmap());
ps.setInt(8, OFFSET_NEXT);
ps.addBatch();
int[] batchAffected = ps.executeBatch();
if (batchAffected.length != 3) throw new DbStateException();
for (int rows : batchAffected)
if (rows != 1) throw new DbStateException();
ps.close();
return keySetId;
} catch (SQLException e) {
tryToClose(rs, LOG, WARNING);
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override @Override
public void addLocalAuthor(Connection txn, LocalAuthor a) public void addLocalAuthor(Connection txn, LocalAuthor a)
throws DbException { throws DbException {
@@ -974,104 +1073,6 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
@Override
public StaticTransportKeySetId addStaticTransportKeys(Connection txn,
ContactId c, StaticTransportKeys k) throws DbException {
return addStaticTransportKeys(txn, c, null, k);
}
@Override
public StaticTransportKeySetId addStaticTransportKeys(Connection txn,
PendingContactId p, StaticTransportKeys k) throws DbException {
return addStaticTransportKeys(txn, null, p, k);
}
private StaticTransportKeySetId addStaticTransportKeys(Connection txn,
@Nullable ContactId c, @Nullable PendingContactId p,
StaticTransportKeys k) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Store the outgoing keys
String sql = "INSERT INTO outgoingStaticKeys (contactId,"
+ " pendingContactId, transportId, rootKey, alice,"
+ " timePeriod, tagKey, headerKey, stream)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
if (c == null) ps.setNull(1, INTEGER);
else ps.setInt(1, c.getInt());
if (p == null) ps.setNull(2, BINARY);
else ps.setBytes(2, p.getBytes());
ps.setString(3, k.getTransportId().getString());
ps.setBytes(4, k.getRootKey().getBytes());
ps.setBoolean(5, k.isAlice());
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
ps.setLong(6, outCurr.getTimePeriod());
ps.setBytes(7, outCurr.getTagKey().getBytes());
ps.setBytes(8, outCurr.getHeaderKey().getBytes());
ps.setLong(9, outCurr.getStreamCounter());
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
// Get the new (highest) key set ID
sql = "SELECT keySetId FROM outgoingStaticKeys"
+ " ORDER BY keySetId DESC LIMIT 1";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
if (!rs.next()) throw new DbStateException();
StaticTransportKeySetId keySetId = new
StaticTransportKeySetId(rs.getInt(1));
if (rs.next()) throw new DbStateException();
rs.close();
ps.close();
// Store the incoming keys
sql = "INSERT INTO incomingStaticKeys (keySetId, transportId,"
+ " timePeriod, tagKey, headerKey, base, bitmap,"
+ " periodOffset)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setInt(1, keySetId.getInt());
ps.setString(2, k.getTransportId().getString());
// Previous time period
IncomingKeys inPrev = k.getPreviousIncomingKeys();
ps.setLong(3, inPrev.getTimePeriod());
ps.setBytes(4, inPrev.getTagKey().getBytes());
ps.setBytes(5, inPrev.getHeaderKey().getBytes());
ps.setLong(6, inPrev.getWindowBase());
ps.setBytes(7, inPrev.getWindowBitmap());
ps.setInt(8, OFFSET_PREV);
ps.addBatch();
// Current time period
IncomingKeys inCurr = k.getCurrentIncomingKeys();
ps.setLong(3, inCurr.getTimePeriod());
ps.setBytes(4, inCurr.getTagKey().getBytes());
ps.setBytes(5, inCurr.getHeaderKey().getBytes());
ps.setLong(6, inCurr.getWindowBase());
ps.setBytes(7, inCurr.getWindowBitmap());
ps.setInt(8, OFFSET_CURR);
ps.addBatch();
// Next time period
IncomingKeys inNext = k.getNextIncomingKeys();
ps.setLong(3, inNext.getTimePeriod());
ps.setBytes(4, inNext.getTagKey().getBytes());
ps.setBytes(5, inNext.getHeaderKey().getBytes());
ps.setLong(6, inNext.getWindowBase());
ps.setBytes(7, inNext.getWindowBitmap());
ps.setInt(8, OFFSET_NEXT);
ps.addBatch();
int[] batchAffected = ps.executeBatch();
if (batchAffected.length != 3) throw new DbStateException();
for (int rows : batchAffected)
if (rows != 1) throw new DbStateException();
ps.close();
return keySetId;
} catch (SQLException e) {
tryToClose(rs, LOG, WARNING);
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override @Override
public void addTransport(Connection txn, TransportId t, int maxLatency) public void addTransport(Connection txn, TransportId t, int maxLatency)
throws DbException { throws DbException {
@@ -1684,6 +1685,86 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
@Override
public Collection<HandshakeKeySet> getHandshakeKeys(Connection txn,
TransportId t) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Retrieve the incoming keys
String sql = "SELECT timePeriod, tagKey, headerKey, base, bitmap"
+ " FROM incomingHandshakeKeys"
+ " WHERE transportId = ?"
+ " ORDER BY keySetId, periodOffset";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
rs = ps.executeQuery();
List<IncomingKeys> inKeys = new ArrayList<>();
while (rs.next()) {
long timePeriod = rs.getLong(1);
SecretKey tagKey = new SecretKey(rs.getBytes(2));
SecretKey headerKey = new SecretKey(rs.getBytes(3));
long windowBase = rs.getLong(4);
byte[] windowBitmap = rs.getBytes(5);
inKeys.add(new IncomingKeys(tagKey, headerKey, timePeriod,
windowBase, windowBitmap));
}
rs.close();
ps.close();
// Retrieve the outgoing keys in the same order
sql = "SELECT keySetId, contactId, pendingContactId, timePeriod,"
+ " tagKey, headerKey, rootKey, alice, stream"
+ " FROM outgoingHandshakeKeys"
+ " WHERE transportId = ?"
+ " ORDER BY keySetId";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
rs = ps.executeQuery();
Collection<HandshakeKeySet> keys = new ArrayList<>();
for (int i = 0; rs.next(); i++) {
// There should be three times as many incoming keys
if (inKeys.size() < (i + 1) * 3) throw new DbStateException();
HandshakeKeySetId keySetId =
new HandshakeKeySetId(rs.getInt(1));
ContactId contactId = null;
int cId = rs.getInt(2);
if (!rs.wasNull()) contactId = new ContactId(cId);
PendingContactId pendingContactId = null;
byte[] pId = rs.getBytes(3);
if (!rs.wasNull()) pendingContactId = new PendingContactId(pId);
long timePeriod = rs.getLong(4);
SecretKey tagKey = new SecretKey(rs.getBytes(5));
SecretKey headerKey = new SecretKey(rs.getBytes(6));
SecretKey rootKey = new SecretKey(rs.getBytes(7));
boolean alice = rs.getBoolean(8);
long streamCounter = rs.getLong(9);
OutgoingKeys outCurr = new OutgoingKeys(tagKey, headerKey,
timePeriod, streamCounter, true);
IncomingKeys inPrev = inKeys.get(i * 3);
IncomingKeys inCurr = inKeys.get(i * 3 + 1);
IncomingKeys inNext = inKeys.get(i * 3 + 2);
HandshakeKeys handshakeKeys = new HandshakeKeys(t, inPrev,
inCurr, inNext, outCurr, rootKey, alice);
if (contactId == null) {
if (pendingContactId == null) throw new DbStateException();
keys.add(new HandshakeKeySet(keySetId, pendingContactId,
handshakeKeys));
} else {
if (pendingContactId != null) throw new DbStateException();
keys.add(new HandshakeKeySet(keySetId, contactId,
handshakeKeys));
}
}
rs.close();
ps.close();
return keys;
} catch (SQLException e) {
tryToClose(rs, LOG, WARNING);
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override @Override
public Collection<LocalAuthor> getLocalAuthors(Connection txn) public Collection<LocalAuthor> getLocalAuthors(Connection txn)
throws DbException { throws DbException {
@@ -2377,87 +2458,6 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
@Override
public Collection<StaticTransportKeySet> getStaticTransportKeys(
Connection txn, TransportId t) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Retrieve the incoming keys
String sql = "SELECT timePeriod, tagKey, headerKey, base, bitmap"
+ " FROM incomingStaticKeys"
+ " WHERE transportId = ?"
+ " ORDER BY keySetId, periodOffset";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
rs = ps.executeQuery();
List<IncomingKeys> inKeys = new ArrayList<>();
while (rs.next()) {
long timePeriod = rs.getLong(1);
SecretKey tagKey = new SecretKey(rs.getBytes(2));
SecretKey headerKey = new SecretKey(rs.getBytes(3));
long windowBase = rs.getLong(4);
byte[] windowBitmap = rs.getBytes(5);
inKeys.add(new IncomingKeys(tagKey, headerKey, timePeriod,
windowBase, windowBitmap));
}
rs.close();
ps.close();
// Retrieve the outgoing keys in the same order
sql = "SELECT keySetId, contactId, pendingContactId, timePeriod,"
+ " tagKey, headerKey, rootKey, alice, stream"
+ " FROM outgoingStaticKeys"
+ " WHERE transportId = ?"
+ " ORDER BY keySetId";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
rs = ps.executeQuery();
Collection<StaticTransportKeySet> keys = new ArrayList<>();
for (int i = 0; rs.next(); i++) {
// There should be three times as many incoming keys
if (inKeys.size() < (i + 1) * 3) throw new DbStateException();
StaticTransportKeySetId keySetId =
new StaticTransportKeySetId(rs.getInt(1));
ContactId contactId = null;
int cId = rs.getInt(2);
if (!rs.wasNull()) contactId = new ContactId(cId);
PendingContactId pendingContactId = null;
byte[] pId = rs.getBytes(3);
if (!rs.wasNull()) pendingContactId = new PendingContactId(pId);
long timePeriod = rs.getLong(4);
SecretKey tagKey = new SecretKey(rs.getBytes(5));
SecretKey headerKey = new SecretKey(rs.getBytes(6));
SecretKey rootKey = new SecretKey(rs.getBytes(7));
boolean alice = rs.getBoolean(8);
long streamCounter = rs.getLong(9);
OutgoingKeys outCurr = new OutgoingKeys(tagKey, headerKey,
timePeriod, streamCounter, true);
IncomingKeys inPrev = inKeys.get(i * 3);
IncomingKeys inCurr = inKeys.get(i * 3 + 1);
IncomingKeys inNext = inKeys.get(i * 3 + 2);
StaticTransportKeys staticTransportKeys =
new StaticTransportKeys(t, inPrev, inCurr, inNext,
outCurr, rootKey, alice);
if (contactId == null) {
if (pendingContactId == null) throw new DbStateException();
keys.add(new StaticTransportKeySet(keySetId,
pendingContactId, staticTransportKeys));
} else {
if (pendingContactId != null) throw new DbStateException();
keys.add(new StaticTransportKeySet(keySetId, contactId,
staticTransportKeys));
}
}
rs.close();
ps.close();
return keys;
} catch (SQLException e) {
tryToClose(rs, LOG, WARNING);
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override @Override
public Collection<TransportKeySet> getTransportKeys(Connection txn, public Collection<TransportKeySet> getTransportKeys(Connection txn,
TransportId t) throws DbException { TransportId t) throws DbException {
@@ -2527,10 +2527,10 @@ abstract class JdbcDatabase implements Database<Connection> {
@Override @Override
public void incrementStreamCounter(Connection txn, TransportId t, public void incrementStreamCounter(Connection txn, TransportId t,
StaticTransportKeySetId k) throws DbException { HandshakeKeySetId k) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE outgoingStaticKeys SET stream = stream + 1" String sql = "UPDATE outgoingHandshakeKeys SET stream = stream + 1"
+ " WHERE transportId = ? AND keySetId = ?"; + " WHERE transportId = ? AND keySetId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setString(1, t.getString()); ps.setString(1, t.getString());
@@ -2928,6 +2928,27 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
@Override
public void removeHandshakeKeys(Connection txn, TransportId t,
HandshakeKeySetId k) throws DbException {
PreparedStatement ps = null;
try {
// Delete any existing outgoing keys - this will also remove any
// incoming keys with the same key set ID
String sql = "DELETE FROM outgoingHandshakeKeys"
+ " WHERE transportId = ? AND keySetId = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
ps.setInt(2, k.getInt());
int affected = ps.executeUpdate();
if (affected < 0) throw new DbStateException();
ps.close();
} catch (SQLException e) {
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override @Override
public void removeLocalAuthor(Connection txn, AuthorId a) public void removeLocalAuthor(Connection txn, AuthorId a)
throws DbException { throws DbException {
@@ -3024,27 +3045,6 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
@Override
public void removeStaticTransportKeys(Connection txn, TransportId t,
StaticTransportKeySetId k) throws DbException {
PreparedStatement ps = null;
try {
// Delete any existing outgoing keys - this will also remove any
// incoming keys with the same key set ID
String sql = "DELETE FROM outgoingStaticKeys"
+ " WHERE transportId = ? AND keySetId = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
ps.setInt(2, k.getInt());
int affected = ps.executeUpdate();
if (affected < 0) throw new DbStateException();
ps.close();
} catch (SQLException e) {
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override @Override
public void removeTransport(Connection txn, TransportId t) public void removeTransport(Connection txn, TransportId t)
throws DbException { throws DbException {
@@ -3317,12 +3317,12 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
@Override @Override
public void setStaticReorderingWindow(Connection txn, public void setReorderingWindow(Connection txn, HandshakeKeySetId k,
StaticTransportKeySetId k, TransportId t, long timePeriod, TransportId t, long timePeriod, long base, byte[] bitmap)
long base, byte[] bitmap) throws DbException { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE incomingStaticKeys SET base = ?, bitmap = ?" String sql = "UPDATE incomingHandshakeKeys SET base = ?, bitmap = ?"
+ " WHERE transportId = ? AND keySetId = ?" + " WHERE transportId = ? AND keySetId = ?"
+ " AND timePeriod = ?"; + " AND timePeriod = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
@@ -3465,16 +3465,16 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
@Override @Override
public void updateStaticTransportKeys(Connection txn, public void updateHandshakeKeys(Connection txn, HandshakeKeySet ks)
StaticTransportKeySet ks) throws DbException { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
// Update the outgoing keys // Update the outgoing keys
String sql = "UPDATE outgoingStaticKeys SET timePeriod = ?," String sql = "UPDATE outgoingHandshakeKeys SET timePeriod = ?,"
+ " tagKey = ?, headerKey = ?, stream = ?" + " tagKey = ?, headerKey = ?, stream = ?"
+ " WHERE transportId = ? AND keySetId = ?"; + " WHERE transportId = ? AND keySetId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
StaticTransportKeys k = ks.getKeys(); HandshakeKeys k = ks.getKeys();
OutgoingKeys outCurr = k.getCurrentOutgoingKeys(); OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
ps.setLong(1, outCurr.getTimePeriod()); ps.setLong(1, outCurr.getTimePeriod());
ps.setBytes(2, outCurr.getTagKey().getBytes()); ps.setBytes(2, outCurr.getTagKey().getBytes());
@@ -3486,7 +3486,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
// Update the incoming keys // Update the incoming keys
sql = "UPDATE incomingStaticKeys SET timePeriod = ?," sql = "UPDATE incomingHandshakeKeys SET timePeriod = ?,"
+ " tagKey = ?, headerKey = ?, base = ?, bitmap = ?" + " tagKey = ?, headerKey = ?, base = ?, bitmap = ?"
+ " WHERE transportId = ? AND keySetId = ?" + " WHERE transportId = ? AND keySetId = ?"
+ " AND periodOffset = ?"; + " AND periodOffset = ?";

View File

@@ -50,7 +50,7 @@ class Migration41_42 implements Migration<Connection> {
+ " state INT NOT NULL," + " state INT NOT NULL,"
+ " timestamp BIGINT NOT NULL," + " timestamp BIGINT NOT NULL,"
+ " PRIMARY KEY (pendingContactId))")); + " PRIMARY KEY (pendingContactId))"));
s.execute(dbTypes.replaceTypes("CREATE TABLE outgoingStaticKeys" s.execute(dbTypes.replaceTypes("CREATE TABLE outgoingHandshakeKeys"
+ " (transportId _STRING NOT NULL," + " (transportId _STRING NOT NULL,"
+ " keySetId _COUNTER," + " keySetId _COUNTER,"
+ " timePeriod BIGINT NOT NULL," + " timePeriod BIGINT NOT NULL,"
@@ -72,7 +72,7 @@ class Migration41_42 implements Migration<Connection> {
+ " FOREIGN KEY (pendingContactId)" + " FOREIGN KEY (pendingContactId)"
+ " REFERENCES pendingContacts (pendingContactId)" + " REFERENCES pendingContacts (pendingContactId)"
+ " ON DELETE CASCADE)")); + " ON DELETE CASCADE)"));
s.execute(dbTypes.replaceTypes("CREATE TABLE incomingStaticKeys" s.execute(dbTypes.replaceTypes("CREATE TABLE incomingHandshakeKeys"
+ " (transportId _STRING NOT NULL," + " (transportId _STRING NOT NULL,"
+ " keySetId INT NOT NULL," + " keySetId INT NOT NULL,"
+ " timePeriod BIGINT NOT NULL," + " timePeriod BIGINT NOT NULL,"
@@ -86,7 +86,7 @@ class Migration41_42 implements Migration<Connection> {
+ " REFERENCES transports (transportId)" + " REFERENCES transports (transportId)"
+ " ON DELETE CASCADE," + " ON DELETE CASCADE,"
+ " FOREIGN KEY (keySetId)" + " FOREIGN KEY (keySetId)"
+ " REFERENCES outgoingStaticKeys (keySetId)" + " REFERENCES outgoingHandshakeKeys (keySetId)"
+ " ON DELETE CASCADE)")); + " ON DELETE CASCADE)"));
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s, LOG, WARNING); tryToClose(s, LOG, WARNING);

View File

@@ -4,7 +4,7 @@ import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.crypto.TransportCrypto; import org.briarproject.bramble.api.crypto.TransportCrypto;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.transport.StaticTransportKeys; import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.BrambleTestCase;
import org.briarproject.bramble.test.TestSecureRandomProvider; import org.briarproject.bramble.test.TestSecureRandomProvider;
import org.junit.Test; import org.junit.Test;
@@ -19,7 +19,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
public class StaticTransportKeyDerivationTest extends BrambleTestCase { public class HandshakeKeyDerivationTest extends BrambleTestCase {
private final CryptoComponent crypto = private final CryptoComponent crypto =
new CryptoComponentImpl(new TestSecureRandomProvider(), null); new CryptoComponentImpl(new TestSecureRandomProvider(), null);
@@ -30,74 +30,69 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
@Test @Test
public void testKeysAreDistinct() { public void testKeysAreDistinct() {
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, false); rootKey, 123, false);
assertAllDifferent(kA); assertAllDifferent(kA);
assertAllDifferent(kB); assertAllDifferent(kB);
} }
@Test @Test
public void testKeysAreNotUpdatedToPreviousPeriod() { public void testKeysAreNotUpdatedToPreviousPeriod() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 122);
transportCrypto.updateStaticTransportKeys(k, 122);
assertSame(k, k1); assertSame(k, k1);
} }
@Test @Test
public void testKeysAreNotUpdatedToCurrentPeriod() { public void testKeysAreNotUpdatedToCurrentPeriod() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 123);
transportCrypto.updateStaticTransportKeys(k, 123);
assertSame(k, k1); assertSame(k, k1);
} }
@Test @Test
public void testKeysAreUpdatedByOnePeriod() { public void testKeysAreUpdatedByOnePeriod() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 124);
transportCrypto.updateStaticTransportKeys(k, 124);
assertSame(k.getCurrentIncomingKeys(), k1.getPreviousIncomingKeys()); assertSame(k.getCurrentIncomingKeys(), k1.getPreviousIncomingKeys());
assertSame(k.getNextIncomingKeys(), k1.getCurrentIncomingKeys()); assertSame(k.getNextIncomingKeys(), k1.getCurrentIncomingKeys());
} }
@Test @Test
public void testKeysAreUpdatedByTwoPeriods() { public void testKeysAreUpdatedByTwoPeriods() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 125);
transportCrypto.updateStaticTransportKeys(k, 125);
assertSame(k.getNextIncomingKeys(), k1.getPreviousIncomingKeys()); assertSame(k.getNextIncomingKeys(), k1.getPreviousIncomingKeys());
} }
@Test @Test
public void testKeysAreUpdatedByThreePeriods() { public void testKeysAreUpdatedByThreePeriods() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 126);
transportCrypto.updateStaticTransportKeys(k, 126);
assertAllDifferent(k, k1); assertAllDifferent(k, k1);
} }
@Test @Test
public void testCurrentKeysMatchContact() { public void testCurrentKeysMatchContact() {
// Start in time period 123 // Start in time period 123
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, false); rootKey, 123, false);
// Alice's incoming keys should equal Bob's outgoing keys // Alice's incoming keys should equal Bob's outgoing keys
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys()); assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
// Bob's incoming keys should equal Alice's outgoing keys // Bob's incoming keys should equal Alice's outgoing keys
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys()); assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
// Update into the future // Update into the future
kA = transportCrypto.updateStaticTransportKeys(kA, 456); kA = transportCrypto.updateHandshakeKeys(kA, 456);
kB = transportCrypto.updateStaticTransportKeys(kB, 456); kB = transportCrypto.updateHandshakeKeys(kB, 456);
// Alice's incoming keys should equal Bob's outgoing keys // Alice's incoming keys should equal Bob's outgoing keys
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys()); assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
// Bob's incoming keys should equal Alice's outgoing keys // Bob's incoming keys should equal Alice's outgoing keys
@@ -107,21 +102,21 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
@Test @Test
public void testPreviousKeysMatchContact() { public void testPreviousKeysMatchContact() {
// Start in time period 123 // Start in time period 123
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, false); rootKey, 123, false);
// Compare Alice's previous keys in period 456 with Bob's current keys // Compare Alice's previous keys in period 456 with Bob's current keys
// in period 455 // in period 455
kA = transportCrypto.updateStaticTransportKeys(kA, 456); kA = transportCrypto.updateHandshakeKeys(kA, 456);
kB = transportCrypto.updateStaticTransportKeys(kB, 455); kB = transportCrypto.updateHandshakeKeys(kB, 455);
// Alice's previous incoming keys should equal Bob's current // Alice's previous incoming keys should equal Bob's current
// outgoing keys // outgoing keys
assertMatches(kA.getPreviousIncomingKeys(), assertMatches(kA.getPreviousIncomingKeys(),
kB.getCurrentOutgoingKeys()); kB.getCurrentOutgoingKeys());
// Compare Alice's current keys in period 456 with Bob's previous keys // Compare Alice's current keys in period 456 with Bob's previous keys
// in period 457 // in period 457
kB = transportCrypto.updateStaticTransportKeys(kB, 457); kB = transportCrypto.updateHandshakeKeys(kB, 457);
// Bob's previous incoming keys should equal Alice's current // Bob's previous incoming keys should equal Alice's current
// outgoing keys // outgoing keys
assertMatches(kB.getPreviousIncomingKeys(), assertMatches(kB.getPreviousIncomingKeys(),
@@ -131,19 +126,19 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
@Test @Test
public void testNextKeysMatchContact() { public void testNextKeysMatchContact() {
// Start in time period 123 // Start in time period 123
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys( HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, false); rootKey, 123, false);
// Compare Alice's current keys in period 456 with Bob's next keys in // Compare Alice's current keys in period 456 with Bob's next keys in
// period 455 // period 455
kA = transportCrypto.updateStaticTransportKeys(kA, 456); kA = transportCrypto.updateHandshakeKeys(kA, 456);
kB = transportCrypto.updateStaticTransportKeys(kB, 455); kB = transportCrypto.updateHandshakeKeys(kB, 455);
// Bob's next incoming keys should equal Alice's current outgoing keys // Bob's next incoming keys should equal Alice's current outgoing keys
assertMatches(kB.getNextIncomingKeys(), kA.getCurrentOutgoingKeys()); assertMatches(kB.getNextIncomingKeys(), kA.getCurrentOutgoingKeys());
// Compare Alice's next keys in period 456 with Bob's current keys // Compare Alice's next keys in period 456 with Bob's current keys
// in period 457 // in period 457
kB = transportCrypto.updateStaticTransportKeys(kB, 457); kB = transportCrypto.updateHandshakeKeys(kB, 457);
// Alice's next incoming keys should equal Bob's current outgoing keys // Alice's next incoming keys should equal Bob's current outgoing keys
assertMatches(kA.getNextIncomingKeys(), kB.getCurrentOutgoingKeys()); assertMatches(kA.getNextIncomingKeys(), kB.getCurrentOutgoingKeys());
} }
@@ -152,10 +147,10 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
public void testRootKeyAffectsOutput() { public void testRootKeyAffectsOutput() {
SecretKey rootKey1 = getSecretKey(); SecretKey rootKey1 = getSecretKey();
assertFalse(Arrays.equals(rootKey.getBytes(), rootKey1.getBytes())); assertFalse(Arrays.equals(rootKey.getBytes(), rootKey1.getBytes()));
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k1 = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey1, 123, true); rootKey1, 123, true);
assertAllDifferent(k, k1); assertAllDifferent(k, k1);
} }
@@ -163,10 +158,10 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
public void testTransportIdAffectsOutput() { public void testTransportIdAffectsOutput() {
TransportId transportId1 = getTransportId(); TransportId transportId1 = getTransportId();
assertNotEquals(transportId.getString(), transportId1.getString()); assertNotEquals(transportId.getString(), transportId1.getString());
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
transportId, rootKey, 123, true); rootKey, 123, true);
StaticTransportKeys k1 = transportCrypto.deriveStaticTransportKeys( HandshakeKeys k1 = transportCrypto.deriveHandshakeKeys(transportId1,
transportId1, rootKey, 123, true); rootKey, 123, true);
assertAllDifferent(k, k1); assertAllDifferent(k, k1);
} }
} }

View File

@@ -21,11 +21,11 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.MessageStatus; import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.validation.MessageState; import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.system.Clock; 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.IncomingKeys;
import org.briarproject.bramble.api.transport.OutgoingKeys; import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.StaticTransportKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.TransportKeySet; import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId; import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.api.transport.TransportKeys;
@@ -109,7 +109,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
private final TransportId transportId; private final TransportId transportId;
private final ContactId contactId; private final ContactId contactId;
private final TransportKeySetId keySetId, keySetId1; private final TransportKeySetId keySetId, keySetId1;
private final StaticTransportKeySetId staticKeySetId, staticKeySetId1; private final HandshakeKeySetId handshakeKeySetId, handshakeKeySetId1;
private final PendingContact pendingContact; private final PendingContact pendingContact;
private final Random random = new Random(); private final Random random = new Random();
@@ -126,8 +126,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
contactId = new ContactId(1); contactId = new ContactId(1);
keySetId = new TransportKeySetId(1); keySetId = new TransportKeySetId(1);
keySetId1 = new TransportKeySetId(2); keySetId1 = new TransportKeySetId(2);
staticKeySetId = new StaticTransportKeySetId(1); handshakeKeySetId = new HandshakeKeySetId(1);
staticKeySetId1 = new StaticTransportKeySetId(2); handshakeKeySetId1 = new HandshakeKeySetId(2);
pendingContact = getPendingContact(); pendingContact = getPendingContact();
} }
@@ -761,81 +761,79 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
} }
@Test @Test
public void testStaticTransportKeys() throws Exception { public void testHandshakeKeys() throws Exception {
long timePeriod = 123, timePeriod1 = 234; long timePeriod = 123, timePeriod1 = 234;
boolean alice = random.nextBoolean(); boolean alice = random.nextBoolean();
SecretKey rootKey = getSecretKey(); SecretKey rootKey = getSecretKey();
SecretKey rootKey1 = getSecretKey(); SecretKey rootKey1 = getSecretKey();
StaticTransportKeys keys = HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
createStaticTransportKeys(timePeriod, rootKey, alice); HandshakeKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
StaticTransportKeys keys1 =
createStaticTransportKeys(timePeriod1, rootKey1, alice);
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Initially there should be no static transport keys in the database // Initially there should be no handshake keys in the database
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId)); assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
// Add the contact, the transport and the static transport keys // Add the contact, the transport and the handshake keys
db.addLocalAuthor(txn, localAuthor); db.addLocalAuthor(txn, localAuthor);
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(), assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
true, true)); true, true));
db.addTransport(txn, transportId, 123); db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId, assertEquals(handshakeKeySetId,
db.addStaticTransportKeys(txn, contactId, keys)); db.addHandshakeKeys(txn, contactId, keys));
assertEquals(staticKeySetId1, assertEquals(handshakeKeySetId1,
db.addStaticTransportKeys(txn, contactId, keys1)); db.addHandshakeKeys(txn, contactId, keys1));
// Retrieve the static transport keys // Retrieve the handshake keys
Collection<StaticTransportKeySet> allKeys = Collection<HandshakeKeySet> allKeys =
db.getStaticTransportKeys(txn, transportId); db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size()); assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) { for (HandshakeKeySet ks : allKeys) {
assertEquals(contactId, ks.getContactId()); assertEquals(contactId, ks.getContactId());
assertNull(ks.getPendingContactId()); assertNull(ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) { if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(keys, ks.getKeys()); assertKeysEquals(keys, ks.getKeys());
} else { } else {
assertEquals(staticKeySetId1, ks.getKeySetId()); assertEquals(handshakeKeySetId1, ks.getKeySetId());
assertKeysEquals(keys1, ks.getKeys()); assertKeysEquals(keys1, ks.getKeys());
} }
} }
// Update the transport keys // Update the handshake keys
StaticTransportKeys updated = HandshakeKeys updated =
createStaticTransportKeys(timePeriod + 1, rootKey, alice); createHandshakeKeys(timePeriod + 1, rootKey, alice);
StaticTransportKeys updated1 = HandshakeKeys updated1 =
createStaticTransportKeys(timePeriod1 + 1, rootKey1, alice); createHandshakeKeys(timePeriod1 + 1, rootKey1, alice);
db.updateStaticTransportKeys(txn, new StaticTransportKeySet( db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId,
staticKeySetId, contactId, updated)); contactId, updated));
db.updateStaticTransportKeys(txn, new StaticTransportKeySet( db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId1,
staticKeySetId1, contactId, updated1)); contactId, updated1));
// Retrieve the static transport keys again // Retrieve the handshake keys again
allKeys = db.getStaticTransportKeys(txn, transportId); allKeys = db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size()); assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) { for (HandshakeKeySet ks : allKeys) {
assertEquals(contactId, ks.getContactId()); assertEquals(contactId, ks.getContactId());
assertNull(ks.getPendingContactId()); assertNull(ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) { if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(updated, ks.getKeys()); assertKeysEquals(updated, ks.getKeys());
} else { } else {
assertEquals(staticKeySetId1, ks.getKeySetId()); assertEquals(handshakeKeySetId1, ks.getKeySetId());
assertKeysEquals(updated1, ks.getKeys()); assertKeysEquals(updated1, ks.getKeys());
} }
} }
// Removing the contact should remove the static transport keys // Removing the contact should remove the handshake keys
db.removeContact(txn, contactId); db.removeContact(txn, contactId);
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId)); assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
db.commitTransaction(txn); db.commitTransaction(txn);
db.close(); db.close();
} }
private void assertKeysEquals(StaticTransportKeys expected, private void assertKeysEquals(HandshakeKeys expected,
StaticTransportKeys actual) { HandshakeKeys actual) {
assertEquals(expected.getTransportId(), actual.getTransportId()); assertEquals(expected.getTransportId(), actual.getTransportId());
assertEquals(expected.getTimePeriod(), actual.getTimePeriod()); assertEquals(expected.getTimePeriod(), actual.getTimePeriod());
assertArrayEquals(expected.getRootKey().getBytes(), assertArrayEquals(expected.getRootKey().getBytes(),
@@ -852,72 +850,70 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
} }
@Test @Test
public void testStaticTransportKeysForPendingContact() throws Exception { public void testHandshakeKeysForPendingContact() throws Exception {
long timePeriod = 123, timePeriod1 = 234; long timePeriod = 123, timePeriod1 = 234;
boolean alice = random.nextBoolean(); boolean alice = random.nextBoolean();
SecretKey rootKey = getSecretKey(); SecretKey rootKey = getSecretKey();
SecretKey rootKey1 = getSecretKey(); SecretKey rootKey1 = getSecretKey();
StaticTransportKeys keys = HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
createStaticTransportKeys(timePeriod, rootKey, alice); HandshakeKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
StaticTransportKeys keys1 =
createStaticTransportKeys(timePeriod1, rootKey1, alice);
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Initially there should be no static transport keys in the database // Initially there should be no handshake keys in the database
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId)); assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
// Add the pending contact, the transport and the static transport keys // Add the pending contact, the transport and the handshake keys
db.addPendingContact(txn, pendingContact); db.addPendingContact(txn, pendingContact);
db.addTransport(txn, transportId, 123); db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId, assertEquals(handshakeKeySetId, db.addHandshakeKeys(txn,
db.addStaticTransportKeys(txn, pendingContact.getId(), keys)); pendingContact.getId(), keys));
assertEquals(staticKeySetId1, assertEquals(handshakeKeySetId1, db.addHandshakeKeys(txn,
db.addStaticTransportKeys(txn, pendingContact.getId(), keys1)); pendingContact.getId(), keys1));
// Retrieve the static transport keys // Retrieve the handshake keys
Collection<StaticTransportKeySet> allKeys = Collection<HandshakeKeySet> allKeys =
db.getStaticTransportKeys(txn, transportId); db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size()); assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) { for (HandshakeKeySet ks : allKeys) {
assertNull(ks.getContactId()); assertNull(ks.getContactId());
assertEquals(pendingContact.getId(), ks.getPendingContactId()); assertEquals(pendingContact.getId(), ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) { if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(keys, ks.getKeys()); assertKeysEquals(keys, ks.getKeys());
} else { } else {
assertEquals(staticKeySetId1, ks.getKeySetId()); assertEquals(handshakeKeySetId1, ks.getKeySetId());
assertKeysEquals(keys1, ks.getKeys()); assertKeysEquals(keys1, ks.getKeys());
} }
} }
// Update the transport keys // Update the handshake keys
StaticTransportKeys updated = HandshakeKeys updated =
createStaticTransportKeys(timePeriod + 1, rootKey, alice); createHandshakeKeys(timePeriod + 1, rootKey, alice);
StaticTransportKeys updated1 = HandshakeKeys updated1 =
createStaticTransportKeys(timePeriod1 + 1, rootKey1, alice); createHandshakeKeys(timePeriod1 + 1, rootKey1, alice);
db.updateStaticTransportKeys(txn, new StaticTransportKeySet( db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId,
staticKeySetId, pendingContact.getId(), updated)); pendingContact.getId(), updated));
db.updateStaticTransportKeys(txn, new StaticTransportKeySet( db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId1,
staticKeySetId1, pendingContact.getId(), updated1)); pendingContact.getId(), updated1));
// Retrieve the static transport keys again // Retrieve the handshake keys again
allKeys = db.getStaticTransportKeys(txn, transportId); allKeys = db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size()); assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) { for (HandshakeKeySet ks : allKeys) {
assertNull(ks.getContactId()); assertNull(ks.getContactId());
assertEquals(pendingContact.getId(), ks.getPendingContactId()); assertEquals(pendingContact.getId(), ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) { if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(updated, ks.getKeys()); assertKeysEquals(updated, ks.getKeys());
} else { } else {
assertEquals(staticKeySetId1, ks.getKeySetId()); assertEquals(handshakeKeySetId1, ks.getKeySetId());
assertKeysEquals(updated1, ks.getKeys()); assertKeysEquals(updated1, ks.getKeys());
} }
} }
// Removing the pending contact should remove the static transport keys // Removing the pending contact should remove the handshake keys
db.removePendingContact(txn, pendingContact.getId()); db.removePendingContact(txn, pendingContact.getId());
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId)); assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
db.commitTransaction(txn); db.commitTransaction(txn);
db.close(); db.close();
@@ -970,31 +966,29 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
long timePeriod = 123; long timePeriod = 123;
SecretKey rootKey = getSecretKey(); SecretKey rootKey = getSecretKey();
boolean alice = random.nextBoolean(); boolean alice = random.nextBoolean();
StaticTransportKeys keys = HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
createStaticTransportKeys(timePeriod, rootKey, alice);
long streamCounter = keys.getCurrentOutgoingKeys().getStreamCounter(); long streamCounter = keys.getCurrentOutgoingKeys().getStreamCounter();
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add the contact, transport and static transport keys // Add the contact, transport and handshake keys
db.addLocalAuthor(txn, localAuthor); db.addLocalAuthor(txn, localAuthor);
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(), assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
true, true)); true, true));
db.addTransport(txn, transportId, 123); db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId, assertEquals(handshakeKeySetId, db.addHandshakeKeys(txn, contactId, keys));
db.addStaticTransportKeys(txn, contactId, keys));
// Increment the stream counter twice and retrieve the keys // Increment the stream counter twice and retrieve the handshake keys
db.incrementStreamCounter(txn, transportId, staticKeySetId); db.incrementStreamCounter(txn, transportId, handshakeKeySetId);
db.incrementStreamCounter(txn, transportId, staticKeySetId); db.incrementStreamCounter(txn, transportId, handshakeKeySetId);
Collection<StaticTransportKeySet> newKeys = Collection<HandshakeKeySet> newKeys =
db.getStaticTransportKeys(txn, transportId); db.getHandshakeKeys(txn, transportId);
assertEquals(1, newKeys.size()); assertEquals(1, newKeys.size());
StaticTransportKeySet ks = newKeys.iterator().next(); HandshakeKeySet ks = newKeys.iterator().next();
assertEquals(staticKeySetId, ks.getKeySetId()); assertEquals(handshakeKeySetId, ks.getKeySetId());
assertEquals(contactId, ks.getContactId()); assertEquals(contactId, ks.getContactId());
StaticTransportKeys k = ks.getKeys(); HandshakeKeys k = ks.getKeys();
assertEquals(transportId, k.getTransportId()); assertEquals(transportId, k.getTransportId());
assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes()); assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes());
assertEquals(alice, k.isAlice()); assertEquals(alice, k.isAlice());
@@ -1064,33 +1058,32 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
long timePeriod = 123; long timePeriod = 123;
SecretKey rootKey = getSecretKey(); SecretKey rootKey = getSecretKey();
boolean alice = random.nextBoolean(); boolean alice = random.nextBoolean();
StaticTransportKeys keys = HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
createStaticTransportKeys(timePeriod, rootKey, alice);
long base = keys.getCurrentIncomingKeys().getWindowBase(); long base = keys.getCurrentIncomingKeys().getWindowBase();
byte[] bitmap = keys.getCurrentIncomingKeys().getWindowBitmap(); byte[] bitmap = keys.getCurrentIncomingKeys().getWindowBitmap();
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add the contact, transport and static transport keys // Add the contact, transport and handshake keys
db.addLocalAuthor(txn, localAuthor); db.addLocalAuthor(txn, localAuthor);
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(), assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
true, true)); true, true));
db.addTransport(txn, transportId, 123); db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId, assertEquals(handshakeKeySetId,
db.addStaticTransportKeys(txn, contactId, keys)); db.addHandshakeKeys(txn, contactId, keys));
// Update the reordering window and retrieve the static transport keys // Update the reordering window and retrieve the handshake keys
random.nextBytes(bitmap); random.nextBytes(bitmap);
db.setStaticReorderingWindow(txn, staticKeySetId, transportId, db.setReorderingWindow(txn, handshakeKeySetId, transportId, timePeriod,
timePeriod, base + 1, bitmap); base + 1, bitmap);
Collection<StaticTransportKeySet> newKeys = Collection<HandshakeKeySet> newKeys =
db.getStaticTransportKeys(txn, transportId); db.getHandshakeKeys(txn, transportId);
assertEquals(1, newKeys.size()); assertEquals(1, newKeys.size());
StaticTransportKeySet ks = newKeys.iterator().next(); HandshakeKeySet ks = newKeys.iterator().next();
assertEquals(staticKeySetId, ks.getKeySetId()); assertEquals(handshakeKeySetId, ks.getKeySetId());
assertEquals(contactId, ks.getContactId()); assertEquals(contactId, ks.getContactId());
StaticTransportKeys k = ks.getKeys(); HandshakeKeys k = ks.getKeys();
assertEquals(transportId, k.getTransportId()); assertEquals(transportId, k.getTransportId());
assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes()); assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes());
assertEquals(alice, k.isAlice()); assertEquals(alice, k.isAlice());
@@ -2308,7 +2301,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr); return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);
} }
private StaticTransportKeys createStaticTransportKeys(long timePeriod, private HandshakeKeys createHandshakeKeys(long timePeriod,
SecretKey rootKey, boolean alice) { SecretKey rootKey, boolean alice) {
SecretKey inPrevTagKey = getSecretKey(); SecretKey inPrevTagKey = getSecretKey();
SecretKey inPrevHeaderKey = getSecretKey(); SecretKey inPrevHeaderKey = getSecretKey();
@@ -2326,8 +2319,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
SecretKey outCurrHeaderKey = getSecretKey(); SecretKey outCurrHeaderKey = getSecretKey();
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey, OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
timePeriod, 456, true); timePeriod, 456, true);
return new StaticTransportKeys(transportId, inPrev, inCurr, inNext, return new HandshakeKeys(transportId, inPrev, inCurr, inNext, outCurr,
outCurr, rootKey, alice); rootKey, alice);
} }
@After @After