mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Encode transport properties more compactly in QR codes.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package org.briarproject.keyagreement;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.api.keyagreement.Payload;
|
||||
import org.briarproject.api.keyagreement.TransportDescriptor;
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.PluginManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
@@ -14,7 +16,10 @@ import org.briarproject.api.system.Clock;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletionService;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -68,14 +73,13 @@ class KeyAgreementConnector {
|
||||
byte[] commitment = crypto.deriveKeyCommitment(
|
||||
localKeyPair.getPublic().getEncoded());
|
||||
// Start all listeners and collect their descriptors
|
||||
List<TransportDescriptor> descriptors =
|
||||
new ArrayList<TransportDescriptor>();
|
||||
Map<TransportId, BdfList> descriptors =
|
||||
new HashMap<TransportId, BdfList>();
|
||||
for (DuplexPlugin plugin : pluginManager.getKeyAgreementPlugins()) {
|
||||
KeyAgreementListener l = plugin.createKeyAgreementListener(
|
||||
commitment);
|
||||
KeyAgreementListener l =
|
||||
plugin.createKeyAgreementListener(commitment);
|
||||
if (l != null) {
|
||||
TransportDescriptor d = l.getDescriptor();
|
||||
descriptors.add(d);
|
||||
descriptors.put(plugin.getId(), l.getDescriptor());
|
||||
pending.add(connect.submit(new ReadableTask(l.listen())));
|
||||
listeners.add(l);
|
||||
}
|
||||
@@ -100,13 +104,16 @@ class KeyAgreementConnector {
|
||||
|
||||
// Start connecting over supported transports
|
||||
LOG.info("Starting outgoing BQP connections");
|
||||
for (TransportDescriptor d : remotePayload.getTransportDescriptors()) {
|
||||
DuplexPlugin plugin = (DuplexPlugin) pluginManager.getPlugin(
|
||||
d.getIdentifier());
|
||||
if (plugin != null)
|
||||
Map<TransportId, BdfList> descriptors =
|
||||
remotePayload.getTransportDescriptors();
|
||||
for (Entry<TransportId, BdfList> e : descriptors.entrySet()) {
|
||||
Plugin p = pluginManager.getPlugin(e.getKey());
|
||||
if (p instanceof DuplexPlugin) {
|
||||
DuplexPlugin plugin = (DuplexPlugin) p;
|
||||
pending.add(connect.submit(new ReadableTask(
|
||||
new ConnectorTask(plugin, remotePayload.getCommitment(),
|
||||
d, end))));
|
||||
e.getValue(), end))));
|
||||
}
|
||||
}
|
||||
|
||||
// Get chosen connection
|
||||
@@ -170,12 +177,12 @@ class KeyAgreementConnector {
|
||||
private class ConnectorTask implements Callable<KeyAgreementConnection> {
|
||||
|
||||
private final byte[] commitment;
|
||||
private final TransportDescriptor descriptor;
|
||||
private final BdfList descriptor;
|
||||
private final long end;
|
||||
private final DuplexPlugin plugin;
|
||||
|
||||
private ConnectorTask(DuplexPlugin plugin, byte[] commitment,
|
||||
TransportDescriptor descriptor, long end) {
|
||||
BdfList descriptor, long end) {
|
||||
this.plugin = plugin;
|
||||
this.commitment = commitment;
|
||||
this.descriptor = descriptor;
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
package org.briarproject.keyagreement;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.data.BdfWriter;
|
||||
import org.briarproject.api.data.BdfWriterFactory;
|
||||
import org.briarproject.api.keyagreement.Payload;
|
||||
import org.briarproject.api.keyagreement.PayloadEncoder;
|
||||
import org.briarproject.api.keyagreement.TransportDescriptor;
|
||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.PROTOCOL_VERSION;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class PayloadEncoderImpl implements PayloadEncoder {
|
||||
|
||||
private final BdfWriterFactory bdfWriterFactory;
|
||||
|
||||
@Inject
|
||||
public PayloadEncoderImpl(BdfWriterFactory bdfWriterFactory) {
|
||||
PayloadEncoderImpl(BdfWriterFactory bdfWriterFactory) {
|
||||
this.bdfWriterFactory = bdfWriterFactory;
|
||||
}
|
||||
|
||||
@@ -30,18 +36,13 @@ class PayloadEncoderImpl implements PayloadEncoder {
|
||||
w.writeListStart(); // Payload start
|
||||
w.writeLong(PROTOCOL_VERSION);
|
||||
w.writeRaw(p.getCommitment());
|
||||
w.writeListStart(); // Descriptors start
|
||||
for (TransportDescriptor d : p.getTransportDescriptors()) {
|
||||
w.writeListStart();
|
||||
w.writeString(d.getIdentifier().getString());
|
||||
w.writeDictionary(d.getProperties());
|
||||
w.writeListEnd();
|
||||
}
|
||||
w.writeListEnd(); // Descriptors end
|
||||
Map<TransportId, BdfList> descriptors = p.getTransportDescriptors();
|
||||
for (BdfList descriptor : descriptors.values())
|
||||
w.writeList(descriptor);
|
||||
w.writeListEnd(); // Payload end
|
||||
} catch (IOException e) {
|
||||
// Shouldn't happen with ByteArrayOutputStream
|
||||
throw new RuntimeException(e);
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -2,30 +2,34 @@ package org.briarproject.keyagreement;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.data.BdfReader;
|
||||
import org.briarproject.api.data.BdfReaderFactory;
|
||||
import org.briarproject.api.keyagreement.Payload;
|
||||
import org.briarproject.api.keyagreement.PayloadParser;
|
||||
import org.briarproject.api.keyagreement.TransportDescriptor;
|
||||
import org.briarproject.api.properties.TransportProperties;
|
||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.PROTOCOL_VERSION;
|
||||
import static org.briarproject.api.properties.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
|
||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class PayloadParserImpl implements PayloadParser {
|
||||
|
||||
private final BdfReaderFactory bdfReaderFactory;
|
||||
|
||||
@Inject
|
||||
public PayloadParserImpl(BdfReaderFactory bdfReaderFactory) {
|
||||
PayloadParserImpl(BdfReaderFactory bdfReaderFactory) {
|
||||
this.bdfReaderFactory = bdfReaderFactory;
|
||||
}
|
||||
|
||||
@@ -33,36 +37,28 @@ class PayloadParserImpl implements PayloadParser {
|
||||
public Payload parse(byte[] raw) throws IOException {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(raw);
|
||||
BdfReader r = bdfReaderFactory.createReader(in);
|
||||
r.readListStart(); // Payload start
|
||||
int proto = (int) r.readLong();
|
||||
if (proto != PROTOCOL_VERSION)
|
||||
throw new FormatException();
|
||||
byte[] commitment = r.readRaw(COMMIT_LENGTH);
|
||||
if (commitment.length != COMMIT_LENGTH)
|
||||
throw new FormatException();
|
||||
List<TransportDescriptor> descriptors = new ArrayList<TransportDescriptor>();
|
||||
r.readListStart(); // Descriptors start
|
||||
while (r.hasList()) {
|
||||
r.readListStart();
|
||||
while (!r.hasListEnd()) {
|
||||
TransportId id =
|
||||
new TransportId(r.readString(MAX_PROPERTY_LENGTH));
|
||||
TransportProperties p = new TransportProperties();
|
||||
r.readDictionaryStart();
|
||||
while (!r.hasDictionaryEnd()) {
|
||||
String key = r.readString(MAX_PROPERTY_LENGTH);
|
||||
String value = r.readString(MAX_PROPERTY_LENGTH);
|
||||
p.put(key, value);
|
||||
}
|
||||
r.readDictionaryEnd();
|
||||
descriptors.add(new TransportDescriptor(id, p));
|
||||
// The payload is a BDF list with two or more elements
|
||||
BdfList payload = r.readList();
|
||||
if (payload.size() < 2) throw new FormatException();
|
||||
if (!r.eof()) throw new FormatException();
|
||||
// First element: the protocol version
|
||||
long protocolVersion = payload.getLong(0);
|
||||
if (protocolVersion != PROTOCOL_VERSION) throw new FormatException();
|
||||
// Second element: the public key commitment
|
||||
byte[] commitment = payload.getRaw(1);
|
||||
if (commitment.length != COMMIT_LENGTH) throw new FormatException();
|
||||
// Remaining elements: transport descriptors
|
||||
Map<TransportId, BdfList> recognised =
|
||||
new HashMap<TransportId, BdfList>();
|
||||
for (int i = 2; i < payload.size(); i++) {
|
||||
BdfList descriptor = payload.getList(i);
|
||||
long transportId = descriptor.getLong(0);
|
||||
if (transportId == TRANSPORT_ID_BLUETOOTH) {
|
||||
recognised.put(new TransportId("bt"), descriptor);
|
||||
} else if (transportId == TRANSPORT_ID_LAN) {
|
||||
recognised.put(new TransportId("lan"), descriptor);
|
||||
}
|
||||
r.readListEnd();
|
||||
}
|
||||
r.readListEnd(); // Descriptors end
|
||||
r.readListEnd(); // Payload end
|
||||
if (!r.eof())
|
||||
throw new FormatException();
|
||||
return new Payload(commitment, descriptors);
|
||||
return new Payload(commitment, recognised);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user