Create BQP API

This commit is contained in:
str4d
2016-02-02 05:52:30 +00:00
parent d2d8d9d46e
commit ce7c189923
15 changed files with 223 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package org.briarproject.api.event;
/** An event that is broadcast when a BQP protocol aborts. */
public class KeyAgreementAbortedEvent extends Event {
private final boolean remoteAborted;
public KeyAgreementAbortedEvent(boolean remoteAborted) {
this.remoteAborted = remoteAborted;
}
public boolean didRemoteAbort() {
return remoteAborted;
}
}

View File

@@ -0,0 +1,6 @@
package org.briarproject.api.event;
/** An event that is broadcast when a BQP connection cannot be created. */
public class KeyAgreementFailedEvent extends Event {
}

View File

@@ -0,0 +1,17 @@
package org.briarproject.api.event;
import org.briarproject.api.keyagreement.KeyAgreementResult;
/** An event that is broadcast when a BQP protocol completes. */
public class KeyAgreementFinishedEvent extends Event {
private final KeyAgreementResult result;
public KeyAgreementFinishedEvent(KeyAgreementResult result) {
this.result = result;
}
public KeyAgreementResult getResult() {
return result;
}
}

View File

@@ -0,0 +1,17 @@
package org.briarproject.api.event;
import org.briarproject.api.keyagreement.Payload;
/** An event that is broadcast when a BQP task is listening. */
public class KeyAgreementListeningEvent extends Event {
private final Payload localPayload;
public KeyAgreementListeningEvent(Payload localPayload) {
this.localPayload = localPayload;
}
public Payload getLocalPayload() {
return localPayload;
}
}

View File

@@ -0,0 +1,6 @@
package org.briarproject.api.event;
/** An event that is broadcast when a BQP protocol completes. */
public class KeyAgreementStartedEvent extends Event {
}

View File

@@ -0,0 +1,9 @@
package org.briarproject.api.event;
/**
* An event that is broadcast when a BQP protocol is waiting on the remote
* peer to start.
*/
public class KeyAgreementWaitingEvent extends Event {
}

View File

@@ -3,6 +3,17 @@ package org.briarproject.api.keyagreement;
public interface KeyAgreementConstants {
/** The current version of the BQP protocol. */
byte PROTOCOL_VERSION = 1;
/** The length of the record header in bytes. */
int RECORD_HEADER_LENGTH = 4;
/** The offset of the payload length in the record header, in bytes. */
int RECORD_HEADER_PAYLOAD_LENGTH_OFFSET = 2;
/** The length of the BQP key commitment in bytes. */
int COMMIT_LENGTH = 16;
long CONNECTION_TIMEOUT = 20 * 1000; // Milliseconds
}

View File

@@ -0,0 +1,38 @@
package org.briarproject.api.keyagreement;
import org.briarproject.api.TransportId;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
public class KeyAgreementResult {
private final SecretKey masterKey;
private final DuplexTransportConnection connection;
private final TransportId transportId;
private final boolean alice;
public KeyAgreementResult(SecretKey masterKey,
DuplexTransportConnection connection, TransportId transportId,
boolean alice) {
this.masterKey = masterKey;
this.connection = connection;
this.transportId = transportId;
this.alice = alice;
}
public SecretKey getMasterKey() {
return masterKey;
}
public DuplexTransportConnection getConnection() {
return connection;
}
public TransportId getTransportId() {
return transportId;
}
public boolean wasAlice() {
return alice;
}
}

View File

@@ -0,0 +1,21 @@
package org.briarproject.api.keyagreement;
/** A task for conducting a key agreement with a remote peer. */
public interface KeyAgreementTask {
/**
* Start listening for short-range BQP connections, if we are not already.
* <p/>
* Will trigger a KeyAgreementListeningEvent containing the local Payload,
* even if we are already listening.
*/
void listen();
/**
* Stop listening for short-range BQP connections.
*/
void stopListening();
/** Asynchronously start the connection process. */
void connectAndRunProtocol(Payload remotePayload);
}

View File

@@ -0,0 +1,8 @@
package org.briarproject.api.keyagreement;
/** Manages tasks for conducting key agreements with remote peers. */
public interface KeyAgreementTaskFactory {
/** Gets the current key agreement task. */
KeyAgreementTask getTask();
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.keyagreement;
import org.briarproject.api.UniqueId;
/**
* Type-safe wrapper for a byte array that uniquely identifies a BQP task.
*/
public class KeyAgreementTaskId extends UniqueId {
public KeyAgreementTaskId(byte[] id) {
super(id);
}
@Override
public boolean equals(Object o) {
return o instanceof KeyAgreementTaskId && super.equals(o);
}
}

View File

@@ -0,0 +1,34 @@
package org.briarproject.api.keyagreement;
import org.briarproject.api.Bytes;
import java.util.List;
/**
* A BQP payload.
*/
public class Payload implements Comparable<Payload> {
private final Bytes commitment;
private final List<TransportDescriptor> descriptors;
public Payload(byte[] commitment, List<TransportDescriptor> descriptors) {
this.commitment = new Bytes(commitment);
this.descriptors = descriptors;
}
/** Returns the commitment contained in this payload. */
public byte[] getCommitment() {
return commitment.getBytes();
}
/** Returns the transport descriptors contained in this payload. */
public List<TransportDescriptor> getTransportDescriptors() {
return descriptors;
}
@Override
public int compareTo(Payload p) {
return commitment.compareTo(p.commitment);
}
}

View File

@@ -0,0 +1,6 @@
package org.briarproject.api.keyagreement;
public interface PayloadEncoder {
byte[] encode(Payload p);
}

View File

@@ -0,0 +1,8 @@
package org.briarproject.api.keyagreement;
import java.io.IOException;
public interface PayloadParser {
Payload parse(byte[] raw) throws IOException;
}

View File

@@ -0,0 +1,9 @@
package org.briarproject.api.keyagreement;
/** Record types for BQP. */
public interface RecordTypes {
byte KEY = 0;
byte CONFIRM = 1;
byte ABORT = 2;
}