mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Merge branch '112-transport-properties-manager' into 'master'
Transport properties manager facade, key manager refactoring. #112 Refactoring for #112: decouple the invitation and plugin code from the database with a TransportPropertiesManager facade (which will become a BSP client), and move some key management logic from the invitation code to the KeyManager. Update the integration tests to use the new FooManager facades. See merge request !49
This commit is contained in:
@@ -2,6 +2,7 @@ package org.briarproject.invitation;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
@@ -9,14 +10,15 @@ import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
@@ -39,20 +41,24 @@ class AliceConnector extends Connector {
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AliceConnector.class.getName());
|
||||
|
||||
AliceConnector(CryptoComponent crypto, DatabaseComponent db,
|
||||
AliceConnector(CryptoComponent crypto,
|
||||
ReaderFactory readerFactory, WriterFactory writerFactory,
|
||||
StreamReaderFactory streamReaderFactory,
|
||||
StreamWriterFactory streamWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionManager connectionManager,
|
||||
Clock clock, boolean reuseConnection, ConnectorGroup group,
|
||||
DuplexPlugin plugin, LocalAuthor localAuthor,
|
||||
ContactManager contactManager, MessagingManager messagingManager,
|
||||
TransportPropertyManager transportPropertyManager, Clock clock,
|
||||
boolean reuseConnection, ConnectorGroup group, DuplexPlugin plugin,
|
||||
LocalAuthor localAuthor,
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super(crypto, db, readerFactory, writerFactory, streamReaderFactory,
|
||||
super(crypto, readerFactory, writerFactory, streamReaderFactory,
|
||||
streamWriterFactory, authorFactory, groupFactory,
|
||||
keyManager, connectionManager, clock, reuseConnection, group,
|
||||
plugin, localAuthor, localProps, random);
|
||||
keyManager, connectionManager, contactManager,
|
||||
messagingManager, transportPropertyManager, clock,
|
||||
reuseConnection, group, plugin, localAuthor, localProps,
|
||||
random);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.invitation;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
@@ -9,14 +10,15 @@ import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
@@ -39,20 +41,24 @@ class BobConnector extends Connector {
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BobConnector.class.getName());
|
||||
|
||||
BobConnector(CryptoComponent crypto, DatabaseComponent db,
|
||||
BobConnector(CryptoComponent crypto,
|
||||
ReaderFactory readerFactory, WriterFactory writerFactory,
|
||||
StreamReaderFactory streamReaderFactory,
|
||||
StreamWriterFactory streamWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionManager connectionManager,
|
||||
Clock clock, boolean reuseConnection, ConnectorGroup group,
|
||||
DuplexPlugin plugin, LocalAuthor localAuthor,
|
||||
ContactManager contactManager, MessagingManager messagingManager,
|
||||
TransportPropertyManager transportPropertyManager, Clock clock,
|
||||
boolean reuseConnection, ConnectorGroup group, DuplexPlugin plugin,
|
||||
LocalAuthor localAuthor,
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super(crypto, db, readerFactory, writerFactory, streamReaderFactory,
|
||||
super(crypto, readerFactory, writerFactory, streamReaderFactory,
|
||||
streamWriterFactory, authorFactory, groupFactory,
|
||||
keyManager, connectionManager, clock, reuseConnection, group,
|
||||
plugin, localAuthor, localProps, random);
|
||||
keyManager, connectionManager, contactManager,
|
||||
messagingManager, transportPropertyManager, clock,
|
||||
reuseConnection, group, plugin, localAuthor, localProps,
|
||||
random);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
import org.briarproject.api.crypto.KeyParser;
|
||||
@@ -15,28 +16,25 @@ import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Logger;
|
||||
@@ -50,7 +48,6 @@ import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENG
|
||||
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
|
||||
import static org.briarproject.api.invitation.InvitationConstants.CONNECTION_TIMEOUT;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
|
||||
// FIXME: This class has way too many dependencies
|
||||
abstract class Connector extends Thread {
|
||||
@@ -59,7 +56,6 @@ abstract class Connector extends Thread {
|
||||
Logger.getLogger(Connector.class.getName());
|
||||
|
||||
protected final CryptoComponent crypto;
|
||||
protected final DatabaseComponent db;
|
||||
protected final ReaderFactory readerFactory;
|
||||
protected final WriterFactory writerFactory;
|
||||
protected final StreamReaderFactory streamReaderFactory;
|
||||
@@ -68,6 +64,9 @@ abstract class Connector extends Thread {
|
||||
protected final GroupFactory groupFactory;
|
||||
protected final KeyManager keyManager;
|
||||
protected final ConnectionManager connectionManager;
|
||||
protected final ContactManager contactManager;
|
||||
protected final MessagingManager messagingManager;
|
||||
protected final TransportPropertyManager transportPropertyManager;
|
||||
protected final Clock clock;
|
||||
protected final boolean reuseConnection;
|
||||
protected final ConnectorGroup group;
|
||||
@@ -83,19 +82,20 @@ abstract class Connector extends Thread {
|
||||
|
||||
private volatile ContactId contactId = null;
|
||||
|
||||
Connector(CryptoComponent crypto, DatabaseComponent db,
|
||||
Connector(CryptoComponent crypto,
|
||||
ReaderFactory readerFactory, WriterFactory writerFactory,
|
||||
StreamReaderFactory streamReaderFactory,
|
||||
StreamWriterFactory streamWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionManager connectionManager,
|
||||
Clock clock, boolean reuseConnection, ConnectorGroup group,
|
||||
DuplexPlugin plugin, LocalAuthor localAuthor,
|
||||
ContactManager contactManager, MessagingManager messagingManager,
|
||||
TransportPropertyManager transportPropertyManager, Clock clock,
|
||||
boolean reuseConnection, ConnectorGroup group, DuplexPlugin plugin,
|
||||
LocalAuthor localAuthor,
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super("Connector");
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
this.readerFactory = readerFactory;
|
||||
this.writerFactory = writerFactory;
|
||||
this.streamReaderFactory = streamReaderFactory;
|
||||
@@ -104,6 +104,9 @@ abstract class Connector extends Thread {
|
||||
this.groupFactory = groupFactory;
|
||||
this.keyManager = keyManager;
|
||||
this.connectionManager = connectionManager;
|
||||
this.contactManager = contactManager;
|
||||
this.messagingManager = messagingManager;
|
||||
this.transportPropertyManager = transportPropertyManager;
|
||||
this.clock = clock;
|
||||
this.reuseConnection = reuseConnection;
|
||||
this.group = group;
|
||||
@@ -274,31 +277,15 @@ abstract class Connector extends Thread {
|
||||
Map<TransportId, TransportProperties> remoteProps, SecretKey master,
|
||||
long timestamp, boolean alice) throws DbException {
|
||||
// Add the contact to the database
|
||||
contactId = db.addContact(remoteAuthor, localAuthor.getId());
|
||||
// Create and store the inbox group
|
||||
byte[] salt = crypto.deriveGroupSalt(master);
|
||||
Group inbox = groupFactory.createGroup("Inbox", salt);
|
||||
db.addGroup(inbox);
|
||||
db.setInboxGroup(contactId, inbox);
|
||||
contactId = contactManager.addContact(remoteAuthor,
|
||||
localAuthor.getId());
|
||||
// Create a private messaging conversation
|
||||
messagingManager.addContact(contactId, master);
|
||||
// Store the remote transport properties
|
||||
db.setRemoteProperties(contactId, remoteProps);
|
||||
transportPropertyManager.setRemoteProperties(contactId, remoteProps);
|
||||
// Derive transport keys for each transport shared with the contact
|
||||
Map<TransportId, Integer> latencies = db.getTransportLatencies();
|
||||
List<TransportKeys> keys = new ArrayList<TransportKeys>();
|
||||
for (TransportId t : localProps.keySet()) {
|
||||
if (remoteProps.containsKey(t) && latencies.containsKey(t)) {
|
||||
// Work out what rotation period the timestamp belongs to
|
||||
long latency = latencies.get(t);
|
||||
long rotationPeriodLength = latency + MAX_CLOCK_DIFFERENCE;
|
||||
long rotationPeriod = timestamp / rotationPeriodLength;
|
||||
// Derive the transport keys
|
||||
TransportKeys k = crypto.deriveTransportKeys(t, master,
|
||||
rotationPeriod, alice);
|
||||
db.addTransportKeys(contactId, k);
|
||||
keys.add(k);
|
||||
}
|
||||
}
|
||||
keyManager.contactAdded(contactId, keys);
|
||||
keyManager.addContact(contactId, remoteProps.keySet(), master,
|
||||
timestamp, alice);
|
||||
}
|
||||
|
||||
protected void tryToClose(DuplexTransportConnection conn,
|
||||
|
||||
@@ -2,22 +2,25 @@ package org.briarproject.invitation;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.invitation.InvitationListener;
|
||||
import org.briarproject.api.invitation.InvitationState;
|
||||
import org.briarproject.api.invitation.InvitationTask;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.PluginManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
@@ -45,7 +48,6 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
Logger.getLogger(ConnectorGroup.class.getName());
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final DatabaseComponent db;
|
||||
private final ReaderFactory readerFactory;
|
||||
private final WriterFactory writerFactory;
|
||||
private final StreamReaderFactory streamReaderFactory;
|
||||
@@ -54,6 +56,10 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
private final GroupFactory groupFactory;
|
||||
private final KeyManager keyManager;
|
||||
private final ConnectionManager connectionManager;
|
||||
private final IdentityManager identityManager;
|
||||
private final ContactManager contactManager;
|
||||
private final MessagingManager messagingManager;
|
||||
private final TransportPropertyManager transportPropertyManager;
|
||||
private final Clock clock;
|
||||
private final PluginManager pluginManager;
|
||||
private final AuthorId localAuthorId;
|
||||
@@ -71,18 +77,20 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
private boolean localMatched = false, remoteMatched = false;
|
||||
private String remoteName = null;
|
||||
|
||||
ConnectorGroup(CryptoComponent crypto, DatabaseComponent db,
|
||||
ConnectorGroup(CryptoComponent crypto,
|
||||
ReaderFactory readerFactory, WriterFactory writerFactory,
|
||||
StreamReaderFactory streamReaderFactory,
|
||||
StreamWriterFactory streamWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionManager connectionManager,
|
||||
Clock clock, PluginManager pluginManager, AuthorId localAuthorId,
|
||||
IdentityManager identityManager, ContactManager contactManager,
|
||||
MessagingManager messagingManager,
|
||||
TransportPropertyManager transportPropertyManager, Clock clock,
|
||||
PluginManager pluginManager, AuthorId localAuthorId,
|
||||
int localInvitationCode, int remoteInvitationCode,
|
||||
boolean reuseConnection) {
|
||||
super("ConnectorGroup");
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
this.readerFactory = readerFactory;
|
||||
this.writerFactory = writerFactory;
|
||||
this.streamReaderFactory = streamReaderFactory;
|
||||
@@ -91,6 +99,10 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
this.groupFactory = groupFactory;
|
||||
this.keyManager = keyManager;
|
||||
this.connectionManager = connectionManager;
|
||||
this.identityManager = identityManager;
|
||||
this.contactManager = contactManager;
|
||||
this.messagingManager = messagingManager;
|
||||
this.transportPropertyManager = transportPropertyManager;
|
||||
this.clock = clock;
|
||||
this.pluginManager = pluginManager;
|
||||
this.localAuthorId = localAuthorId;
|
||||
@@ -130,8 +142,8 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
Map<TransportId, TransportProperties> localProps;
|
||||
// Load the local pseudonym and transport properties
|
||||
try {
|
||||
localAuthor = db.getLocalAuthor(localAuthorId);
|
||||
localProps = db.getLocalProperties();
|
||||
localAuthor = identityManager.getLocalAuthor(localAuthorId);
|
||||
localProps = transportPropertyManager.getLocalProperties();
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
lock.lock();
|
||||
@@ -185,9 +197,10 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
Map<TransportId, TransportProperties> localProps) {
|
||||
PseudoRandom random = crypto.getPseudoRandom(localInvitationCode,
|
||||
remoteInvitationCode);
|
||||
return new AliceConnector(crypto, db, readerFactory, writerFactory,
|
||||
return new AliceConnector(crypto, readerFactory, writerFactory,
|
||||
streamReaderFactory, streamWriterFactory, authorFactory,
|
||||
groupFactory, keyManager, connectionManager, clock,
|
||||
groupFactory, keyManager, connectionManager, contactManager,
|
||||
messagingManager, transportPropertyManager, clock,
|
||||
reuseConnection, this, plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
@@ -196,9 +209,10 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
Map<TransportId, TransportProperties> localProps) {
|
||||
PseudoRandom random = crypto.getPseudoRandom(remoteInvitationCode,
|
||||
localInvitationCode);
|
||||
return new BobConnector(crypto, db, readerFactory, writerFactory,
|
||||
return new BobConnector(crypto, readerFactory, writerFactory,
|
||||
streamReaderFactory, streamWriterFactory, authorFactory,
|
||||
groupFactory, keyManager, connectionManager, clock,
|
||||
groupFactory, keyManager, connectionManager, contactManager,
|
||||
messagingManager, transportPropertyManager, clock,
|
||||
reuseConnection, this, plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
package org.briarproject.invitation;
|
||||
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.invitation.InvitationTask;
|
||||
import org.briarproject.api.invitation.InvitationTaskFactory;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.PluginManager;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
@@ -21,7 +24,6 @@ import javax.inject.Inject;
|
||||
class InvitationTaskFactoryImpl implements InvitationTaskFactory {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final DatabaseComponent db;
|
||||
private final ReaderFactory readerFactory;
|
||||
private final WriterFactory writerFactory;
|
||||
private final StreamReaderFactory streamReaderFactory;
|
||||
@@ -30,19 +32,25 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory {
|
||||
private final GroupFactory groupFactory;
|
||||
private final KeyManager keyManager;
|
||||
private final ConnectionManager connectionManager;
|
||||
private final IdentityManager identityManager;
|
||||
private final ContactManager contactManager;
|
||||
private final MessagingManager messagingManager;
|
||||
private final TransportPropertyManager transportPropertyManager;
|
||||
private final Clock clock;
|
||||
private final PluginManager pluginManager;
|
||||
|
||||
@Inject
|
||||
InvitationTaskFactoryImpl(CryptoComponent crypto, DatabaseComponent db,
|
||||
InvitationTaskFactoryImpl(CryptoComponent crypto,
|
||||
ReaderFactory readerFactory, WriterFactory writerFactory,
|
||||
StreamReaderFactory streamReaderFactory,
|
||||
StreamWriterFactory streamWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionManager connectionManager,
|
||||
IdentityManager identityManager, ContactManager contactManager,
|
||||
MessagingManager messagingManager,
|
||||
TransportPropertyManager transportPropertyManager,
|
||||
Clock clock, PluginManager pluginManager) {
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
this.readerFactory = readerFactory;
|
||||
this.writerFactory = writerFactory;
|
||||
this.streamReaderFactory = streamReaderFactory;
|
||||
@@ -51,16 +59,21 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory {
|
||||
this.groupFactory = groupFactory;
|
||||
this.keyManager = keyManager;
|
||||
this.connectionManager = connectionManager;
|
||||
this.identityManager = identityManager;
|
||||
this.contactManager = contactManager;
|
||||
this.messagingManager = messagingManager;
|
||||
this.transportPropertyManager = transportPropertyManager;
|
||||
this.clock = clock;
|
||||
this.pluginManager = pluginManager;
|
||||
}
|
||||
|
||||
public InvitationTask createTask(AuthorId localAuthorId, int localCode,
|
||||
int remoteCode, boolean reuseConnection) {
|
||||
return new ConnectorGroup(crypto, db, readerFactory, writerFactory,
|
||||
return new ConnectorGroup(crypto, readerFactory, writerFactory,
|
||||
streamReaderFactory, streamWriterFactory, authorFactory,
|
||||
groupFactory, keyManager, connectionManager, clock,
|
||||
pluginManager, localAuthorId, localCode, remoteCode,
|
||||
groupFactory, keyManager, connectionManager, identityManager,
|
||||
contactManager, messagingManager, transportPropertyManager,
|
||||
clock, pluginManager, localAuthorId, localCode, remoteCode,
|
||||
reuseConnection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,15 @@ package org.briarproject.messaging;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.messaging.PrivateConversation;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.sync.MessageHeader;
|
||||
@@ -22,10 +26,23 @@ import java.util.List;
|
||||
class MessagingManagerImpl implements MessagingManager {
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
private final GroupFactory groupFactory;
|
||||
|
||||
@Inject
|
||||
MessagingManagerImpl(DatabaseComponent db) {
|
||||
MessagingManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||
GroupFactory groupFactory) {
|
||||
this.db = db;
|
||||
this.crypto = crypto;
|
||||
this.groupFactory = groupFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addContact(ContactId c, SecretKey master) throws DbException {
|
||||
byte[] salt = crypto.deriveGroupSalt(master);
|
||||
Group inbox = groupFactory.createGroup("Inbox", salt);
|
||||
db.addGroup(inbox);
|
||||
db.setInboxGroup(c, inbox);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.plugins;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||
@@ -130,15 +129,11 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
StreamContext ctx;
|
||||
try {
|
||||
byte[] tag = readTag(transportId, reader);
|
||||
ctx = keyManager.recogniseTag(transportId, tag);
|
||||
ctx = keyManager.getStreamContext(transportId, tag);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, false);
|
||||
return;
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, false);
|
||||
return;
|
||||
}
|
||||
if (ctx == null) {
|
||||
LOG.info("Unrecognised tag");
|
||||
@@ -234,15 +229,11 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
StreamContext ctx;
|
||||
try {
|
||||
byte[] tag = readTag(transportId, reader);
|
||||
ctx = keyManager.recogniseTag(transportId, tag);
|
||||
ctx = keyManager.getStreamContext(transportId, tag);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, false);
|
||||
return;
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, false);
|
||||
return;
|
||||
}
|
||||
if (ctx == null) {
|
||||
LOG.info("Unrecognised tag");
|
||||
@@ -363,15 +354,11 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
StreamContext ctx;
|
||||
try {
|
||||
byte[] tag = readTag(transportId, reader);
|
||||
ctx = keyManager.recogniseTag(transportId, tag);
|
||||
ctx = keyManager.getStreamContext(transportId, tag);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, true);
|
||||
return;
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, true);
|
||||
return;
|
||||
}
|
||||
// Unrecognised tags are suspicious in this case
|
||||
if (ctx == null) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.briarproject.api.plugins.simplex.SimplexPlugin;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginConfig;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
|
||||
@@ -58,6 +59,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
private final DatabaseComponent db;
|
||||
private final Poller poller;
|
||||
private final ConnectionManager connectionManager;
|
||||
private final TransportPropertyManager transportPropertyManager;
|
||||
private final UiCallback uiCallback;
|
||||
private final Map<TransportId, Plugin> plugins;
|
||||
private final List<SimplexPlugin> simplexPlugins;
|
||||
@@ -68,7 +70,9 @@ class PluginManagerImpl implements PluginManager {
|
||||
SimplexPluginConfig simplexPluginConfig,
|
||||
DuplexPluginConfig duplexPluginConfig, Clock clock,
|
||||
DatabaseComponent db, Poller poller,
|
||||
ConnectionManager connectionManager, UiCallback uiCallback) {
|
||||
ConnectionManager connectionManager,
|
||||
TransportPropertyManager transportPropertyManager,
|
||||
UiCallback uiCallback) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.eventBus = eventBus;
|
||||
this.simplexPluginConfig = simplexPluginConfig;
|
||||
@@ -77,6 +81,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
this.db = db;
|
||||
this.poller = poller;
|
||||
this.connectionManager = connectionManager;
|
||||
this.transportPropertyManager = transportPropertyManager;
|
||||
this.uiCallback = uiCallback;
|
||||
plugins = new ConcurrentHashMap<TransportId, Plugin>();
|
||||
simplexPlugins = new CopyOnWriteArrayList<SimplexPlugin>();
|
||||
@@ -320,7 +325,8 @@ class PluginManagerImpl implements PluginManager {
|
||||
|
||||
public TransportProperties getLocalProperties() {
|
||||
try {
|
||||
TransportProperties p = db.getLocalProperties(id);
|
||||
TransportProperties p =
|
||||
transportPropertyManager.getLocalProperties(id);
|
||||
return p == null ? new TransportProperties() : p;
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
@@ -330,7 +336,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
|
||||
public Map<ContactId, TransportProperties> getRemoteProperties() {
|
||||
try {
|
||||
return db.getRemoteProperties(id);
|
||||
return transportPropertyManager.getRemoteProperties(id);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return Collections.emptyMap();
|
||||
@@ -347,7 +353,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
|
||||
public void mergeLocalProperties(TransportProperties p) {
|
||||
try {
|
||||
db.mergeLocalProperties(id, p);
|
||||
transportPropertyManager.mergeLocalProperties(id, p);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
|
||||
14
briar-core/src/org/briarproject/property/PropertyModule.java
Normal file
14
briar-core/src/org/briarproject/property/PropertyModule.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package org.briarproject.property;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
|
||||
public class PropertyModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(TransportPropertyManager.class).to(
|
||||
TransportPropertyManagerImpl.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.briarproject.property;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
// Temporary facade during sync protocol refactoring
|
||||
class TransportPropertyManagerImpl implements TransportPropertyManager {
|
||||
|
||||
private final DatabaseComponent db;
|
||||
|
||||
@Inject
|
||||
TransportPropertyManagerImpl(DatabaseComponent db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<TransportId, TransportProperties> getLocalProperties()
|
||||
throws DbException {
|
||||
return db.getLocalProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProperties getLocalProperties(TransportId t)
|
||||
throws DbException {
|
||||
return db.getLocalProperties(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<ContactId, TransportProperties> getRemoteProperties(
|
||||
TransportId t) throws DbException {
|
||||
return db.getRemoteProperties(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mergeLocalProperties(TransportId t, TransportProperties p)
|
||||
throws DbException {
|
||||
db.mergeLocalProperties(t, p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRemoteProperties(ContactId c,
|
||||
Map<TransportId, TransportProperties> p) throws DbException {
|
||||
db.setRemoteProperties(c, p);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.transport;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
@@ -16,7 +17,6 @@ import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.Timer;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
@@ -73,10 +73,11 @@ class KeyManagerImpl implements KeyManager, EventListener {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void contactAdded(ContactId c, Collection<TransportKeys> keys) {
|
||||
for (TransportKeys k : keys) {
|
||||
TransportKeyManager m = managers.get(k.getTransportId());
|
||||
if (m != null) m.addContact(c, k);
|
||||
public void addContact(ContactId c, Collection<TransportId> transports,
|
||||
SecretKey master, long timestamp, boolean alice) {
|
||||
for (TransportId t : transports) {
|
||||
TransportKeyManager m = managers.get(t);
|
||||
if (m != null) m.addContact(c, master, timestamp, alice);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,8 +86,7 @@ class KeyManagerImpl implements KeyManager, EventListener {
|
||||
return m == null ? null : m.getStreamContext(c);
|
||||
}
|
||||
|
||||
public StreamContext recogniseTag(TransportId t, byte[] tag)
|
||||
throws DbException {
|
||||
public StreamContext getStreamContext(TransportId t, byte[] tag) {
|
||||
TransportKeyManager m = managers.get(t);
|
||||
return m == null ? null : m.recogniseTag(tag);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.api.Bytes;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.system.Clock;
|
||||
@@ -98,9 +99,9 @@ class TransportKeyManager extends TimerTask {
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// Schedule a periodic task to rotate the keys
|
||||
// Schedule the next key rotation
|
||||
long delay = rotationPeriodLength - now % rotationPeriodLength;
|
||||
timer.scheduleAtFixedRate(this, delay, rotationPeriodLength);
|
||||
timer.schedule(this, delay);
|
||||
}
|
||||
|
||||
// Locking: lock
|
||||
@@ -136,16 +137,40 @@ class TransportKeyManager extends TimerTask {
|
||||
});
|
||||
}
|
||||
|
||||
void addContact(ContactId c, TransportKeys k) {
|
||||
void addContact(ContactId c, SecretKey master, long timestamp,
|
||||
boolean alice) {
|
||||
// Work out what rotation period the timestamp belongs to
|
||||
long rotationPeriod = timestamp / rotationPeriodLength;
|
||||
// Derive the transport keys
|
||||
TransportKeys k = crypto.deriveTransportKeys(transportId, master,
|
||||
rotationPeriod, alice);
|
||||
// Rotate the keys to the current rotation period if necessary
|
||||
rotationPeriod = clock.currentTimeMillis() / rotationPeriodLength;
|
||||
k = crypto.rotateTransportKeys(k, rotationPeriod);
|
||||
lock.lock();
|
||||
try {
|
||||
// Initialise mutable state for the contact
|
||||
addKeys(c, new MutableTransportKeys(k));
|
||||
// Write the keys back to the DB
|
||||
saveTransportKeys(c, k);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveTransportKeys(final ContactId c, final TransportKeys k) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
db.addTransportKeys(c, k);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void removeContact(ContactId c) {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -308,6 +333,10 @@ class TransportKeyManager extends TimerTask {
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// Schedule the next key rotation
|
||||
long now = clock.currentTimeMillis();
|
||||
long delay = rotationPeriodLength - now % rotationPeriodLength;
|
||||
timer.schedule(this, delay);
|
||||
}
|
||||
|
||||
private static class TagContext {
|
||||
|
||||
Reference in New Issue
Block a user