mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Refactor KeyManager and TagRecogniser. #55
This commit is contained in:
@@ -1,20 +1,6 @@
|
||||
package org.briarproject.crypto;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static org.briarproject.api.invitation.InvitationConstants.CODE_BITS;
|
||||
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.crypto.EllipticCurveConstants.PARAMETERS;
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
import org.briarproject.api.crypto.KeyParser;
|
||||
@@ -25,6 +11,9 @@ import org.briarproject.api.crypto.PublicKey;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.Signature;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
import org.briarproject.api.transport.IncomingKeys;
|
||||
import org.briarproject.api.transport.OutgoingKeys;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
import org.briarproject.util.ByteUtils;
|
||||
import org.briarproject.util.StringUtils;
|
||||
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
|
||||
@@ -43,6 +32,21 @@ import org.spongycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.spongycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.spongycastle.crypto.params.KeyParameter;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static org.briarproject.api.invitation.InvitationConstants.CODE_BITS;
|
||||
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.crypto.EllipticCurveConstants.PARAMETERS;
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
private static final Logger LOG =
|
||||
@@ -55,22 +59,33 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
private static final int PBKDF_TARGET_MILLIS = 500;
|
||||
private static final int PBKDF_SAMPLES = 30;
|
||||
|
||||
// Labels for secret derivation
|
||||
private static final byte[] MASTER = { 'M', 'A', 'S', 'T', 'E', 'R', '\0' };
|
||||
private static final byte[] SALT = { 'S', 'A', 'L', 'T', '\0' };
|
||||
private static final byte[] FIRST = { 'F', 'I', 'R', 'S', 'T', '\0' };
|
||||
private static final byte[] ROTATE = { 'R', 'O', 'T', 'A', 'T', 'E', '\0' };
|
||||
// Label for confirmation code derivation
|
||||
private static final byte[] CODE = { 'C', 'O', 'D', 'E', '\0' };
|
||||
// Label for invitation nonce derivation
|
||||
private static final byte[] NONCE = { 'N', 'O', 'N', 'C', 'E', '\0' };
|
||||
// Labels for key derivation
|
||||
private static final byte[] A_TAG = { 'A', '_', 'T', 'A', 'G', '\0' };
|
||||
private static final byte[] B_TAG = { 'B', '_', 'T', 'A', 'G', '\0' };
|
||||
private static final byte[] A_FRAME =
|
||||
{ 'A', '_', 'F', 'R', 'A', 'M', 'E', '\0' };
|
||||
private static final byte[] B_FRAME =
|
||||
{ 'B', '_', 'F', 'R', 'A', 'M', 'E', '\0' };
|
||||
// KDF label for master key derivation
|
||||
private static final byte[] MASTER = { 'M', 'A', 'S', 'T', 'E', 'R' };
|
||||
// KDF labels for confirmation code derivation
|
||||
private static final byte[] A_CONFIRM =
|
||||
{ 'A', '_', 'C', 'O', 'N', 'F', 'I', 'R', 'M' };
|
||||
private static final byte[] B_CONFIRM =
|
||||
{ 'B', '_', 'C', 'O', 'N', 'F', 'I', 'R', 'M' };
|
||||
// KDF labels for invitation stream header key derivation
|
||||
private static final byte[] A_INVITE =
|
||||
{ 'A', '_', 'I', 'N', 'V', 'I', 'T', 'E' };
|
||||
private static final byte[] B_INVITE =
|
||||
{ 'B', '_', 'I', 'N', 'V', 'I', 'T', 'E' };
|
||||
// KDF labels for signature nonce derivation
|
||||
private static final byte[] A_NONCE = { 'A', '_', 'N', 'O', 'N', 'C', 'E' };
|
||||
private static final byte[] B_NONCE = { 'B', '_', 'N', 'O', 'N', 'C', 'E' };
|
||||
// KDF label for group salt derivation
|
||||
private static final byte[] SALT = { 'S', 'A', 'L', 'T' };
|
||||
// KDF labels for tag key derivation
|
||||
private static final byte[] A_TAG = { 'A', '_', 'T', 'A', 'G' };
|
||||
private static final byte[] B_TAG = { 'B', '_', 'T', 'A', 'G' };
|
||||
// KDF labels for header key derivation
|
||||
private static final byte[] A_HEADER =
|
||||
{ 'A', '_', 'H', 'E', 'A', 'D', 'E', 'R' };
|
||||
private static final byte[] B_HEADER =
|
||||
{ 'B', '_', 'H', 'E', 'A', 'D', 'E', 'R' };
|
||||
// KDF label for key rotation
|
||||
private static final byte[] ROTATE = { 'R', 'O', 'T', 'A', 'T', 'E' };
|
||||
|
||||
private final SecureRandom secureRandom;
|
||||
private final ECKeyPairGenerator agreementKeyPairGenerator;
|
||||
@@ -167,26 +182,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
return ByteUtils.readUint(random, CODE_BITS);
|
||||
}
|
||||
|
||||
public int[] deriveConfirmationCodes(byte[] secret) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
byte[] alice = counterModeKdf(secret, CODE, 0);
|
||||
byte[] bob = counterModeKdf(secret, CODE, 1);
|
||||
int[] codes = new int[2];
|
||||
codes[0] = ByteUtils.readUint(alice, CODE_BITS);
|
||||
codes[1] = ByteUtils.readUint(bob, CODE_BITS);
|
||||
return codes;
|
||||
}
|
||||
|
||||
public byte[][] deriveInvitationNonces(byte[] secret) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
byte[] alice = counterModeKdf(secret, NONCE, 0);
|
||||
byte[] bob = counterModeKdf(secret, NONCE, 1);
|
||||
return new byte[][] { alice, bob };
|
||||
}
|
||||
|
||||
public byte[] deriveMasterSecret(byte[] theirPublicKey,
|
||||
public SecretKey deriveMasterSecret(byte[] theirPublicKey,
|
||||
KeyPair ourKeyPair, boolean alice) throws GeneralSecurityException {
|
||||
MessageDigest messageDigest = getMessageDigest();
|
||||
byte[] ourPublicKey = ourKeyPair.getPublic().getEncoded();
|
||||
@@ -204,9 +200,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
PublicKey theirPub = agreementKeyParser.parsePublicKey(theirPublicKey);
|
||||
// The raw secret comes from the key agreement algorithm
|
||||
byte[] raw = deriveSharedSecret(ourPriv, theirPub);
|
||||
// Derive the cooked secret from the raw secret using the
|
||||
// concatenation KDF
|
||||
return concatenationKdf(raw, MASTER, aliceInfo, bobInfo);
|
||||
// Derive the master secret from the raw secret using the hash KDF
|
||||
return new SecretKey(hashKdf(raw, MASTER, aliceInfo, bobInfo));
|
||||
}
|
||||
|
||||
// Package access for testing
|
||||
@@ -228,46 +223,90 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
return secret;
|
||||
}
|
||||
|
||||
public byte[] deriveGroupSalt(byte[] secret) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
return counterModeKdf(secret, SALT, 0);
|
||||
public int deriveConfirmationCode(SecretKey master, boolean alice) {
|
||||
byte[] b = macKdf(master, alice ? A_CONFIRM : B_CONFIRM);
|
||||
return ByteUtils.readUint(b, CODE_BITS);
|
||||
}
|
||||
|
||||
public byte[] deriveInitialSecret(byte[] secret, int transportIndex) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (transportIndex < 0) throw new IllegalArgumentException();
|
||||
return counterModeKdf(secret, FIRST, transportIndex);
|
||||
public SecretKey deriveInvitationKey(SecretKey master, boolean alice) {
|
||||
return new SecretKey(macKdf(master, alice ? A_INVITE : B_INVITE));
|
||||
}
|
||||
|
||||
public byte[] deriveNextSecret(byte[] secret, long period) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (period < 0 || period > MAX_32_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
return counterModeKdf(secret, ROTATE, period);
|
||||
public byte[] deriveSignatureNonce(SecretKey master, boolean alice) {
|
||||
return macKdf(master, alice ? A_NONCE : B_NONCE);
|
||||
}
|
||||
|
||||
public SecretKey deriveTagKey(byte[] secret, boolean alice) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (alice) return deriveKey(secret, A_TAG, 0);
|
||||
else return deriveKey(secret, B_TAG, 0);
|
||||
public byte[] deriveGroupSalt(SecretKey master) {
|
||||
return macKdf(master, SALT);
|
||||
}
|
||||
|
||||
public SecretKey deriveFrameKey(byte[] secret, long streamNumber,
|
||||
public TransportKeys deriveTransportKeys(TransportId t,
|
||||
SecretKey master, long rotationPeriod, boolean alice) {
|
||||
// Keys for the previous period are derived from the master secret
|
||||
SecretKey inTagPrev = deriveTagKey(master, t, !alice);
|
||||
SecretKey inHeaderPrev = deriveHeaderKey(master, t, !alice);
|
||||
SecretKey outTagPrev = deriveTagKey(master, t, alice);
|
||||
SecretKey outHeaderPrev = deriveHeaderKey(master, t, alice);
|
||||
// Derive the keys for the current and next periods
|
||||
SecretKey inTagCurr = rotateKey(inTagPrev, rotationPeriod);
|
||||
SecretKey inHeaderCurr = rotateKey(inHeaderPrev, rotationPeriod);
|
||||
SecretKey inTagNext = rotateKey(inTagCurr, rotationPeriod + 1);
|
||||
SecretKey inHeaderNext = rotateKey(inHeaderCurr, rotationPeriod + 1);
|
||||
SecretKey outTagCurr = rotateKey(outTagPrev, rotationPeriod);
|
||||
SecretKey outHeaderCurr = rotateKey(outHeaderPrev, rotationPeriod);
|
||||
// Initialise the reordering windows and stream counters
|
||||
IncomingKeys inPrev = new IncomingKeys(inTagPrev, inHeaderPrev,
|
||||
rotationPeriod - 1);
|
||||
IncomingKeys inCurr = new IncomingKeys(inTagCurr, inHeaderCurr,
|
||||
rotationPeriod);
|
||||
IncomingKeys inNext = new IncomingKeys(inTagNext, inHeaderNext,
|
||||
rotationPeriod + 1);
|
||||
OutgoingKeys outCurr = new OutgoingKeys(outTagCurr, outHeaderCurr,
|
||||
rotationPeriod);
|
||||
// Collect and return the keys
|
||||
return new TransportKeys(t, inPrev, inCurr, inNext, outCurr);
|
||||
}
|
||||
|
||||
public TransportKeys rotateTransportKeys(TransportKeys k,
|
||||
long rotationPeriod) {
|
||||
if (k.getRotationPeriod() >= rotationPeriod) return k;
|
||||
IncomingKeys inPrev = k.getPreviousIncomingKeys();
|
||||
IncomingKeys inCurr = k.getCurrentIncomingKeys();
|
||||
IncomingKeys inNext = k.getNextIncomingKeys();
|
||||
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
|
||||
long startPeriod = outCurr.getRotationPeriod();
|
||||
// Rotate the keys
|
||||
for (long p = startPeriod + 1; p <= rotationPeriod; p++) {
|
||||
inPrev = inCurr;
|
||||
inCurr = inNext;
|
||||
SecretKey inNextTag = rotateKey(inNext.getTagKey(), p + 1);
|
||||
SecretKey inNextHeader = rotateKey(inNext.getHeaderKey(), p + 1);
|
||||
inNext = new IncomingKeys(inNextTag, inNextHeader, p);
|
||||
SecretKey outCurrTag = rotateKey(outCurr.getTagKey(), p);
|
||||
SecretKey outCurrHeader = rotateKey(outCurr.getHeaderKey(), p);
|
||||
outCurr = new OutgoingKeys(outCurrTag, outCurrHeader, p);
|
||||
}
|
||||
// Collect and return the keys
|
||||
return new TransportKeys(k.getTransportId(), inPrev, inCurr, inNext,
|
||||
outCurr);
|
||||
}
|
||||
|
||||
private SecretKey rotateKey(SecretKey k, long rotationPeriod) {
|
||||
byte[] period = new byte[4];
|
||||
ByteUtils.writeUint32(rotationPeriod, period, 0);
|
||||
return new SecretKey(macKdf(k, ROTATE, period));
|
||||
}
|
||||
|
||||
private SecretKey deriveTagKey(SecretKey master, TransportId t,
|
||||
boolean alice) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (streamNumber < 0 || streamNumber > MAX_32_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
if (alice) return deriveKey(secret, A_FRAME, streamNumber);
|
||||
else return deriveKey(secret, B_FRAME, streamNumber);
|
||||
byte[] id = StringUtils.toUtf8(t.getString());
|
||||
return new SecretKey(macKdf(master, alice ? A_TAG : B_TAG, id));
|
||||
}
|
||||
|
||||
private SecretKey deriveKey(byte[] secret, byte[] label, long context) {
|
||||
return new SecretKey(counterModeKdf(secret, label, context));
|
||||
private SecretKey deriveHeaderKey(SecretKey master, TransportId t,
|
||||
boolean alice) {
|
||||
byte[] id = StringUtils.toUtf8(t.getString());
|
||||
return new SecretKey(macKdf(master, alice ? A_HEADER : B_HEADER, id));
|
||||
}
|
||||
|
||||
public void encodeTag(byte[] tag, SecretKey tagKey, long streamNumber) {
|
||||
@@ -277,7 +316,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
for (int i = 0; i < TAG_LENGTH; i++) tag[i] = 0;
|
||||
ByteUtils.writeUint32(streamNumber, tag, 0);
|
||||
BlockCipher cipher = new AESLightEngine();
|
||||
assert cipher.getBlockSize() == TAG_LENGTH;
|
||||
if (cipher.getBlockSize() != TAG_LENGTH)
|
||||
throw new IllegalStateException();
|
||||
KeyParameter k = new KeyParameter(tagKey.getBytes());
|
||||
cipher.init(true, k);
|
||||
cipher.processBlock(tag, 0, tag, 0);
|
||||
@@ -348,16 +388,16 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
// Key derivation function based on a hash function - see NIST SP 800-56A,
|
||||
// section 5.8
|
||||
private byte[] concatenationKdf(byte[]... inputs) {
|
||||
private byte[] hashKdf(byte[]... inputs) {
|
||||
// The output of the hash function must be long enough to use as a key
|
||||
MessageDigest messageDigest = getMessageDigest();
|
||||
if (messageDigest.getDigestLength() < SecretKey.LENGTH)
|
||||
throw new RuntimeException();
|
||||
// Each input is length-prefixed - the length must fit in an
|
||||
// unsigned 8-bit integer
|
||||
throw new IllegalStateException();
|
||||
// Calculate the hash over the concatenated length-prefixed inputs
|
||||
byte[] length = new byte[4];
|
||||
for (byte[] input : inputs) {
|
||||
if (input.length > 255) throw new IllegalArgumentException();
|
||||
messageDigest.update((byte) input.length);
|
||||
ByteUtils.writeUint32(input.length, length, 0);
|
||||
messageDigest.update(length);
|
||||
messageDigest.update(input);
|
||||
}
|
||||
byte[] hash = messageDigest.digest();
|
||||
@@ -368,28 +408,24 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
return truncated;
|
||||
}
|
||||
|
||||
// Key derivation function based on a PRF in counter mode - see
|
||||
// Key derivation function based on a pseudo-random function - see
|
||||
// NIST SP 800-108, section 5.1
|
||||
private byte[] counterModeKdf(byte[] secret, byte[] label, long context) {
|
||||
if (secret.length != SecretKey.LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
// The label must be null-terminated
|
||||
if (label[label.length - 1] != '\0')
|
||||
throw new IllegalArgumentException();
|
||||
private byte[] macKdf(SecretKey key, byte[]... inputs) {
|
||||
// Initialise the PRF
|
||||
Mac prf = new HMac(new SHA256Digest());
|
||||
KeyParameter k = new KeyParameter(secret);
|
||||
prf.init(k);
|
||||
int macLength = prf.getMacSize();
|
||||
prf.init(new KeyParameter(key.getBytes()));
|
||||
// The output of the PRF must be long enough to use as a key
|
||||
if (macLength < SecretKey.LENGTH) throw new RuntimeException();
|
||||
int macLength = prf.getMacSize();
|
||||
if (macLength < SecretKey.LENGTH)
|
||||
throw new IllegalStateException();
|
||||
// Calculate the PRF over the concatenated length-prefixed inputs
|
||||
byte[] length = new byte[4];
|
||||
for (byte[] input : inputs) {
|
||||
ByteUtils.writeUint32(input.length, length, 0);
|
||||
prf.update(length, 0, length.length);
|
||||
prf.update(input, 0, input.length);
|
||||
}
|
||||
byte[] mac = new byte[macLength];
|
||||
prf.update((byte) 0); // Counter
|
||||
prf.update(label, 0, label.length); // Null-terminated
|
||||
byte[] contextBytes = new byte[4];
|
||||
ByteUtils.writeUint32(context, contextBytes, 0);
|
||||
prf.update(contextBytes, 0, contextBytes.length);
|
||||
prf.update((byte) SecretKey.LENGTH); // Output length
|
||||
prf.doFinal(mac, 0);
|
||||
// The output is the first SecretKey.LENGTH bytes of the MAC
|
||||
if (mac.length == SecretKey.LENGTH) return mac;
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.io.InputStream;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamDecrypter;
|
||||
import org.briarproject.api.crypto.StreamDecrypterFactory;
|
||||
@@ -13,34 +12,21 @@ import org.briarproject.api.transport.StreamContext;
|
||||
|
||||
class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final Provider<AuthenticatedCipher> cipherProvider;
|
||||
|
||||
@Inject
|
||||
StreamDecrypterFactoryImpl(CryptoComponent crypto,
|
||||
Provider<AuthenticatedCipher> cipherProvider) {
|
||||
this.crypto = crypto;
|
||||
StreamDecrypterFactoryImpl(Provider<AuthenticatedCipher> cipherProvider) {
|
||||
this.cipherProvider = cipherProvider;
|
||||
}
|
||||
|
||||
public StreamDecrypter createStreamDecrypter(InputStream in,
|
||||
StreamContext ctx) {
|
||||
// Derive the frame key
|
||||
byte[] secret = ctx.getSecret();
|
||||
long streamNumber = ctx.getStreamNumber();
|
||||
boolean alice = !ctx.getAlice();
|
||||
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
|
||||
// Create the decrypter
|
||||
AuthenticatedCipher cipher = cipherProvider.get();
|
||||
return new StreamDecrypterImpl(in, cipher, frameKey);
|
||||
return new StreamDecrypterImpl(in, cipher, ctx.getHeaderKey());
|
||||
}
|
||||
|
||||
public StreamDecrypter createInvitationStreamDecrypter(InputStream in,
|
||||
byte[] secret, boolean alice) {
|
||||
// Derive the frame key
|
||||
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
|
||||
// Create the decrypter
|
||||
AuthenticatedCipher cipher = cipherProvider.get();
|
||||
return new StreamDecrypterImpl(in, cipher, frameKey);
|
||||
SecretKey headerKey) {
|
||||
return new StreamDecrypterImpl(in, cipherProvider.get(), headerKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +1,77 @@
|
||||
package org.briarproject.crypto;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamDecrypter;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamDecrypter;
|
||||
|
||||
// FIXME: Implementation is incomplete, doesn't read the stream header
|
||||
class StreamDecrypterImpl implements StreamDecrypter {
|
||||
|
||||
private final InputStream in;
|
||||
private final AuthenticatedCipher frameCipher;
|
||||
private final SecretKey frameKey;
|
||||
private final byte[] iv, header, ciphertext;
|
||||
private final byte[] iv, frameHeader, frameCiphertext;
|
||||
|
||||
private long frameNumber;
|
||||
private boolean finalFrame;
|
||||
|
||||
StreamDecrypterImpl(InputStream in, AuthenticatedCipher frameCipher,
|
||||
SecretKey frameKey) {
|
||||
SecretKey headerKey) {
|
||||
this.in = in;
|
||||
this.frameCipher = frameCipher;
|
||||
this.frameKey = frameKey;
|
||||
this.frameKey = headerKey; // FIXME
|
||||
iv = new byte[IV_LENGTH];
|
||||
header = new byte[HEADER_LENGTH];
|
||||
ciphertext = new byte[MAX_FRAME_LENGTH];
|
||||
frameHeader = new byte[HEADER_LENGTH];
|
||||
frameCiphertext = new byte[MAX_FRAME_LENGTH];
|
||||
frameNumber = 0;
|
||||
finalFrame = false;
|
||||
}
|
||||
|
||||
public int readFrame(byte[] payload) throws IOException {
|
||||
// The buffer must be big enough for a full-size frame
|
||||
if (payload.length < MAX_PAYLOAD_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (finalFrame) return -1;
|
||||
// Read the header
|
||||
// Read the frame header
|
||||
int offset = 0;
|
||||
while (offset < HEADER_LENGTH) {
|
||||
int read = in.read(ciphertext, offset, HEADER_LENGTH - offset);
|
||||
int read = in.read(frameCiphertext, offset, HEADER_LENGTH - offset);
|
||||
if (read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
// Decrypt and authenticate the header
|
||||
// Decrypt and authenticate the frame header
|
||||
FrameEncoder.encodeIv(iv, frameNumber, true);
|
||||
try {
|
||||
frameCipher.init(false, frameKey, iv);
|
||||
int decrypted = frameCipher.process(ciphertext, 0, HEADER_LENGTH,
|
||||
header, 0);
|
||||
int decrypted = frameCipher.process(frameCiphertext, 0,
|
||||
HEADER_LENGTH, frameHeader, 0);
|
||||
if (decrypted != HEADER_LENGTH - MAC_LENGTH)
|
||||
throw new RuntimeException();
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new FormatException();
|
||||
}
|
||||
// Decode and validate the header
|
||||
finalFrame = FrameEncoder.isFinalFrame(header);
|
||||
int payloadLength = FrameEncoder.getPayloadLength(header);
|
||||
int paddingLength = FrameEncoder.getPaddingLength(header);
|
||||
// Decode and validate the frame header
|
||||
finalFrame = FrameEncoder.isFinalFrame(frameHeader);
|
||||
int payloadLength = FrameEncoder.getPayloadLength(frameHeader);
|
||||
int paddingLength = FrameEncoder.getPaddingLength(frameHeader);
|
||||
if (payloadLength + paddingLength > MAX_PAYLOAD_LENGTH)
|
||||
throw new FormatException();
|
||||
// Read the payload and padding
|
||||
int frameLength = HEADER_LENGTH + payloadLength + paddingLength
|
||||
+ MAC_LENGTH;
|
||||
while (offset < frameLength) {
|
||||
int read = in.read(ciphertext, offset, frameLength - offset);
|
||||
int read = in.read(frameCiphertext, offset, frameLength - offset);
|
||||
if (read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
@@ -77,7 +79,7 @@ class StreamDecrypterImpl implements StreamDecrypter {
|
||||
FrameEncoder.encodeIv(iv, frameNumber, false);
|
||||
try {
|
||||
frameCipher.init(false, frameKey, iv);
|
||||
int decrypted = frameCipher.process(ciphertext, HEADER_LENGTH,
|
||||
int decrypted = frameCipher.process(frameCiphertext, HEADER_LENGTH,
|
||||
payloadLength + paddingLength + MAC_LENGTH, payload, 0);
|
||||
if (decrypted != payloadLength + paddingLength)
|
||||
throw new RuntimeException();
|
||||
|
||||
@@ -27,26 +27,15 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
||||
|
||||
public StreamEncrypter createStreamEncrypter(OutputStream out,
|
||||
StreamContext ctx) {
|
||||
byte[] secret = ctx.getSecret();
|
||||
long streamNumber = ctx.getStreamNumber();
|
||||
boolean alice = ctx.getAlice();
|
||||
// Encode the tag
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
SecretKey tagKey = crypto.deriveTagKey(secret, alice);
|
||||
crypto.encodeTag(tag, tagKey, streamNumber);
|
||||
// Derive the frame key
|
||||
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
|
||||
// Create the encrypter
|
||||
crypto.encodeTag(tag, ctx.getTagKey(), ctx.getStreamNumber());
|
||||
AuthenticatedCipher cipher = cipherProvider.get();
|
||||
return new StreamEncrypterImpl(out, cipher, frameKey, tag);
|
||||
return new StreamEncrypterImpl(out, cipher, ctx.getHeaderKey(), tag);
|
||||
}
|
||||
|
||||
public StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
|
||||
byte[] secret, boolean alice) {
|
||||
// Derive the frame key
|
||||
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
|
||||
// Create the encrypter
|
||||
SecretKey headerKey) {
|
||||
AuthenticatedCipher cipher = cipherProvider.get();
|
||||
return new StreamEncrypterImpl(out, cipher, frameKey, null);
|
||||
return new StreamEncrypterImpl(out, cipher, headerKey, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package org.briarproject.crypto;
|
||||
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamEncrypter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
|
||||
@@ -7,32 +14,26 @@ import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamEncrypter;
|
||||
|
||||
// FIXME: Implementation is incomplete, doesn't write the stream header
|
||||
class StreamEncrypterImpl implements StreamEncrypter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final AuthenticatedCipher frameCipher;
|
||||
private final SecretKey frameKey;
|
||||
private final byte[] tag, iv, plaintext, ciphertext;
|
||||
private final byte[] tag, iv, framePlaintext, frameCiphertext;
|
||||
|
||||
private long frameNumber;
|
||||
private boolean writeTag;
|
||||
|
||||
StreamEncrypterImpl(OutputStream out, AuthenticatedCipher frameCipher,
|
||||
SecretKey frameKey, byte[] tag) {
|
||||
SecretKey headerKey, byte[] tag) {
|
||||
this.out = out;
|
||||
this.frameCipher = frameCipher;
|
||||
this.frameKey = frameKey;
|
||||
this.frameKey = headerKey; // FIXME
|
||||
this.tag = tag;
|
||||
iv = new byte[IV_LENGTH];
|
||||
plaintext = new byte[HEADER_LENGTH + MAX_PAYLOAD_LENGTH];
|
||||
ciphertext = new byte[MAX_FRAME_LENGTH];
|
||||
framePlaintext = new byte[HEADER_LENGTH + MAX_PAYLOAD_LENGTH];
|
||||
frameCiphertext = new byte[MAX_FRAME_LENGTH];
|
||||
frameNumber = 0;
|
||||
writeTag = (tag != null);
|
||||
}
|
||||
@@ -48,37 +49,39 @@ class StreamEncrypterImpl implements StreamEncrypter {
|
||||
out.write(tag, 0, tag.length);
|
||||
writeTag = false;
|
||||
}
|
||||
// Encode the header
|
||||
FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength,
|
||||
// Encode the frame header
|
||||
FrameEncoder.encodeHeader(framePlaintext, finalFrame, payloadLength,
|
||||
paddingLength);
|
||||
// Encrypt and authenticate the header
|
||||
// Encrypt and authenticate the frame header
|
||||
FrameEncoder.encodeIv(iv, frameNumber, true);
|
||||
try {
|
||||
frameCipher.init(true, frameKey, iv);
|
||||
int encrypted = frameCipher.process(plaintext, 0,
|
||||
HEADER_LENGTH - MAC_LENGTH, ciphertext, 0);
|
||||
int encrypted = frameCipher.process(framePlaintext, 0,
|
||||
HEADER_LENGTH - MAC_LENGTH, frameCiphertext, 0);
|
||||
if (encrypted != HEADER_LENGTH) throw new RuntimeException();
|
||||
} catch (GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
// Combine the payload and padding
|
||||
System.arraycopy(payload, 0, plaintext, HEADER_LENGTH, payloadLength);
|
||||
System.arraycopy(payload, 0, framePlaintext, HEADER_LENGTH,
|
||||
payloadLength);
|
||||
for (int i = 0; i < paddingLength; i++)
|
||||
plaintext[HEADER_LENGTH + payloadLength + i] = 0;
|
||||
framePlaintext[HEADER_LENGTH + payloadLength + i] = 0;
|
||||
// Encrypt and authenticate the payload and padding
|
||||
FrameEncoder.encodeIv(iv, frameNumber, false);
|
||||
try {
|
||||
frameCipher.init(true, frameKey, iv);
|
||||
int encrypted = frameCipher.process(plaintext, HEADER_LENGTH,
|
||||
payloadLength + paddingLength, ciphertext, HEADER_LENGTH);
|
||||
int encrypted = frameCipher.process(framePlaintext, HEADER_LENGTH,
|
||||
payloadLength + paddingLength, frameCiphertext,
|
||||
HEADER_LENGTH);
|
||||
if (encrypted != payloadLength + paddingLength + MAC_LENGTH)
|
||||
throw new RuntimeException();
|
||||
} catch (GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
// Write the frame
|
||||
out.write(ciphertext, 0, HEADER_LENGTH + payloadLength + paddingLength
|
||||
+ MAC_LENGTH);
|
||||
out.write(frameCiphertext, 0, HEADER_LENGTH + payloadLength
|
||||
+ paddingLength + MAC_LENGTH);
|
||||
frameNumber++;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package org.briarproject.db;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorId;
|
||||
import org.briarproject.api.Contact;
|
||||
@@ -25,8 +21,11 @@ import org.briarproject.api.messaging.SubscriptionAck;
|
||||
import org.briarproject.api.messaging.SubscriptionUpdate;
|
||||
import org.briarproject.api.messaging.TransportAck;
|
||||
import org.briarproject.api.messaging.TransportUpdate;
|
||||
import org.briarproject.api.transport.Endpoint;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
// FIXME: Document the preconditions for calling each method
|
||||
|
||||
@@ -89,13 +88,6 @@ interface Database<T> {
|
||||
ContactId addContact(T txn, Author remote, AuthorId local)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Stores an endpoint.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void addEndpoint(T txn, Endpoint ep) throws DbException;
|
||||
|
||||
/**
|
||||
* Subscribes to a group, or returns false if the user already has the
|
||||
* maximum number of subscriptions.
|
||||
@@ -125,15 +117,6 @@ interface Database<T> {
|
||||
*/
|
||||
void addOfferedMessage(T txn, ContactId c, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given temporary secrets and deletes any secrets that have
|
||||
* been made obsolete.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void addSecrets(T txn, Collection<TemporarySecret> secrets)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Initialises the status of the given message with respect to the given
|
||||
* contact.
|
||||
@@ -154,6 +137,13 @@ interface Database<T> {
|
||||
boolean addTransport(T txn, TransportId t, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given transport keys for a newly added contact.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void addTransportKeys(T txn, ContactId c, TransportKeys k) throws DbException;
|
||||
|
||||
/**
|
||||
* Makes a group visible to the given contact.
|
||||
* <p>
|
||||
@@ -270,13 +260,6 @@ interface Database<T> {
|
||||
*/
|
||||
Collection<ContactId> getContacts(T txn, AuthorId a) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all endpoints.
|
||||
* <p>
|
||||
* Locking: read.
|
||||
*/
|
||||
Collection<Endpoint> getEndpoints(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the amount of free storage space available to the database, in
|
||||
* bytes. This is based on the minimum of the space available on the device
|
||||
@@ -461,13 +444,6 @@ interface Database<T> {
|
||||
RetentionUpdate getRetentionUpdate(T txn, ContactId c, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all temporary secrets.
|
||||
* <p>
|
||||
* Locking: read.
|
||||
*/
|
||||
Collection<TemporarySecret> getSecrets(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all settings.
|
||||
* <p>
|
||||
@@ -509,7 +485,15 @@ interface Database<T> {
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the maximum latencies of all supported transports.
|
||||
* Returns all transport keys for the given transport.
|
||||
* <p>
|
||||
* Locking: read.
|
||||
*/
|
||||
Map<ContactId, TransportKeys> getTransportKeys(T txn, TransportId t)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the maximum latencies in milliseconds of all transports.
|
||||
* <p>
|
||||
* Locking: read.
|
||||
*/
|
||||
@@ -540,14 +524,13 @@ interface Database<T> {
|
||||
Collection<ContactId> getVisibility(T txn, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Increments the outgoing stream counter for the given endpoint in the
|
||||
* given rotation period and returns the old value, or -1 if the counter
|
||||
* does not exist.
|
||||
* Increments the outgoing stream counter for the given contact and
|
||||
* transport in the given rotation period.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
long incrementStreamCounter(T txn, ContactId c, TransportId t, long period)
|
||||
throws DbException;
|
||||
void incrementStreamCounter(T txn, ContactId c, TransportId t,
|
||||
long rotationPeriod) throws DbException;
|
||||
|
||||
/**
|
||||
* Increments the retention time versions for all contacts to indicate that
|
||||
@@ -692,13 +675,13 @@ interface Database<T> {
|
||||
void resetExpiryTime(T txn, ContactId c, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the reordering window for the given endpoint in the given rotation
|
||||
* period.
|
||||
* Sets the reordering window for the given contact and transport in the
|
||||
* given rotation period.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void setReorderingWindow(T txn, ContactId c, TransportId t, long period,
|
||||
long centre, byte[] bitmap) throws DbException;
|
||||
void setReorderingWindow(T txn, ContactId c, TransportId t,
|
||||
long rotationPeriod, long base, byte[] bitmap) throws DbException;
|
||||
|
||||
/**
|
||||
* Updates the groups to which the given contact subscribes and returns
|
||||
@@ -716,7 +699,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
public void setInboxGroup(T txn, ContactId c, Group g) throws DbException;
|
||||
void setInboxGroup(T txn, ContactId c, Group g) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks a message as read or unread.
|
||||
@@ -798,4 +781,12 @@ interface Database<T> {
|
||||
*/
|
||||
void updateExpiryTime(T txn, ContactId c, MessageId m, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given transport keys, deleting any keys they have replaced.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void updateTransportKeys(T txn, Map<ContactId, TransportKeys> keys)
|
||||
throws DbException;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,5 @@
|
||||
package org.briarproject.db;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.db.DatabaseConstants.BYTES_PER_SWEEP;
|
||||
import static org.briarproject.db.DatabaseConstants.CRITICAL_FREE_SPACE;
|
||||
import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
|
||||
import static org.briarproject.db.DatabaseConstants.MAX_TRANSACTIONS_BETWEEN_SPACE_CHECKS;
|
||||
import static org.briarproject.db.DatabaseConstants.MIN_FREE_SPACE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorId;
|
||||
import org.briarproject.api.Contact;
|
||||
@@ -75,8 +55,29 @@ import org.briarproject.api.messaging.SubscriptionAck;
|
||||
import org.briarproject.api.messaging.SubscriptionUpdate;
|
||||
import org.briarproject.api.messaging.TransportAck;
|
||||
import org.briarproject.api.messaging.TransportUpdate;
|
||||
import org.briarproject.api.transport.Endpoint;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.db.DatabaseConstants.BYTES_PER_SWEEP;
|
||||
import static org.briarproject.db.DatabaseConstants.CRITICAL_FREE_SPACE;
|
||||
import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
|
||||
import static org.briarproject.db.DatabaseConstants.MAX_TRANSACTIONS_BETWEEN_SPACE_CHECKS;
|
||||
import static org.briarproject.db.DatabaseConstants.MIN_FREE_SPACE;
|
||||
|
||||
/**
|
||||
* An implementation of DatabaseComponent using reentrant read-write locks.
|
||||
@@ -85,7 +86,7 @@ import org.briarproject.api.transport.TemporarySecret;
|
||||
* implementation is safe on a given JVM.
|
||||
*/
|
||||
class DatabaseComponentImpl<T> implements DatabaseComponent,
|
||||
DatabaseCleaner.Callback {
|
||||
DatabaseCleaner.Callback {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(DatabaseComponentImpl.class.getName());
|
||||
@@ -180,26 +181,6 @@ DatabaseCleaner.Callback {
|
||||
return c;
|
||||
}
|
||||
|
||||
public void addEndpoint(Endpoint ep) throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsContact(txn, ep.getContactId()))
|
||||
throw new NoSuchContactException();
|
||||
if (!db.containsTransport(txn, ep.getTransportId()))
|
||||
throw new NoSuchTransportException();
|
||||
db.addEndpoint(txn, ep);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addGroup(Group g) throws DbException {
|
||||
boolean added = false;
|
||||
lock.writeLock().lock();
|
||||
@@ -290,30 +271,6 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public void addSecrets(Collection<TemporarySecret> secrets)
|
||||
throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Collection<TemporarySecret> relevant =
|
||||
new ArrayList<TemporarySecret>();
|
||||
for (TemporarySecret s : secrets) {
|
||||
if (db.containsContact(txn, s.getContactId()))
|
||||
if (db.containsTransport(txn, s.getTransportId()))
|
||||
relevant.add(s);
|
||||
}
|
||||
if (!secrets.isEmpty()) db.addSecrets(txn, relevant);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addTransport(TransportId t, int maxLatency)
|
||||
throws DbException {
|
||||
boolean added;
|
||||
@@ -334,6 +291,27 @@ DatabaseCleaner.Callback {
|
||||
return added;
|
||||
}
|
||||
|
||||
public void addTransportKeys(ContactId c, TransportKeys k)
|
||||
throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
if (!db.containsTransport(txn, k.getTransportId()))
|
||||
throw new NoSuchTransportException();
|
||||
db.addTransportKeys(txn, c, k);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Ack generateAck(ContactId c, int maxMessages) throws DbException {
|
||||
Collection<MessageId> ids;
|
||||
lock.writeLock().lock();
|
||||
@@ -883,23 +861,6 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<TemporarySecret> getSecrets() throws DbException {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Collection<TemporarySecret> secrets = db.getSecrets(txn);
|
||||
db.commitTransaction(txn);
|
||||
return secrets;
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Settings getSettings() throws DbException {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
@@ -934,6 +895,27 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<ContactId, TransportKeys> getTransportKeys(TransportId t)
|
||||
throws DbException {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
Map<ContactId, TransportKeys> keys =
|
||||
db.getTransportKeys(txn, t);
|
||||
db.commitTransaction(txn);
|
||||
return keys;
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<TransportId, Integer> getTransportLatencies()
|
||||
throws DbException {
|
||||
lock.readLock().lock();
|
||||
@@ -989,8 +971,8 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public long incrementStreamCounter(ContactId c, TransportId t,
|
||||
long period) throws DbException {
|
||||
public void incrementStreamCounter(ContactId c, TransportId t,
|
||||
long rotationPeriod) throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
@@ -999,9 +981,8 @@ DatabaseCleaner.Callback {
|
||||
throw new NoSuchContactException();
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
long counter = db.incrementStreamCounter(txn, c, t, period);
|
||||
db.incrementStreamCounter(txn, c, t, rotationPeriod);
|
||||
db.commitTransaction(txn);
|
||||
return counter;
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
@@ -1404,27 +1385,6 @@ DatabaseCleaner.Callback {
|
||||
eventBus.broadcast(new TransportRemovedEvent(t));
|
||||
}
|
||||
|
||||
public void setReorderingWindow(ContactId c, TransportId t, long period,
|
||||
long centre, byte[] bitmap) throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.setReorderingWindow(txn, c, t, period, centre, bitmap);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void setInboxGroup(ContactId c, Group g) throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
@@ -1480,6 +1440,27 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public void setReorderingWindow(ContactId c, TransportId t,
|
||||
long rotationPeriod, long base, byte[] bitmap) throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.setReorderingWindow(txn, c, t, rotationPeriod, base, bitmap);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void setVisibility(GroupId g, Collection<ContactId> visible)
|
||||
throws DbException {
|
||||
Collection<ContactId> affected = new ArrayList<ContactId>();
|
||||
@@ -1552,6 +1533,33 @@ DatabaseCleaner.Callback {
|
||||
eventBus.broadcast(new LocalSubscriptionsUpdatedEvent(affected));
|
||||
}
|
||||
|
||||
public void updateTransportKeys(Map<ContactId, TransportKeys> keys)
|
||||
throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<ContactId, TransportKeys> filtered =
|
||||
new HashMap<ContactId, TransportKeys>();
|
||||
for (Entry<ContactId, TransportKeys> e : keys.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
TransportKeys k = e.getValue();
|
||||
if (db.containsContact(txn, c)
|
||||
&& db.containsTransport(txn, k.getTransportId())) {
|
||||
filtered.put(c, k);
|
||||
}
|
||||
}
|
||||
db.updateTransportKeys(txn, filtered);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void checkFreeSpaceAndClean() throws DbException {
|
||||
long freeSpace = db.getFreeSpace();
|
||||
if (LOG.isLoggable(INFO)) LOG.info(freeSpace + " bytes free space");
|
||||
|
||||
@@ -1,14 +1,33 @@
|
||||
package org.briarproject.db;
|
||||
|
||||
import static java.sql.Types.BINARY;
|
||||
import static java.sql.Types.VARCHAR;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.Author.Status.ANONYMOUS;
|
||||
import static org.briarproject.api.Author.Status.UNKNOWN;
|
||||
import static org.briarproject.api.Author.Status.VERIFIED;
|
||||
import static org.briarproject.api.messaging.MessagingConstants.MAX_SUBSCRIPTIONS;
|
||||
import static org.briarproject.api.messaging.MessagingConstants.RETENTION_GRANULARITY;
|
||||
import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorId;
|
||||
import org.briarproject.api.Contact;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.Settings;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DbClosedException;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.MessageHeader;
|
||||
import org.briarproject.api.db.MessageHeader.State;
|
||||
import org.briarproject.api.messaging.Group;
|
||||
import org.briarproject.api.messaging.GroupId;
|
||||
import org.briarproject.api.messaging.Message;
|
||||
import org.briarproject.api.messaging.MessageId;
|
||||
import org.briarproject.api.messaging.RetentionAck;
|
||||
import org.briarproject.api.messaging.RetentionUpdate;
|
||||
import org.briarproject.api.messaging.SubscriptionAck;
|
||||
import org.briarproject.api.messaging.SubscriptionUpdate;
|
||||
import org.briarproject.api.messaging.TransportAck;
|
||||
import org.briarproject.api.messaging.TransportUpdate;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.IncomingKeys;
|
||||
import org.briarproject.api.transport.OutgoingKeys;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
@@ -32,32 +51,15 @@ import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorId;
|
||||
import org.briarproject.api.Contact;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.Settings;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.db.DbClosedException;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.MessageHeader;
|
||||
import org.briarproject.api.db.MessageHeader.State;
|
||||
import org.briarproject.api.messaging.Group;
|
||||
import org.briarproject.api.messaging.GroupId;
|
||||
import org.briarproject.api.messaging.Message;
|
||||
import org.briarproject.api.messaging.MessageId;
|
||||
import org.briarproject.api.messaging.RetentionAck;
|
||||
import org.briarproject.api.messaging.RetentionUpdate;
|
||||
import org.briarproject.api.messaging.SubscriptionAck;
|
||||
import org.briarproject.api.messaging.SubscriptionUpdate;
|
||||
import org.briarproject.api.messaging.TransportAck;
|
||||
import org.briarproject.api.messaging.TransportUpdate;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.Endpoint;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
import static java.sql.Types.BINARY;
|
||||
import static java.sql.Types.VARCHAR;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.Author.Status.ANONYMOUS;
|
||||
import static org.briarproject.api.Author.Status.UNKNOWN;
|
||||
import static org.briarproject.api.Author.Status.VERIFIED;
|
||||
import static org.briarproject.api.messaging.MessagingConstants.MAX_SUBSCRIPTIONS;
|
||||
import static org.briarproject.api.messaging.MessagingConstants.RETENTION_GRANULARITY;
|
||||
import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
|
||||
|
||||
/**
|
||||
* A generic database implementation that can be used with any JDBC-compatible
|
||||
@@ -65,8 +67,8 @@ import org.briarproject.api.transport.TemporarySecret;
|
||||
*/
|
||||
abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
private static final int SCHEMA_VERSION = 9;
|
||||
private static final int MIN_SCHEMA_VERSION = 9;
|
||||
private static final int SCHEMA_VERSION = 10;
|
||||
private static final int MIN_SCHEMA_VERSION = 10;
|
||||
|
||||
private static final String CREATE_SETTINGS =
|
||||
"CREATE TABLE settings"
|
||||
@@ -277,13 +279,16 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " REFERENCES contacts (contactId)"
|
||||
+ " ON DELETE CASCADE)";
|
||||
|
||||
private static final String CREATE_ENDPOINTS =
|
||||
"CREATE TABLE endpoints"
|
||||
private static final String CREATE_INCOMING_KEYS =
|
||||
"CREATE TABLE incomingKeys"
|
||||
+ " (contactId INT NOT NULL,"
|
||||
+ " transportId VARCHAR NOT NULL,"
|
||||
+ " epoch BIGINT NOT NULL,"
|
||||
+ " alice BOOLEAN NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId, transportId),"
|
||||
+ " period BIGINT NOT NULL,"
|
||||
+ " tagKey SECRET NOT NULL,"
|
||||
+ " headerKey SECRET NOT NULL,"
|
||||
+ " base BIGINT NOT NULL,"
|
||||
+ " bitmap BINARY NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId, transportId, period),"
|
||||
+ " FOREIGN KEY (contactId)"
|
||||
+ " REFERENCES contacts (contactId)"
|
||||
+ " ON DELETE CASCADE,"
|
||||
@@ -291,16 +296,15 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " REFERENCES transports (transportId)"
|
||||
+ " ON DELETE CASCADE)";
|
||||
|
||||
private static final String CREATE_SECRETS =
|
||||
"CREATE TABLE secrets"
|
||||
private static final String CREATE_OUTGOING_KEYS =
|
||||
"CREATE TABLE outgoingKeys"
|
||||
+ " (contactId INT NOT NULL,"
|
||||
+ " transportId VARCHAR NOT NULL,"
|
||||
+ " period BIGINT NOT NULL,"
|
||||
+ " secret SECRET NOT NULL,"
|
||||
+ " outgoing BIGINT NOT NULL,"
|
||||
+ " centre BIGINT NOT NULL,"
|
||||
+ " bitmap BINARY NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId, transportId, period),"
|
||||
+ " tagKey SECRET NOT NULL,"
|
||||
+ " headerKey SECRET NOT NULL,"
|
||||
+ " stream BIGINT NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId, transportId),"
|
||||
+ " FOREIGN KEY (contactId)"
|
||||
+ " REFERENCES contacts (contactId)"
|
||||
+ " ON DELETE CASCADE,"
|
||||
@@ -324,6 +328,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
private boolean closed = false; // Locking: connectionsLock
|
||||
|
||||
protected abstract Connection createConnection() throws SQLException;
|
||||
|
||||
protected abstract void flushBuffersToDisk(Statement s) throws SQLException;
|
||||
|
||||
private final Lock connectionsLock = new ReentrantLock();
|
||||
@@ -339,7 +344,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
|
||||
protected void open(String driverClass, boolean reopen) throws DbException,
|
||||
IOException {
|
||||
IOException {
|
||||
// Load the JDBC driver
|
||||
try {
|
||||
Class.forName(driverClass);
|
||||
@@ -382,7 +387,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
try {
|
||||
if (rs != null) rs.close();
|
||||
} catch (SQLException e) {
|
||||
if (LOG.isLoggable(WARNING))LOG.log(WARNING, e.toString(), e);
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,7 +395,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
try {
|
||||
if (s != null) s.close();
|
||||
} catch (SQLException e) {
|
||||
if (LOG.isLoggable(WARNING))LOG.log(WARNING, e.toString(), e);
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -418,8 +423,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
s.executeUpdate(insertTypeNames(CREATE_TRANSPORT_VERSIONS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_CONTACT_TRANSPORT_PROPS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_CONTACT_TRANSPORT_VERSIONS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_ENDPOINTS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_SECRETS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_INCOMING_KEYS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_OUTGOING_KEYS));
|
||||
s.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(s);
|
||||
@@ -480,7 +485,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
try {
|
||||
txn.close();
|
||||
} catch (SQLException e1) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e1.toString(), e1);
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e1.toString(), e1);
|
||||
}
|
||||
// Whatever happens, allow the database to close
|
||||
connectionsLock.lock();
|
||||
@@ -679,26 +685,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void addEndpoint(Connection txn, Endpoint ep) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "INSERT INTO endpoints"
|
||||
+ " (contactId, transportId, epoch, alice)"
|
||||
+ " VALUES (?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, ep.getContactId().getInt());
|
||||
ps.setString(2, ep.getTransportId().getString());
|
||||
ps.setLong(3, ep.getEpoch());
|
||||
ps.setBoolean(4, ep.getAlice());
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addGroup(Connection txn, Group g) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -824,52 +810,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void addSecrets(Connection txn, Collection<TemporarySecret> secrets)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Store the new secrets
|
||||
String sql = "INSERT INTO secrets (contactId, transportId, period,"
|
||||
+ " secret, outgoing, centre, bitmap)"
|
||||
+ " VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
for (TemporarySecret s : secrets) {
|
||||
ps.setInt(1, s.getContactId().getInt());
|
||||
ps.setString(2, s.getTransportId().getString());
|
||||
ps.setLong(3, s.getPeriod());
|
||||
ps.setBytes(4, s.getSecret());
|
||||
ps.setLong(5, s.getOutgoingStreamCounter());
|
||||
ps.setLong(6, s.getWindowCentre());
|
||||
ps.setBytes(7, s.getWindowBitmap());
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != secrets.size())
|
||||
throw new DbStateException();
|
||||
for (int i = 0; i < batchAffected.length; i++) {
|
||||
if (batchAffected[i] != 1) throw new DbStateException();
|
||||
}
|
||||
ps.close();
|
||||
// Delete any obsolete secrets
|
||||
sql = "DELETE FROM secrets"
|
||||
+ " WHERE contactId = ? AND transportId = ? AND period < ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
for (TemporarySecret s : secrets) {
|
||||
ps.setInt(1, s.getContactId().getInt());
|
||||
ps.setString(2, s.getTransportId().getString());
|
||||
ps.setLong(3, s.getPeriod() - 2);
|
||||
ps.addBatch();
|
||||
}
|
||||
batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != secrets.size())
|
||||
throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addStatus(Connection txn, ContactId c, MessageId m, boolean ack,
|
||||
boolean seen) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -947,6 +887,68 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void addTransportKeys(Connection txn, ContactId c, TransportKeys k)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Store the incoming keys
|
||||
String sql = "INSERT INTO incomingKeys (contactId, transportId,"
|
||||
+ " period, tagKey, headerKey, base, bitmap)"
|
||||
+ " VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setString(2, k.getTransportId().getString());
|
||||
// Previous rotation period
|
||||
IncomingKeys inPrev = k.getPreviousIncomingKeys();
|
||||
ps.setLong(3, inPrev.getRotationPeriod());
|
||||
ps.setBytes(4, inPrev.getTagKey().getBytes());
|
||||
ps.setBytes(5, inPrev.getHeaderKey().getBytes());
|
||||
ps.setLong(6, inPrev.getWindowBase());
|
||||
ps.setBytes(7, inPrev.getWindowBitmap());
|
||||
ps.addBatch();
|
||||
// Current rotation period
|
||||
IncomingKeys inCurr = k.getCurrentIncomingKeys();
|
||||
ps.setLong(3, inCurr.getRotationPeriod());
|
||||
ps.setBytes(4, inCurr.getTagKey().getBytes());
|
||||
ps.setBytes(5, inCurr.getHeaderKey().getBytes());
|
||||
ps.setLong(6, inCurr.getWindowBase());
|
||||
ps.setBytes(7, inCurr.getWindowBitmap());
|
||||
ps.addBatch();
|
||||
// Next rotation period
|
||||
IncomingKeys inNext = k.getNextIncomingKeys();
|
||||
ps.setLong(3, inNext.getRotationPeriod());
|
||||
ps.setBytes(4, inNext.getTagKey().getBytes());
|
||||
ps.setBytes(5, inNext.getHeaderKey().getBytes());
|
||||
ps.setLong(6, inNext.getWindowBase());
|
||||
ps.setBytes(7, inNext.getWindowBitmap());
|
||||
ps.addBatch();
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != 3) throw new DbStateException();
|
||||
for (int i = 0; i < batchAffected.length; i++) {
|
||||
if (batchAffected[i] != 1) throw new DbStateException();
|
||||
}
|
||||
ps.close();
|
||||
// Store the outgoing keys
|
||||
sql = "INSERT INTO outgoingKeys (contactId, transportId, period,"
|
||||
+ " tagKey, headerKey, stream)"
|
||||
+ " VALUES (?, ?, ?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setString(2, k.getTransportId().getString());
|
||||
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
|
||||
ps.setLong(3, outCurr.getRotationPeriod());
|
||||
ps.setBytes(4, outCurr.getTagKey().getBytes());
|
||||
ps.setBytes(5, outCurr.getHeaderKey().getBytes());
|
||||
ps.setLong(6, outCurr.getStreamCounter());
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addVisibility(Connection txn, ContactId c, GroupId g)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -1326,32 +1328,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Endpoint> getEndpoints(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId, transportId, epoch, alice"
|
||||
+ " FROM endpoints";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
List<Endpoint> endpoints = new ArrayList<Endpoint>();
|
||||
while (rs.next()) {
|
||||
ContactId contactId = new ContactId(rs.getInt(1));
|
||||
TransportId transportId = new TransportId(rs.getString(2));
|
||||
long epoch = rs.getLong(3);
|
||||
boolean alice = rs.getBoolean(4);
|
||||
endpoints.add(new Endpoint(contactId, transportId, epoch,
|
||||
alice));
|
||||
}
|
||||
return Collections.unmodifiableList(endpoints);
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Group getGroup(Connection txn, GroupId g) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -2098,43 +2074,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<TemporarySecret> getSecrets(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT e.contactId, e.transportId, epoch, alice,"
|
||||
+ " period, secret, outgoing, centre, bitmap"
|
||||
+ " FROM endpoints AS e"
|
||||
+ " JOIN secrets AS s"
|
||||
+ " ON e.contactId = s.contactId"
|
||||
+ " AND e.transportId = s.transportId";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
List<TemporarySecret> secrets = new ArrayList<TemporarySecret>();
|
||||
while (rs.next()) {
|
||||
ContactId contactId = new ContactId(rs.getInt(1));
|
||||
TransportId transportId = new TransportId(rs.getString(2));
|
||||
long epoch = rs.getLong(3);
|
||||
boolean alice = rs.getBoolean(4);
|
||||
long period = rs.getLong(5);
|
||||
byte[] secret = rs.getBytes(6);
|
||||
long outgoing = rs.getLong(7);
|
||||
long centre = rs.getLong(8);
|
||||
byte[] bitmap = rs.getBytes(9);
|
||||
secrets.add(new TemporarySecret(contactId, transportId, epoch,
|
||||
alice, period, secret, outgoing, centre, bitmap));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return Collections.unmodifiableList(secrets);
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Settings getSettings(Connection txn) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -2317,6 +2256,67 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<ContactId, TransportKeys> getTransportKeys(Connection txn,
|
||||
TransportId t) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
// Retrieve the incoming keys
|
||||
String sql = "SELECT period, tagKey, headerKey, base, bitmap"
|
||||
+ " FROM incomingKeys"
|
||||
+ " WHERE transportId = ?"
|
||||
+ " ORDER BY contactId, period";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, t.getString());
|
||||
rs = ps.executeQuery();
|
||||
List<IncomingKeys> inKeys = new ArrayList<IncomingKeys>();
|
||||
while (rs.next()) {
|
||||
long rotationPeriod = rs.getLong(1);
|
||||
SecretKey tagKey = new SecretKey(rs.getBytes(2));
|
||||
SecretKey headerKey = new SecretKey(rs.getBytes(3));
|
||||
long windowBase = rs.getLong(4);
|
||||
byte[] windowBitmap = rs.getBytes(5);
|
||||
inKeys.add(new IncomingKeys(tagKey, headerKey, rotationPeriod,
|
||||
windowBase, windowBitmap));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
// Retrieve the outgoing keys in the same order
|
||||
sql = "SELECT contactId, period, tagKey, headerKey, stream"
|
||||
+ " FROM outgoingKeys"
|
||||
+ " WHERE transportId = ?"
|
||||
+ " ORDER BY contactId, period";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, t.getString());
|
||||
rs = ps.executeQuery();
|
||||
Map<ContactId, TransportKeys> keys =
|
||||
new HashMap<ContactId, TransportKeys>();
|
||||
for (int i = 0; rs.next(); i++) {
|
||||
// There should be three times as many incoming keys
|
||||
if (inKeys.size() < (i + 1) * 3) throw new DbStateException();
|
||||
ContactId contactId = new ContactId(rs.getInt(1));
|
||||
long rotationPeriod = rs.getLong(2);
|
||||
SecretKey tagKey = new SecretKey(rs.getBytes(3));
|
||||
SecretKey headerKey = new SecretKey(rs.getBytes(4));
|
||||
long streamCounter = rs.getLong(5);
|
||||
OutgoingKeys outCurr = new OutgoingKeys(tagKey, headerKey,
|
||||
rotationPeriod, streamCounter);
|
||||
IncomingKeys inPrev = inKeys.get(i * 3);
|
||||
IncomingKeys inCurr = inKeys.get(i * 3 + 1);
|
||||
IncomingKeys inNext = inKeys.get(i * 3 + 2);
|
||||
keys.put(contactId, new TransportKeys(t, inPrev, inCurr,
|
||||
inNext, outCurr));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return Collections.unmodifiableMap(keys);
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<TransportId, Integer> getTransportLatencies(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -2327,7 +2327,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
rs = ps.executeQuery();
|
||||
Map<TransportId, Integer> latencies =
|
||||
new HashMap<TransportId, Integer>();
|
||||
while (rs.next()){
|
||||
while (rs.next()) {
|
||||
TransportId id = new TransportId(rs.getString(1));
|
||||
latencies.put(id, rs.getInt(2));
|
||||
}
|
||||
@@ -2392,7 +2392,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps.setString(3, u.getId().getString());
|
||||
ps.addBatch();
|
||||
}
|
||||
int [] batchAffected = ps.executeBatch();
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != updates.size())
|
||||
throw new DbStateException();
|
||||
for (i = 0; i < batchAffected.length; i++) {
|
||||
@@ -2455,42 +2455,21 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public long incrementStreamCounter(Connection txn, ContactId c,
|
||||
TransportId t, long period) throws DbException {
|
||||
public void incrementStreamCounter(Connection txn, ContactId c,
|
||||
TransportId t, long rotationPeriod) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
// Get the current stream counter
|
||||
String sql = "SELECT outgoing FROM secrets"
|
||||
String sql = "UPDATE outgoingKeys SET stream = stream + 1"
|
||||
+ " WHERE contactId = ? AND transportId = ? AND period = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setString(2, t.getString());
|
||||
ps.setLong(3, period);
|
||||
rs = ps.executeQuery();
|
||||
if (!rs.next()) {
|
||||
rs.close();
|
||||
ps.close();
|
||||
return -1;
|
||||
}
|
||||
long streamNumber = rs.getLong(1);
|
||||
if (rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
// Increment the stream counter
|
||||
sql = "UPDATE secrets SET outgoing = outgoing + 1"
|
||||
+ " WHERE contactId = ? AND transportId = ? AND period = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setString(2, t.getString());
|
||||
ps.setLong(3, period);
|
||||
ps.setLong(3, rotationPeriod);
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
return streamNumber;
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
tryToClose(rs);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
@@ -2929,18 +2908,19 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setReorderingWindow(Connection txn, ContactId c, TransportId t,
|
||||
long period, long centre, byte[] bitmap) throws DbException {
|
||||
long rotationPeriod, long base, byte[] bitmap) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "UPDATE secrets SET centre = ?, bitmap = ?"
|
||||
String sql = "UPDATE incomingKeys SET base = ?, bitmap = ?"
|
||||
+ " WHERE contactId = ? AND transportId = ? AND period = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setLong(1, centre);
|
||||
ps.setLong(1, base);
|
||||
ps.setBytes(2, bitmap);
|
||||
ps.setInt(3, c.getInt());
|
||||
ps.setString(4, t.getString());
|
||||
ps.setLong(5, period);
|
||||
ps.setLong(5, rotationPeriod);
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected < 0 || affected > 1) throw new DbStateException();
|
||||
ps.close();
|
||||
@@ -3139,7 +3119,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
public boolean setRemoteProperties(Connection txn, ContactId c,
|
||||
TransportId t, TransportProperties p, long version)
|
||||
throws DbException {
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
@@ -3354,4 +3334,46 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateTransportKeys(Connection txn,
|
||||
Map<ContactId, TransportKeys> keys) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Delete any existing incoming keys
|
||||
String sql = "DELETE FROM incomingKeys"
|
||||
+ " WHERE contactId = ?"
|
||||
+ " AND transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
for (Entry<ContactId, TransportKeys> e : keys.entrySet()) {
|
||||
ps.setInt(1, e.getKey().getInt());
|
||||
ps.setString(2, e.getValue().getTransportId().getString());
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != keys.size())
|
||||
throw new DbStateException();
|
||||
ps.close();
|
||||
// Delete any existing outgoing keys
|
||||
sql = "DELETE FROM outgoingKeys"
|
||||
+ " WHERE contactId = ?"
|
||||
+ " AND transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
for (Entry<ContactId, TransportKeys> e : keys.entrySet()) {
|
||||
ps.setInt(1, e.getKey().getInt());
|
||||
ps.setString(2, e.getValue().getTransportId().getString());
|
||||
ps.addBatch();
|
||||
}
|
||||
batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != keys.size())
|
||||
throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
// Store the new keys
|
||||
for (Entry<ContactId, TransportKeys> e : keys.entrySet()) {
|
||||
addTransportKeys(txn, e.getKey(), e.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
package org.briarproject.invitation;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
@@ -29,9 +19,20 @@ import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
/** A connection thread for the peer being Alice in the invitation protocol. */
|
||||
class AliceConnector extends Connector {
|
||||
|
||||
@@ -49,9 +50,9 @@ class AliceConnector extends Connector {
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super(crypto, db, readerFactory, writerFactory, streamReaderFactory,
|
||||
streamWriterFactory, authorFactory, groupFactory, keyManager,
|
||||
connectionManager, clock, reuseConnection, group, plugin,
|
||||
localAuthor, localProps, random);
|
||||
streamWriterFactory, authorFactory, groupFactory,
|
||||
keyManager, connectionManager, clock, reuseConnection, group,
|
||||
plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,7 +72,7 @@ class AliceConnector extends Connector {
|
||||
OutputStream out;
|
||||
Reader r;
|
||||
Writer w;
|
||||
byte[] secret;
|
||||
SecretKey master;
|
||||
try {
|
||||
in = conn.getReader().getInputStream();
|
||||
out = conn.getWriter().getOutputStream();
|
||||
@@ -82,7 +83,7 @@ class AliceConnector extends Connector {
|
||||
byte[] hash = receivePublicKeyHash(r);
|
||||
sendPublicKey(w);
|
||||
byte[] key = receivePublicKey(r);
|
||||
secret = deriveMasterSecret(hash, key, true);
|
||||
master = deriveMasterSecret(hash, key, true);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
group.keyAgreementFailed();
|
||||
@@ -96,8 +97,8 @@ class AliceConnector extends Connector {
|
||||
}
|
||||
// The key agreement succeeded - derive the confirmation codes
|
||||
if (LOG.isLoggable(INFO)) LOG.info(pluginName + " agreement succeeded");
|
||||
int[] codes = crypto.deriveConfirmationCodes(secret);
|
||||
int aliceCode = codes[0], bobCode = codes[1];
|
||||
int aliceCode = crypto.deriveConfirmationCode(master, true);
|
||||
int bobCode = crypto.deriveConfirmationCode(master, false);
|
||||
group.keyAgreementSucceeded(aliceCode, bobCode);
|
||||
// Exchange confirmation results
|
||||
boolean localMatched, remoteMatched;
|
||||
@@ -130,19 +131,22 @@ class AliceConnector extends Connector {
|
||||
// Confirmation succeeded - upgrade to a secure connection
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info(pluginName + " confirmation succeeded");
|
||||
// Derive the header keys
|
||||
SecretKey aliceHeaderKey = crypto.deriveInvitationKey(master, true);
|
||||
SecretKey bobHeaderKey = crypto.deriveInvitationKey(master, false);
|
||||
// Create the readers
|
||||
InputStream streamReader =
|
||||
streamReaderFactory.createInvitationStreamReader(in,
|
||||
secret, false); // Bob's stream
|
||||
bobHeaderKey);
|
||||
r = readerFactory.createReader(streamReader);
|
||||
// Create the writers
|
||||
OutputStream streamWriter =
|
||||
streamWriterFactory.createInvitationStreamWriter(out,
|
||||
secret, true); // Alice's stream
|
||||
aliceHeaderKey);
|
||||
w = writerFactory.createWriter(streamWriter);
|
||||
// Derive the invitation nonces
|
||||
byte[][] nonces = crypto.deriveInvitationNonces(secret);
|
||||
byte[] aliceNonce = nonces[0], bobNonce = nonces[1];
|
||||
byte[] aliceNonce = crypto.deriveSignatureNonce(master, true);
|
||||
byte[] bobNonce = crypto.deriveSignatureNonce(master, false);
|
||||
// Exchange pseudonyms, signed nonces, timestamps and transports
|
||||
Author remoteAuthor;
|
||||
long remoteTimestamp;
|
||||
@@ -171,11 +175,11 @@ class AliceConnector extends Connector {
|
||||
tryToClose(conn, true);
|
||||
return;
|
||||
}
|
||||
// The epoch is the minimum of the peers' timestamps
|
||||
long epoch = Math.min(localTimestamp, remoteTimestamp);
|
||||
// The agreed timestamp is the minimum of the peers' timestamps
|
||||
long timestamp = Math.min(localTimestamp, remoteTimestamp);
|
||||
// Add the contact and store the transports
|
||||
try {
|
||||
addContact(remoteAuthor, remoteProps, secret, epoch, true);
|
||||
addContact(remoteAuthor, remoteProps, master, timestamp, true);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
tryToClose(conn, true);
|
||||
@@ -190,4 +194,4 @@ class AliceConnector extends Connector {
|
||||
LOG.info(pluginName + " pseudonym exchange succeeded");
|
||||
group.pseudonymExchangeSucceeded(remoteAuthor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
package org.briarproject.invitation;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
@@ -29,9 +19,20 @@ import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
/** A connection thread for the peer being Bob in the invitation protocol. */
|
||||
class BobConnector extends Connector {
|
||||
|
||||
@@ -49,9 +50,9 @@ class BobConnector extends Connector {
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super(crypto, db, readerFactory, writerFactory, streamReaderFactory,
|
||||
streamWriterFactory, authorFactory, groupFactory, keyManager,
|
||||
connectionManager, clock, reuseConnection, group, plugin,
|
||||
localAuthor, localProps, random);
|
||||
streamWriterFactory, authorFactory, groupFactory,
|
||||
keyManager, connectionManager, clock, reuseConnection, group,
|
||||
plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,7 +66,7 @@ class BobConnector extends Connector {
|
||||
OutputStream out;
|
||||
Reader r;
|
||||
Writer w;
|
||||
byte[] secret;
|
||||
SecretKey master;
|
||||
try {
|
||||
in = conn.getReader().getInputStream();
|
||||
out = conn.getWriter().getOutputStream();
|
||||
@@ -82,7 +83,7 @@ class BobConnector extends Connector {
|
||||
sendPublicKeyHash(w);
|
||||
byte[] key = receivePublicKey(r);
|
||||
sendPublicKey(w);
|
||||
secret = deriveMasterSecret(hash, key, false);
|
||||
master = deriveMasterSecret(hash, key, false);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
group.keyAgreementFailed();
|
||||
@@ -96,8 +97,8 @@ class BobConnector extends Connector {
|
||||
}
|
||||
// The key agreement succeeded - derive the confirmation codes
|
||||
if (LOG.isLoggable(INFO)) LOG.info(pluginName + " agreement succeeded");
|
||||
int[] codes = crypto.deriveConfirmationCodes(secret);
|
||||
int aliceCode = codes[0], bobCode = codes[1];
|
||||
int aliceCode = crypto.deriveConfirmationCode(master, true);
|
||||
int bobCode = crypto.deriveConfirmationCode(master, false);
|
||||
group.keyAgreementSucceeded(bobCode, aliceCode);
|
||||
// Exchange confirmation results
|
||||
boolean localMatched, remoteMatched;
|
||||
@@ -130,19 +131,22 @@ class BobConnector extends Connector {
|
||||
// Confirmation succeeded - upgrade to a secure connection
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info(pluginName + " confirmation succeeded");
|
||||
// Derive the header keys
|
||||
SecretKey aliceHeaderKey = crypto.deriveInvitationKey(master, true);
|
||||
SecretKey bobHeaderKey = crypto.deriveInvitationKey(master, false);
|
||||
// Create the readers
|
||||
InputStream streamReader =
|
||||
streamReaderFactory.createInvitationStreamReader(in,
|
||||
secret, true); // Alice's stream
|
||||
aliceHeaderKey);
|
||||
r = readerFactory.createReader(streamReader);
|
||||
// Create the writers
|
||||
OutputStream streamWriter =
|
||||
streamWriterFactory.createInvitationStreamWriter(out,
|
||||
secret, false); // Bob's stream
|
||||
bobHeaderKey);
|
||||
w = writerFactory.createWriter(streamWriter);
|
||||
// Derive the nonces
|
||||
byte[][] nonces = crypto.deriveInvitationNonces(secret);
|
||||
byte[] aliceNonce = nonces[0], bobNonce = nonces[1];
|
||||
byte[] aliceNonce = crypto.deriveSignatureNonce(master, true);
|
||||
byte[] bobNonce = crypto.deriveSignatureNonce(master, false);
|
||||
// Exchange pseudonyms, signed nonces, timestamps and transports
|
||||
Author remoteAuthor;
|
||||
long remoteTimestamp;
|
||||
@@ -171,11 +175,11 @@ class BobConnector extends Connector {
|
||||
tryToClose(conn, true);
|
||||
return;
|
||||
}
|
||||
// The epoch is the minimum of the peers' timestamps
|
||||
long epoch = Math.min(localTimestamp, remoteTimestamp);
|
||||
// The agreed timestamp is the minimum of the peers' timestamps
|
||||
long timestamp = Math.min(localTimestamp, remoteTimestamp);
|
||||
// Add the contact and store the transports
|
||||
try {
|
||||
addContact(remoteAuthor, remoteProps, secret, epoch, false);
|
||||
addContact(remoteAuthor, remoteProps, master, timestamp, false);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
tryToClose(conn, true);
|
||||
|
||||
@@ -1,5 +1,46 @@
|
||||
package org.briarproject.invitation;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
import org.briarproject.api.crypto.KeyParser;
|
||||
import org.briarproject.api.crypto.MessageDigest;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.Signature;
|
||||
import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.messaging.Group;
|
||||
import org.briarproject.api.messaging.GroupFactory;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
@@ -9,50 +50,9 @@ import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTIES_PER
|
||||
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
|
||||
import static org.briarproject.api.TransportPropertyConstants.MAX_TRANSPORT_ID_LENGTH;
|
||||
import static org.briarproject.api.invitation.InvitationConstants.CONNECTION_TIMEOUT;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
import org.briarproject.api.crypto.KeyParser;
|
||||
import org.briarproject.api.crypto.MessageDigest;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.Signature;
|
||||
import org.briarproject.api.data.Reader;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.Writer;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.NoSuchTransportException;
|
||||
import org.briarproject.api.messaging.Group;
|
||||
import org.briarproject.api.messaging.GroupFactory;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.Endpoint;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
|
||||
// FIXME: This class has way too many dependencies
|
||||
abstract class Connector extends Thread {
|
||||
|
||||
private static final Logger LOG =
|
||||
@@ -152,8 +152,8 @@ abstract class Connector extends Thread {
|
||||
return b;
|
||||
}
|
||||
|
||||
protected byte[] deriveMasterSecret(byte[] hash, byte[] key, boolean alice)
|
||||
throws GeneralSecurityException {
|
||||
protected SecretKey deriveMasterSecret(byte[] hash, byte[] key,
|
||||
boolean alice) throws GeneralSecurityException {
|
||||
// Check that the hash matches the key
|
||||
if (!Arrays.equals(hash, messageDigest.digest(key))) {
|
||||
if (LOG.isLoggable(INFO))
|
||||
@@ -271,39 +271,34 @@ abstract class Connector extends Thread {
|
||||
}
|
||||
|
||||
protected void addContact(Author remoteAuthor,
|
||||
Map<TransportId, TransportProperties> remoteProps, byte[] secret,
|
||||
long epoch, boolean alice) throws DbException {
|
||||
Map<TransportId, TransportProperties> remoteProps, SecretKey master,
|
||||
long timestamp, boolean alice) throws DbException {
|
||||
// Add the contact to the database
|
||||
contactId = db.addContact(remoteAuthor, localAuthor.getId());
|
||||
// Create and store the inbox group
|
||||
byte[] salt = crypto.deriveGroupSalt(secret);
|
||||
byte[] salt = crypto.deriveGroupSalt(master);
|
||||
Group inbox = groupFactory.createGroup("Inbox", salt);
|
||||
db.addGroup(inbox);
|
||||
db.setInboxGroup(contactId, inbox);
|
||||
// Store the remote transport properties
|
||||
db.setRemoteProperties(contactId, remoteProps);
|
||||
// Create an endpoint for each transport shared with the contact
|
||||
List<TransportId> ids = new ArrayList<TransportId>();
|
||||
// Derive transport keys for each transport shared with the contact
|
||||
Map<TransportId, Integer> latencies = db.getTransportLatencies();
|
||||
for (TransportId id : localProps.keySet()) {
|
||||
if (latencies.containsKey(id) && remoteProps.containsKey(id))
|
||||
ids.add(id);
|
||||
}
|
||||
// Assign indices to the transports deterministically and derive keys
|
||||
Collections.sort(ids, TransportIdComparator.INSTANCE);
|
||||
int size = ids.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
TransportId id = ids.get(i);
|
||||
Endpoint ep = new Endpoint(contactId, id, epoch, alice);
|
||||
int maxLatency = latencies.get(id);
|
||||
try {
|
||||
db.addEndpoint(ep);
|
||||
} catch (NoSuchTransportException e) {
|
||||
continue;
|
||||
List<TransportKeys> keys = new ArrayList<TransportKeys>();
|
||||
for (TransportId t : localProps.keySet()) {
|
||||
if (remoteProps.containsKey(t) && latencies.containsKey(t)) {
|
||||
// Work out what rotation period the timestamp belongs to
|
||||
long latency = latencies.get(t);
|
||||
long rotationPeriodLength = latency + MAX_CLOCK_DIFFERENCE;
|
||||
long rotationPeriod = timestamp / rotationPeriodLength;
|
||||
// Derive the transport keys
|
||||
TransportKeys k = crypto.deriveTransportKeys(t, master,
|
||||
rotationPeriod, alice);
|
||||
db.addTransportKeys(contactId, k);
|
||||
keys.add(k);
|
||||
}
|
||||
byte[] initialSecret = crypto.deriveInitialSecret(secret, i);
|
||||
keyManager.endpointAdded(ep, maxLatency, initialSecret);
|
||||
}
|
||||
keyManager.contactAdded(contactId, keys);
|
||||
}
|
||||
|
||||
protected void tryToClose(DuplexTransportConnection conn,
|
||||
@@ -322,16 +317,4 @@ abstract class Connector extends Thread {
|
||||
TransportId t = plugin.getId();
|
||||
connectionManager.manageOutgoingConnection(contactId, t, conn);
|
||||
}
|
||||
|
||||
private static class TransportIdComparator
|
||||
implements Comparator<TransportId> {
|
||||
|
||||
private static final TransportIdComparator INSTANCE =
|
||||
new TransportIdComparator();
|
||||
|
||||
public int compare(TransportId t1, TransportId t2) {
|
||||
String s1 = t1.getString(), s2 = t2.getString();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(s1, s2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
package org.briarproject.invitation;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.invitation.InvitationConstants.CONFIRMATION_TIMEOUT;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
import org.briarproject.api.AuthorId;
|
||||
@@ -21,7 +7,6 @@ import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
@@ -35,9 +20,24 @@ import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.PluginManager;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.invitation.InvitationConstants.CONFIRMATION_TIMEOUT;
|
||||
|
||||
/** A task consisting of one or more parallel connection attempts. */
|
||||
class ConnectorGroup extends Thread implements InvitationTask {
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import javax.inject.Inject;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
import org.briarproject.api.AuthorId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.data.ReaderFactory;
|
||||
import org.briarproject.api.data.WriterFactory;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
@@ -15,6 +14,7 @@ import org.briarproject.api.messaging.GroupFactory;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.PluginManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.api.messaging.MessagingSession;
|
||||
@@ -24,10 +23,10 @@ import org.briarproject.api.plugins.ConnectionRegistry;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.api.transport.TagRecogniser;
|
||||
|
||||
class ConnectionManagerImpl implements ConnectionManager {
|
||||
|
||||
@@ -36,7 +35,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final KeyManager keyManager;
|
||||
private final TagRecogniser tagRecogniser;
|
||||
private final StreamReaderFactory streamReaderFactory;
|
||||
private final StreamWriterFactory streamWriterFactory;
|
||||
private final MessagingSessionFactory messagingSessionFactory;
|
||||
@@ -44,14 +42,12 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
|
||||
@Inject
|
||||
ConnectionManagerImpl(@IoExecutor Executor ioExecutor,
|
||||
KeyManager keyManager, TagRecogniser tagRecogniser,
|
||||
StreamReaderFactory streamReaderFactory,
|
||||
KeyManager keyManager, StreamReaderFactory streamReaderFactory,
|
||||
StreamWriterFactory streamWriterFactory,
|
||||
MessagingSessionFactory messagingSessionFactory,
|
||||
ConnectionRegistry connectionRegistry) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.keyManager = keyManager;
|
||||
this.tagRecogniser = tagRecogniser;
|
||||
this.streamReaderFactory = streamReaderFactory;
|
||||
this.streamWriterFactory = streamWriterFactory;
|
||||
this.messagingSessionFactory = messagingSessionFactory;
|
||||
@@ -134,7 +130,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
StreamContext ctx;
|
||||
try {
|
||||
byte[] tag = readTag(transportId, reader);
|
||||
ctx = tagRecogniser.recogniseTag(transportId, tag);
|
||||
ctx = keyManager.recogniseTag(transportId, tag);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, false);
|
||||
@@ -238,7 +234,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
StreamContext ctx;
|
||||
try {
|
||||
byte[] tag = readTag(transportId, reader);
|
||||
ctx = tagRecogniser.recogniseTag(transportId, tag);
|
||||
ctx = keyManager.recogniseTag(transportId, tag);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, false);
|
||||
@@ -367,7 +363,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
StreamContext ctx;
|
||||
try {
|
||||
byte[] tag = readTag(transportId, reader);
|
||||
ctx = tagRecogniser.recogniseTag(transportId, tag);
|
||||
ctx = keyManager.recogniseTag(transportId, tag);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
disposeReader(true, true);
|
||||
@@ -420,4 +416,4 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,5 @@
|
||||
package org.briarproject.plugins;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
@@ -42,6 +25,23 @@ import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
class PluginManagerImpl implements PluginManager {
|
||||
|
||||
private static final Logger LOG =
|
||||
@@ -367,7 +367,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
}
|
||||
|
||||
private class SimplexCallback extends PluginCallbackImpl
|
||||
implements SimplexPluginCallback {
|
||||
implements SimplexPluginCallback {
|
||||
|
||||
private SimplexCallback(TransportId id) {
|
||||
super(id);
|
||||
@@ -383,7 +383,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
}
|
||||
|
||||
private class DuplexCallback extends PluginCallbackImpl
|
||||
implements DuplexPluginCallback {
|
||||
implements DuplexPluginCallback {
|
||||
|
||||
private DuplexCallback(TransportId id) {
|
||||
super(id);
|
||||
|
||||
@@ -1,27 +1,10 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.event.ContactRemovedEvent;
|
||||
import org.briarproject.api.event.Event;
|
||||
@@ -31,429 +14,115 @@ import org.briarproject.api.event.TransportAddedEvent;
|
||||
import org.briarproject.api.event.TransportRemovedEvent;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.Timer;
|
||||
import org.briarproject.api.transport.Endpoint;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TagRecogniser;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
// FIXME: Don't make alien calls with a lock held
|
||||
class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
private static final int MS_BETWEEN_CHECKS = 60 * 1000;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
class KeyManagerImpl implements KeyManager, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyManagerImpl.class.getName());
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
private final Executor dbExecutor;
|
||||
private final EventBus eventBus;
|
||||
private final TagRecogniser tagRecogniser;
|
||||
private final Clock clock;
|
||||
private final Timer timer;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
// The following are locking: lock
|
||||
private final Map<TransportId, Integer> maxLatencies;
|
||||
private final Map<EndpointKey, TemporarySecret> oldSecrets;
|
||||
private final Map<EndpointKey, TemporarySecret> currentSecrets;
|
||||
private final Map<EndpointKey, TemporarySecret> newSecrets;
|
||||
private final Clock clock;
|
||||
private final ConcurrentHashMap<TransportId, TransportKeyManager> managers;
|
||||
|
||||
@Inject
|
||||
KeyManagerImpl(CryptoComponent crypto, DatabaseComponent db,
|
||||
EventBus eventBus, TagRecogniser tagRecogniser, Clock clock,
|
||||
Timer timer) {
|
||||
this.crypto = crypto;
|
||||
KeyManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||
@DatabaseExecutor Executor dbExecutor, EventBus eventBus,
|
||||
Timer timer, Clock clock) {
|
||||
this.db = db;
|
||||
this.crypto = crypto;
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.eventBus = eventBus;
|
||||
this.tagRecogniser = tagRecogniser;
|
||||
this.clock = clock;
|
||||
this.timer = timer;
|
||||
maxLatencies = new HashMap<TransportId, Integer>();
|
||||
oldSecrets = new HashMap<EndpointKey, TemporarySecret>();
|
||||
currentSecrets = new HashMap<EndpointKey, TemporarySecret>();
|
||||
newSecrets = new HashMap<EndpointKey, TemporarySecret>();
|
||||
this.clock = clock;
|
||||
managers = new ConcurrentHashMap<TransportId, TransportKeyManager>();
|
||||
}
|
||||
|
||||
public boolean start() {
|
||||
lock.lock();
|
||||
eventBus.addListener(this);
|
||||
try {
|
||||
eventBus.addListener(this);
|
||||
// Load the temporary secrets and transport latencies from the DB
|
||||
Collection<TemporarySecret> secrets;
|
||||
try {
|
||||
secrets = db.getSecrets();
|
||||
maxLatencies.putAll(db.getTransportLatencies());
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
// Work out what phase of its lifecycle each secret is in
|
||||
long now = clock.currentTimeMillis();
|
||||
Collection<TemporarySecret> dead =
|
||||
assignSecretsToMaps(now, secrets);
|
||||
// Replace any dead secrets
|
||||
Collection<TemporarySecret> created = replaceDeadSecrets(now, dead);
|
||||
if (!created.isEmpty()) {
|
||||
// Store any secrets that have been created,
|
||||
// removing any dead ones
|
||||
try {
|
||||
db.addSecrets(created);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Pass the old, current and new secrets to the recogniser
|
||||
for (TemporarySecret s : oldSecrets.values())
|
||||
tagRecogniser.addSecret(s);
|
||||
for (TemporarySecret s : currentSecrets.values())
|
||||
tagRecogniser.addSecret(s);
|
||||
for (TemporarySecret s : newSecrets.values())
|
||||
tagRecogniser.addSecret(s);
|
||||
// Schedule periodic key rotation
|
||||
timer.scheduleAtFixedRate(this, MS_BETWEEN_CHECKS,
|
||||
MS_BETWEEN_CHECKS);
|
||||
return true;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
Map<TransportId, Integer> latencies = db.getTransportLatencies();
|
||||
for (Entry<TransportId, Integer> e : latencies.entrySet())
|
||||
addTransport(e.getKey(), e.getValue());
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Assigns secrets to the appropriate maps and returns any dead secrets
|
||||
// Locking: lock
|
||||
private Collection<TemporarySecret> assignSecretsToMaps(long now,
|
||||
Collection<TemporarySecret> secrets) {
|
||||
Collection<TemporarySecret> dead = new ArrayList<TemporarySecret>();
|
||||
for (TemporarySecret s : secrets) {
|
||||
// Discard the secret if the transport has been removed
|
||||
Integer maxLatency = maxLatencies.get(s.getTransportId());
|
||||
if (maxLatency == null) {
|
||||
LOG.info("Discarding obsolete secret");
|
||||
continue;
|
||||
}
|
||||
long rotation = maxLatency + MAX_CLOCK_DIFFERENCE;
|
||||
long creationTime = s.getEpoch() + rotation * (s.getPeriod() - 2);
|
||||
long activationTime = creationTime + rotation;
|
||||
long deactivationTime = activationTime + rotation;
|
||||
long destructionTime = deactivationTime + rotation;
|
||||
if (now >= destructionTime) {
|
||||
dead.add(s);
|
||||
} else if (now >= deactivationTime) {
|
||||
oldSecrets.put(new EndpointKey(s), s);
|
||||
} else if (now >= activationTime) {
|
||||
currentSecrets.put(new EndpointKey(s), s);
|
||||
} else if (now >= creationTime) {
|
||||
newSecrets.put(new EndpointKey(s), s);
|
||||
} else {
|
||||
// FIXME: Work out what to do here
|
||||
throw new Error("Clock has moved backwards");
|
||||
}
|
||||
}
|
||||
return dead;
|
||||
}
|
||||
|
||||
// Replaces the given secrets and returns any secrets created
|
||||
// Locking: lock
|
||||
private Collection<TemporarySecret> replaceDeadSecrets(long now,
|
||||
Collection<TemporarySecret> dead) {
|
||||
// If there are several dead secrets for an endpoint, use the newest
|
||||
Map<EndpointKey, TemporarySecret> newest =
|
||||
new HashMap<EndpointKey, TemporarySecret>();
|
||||
for (TemporarySecret s : dead) {
|
||||
EndpointKey k = new EndpointKey(s);
|
||||
TemporarySecret exists = newest.get(k);
|
||||
if (exists == null) {
|
||||
// There's no other secret for this endpoint
|
||||
newest.put(k, s);
|
||||
} else if (exists.getPeriod() < s.getPeriod()) {
|
||||
// There's an older secret - use this one instead
|
||||
newest.put(k, s);
|
||||
} else {
|
||||
// There's a newer secret - keep using it
|
||||
}
|
||||
}
|
||||
Collection<TemporarySecret> created = new ArrayList<TemporarySecret>();
|
||||
for (Entry<EndpointKey, TemporarySecret> e : newest.entrySet()) {
|
||||
TemporarySecret s = e.getValue();
|
||||
Integer maxLatency = maxLatencies.get(s.getTransportId());
|
||||
if (maxLatency == null) throw new IllegalStateException();
|
||||
// Work out which rotation period we're in
|
||||
long elapsed = now - s.getEpoch();
|
||||
long rotation = maxLatency + MAX_CLOCK_DIFFERENCE;
|
||||
long period = (elapsed / rotation) + 1;
|
||||
if (period < 1) throw new IllegalStateException();
|
||||
if (period - s.getPeriod() < 2)
|
||||
throw new IllegalStateException();
|
||||
// Derive the old, current and new secrets
|
||||
byte[] b1 = s.getSecret();
|
||||
for (long p = s.getPeriod() + 1; p < period; p++)
|
||||
b1 = crypto.deriveNextSecret(b1, p);
|
||||
byte[] b2 = crypto.deriveNextSecret(b1, period);
|
||||
byte[] b3 = crypto.deriveNextSecret(b2, period + 1);
|
||||
// Add the secrets to their respective maps if not already present
|
||||
EndpointKey k = e.getKey();
|
||||
if (!oldSecrets.containsKey(k)) {
|
||||
TemporarySecret s1 = new TemporarySecret(s, period - 1, b1);
|
||||
oldSecrets.put(k, s1);
|
||||
created.add(s1);
|
||||
}
|
||||
if (!currentSecrets.containsKey(k)) {
|
||||
TemporarySecret s2 = new TemporarySecret(s, period, b2);
|
||||
currentSecrets.put(k, s2);
|
||||
created.add(s2);
|
||||
}
|
||||
if (!newSecrets.containsKey(k)) {
|
||||
TemporarySecret s3 = new TemporarySecret(s, period + 1, b3);
|
||||
newSecrets.put(k, s3);
|
||||
created.add(s3);
|
||||
}
|
||||
}
|
||||
return created;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean stop() {
|
||||
lock.lock();
|
||||
try {
|
||||
eventBus.removeListener(this);
|
||||
timer.cancel();
|
||||
tagRecogniser.removeSecrets();
|
||||
maxLatencies.clear();
|
||||
oldSecrets.clear();
|
||||
currentSecrets.clear();
|
||||
newSecrets.clear();
|
||||
return true;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
eventBus.removeListener(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void contactAdded(ContactId c, Collection<TransportKeys> keys) {
|
||||
for (TransportKeys k : keys) {
|
||||
TransportKeyManager m = managers.get(k.getTransportId());
|
||||
if (m != null) m.addContact(c, k);
|
||||
}
|
||||
}
|
||||
|
||||
public StreamContext getStreamContext(ContactId c,
|
||||
TransportId t) {
|
||||
lock.lock();
|
||||
try {
|
||||
TemporarySecret s = currentSecrets.get(new EndpointKey(c, t));
|
||||
if (s == null) {
|
||||
LOG.info("No secret for endpoint");
|
||||
return null;
|
||||
}
|
||||
long streamNumber;
|
||||
try {
|
||||
streamNumber = db.incrementStreamCounter(c, t, s.getPeriod());
|
||||
if (streamNumber == -1) {
|
||||
LOG.info("No counter for period");
|
||||
return null;
|
||||
}
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return null;
|
||||
}
|
||||
byte[] secret = s.getSecret();
|
||||
return new StreamContext(c, t, secret, streamNumber, s.getAlice());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
public StreamContext getStreamContext(ContactId c, TransportId t) {
|
||||
TransportKeyManager m = managers.get(t);
|
||||
return m == null ? null : m.getStreamContext(c);
|
||||
}
|
||||
|
||||
public void endpointAdded(Endpoint ep, int maxLatency,
|
||||
byte[] initialSecret) {
|
||||
lock.lock();
|
||||
try {
|
||||
maxLatencies.put(ep.getTransportId(), maxLatency);
|
||||
// Work out which rotation period we're in
|
||||
long elapsed = clock.currentTimeMillis() - ep.getEpoch();
|
||||
long rotation = maxLatency + MAX_CLOCK_DIFFERENCE;
|
||||
long period = (elapsed / rotation) + 1;
|
||||
if (period < 1) throw new IllegalStateException();
|
||||
// Derive the old, current and new secrets
|
||||
byte[] b1 = initialSecret;
|
||||
for (long p = 0; p < period; p++)
|
||||
b1 = crypto.deriveNextSecret(b1, p);
|
||||
byte[] b2 = crypto.deriveNextSecret(b1, period);
|
||||
byte[] b3 = crypto.deriveNextSecret(b2, period + 1);
|
||||
TemporarySecret s1 = new TemporarySecret(ep, period - 1, b1);
|
||||
TemporarySecret s2 = new TemporarySecret(ep, period, b2);
|
||||
TemporarySecret s3 = new TemporarySecret(ep, period + 1, b3);
|
||||
// Add the incoming secrets to their respective maps
|
||||
EndpointKey k = new EndpointKey(ep);
|
||||
oldSecrets.put(k, s1);
|
||||
currentSecrets.put(k, s2);
|
||||
newSecrets.put(k, s3);
|
||||
// Store the new secrets
|
||||
try {
|
||||
db.addSecrets(Arrays.asList(s1, s2, s3));
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return;
|
||||
}
|
||||
// Pass the new secrets to the recogniser
|
||||
tagRecogniser.addSecret(s1);
|
||||
tagRecogniser.addSecret(s2);
|
||||
tagRecogniser.addSecret(s3);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
lock.lock();
|
||||
try {
|
||||
// Rebuild the maps because we may be running a whole period late
|
||||
Collection<TemporarySecret> secrets = new ArrayList<TemporarySecret>();
|
||||
secrets.addAll(oldSecrets.values());
|
||||
secrets.addAll(currentSecrets.values());
|
||||
secrets.addAll(newSecrets.values());
|
||||
oldSecrets.clear();
|
||||
currentSecrets.clear();
|
||||
newSecrets.clear();
|
||||
// Work out what phase of its lifecycle each secret is in
|
||||
long now = clock.currentTimeMillis();
|
||||
Collection<TemporarySecret> dead = assignSecretsToMaps(now, secrets);
|
||||
// Remove any dead secrets from the recogniser
|
||||
for (TemporarySecret s : dead) {
|
||||
ContactId c = s.getContactId();
|
||||
TransportId t = s.getTransportId();
|
||||
long period = s.getPeriod();
|
||||
tagRecogniser.removeSecret(c, t, period);
|
||||
}
|
||||
// Replace any dead secrets
|
||||
Collection<TemporarySecret> created = replaceDeadSecrets(now, dead);
|
||||
if (!created.isEmpty()) {
|
||||
// Store any secrets that have been created
|
||||
try {
|
||||
db.addSecrets(created);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
// Pass any secrets that have been created to the recogniser
|
||||
for (TemporarySecret s : created) tagRecogniser.addSecret(s);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
public StreamContext recogniseTag(TransportId t, byte[] tag)
|
||||
throws DbException {
|
||||
TransportKeyManager m = managers.get(t);
|
||||
return m == null ? null : m.recogniseTag(tag);
|
||||
}
|
||||
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof ContactRemovedEvent) {
|
||||
ContactRemovedEvent c = (ContactRemovedEvent) e;
|
||||
timer.schedule(new ContactRemovedTask(c), 0);
|
||||
} else if (e instanceof TransportAddedEvent) {
|
||||
if (e instanceof TransportAddedEvent) {
|
||||
TransportAddedEvent t = (TransportAddedEvent) e;
|
||||
timer.schedule(new TransportAddedTask(t), 0);
|
||||
addTransport(t.getTransportId(), t.getMaxLatency());
|
||||
} else if (e instanceof TransportRemovedEvent) {
|
||||
TransportRemovedEvent t = (TransportRemovedEvent) e;
|
||||
timer.schedule(new TransportRemovedTask(t), 0);
|
||||
removeTransport(((TransportRemovedEvent) e).getTransportId());
|
||||
} else if (e instanceof ContactRemovedEvent) {
|
||||
removeContact(((ContactRemovedEvent) e).getContactId());
|
||||
}
|
||||
}
|
||||
|
||||
// Locking: lock
|
||||
private void removeSecrets(ContactId c, Map<?, TemporarySecret> m) {
|
||||
Iterator<TemporarySecret> it = m.values().iterator();
|
||||
while (it.hasNext())
|
||||
if (it.next().getContactId().equals(c)) it.remove();
|
||||
}
|
||||
|
||||
// Locking: lock
|
||||
private void removeSecrets(TransportId t, Map<?, TemporarySecret> m) {
|
||||
Iterator<TemporarySecret> it = m.values().iterator();
|
||||
while (it.hasNext())
|
||||
if (it.next().getTransportId().equals(t)) it.remove();
|
||||
}
|
||||
|
||||
private static class EndpointKey {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final TransportId transportId;
|
||||
|
||||
private EndpointKey(ContactId contactId, TransportId transportId) {
|
||||
this.contactId = contactId;
|
||||
this.transportId = transportId;
|
||||
}
|
||||
|
||||
private EndpointKey(Endpoint ep) {
|
||||
this(ep.getContactId(), ep.getTransportId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return contactId.hashCode() ^ transportId.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof EndpointKey) {
|
||||
EndpointKey k = (EndpointKey) o;
|
||||
return contactId.equals(k.contactId) &&
|
||||
transportId.equals(k.transportId);
|
||||
private void addTransport(final TransportId t, final int maxLatency) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
TransportKeyManager m = new TransportKeyManager(db, crypto,
|
||||
dbExecutor, timer, clock, t, maxLatency);
|
||||
// Don't add transport twice if event is received during startup
|
||||
if (managers.putIfAbsent(t, m) == null) m.start();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class ContactRemovedTask extends TimerTask {
|
||||
|
||||
private final ContactRemovedEvent event;
|
||||
|
||||
private ContactRemovedTask(ContactRemovedEvent event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ContactId c = event.getContactId();
|
||||
tagRecogniser.removeSecrets(c);
|
||||
lock.lock();
|
||||
try {
|
||||
removeSecrets(c, oldSecrets);
|
||||
removeSecrets(c, currentSecrets);
|
||||
removeSecrets(c, newSecrets);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
private void removeTransport(TransportId t) {
|
||||
managers.remove(t);
|
||||
}
|
||||
|
||||
private class TransportAddedTask extends TimerTask {
|
||||
|
||||
private final TransportAddedEvent event;
|
||||
|
||||
private TransportAddedTask(TransportAddedEvent event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
lock.lock();
|
||||
try {
|
||||
maxLatencies.put(event.getTransportId(), event.getMaxLatency());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
private void removeContact(final ContactId c) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
for (TransportKeyManager m : managers.values())
|
||||
m.removeContact(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class TransportRemovedTask extends TimerTask {
|
||||
|
||||
private TransportRemovedEvent event;
|
||||
|
||||
private TransportRemovedTask(TransportRemovedEvent event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
TransportId t = event.getTransportId();
|
||||
tagRecogniser.removeSecrets(t);
|
||||
lock.lock();
|
||||
try {
|
||||
maxLatencies.remove(t);
|
||||
removeSecrets(t, oldSecrets);
|
||||
removeSecrets(t, currentSecrets);
|
||||
removeSecrets(t, newSecrets);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.transport.IncomingKeys;
|
||||
|
||||
// This class is not thread-safe
|
||||
class MutableIncomingKeys {
|
||||
|
||||
private final SecretKey tagKey, headerKey;
|
||||
private final long rotationPeriod;
|
||||
private final ReorderingWindow window;
|
||||
|
||||
MutableIncomingKeys(IncomingKeys in) {
|
||||
tagKey = in.getTagKey();
|
||||
headerKey = in.getHeaderKey();
|
||||
rotationPeriod = in.getRotationPeriod();
|
||||
window = new ReorderingWindow(in.getWindowBase(), in.getWindowBitmap());
|
||||
}
|
||||
|
||||
IncomingKeys snapshot() {
|
||||
return new IncomingKeys(tagKey, headerKey, rotationPeriod,
|
||||
window.getBase(), window.getBitmap());
|
||||
}
|
||||
|
||||
SecretKey getTagKey() {
|
||||
return tagKey;
|
||||
}
|
||||
|
||||
SecretKey getHeaderKey() {
|
||||
return headerKey;
|
||||
}
|
||||
|
||||
long getRotationPeriod() {
|
||||
return rotationPeriod;
|
||||
}
|
||||
|
||||
ReorderingWindow getWindow() {
|
||||
return window;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.transport.OutgoingKeys;
|
||||
|
||||
// This class is not thread-safe
|
||||
class MutableOutgoingKeys {
|
||||
|
||||
private final SecretKey tagKey, headerKey;
|
||||
private final long rotationPeriod;
|
||||
private long streamCounter;
|
||||
|
||||
MutableOutgoingKeys(OutgoingKeys out) {
|
||||
tagKey = out.getTagKey();
|
||||
headerKey = out.getHeaderKey();
|
||||
rotationPeriod = out.getRotationPeriod();
|
||||
streamCounter = out.getStreamCounter();
|
||||
}
|
||||
|
||||
OutgoingKeys snapshot() {
|
||||
return new OutgoingKeys(tagKey, headerKey, rotationPeriod,
|
||||
streamCounter);
|
||||
}
|
||||
|
||||
SecretKey getTagKey() {
|
||||
return tagKey;
|
||||
}
|
||||
|
||||
SecretKey getHeaderKey() {
|
||||
return headerKey;
|
||||
}
|
||||
|
||||
long getRotationPeriod() {
|
||||
return rotationPeriod;
|
||||
}
|
||||
|
||||
long getStreamCounter() {
|
||||
return streamCounter;
|
||||
}
|
||||
|
||||
void incrementStreamCounter() {
|
||||
streamCounter++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
|
||||
class MutableTransportKeys {
|
||||
|
||||
private final TransportId transportId;
|
||||
private final MutableIncomingKeys inPrev, inCurr, inNext;
|
||||
private final MutableOutgoingKeys outCurr;
|
||||
|
||||
MutableTransportKeys(TransportKeys k) {
|
||||
transportId = k.getTransportId();
|
||||
inPrev = new MutableIncomingKeys(k.getPreviousIncomingKeys());
|
||||
inCurr = new MutableIncomingKeys(k.getCurrentIncomingKeys());
|
||||
inNext = new MutableIncomingKeys(k.getNextIncomingKeys());
|
||||
outCurr = new MutableOutgoingKeys(k.getCurrentOutgoingKeys());
|
||||
}
|
||||
|
||||
TransportKeys snapshot() {
|
||||
return new TransportKeys(transportId, inPrev.snapshot(),
|
||||
inCurr.snapshot(), inNext.snapshot(), outCurr.snapshot());
|
||||
}
|
||||
|
||||
TransportId getTransportId() {
|
||||
return transportId;
|
||||
}
|
||||
|
||||
MutableIncomingKeys getPreviousIncomingKeys() {
|
||||
return inPrev;
|
||||
}
|
||||
|
||||
MutableIncomingKeys getCurrentIncomingKeys() {
|
||||
return inCurr;
|
||||
}
|
||||
|
||||
MutableIncomingKeys getNextIncomingKeys() {
|
||||
return inNext;
|
||||
}
|
||||
|
||||
MutableOutgoingKeys getCurrentOutgoingKeys() {
|
||||
return outCurr;
|
||||
}
|
||||
}
|
||||
@@ -1,102 +1,98 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import static org.briarproject.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
// This class is not thread-safe
|
||||
class ReorderingWindow {
|
||||
|
||||
private final Set<Long> unseen;
|
||||
private long base;
|
||||
private boolean[] seen;
|
||||
|
||||
private long centre;
|
||||
|
||||
ReorderingWindow() {
|
||||
unseen = new HashSet<Long>();
|
||||
for (long l = 0; l < REORDERING_WINDOW_SIZE / 2; l++) unseen.add(l);
|
||||
centre = 0;
|
||||
}
|
||||
|
||||
ReorderingWindow(long centre, byte[] bitmap) {
|
||||
if (centre < 0 || centre > MAX_32_BIT_UNSIGNED + 1)
|
||||
ReorderingWindow(long base, byte[] bitmap) {
|
||||
if (base < 0) throw new IllegalArgumentException();
|
||||
if (base > MAX_32_BIT_UNSIGNED + 1)
|
||||
throw new IllegalArgumentException();
|
||||
if (bitmap.length != REORDERING_WINDOW_SIZE / 8)
|
||||
throw new IllegalArgumentException();
|
||||
this.centre = centre;
|
||||
unseen = new HashSet<Long>();
|
||||
long bitmapBottom = centre - REORDERING_WINDOW_SIZE / 2;
|
||||
for (int bytes = 0; bytes < bitmap.length; bytes++) {
|
||||
for (int bits = 0; bits < 8; bits++) {
|
||||
long streamNumber = bitmapBottom + bytes * 8 + bits;
|
||||
if (streamNumber >= 0 && streamNumber <= MAX_32_BIT_UNSIGNED) {
|
||||
if ((bitmap[bytes] & (128 >> bits)) == 0)
|
||||
unseen.add(streamNumber);
|
||||
}
|
||||
this.base = base;
|
||||
seen = new boolean[bitmap.length * 8];
|
||||
for (int i = 0; i < bitmap.length; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if ((bitmap[i] & (128 >> j)) != 0) seen[i * 8 + j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean isSeen(long streamNumber) {
|
||||
return !unseen.contains(streamNumber);
|
||||
}
|
||||
|
||||
Collection<Long> setSeen(long streamNumber) {
|
||||
long bottom = getBottom(centre);
|
||||
long top = getTop(centre);
|
||||
if (streamNumber < bottom || streamNumber > top)
|
||||
throw new IllegalArgumentException();
|
||||
if (!unseen.remove(streamNumber))
|
||||
throw new IllegalArgumentException();
|
||||
Collection<Long> changed = new ArrayList<Long>();
|
||||
if (streamNumber >= centre) {
|
||||
centre = streamNumber + 1;
|
||||
long newBottom = getBottom(centre);
|
||||
long newTop = getTop(centre);
|
||||
for (long l = bottom; l < newBottom; l++) {
|
||||
if (unseen.remove(l)) changed.add(l);
|
||||
}
|
||||
for (long l = top + 1; l <= newTop; l++) {
|
||||
if (unseen.add(l)) changed.add(l);
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
long getCentre() {
|
||||
return centre;
|
||||
long getBase() {
|
||||
return base;
|
||||
}
|
||||
|
||||
byte[] getBitmap() {
|
||||
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
|
||||
long bitmapBottom = centre - REORDERING_WINDOW_SIZE / 2;
|
||||
for (int bytes = 0; bytes < bitmap.length; bytes++) {
|
||||
for (int bits = 0; bits < 8; bits++) {
|
||||
long streamNumber = bitmapBottom + bytes * 8 + bits;
|
||||
if (streamNumber >= 0 && streamNumber <= MAX_32_BIT_UNSIGNED) {
|
||||
if (!unseen.contains(streamNumber))
|
||||
bitmap[bytes] |= 128 >> bits;
|
||||
}
|
||||
byte[] bitmap = new byte[seen.length / 8];
|
||||
for (int i = 0; i < bitmap.length; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if (seen[i * 8 + j]) bitmap[i] |= 128 >> j;
|
||||
}
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
// Returns the lowest value contained in a window with the given centre
|
||||
private static long getBottom(long centre) {
|
||||
return Math.max(0, centre - REORDERING_WINDOW_SIZE / 2);
|
||||
}
|
||||
|
||||
// Returns the highest value contained in a window with the given centre
|
||||
private static long getTop(long centre) {
|
||||
return Math.min(MAX_32_BIT_UNSIGNED,
|
||||
centre + REORDERING_WINDOW_SIZE / 2 - 1);
|
||||
}
|
||||
|
||||
public Collection<Long> getUnseen() {
|
||||
List<Long> getUnseen() {
|
||||
List<Long> unseen = new ArrayList<Long>(seen.length);
|
||||
for (int i = 0; i < seen.length; i++)
|
||||
if (!seen[i]) unseen.add(base + i);
|
||||
return unseen;
|
||||
}
|
||||
|
||||
Change setSeen(long index) {
|
||||
if (index < base) throw new IllegalArgumentException();
|
||||
if (index >= base + seen.length) throw new IllegalArgumentException();
|
||||
if (index > MAX_32_BIT_UNSIGNED) throw new IllegalArgumentException();
|
||||
int offset = (int) (index - base);
|
||||
if (seen[offset]) throw new IllegalArgumentException();
|
||||
seen[offset] = true;
|
||||
// Rule 1: Slide until all elements above the midpoint are unseen
|
||||
int slide = Math.max(0, offset + 1 - seen.length / 2);
|
||||
// Rule 2: Slide until the lowest element is unseen
|
||||
while (seen[slide]) slide++;
|
||||
// If the window doesn't need to slide, return
|
||||
if (slide == 0) {
|
||||
List<Long> added = Collections.emptyList();
|
||||
List<Long> removed = Collections.singletonList(index);
|
||||
return new Change(added, removed);
|
||||
}
|
||||
// Record the elements that will be added and removed
|
||||
List<Long> added = new ArrayList<Long>(slide);
|
||||
List<Long> removed = new ArrayList<Long>(slide);
|
||||
for (int i = 0; i < slide; i++) {
|
||||
if (!seen[i]) removed.add(base + i);
|
||||
added.add(base + seen.length + i);
|
||||
}
|
||||
removed.add(index);
|
||||
// Update the window
|
||||
base += slide;
|
||||
for (int i = 0; i + slide < seen.length; i++) seen[i] = seen[i + slide];
|
||||
for (int i = seen.length - slide; i < seen.length; i++) seen[i] = false;
|
||||
return new Change(added, removed);
|
||||
}
|
||||
|
||||
static class Change {
|
||||
|
||||
private final List<Long> added, removed;
|
||||
|
||||
Change(List<Long> added, List<Long> removed) {
|
||||
this.added = added;
|
||||
this.removed = removed;
|
||||
}
|
||||
|
||||
List<Long> getAdded() {
|
||||
return added;
|
||||
}
|
||||
|
||||
List<Long> getRemoved() {
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.InputStream;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamDecrypterFactory;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
@@ -23,9 +24,9 @@ class StreamReaderFactoryImpl implements StreamReaderFactory {
|
||||
}
|
||||
|
||||
public InputStream createInvitationStreamReader(InputStream in,
|
||||
byte[] secret, boolean alice) {
|
||||
SecretKey headerKey) {
|
||||
return new StreamReaderImpl(
|
||||
streamDecrypterFactory.createInvitationStreamDecrypter(in,
|
||||
secret, alice));
|
||||
headerKey));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,12 @@ import java.io.InputStream;
|
||||
|
||||
import org.briarproject.api.crypto.StreamDecrypter;
|
||||
|
||||
/**
|
||||
* An {@link java.io.InputStream InputStream} that unpacks payload data from
|
||||
* transport frames.
|
||||
* <p>
|
||||
* This class is not thread-safe.
|
||||
*/
|
||||
class StreamReaderImpl extends InputStream {
|
||||
|
||||
private final StreamDecrypter decrypter;
|
||||
@@ -50,7 +56,7 @@ class StreamReaderImpl extends InputStream {
|
||||
}
|
||||
|
||||
private void readFrame() throws IOException {
|
||||
assert length == 0;
|
||||
if (length != 0) throw new IllegalStateException();
|
||||
offset = 0;
|
||||
length = decrypter.readFrame(payload);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.OutputStream;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.StreamEncrypterFactory;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
@@ -24,9 +25,9 @@ class StreamWriterFactoryImpl implements StreamWriterFactory {
|
||||
}
|
||||
|
||||
public OutputStream createInvitationStreamWriter(OutputStream out,
|
||||
byte[] secret, boolean alice) {
|
||||
SecretKey headerKey) {
|
||||
return new StreamWriterImpl(
|
||||
streamEncrypterFactory.createInvitationStreamEncrypter(out,
|
||||
secret, alice));
|
||||
headerKey));
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,9 @@ import java.io.OutputStream;
|
||||
import org.briarproject.api.crypto.StreamEncrypter;
|
||||
|
||||
/**
|
||||
* A {@link org.briarproject.api.transport.StreamWriter StreamWriter} that
|
||||
* buffers its input and writes a frame whenever there is a full frame to write
|
||||
* or the {@link #flush()} method is called.
|
||||
* An {@link java.io.OutputStream OutputStream} that packs data into transport
|
||||
* frames, writing a frame whenever there is a full frame to write or the
|
||||
* {@link #flush()} method is called.
|
||||
* <p>
|
||||
* This class is not thread-safe.
|
||||
*/
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TagRecogniser;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
|
||||
class TagRecogniserImpl implements TagRecogniser {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final DatabaseComponent db;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
// Locking: lock
|
||||
private final Map<TransportId, TransportTagRecogniser> recognisers;
|
||||
|
||||
@Inject
|
||||
TagRecogniserImpl(CryptoComponent crypto, DatabaseComponent db) {
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
recognisers = new HashMap<TransportId, TransportTagRecogniser>();
|
||||
}
|
||||
|
||||
public StreamContext recogniseTag(TransportId t, byte[] tag)
|
||||
throws DbException {
|
||||
TransportTagRecogniser r;
|
||||
lock.lock();
|
||||
try {
|
||||
r = recognisers.get(t);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
if (r == null) return null;
|
||||
return r.recogniseTag(tag);
|
||||
}
|
||||
|
||||
public void addSecret(TemporarySecret s) {
|
||||
TransportId t = s.getTransportId();
|
||||
TransportTagRecogniser r;
|
||||
lock.lock();
|
||||
try {
|
||||
r = recognisers.get(t);
|
||||
if (r == null) {
|
||||
r = new TransportTagRecogniser(crypto, db, t);
|
||||
recognisers.put(t, r);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
r.addSecret(s);
|
||||
}
|
||||
|
||||
public void removeSecret(ContactId c, TransportId t, long period) {
|
||||
TransportTagRecogniser r;
|
||||
lock.lock();
|
||||
try {
|
||||
r = recognisers.get(t);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
if (r != null) r.removeSecret(c, period);
|
||||
}
|
||||
|
||||
public void removeSecrets(ContactId c) {
|
||||
lock.lock();
|
||||
try {
|
||||
for (TransportTagRecogniser r : recognisers.values())
|
||||
r.removeSecrets(c);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSecrets(TransportId t) {
|
||||
lock.lock();
|
||||
try {
|
||||
recognisers.remove(t);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void removeSecrets() {
|
||||
lock.lock();
|
||||
try {
|
||||
for (TransportTagRecogniser r : recognisers.values())
|
||||
r.removeSecrets();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import org.briarproject.api.Bytes;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.Timer;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
import org.briarproject.transport.ReorderingWindow.Change;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
class TransportKeyManager extends TimerTask {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(TransportKeyManager.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
private final Executor dbExecutor;
|
||||
private final Timer timer;
|
||||
private final Clock clock;
|
||||
private final TransportId transportId;
|
||||
private final long rotationPeriodLength;
|
||||
private final ReentrantLock lock;
|
||||
|
||||
// The following are locking: lock
|
||||
private final Map<Bytes, TagContext> inContexts;
|
||||
private final Map<ContactId, MutableOutgoingKeys> outContexts;
|
||||
private final Map<ContactId, MutableTransportKeys> keys;
|
||||
|
||||
TransportKeyManager(DatabaseComponent db, CryptoComponent crypto,
|
||||
Executor dbExecutor, Timer timer, Clock clock,
|
||||
TransportId transportId, long maxLatency) {
|
||||
this.db = db;
|
||||
this.crypto = crypto;
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.timer = timer;
|
||||
this.clock = clock;
|
||||
this.transportId = transportId;
|
||||
rotationPeriodLength = maxLatency + MAX_CLOCK_DIFFERENCE;
|
||||
lock = new ReentrantLock();
|
||||
inContexts = new HashMap<Bytes, TagContext>();
|
||||
outContexts = new HashMap<ContactId, MutableOutgoingKeys>();
|
||||
keys = new HashMap<ContactId, MutableTransportKeys>();
|
||||
}
|
||||
|
||||
void start() {
|
||||
// Load the transport keys from the DB
|
||||
Map<ContactId, TransportKeys> loaded;
|
||||
try {
|
||||
loaded = db.getTransportKeys(transportId);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return;
|
||||
}
|
||||
// Rotate the keys to the current rotation period
|
||||
Map<ContactId, TransportKeys> rotated =
|
||||
new HashMap<ContactId, TransportKeys>();
|
||||
Map<ContactId, TransportKeys> current =
|
||||
new HashMap<ContactId, TransportKeys>();
|
||||
long now = clock.currentTimeMillis();
|
||||
long rotationPeriod = now / rotationPeriodLength;
|
||||
for (Entry<ContactId, TransportKeys> e : loaded.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
TransportKeys k = e.getValue();
|
||||
TransportKeys k1 = crypto.rotateTransportKeys(k, rotationPeriod);
|
||||
if (k1.getRotationPeriod() > k.getRotationPeriod())
|
||||
rotated.put(c, k1);
|
||||
current.put(c, k1);
|
||||
}
|
||||
lock.lock();
|
||||
try {
|
||||
// Initialise mutable state for all contacts
|
||||
for (Entry<ContactId, TransportKeys> e : current.entrySet())
|
||||
addKeys(e.getKey(), new MutableTransportKeys(e.getValue()));
|
||||
// Write any rotated keys back to the DB
|
||||
saveTransportKeys(rotated);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// Schedule a periodic task to rotate the keys
|
||||
long delay = rotationPeriodLength - now % rotationPeriodLength;
|
||||
timer.scheduleAtFixedRate(this, delay, rotationPeriodLength);
|
||||
}
|
||||
|
||||
// Locking: lock
|
||||
private void addKeys(ContactId c, MutableTransportKeys m) {
|
||||
encodeTags(c, m.getPreviousIncomingKeys());
|
||||
encodeTags(c, m.getCurrentIncomingKeys());
|
||||
encodeTags(c, m.getNextIncomingKeys());
|
||||
outContexts.put(c, m.getCurrentOutgoingKeys());
|
||||
keys.put(c, m);
|
||||
}
|
||||
|
||||
// Locking: lock
|
||||
private void encodeTags(ContactId c, MutableIncomingKeys inKeys) {
|
||||
for (long streamNumber : inKeys.getWindow().getUnseen()) {
|
||||
TagContext tagCtx = new TagContext(c, inKeys, streamNumber);
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
crypto.encodeTag(tag, inKeys.getTagKey(), streamNumber);
|
||||
inContexts.put(new Bytes(tag), tagCtx);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveTransportKeys(final Map<ContactId, TransportKeys> rotated) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
db.updateTransportKeys(rotated);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void addContact(ContactId c, TransportKeys k) {
|
||||
lock.lock();
|
||||
try {
|
||||
// Initialise mutable state for the contact
|
||||
addKeys(c, new MutableTransportKeys(k));
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void removeContact(ContactId c) {
|
||||
lock.lock();
|
||||
try {
|
||||
// Remove mutable state for the contact
|
||||
Iterator<Entry<Bytes, TagContext>> it =
|
||||
inContexts.entrySet().iterator();
|
||||
while (it.hasNext())
|
||||
if (it.next().getValue().contactId.equals(c)) it.remove();
|
||||
outContexts.remove(c);
|
||||
keys.remove(c);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
StreamContext getStreamContext(ContactId c) {
|
||||
StreamContext ctx;
|
||||
lock.lock();
|
||||
try {
|
||||
// Look up the outgoing keys for the contact
|
||||
MutableOutgoingKeys outKeys = outContexts.get(c);
|
||||
if (outKeys == null) return null;
|
||||
if (outKeys.getStreamCounter() > MAX_32_BIT_UNSIGNED) return null;
|
||||
// Create a stream context
|
||||
ctx = new StreamContext(c, transportId, outKeys.getTagKey(),
|
||||
outKeys.getHeaderKey(), outKeys.getStreamCounter());
|
||||
// Increment the stream counter and write it back to the DB
|
||||
outKeys.incrementStreamCounter();
|
||||
saveIncrementedStreamCounter(c, outKeys.getRotationPeriod());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// TODO: Wait for save to complete, return null if it fails
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private void saveIncrementedStreamCounter(final ContactId c,
|
||||
final long rotationPeriod) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
db.incrementStreamCounter(c, transportId, rotationPeriod);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
StreamContext recogniseTag(byte[] tag) {
|
||||
StreamContext ctx;
|
||||
lock.lock();
|
||||
try {
|
||||
// Look up the incoming keys for the tag
|
||||
TagContext tagCtx = inContexts.remove(new Bytes(tag));
|
||||
if (tagCtx == null) return null;
|
||||
MutableIncomingKeys inKeys = tagCtx.inKeys;
|
||||
// Create a stream context
|
||||
ctx = new StreamContext(tagCtx.contactId, transportId,
|
||||
inKeys.getTagKey(), inKeys.getHeaderKey(),
|
||||
tagCtx.streamNumber);
|
||||
// Update the reordering window
|
||||
ReorderingWindow window = inKeys.getWindow();
|
||||
Change change = window.setSeen(tagCtx.streamNumber);
|
||||
// Add tags for any stream numbers added to the window
|
||||
for (long streamNumber : change.getAdded()) {
|
||||
byte[] addTag = new byte[TAG_LENGTH];
|
||||
crypto.encodeTag(addTag, inKeys.getTagKey(), streamNumber);
|
||||
inContexts.put(new Bytes(addTag), new TagContext(
|
||||
tagCtx.contactId, inKeys, streamNumber));
|
||||
}
|
||||
// Remove tags for any stream numbers removed from the window
|
||||
for (long streamNumber : change.getRemoved()) {
|
||||
byte[] removeTag = new byte[TAG_LENGTH];
|
||||
crypto.encodeTag(removeTag, inKeys.getTagKey(), streamNumber);
|
||||
inContexts.remove(new Bytes(removeTag));
|
||||
}
|
||||
// Write the window back to the DB
|
||||
saveReorderingWindow(tagCtx.contactId, inKeys.getRotationPeriod(),
|
||||
window.getBase(), window.getBitmap());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// TODO: Wait for save to complete, return null if it fails
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private void saveReorderingWindow(final ContactId c,
|
||||
final long rotationPeriod, final long base, final byte[] bitmap) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
db.setReorderingWindow(c, transportId, rotationPeriod,
|
||||
base, bitmap);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
lock.lock();
|
||||
try {
|
||||
// Rotate the keys to the current rotation period
|
||||
Map<ContactId, TransportKeys> rotated =
|
||||
new HashMap<ContactId, TransportKeys>();
|
||||
Map<ContactId, TransportKeys> current =
|
||||
new HashMap<ContactId, TransportKeys>();
|
||||
long now = clock.currentTimeMillis();
|
||||
long rotationPeriod = now / rotationPeriodLength;
|
||||
for (Entry<ContactId, MutableTransportKeys> e : keys.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
TransportKeys k = e.getValue().snapshot();
|
||||
TransportKeys k1 = crypto.rotateTransportKeys(k,
|
||||
rotationPeriod);
|
||||
if (k1.getRotationPeriod() > k.getRotationPeriod())
|
||||
rotated.put(c, k1);
|
||||
current.put(c, k1);
|
||||
}
|
||||
// Rebuild the mutable state for all contacts
|
||||
inContexts.clear();
|
||||
outContexts.clear();
|
||||
keys.clear();
|
||||
for (Entry<ContactId, TransportKeys> e : current.entrySet())
|
||||
addKeys(e.getKey(), new MutableTransportKeys(e.getValue()));
|
||||
// Write any rotated keys back to the DB
|
||||
saveTransportKeys(rotated);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static class TagContext {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final MutableIncomingKeys inKeys;
|
||||
private final long streamNumber;
|
||||
|
||||
private TagContext(ContactId contactId, MutableIncomingKeys inKeys,
|
||||
long streamNumber) {
|
||||
this.contactId = contactId;
|
||||
this.inKeys = inKeys;
|
||||
this.streamNumber = streamNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,20 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.briarproject.api.crypto.KeyManager;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.api.transport.TagRecogniser;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
public class TransportModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(StreamReaderFactory.class).to(StreamReaderFactoryImpl.class);
|
||||
bind(TagRecogniser.class).to(
|
||||
TagRecogniserImpl.class).in(Singleton.class);
|
||||
bind(StreamWriterFactory.class).to(StreamWriterFactoryImpl.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,215 +0,0 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.briarproject.api.Bytes;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
|
||||
// FIXME: Don't make alien calls with a lock held
|
||||
/**
|
||||
* A {@link org.briarproject.api.transport.TagRecogniser TagRecogniser} for a
|
||||
* specific transport.
|
||||
*/
|
||||
class TransportTagRecogniser {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final DatabaseComponent db;
|
||||
private final TransportId transportId;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
// The following are locking: lock
|
||||
private final Map<Bytes, TagContext> tagMap;
|
||||
private final Map<RemovalKey, RemovalContext> removalMap;
|
||||
|
||||
TransportTagRecogniser(CryptoComponent crypto, DatabaseComponent db,
|
||||
TransportId transportId) {
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
this.transportId = transportId;
|
||||
tagMap = new HashMap<Bytes, TagContext>();
|
||||
removalMap = new HashMap<RemovalKey, RemovalContext>();
|
||||
}
|
||||
|
||||
StreamContext recogniseTag(byte[] tag) throws DbException {
|
||||
lock.lock();
|
||||
try {
|
||||
TagContext t = tagMap.remove(new Bytes(tag));
|
||||
if (t == null) return null; // The tag was not expected
|
||||
// Update the reordering window and the expected tags
|
||||
SecretKey key = crypto.deriveTagKey(t.secret, !t.alice);
|
||||
for (long streamNumber : t.window.setSeen(t.streamNumber)) {
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
crypto.encodeTag(tag1, key, streamNumber);
|
||||
if (streamNumber < t.streamNumber) {
|
||||
TagContext removed = tagMap.remove(new Bytes(tag1));
|
||||
assert removed != null;
|
||||
} else {
|
||||
TagContext added = new TagContext(t, streamNumber);
|
||||
TagContext duplicate = tagMap.put(new Bytes(tag1), added);
|
||||
assert duplicate == null;
|
||||
}
|
||||
}
|
||||
// Store the updated reordering window in the DB
|
||||
db.setReorderingWindow(t.contactId, transportId, t.period,
|
||||
t.window.getCentre(), t.window.getBitmap());
|
||||
return new StreamContext(t.contactId, transportId, t.secret,
|
||||
t.streamNumber, t.alice);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void addSecret(TemporarySecret s) {
|
||||
lock.lock();
|
||||
try {
|
||||
ContactId contactId = s.getContactId();
|
||||
boolean alice = s.getAlice();
|
||||
long period = s.getPeriod();
|
||||
byte[] secret = s.getSecret();
|
||||
long centre = s.getWindowCentre();
|
||||
byte[] bitmap = s.getWindowBitmap();
|
||||
// Create the reordering window and the expected tags
|
||||
SecretKey key = crypto.deriveTagKey(secret, !alice);
|
||||
ReorderingWindow window = new ReorderingWindow(centre, bitmap);
|
||||
for (long streamNumber : window.getUnseen()) {
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
crypto.encodeTag(tag, key, streamNumber);
|
||||
TagContext added = new TagContext(contactId, alice, period,
|
||||
secret, window, streamNumber);
|
||||
TagContext duplicate = tagMap.put(new Bytes(tag), added);
|
||||
assert duplicate == null;
|
||||
}
|
||||
// Create a removal context to remove the window and the tags later
|
||||
RemovalContext r = new RemovalContext(window, secret, alice);
|
||||
removalMap.put(new RemovalKey(contactId, period), r);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void removeSecret(ContactId contactId, long period) {
|
||||
lock.lock();
|
||||
try {
|
||||
RemovalKey k = new RemovalKey(contactId, period);
|
||||
RemovalContext removed = removalMap.remove(k);
|
||||
if (removed == null) throw new IllegalArgumentException();
|
||||
removeSecret(removed);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// Locking: lock
|
||||
private void removeSecret(RemovalContext r) {
|
||||
// Remove the expected tags
|
||||
SecretKey key = crypto.deriveTagKey(r.secret, !r.alice);
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
for (long streamNumber : r.window.getUnseen()) {
|
||||
crypto.encodeTag(tag, key, streamNumber);
|
||||
TagContext removed = tagMap.remove(new Bytes(tag));
|
||||
assert removed != null;
|
||||
}
|
||||
}
|
||||
|
||||
void removeSecrets(ContactId c) {
|
||||
lock.lock();
|
||||
try {
|
||||
Collection<RemovalKey> keysToRemove = new ArrayList<RemovalKey>();
|
||||
for (RemovalKey k : removalMap.keySet())
|
||||
if (k.contactId.equals(c)) keysToRemove.add(k);
|
||||
for (RemovalKey k : keysToRemove)
|
||||
removeSecret(k.contactId, k.period);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void removeSecrets() {
|
||||
lock.lock();
|
||||
try {
|
||||
for (RemovalContext r : removalMap.values()) removeSecret(r);
|
||||
assert tagMap.isEmpty();
|
||||
removalMap.clear();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static class TagContext {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final boolean alice;
|
||||
private final long period;
|
||||
private final byte[] secret;
|
||||
private final ReorderingWindow window;
|
||||
private final long streamNumber;
|
||||
|
||||
private TagContext(ContactId contactId, boolean alice, long period,
|
||||
byte[] secret, ReorderingWindow window, long streamNumber) {
|
||||
this.contactId = contactId;
|
||||
this.alice = alice;
|
||||
this.period = period;
|
||||
this.secret = secret;
|
||||
this.window = window;
|
||||
this.streamNumber = streamNumber;
|
||||
}
|
||||
|
||||
private TagContext(TagContext t, long streamNumber) {
|
||||
this(t.contactId, t.alice, t.period, t.secret, t.window,
|
||||
streamNumber);
|
||||
}
|
||||
}
|
||||
|
||||
private static class RemovalKey {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final long period;
|
||||
|
||||
private RemovalKey(ContactId contactId, long period) {
|
||||
this.contactId = contactId;
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return contactId.hashCode() ^ (int) (period ^ (period >>> 32));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof RemovalKey) {
|
||||
RemovalKey k = (RemovalKey) o;
|
||||
return contactId.equals(k.contactId) && period == k.period;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static class RemovalContext {
|
||||
|
||||
private final ReorderingWindow window;
|
||||
private final byte[] secret;
|
||||
private final boolean alice;
|
||||
|
||||
private RemovalContext(ReorderingWindow window, byte[] secret,
|
||||
boolean alice) {
|
||||
this.window = window;
|
||||
this.secret = secret;
|
||||
this.alice = alice;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user