mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-22 23:59:54 +01:00
Type-safe transport IDs.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||
@@ -19,7 +20,7 @@ class ConnectionRecogniserFactoryImpl implements ConnectionRecogniserFactory {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public ConnectionRecogniser createConnectionRecogniser(int transportId) {
|
||||
return new ConnectionRecogniserImpl(transportId, crypto, db);
|
||||
public ConnectionRecogniser createConnectionRecogniser(TransportId t) {
|
||||
return new ConnectionRecogniserImpl(t, crypto, db);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import javax.crypto.SecretKey;
|
||||
|
||||
import net.sf.briar.api.Bytes;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseListener;
|
||||
@@ -24,7 +25,7 @@ import net.sf.briar.api.transport.ConnectionWindow;
|
||||
class ConnectionRecogniserImpl implements ConnectionRecogniser,
|
||||
DatabaseListener {
|
||||
|
||||
private final int transportId;
|
||||
private final TransportId id;
|
||||
private final CryptoComponent crypto;
|
||||
private final DatabaseComponent db;
|
||||
private final Map<Bytes, ContactId> ivToContact;
|
||||
@@ -34,9 +35,9 @@ DatabaseListener {
|
||||
private final Map<ContactId, ConnectionWindow> contactToWindow;
|
||||
private boolean initialised = false;
|
||||
|
||||
ConnectionRecogniserImpl(int transportId, CryptoComponent crypto,
|
||||
ConnectionRecogniserImpl(TransportId id, CryptoComponent crypto,
|
||||
DatabaseComponent db) {
|
||||
this.transportId = transportId;
|
||||
this.id = id;
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
// FIXME: There's probably a tidier way of maintaining all this state
|
||||
@@ -62,7 +63,7 @@ DatabaseListener {
|
||||
}
|
||||
contactToCipher.put(c, cipher);
|
||||
// Calculate the IVs for the contact's connection window
|
||||
ConnectionWindow w = db.getConnectionWindow(c, transportId);
|
||||
ConnectionWindow w = db.getConnectionWindow(c, id);
|
||||
Map<Long, Bytes> ivs = new HashMap<Long, Bytes>();
|
||||
for(Long unseen : w.getUnseenConnectionNumbers()) {
|
||||
Bytes expectedIv = new Bytes(encryptIv(c, unseen));
|
||||
@@ -81,7 +82,7 @@ DatabaseListener {
|
||||
}
|
||||
|
||||
private synchronized byte[] encryptIv(ContactId c, long connection) {
|
||||
byte[] iv = IvEncoder.encodeIv(true, transportId, connection);
|
||||
byte[] iv = IvEncoder.encodeIv(true, id, connection);
|
||||
Cipher cipher = contactToCipher.get(c);
|
||||
assert cipher != null;
|
||||
try {
|
||||
@@ -107,7 +108,7 @@ DatabaseListener {
|
||||
ConnectionWindow w = contactToWindow.get(contactId);
|
||||
assert w != null;
|
||||
w.setSeen(connection);
|
||||
db.setConnectionWindow(contactId, transportId, w);
|
||||
db.setConnectionWindow(contactId, id, w);
|
||||
// Update the set of expected IVs
|
||||
Map<Long, Bytes> oldIvs = contactToIvs.remove(contactId);
|
||||
assert oldIvs != null;
|
||||
|
||||
@@ -6,6 +6,7 @@ import javax.crypto.Cipher;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.SecretKey;
|
||||
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
@@ -22,14 +23,14 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
||||
}
|
||||
|
||||
public ConnectionWriter createConnectionWriter(OutputStream out,
|
||||
long capacity, boolean initiator, int transportId, long connection,
|
||||
long capacity, boolean initiator, TransportId t, long connection,
|
||||
byte[] secret) {
|
||||
// Create the encrypter
|
||||
Cipher ivCipher = crypto.getIvCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
SecretKey ivKey = crypto.deriveOutgoingIvKey(secret);
|
||||
SecretKey frameKey = crypto.deriveOutgoingFrameKey(secret);
|
||||
byte[] iv = IvEncoder.encodeIv(initiator, transportId, connection);
|
||||
byte[] iv = IvEncoder.encodeIv(initiator, t, connection);
|
||||
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
||||
capacity, iv, ivCipher, frameCipher, ivKey, frameKey);
|
||||
// Create the writer
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.util.ByteUtils;
|
||||
|
||||
class IvEncoder {
|
||||
|
||||
static byte[] encodeIv(boolean initiator, int transportId,
|
||||
static byte[] encodeIv(boolean initiator, TransportId transport,
|
||||
long connection) {
|
||||
byte[] iv = new byte[IV_LENGTH];
|
||||
// Bit 31 is the initiator flag
|
||||
if(initiator) iv[3] = 1;
|
||||
// Encode the transport identifier as an unsigned 16-bit integer
|
||||
ByteUtils.writeUint16(transportId, iv, 4);
|
||||
ByteUtils.writeUint16(transport.getInt(), iv, 4);
|
||||
// Encode the connection number as an unsigned 32-bit integer
|
||||
ByteUtils.writeUint32(connection, iv, 6);
|
||||
return iv;
|
||||
|
||||
Reference in New Issue
Block a user