Make the slot lookup code a little nicer

This commit is contained in:
Impyy 2017-05-03 21:34:33 +02:00 committed by Alexander Bakker
parent e3f4503967
commit 3040276942
2 changed files with 22 additions and 20 deletions

View file

@ -44,7 +44,9 @@ import me.impy.aegis.crypto.CryptResult;
import me.impy.aegis.crypto.CryptoUtils; import me.impy.aegis.crypto.CryptoUtils;
import me.impy.aegis.crypto.MasterKey; import me.impy.aegis.crypto.MasterKey;
import me.impy.aegis.crypto.otp.OTP; import me.impy.aegis.crypto.otp.OTP;
import me.impy.aegis.crypto.slots.FingerprintSlot;
import me.impy.aegis.crypto.slots.PasswordSlot; import me.impy.aegis.crypto.slots.PasswordSlot;
import me.impy.aegis.crypto.slots.RawSlot;
import me.impy.aegis.crypto.slots.Slot; import me.impy.aegis.crypto.slots.Slot;
import me.impy.aegis.crypto.slots.SlotCollection; import me.impy.aegis.crypto.slots.SlotCollection;
import me.impy.aegis.db.Database; import me.impy.aegis.db.Database;
@ -364,12 +366,7 @@ public class MainActivity extends AppCompatActivity {
private void createDatabase() { private void createDatabase() {
database = new Database(); database = new Database();
try { databaseFile = new DatabaseFile();
databaseFile = new DatabaseFile();
} catch (Exception e) {
// TODO: tell the user to stop using a weird platform
throw new UndeclaredThrowableException(e);
}
try { try {
masterKey = new MasterKey(null); masterKey = new MasterKey(null);
@ -383,7 +380,7 @@ public class MainActivity extends AppCompatActivity {
try { try {
PasswordSlot slot = new PasswordSlot(); PasswordSlot slot = new PasswordSlot();
byte[] salt = CryptoUtils.generateSalt(); byte[] salt = CryptoUtils.generateSalt();
SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray(), salt, 1000); SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray(), salt, CryptoUtils.CRYPTO_ITERATION_COUNT);
Cipher cipher = Slot.createCipher(derivedKey, Cipher.ENCRYPT_MODE); Cipher cipher = Slot.createCipher(derivedKey, Cipher.ENCRYPT_MODE);
masterKey.encryptSlot(slot, cipher); masterKey.encryptSlot(slot, cipher);
slots.add(slot); slots.add(slot);
@ -407,21 +404,22 @@ public class MainActivity extends AppCompatActivity {
byte[] content = databaseFile.getContent(); byte[] content = databaseFile.getContent();
if (databaseFile.isEncrypted()) { if (databaseFile.isEncrypted()) {
SlotCollection slots = databaseFile.getSlots(); try {
for (Slot slot : slots) { SlotCollection slots = databaseFile.getSlots();
if (slot instanceof PasswordSlot) { // look up slots in order of preference
try { if (slots.has(FingerprintSlot.class)) {
PasswordSlot derSlot = (PasswordSlot)slot; FingerprintSlot slot = slots.find(FingerprintSlot.class);
SecretKey derivedKey = derSlot.deriveKey("testpassword".toCharArray()); } else if (slots.has(PasswordSlot.class)) {
Cipher cipher = Slot.createCipher(derivedKey, Cipher.DECRYPT_MODE); PasswordSlot slot = slots.find(PasswordSlot.class);
masterKey = MasterKey.decryptSlot(slot, cipher); SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray());
} catch (Exception e) { Cipher cipher = Slot.createCipher(derivedKey, Cipher.DECRYPT_MODE);
throw new UndeclaredThrowableException(e); masterKey = MasterKey.decryptSlot(slot, cipher);
} //} else if (slots.has(RawSlot.class)) {
break;
} else { } else {
throw new Exception("the slot collection doesn't contain any supported slot types");
} }
} catch (Exception e) {
throw new UndeclaredThrowableException(e);
} }
CryptResult result; CryptResult result;

View file

@ -77,6 +77,10 @@ public class SlotCollection implements Iterable<Slot> {
return null; return null;
} }
public <T extends Slot> boolean has(Class<T> type) {
return find(type) != null;
}
@Override @Override
public Iterator<Slot> iterator() { public Iterator<Slot> iterator() {
return _slots.iterator(); return _slots.iterator();