If current keys are not in handshake mode, look for handshake mode keys

This commit is contained in:
ameba23
2021-08-27 10:04:50 +02:00
parent 9cf00efa9c
commit 8437aa1b10
2 changed files with 51 additions and 1 deletions

View File

@@ -48,4 +48,7 @@ interface TransportKeyManager {
StreamContext getStreamContext(Transaction txn, byte[] tag)
throws DbException;
@Nullable
StreamContext getStreamContextInHandshakeMode(Transaction txn, ContactId c)
throws DbException;
}

View File

@@ -372,6 +372,8 @@ class TransportKeyManagerImpl implements TransportKeyManager {
MutableTransportKeySet ks = getOutgoingKeySet(c, p);
if (ks == null) return null;
MutableTransportKeys keys = ks.getKeys();
LOG.info("Using keys for outgoing connection - handshake mode: " + keys.isHandshakeMode());
MutableOutgoingKeys outKeys = keys.getCurrentOutgoingKeys();
if (!outKeys.isActive()) throw new AssertionError();
if (outKeys.getStreamCounter() > MAX_32_BIT_UNSIGNED) return null;
@@ -395,7 +397,14 @@ class TransportKeyManagerImpl implements TransportKeyManager {
try {
// Look up the incoming keys for the tag
TagContext tagCtx = inContexts.remove(new Bytes(tag));
if (tagCtx == null) return null;
if (tagCtx == null) {
LOG.info("Cannot find tag!");
for (MutableTransportKeySet t : keys.values()) {
LOG.info("Header key: " + t.getKeys().getCurrentIncomingKeys().getHeaderKey().getBytes().toString());
LOG.info("Tag key: " + t.getKeys().getCurrentIncomingKeys().getTagKey().getBytes().toString());
}
return null;
}
MutableIncomingKeys inKeys = tagCtx.inKeys;
// Create a stream context
StreamContext ctx = new StreamContext(tagCtx.contactId,
@@ -443,6 +452,44 @@ class TransportKeyManagerImpl implements TransportKeyManager {
}
}
@Override
public StreamContext getStreamContextInHandshakeMode(Transaction txn, ContactId c)
throws DbException {
lock.lock();
try {
MutableTransportKeySet ks = getOutgoingKeySet(c, null);
if (ks == null) return null;
MutableTransportKeys currentKeys = ks.getKeys();
LOG.info("Current keys handshake mode? " + currentKeys.isHandshakeMode());
if (currentKeys.isHandshakeMode()) return getStreamContext(txn, c, null);
for (MutableTransportKeySet keySet : this.keys.values()) {
MutableTransportKeys keys = keySet.getKeys();
if (keys.isHandshakeMode()) continue;
LOG.info("Found handshake mode keys");
MutableOutgoingKeys outKeys = keys.getCurrentOutgoingKeys();
// if (!outKeys.isActive()) throw new AssertionError();
if (outKeys.getStreamCounter() > MAX_32_BIT_UNSIGNED) return null;
// Create a stream context
LOG.info("Creating handshake mode stream context");
StreamContext ctx = new StreamContext(c, null, transportId,
outKeys.getTagKey(), outKeys.getHeaderKey(),
outKeys.getStreamCounter(), keys.isHandshakeMode());
LOG.info("Tag key: " + outKeys.getTagKey().getBytes().toString());
LOG.info("Header key: " + outKeys.getHeaderKey().getBytes().toString());
// Increment the stream counter and write it back to the DB
outKeys.incrementStreamCounter();
db.incrementStreamCounter(txn, transportId, keySet.getKeySetId());
LOG.info("Returning");
return ctx;
}
LOG.info("Cannot find handshake mode keys");
return null;
} finally {
lock.unlock();
}
}
@DatabaseExecutor
@Wakeful
private void updateKeys(Transaction txn) throws DbException {