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;
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;
/**
@@ -27,21 +27,19 @@ public interface TransportCrypto {
TransportKeys rotateTransportKeys(TransportKeys k, long timePeriod);
/**
* Derives static transport keys for the given transport in the given time
* period from the given root key.
* Derives handshake keys for the given transport in the given time period
* from the given root key.
*
* @param alice whether the keys are for use by Alice or Bob.
*/
StaticTransportKeys deriveStaticTransportKeys(TransportId t,
SecretKey rootKey, long timePeriod, boolean alice);
HandshakeKeys deriveHandshakeKeys(TransportId t, SecretKey rootKey,
long timePeriod, boolean alice);
/**
* Updates the given static transport keys to the given time period. If
* the keys are for the given period or any later period they are not
* updated.
* Updates the given handshake keys to the given time period. If the keys
* are for the given period or any later period they are not updated.
*/
StaticTransportKeys updateStaticTransportKeys(StaticTransportKeys k,
long timePeriod);
HandshakeKeys updateHandshakeKeys(HandshakeKeys k, long timePeriod);
/**
* 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.Request;
import org.briarproject.bramble.api.sync.validation.MessageState;
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.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
@@ -112,6 +112,20 @@ public interface DatabaseComponent {
*/
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.
*/
@@ -123,20 +137,6 @@ public interface DatabaseComponent {
void addLocalMessage(Transaction txn, Message m, Metadata meta,
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.
*/
@@ -287,6 +287,14 @@ public interface DatabaseComponent {
Visibility getGroupVisibility(Transaction txn, ContactId c, GroupId g)
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.
* <p/>
@@ -442,14 +450,6 @@ public interface DatabaseComponent {
*/
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.
* <p/>
@@ -459,11 +459,10 @@ public interface DatabaseComponent {
throws DbException;
/**
* Increments the outgoing stream counter for the given static transport
* keys.
* Increments the outgoing stream counter for the given handshake keys.
*/
void incrementStreamCounter(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException;
HandshakeKeySetId k) throws DbException;
/**
* 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;
/**
* 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.
*/
@@ -534,12 +539,6 @@ public interface DatabaseComponent {
*/
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.
*/
@@ -606,11 +605,10 @@ public interface DatabaseComponent {
TransportKeySetId k) throws DbException;
/**
* Stores the given static transport keys, deleting any keys they have
* replaced.
* Stores the given handshake keys, deleting any keys they have replaced.
*/
void updateStaticTransportKeys(Transaction txn,
Collection<StaticTransportKeySet> keys) throws DbException;
void updateHandshakeKeys(Transaction txn, Collection<HandshakeKeySet> keys)
throws DbException;
/**
* 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;
/**
* Abstract superclass for {@link TransportKeys} and
* {@link StaticTransportKeys}.
* Abstract superclass for {@link TransportKeys} and {@link HandshakeKeys}.
*/
@Immutable
@NotNullByDefault

View File

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

View File

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

View File

@@ -109,26 +109,26 @@ public interface TransportConstants {
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";
/**
* 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";
/**
* 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";
/**
* 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";
}

View File

@@ -6,7 +6,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
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
@NotNullByDefault

View File

@@ -7,7 +7,7 @@ import javax.annotation.concurrent.Immutable;
/**
* 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
@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.TransportCrypto;
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.OutgoingKeys;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.TransportKeys;
import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.Blake2bDigest;
@@ -14,13 +14,13 @@ import org.spongycastle.crypto.digests.Blake2bDigest;
import javax.inject.Inject;
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_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.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_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.ROTATE_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
@@ -115,49 +115,51 @@ class TransportCryptoImpl implements TransportCrypto {
}
@Override
public StaticTransportKeys deriveStaticTransportKeys(TransportId t,
SecretKey rootKey, long timePeriod, boolean alice) {
public HandshakeKeys deriveHandshakeKeys(TransportId t, SecretKey rootKey,
long timePeriod, boolean alice) {
if (timePeriod < 1) throw new IllegalArgumentException();
IncomingKeys inPrev = deriveStaticIncomingKeys(t, rootKey, alice,
IncomingKeys inPrev = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod - 1);
IncomingKeys inCurr = deriveStaticIncomingKeys(t, rootKey, alice,
IncomingKeys inCurr = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod);
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice,
IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice,
OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey, alice,
timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr, rootKey,
alice);
}
private IncomingKeys deriveStaticIncomingKeys(TransportId t,
private IncomingKeys deriveIncomingHandshakeKeys(TransportId t,
SecretKey rootKey, boolean alice, long timePeriod) {
SecretKey tag = deriveStaticTagKey(t, rootKey, !alice, timePeriod);
SecretKey header = deriveStaticHeaderKey(t, rootKey, !alice,
SecretKey tag = deriveHandshakeTagKey(t, rootKey, !alice, timePeriod);
SecretKey header = deriveHandshakeHeaderKey(t, rootKey, !alice,
timePeriod);
return new IncomingKeys(tag, header, timePeriod);
}
private OutgoingKeys deriveStaticOutgoingKeys(TransportId t,
private OutgoingKeys deriveOutgoingHandshakeKeys(TransportId t,
SecretKey rootKey, boolean alice, long timePeriod) {
SecretKey tag = deriveStaticTagKey(t, rootKey, alice, timePeriod);
SecretKey header = deriveStaticHeaderKey(t, rootKey, alice, timePeriod);
SecretKey tag = deriveHandshakeTagKey(t, rootKey, alice, timePeriod);
SecretKey header = deriveHandshakeHeaderKey(t, rootKey, alice,
timePeriod);
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) {
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[] period = new byte[INT_64_BYTES];
writeUint64(timePeriod, period, 0);
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) {
String label =
alice ? ALICE_STATIC_HEADER_LABEL : BOB_STATIC_HEADER_LABEL;
String label = alice ? ALICE_HANDSHAKE_HEADER_LABEL :
BOB_HANDSHAKE_HEADER_LABEL;
byte[] id = toUtf8(t.getString());
byte[] period = new byte[INT_64_BYTES];
writeUint64(timePeriod, period, 0);
@@ -165,8 +167,7 @@ class TransportCryptoImpl implements TransportCrypto {
}
@Override
public StaticTransportKeys updateStaticTransportKeys(StaticTransportKeys k,
long timePeriod) {
public HandshakeKeys updateHandshakeKeys(HandshakeKeys k, long timePeriod) {
long elapsed = timePeriod - k.getTimePeriod();
TransportId t = k.getTransportId();
SecretKey rootKey = k.getRootKey();
@@ -178,26 +179,26 @@ class TransportCryptoImpl implements TransportCrypto {
// The keys are one period old - shift by one period
IncomingKeys inPrev = k.getCurrentIncomingKeys();
IncomingKeys inCurr = k.getNextIncomingKeys();
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice,
timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice,
timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr,
IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey,
alice, timePeriod + 1);
OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey,
alice, timePeriod);
return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
} else if (elapsed == 2) {
// The keys are two periods old - shift by two periods
IncomingKeys inPrev = k.getNextIncomingKeys();
IncomingKeys inCurr = deriveStaticIncomingKeys(t, rootKey, alice,
timePeriod);
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice,
timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice,
timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr,
IncomingKeys inCurr = deriveIncomingHandshakeKeys(t, rootKey,
alice, timePeriod);
IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey,
alice, timePeriod + 1);
OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey,
alice, timePeriod);
return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
} else {
// 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.MessageStatus;
import org.briarproject.bramble.api.sync.validation.MessageState;
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.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
@@ -105,6 +105,20 @@ interface Database<T> {
void addGroupVisibility(T txn, ContactId c, GroupId g, boolean shared)
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.
*/
@@ -136,20 +150,6 @@ interface Database<T> {
*/
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.
*/
@@ -314,6 +314,14 @@ interface Database<T> {
Map<ContactId, Boolean> getGroupVisibility(T txn, GroupId g)
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.
* <p/>
@@ -528,14 +536,6 @@ interface Database<T> {
*/
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.
* <p/>
@@ -545,10 +545,9 @@ interface Database<T> {
throws DbException;
/**
* Increments the outgoing stream counter for the given static transport
* keys.
* Increments the outgoing stream counter for the given handshake keys.
*/
void incrementStreamCounter(T txn, TransportId t, StaticTransportKeySetId k)
void incrementStreamCounter(T txn, TransportId t, HandshakeKeySetId k)
throws DbException;
/**
@@ -623,6 +622,12 @@ interface Database<T> {
void removeGroupVisibility(T txn, ContactId c, GroupId g)
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.
*/
@@ -645,12 +650,6 @@ interface Database<T> {
*/
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.
*/
@@ -710,19 +709,18 @@ interface Database<T> {
PendingContactState state) throws DbException;
/**
* Sets the reordering window for the given key set and transport in the
* given time period.
* Sets the reordering window for the given transport key set in the given
* time period.
*/
void setReorderingWindow(T txn, TransportKeySetId k, TransportId t,
long timePeriod, long base, byte[] bitmap) throws DbException;
/**
* Sets the reordering window for the given static key set and transport in
* the given time period.
* Sets the reordering window for the given handshake key set in the given
* time period.
*/
void setStaticReorderingWindow(T txn, StaticTransportKeySetId k,
TransportId t, long timePeriod, long base, byte[] bitmap)
throws DbException;
void setReorderingWindow(T txn, HandshakeKeySetId k, TransportId t,
long timePeriod, long base, byte[] bitmap) throws DbException;
/**
* Marks the given transport keys as usable for outgoing streams.
@@ -739,10 +737,9 @@ interface Database<T> {
throws DbException;
/**
* Updates the given static transport keys following key rotation.
* Updates the given handshake keys.
*/
void updateStaticTransportKeys(T txn, StaticTransportKeySet ks)
throws DbException;
void updateHandshakeKeys(T txn, HandshakeKeySet ks) throws DbException;
/**
* 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.MessagesSentEvent;
import org.briarproject.bramble.api.sync.validation.MessageState;
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.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
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
public void addLocalAuthor(Transaction transaction, LocalAuthor a)
throws DbException {
@@ -285,32 +309,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
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
public void addTransport(Transaction transaction, TransportId t,
int maxLatency) throws DbException {
@@ -528,6 +526,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
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
public LocalAuthor getLocalAuthor(Transaction transaction, AuthorId a)
throws DbException {
@@ -692,15 +699,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
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
public Collection<TransportKeySet> getTransportKeys(Transaction transaction,
TransportId t) throws DbException {
@@ -712,8 +710,8 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
@Override
public void incrementStreamCounter(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException {
HandshakeKeySetId k) throws DbException {
// TODO
}
@Override
@@ -867,6 +865,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
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
public void removeLocalAuthor(Transaction transaction, AuthorId a)
throws DbException {
@@ -889,16 +897,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
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
public void removeTransport(Transaction transaction, TransportId t)
throws DbException {
@@ -1031,14 +1029,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
@Override
public void updateStaticTransportKeys(Transaction transaction,
Collection<StaticTransportKeySet> keys) throws DbException {
public void updateHandshakeKeys(Transaction transaction,
Collection<HandshakeKeySet> keys) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
for (StaticTransportKeySet ks : keys) {
for (HandshakeKeySet ks : keys) {
TransportId t = ks.getKeys().getTransportId();
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.validation.MessageState;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.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.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
@@ -296,8 +296,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " timestamp BIGINT NOT NULL,"
+ " PRIMARY KEY (pendingContactId))";
private static final String CREATE_OUTGOING_STATIC_KEYS =
"CREATE TABLE outgoingStaticKeys"
private static final String CREATE_OUTGOING_HANDSHAKE_KEYS =
"CREATE TABLE outgoingHandshakeKeys"
+ " (transportId _STRING NOT NULL,"
+ " keySetId _COUNTER,"
+ " timePeriod BIGINT NOT NULL,"
@@ -320,8 +320,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " REFERENCES pendingContacts (pendingContactId)"
+ " ON DELETE CASCADE)";
private static final String CREATE_INCOMING_STATIC_KEYS =
"CREATE TABLE incomingStaticKeys"
private static final String CREATE_INCOMING_HANDSHAKE_KEYS =
"CREATE TABLE incomingHandshakeKeys"
+ " (transportId _STRING NOT NULL,"
+ " keySetId INT NOT NULL,"
+ " timePeriod BIGINT NOT NULL,"
@@ -335,7 +335,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " REFERENCES transports (transportId)"
+ " ON DELETE CASCADE,"
+ " FOREIGN KEY (keySetId)"
+ " REFERENCES outgoingStaticKeys (keySetId)"
+ " REFERENCES outgoingHandshakeKeys (keySetId)"
+ " ON DELETE CASCADE)";
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_INCOMING_KEYS));
s.executeUpdate(dbTypes.replaceTypes(CREATE_PENDING_CONTACTS));
s.executeUpdate(dbTypes.replaceTypes(CREATE_OUTGOING_STATIC_KEYS));
s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_STATIC_KEYS));
s.executeUpdate(dbTypes.replaceTypes(
CREATE_OUTGOING_HANDSHAKE_KEYS));
s.executeUpdate(dbTypes.replaceTypes(
CREATE_INCOMING_HANDSHAKE_KEYS));
s.close();
} catch (SQLException e) {
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
public void addLocalAuthor(Connection txn, LocalAuthor a)
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
public void addTransport(Connection txn, TransportId t, int maxLatency)
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
public Collection<LocalAuthor> getLocalAuthors(Connection txn)
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
public Collection<TransportKeySet> getTransportKeys(Connection txn,
TransportId t) throws DbException {
@@ -2527,10 +2527,10 @@ abstract class JdbcDatabase implements Database<Connection> {
@Override
public void incrementStreamCounter(Connection txn, TransportId t,
StaticTransportKeySetId k) throws DbException {
HandshakeKeySetId k) throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE outgoingStaticKeys SET stream = stream + 1"
String sql = "UPDATE outgoingHandshakeKeys SET stream = stream + 1"
+ " WHERE transportId = ? AND keySetId = ?";
ps = txn.prepareStatement(sql);
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
public void removeLocalAuthor(Connection txn, AuthorId a)
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
public void removeTransport(Connection txn, TransportId t)
throws DbException {
@@ -3317,12 +3317,12 @@ abstract class JdbcDatabase implements Database<Connection> {
}
@Override
public void setStaticReorderingWindow(Connection txn,
StaticTransportKeySetId k, TransportId t, long timePeriod,
long base, byte[] bitmap) throws DbException {
public void setReorderingWindow(Connection txn, HandshakeKeySetId k,
TransportId t, long timePeriod, long base, byte[] bitmap)
throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE incomingStaticKeys SET base = ?, bitmap = ?"
String sql = "UPDATE incomingHandshakeKeys SET base = ?, bitmap = ?"
+ " WHERE transportId = ? AND keySetId = ?"
+ " AND timePeriod = ?";
ps = txn.prepareStatement(sql);
@@ -3465,16 +3465,16 @@ abstract class JdbcDatabase implements Database<Connection> {
}
@Override
public void updateStaticTransportKeys(Connection txn,
StaticTransportKeySet ks) throws DbException {
public void updateHandshakeKeys(Connection txn, HandshakeKeySet ks)
throws DbException {
PreparedStatement ps = null;
try {
// Update the outgoing keys
String sql = "UPDATE outgoingStaticKeys SET timePeriod = ?,"
String sql = "UPDATE outgoingHandshakeKeys SET timePeriod = ?,"
+ " tagKey = ?, headerKey = ?, stream = ?"
+ " WHERE transportId = ? AND keySetId = ?";
ps = txn.prepareStatement(sql);
StaticTransportKeys k = ks.getKeys();
HandshakeKeys k = ks.getKeys();
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
ps.setLong(1, outCurr.getTimePeriod());
ps.setBytes(2, outCurr.getTagKey().getBytes());
@@ -3486,7 +3486,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException();
ps.close();
// Update the incoming keys
sql = "UPDATE incomingStaticKeys SET timePeriod = ?,"
sql = "UPDATE incomingHandshakeKeys SET timePeriod = ?,"
+ " tagKey = ?, headerKey = ?, base = ?, bitmap = ?"
+ " WHERE transportId = ? AND keySetId = ?"
+ " AND periodOffset = ?";

View File

@@ -50,7 +50,7 @@ class Migration41_42 implements Migration<Connection> {
+ " state INT NOT NULL,"
+ " timestamp BIGINT NOT NULL,"
+ " PRIMARY KEY (pendingContactId))"));
s.execute(dbTypes.replaceTypes("CREATE TABLE outgoingStaticKeys"
s.execute(dbTypes.replaceTypes("CREATE TABLE outgoingHandshakeKeys"
+ " (transportId _STRING NOT NULL,"
+ " keySetId _COUNTER,"
+ " timePeriod BIGINT NOT NULL,"
@@ -72,7 +72,7 @@ class Migration41_42 implements Migration<Connection> {
+ " FOREIGN KEY (pendingContactId)"
+ " REFERENCES pendingContacts (pendingContactId)"
+ " ON DELETE CASCADE)"));
s.execute(dbTypes.replaceTypes("CREATE TABLE incomingStaticKeys"
s.execute(dbTypes.replaceTypes("CREATE TABLE incomingHandshakeKeys"
+ " (transportId _STRING NOT NULL,"
+ " keySetId INT NOT NULL,"
+ " timePeriod BIGINT NOT NULL,"
@@ -86,7 +86,7 @@ class Migration41_42 implements Migration<Connection> {
+ " REFERENCES transports (transportId)"
+ " ON DELETE CASCADE,"
+ " FOREIGN KEY (keySetId)"
+ " REFERENCES outgoingStaticKeys (keySetId)"
+ " REFERENCES outgoingHandshakeKeys (keySetId)"
+ " ON DELETE CASCADE)"));
} catch (SQLException e) {
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.TransportCrypto;
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.TestSecureRandomProvider;
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.assertSame;
public class StaticTransportKeyDerivationTest extends BrambleTestCase {
public class HandshakeKeyDerivationTest extends BrambleTestCase {
private final CryptoComponent crypto =
new CryptoComponentImpl(new TestSecureRandomProvider(), null);
@@ -30,74 +30,69 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
@Test
public void testKeysAreDistinct() {
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, false);
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, false);
assertAllDifferent(kA);
assertAllDifferent(kB);
}
@Test
public void testKeysAreNotUpdatedToPreviousPeriod() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 =
transportCrypto.updateStaticTransportKeys(k, 122);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 122);
assertSame(k, k1);
}
@Test
public void testKeysAreNotUpdatedToCurrentPeriod() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 =
transportCrypto.updateStaticTransportKeys(k, 123);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 123);
assertSame(k, k1);
}
@Test
public void testKeysAreUpdatedByOnePeriod() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 =
transportCrypto.updateStaticTransportKeys(k, 124);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 124);
assertSame(k.getCurrentIncomingKeys(), k1.getPreviousIncomingKeys());
assertSame(k.getNextIncomingKeys(), k1.getCurrentIncomingKeys());
}
@Test
public void testKeysAreUpdatedByTwoPeriods() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 =
transportCrypto.updateStaticTransportKeys(k, 125);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 125);
assertSame(k.getNextIncomingKeys(), k1.getPreviousIncomingKeys());
}
@Test
public void testKeysAreUpdatedByThreePeriods() {
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 =
transportCrypto.updateStaticTransportKeys(k, 126);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.updateHandshakeKeys(k, 126);
assertAllDifferent(k, k1);
}
@Test
public void testCurrentKeysMatchContact() {
// Start in time period 123
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, false);
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, false);
// Alice's incoming keys should equal Bob's outgoing keys
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
// Bob's incoming keys should equal Alice's outgoing keys
assertMatches(kB.getCurrentIncomingKeys(), kA.getCurrentOutgoingKeys());
// Update into the future
kA = transportCrypto.updateStaticTransportKeys(kA, 456);
kB = transportCrypto.updateStaticTransportKeys(kB, 456);
kA = transportCrypto.updateHandshakeKeys(kA, 456);
kB = transportCrypto.updateHandshakeKeys(kB, 456);
// Alice's incoming keys should equal Bob's outgoing keys
assertMatches(kA.getCurrentIncomingKeys(), kB.getCurrentOutgoingKeys());
// Bob's incoming keys should equal Alice's outgoing keys
@@ -107,21 +102,21 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
@Test
public void testPreviousKeysMatchContact() {
// Start in time period 123
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, false);
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, false);
// Compare Alice's previous keys in period 456 with Bob's current keys
// in period 455
kA = transportCrypto.updateStaticTransportKeys(kA, 456);
kB = transportCrypto.updateStaticTransportKeys(kB, 455);
kA = transportCrypto.updateHandshakeKeys(kA, 456);
kB = transportCrypto.updateHandshakeKeys(kB, 455);
// Alice's previous incoming keys should equal Bob's current
// outgoing keys
assertMatches(kA.getPreviousIncomingKeys(),
kB.getCurrentOutgoingKeys());
// Compare Alice's current keys in period 456 with Bob's previous keys
// in period 457
kB = transportCrypto.updateStaticTransportKeys(kB, 457);
kB = transportCrypto.updateHandshakeKeys(kB, 457);
// Bob's previous incoming keys should equal Alice's current
// outgoing keys
assertMatches(kB.getPreviousIncomingKeys(),
@@ -131,19 +126,19 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
@Test
public void testNextKeysMatchContact() {
// Start in time period 123
StaticTransportKeys kA = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys kB = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, false);
HandshakeKeys kA = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys kB = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, false);
// Compare Alice's current keys in period 456 with Bob's next keys in
// period 455
kA = transportCrypto.updateStaticTransportKeys(kA, 456);
kB = transportCrypto.updateStaticTransportKeys(kB, 455);
kA = transportCrypto.updateHandshakeKeys(kA, 456);
kB = transportCrypto.updateHandshakeKeys(kB, 455);
// Bob's next incoming keys should equal Alice's current outgoing keys
assertMatches(kB.getNextIncomingKeys(), kA.getCurrentOutgoingKeys());
// Compare Alice's next keys in period 456 with Bob's current keys
// in period 457
kB = transportCrypto.updateStaticTransportKeys(kB, 457);
kB = transportCrypto.updateHandshakeKeys(kB, 457);
// Alice's next incoming keys should equal Bob's current outgoing keys
assertMatches(kA.getNextIncomingKeys(), kB.getCurrentOutgoingKeys());
}
@@ -152,10 +147,10 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
public void testRootKeyAffectsOutput() {
SecretKey rootKey1 = getSecretKey();
assertFalse(Arrays.equals(rootKey.getBytes(), rootKey1.getBytes()));
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey1, 123, true);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.deriveHandshakeKeys(transportId,
rootKey1, 123, true);
assertAllDifferent(k, k1);
}
@@ -163,10 +158,10 @@ public class StaticTransportKeyDerivationTest extends BrambleTestCase {
public void testTransportIdAffectsOutput() {
TransportId transportId1 = getTransportId();
assertNotEquals(transportId.getString(), transportId1.getString());
StaticTransportKeys k = transportCrypto.deriveStaticTransportKeys(
transportId, rootKey, 123, true);
StaticTransportKeys k1 = transportCrypto.deriveStaticTransportKeys(
transportId1, rootKey, 123, true);
HandshakeKeys k = transportCrypto.deriveHandshakeKeys(transportId,
rootKey, 123, true);
HandshakeKeys k1 = transportCrypto.deriveHandshakeKeys(transportId1,
rootKey, 123, true);
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.validation.MessageState;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.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.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
@@ -109,7 +109,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
private final TransportId transportId;
private final ContactId contactId;
private final TransportKeySetId keySetId, keySetId1;
private final StaticTransportKeySetId staticKeySetId, staticKeySetId1;
private final HandshakeKeySetId handshakeKeySetId, handshakeKeySetId1;
private final PendingContact pendingContact;
private final Random random = new Random();
@@ -126,8 +126,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
contactId = new ContactId(1);
keySetId = new TransportKeySetId(1);
keySetId1 = new TransportKeySetId(2);
staticKeySetId = new StaticTransportKeySetId(1);
staticKeySetId1 = new StaticTransportKeySetId(2);
handshakeKeySetId = new HandshakeKeySetId(1);
handshakeKeySetId1 = new HandshakeKeySetId(2);
pendingContact = getPendingContact();
}
@@ -761,81 +761,79 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
}
@Test
public void testStaticTransportKeys() throws Exception {
public void testHandshakeKeys() throws Exception {
long timePeriod = 123, timePeriod1 = 234;
boolean alice = random.nextBoolean();
SecretKey rootKey = getSecretKey();
SecretKey rootKey1 = getSecretKey();
StaticTransportKeys keys =
createStaticTransportKeys(timePeriod, rootKey, alice);
StaticTransportKeys keys1 =
createStaticTransportKeys(timePeriod1, rootKey1, alice);
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
HandshakeKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Initially there should be no static transport keys in the database
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId));
// Initially there should be no handshake keys in the database
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);
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
true, true));
db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId,
db.addStaticTransportKeys(txn, contactId, keys));
assertEquals(staticKeySetId1,
db.addStaticTransportKeys(txn, contactId, keys1));
assertEquals(handshakeKeySetId,
db.addHandshakeKeys(txn, contactId, keys));
assertEquals(handshakeKeySetId1,
db.addHandshakeKeys(txn, contactId, keys1));
// Retrieve the static transport keys
Collection<StaticTransportKeySet> allKeys =
db.getStaticTransportKeys(txn, transportId);
// Retrieve the handshake keys
Collection<HandshakeKeySet> allKeys =
db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) {
for (HandshakeKeySet ks : allKeys) {
assertEquals(contactId, ks.getContactId());
assertNull(ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) {
if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(keys, ks.getKeys());
} else {
assertEquals(staticKeySetId1, ks.getKeySetId());
assertEquals(handshakeKeySetId1, ks.getKeySetId());
assertKeysEquals(keys1, ks.getKeys());
}
}
// Update the transport keys
StaticTransportKeys updated =
createStaticTransportKeys(timePeriod + 1, rootKey, alice);
StaticTransportKeys updated1 =
createStaticTransportKeys(timePeriod1 + 1, rootKey1, alice);
db.updateStaticTransportKeys(txn, new StaticTransportKeySet(
staticKeySetId, contactId, updated));
db.updateStaticTransportKeys(txn, new StaticTransportKeySet(
staticKeySetId1, contactId, updated1));
// Update the handshake keys
HandshakeKeys updated =
createHandshakeKeys(timePeriod + 1, rootKey, alice);
HandshakeKeys updated1 =
createHandshakeKeys(timePeriod1 + 1, rootKey1, alice);
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId,
contactId, updated));
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId1,
contactId, updated1));
// Retrieve the static transport keys again
allKeys = db.getStaticTransportKeys(txn, transportId);
// Retrieve the handshake keys again
allKeys = db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) {
for (HandshakeKeySet ks : allKeys) {
assertEquals(contactId, ks.getContactId());
assertNull(ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) {
if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(updated, ks.getKeys());
} else {
assertEquals(staticKeySetId1, ks.getKeySetId());
assertEquals(handshakeKeySetId1, ks.getKeySetId());
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);
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId));
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
db.commitTransaction(txn);
db.close();
}
private void assertKeysEquals(StaticTransportKeys expected,
StaticTransportKeys actual) {
private void assertKeysEquals(HandshakeKeys expected,
HandshakeKeys actual) {
assertEquals(expected.getTransportId(), actual.getTransportId());
assertEquals(expected.getTimePeriod(), actual.getTimePeriod());
assertArrayEquals(expected.getRootKey().getBytes(),
@@ -852,72 +850,70 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
}
@Test
public void testStaticTransportKeysForPendingContact() throws Exception {
public void testHandshakeKeysForPendingContact() throws Exception {
long timePeriod = 123, timePeriod1 = 234;
boolean alice = random.nextBoolean();
SecretKey rootKey = getSecretKey();
SecretKey rootKey1 = getSecretKey();
StaticTransportKeys keys =
createStaticTransportKeys(timePeriod, rootKey, alice);
StaticTransportKeys keys1 =
createStaticTransportKeys(timePeriod1, rootKey1, alice);
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
HandshakeKeys keys1 = createHandshakeKeys(timePeriod1, rootKey1, alice);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Initially there should be no static transport keys in the database
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId));
// Initially there should be no handshake keys in the database
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.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId,
db.addStaticTransportKeys(txn, pendingContact.getId(), keys));
assertEquals(staticKeySetId1,
db.addStaticTransportKeys(txn, pendingContact.getId(), keys1));
assertEquals(handshakeKeySetId, db.addHandshakeKeys(txn,
pendingContact.getId(), keys));
assertEquals(handshakeKeySetId1, db.addHandshakeKeys(txn,
pendingContact.getId(), keys1));
// Retrieve the static transport keys
Collection<StaticTransportKeySet> allKeys =
db.getStaticTransportKeys(txn, transportId);
// Retrieve the handshake keys
Collection<HandshakeKeySet> allKeys =
db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) {
for (HandshakeKeySet ks : allKeys) {
assertNull(ks.getContactId());
assertEquals(pendingContact.getId(), ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) {
if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(keys, ks.getKeys());
} else {
assertEquals(staticKeySetId1, ks.getKeySetId());
assertEquals(handshakeKeySetId1, ks.getKeySetId());
assertKeysEquals(keys1, ks.getKeys());
}
}
// Update the transport keys
StaticTransportKeys updated =
createStaticTransportKeys(timePeriod + 1, rootKey, alice);
StaticTransportKeys updated1 =
createStaticTransportKeys(timePeriod1 + 1, rootKey1, alice);
db.updateStaticTransportKeys(txn, new StaticTransportKeySet(
staticKeySetId, pendingContact.getId(), updated));
db.updateStaticTransportKeys(txn, new StaticTransportKeySet(
staticKeySetId1, pendingContact.getId(), updated1));
// Update the handshake keys
HandshakeKeys updated =
createHandshakeKeys(timePeriod + 1, rootKey, alice);
HandshakeKeys updated1 =
createHandshakeKeys(timePeriod1 + 1, rootKey1, alice);
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId,
pendingContact.getId(), updated));
db.updateHandshakeKeys(txn, new HandshakeKeySet(handshakeKeySetId1,
pendingContact.getId(), updated1));
// Retrieve the static transport keys again
allKeys = db.getStaticTransportKeys(txn, transportId);
// Retrieve the handshake keys again
allKeys = db.getHandshakeKeys(txn, transportId);
assertEquals(2, allKeys.size());
for (StaticTransportKeySet ks : allKeys) {
for (HandshakeKeySet ks : allKeys) {
assertNull(ks.getContactId());
assertEquals(pendingContact.getId(), ks.getPendingContactId());
if (ks.getKeySetId().equals(staticKeySetId)) {
if (ks.getKeySetId().equals(handshakeKeySetId)) {
assertKeysEquals(updated, ks.getKeys());
} else {
assertEquals(staticKeySetId1, ks.getKeySetId());
assertEquals(handshakeKeySetId1, ks.getKeySetId());
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());
assertEquals(emptyList(), db.getStaticTransportKeys(txn, transportId));
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
db.commitTransaction(txn);
db.close();
@@ -970,31 +966,29 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
long timePeriod = 123;
SecretKey rootKey = getSecretKey();
boolean alice = random.nextBoolean();
StaticTransportKeys keys =
createStaticTransportKeys(timePeriod, rootKey, alice);
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
long streamCounter = keys.getCurrentOutgoingKeys().getStreamCounter();
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add the contact, transport and static transport keys
// Add the contact, transport and handshake keys
db.addLocalAuthor(txn, localAuthor);
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
true, true));
db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId,
db.addStaticTransportKeys(txn, contactId, keys));
assertEquals(handshakeKeySetId, db.addHandshakeKeys(txn, contactId, keys));
// Increment the stream counter twice and retrieve the keys
db.incrementStreamCounter(txn, transportId, staticKeySetId);
db.incrementStreamCounter(txn, transportId, staticKeySetId);
Collection<StaticTransportKeySet> newKeys =
db.getStaticTransportKeys(txn, transportId);
// Increment the stream counter twice and retrieve the handshake keys
db.incrementStreamCounter(txn, transportId, handshakeKeySetId);
db.incrementStreamCounter(txn, transportId, handshakeKeySetId);
Collection<HandshakeKeySet> newKeys =
db.getHandshakeKeys(txn, transportId);
assertEquals(1, newKeys.size());
StaticTransportKeySet ks = newKeys.iterator().next();
assertEquals(staticKeySetId, ks.getKeySetId());
HandshakeKeySet ks = newKeys.iterator().next();
assertEquals(handshakeKeySetId, ks.getKeySetId());
assertEquals(contactId, ks.getContactId());
StaticTransportKeys k = ks.getKeys();
HandshakeKeys k = ks.getKeys();
assertEquals(transportId, k.getTransportId());
assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes());
assertEquals(alice, k.isAlice());
@@ -1064,33 +1058,32 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
long timePeriod = 123;
SecretKey rootKey = getSecretKey();
boolean alice = random.nextBoolean();
StaticTransportKeys keys =
createStaticTransportKeys(timePeriod, rootKey, alice);
HandshakeKeys keys = createHandshakeKeys(timePeriod, rootKey, alice);
long base = keys.getCurrentIncomingKeys().getWindowBase();
byte[] bitmap = keys.getCurrentIncomingKeys().getWindowBitmap();
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add the contact, transport and static transport keys
// Add the contact, transport and handshake keys
db.addLocalAuthor(txn, localAuthor);
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
true, true));
db.addTransport(txn, transportId, 123);
assertEquals(staticKeySetId,
db.addStaticTransportKeys(txn, contactId, keys));
assertEquals(handshakeKeySetId,
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);
db.setStaticReorderingWindow(txn, staticKeySetId, transportId,
timePeriod, base + 1, bitmap);
Collection<StaticTransportKeySet> newKeys =
db.getStaticTransportKeys(txn, transportId);
db.setReorderingWindow(txn, handshakeKeySetId, transportId, timePeriod,
base + 1, bitmap);
Collection<HandshakeKeySet> newKeys =
db.getHandshakeKeys(txn, transportId);
assertEquals(1, newKeys.size());
StaticTransportKeySet ks = newKeys.iterator().next();
assertEquals(staticKeySetId, ks.getKeySetId());
HandshakeKeySet ks = newKeys.iterator().next();
assertEquals(handshakeKeySetId, ks.getKeySetId());
assertEquals(contactId, ks.getContactId());
StaticTransportKeys k = ks.getKeys();
HandshakeKeys k = ks.getKeys();
assertEquals(transportId, k.getTransportId());
assertArrayEquals(rootKey.getBytes(), k.getRootKey().getBytes());
assertEquals(alice, k.isAlice());
@@ -2308,7 +2301,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);
}
private StaticTransportKeys createStaticTransportKeys(long timePeriod,
private HandshakeKeys createHandshakeKeys(long timePeriod,
SecretKey rootKey, boolean alice) {
SecretKey inPrevTagKey = getSecretKey();
SecretKey inPrevHeaderKey = getSecretKey();
@@ -2326,8 +2319,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
SecretKey outCurrHeaderKey = getSecretKey();
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
timePeriod, 456, true);
return new StaticTransportKeys(transportId, inPrev, inCurr, inNext,
outCurr, rootKey, alice);
return new HandshakeKeys(transportId, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
}
@After