mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Updated java.library.path.
This commit is contained in:
3
bramble-api/.gitignore
vendored
Normal file
3
bramble-api/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
bin
|
||||
build
|
||||
.settings
|
||||
22
bramble-api/build.gradle
Normal file
22
bramble-api/build.gradle
Normal file
@@ -0,0 +1,22 @@
|
||||
apply plugin: 'java'
|
||||
sourceCompatibility = 1.6
|
||||
targetCompatibility = 1.6
|
||||
|
||||
apply plugin: 'witness'
|
||||
|
||||
dependencies {
|
||||
compile "com.google.dagger:dagger:2.0.2"
|
||||
compile 'com.google.dagger:dagger-compiler:2.0.2'
|
||||
compile 'com.google.code.findbugs:jsr305:3.0.1'
|
||||
}
|
||||
|
||||
dependencyVerification {
|
||||
verify = [
|
||||
'com.google.dagger:dagger:84c0282ed8be73a29e0475d639da030b55dee72369e58dd35ae7d4fe6243dcf9',
|
||||
'com.google.dagger:dagger-compiler:b74bc9de063dd4c6400b232231f2ef5056145b8fbecbf5382012007dd1c071b3',
|
||||
'javax.inject:javax.inject:91c77044a50c481636c32d916fd89c9118a72195390452c81065080f957de7ff',
|
||||
'com.google.dagger:dagger-producers:99ec15e8a0507ba569e7655bc1165ee5e5ca5aa914b3c8f7e2c2458f724edd6b',
|
||||
'com.google.guava:guava:d664fbfc03d2e5ce9cab2a44fb01f1d0bf9dfebeccc1a473b1f9ea31f79f6f99',
|
||||
'com.google.code.findbugs:jsr305:c885ce34249682bc0236b4a7d56efcc12048e6135a5baf7a9cde8ad8cda13fcd'
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
/**
|
||||
* A wrapper around a byte array, to allow it to be stored in maps etc.
|
||||
*/
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class Bytes implements Comparable<Bytes> {
|
||||
|
||||
public static final BytesComparator COMPARATOR = new BytesComparator();
|
||||
|
||||
private final byte[] bytes;
|
||||
|
||||
private int hashCode = -1;
|
||||
|
||||
public Bytes(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// Thread-safe because if two or more threads check and update the
|
||||
// value, they'll calculate the same value
|
||||
if (hashCode == -1) hashCode = Arrays.hashCode(bytes);
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof Bytes && Arrays.equals(bytes, ((Bytes) o).bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Bytes other) {
|
||||
byte[] aBytes = bytes, bBytes = other.bytes;
|
||||
int length = Math.min(aBytes.length, bBytes.length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
int aUnsigned = aBytes[i] & 0xFF, bUnsigned = bBytes[i] & 0xFF;
|
||||
if (aUnsigned < bUnsigned) return -1;
|
||||
if (aUnsigned > bUnsigned) return 1;
|
||||
}
|
||||
return aBytes.length - bBytes.length;
|
||||
}
|
||||
|
||||
public static class BytesComparator implements Comparator<Bytes> {
|
||||
|
||||
@Override
|
||||
public int compare(Bytes a, Bytes b) {
|
||||
return a.compareTo(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An exception that indicates an unrecoverable formatting error.
|
||||
*/
|
||||
public class FormatException extends IOException {
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class StringMap extends Hashtable<String, String> {
|
||||
|
||||
protected StringMap(Map<String, String> m) {
|
||||
super(m);
|
||||
}
|
||||
|
||||
protected StringMap() {
|
||||
super();
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key, boolean defaultValue) {
|
||||
String s = get(key);
|
||||
if (s == null) return defaultValue;
|
||||
if ("true".equals(s)) return true;
|
||||
if ("false".equals(s)) return false;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void putBoolean(String key, boolean value) {
|
||||
put(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
public int getInt(String key, int defaultValue) {
|
||||
String s = get(key);
|
||||
if (s == null) return defaultValue;
|
||||
try {
|
||||
return Integer.valueOf(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public void putInt(String key, int value) {
|
||||
put(key, String.valueOf(value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public abstract class UniqueId extends Bytes {
|
||||
|
||||
/**
|
||||
* The length of a unique identifier in bytes.
|
||||
*/
|
||||
public static final int LENGTH = 32;
|
||||
|
||||
protected UniqueId(byte[] id) {
|
||||
super(id);
|
||||
if (id.length != LENGTH) throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.briarproject.bramble.api.client;
|
||||
|
||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class BdfMessageContext {
|
||||
|
||||
private final BdfDictionary dictionary;
|
||||
private final Collection<MessageId> dependencies;
|
||||
|
||||
public BdfMessageContext(BdfDictionary dictionary,
|
||||
Collection<MessageId> dependencies) {
|
||||
this.dictionary = dictionary;
|
||||
this.dependencies = dependencies;
|
||||
}
|
||||
|
||||
public BdfMessageContext(BdfDictionary dictionary) {
|
||||
this(dictionary, Collections.<MessageId>emptyList());
|
||||
}
|
||||
|
||||
public BdfDictionary getDictionary() {
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
public Collection<MessageId> getDependencies() {
|
||||
return dependencies;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.briarproject.bramble.api.client;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.data.MetadataEncoder;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.InvalidMessageException;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageContext;
|
||||
import org.briarproject.bramble.api.sync.ValidationManager.MessageValidator;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public abstract class BdfMessageValidator implements MessageValidator {
|
||||
|
||||
protected static final Logger LOG =
|
||||
Logger.getLogger(BdfMessageValidator.class.getName());
|
||||
|
||||
protected final ClientHelper clientHelper;
|
||||
protected final MetadataEncoder metadataEncoder;
|
||||
protected final Clock clock;
|
||||
|
||||
protected BdfMessageValidator(ClientHelper clientHelper,
|
||||
MetadataEncoder metadataEncoder, Clock clock) {
|
||||
this.clientHelper = clientHelper;
|
||||
this.metadataEncoder = metadataEncoder;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
protected abstract BdfMessageContext validateMessage(Message m, Group g,
|
||||
BdfList body) throws InvalidMessageException, FormatException;
|
||||
|
||||
@Override
|
||||
public MessageContext validateMessage(Message m, Group g)
|
||||
throws InvalidMessageException {
|
||||
// Reject the message if it's too far in the future
|
||||
long now = clock.currentTimeMillis();
|
||||
if (m.getTimestamp() - now > MAX_CLOCK_DIFFERENCE) {
|
||||
throw new InvalidMessageException(
|
||||
"Timestamp is too far in the future");
|
||||
}
|
||||
byte[] raw = m.getRaw();
|
||||
if (raw.length <= MESSAGE_HEADER_LENGTH) {
|
||||
throw new InvalidMessageException("Message is too short");
|
||||
}
|
||||
try {
|
||||
BdfList body = clientHelper.toList(raw, MESSAGE_HEADER_LENGTH,
|
||||
raw.length - MESSAGE_HEADER_LENGTH);
|
||||
BdfMessageContext result = validateMessage(m, g, body);
|
||||
Metadata meta = metadataEncoder.encode(result.getDictionary());
|
||||
return new MessageContext(meta, result.getDependencies());
|
||||
} catch (FormatException e) {
|
||||
throw new InvalidMessageException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package org.briarproject.bramble.api.client;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface ClientHelper {
|
||||
|
||||
void addLocalMessage(Message m, BdfDictionary metadata, boolean shared)
|
||||
throws DbException, FormatException;
|
||||
|
||||
void addLocalMessage(Transaction txn, Message m, BdfDictionary metadata,
|
||||
boolean shared) throws DbException, FormatException;
|
||||
|
||||
Message createMessage(GroupId g, long timestamp, BdfList body)
|
||||
throws FormatException;
|
||||
|
||||
Message createMessageForStoringMetadata(GroupId g);
|
||||
|
||||
@Nullable
|
||||
Message getMessage(MessageId m) throws DbException;
|
||||
|
||||
@Nullable
|
||||
Message getMessage(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
@Nullable
|
||||
BdfList getMessageAsList(MessageId m) throws DbException, FormatException;
|
||||
|
||||
@Nullable
|
||||
BdfList getMessageAsList(Transaction txn, MessageId m) throws DbException,
|
||||
FormatException;
|
||||
|
||||
BdfDictionary getGroupMetadataAsDictionary(GroupId g) throws DbException,
|
||||
FormatException;
|
||||
|
||||
BdfDictionary getGroupMetadataAsDictionary(Transaction txn, GroupId g)
|
||||
throws DbException, FormatException;
|
||||
|
||||
BdfDictionary getMessageMetadataAsDictionary(MessageId m)
|
||||
throws DbException,
|
||||
FormatException;
|
||||
|
||||
BdfDictionary getMessageMetadataAsDictionary(Transaction txn, MessageId m)
|
||||
throws DbException, FormatException;
|
||||
|
||||
Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(GroupId g)
|
||||
throws DbException, FormatException;
|
||||
|
||||
Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(
|
||||
Transaction txn, GroupId g) throws DbException, FormatException;
|
||||
|
||||
Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(GroupId g,
|
||||
BdfDictionary query) throws DbException, FormatException;
|
||||
|
||||
Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(
|
||||
Transaction txn, GroupId g, BdfDictionary query) throws DbException,
|
||||
FormatException;
|
||||
|
||||
void mergeGroupMetadata(GroupId g, BdfDictionary metadata)
|
||||
throws DbException, FormatException;
|
||||
|
||||
void mergeGroupMetadata(Transaction txn, GroupId g, BdfDictionary metadata)
|
||||
throws DbException, FormatException;
|
||||
|
||||
void mergeMessageMetadata(MessageId m, BdfDictionary metadata)
|
||||
throws DbException, FormatException;
|
||||
|
||||
void mergeMessageMetadata(Transaction txn, MessageId m,
|
||||
BdfDictionary metadata) throws DbException, FormatException;
|
||||
|
||||
byte[] toByteArray(BdfDictionary dictionary) throws FormatException;
|
||||
|
||||
byte[] toByteArray(BdfList list) throws FormatException;
|
||||
|
||||
BdfDictionary toDictionary(byte[] b, int off, int len)
|
||||
throws FormatException;
|
||||
|
||||
BdfList toList(byte[] b, int off, int len) throws FormatException;
|
||||
|
||||
BdfList toList(byte[] b) throws FormatException;
|
||||
|
||||
BdfList toList(Message m) throws FormatException;
|
||||
|
||||
byte[] sign(String label, BdfList toSign, byte[] privateKey)
|
||||
throws FormatException, GeneralSecurityException;
|
||||
|
||||
void verifySignature(String label, byte[] sig, byte[] publicKey,
|
||||
BdfList signed) throws FormatException, GeneralSecurityException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.briarproject.bramble.api.client;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.ClientId;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface ContactGroupFactory {
|
||||
|
||||
/**
|
||||
* Creates a group that is not shared with any contacts.
|
||||
*/
|
||||
Group createLocalGroup(ClientId clientId);
|
||||
|
||||
/**
|
||||
* Creates a group for the given client to share with the given contact.
|
||||
*/
|
||||
Group createContactGroup(ClientId clientId, Contact contact);
|
||||
|
||||
/**
|
||||
* Creates a group for the given client to share between the given authors
|
||||
* identified by their AuthorIds.
|
||||
*/
|
||||
Group createContactGroup(ClientId clientId, AuthorId authorId1,
|
||||
AuthorId authorId2);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.briarproject.bramble.api.contact;
|
||||
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Contact {
|
||||
|
||||
private final ContactId id;
|
||||
private final Author author;
|
||||
private final AuthorId localAuthorId;
|
||||
private final boolean verified, active;
|
||||
|
||||
public Contact(ContactId id, Author author, AuthorId localAuthorId,
|
||||
boolean verified, boolean active) {
|
||||
this.id = id;
|
||||
this.author = author;
|
||||
this.localAuthorId = localAuthorId;
|
||||
this.verified = verified;
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
public ContactId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public AuthorId getLocalAuthorId() {
|
||||
return localAuthorId;
|
||||
}
|
||||
|
||||
public boolean isVerified() {
|
||||
return verified;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof Contact && id.equals(((Contact) o).id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.briarproject.bramble.api.contact;
|
||||
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface ContactExchangeListener {
|
||||
|
||||
void contactExchangeSucceeded(Author remoteAuthor);
|
||||
|
||||
/**
|
||||
* The exchange failed because the contact already exists.
|
||||
*/
|
||||
void duplicateContact(Author remoteAuthor);
|
||||
|
||||
/**
|
||||
* A general failure.
|
||||
*/
|
||||
void contactExchangeFailed();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.briarproject.bramble.api.contact;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
|
||||
/**
|
||||
* A task for conducting a contact information exchange with a remote peer.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface ContactExchangeTask {
|
||||
|
||||
/**
|
||||
* Exchanges contact information with a remote peer.
|
||||
*/
|
||||
void startExchange(ContactExchangeListener listener,
|
||||
LocalAuthor localAuthor, SecretKey masterSecret,
|
||||
DuplexTransportConnection conn, TransportId transportId,
|
||||
boolean alice);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.briarproject.bramble.api.contact;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for an integer that uniquely identifies a contact within
|
||||
* the scope of a single node.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ContactId {
|
||||
|
||||
private final int id;
|
||||
|
||||
public ContactId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getInt() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof ContactId && id == ((ContactId) o).id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.briarproject.bramble.api.contact;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface ContactManager {
|
||||
|
||||
/**
|
||||
* Registers a hook to be called whenever a contact is added.
|
||||
*/
|
||||
void registerAddContactHook(AddContactHook hook);
|
||||
|
||||
/**
|
||||
* Registers a hook to be called whenever a contact is removed.
|
||||
*/
|
||||
void registerRemoveContactHook(RemoveContactHook hook);
|
||||
|
||||
/**
|
||||
* Stores a contact within the given transaction associated with the given
|
||||
* local and remote pseudonyms, and returns an ID for the contact.
|
||||
*/
|
||||
ContactId addContact(Transaction txn, Author remote, AuthorId local,
|
||||
SecretKey master, long timestamp, boolean alice, boolean verified,
|
||||
boolean active) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a contact associated with the given local and remote pseudonyms,
|
||||
* and returns an ID for the contact.
|
||||
*/
|
||||
ContactId addContact(Author remote, AuthorId local,
|
||||
SecretKey master, long timestamp, boolean alice, boolean verified,
|
||||
boolean active) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the contact with the given ID.
|
||||
*/
|
||||
Contact getContact(ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all active contacts.
|
||||
*/
|
||||
Collection<Contact> getActiveContacts() throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a contact and all associated state.
|
||||
*/
|
||||
void removeContact(ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a contact and all associated state.
|
||||
*/
|
||||
void removeContact(Transaction txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks a contact as active or inactive.
|
||||
*/
|
||||
void setContactActive(Transaction txn, ContactId c, boolean active)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Return true if a contact with this name and public key already exists
|
||||
*/
|
||||
boolean contactExists(Transaction txn, AuthorId remoteAuthorId,
|
||||
AuthorId localAuthorId) throws DbException;
|
||||
|
||||
/**
|
||||
* Return true if a contact with this name and public key already exists
|
||||
*/
|
||||
boolean contactExists(AuthorId remoteAuthorId, AuthorId localAuthorId)
|
||||
throws DbException;
|
||||
|
||||
interface AddContactHook {
|
||||
void addingContact(Transaction txn, Contact c) throws DbException;
|
||||
}
|
||||
|
||||
interface RemoveContactHook {
|
||||
void removingContact(Transaction txn, Contact c) throws DbException;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.briarproject.bramble.api.contact.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a contact is added.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ContactAddedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final boolean active;
|
||||
|
||||
public ContactAddedEvent(ContactId contactId, boolean active) {
|
||||
this.contactId = contactId;
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.contact.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a contact is removed.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ContactRemovedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
|
||||
public ContactRemovedEvent(ContactId contactId) {
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.briarproject.bramble.api.contact.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a contact is marked active or inactive.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ContactStatusChangedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final boolean active;
|
||||
|
||||
public ContactStatusChangedEvent(ContactId contactId, boolean active) {
|
||||
this.contactId = contactId;
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.api.contact.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a contact is verified.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ContactVerifiedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
|
||||
public ContactVerifiedEvent(ContactId contactId) {
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public interface CryptoComponent {
|
||||
|
||||
SecretKey generateSecretKey();
|
||||
|
||||
MessageDigest getMessageDigest();
|
||||
|
||||
PseudoRandom getPseudoRandom(int seed1, int seed2);
|
||||
|
||||
SecureRandom getSecureRandom();
|
||||
|
||||
KeyPair generateAgreementKeyPair();
|
||||
|
||||
KeyParser getAgreementKeyParser();
|
||||
|
||||
KeyPair generateSignatureKeyPair();
|
||||
|
||||
KeyParser getSignatureKeyParser();
|
||||
|
||||
KeyParser getMessageKeyParser();
|
||||
|
||||
/** Generates a random invitation code. */
|
||||
int generateBTInvitationCode();
|
||||
|
||||
/**
|
||||
* Derives a confirmation code from the given master secret.
|
||||
* @param alice whether the code is for use by Alice or Bob.
|
||||
*/
|
||||
int deriveBTConfirmationCode(SecretKey master, boolean alice);
|
||||
|
||||
/**
|
||||
* Derives a stream header key from the given master secret.
|
||||
* @param alice whether the key is for use by Alice or Bob.
|
||||
*/
|
||||
SecretKey deriveHeaderKey(SecretKey master, boolean alice);
|
||||
|
||||
/**
|
||||
* Derives a message authentication code key from the given master secret.
|
||||
* @param alice whether the key is for use by Alice or Bob.
|
||||
*/
|
||||
SecretKey deriveMacKey(SecretKey master, boolean alice);
|
||||
|
||||
/**
|
||||
* Derives a nonce from the given master secret for one of the parties to
|
||||
* sign.
|
||||
* @param alice whether the nonce is for use by Alice or Bob.
|
||||
*/
|
||||
byte[] deriveSignatureNonce(SecretKey master, boolean alice);
|
||||
|
||||
/**
|
||||
* Derives a commitment to the provided public key.
|
||||
* <p/>
|
||||
* Part of BQP.
|
||||
*
|
||||
* @param publicKey the public key
|
||||
* @return the commitment to the provided public key.
|
||||
*/
|
||||
byte[] deriveKeyCommitment(byte[] publicKey);
|
||||
|
||||
/**
|
||||
* Derives a common shared secret from two public keys and one of the
|
||||
* corresponding private keys.
|
||||
* <p/>
|
||||
* Part of BQP.
|
||||
*
|
||||
* @param theirPublicKey the ephemeral public key of the remote party
|
||||
* @param ourKeyPair our ephemeral keypair
|
||||
* @param alice true if ourKeyPair belongs to Alice
|
||||
* @return the shared secret
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
SecretKey deriveSharedSecret(byte[] theirPublicKey, KeyPair ourKeyPair,
|
||||
boolean alice) throws GeneralSecurityException;
|
||||
|
||||
/**
|
||||
* Derives the content of a confirmation record.
|
||||
* <p/>
|
||||
* Part of BQP.
|
||||
*
|
||||
* @param sharedSecret the common shared secret
|
||||
* @param theirPayload the commit payload from the remote party
|
||||
* @param ourPayload the commit payload we sent
|
||||
* @param theirPublicKey the ephemeral public key of the remote party
|
||||
* @param ourKeyPair our ephemeral keypair
|
||||
* @param alice true if ourKeyPair belongs to Alice
|
||||
* @param aliceRecord true if the confirmation record is for use by Alice
|
||||
* @return the confirmation record
|
||||
*/
|
||||
byte[] deriveConfirmationRecord(SecretKey sharedSecret,
|
||||
byte[] theirPayload, byte[] ourPayload,
|
||||
byte[] theirPublicKey, KeyPair ourKeyPair,
|
||||
boolean alice, boolean aliceRecord);
|
||||
|
||||
/**
|
||||
* Derives a master secret from the given shared secret.
|
||||
* <p/>
|
||||
* Part of BQP.
|
||||
*
|
||||
* @param sharedSecret the common shared secret
|
||||
* @return the master secret
|
||||
*/
|
||||
SecretKey deriveMasterSecret(SecretKey sharedSecret);
|
||||
|
||||
/**
|
||||
* Derives a master secret from two public keys and one of the corresponding
|
||||
* private keys.
|
||||
* <p/>
|
||||
* This is a helper method that calls
|
||||
* deriveMasterSecret(deriveSharedSecret(theirPublicKey, ourKeyPair, alice))
|
||||
*
|
||||
* @param theirPublicKey the ephemeral public key of the remote party
|
||||
* @param ourKeyPair our ephemeral keypair
|
||||
* @param alice true if ourKeyPair belongs to Alice
|
||||
* @return the shared secret
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
SecretKey deriveMasterSecret(byte[] theirPublicKey, KeyPair ourKeyPair,
|
||||
boolean alice) throws GeneralSecurityException;
|
||||
|
||||
/**
|
||||
* Derives initial transport keys for the given transport in the given
|
||||
* rotation period from the given master secret.
|
||||
* @param alice whether the keys are for use by Alice or Bob.
|
||||
*/
|
||||
TransportKeys deriveTransportKeys(TransportId t, SecretKey master,
|
||||
long rotationPeriod, boolean alice);
|
||||
|
||||
/**
|
||||
* Rotates the given transport keys to the given rotation period. If the
|
||||
* keys are for a future rotation period they are not rotated.
|
||||
*/
|
||||
TransportKeys rotateTransportKeys(TransportKeys k, long rotationPeriod);
|
||||
|
||||
/** Encodes the pseudo-random tag that is used to recognise a stream. */
|
||||
void encodeTag(byte[] tag, SecretKey tagKey, long streamNumber);
|
||||
|
||||
/**
|
||||
* Signs the given byte[] with the given PrivateKey.
|
||||
*
|
||||
* @param label A label specific to this signature
|
||||
* to ensure that the signature cannot be repurposed
|
||||
*/
|
||||
byte[] sign(String label, byte[] toSign, byte[] privateKey)
|
||||
throws GeneralSecurityException;
|
||||
|
||||
/**
|
||||
* Verifies that the given signature is valid for the signedData
|
||||
* and the given publicKey.
|
||||
*
|
||||
* @param label A label that was specific to this signature
|
||||
* to ensure that the signature cannot be repurposed
|
||||
* @return true if the signature was valid, false otherwise.
|
||||
*/
|
||||
boolean verify(String label, byte[] signedData, byte[] publicKey,
|
||||
byte[] signature) throws GeneralSecurityException;
|
||||
|
||||
/**
|
||||
* Returns the hash of the given inputs. The inputs are unambiguously
|
||||
* combined by prefixing each input with its length.
|
||||
*/
|
||||
byte[] hash(byte[]... inputs);
|
||||
|
||||
/**
|
||||
* Returns a message authentication code with the given key over the
|
||||
* given inputs. The inputs are unambiguously combined by prefixing each
|
||||
* input with its length.
|
||||
*/
|
||||
byte[] mac(SecretKey macKey, byte[]... inputs);
|
||||
|
||||
/**
|
||||
* Encrypts and authenticates the given plaintext so it can be written to
|
||||
* storage. The encryption and authentication keys are derived from the
|
||||
* given password. The ciphertext will be decryptable using the same
|
||||
* password after the app restarts.
|
||||
*/
|
||||
byte[] encryptWithPassword(byte[] plaintext, String password);
|
||||
|
||||
/**
|
||||
* Decrypts and authenticates the given ciphertext that has been read from
|
||||
* storage. The encryption and authentication keys are derived from the
|
||||
* given password. Returns null if the ciphertext cannot be decrypted and
|
||||
* authenticated (for example, if the password is wrong).
|
||||
*/
|
||||
byte[] decryptWithPassword(byte[] ciphertext, String password);
|
||||
|
||||
/**
|
||||
* Encrypts the given plaintext to the given public key.
|
||||
*/
|
||||
byte[] encryptToKey(PublicKey publicKey, byte[] plaintext);
|
||||
|
||||
/**
|
||||
* Encodes the given data as a hex string divided into lines of the given
|
||||
* length. The line terminator is CRLF.
|
||||
*/
|
||||
String asciiArmour(byte[] b, int lineLength);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Annotation for injecting the executor for long-running crypto tasks. Also
|
||||
* used for annotating methods that should run on the crypto executor.
|
||||
* <p>
|
||||
* The contract of this executor is that tasks may be run concurrently, and
|
||||
* submitting a task will never block. Tasks must not run indefinitely. Tasks
|
||||
* submitted during shutdown are discarded.
|
||||
*/
|
||||
@Qualifier
|
||||
@Target({FIELD, METHOD, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface CryptoExecutor {
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A key pair consisting of a {@link PublicKey} and a {@link PrivateKey).
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class KeyPair {
|
||||
|
||||
private final PublicKey publicKey;
|
||||
private final PrivateKey privateKey;
|
||||
|
||||
public KeyPair(PublicKey publicKey, PrivateKey privateKey) {
|
||||
this.publicKey = publicKey;
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
public PublicKey getPublic() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public PrivateKey getPrivate() {
|
||||
return privateKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface KeyParser {
|
||||
|
||||
PublicKey parsePublicKey(byte[] encodedKey) throws GeneralSecurityException;
|
||||
|
||||
PrivateKey parsePrivateKey(byte[] encodedKey)
|
||||
throws GeneralSecurityException;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface MessageDigest {
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#digest()}
|
||||
*/
|
||||
byte[] digest();
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#digest(byte[])}
|
||||
*/
|
||||
byte[] digest(byte[] input);
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#digest(byte[], int, int)}
|
||||
*/
|
||||
int digest(byte[] buf, int offset, int len);
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#getDigestLength()}
|
||||
*/
|
||||
int getDigestLength();
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#reset()}
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#update(byte)}
|
||||
*/
|
||||
void update(byte input);
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#update(byte[])}
|
||||
*/
|
||||
void update(byte[] input);
|
||||
|
||||
/**
|
||||
* @see {@link java.security.MessageDigest#update(byte[], int, int)}
|
||||
*/
|
||||
void update(byte[] input, int offset, int len);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface PasswordStrengthEstimator {
|
||||
|
||||
float NONE = 0;
|
||||
float WEAK = 0.4f;
|
||||
float QUITE_WEAK = 0.6f;
|
||||
float QUITE_STRONG = 0.8f;
|
||||
float STRONG = 1;
|
||||
|
||||
/**
|
||||
* Returns an estimate between 0 (weakest) and 1 (strongest), inclusive,
|
||||
* of the strength of the given password.
|
||||
*/
|
||||
float estimateStrength(String password);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* The private half of a public/private {@link KeyPair}.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface PrivateKey {
|
||||
|
||||
/**
|
||||
* Returns the encoded representation of this key.
|
||||
*/
|
||||
byte[] getEncoded();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* A deterministic pseudo-random number generator.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface PseudoRandom {
|
||||
|
||||
byte[] nextBytes(int bytes);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* The public half of a public/private {@link KeyPair}.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface PublicKey {
|
||||
|
||||
/**
|
||||
* Returns the encoded representation of this key.
|
||||
*/
|
||||
byte[] getEncoded();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
/**
|
||||
* A secret key used for encryption and/or authentication.
|
||||
*/
|
||||
public class SecretKey {
|
||||
|
||||
/**
|
||||
* The length of a secret key in bytes.
|
||||
*/
|
||||
public static final int LENGTH = 32;
|
||||
|
||||
private final byte[] key;
|
||||
|
||||
public SecretKey(byte[] key) {
|
||||
if (key.length != LENGTH) throw new IllegalArgumentException();
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface StreamDecrypter {
|
||||
|
||||
/**
|
||||
* Reads a frame, decrypts its payload into the given buffer and returns
|
||||
* the payload length, or -1 if no more frames can be read from the stream.
|
||||
*
|
||||
* @throws IOException if an error occurs while reading the frame,
|
||||
* or if authenticated decryption fails.
|
||||
*/
|
||||
int readFrame(byte[] payload) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.transport.StreamContext;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface StreamDecrypterFactory {
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamDecrypter} for decrypting a transport stream.
|
||||
*/
|
||||
StreamDecrypter createStreamDecrypter(InputStream in, StreamContext ctx);
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamDecrypter} for decrypting an invitation stream.
|
||||
*/
|
||||
StreamDecrypter createInvitationStreamDecrypter(InputStream in,
|
||||
SecretKey headerKey);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface StreamEncrypter {
|
||||
|
||||
/**
|
||||
* Encrypts the given frame and writes it to the stream.
|
||||
*/
|
||||
void writeFrame(byte[] payload, int payloadLength, int paddingLength,
|
||||
boolean finalFrame) throws IOException;
|
||||
|
||||
/**
|
||||
* Flushes the stream.
|
||||
*/
|
||||
void flush() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.transport.StreamContext;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface StreamEncrypterFactory {
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamEncrypter} for encrypting a transport stream.
|
||||
*/
|
||||
StreamEncrypter createStreamEncrypter(OutputStream out, StreamContext ctx);
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamEncrypter} for encrypting an invitation stream.
|
||||
*/
|
||||
StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
|
||||
SecretKey headerKey);
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class BdfDictionary extends ConcurrentSkipListMap<String, Object> {
|
||||
|
||||
public static final Object NULL_VALUE = new Object();
|
||||
|
||||
/**
|
||||
* Factory method for constructing dictionaries inline.
|
||||
* <pre>
|
||||
* BdfDictionary.of(
|
||||
* new BdfEntry("foo", foo),
|
||||
* new BdfEntry("bar", bar)
|
||||
* );
|
||||
* </pre>
|
||||
*/
|
||||
public static BdfDictionary of(Entry<String, Object>... entries) {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
for (Entry<String, Object> e : entries) d.put(e.getKey(), e.getValue());
|
||||
return d;
|
||||
}
|
||||
|
||||
public BdfDictionary() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BdfDictionary(Map<String, Object> m) {
|
||||
super(m);
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Boolean getOptionalBoolean(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key, Boolean defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public Long getLong(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof Long) return (Long) o;
|
||||
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||
if (o instanceof Short) return ((Short) o).longValue();
|
||||
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Long getOptionalLong(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof Long) return (Long) o;
|
||||
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||
if (o instanceof Short) return ((Short) o).longValue();
|
||||
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public Long getLong(String key, Long defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof Long) return (Long) o;
|
||||
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||
if (o instanceof Short) return ((Short) o).longValue();
|
||||
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public Double getDouble(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof Double) return (Double) o;
|
||||
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Double getOptionalDouble(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof Double) return (Double) o;
|
||||
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public Double getDouble(String key, Double defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof Double) return (Double) o;
|
||||
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public String getString(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof String) return (String) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getOptionalString(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof String) return (String) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public String getString(String key, String defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof String) return (String) o;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public byte[] getRaw(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof byte[]) return (byte[]) o;
|
||||
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public byte[] getOptionalRaw(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof byte[]) return (byte[]) o;
|
||||
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public byte[] getRaw(String key, byte[] defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof byte[]) return (byte[]) o;
|
||||
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public BdfList getList(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof BdfList) return (BdfList) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BdfList getOptionalList(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof BdfList) return (BdfList) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public BdfList getList(String key, BdfList defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof BdfList) return (BdfList) o;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public BdfDictionary getDictionary(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof BdfDictionary) return (BdfDictionary) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BdfDictionary getOptionalDictionary(String key)
|
||||
throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof BdfDictionary) return (BdfDictionary) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public BdfDictionary getDictionary(String key, BdfDictionary defaultValue) {
|
||||
Object o = get(key);
|
||||
if (o instanceof BdfDictionary) return (BdfDictionary) o;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class BdfEntry implements Entry<String, Object>, Comparable<BdfEntry> {
|
||||
|
||||
private final String key;
|
||||
private final Object value;
|
||||
|
||||
public BdfEntry(String key, Object value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object setValue(Object value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BdfEntry e) {
|
||||
if (e == this) return 0;
|
||||
return key.compareTo(e.key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE;
|
||||
|
||||
public class BdfList extends Vector<Object> {
|
||||
|
||||
/**
|
||||
* Factory method for constructing lists inline.
|
||||
* <pre>
|
||||
* BdfList.of(1, 2, 3);
|
||||
* </pre>
|
||||
*/
|
||||
public static BdfList of(Object... items) {
|
||||
return new BdfList(Arrays.asList(items));
|
||||
}
|
||||
|
||||
public BdfList() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BdfList(List<Object> items) {
|
||||
super(items);
|
||||
}
|
||||
|
||||
private boolean isInRange(int index) {
|
||||
return index >= 0 && index < size();
|
||||
}
|
||||
|
||||
public Boolean getBoolean(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Boolean getOptionalBoolean(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public Boolean getBoolean(int index, Boolean defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public Long getLong(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof Long) return (Long) o;
|
||||
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||
if (o instanceof Short) return ((Short) o).longValue();
|
||||
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Long getOptionalLong(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof Long) return (Long) o;
|
||||
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||
if (o instanceof Short) return ((Short) o).longValue();
|
||||
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public Long getLong(int index, Long defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof Long) return (Long) o;
|
||||
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||
if (o instanceof Short) return ((Short) o).longValue();
|
||||
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public Double getDouble(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof Double) return (Double) o;
|
||||
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Double getOptionalDouble(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof Double) return (Double) o;
|
||||
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public Double getDouble(int index, Double defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof Double) return (Double) o;
|
||||
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public String getString(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof String) return (String) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getOptionalString(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof String) return (String) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public String getString(int index, String defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof String) return (String) o;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public byte[] getRaw(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof byte[]) return (byte[]) o;
|
||||
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public byte[] getOptionalRaw(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof byte[]) return (byte[]) o;
|
||||
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public byte[] getRaw(int index, byte[] defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof byte[]) return (byte[]) o;
|
||||
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public BdfList getList(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof BdfList) return (BdfList) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BdfList getOptionalList(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof BdfList) return (BdfList) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public BdfList getList(int index, BdfList defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof BdfList) return (BdfList) o;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public BdfDictionary getDictionary(int index) throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o instanceof BdfDictionary) return (BdfDictionary) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BdfDictionary getOptionalDictionary(int index)
|
||||
throws FormatException {
|
||||
if (!isInRange(index)) throw new FormatException();
|
||||
Object o = get(index);
|
||||
if (o == null || o == NULL_VALUE) return null;
|
||||
if (o instanceof BdfDictionary) return (BdfDictionary) o;
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public BdfDictionary getDictionary(int index, BdfDictionary defaultValue) {
|
||||
if (!isInRange(index)) return defaultValue;
|
||||
Object o = get(index);
|
||||
if (o instanceof BdfDictionary) return (BdfDictionary) o;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface BdfReader {
|
||||
|
||||
int DEFAULT_NESTED_LIMIT = 5;
|
||||
|
||||
boolean eof() throws IOException;
|
||||
|
||||
void close() throws IOException;
|
||||
|
||||
boolean hasNull() throws IOException;
|
||||
|
||||
void readNull() throws IOException;
|
||||
|
||||
void skipNull() throws IOException;
|
||||
|
||||
boolean hasBoolean() throws IOException;
|
||||
|
||||
boolean readBoolean() throws IOException;
|
||||
|
||||
void skipBoolean() throws IOException;
|
||||
|
||||
boolean hasLong() throws IOException;
|
||||
|
||||
long readLong() throws IOException;
|
||||
|
||||
void skipLong() throws IOException;
|
||||
|
||||
boolean hasDouble() throws IOException;
|
||||
|
||||
double readDouble() throws IOException;
|
||||
|
||||
void skipDouble() throws IOException;
|
||||
|
||||
boolean hasString() throws IOException;
|
||||
|
||||
String readString(int maxLength) throws IOException;
|
||||
|
||||
void skipString() throws IOException;
|
||||
|
||||
boolean hasRaw() throws IOException;
|
||||
|
||||
byte[] readRaw(int maxLength) throws IOException;
|
||||
|
||||
void skipRaw() throws IOException;
|
||||
|
||||
boolean hasList() throws IOException;
|
||||
|
||||
BdfList readList() throws IOException;
|
||||
|
||||
void readListStart() throws IOException;
|
||||
|
||||
boolean hasListEnd() throws IOException;
|
||||
|
||||
void readListEnd() throws IOException;
|
||||
|
||||
void skipList() throws IOException;
|
||||
|
||||
boolean hasDictionary() throws IOException;
|
||||
|
||||
BdfDictionary readDictionary() throws IOException;
|
||||
|
||||
void readDictionaryStart() throws IOException;
|
||||
|
||||
boolean hasDictionaryEnd() throws IOException;
|
||||
|
||||
void readDictionaryEnd() throws IOException;
|
||||
|
||||
void skipDictionary() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface BdfReaderFactory {
|
||||
|
||||
BdfReader createReader(InputStream in);
|
||||
|
||||
BdfReader createReader(InputStream in, int nestedLimit);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BdfWriter {
|
||||
|
||||
void flush() throws IOException;
|
||||
|
||||
void close() throws IOException;
|
||||
|
||||
void writeNull() throws IOException;
|
||||
|
||||
void writeBoolean(boolean b) throws IOException;
|
||||
|
||||
void writeLong(long l) throws IOException;
|
||||
|
||||
void writeDouble(double d) throws IOException;
|
||||
|
||||
void writeString(String s) throws IOException;
|
||||
|
||||
void writeRaw(byte[] b) throws IOException;
|
||||
|
||||
void writeList(Collection<?> c) throws IOException;
|
||||
|
||||
void writeListStart() throws IOException;
|
||||
|
||||
void writeListEnd() throws IOException;
|
||||
|
||||
void writeDictionary(Map<?, ?> m) throws IOException;
|
||||
|
||||
void writeDictionaryStart() throws IOException;
|
||||
|
||||
void writeDictionaryEnd() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface BdfWriterFactory {
|
||||
|
||||
BdfWriter createWriter(OutputStream out);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface MetadataEncoder {
|
||||
|
||||
Metadata encode(BdfDictionary d) throws FormatException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface MetadataParser {
|
||||
|
||||
BdfDictionary parse(Metadata m) throws FormatException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.data;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface ObjectReader<T> {
|
||||
|
||||
T readObject(BdfReader r) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a duplicate contact is added to the database. This exception may
|
||||
* occur due to concurrent updates and does not indicate a database error.
|
||||
*/
|
||||
public class ContactExistsException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,506 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.sync.Ack;
|
||||
import org.briarproject.bramble.api.sync.ClientId;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.Group.Visibility;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.MessageStatus;
|
||||
import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.ValidationManager;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.api.sync.ValidationManager.State;
|
||||
|
||||
/**
|
||||
* Encapsulates the database implementation and exposes high-level operations
|
||||
* to other components.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface DatabaseComponent {
|
||||
|
||||
/**
|
||||
* Opens the database and returns true if the database already existed.
|
||||
*/
|
||||
boolean open() throws DbException;
|
||||
|
||||
/**
|
||||
* Waits for any open transactions to finish and closes the database.
|
||||
*/
|
||||
void close() throws DbException;
|
||||
|
||||
/**
|
||||
* Starts a new transaction and returns an object representing it.
|
||||
* <p/>
|
||||
* This method acquires locks, so it must not be called while holding a
|
||||
* lock.
|
||||
*
|
||||
* @param readOnly true if the transaction will only be used for reading.
|
||||
*/
|
||||
Transaction startTransaction(boolean readOnly) throws DbException;
|
||||
|
||||
/**
|
||||
* Commits a transaction to the database.
|
||||
*/
|
||||
void commitTransaction(Transaction txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Ends a transaction. If the transaction has not been committed,
|
||||
* it will be aborted. If the transaction has been committed,
|
||||
* any events attached to the transaction are broadcast.
|
||||
* The database lock will be released in either case.
|
||||
*/
|
||||
void endTransaction(Transaction txn);
|
||||
|
||||
/**
|
||||
* Stores a contact associated with the given local and remote pseudonyms,
|
||||
* and returns an ID for the contact.
|
||||
*/
|
||||
ContactId addContact(Transaction txn, Author remote, AuthorId local,
|
||||
boolean verified, boolean active) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a group.
|
||||
*/
|
||||
void addGroup(Transaction txn, Group g) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a local pseudonym.
|
||||
*/
|
||||
void addLocalAuthor(Transaction txn, LocalAuthor a) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a local message.
|
||||
*/
|
||||
void addLocalMessage(Transaction txn, Message m, Metadata meta,
|
||||
boolean shared) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a transport.
|
||||
*/
|
||||
void addTransport(Transaction txn, TransportId t, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Stores transport keys for a newly added contact.
|
||||
*/
|
||||
void addTransportKeys(Transaction txn, ContactId c, TransportKeys k)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the database contains the given contact for the given
|
||||
* local pseudonym.
|
||||
*/
|
||||
boolean containsContact(Transaction txn, AuthorId remote, AuthorId local)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the database contains the given group.
|
||||
*/
|
||||
boolean containsGroup(Transaction txn, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the database contains the given local author.
|
||||
*/
|
||||
boolean containsLocalAuthor(Transaction txn, AuthorId local)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Deletes the message with the given ID. The message ID and any other
|
||||
* associated data are not deleted.
|
||||
*/
|
||||
void deleteMessage(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Deletes any metadata associated with the given message.
|
||||
*/
|
||||
void deleteMessageMetadata(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns an acknowledgement for the given contact, or null if there are
|
||||
* no messages to acknowledge.
|
||||
*/
|
||||
@Nullable
|
||||
Ack generateAck(Transaction txn, ContactId c, int maxMessages)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns a batch of raw messages for the given contact, with a total
|
||||
* length less than or equal to the given length, for transmission over a
|
||||
* transport with the given maximum latency. Returns null if there are no
|
||||
* sendable messages that fit in the given length.
|
||||
*/
|
||||
@Nullable
|
||||
Collection<byte[]> generateBatch(Transaction txn, ContactId c,
|
||||
int maxLength, int maxLatency) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns an offer for the given contact for transmission over a
|
||||
* transport with the given maximum latency, or null if there are no
|
||||
* messages to offer.
|
||||
*/
|
||||
@Nullable
|
||||
Offer generateOffer(Transaction txn, ContactId c, int maxMessages,
|
||||
int maxLatency) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns a request for the given contact, or null if there are no
|
||||
* messages to request.
|
||||
*/
|
||||
@Nullable
|
||||
Request generateRequest(Transaction txn, ContactId c, int maxMessages)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns a batch of raw messages for the given contact, with a total
|
||||
* length less than or equal to the given length, for transmission over a
|
||||
* transport with the given maximum latency. Only messages that have been
|
||||
* requested by the contact are returned. Returns null if there are no
|
||||
* sendable messages that fit in the given length.
|
||||
*/
|
||||
@Nullable
|
||||
Collection<byte[]> generateRequestedBatch(Transaction txn, ContactId c,
|
||||
int maxLength, int maxLatency) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the contact with the given ID.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Contact getContact(Transaction txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all contacts.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<Contact> getContacts(Transaction txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns a possibly empty collection of contacts with the given author ID.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<Contact> getContactsByAuthorId(Transaction txn, AuthorId remote)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all contacts associated with the given local pseudonym.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<ContactId> getContacts(Transaction txn, AuthorId a)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the group with the given ID.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Group getGroup(Transaction txn, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the metadata for the given group.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Metadata getGroupMetadata(Transaction txn, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all groups belonging to the given client.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<Group> getGroups(Transaction txn, ClientId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the given group's visibility to the given contact, or
|
||||
* {@link Visibility INVISIBLE} if the group is not in the database.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Visibility getGroupVisibility(Transaction txn, ContactId c, GroupId g)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the local pseudonym with the given ID.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
LocalAuthor getLocalAuthor(Transaction txn, AuthorId a) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all local pseudonyms.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<LocalAuthor> getLocalAuthors(Transaction txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs of any messages that need to be validated by the given
|
||||
* client.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<MessageId> getMessagesToValidate(Transaction txn, ClientId c)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs of any messages that are valid but pending delivery due
|
||||
* to dependencies on other messages for the given client.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<MessageId> getPendingMessages(Transaction txn, ClientId c)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs of any messages from the given client
|
||||
* that have a shared dependent, but are still not shared themselves.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<MessageId> getMessagesToShare(Transaction txn,
|
||||
ClientId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the message with the given ID, in serialised form, or null if
|
||||
* the message has been deleted.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
@Nullable
|
||||
byte[] getRawMessage(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the metadata for all delivered messages in the given group.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Map<MessageId, Metadata> getMessageMetadata(Transaction txn, GroupId g)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the metadata for any messages in the given group with metadata
|
||||
* that matches all entries in the given query. If the query is empty, the
|
||||
* metadata for all messages is returned.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Map<MessageId, Metadata> getMessageMetadata(Transaction txn, GroupId g,
|
||||
Metadata query) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the metadata for the given delivered message.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Metadata getMessageMetadata(Transaction txn, MessageId m)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the metadata for the given delivered and pending message.
|
||||
* This is meant to be only used by the ValidationManager
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Metadata getMessageMetadataForValidator(Transaction txn, MessageId m)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the status of all messages in the given group with respect to
|
||||
* the given contact.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Collection<MessageStatus> getMessageStatus(Transaction txn, ContactId c,
|
||||
GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs and states of all dependencies of the given message.
|
||||
* Missing dependencies have the state
|
||||
* {@link ValidationManager.State UNKNOWN}.
|
||||
* Dependencies in other groups have the state
|
||||
* {@link ValidationManager.State INVALID}.
|
||||
* Note that these states are not set on the dependencies themselves; the
|
||||
* returned states should only be taken in the context of the given message.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Map<MessageId, State> getMessageDependencies(Transaction txn, MessageId m)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all IDs of messages that depend on the given message.
|
||||
* Messages in other groups that declare a dependency on the given message
|
||||
* will be returned even though such dependencies are invalid.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Map<MessageId, State> getMessageDependents(Transaction txn, MessageId m)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Gets the validation and delivery state of the given message.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
State getMessageState(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the status of the given message with respect to the given
|
||||
* contact.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
MessageStatus getMessageStatus(Transaction txn, ContactId c, MessageId m)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all settings in the given namespace.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Settings getSettings(Transaction txn, String namespace) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all transport keys for the given transport.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
Map<ContactId, TransportKeys> getTransportKeys(Transaction txn,
|
||||
TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Increments the outgoing stream counter for the given contact and
|
||||
* transport in the given rotation period .
|
||||
*/
|
||||
void incrementStreamCounter(Transaction txn, ContactId c, TransportId t,
|
||||
long rotationPeriod) throws DbException;
|
||||
|
||||
/**
|
||||
* Merges the given metadata with the existing metadata for the given
|
||||
* group.
|
||||
*/
|
||||
void mergeGroupMetadata(Transaction txn, GroupId g, Metadata meta)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Merges the given metadata with the existing metadata for the given
|
||||
* message.
|
||||
*/
|
||||
void mergeMessageMetadata(Transaction txn, MessageId m, Metadata meta)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Merges the given settings with the existing settings in the given
|
||||
* namespace.
|
||||
*/
|
||||
void mergeSettings(Transaction txn, Settings s, String namespace)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Processes an ack from the given contact.
|
||||
*/
|
||||
void receiveAck(Transaction txn, ContactId c, Ack a) throws DbException;
|
||||
|
||||
/**
|
||||
* Processes a message from the given contact.
|
||||
*/
|
||||
void receiveMessage(Transaction txn, ContactId c, Message m)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Processes an offer from the given contact.
|
||||
*/
|
||||
void receiveOffer(Transaction txn, ContactId c, Offer o) throws DbException;
|
||||
|
||||
/**
|
||||
* Processes a request from the given contact.
|
||||
*/
|
||||
void receiveRequest(Transaction txn, ContactId c, Request r)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a contact (and all associated state) from the database.
|
||||
*/
|
||||
void removeContact(Transaction txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a group (and all associated state) from the database.
|
||||
*/
|
||||
void removeGroup(Transaction txn, Group g) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a local pseudonym (and all associated state) from the database.
|
||||
*/
|
||||
void removeLocalAuthor(Transaction txn, AuthorId a) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a transport (and all associated state) from the database.
|
||||
*/
|
||||
void removeTransport(Transaction txn, TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given contact as verified.
|
||||
*/
|
||||
void setContactVerified(Transaction txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given contact as active or inactive.
|
||||
*/
|
||||
void setContactActive(Transaction txn, ContactId c, boolean active)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the given group's visibility to the given contact.
|
||||
*/
|
||||
void setGroupVisibility(Transaction txn, ContactId c, GroupId g,
|
||||
Visibility v) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given message as shared.
|
||||
*/
|
||||
void setMessageShared(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the validation and delivery state of the given message.
|
||||
*/
|
||||
void setMessageState(Transaction txn, MessageId m, State state)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Adds dependencies for a message
|
||||
*/
|
||||
void addMessageDependencies(Transaction txn, Message dependent,
|
||||
Collection<MessageId> dependencies) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the reordering window for the given contact and transport in the
|
||||
* given rotation period.
|
||||
*/
|
||||
void setReorderingWindow(Transaction txn, ContactId c, TransportId t,
|
||||
long rotationPeriod, long base, byte[] bitmap) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given transport keys, deleting any keys they have replaced.
|
||||
*/
|
||||
void updateTransportKeys(Transaction txn,
|
||||
Map<ContactId, TransportKeys> keys) throws DbException;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface DatabaseConfig {
|
||||
|
||||
boolean databaseExists();
|
||||
|
||||
File getDatabaseDirectory();
|
||||
|
||||
void setEncryptionKey(SecretKey key);
|
||||
|
||||
@Nullable
|
||||
SecretKey getEncryptionKey();
|
||||
|
||||
void setLocalAuthorName(String nickname);
|
||||
|
||||
@Nullable
|
||||
String getLocalAuthorName();
|
||||
|
||||
long getMaxSize();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Annotation for injecting the executor for database tasks. Also used for
|
||||
* annotating methods that should run on the database executor.
|
||||
* <p>
|
||||
* The contract of this executor is that tasks are run in the order they're
|
||||
* submitted, tasks are not run concurrently, and submitting a task will never
|
||||
* block. Tasks must not run indefinitely. Tasks submitted during shutdown are
|
||||
* discarded.
|
||||
*/
|
||||
@Qualifier
|
||||
@Target({FIELD, METHOD, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface DatabaseExecutor {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a database operation is attempted and the database is closed.
|
||||
*/
|
||||
public class DbClosedException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
public class DbException extends Exception {
|
||||
|
||||
public DbException() {
|
||||
}
|
||||
|
||||
public DbException(Throwable t) {
|
||||
super(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
public class Metadata extends Hashtable<String, byte[]> {
|
||||
|
||||
/**
|
||||
* Special value to indicate that a key is being removed.
|
||||
*/
|
||||
public static final byte[] REMOVE = new byte[0];
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a database operation is attempted for a contact that is not in
|
||||
* the database. This exception may occur due to concurrent updates and does
|
||||
* not indicate a database error.
|
||||
*/
|
||||
public class NoSuchContactException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a database operation is attempted for a group that is not in the
|
||||
* database. This exception may occur due to concurrent updates and does not
|
||||
* indicate a database error.
|
||||
*/
|
||||
public class NoSuchGroupException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a database operation is attempted for a pseudonym that is not in
|
||||
* the database. This exception may occur due to concurrent updates and does
|
||||
* not indicate a database error.
|
||||
*/
|
||||
public class NoSuchLocalAuthorException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a database operation is attempted for a message that is not in
|
||||
* the database. This exception may occur due to concurrent updates and does
|
||||
* not indicate a database error.
|
||||
*/
|
||||
public class NoSuchMessageException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
/**
|
||||
* Thrown when a database operation is attempted for a transport that is not in
|
||||
* the database. This exception may occur due to concurrent updates and does
|
||||
* not indicate a database error.
|
||||
*/
|
||||
public class NoSuchTransportException extends DbException {
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
/**
|
||||
* A wrapper around a database transaction. Transactions are not thread-safe.
|
||||
*/
|
||||
@NotThreadSafe
|
||||
public class Transaction {
|
||||
|
||||
private final Object txn;
|
||||
private final boolean readOnly;
|
||||
|
||||
private List<Event> events = null;
|
||||
private boolean committed = false;
|
||||
|
||||
public Transaction(Object txn, boolean readOnly) {
|
||||
this.txn = txn;
|
||||
this.readOnly = readOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the database transaction. The type of the returned object
|
||||
* depends on the database implementation.
|
||||
*/
|
||||
public Object unbox() {
|
||||
return txn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the transaction can only be used for reading.
|
||||
*/
|
||||
public boolean isReadOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches an event to be broadcast when the transaction has been
|
||||
* committed.
|
||||
*/
|
||||
public void attach(Event e) {
|
||||
if (events == null) events = new ArrayList<Event>();
|
||||
events.add(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns any events attached to the transaction.
|
||||
*/
|
||||
public List<Event> getEvents() {
|
||||
if (events == null) return Collections.emptyList();
|
||||
return events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the transaction has been committed.
|
||||
*/
|
||||
public boolean isCommitted() {
|
||||
return committed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the transaction as committed. This method should only be called
|
||||
* by the DatabaseComponent. It must not be called more than once.
|
||||
*/
|
||||
public void setCommitted() {
|
||||
if (committed) throw new IllegalStateException();
|
||||
committed = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.briarproject.bramble.api.event;
|
||||
|
||||
/**
|
||||
* An abstract superclass for events.
|
||||
*/
|
||||
public abstract class Event {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.briarproject.bramble.api.event;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface EventBus {
|
||||
|
||||
/**
|
||||
* Adds a listener to be notified when events occur.
|
||||
*/
|
||||
void addListener(EventListener l);
|
||||
|
||||
/**
|
||||
* Removes a listener.
|
||||
*/
|
||||
void removeListener(EventListener l);
|
||||
|
||||
/**
|
||||
* Notifies all listeners of an event.
|
||||
*/
|
||||
void broadcast(Event e);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.briarproject.bramble.api.event;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* An interface for receiving notifications when events occur.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface EventListener {
|
||||
|
||||
/**
|
||||
* Called when an event is broadcast. Implementations of this method must
|
||||
* not block.
|
||||
*/
|
||||
void eventOccurred(Event e);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.briarproject.bramble.api.identity;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A pseudonym for a user.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Author {
|
||||
|
||||
public enum Status {ANONYMOUS, UNKNOWN, UNVERIFIED, VERIFIED, OURSELVES}
|
||||
|
||||
private final AuthorId id;
|
||||
private final String name;
|
||||
private final byte[] publicKey;
|
||||
|
||||
public Author(AuthorId id, String name, byte[] publicKey) {
|
||||
int length;
|
||||
try {
|
||||
length = name.getBytes("UTF-8").length;
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (length == 0 || length > AuthorConstants.MAX_AUTHOR_NAME_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the author's unique identifier.
|
||||
*/
|
||||
public AuthorId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the author's name.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public key used to verify the pseudonym's signatures.
|
||||
*/
|
||||
public byte[] getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof Author && id.equals(((Author) o).id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.briarproject.bramble.api.identity;
|
||||
|
||||
public interface AuthorConstants {
|
||||
|
||||
/**
|
||||
* The maximum length of an author's name in UTF-8 bytes.
|
||||
*/
|
||||
int MAX_AUTHOR_NAME_LENGTH = 50;
|
||||
|
||||
/**
|
||||
* The maximum length of a public key in bytes.
|
||||
* <p>
|
||||
* Public keys use SEC1 format: 0x04 x y, where x and y are unsigned
|
||||
* big-endian integers.
|
||||
* <p>
|
||||
* For a 256-bit elliptic curve, the maximum length is 2 * 256 / 8 + 1.
|
||||
*/
|
||||
int MAX_PUBLIC_KEY_LENGTH = 65;
|
||||
|
||||
/**
|
||||
* The maximum length of a signature in bytes.
|
||||
* <p>
|
||||
* A signature is an ASN.1 DER sequence containing two integers, r and s.
|
||||
* The format is 0x30 len1 0x02 len2 r 0x02 len3 s, where len1 is
|
||||
* len(0x02 len2 r 0x02 len3 s) as a DER length, len2 is len(r) as a DER
|
||||
* length, len3 is len(s) as a DER length, and r and s are signed
|
||||
* big-endian integers of minimal length.
|
||||
* <p>
|
||||
* For a 256-bit elliptic curve, the lengths are one byte each, so the
|
||||
* maximum length is 2 * 256 / 8 + 8.
|
||||
*/
|
||||
int MAX_SIGNATURE_LENGTH = 72;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.briarproject.bramble.api.identity;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface AuthorFactory {
|
||||
|
||||
Author createAuthor(String name, byte[] publicKey);
|
||||
|
||||
LocalAuthor createLocalAuthor(String name, byte[] publicKey,
|
||||
byte[] privateKey);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.briarproject.bramble.api.identity;
|
||||
|
||||
import org.briarproject.bramble.api.UniqueId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies an
|
||||
* {@link Author}.
|
||||
*/
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class AuthorId extends UniqueId {
|
||||
|
||||
/**
|
||||
* Label for hashing authors to calculate their identities.
|
||||
*/
|
||||
public static final byte[] LABEL =
|
||||
"AUTHOR_ID".getBytes(Charset.forName("US-ASCII"));
|
||||
|
||||
public AuthorId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof AuthorId && super.equals(o);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.briarproject.bramble.api.identity;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author.Status;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface IdentityManager {
|
||||
|
||||
/**
|
||||
* Stores the local pseudonym.
|
||||
*/
|
||||
void registerLocalAuthor(LocalAuthor a) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the cached main local identity, non-blocking, or loads it from
|
||||
* the db, blocking
|
||||
*/
|
||||
LocalAuthor getLocalAuthor() throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the cached main local identity, non-blocking, or loads it from
|
||||
* the db, blocking, within the given Transaction.
|
||||
*/
|
||||
LocalAuthor getLocalAuthor(Transaction txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the trust-level status of the author
|
||||
*/
|
||||
Status getAuthorStatus(AuthorId a) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the trust-level status of the author
|
||||
*/
|
||||
Status getAuthorStatus(Transaction txn, AuthorId a) throws DbException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.briarproject.bramble.api.identity;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A pseudonym for the local user.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class LocalAuthor extends Author {
|
||||
|
||||
private final byte[] privateKey;
|
||||
private final long created;
|
||||
|
||||
public LocalAuthor(AuthorId id, String name, byte[] publicKey,
|
||||
byte[] privateKey, long created) {
|
||||
super(id, name, publicKey);
|
||||
this.privateKey = privateKey;
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the private key used to generate the pseudonym's signatures.
|
||||
*/
|
||||
public byte[] getPrivateKey() {
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time the pseudonym was created, in milliseconds since the
|
||||
* Unix epoch.
|
||||
*/
|
||||
public long getTimeCreated() {
|
||||
return created;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.identity.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a local pseudonym is added.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class LocalAuthorAddedEvent extends Event {
|
||||
|
||||
private final AuthorId authorId;
|
||||
|
||||
public LocalAuthorAddedEvent(AuthorId authorId) {
|
||||
this.authorId = authorId;
|
||||
}
|
||||
|
||||
public AuthorId getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.identity.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a local pseudonym is removed.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class LocalAuthorRemovedEvent extends Event {
|
||||
|
||||
private final AuthorId authorId;
|
||||
|
||||
public LocalAuthorRemovedEvent(AuthorId authorId) {
|
||||
this.authorId = authorId;
|
||||
}
|
||||
|
||||
public AuthorId getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.briarproject.bramble.api.invitation;
|
||||
|
||||
public interface InvitationConstants {
|
||||
|
||||
/**
|
||||
* The connection timeout in milliseconds.
|
||||
*/
|
||||
long CONNECTION_TIMEOUT = 60 * 1000;
|
||||
|
||||
/**
|
||||
* The confirmation timeout in milliseconds.
|
||||
*/
|
||||
long CONFIRMATION_TIMEOUT = 60 * 1000;
|
||||
|
||||
/**
|
||||
* The number of bits in an invitation or confirmation code. Codes must fit
|
||||
* into six decimal digits.
|
||||
*/
|
||||
int CODE_BITS = 19;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.briarproject.bramble.api.invitation;
|
||||
|
||||
/**
|
||||
* An interface for receiving updates about the state of an
|
||||
* {@link InvitationTask}.
|
||||
*/
|
||||
public interface InvitationListener {
|
||||
|
||||
/** Called if a connection to the remote peer is established. */
|
||||
void connectionSucceeded();
|
||||
|
||||
/**
|
||||
* Called if a connection to the remote peer cannot be established. This
|
||||
* indicates that the protocol has ended unsuccessfully.
|
||||
*/
|
||||
void connectionFailed();
|
||||
|
||||
/** Called if key agreement with the remote peer succeeds. */
|
||||
void keyAgreementSucceeded(int localCode, int remoteCode);
|
||||
|
||||
/**
|
||||
* Called if key agreement with the remote peer fails or the connection is
|
||||
* lost. This indicates that the protocol has ended unsuccessfully.
|
||||
*/
|
||||
void keyAgreementFailed();
|
||||
|
||||
/** Called if the remote peer's confirmation check succeeds. */
|
||||
void remoteConfirmationSucceeded();
|
||||
|
||||
/**
|
||||
* Called if remote peer's confirmation check fails or the connection is
|
||||
* lost. This indicates that the protocol has ended unsuccessfully.
|
||||
*/
|
||||
void remoteConfirmationFailed();
|
||||
|
||||
/**
|
||||
* Called if the exchange of pseudonyms succeeds. This indicates that the
|
||||
* protocol has ended successfully.
|
||||
*/
|
||||
void pseudonymExchangeSucceeded(String remoteName);
|
||||
|
||||
/**
|
||||
* Called if the exchange of pseudonyms fails or the connection is lost.
|
||||
* This indicates that the protocol has ended unsuccessfully.
|
||||
*/
|
||||
void pseudonymExchangeFailed();
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.briarproject.bramble.api.invitation;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A snapshot of the state of an {@link InvitationTask}.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class InvitationState {
|
||||
|
||||
private final int localInvitationCode, remoteInvitationCode;
|
||||
private final int localConfirmationCode, remoteConfirmationCode;
|
||||
private final boolean connected, connectionFailed;
|
||||
private final boolean localCompared, remoteCompared;
|
||||
private final boolean localMatched, remoteMatched;
|
||||
@Nullable
|
||||
private final String contactName;
|
||||
|
||||
public InvitationState(int localInvitationCode, int remoteInvitationCode,
|
||||
int localConfirmationCode, int remoteConfirmationCode,
|
||||
boolean connected, boolean connectionFailed, boolean localCompared,
|
||||
boolean remoteCompared, boolean localMatched,
|
||||
boolean remoteMatched, @Nullable String contactName) {
|
||||
this.localInvitationCode = localInvitationCode;
|
||||
this.remoteInvitationCode = remoteInvitationCode;
|
||||
this.localConfirmationCode = localConfirmationCode;
|
||||
this.remoteConfirmationCode = remoteConfirmationCode;
|
||||
this.connected = connected;
|
||||
this.connectionFailed = connectionFailed;
|
||||
this.localCompared = localCompared;
|
||||
this.remoteCompared = remoteCompared;
|
||||
this.localMatched = localMatched;
|
||||
this.remoteMatched = remoteMatched;
|
||||
this.contactName = contactName;
|
||||
}
|
||||
|
||||
public int getLocalInvitationCode() {
|
||||
return localInvitationCode;
|
||||
}
|
||||
|
||||
public int getRemoteInvitationCode() {
|
||||
return remoteInvitationCode;
|
||||
}
|
||||
|
||||
public int getLocalConfirmationCode() {
|
||||
return localConfirmationCode;
|
||||
}
|
||||
|
||||
public int getRemoteConfirmationCode() {
|
||||
return remoteConfirmationCode;
|
||||
}
|
||||
|
||||
public boolean getConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public boolean getConnectionFailed() {
|
||||
return connectionFailed;
|
||||
}
|
||||
|
||||
public boolean getLocalCompared() {
|
||||
return localCompared;
|
||||
}
|
||||
|
||||
public boolean getRemoteCompared() {
|
||||
return remoteCompared;
|
||||
}
|
||||
|
||||
public boolean getLocalMatched() {
|
||||
return localMatched;
|
||||
}
|
||||
|
||||
public boolean getRemoteMatched() {
|
||||
return remoteMatched;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getContactName() {
|
||||
return contactName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.briarproject.bramble.api.invitation;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* A task for exchanging invitations with a remote peer.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface InvitationTask {
|
||||
|
||||
/**
|
||||
* Adds a listener to be informed of state changes and returns the
|
||||
* task's current state.
|
||||
*/
|
||||
InvitationState addListener(InvitationListener l);
|
||||
|
||||
/**
|
||||
* Removes the given listener.
|
||||
*/
|
||||
void removeListener(InvitationListener l);
|
||||
|
||||
/**
|
||||
* Asynchronously starts the connection process.
|
||||
*/
|
||||
void connect();
|
||||
|
||||
/**
|
||||
* Asynchronously informs the remote peer that the local peer's
|
||||
* confirmation codes matched.
|
||||
*/
|
||||
void localConfirmationSucceeded();
|
||||
|
||||
/**
|
||||
* Asynchronously informs the remote peer that the local peer's
|
||||
* confirmation codes did not match.
|
||||
*/
|
||||
void localConfirmationFailed();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.briarproject.bramble.api.invitation;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* Creates tasks for exchanging invitations with remote peers.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface InvitationTaskFactory {
|
||||
|
||||
/**
|
||||
* Creates a task using the given local and remote invitation codes.
|
||||
*/
|
||||
InvitationTask createTask(int localCode, int remoteCode);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class KeyAgreementConnection {
|
||||
|
||||
private final DuplexTransportConnection conn;
|
||||
private final TransportId id;
|
||||
|
||||
public KeyAgreementConnection(DuplexTransportConnection conn,
|
||||
TransportId id) {
|
||||
this.conn = conn;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public DuplexTransportConnection getConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
public TransportId getTransportId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
public interface KeyAgreementConstants {
|
||||
|
||||
/**
|
||||
* The current version of the BQP protocol.
|
||||
*/
|
||||
byte PROTOCOL_VERSION = 2;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* The transport identifier for Bluetooth.
|
||||
*/
|
||||
int TRANSPORT_ID_BLUETOOTH = 0;
|
||||
|
||||
/**
|
||||
* The transport identifier for LAN.
|
||||
*/
|
||||
int TRANSPORT_ID_LAN = 1;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* An class for managing a particular key agreement listener.
|
||||
*/
|
||||
public abstract class KeyAgreementListener {
|
||||
|
||||
private final BdfList descriptor;
|
||||
|
||||
public KeyAgreementListener(BdfList descriptor) {
|
||||
this.descriptor = descriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the descriptor that a remote peer can use to connect to this
|
||||
* listener.
|
||||
*/
|
||||
public BdfList getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts listening for incoming connections, and returns a Callable that
|
||||
* will return a KeyAgreementConnection when an incoming connection is
|
||||
* received.
|
||||
*/
|
||||
public abstract Callable<KeyAgreementConnection> listen();
|
||||
|
||||
/**
|
||||
* Closes the underlying server socket.
|
||||
*/
|
||||
public abstract void close();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.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,28 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* A task for conducting a key agreement with a remote peer.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
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,15 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* Manages tasks for conducting key agreements with remote peers.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface KeyAgreementTaskFactory {
|
||||
|
||||
/**
|
||||
* Gets the current key agreement task.
|
||||
*/
|
||||
KeyAgreementTask createTask();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A BQP payload.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
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,9 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface PayloadEncoder {
|
||||
|
||||
byte[] encode(Payload p);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface PayloadParser {
|
||||
|
||||
Payload parse(byte[] raw) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
/**
|
||||
* Record types for BQP.
|
||||
*/
|
||||
public interface RecordTypes {
|
||||
|
||||
byte KEY = 0;
|
||||
byte CONFIRM = 1;
|
||||
byte ABORT = 2;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.briarproject.bramble.api.keyagreement;
|
||||
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class TransportDescriptor {
|
||||
|
||||
private final TransportId id;
|
||||
private final BdfList descriptor;
|
||||
|
||||
public TransportDescriptor(TransportId id, BdfList descriptor) {
|
||||
this.id = id;
|
||||
this.descriptor = descriptor;
|
||||
}
|
||||
|
||||
public TransportId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public BdfList getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.keyagreement.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a BQP protocol aborts.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class KeyAgreementAbortedEvent extends Event {
|
||||
|
||||
private final boolean remoteAborted;
|
||||
|
||||
public KeyAgreementAbortedEvent(boolean remoteAborted) {
|
||||
this.remoteAborted = remoteAborted;
|
||||
}
|
||||
|
||||
public boolean didRemoteAbort() {
|
||||
return remoteAborted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.keyagreement.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a BQP connection cannot be created.
|
||||
*/
|
||||
public class KeyAgreementFailedEvent extends Event {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.keyagreement.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a BQP protocol completes.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class KeyAgreementFinishedEvent extends Event {
|
||||
|
||||
private final KeyAgreementResult result;
|
||||
|
||||
public KeyAgreementFinishedEvent(KeyAgreementResult result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public KeyAgreementResult getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.keyagreement.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.keyagreement.Payload;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a BQP task is listening.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class KeyAgreementListeningEvent extends Event {
|
||||
|
||||
private final Payload localPayload;
|
||||
|
||||
public KeyAgreementListeningEvent(Payload localPayload) {
|
||||
this.localPayload = localPayload;
|
||||
}
|
||||
|
||||
public Payload getLocalPayload() {
|
||||
return localPayload;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.keyagreement.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a BQP protocol completes.
|
||||
*/
|
||||
public class KeyAgreementStartedEvent extends Event {
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.briarproject.bramble.api.keyagreement.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a BQP protocol is waiting on the remote
|
||||
* peer to start.
|
||||
*/
|
||||
public class KeyAgreementWaitingEvent extends Event {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.lifecycle;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Annotation for injecting the executor for long-running IO tasks. Also used
|
||||
* for annotating methods that should run on the UI executor.
|
||||
* <p>
|
||||
* The contract of this executor is that tasks may be run concurrently, and
|
||||
* submitting a task will never block. Tasks may run indefinitely. Tasks
|
||||
* submitted during shutdown are discarded.
|
||||
*/
|
||||
@Qualifier
|
||||
@Target({FIELD, METHOD, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface IoExecutor {
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.briarproject.bramble.api.lifecycle;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.Client;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Manages the lifecycle of the app, starting {@link Client Clients}, starting
|
||||
* and stopping {@link Service Services}, shutting down
|
||||
* {@link ExecutorService ExecutorServices}, and opening and closing the
|
||||
* {@link DatabaseComponent}.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface LifecycleManager {
|
||||
|
||||
/**
|
||||
* The result of calling {@link #startServices(String)}.
|
||||
*/
|
||||
enum StartResult {
|
||||
ALREADY_RUNNING, DB_ERROR, SERVICE_ERROR, SUCCESS
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a {@link Service} to be started and stopped.
|
||||
*/
|
||||
void registerService(Service s);
|
||||
|
||||
/**
|
||||
* Registers a {@link Client} to be started.
|
||||
*/
|
||||
void registerClient(Client c);
|
||||
|
||||
/**
|
||||
* Registers an {@link ExecutorService} to be shut down.
|
||||
*/
|
||||
void registerForShutdown(ExecutorService e);
|
||||
|
||||
/**
|
||||
* Opens the {@link DatabaseComponent}, optionally creates a local author
|
||||
* with the provided nickname, and starts any registered
|
||||
* {@link Client Clients} and {@link Service Services}.
|
||||
*/
|
||||
StartResult startServices(@Nullable String nickname);
|
||||
|
||||
/**
|
||||
* Stops any registered {@link Service Services}, shuts down any
|
||||
* registered {@link ExecutorService ExecutorServices}, and closes the
|
||||
* {@link DatabaseComponent}.
|
||||
*/
|
||||
void stopServices();
|
||||
|
||||
/**
|
||||
* Waits for the {@link DatabaseComponent} to be opened before returning.
|
||||
*/
|
||||
void waitForDatabase() throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Waits for the {@link DatabaseComponent} to be opened and all registered
|
||||
* {@link Client Clients} and {@link Service Services} to start before
|
||||
* returning.
|
||||
*/
|
||||
void waitForStartup() throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Waits for all registered {@link Service Services} to stop, all
|
||||
* registered {@link ExecutorService ExecutorServices} to shut down, and
|
||||
* the {@link DatabaseComponent} to be closed before returning.
|
||||
*/
|
||||
void waitForShutdown() throws InterruptedException;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.briarproject.bramble.api.lifecycle;
|
||||
|
||||
public interface Service {
|
||||
|
||||
/**
|
||||
* Starts the service.This method must not be called concurrently with
|
||||
* {@link #stopService()}.
|
||||
*/
|
||||
void startService() throws ServiceException;
|
||||
|
||||
/**
|
||||
* Stops the service. This method must not be called concurrently with
|
||||
* {@link #startService()}.
|
||||
*/
|
||||
void stopService() throws ServiceException;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.briarproject.bramble.api.lifecycle;
|
||||
|
||||
/**
|
||||
* An exception that indicates an error starting or stopping a {@link Service}.
|
||||
*/
|
||||
public class ServiceException extends Exception {
|
||||
|
||||
public ServiceException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.briarproject.bramble.api.lifecycle;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface ShutdownManager {
|
||||
|
||||
/**
|
||||
* Registers a hook to be run when the JVM shuts down and returns a handle
|
||||
* that can be used to remove the hook.
|
||||
*/
|
||||
int addShutdownHook(Runnable hook);
|
||||
|
||||
/**
|
||||
* Removes the shutdown hook identified by the given handle and returns
|
||||
* true if the hook was removed.
|
||||
*/
|
||||
boolean removeShutdownHook(int handle);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.bramble.api.lifecycle.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when the app is shutting down.
|
||||
*/
|
||||
public class ShutdownEvent extends Event {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.api.nullsafety;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.meta.TypeQualifierDefault;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* This annotation can be applied to a package or class to indicate that
|
||||
* the fields in that element are non-null by default unless:
|
||||
* <ul>
|
||||
* <li> There is an explicit nullness annotation
|
||||
* <li> There is a default nullness annotation applied to a more tightly
|
||||
* nested element.
|
||||
* </ul>
|
||||
*/
|
||||
@Documented
|
||||
@Nonnull
|
||||
@TypeQualifierDefault(FIELD)
|
||||
@Retention(RUNTIME)
|
||||
public @interface FieldsNotNullByDefault {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.briarproject.bramble.api.nullsafety;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.meta.TypeQualifierDefault;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* This annotation can be applied to a package or class to indicate that
|
||||
* the methods in that element are non-null by default unless:
|
||||
* <ul>
|
||||
* <li> There is an explicit nullness annotation
|
||||
* <li> The method overrides a method in a superclass (in which case the
|
||||
* annotation of the corresponding method in the superclass applies)
|
||||
* <li> There is a default nullness annotation applied to a more tightly
|
||||
* nested element.
|
||||
* </ul>
|
||||
*/
|
||||
@Documented
|
||||
@Nonnull
|
||||
@TypeQualifierDefault(METHOD)
|
||||
@Retention(RUNTIME)
|
||||
public @interface MethodsNotNullByDefault {
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.briarproject.bramble.api.nullsafety;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.meta.TypeQualifierDefault;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* This annotation can be applied to a package or class to indicate that
|
||||
* the fields, methods and parameters in that element are non-null by default
|
||||
* unless:
|
||||
* <ul>
|
||||
* <li> There is an explicit nullness annotation
|
||||
* <li> The method overrides a method in a superclass (in which case the
|
||||
* annotation of the corresponding method or parameter in the superclass
|
||||
* applies)
|
||||
* <li> There is a default nullness annotation applied to a more tightly
|
||||
* nested element.
|
||||
* </ul>
|
||||
*/
|
||||
@Documented
|
||||
@Nonnull
|
||||
@TypeQualifierDefault({FIELD, METHOD, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface NotNullByDefault {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.briarproject.bramble.api.nullsafety;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.meta.TypeQualifierDefault;
|
||||
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* This annotation can be applied to a package or class to indicate that
|
||||
* the method parameters in that element are non-null by default unless:
|
||||
* <ul>
|
||||
* <li> There is an explicit nullness annotation
|
||||
* <li> The method overrides a method in a superclass (in which case the
|
||||
* annotation of the corresponding parameter in the superclass applies)
|
||||
* <li> There is a default nullness annotation applied to a more tightly
|
||||
* nested element.
|
||||
* </ul>
|
||||
*/
|
||||
@Documented
|
||||
@Nonnull
|
||||
@TypeQualifierDefault(PARAMETER)
|
||||
@Retention(RUNTIME)
|
||||
public @interface ParametersNotNullByDefault {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.briarproject.bramble.api.plugin;
|
||||
|
||||
/**
|
||||
* Calculates polling intervals for transport plugins that use backoff.
|
||||
*/
|
||||
public interface Backoff {
|
||||
|
||||
/**
|
||||
* Returns the current polling interval.
|
||||
*/
|
||||
int getPollingInterval();
|
||||
|
||||
/**
|
||||
* Increments the backoff counter.
|
||||
*/
|
||||
void increment();
|
||||
|
||||
/**
|
||||
* Resets the backoff counter.
|
||||
*/
|
||||
void reset();
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user