mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Create BQP API
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.briarproject.api.event;
|
||||
|
||||
/** An event that is broadcast when a BQP protocol completes. */
|
||||
public class KeyAgreementStartedEvent extends Event {
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
34
briar-api/src/org/briarproject/api/keyagreement/Payload.java
Normal file
34
briar-api/src/org/briarproject/api/keyagreement/Payload.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.briarproject.api.keyagreement;
|
||||
|
||||
public interface PayloadEncoder {
|
||||
|
||||
byte[] encode(Payload p);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.briarproject.api.keyagreement;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface PayloadParser {
|
||||
|
||||
Payload parse(byte[] raw) throws IOException;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user