Move DB key management into account manager.

This commit is contained in:
akwizgran
2018-07-27 11:35:27 +01:00
parent 4a9977fa58
commit 6ca0339da2
13 changed files with 123 additions and 166 deletions

View File

@@ -1,7 +1,6 @@
package org.briarproject.briar.android.login;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.ImmediateExecutor;
@@ -13,46 +12,28 @@ import java.util.concurrent.atomic.AtomicBoolean;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.briarproject.bramble.util.StringUtils.toHexString;
public class PasswordControllerImplTest extends BrambleMockTestCase {
private final AccountManager accountManager =
context.mock(AccountManager.class);
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
private final PasswordStrengthEstimator estimator =
context.mock(PasswordStrengthEstimator.class);
private final Executor cryptoExecutor = new ImmediateExecutor();
private final Executor ioExecutor = new ImmediateExecutor();
private final String oldPassword = "some.old.pass";
private final String newPassword = "some.new.pass";
private final byte[] oldEncryptedKey = getRandomBytes(123);
private final byte[] newEncryptedKey = getRandomBytes(123);
private final byte[] key = getSecretKey().getBytes();
private final String oldEncryptedKeyHex = toHexString(oldEncryptedKey);
private final String newEncryptedKeyHex = toHexString(newEncryptedKey);
@Test
public void testChangePasswordReturnsTrue() {
context.checking(new Expectations() {{
// Look up the encrypted DB key
oneOf(accountManager).getEncryptedDatabaseKey();
will(returnValue(oldEncryptedKeyHex));
// Decrypt and re-encrypt the key
oneOf(crypto).decryptWithPassword(oldEncryptedKey, oldPassword);
will(returnValue(key));
oneOf(crypto).encryptWithPassword(key, newPassword);
will(returnValue(newEncryptedKey));
// Store the new key
oneOf(accountManager).storeEncryptedDatabaseKey(newEncryptedKeyHex);
oneOf(accountManager).changePassword(oldPassword, newPassword);
will(returnValue(true));
}});
PasswordControllerImpl p = new PasswordControllerImpl(accountManager,
cryptoExecutor, crypto, estimator);
ioExecutor, estimator);
AtomicBoolean capturedResult = new AtomicBoolean(false);
p.changePassword(oldPassword, newPassword, capturedResult::set);
@@ -62,16 +43,12 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
@Test
public void testChangePasswordReturnsFalseIfOldPasswordIsWrong() {
context.checking(new Expectations() {{
// Look up the encrypted DB key
oneOf(accountManager).getEncryptedDatabaseKey();
will(returnValue(oldEncryptedKeyHex));
// Try to decrypt the key - the password is wrong
oneOf(crypto).decryptWithPassword(oldEncryptedKey, oldPassword);
will(returnValue(null));
oneOf(accountManager).changePassword(oldPassword, newPassword);
will(returnValue(false));
}});
PasswordControllerImpl p = new PasswordControllerImpl(accountManager,
cryptoExecutor, crypto, estimator);
ioExecutor, estimator);
AtomicBoolean capturedResult = new AtomicBoolean(true);
p.changePassword(oldPassword, newPassword, capturedResult::set);

View File

@@ -1,9 +1,7 @@
package org.briarproject.briar.android.login;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.test.BrambleMockTestCase;
@@ -18,30 +16,23 @@ import java.util.concurrent.atomic.AtomicBoolean;
import static junit.framework.Assert.assertTrue;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.toHexString;
public class SetupControllerImplTest extends BrambleMockTestCase {
private final AccountManager accountManager =
context.mock(AccountManager.class);
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
private final PasswordStrengthEstimator estimator =
context.mock(PasswordStrengthEstimator.class);
private final IdentityManager identityManager =
context.mock(IdentityManager.class);
private final SetupActivity setupActivity;
private final Executor cryptoExecutor = new ImmediateExecutor();
private final Executor ioExecutor = new ImmediateExecutor();
private final String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
private final String password = "some.strong.pass";
private final LocalAuthor localAuthor = getLocalAuthor();
private final byte[] encryptedKey = getRandomBytes(123);
private final String encryptedKeyHex = toHexString(encryptedKey);
private final SecretKey key = getSecretKey();
public SetupControllerImplTest() {
context.setImposteriser(ClassImposteriser.INSTANCE);
@@ -64,21 +55,13 @@ public class SetupControllerImplTest extends BrambleMockTestCase {
oneOf(identityManager).createLocalAuthor(authorName);
will(returnValue(localAuthor));
oneOf(identityManager).registerLocalAuthor(localAuthor);
// Generate a database key
oneOf(crypto).generateSecretKey();
will(returnValue(key));
// Encrypt the key with the password
oneOf(crypto).encryptWithPassword(key.getBytes(), password);
will(returnValue(encryptedKey));
// Store the encrypted key
oneOf(accountManager).storeEncryptedDatabaseKey(encryptedKeyHex);
// Create the account
oneOf(accountManager).createAccount(password);
will(returnValue(true));
// Pass the database key to the account manager
oneOf(accountManager).setDatabaseKey(key);
}});
SetupControllerImpl s = new SetupControllerImpl(accountManager,
cryptoExecutor, crypto, estimator, identityManager);
ioExecutor, estimator, identityManager);
s.setSetupActivity(setupActivity);
AtomicBoolean called = new AtomicBoolean(false);