From 304027694233b5945335668e291a02ab964e168e Mon Sep 17 00:00:00 2001 From: Impyy Date: Wed, 3 May 2017 21:34:33 +0200 Subject: [PATCH] Make the slot lookup code a little nicer --- .../main/java/me/impy/aegis/MainActivity.java | 38 +++++++++---------- .../aegis/crypto/slots/SlotCollection.java | 4 ++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/me/impy/aegis/MainActivity.java b/app/src/main/java/me/impy/aegis/MainActivity.java index 8b87dd5f..44b7f6b3 100644 --- a/app/src/main/java/me/impy/aegis/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/MainActivity.java @@ -44,7 +44,9 @@ import me.impy.aegis.crypto.CryptResult; import me.impy.aegis.crypto.CryptoUtils; import me.impy.aegis.crypto.MasterKey; 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.RawSlot; import me.impy.aegis.crypto.slots.Slot; import me.impy.aegis.crypto.slots.SlotCollection; import me.impy.aegis.db.Database; @@ -364,12 +366,7 @@ public class MainActivity extends AppCompatActivity { private void createDatabase() { database = new Database(); - try { - databaseFile = new DatabaseFile(); - } catch (Exception e) { - // TODO: tell the user to stop using a weird platform - throw new UndeclaredThrowableException(e); - } + databaseFile = new DatabaseFile(); try { masterKey = new MasterKey(null); @@ -383,7 +380,7 @@ public class MainActivity extends AppCompatActivity { try { PasswordSlot slot = new PasswordSlot(); 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); masterKey.encryptSlot(slot, cipher); slots.add(slot); @@ -407,21 +404,22 @@ public class MainActivity extends AppCompatActivity { byte[] content = databaseFile.getContent(); if (databaseFile.isEncrypted()) { - SlotCollection slots = databaseFile.getSlots(); - for (Slot slot : slots) { - if (slot instanceof PasswordSlot) { - try { - PasswordSlot derSlot = (PasswordSlot)slot; - SecretKey derivedKey = derSlot.deriveKey("testpassword".toCharArray()); - Cipher cipher = Slot.createCipher(derivedKey, Cipher.DECRYPT_MODE); - masterKey = MasterKey.decryptSlot(slot, cipher); - } catch (Exception e) { - throw new UndeclaredThrowableException(e); - } - break; + try { + SlotCollection slots = databaseFile.getSlots(); + // look up slots in order of preference + if (slots.has(FingerprintSlot.class)) { + FingerprintSlot slot = slots.find(FingerprintSlot.class); + } else if (slots.has(PasswordSlot.class)) { + PasswordSlot slot = slots.find(PasswordSlot.class); + SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray()); + Cipher cipher = Slot.createCipher(derivedKey, Cipher.DECRYPT_MODE); + masterKey = MasterKey.decryptSlot(slot, cipher); + //} else if (slots.has(RawSlot.class)) { } else { - + throw new Exception("the slot collection doesn't contain any supported slot types"); } + } catch (Exception e) { + throw new UndeclaredThrowableException(e); } CryptResult result; diff --git a/app/src/main/java/me/impy/aegis/crypto/slots/SlotCollection.java b/app/src/main/java/me/impy/aegis/crypto/slots/SlotCollection.java index b4eb01f9..7422c568 100644 --- a/app/src/main/java/me/impy/aegis/crypto/slots/SlotCollection.java +++ b/app/src/main/java/me/impy/aegis/crypto/slots/SlotCollection.java @@ -77,6 +77,10 @@ public class SlotCollection implements Iterable { return null; } + public boolean has(Class type) { + return find(type) != null; + } + @Override public Iterator iterator() { return _slots.iterator();