mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Rename static transport keys to handshake keys.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = ?";
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user