From 85683a57f14d7fb95e08ad7a4bce419b31a8d321 Mon Sep 17 00:00:00 2001 From: ameba23 Date: Tue, 15 Jun 2021 21:10:33 +0200 Subject: [PATCH] Use a provider to instantiate AuthenticatedCipher on each use, to prevent concurrency problems --- .../socialbackup/recovery/CustodianTaskImpl.java | 9 ++++----- .../recovery/ReturnShardTaskImpl.java | 16 +++++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/CustodianTaskImpl.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/CustodianTaskImpl.java index 7a6e5dc4e..78d9e659d 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/CustodianTaskImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/CustodianTaskImpl.java @@ -19,6 +19,7 @@ import java.security.GeneralSecurityException; import java.util.logging.Logger; import javax.inject.Inject; +import javax.inject.Provider; import static java.util.logging.Logger.getLogger; @@ -29,7 +30,6 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl private final ClientHelper clientHelper; private InetSocketAddress remoteSocketAddress; private Socket socket; - private final AuthenticatedCipher cipher; private byte[] payload; private static final Logger LOG = @@ -37,11 +37,10 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl @Inject CustodianTaskImpl(CryptoComponent crypto, ClientHelper clientHelper, - AuthenticatedCipher cipher) { - super(cipher, crypto); + Provider cipherProvider) { + super(cipherProvider, crypto); this.clientHelper = clientHelper; - this.cipher = cipher; } @Override @@ -141,7 +140,7 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl DataInputStream inputStream = new DataInputStream(socket.getInputStream()); byte[] ackNonce = read(inputStream, NONCE_LENGTH); byte[] ackMessageEncrypted = - read(inputStream, 3 + cipher.getMacBytes()); + read(inputStream, 3 + AUTH_TAG_BYTES); byte[] ackMessage = decrypt(ackMessageEncrypted, ackNonce); String ackMessageString = new String(ackMessage); LOG.info("Received ack message: " + new String(ackMessage)); diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/ReturnShardTaskImpl.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/ReturnShardTaskImpl.java index e146d8193..f42a830c7 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/ReturnShardTaskImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/recovery/ReturnShardTaskImpl.java @@ -5,24 +5,28 @@ import org.briarproject.bramble.api.crypto.AuthenticatedCipher; import org.briarproject.bramble.api.crypto.CryptoComponent; import org.briarproject.bramble.api.crypto.KeyPair; import org.briarproject.bramble.api.crypto.SecretKey; +import org.briarproject.briar.socialbackup.SocialBackupConstants; import java.io.DataInputStream; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.SecureRandom; +import javax.inject.Provider; + public class ReturnShardTaskImpl { - private final AuthenticatedCipher cipher; + private final Provider cipherProvider; private final CryptoComponent crypto; private final SecureRandom secureRandom; - final int TIMEOUT = 120 * 1000; - final int NONCE_LENGTH = 24; // TODO get these constants + final int NONCE_LENGTH = SocialBackupConstants.NONCE_BYTES; + final int AUTH_TAG_BYTES = SocialBackupConstants.AUTH_TAG_BYTES; + final int TIMEOUT = 120 * 1000; // TODO move to SocialBackupConstants final int AGREEMENT_PUBLIC_KEY_LENGTH = 32; SecretKey sharedSecret; final KeyPair localKeyPair; - ReturnShardTaskImpl(AuthenticatedCipher cipher, CryptoComponent crypto) { - this.cipher = cipher; + ReturnShardTaskImpl(Provider cipherProvider, CryptoComponent crypto) { + this.cipherProvider = cipherProvider; this.crypto = crypto; this.secureRandom = crypto.getSecureRandom(); localKeyPair = crypto.generateAgreementKeyPair(); @@ -44,6 +48,7 @@ public class ReturnShardTaskImpl { byte[] encrypt(byte[] message, byte[] nonce) throws GeneralSecurityException { + AuthenticatedCipher cipher = cipherProvider.get(); cipher.init(true, sharedSecret, nonce); byte[] cipherText = new byte[message.length + cipher.getMacBytes()]; cipher.process(message, 0, message.length, cipherText, 0); @@ -52,6 +57,7 @@ public class ReturnShardTaskImpl { byte[] decrypt(byte[] cipherText, byte[] nonce) throws GeneralSecurityException { + AuthenticatedCipher cipher = cipherProvider.get(); cipher.init(false, sharedSecret, nonce); byte[] message = new byte[cipherText.length - cipher.getMacBytes()]; cipher.process(cipherText, 0, cipherText.length, message, 0);