From 10c32266c2997bb6fcdfe3c0a57ed9a022e5425b Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Mon, 26 Mar 2018 16:04:38 +0200 Subject: [PATCH] Add accessors to PwDatabase --- .../com/keepassdroid/tests/PwEntryTestV3.java | 2 +- .../tests/database/DeleteEntry.java | 6 +- .../keepassdroid/tests/database/EntryV4.java | 2 +- .../tests/database/Kdb3Twofish.java | 3 +- .../tests/database/Kdb4Header.java | 4 +- .../tests/database/SprEngineTest.java | 2 +- .../tests/output/PwManagerOutputTest.java | 10 +- .../activities/EntryActivity.java | 2 +- .../activities/EntryEditActivity.java | 6 +- .../activities/GroupActivity.java | 4 +- .../activities/ListNodesActivity.java | 4 +- .../com/keepassdroid/database/BinaryPool.java | 1 - .../com/keepassdroid/database/Database.java | 2 +- .../database/MemoryProtectionConfig.java | 22 ++ .../com/keepassdroid/database/PwDatabase.java | 114 ++++-- .../keepassdroid/database/PwDatabaseV3.java | 112 +++--- .../database/PwDatabaseV3Debug.java | 14 +- .../keepassdroid/database/PwDatabaseV4.java | 370 ++++++++++++++---- .../database/PwDatabaseV4Debug.java | 26 -- .../database/PwDatabaseV4XML.java | 1 - .../keepassdroid/database/PwDbHeaderV4.java | 26 +- .../com/keepassdroid/database/PwEntryV3.java | 2 +- .../com/keepassdroid/database/PwEntryV4.java | 14 +- .../com/keepassdroid/database/PwGroupV3.java | 2 +- .../keepassdroid/database/PwIconFactory.java | 6 +- .../keepassdroid/database/edit/AddGroup.java | 2 +- .../database/edit/SetPassword.java | 8 +- .../database/load/ImporterV3.java | 24 +- .../database/load/ImporterV3Debug.java | 1 - .../database/load/ImporterV4.java | 98 +++-- .../database/load/ImporterV4Debug.java | 44 --- .../database/save/PwDbHeaderOutputV4.java | 16 +- .../save/PwDbInnerHeaderOutputV4.java | 2 +- .../database/save/PwDbV3Output.java | 24 +- .../database/save/PwDbV3OutputDebug.java | 2 +- .../database/save/PwDbV4Output.java | 84 ++-- .../keepassdroid/search/SearchDbHelper.java | 6 +- .../settings/NestedSettingsFragment.java | 4 +- .../com/keepassdroid/utils/SprEngineV4.java | 4 +- 39 files changed, 656 insertions(+), 420 deletions(-) create mode 100644 app/src/main/java/com/keepassdroid/database/MemoryProtectionConfig.java delete mode 100644 app/src/main/java/com/keepassdroid/database/PwDatabaseV4Debug.java delete mode 100644 app/src/main/java/com/keepassdroid/database/load/ImporterV4Debug.java diff --git a/app/src/androidTest/java/com/keepassdroid/tests/PwEntryTestV3.java b/app/src/androidTest/java/com/keepassdroid/tests/PwEntryTestV3.java index bfc8aa58d..ad58f6fe9 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/PwEntryTestV3.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/PwEntryTestV3.java @@ -37,7 +37,7 @@ public class PwEntryTestV3 extends AndroidTestCase { protected void setUp() throws Exception { super.setUp(); - mPE = (PwEntryV3) TestData.GetTest1(getContext()).entries.get(0); + mPE = (PwEntryV3) TestData.GetTest1(getContext()).getEntryAt(0); } diff --git a/app/src/androidTest/java/com/keepassdroid/tests/database/DeleteEntry.java b/app/src/androidTest/java/com/keepassdroid/tests/database/DeleteEntry.java index 4048b820e..f2d4304dd 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/database/DeleteEntry.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/database/DeleteEntry.java @@ -80,13 +80,11 @@ public class DeleteEntry extends AndroidTestCase { // Verify the group was deleted group1 = getGroup(pm, GROUP1_NAME); assertNull("Group 1 was not removed.", group1); - - - + } private PwEntry getEntry(PwDatabaseV3 pm, String name) { - List entries = pm.entries; + List entries = pm.getEntries(); for ( int i = 0; i < entries.size(); i++ ) { PwEntry entry = entries.get(i); if ( entry.getTitle().equals(name) ) { diff --git a/app/src/androidTest/java/com/keepassdroid/tests/database/EntryV4.java b/app/src/androidTest/java/com/keepassdroid/tests/database/EntryV4.java index 452bdaffd..43297f407 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/database/EntryV4.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/database/EntryV4.java @@ -29,7 +29,7 @@ public class EntryV4 extends TestCase { public void testBackup() { PwDatabaseV4 db = new PwDatabaseV4(); - db.historyMaxItems = 2; + db.setHistoryMaxItems(2); PwEntryV4 entry = new PwEntryV4(); entry.startToDecodeReference(db); diff --git a/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb3Twofish.java b/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb3Twofish.java index 5d3ba4852..f0fb98fba 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb3Twofish.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb3Twofish.java @@ -40,10 +40,9 @@ public class Kdb3Twofish extends AndroidTestCase { PwDatabaseV3 db = importer.openDatabase(is, "12345", null); - assertTrue(db.algorithm == PwEncryptionAlgorithm.Twofish); + assertTrue(db.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Twofish); is.close(); } - } diff --git a/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb4Header.java b/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb4Header.java index 6d25e0257..9f52727f6 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb4Header.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/database/Kdb4Header.java @@ -41,9 +41,9 @@ public class Kdb4Header extends AndroidTestCase { PwDatabaseV4 db = importer.openDatabase(is, "12345", null); - assertEquals(6000, db.numKeyEncRounds); + assertEquals(6000, db.getNumKeyEncRounds()); - assertTrue(db.dataCipher.equals(AesEngine.CIPHER_UUID)); + assertTrue(db.getDataCipher().equals(AesEngine.CIPHER_UUID)); is.close(); diff --git a/app/src/androidTest/java/com/keepassdroid/tests/database/SprEngineTest.java b/app/src/androidTest/java/com/keepassdroid/tests/database/SprEngineTest.java index e45d625ad..280309acb 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/database/SprEngineTest.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/database/SprEngineTest.java @@ -61,7 +61,7 @@ public class SprEngineTest extends AndroidTestCase { public void testRefReplace() { UUID entryUUID = decodeUUID(ENCODE_UUID); - PwEntryV4 entry = (PwEntryV4) db.entries.get(entryUUID); + PwEntryV4 entry = (PwEntryV4) db.getEntryByUUIDId(entryUUID); assertEquals(RESULT, spr.compile(REF, entry, db)); diff --git a/app/src/androidTest/java/com/keepassdroid/tests/output/PwManagerOutputTest.java b/app/src/androidTest/java/com/keepassdroid/tests/output/PwManagerOutputTest.java index 42de93c20..4f329c6ae 100644 --- a/app/src/androidTest/java/com/keepassdroid/tests/output/PwManagerOutputTest.java +++ b/app/src/androidTest/java/com/keepassdroid/tests/output/PwManagerOutputTest.java @@ -60,7 +60,7 @@ public class PwManagerOutputTest extends AndroidTestCase { pos.outputPlanGroupAndEntries(bos); assertTrue("No output", bos.toByteArray().length > 0); - assertArrayEquals("Group and entry output doesn't match.", mPM.postHeader, bos.toByteArray()); + assertArrayEquals("Group and entry output doesn't match.", mPM.getPostHeader(), bos.toByteArray()); } @@ -77,7 +77,7 @@ public class PwManagerOutputTest extends AndroidTestCase { byte[] digest = md.digest(); assertTrue("No output", digest.length > 0); - assertArrayEquals("Hash of groups and entries failed.", mPM.dbHeader.contentsHash, digest); + assertArrayEquals("Hash of groups and entries failed.", mPM.getDbHeader().contentsHash, digest); } private void assertHeadersEquals(PwDbHeaderV3 expected, PwDbHeaderV3 actual) { @@ -100,10 +100,10 @@ public class PwManagerOutputTest extends AndroidTestCase { PwDbHeaderV3 header = pActual.outputHeader(bActual); ByteArrayOutputStream bExpected = new ByteArrayOutputStream(); - PwDbHeaderOutputV3 outExpected = new PwDbHeaderOutputV3(mPM.dbHeader, bExpected); + PwDbHeaderOutputV3 outExpected = new PwDbHeaderOutputV3(mPM.getDbHeader(), bExpected); outExpected.output(); - assertHeadersEquals(mPM.dbHeader, header); + assertHeadersEquals(mPM.getDbHeader(), header); assertTrue("No output", bActual.toByteArray().length > 0); assertArrayEquals("Header does not match.", bExpected.toByteArray(), bActual.toByteArray()); } @@ -114,7 +114,7 @@ public class PwManagerOutputTest extends AndroidTestCase { PwDbHeader hActual = pActual.outputHeader(bActual); byte[] finalKey = pActual.getFinalKey(hActual); - assertArrayEquals("Keys mismatched", mPM.finalKey, finalKey); + assertArrayEquals("Keys mismatched", mPM.getFinalKey(), finalKey); } diff --git a/app/src/main/java/com/keepassdroid/activities/EntryActivity.java b/app/src/main/java/com/keepassdroid/activities/EntryActivity.java index c2c54289c..a59648391 100644 --- a/app/src/main/java/com/keepassdroid/activities/EntryActivity.java +++ b/app/src/main/java/com/keepassdroid/activities/EntryActivity.java @@ -107,7 +107,7 @@ public class EntryActivity extends LockingHideActivity { // Get Entry from UUID Intent i = getIntent(); UUID uuid = Types.bytestoUUID(i.getByteArrayExtra(KEY_ENTRY)); - mEntry = db.pm.entries.get(uuid); + mEntry = db.pm.getEntryByUUIDId(uuid); if (mEntry == null) { Toast.makeText(this, R.string.entry_not_found, Toast.LENGTH_LONG).show(); finish(); diff --git a/app/src/main/java/com/keepassdroid/activities/EntryEditActivity.java b/app/src/main/java/com/keepassdroid/activities/EntryEditActivity.java index 1de4863a9..4267b7d36 100644 --- a/app/src/main/java/com/keepassdroid/activities/EntryEditActivity.java +++ b/app/src/main/java/com/keepassdroid/activities/EntryEditActivity.java @@ -153,12 +153,12 @@ public class EntryEditActivity extends LockingHideActivity PwDatabase pm = db.pm; if ( uuidBytes == null ) { PwGroupId parentId = (PwGroupId) intent.getSerializableExtra(KEY_PARENT); - PwGroup parent = pm.groups.get(parentId); + PwGroup parent = pm.getGroupByGroupId(parentId); mEntry = PwEntry.getInstance(parent); mIsNew = true; } else { UUID uuid = Types.bytestoUUID(uuidBytes); - mEntry = pm.entries.get(uuid); + mEntry = pm.getEntryByUUIDId(uuid); mIsNew = false; fillData(); } @@ -258,7 +258,7 @@ public class EntryEditActivity extends LockingHideActivity newEntry.setIcon(new PwIconStandard(mSelectedIconID)); else { if (mIsNew) { - newEntry.setIcon(App.getDB().pm.iconFactory.getIcon(0)); + newEntry.setIcon(App.getDB().pm.getIconFactory().getFirstIcon()); } else { // Keep previous icon, if no new one was selected diff --git a/app/src/main/java/com/keepassdroid/activities/GroupActivity.java b/app/src/main/java/com/keepassdroid/activities/GroupActivity.java index 5fe11d27d..6c3f05caa 100644 --- a/app/src/main/java/com/keepassdroid/activities/GroupActivity.java +++ b/app/src/main/java/com/keepassdroid/activities/GroupActivity.java @@ -179,14 +179,14 @@ public class GroupActivity extends ListNodesActivity PwGroup currentGroup; Database db = App.getDB(); readOnly = db.readOnly; - PwGroup root = db.pm.rootGroup; + PwGroup root = db.pm.getRootGroup(); Log.w(TAG, "Creating tree view"); PwGroupId pwGroupId = (PwGroupId) getIntent().getSerializableExtra(GROUP_ID_KEY); if ( pwGroupId == null ) { currentGroup = root; } else { - currentGroup = db.pm.groups.get(pwGroupId); + currentGroup = db.pm.getGroupByGroupId(pwGroupId); } if (currentGroup != null) { diff --git a/app/src/main/java/com/keepassdroid/activities/ListNodesActivity.java b/app/src/main/java/com/keepassdroid/activities/ListNodesActivity.java index 7396403aa..8c9e6933d 100644 --- a/app/src/main/java/com/keepassdroid/activities/ListNodesActivity.java +++ b/app/src/main/java/com/keepassdroid/activities/ListNodesActivity.java @@ -180,7 +180,7 @@ public abstract class ListNodesActivity extends LockingActivity PwDatabase database = App.getDB().pm; /* // TODO Recycle bin bottom - if (database.isRecycleBinAvailable() && database.isRecycleBinEnable()) { + if (database.isRecycleBinAvailable() && database.isRecycleBinEnabled()) { sortDialogFragment = SortDialogFragment.getInstance( PrefsUtil.getListSort(this), @@ -295,7 +295,7 @@ public abstract class ListNodesActivity extends LockingActivity mAdapter.removeNode(pwNode); PwGroup parent = pwNode.getParent(); PwDatabase database = App.getDB().pm; - if (database.isRecycleBinAvailable() && database.isRecycleBinEnable()) { + if (database.isRecycleBinAvailable() && database.isRecycleBinEnabled()) { PwGroup recycleBin = database.getRecycleBin(); // Add trash if it doesn't exists if (parent.equals(recycleBin) diff --git a/app/src/main/java/com/keepassdroid/database/BinaryPool.java b/app/src/main/java/com/keepassdroid/database/BinaryPool.java index 372c23a6b..98b88699e 100644 --- a/app/src/main/java/com/keepassdroid/database/BinaryPool.java +++ b/app/src/main/java/com/keepassdroid/database/BinaryPool.java @@ -31,7 +31,6 @@ public class BinaryPool { private HashMap pool = new HashMap<>(); public BinaryPool() { - } public BinaryPool(PwGroupV4 rootGroup) { diff --git a/app/src/main/java/com/keepassdroid/database/Database.java b/app/src/main/java/com/keepassdroid/database/Database.java index 604d0a778..8ffc0cbc7 100644 --- a/app/src/main/java/com/keepassdroid/database/Database.java +++ b/app/src/main/java/com/keepassdroid/database/Database.java @@ -150,7 +150,7 @@ public class Database { pm = imp.openDatabase(bis, password, kfIs, status, roundsFix); if ( pm != null ) { - PwGroup root = pm.rootGroup; + PwGroup root = pm.getRootGroup(); pm.populateGlobals(root); LoadData(ctx, pm, password, kfIs, status); } diff --git a/app/src/main/java/com/keepassdroid/database/MemoryProtectionConfig.java b/app/src/main/java/com/keepassdroid/database/MemoryProtectionConfig.java new file mode 100644 index 000000000..1f81eac2f --- /dev/null +++ b/app/src/main/java/com/keepassdroid/database/MemoryProtectionConfig.java @@ -0,0 +1,22 @@ +package com.keepassdroid.database; + +public class MemoryProtectionConfig { + + public boolean protectTitle = false; + public boolean protectUserName = false; + public boolean protectPassword = false; + public boolean protectUrl = false; + public boolean protectNotes = false; + + public boolean autoEnableVisualHiding = false; + + public boolean isProtected(String field) { + if (field.equalsIgnoreCase(PwDefsV4.TITLE_FIELD)) return protectTitle; + if (field.equalsIgnoreCase(PwDefsV4.USERNAME_FIELD)) return protectUserName; + if (field.equalsIgnoreCase(PwDefsV4.PASSWORD_FIELD)) return protectPassword; + if (field.equalsIgnoreCase(PwDefsV4.URL_FIELD)) return protectUrl; + if (field.equalsIgnoreCase(PwDefsV4.NOTES_FIELD)) return protectNotes; + + return false; + } +} diff --git a/app/src/main/java/com/keepassdroid/database/PwDatabase.java b/app/src/main/java/com/keepassdroid/database/PwDatabase.java index 359d703b7..a9576fd5a 100644 --- a/app/src/main/java/com/keepassdroid/database/PwDatabase.java +++ b/app/src/main/java/com/keepassdroid/database/PwDatabase.java @@ -42,14 +42,16 @@ import com.keepassdroid.utils.Util; public abstract class PwDatabase { public static final UUID UUID_ZERO = new UUID(0,0); - public byte masterKey[] = new byte[32]; - public byte[] finalKey; - public String name = "KeePass database"; - public PwGroup rootGroup; - public PwIconFactory iconFactory = new PwIconFactory(); - public Map groups = new HashMap<>(); - public Map entries = new HashMap<>(); + protected byte masterKey[] = new byte[32]; + protected byte[] finalKey; + + protected String name = "KeePass database"; + protected PwGroup rootGroup; + protected PwIconFactory iconFactory = new PwIconFactory(); + + protected Map groups = new HashMap<>(); + protected Map entries = new HashMap<>(); private static boolean isKDBExtension(String filename) { if (filename == null) { return false; } @@ -68,10 +70,42 @@ public abstract class PwDatabase { } } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public PwGroup getRootGroup() { + return rootGroup; + } + + public void setRootGroup(PwGroup rootGroup) { + this.rootGroup = rootGroup; + } + + public PwIconFactory getIconFactory() { + return iconFactory; + } + + public byte[] getMasterKey() { + return masterKey; + } + + public void setMasterKey(byte[] masterKey) { + this.masterKey = masterKey; + } + + public byte[] getFinalKey() { + return finalKey; + } + public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException { // Write checksum Checksum - MessageDigest md = null; + MessageDigest md; try { md = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { @@ -87,13 +121,11 @@ public abstract class PwDatabase { finalKey = md.digest(); } - /** * Encrypt the master key a few times to make brute-force key-search harder * @throws IOException */ - protected static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds ) throws IOException - { + protected static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds ) throws IOException { FinalKey key = FinalKeyFactory.createFinalKey(); return key.transformMasterKey(pKeySeed, pKey, rounds); @@ -237,19 +269,17 @@ public abstract class PwDatabase { return md.digest(); } - public abstract List getGrpRoots(); - - public abstract List getGroups(); - - public abstract List getEntries(); - public abstract long getNumRounds(); public abstract void setNumRounds(long rounds) throws NumberFormatException; public abstract boolean algorithmSettingsEnabled(); - public abstract PwEncryptionAlgorithm getEncAlgorithm(); + public abstract PwEncryptionAlgorithm getEncryptionAlgorithm(); + + public abstract List getGrpRoots(); + + public abstract List getGroups(); public void addGroupTo(PwGroup newGroup, PwGroup parent) { // Add tree to parent tree @@ -272,26 +302,12 @@ public abstract class PwDatabase { groups.remove(remove.getId()); } - public void addEntryTo(PwEntry newEntry, PwGroup parent) { - // Add entry to parent - if (parent != null) { - parent.addChildEntry(newEntry); - } - newEntry.setParent(parent); - - entries.put(newEntry.getUUID(), newEntry); - } - - public void removeEntryFrom(PwEntry remove, PwGroup parent) { - // Remove entry for parent - if (parent != null) { - parent.removeChildEntry(remove); - } - entries.remove(remove.getUUID()); - } - public abstract PwGroupId newGroupId(); + public PwGroup getGroupByGroupId(PwGroupId id) { + return this.groups.get(id); + } + /** * Determine if an id number is already in use * @@ -314,6 +330,30 @@ public abstract class PwDatabase { public abstract PwGroup createGroup(); + public abstract List getEntries(); + + public PwEntry getEntryByUUIDId(UUID id) { + return this.entries.get(id); + } + + public void addEntryTo(PwEntry newEntry, PwGroup parent) { + // Add entry to parent + if (parent != null) { + parent.addChildEntry(newEntry); + } + newEntry.setParent(parent); + + entries.put(newEntry.getUUID(), newEntry); + } + + public void removeEntryFrom(PwEntry remove, PwGroup parent) { + // Remove entry for parent + if (parent != null) { + parent.removeChildEntry(remove); + } + entries.remove(remove.getUUID()); + } + public abstract boolean isBackup(PwGroup group); public void populateGlobals(PwGroup currentGroup) { @@ -345,7 +385,7 @@ public abstract class PwDatabase { * Determine if RecycleBin is enable or not * @return true if RecycleBin enable, false if is not available or not enable */ - public boolean isRecycleBinEnable() { + public boolean isRecycleBinEnabled() { return false; } diff --git a/app/src/main/java/com/keepassdroid/database/PwDatabaseV3.java b/app/src/main/java/com/keepassdroid/database/PwDatabaseV3.java index bfcff97db..628efa85a 100644 --- a/app/src/main/java/com/keepassdroid/database/PwDatabaseV3.java +++ b/app/src/main/java/com/keepassdroid/database/PwDatabaseV3.java @@ -60,44 +60,87 @@ import com.keepassdroid.database.exception.InvalidKeyFileException; * @author Dominik Reichl */ public class PwDatabaseV3 extends PwDatabase { - // Constants - // private static final int PWM_SESSION_KEY_SIZE = 12; - private final int DEFAULT_ENCRYPTION_ROUNDS = 300; - - // Special entry for settings - public PwEntry metaInfo; + private static final int DEFAULT_ENCRYPTION_ROUNDS = 300; // all entries - public List entries = new ArrayList<>(); + private List entries = new ArrayList<>(); // all groups - public List groups = new ArrayList<>(); + private List groups = new ArrayList<>(); // Algorithm used to encrypt the database - public PwEncryptionAlgorithm algorithm; - public int numKeyEncRounds; + private PwEncryptionAlgorithm algorithm; + private int numKeyEncRounds; + + private void initAndAddGroup(String name, int iconId, PwGroup parent) { + PwGroup group = createGroup(); + group.initNewGroup(name, newGroupId()); + group.setIcon(iconFactory.getIcon(iconId)); + addGroupTo(group, parent); + } + + @Override + public void initNew(String dbPath) { + algorithm = PwEncryptionAlgorithm.Rjindal; + numKeyEncRounds = DEFAULT_ENCRYPTION_ROUNDS; + name = "KeePass Password Manager"; // TODO as resource + // Build the root tree + constructTree(null); + + // Add a couple default groups + initAndAddGroup("Internet", 1, rootGroup); + initAndAddGroup("eMail", 19, rootGroup); + } @Override - public PwEncryptionAlgorithm getEncAlgorithm() { + public PwEncryptionAlgorithm getEncryptionAlgorithm() { return algorithm; } - public int getNumKeyEncRecords() { + public void setEncryptionAlgorithm(PwEncryptionAlgorithm algorithm) { + this.algorithm = algorithm; + } + + public int getNumKeyEncRounds() { return numKeyEncRounds; } + public void setNumKeyEncRounds(int numKeyEncRounds) { + this.numKeyEncRounds = numKeyEncRounds; + } + @Override public List getGroups() { return groups; } + public void setGroups(List grp) { + groups = grp; + } + + public void addGroup(PwGroup group) { + this.groups.add(group); + } + + public int numberOfGroups() { + return groups.size(); + } + @Override public List getEntries() { return entries; } - public void setGroups(List grp) { - groups = grp; - } + public PwEntry getEntryAt(int position) { + return entries.get(position); + } + + public void addEntry(PwEntry entry) { + this.entries.add(entry); + } + + public int numberOfEntries() { + return entries.size(); + } @Override public List getGrpRoots() { @@ -111,17 +154,6 @@ public class PwDatabaseV3 extends PwDatabase { return kids; } - public int getRootGroupId() { - for (int i = 0; i < groups.size(); i++) { - PwGroupV3 grp = (PwGroupV3) groups.get(i); - if (grp.getLevel() == 0) { - return grp.getGroupId(); - } - } - - return -1; - } - public List getGrpChildren(PwGroupV3 parent) { int idx = groups.indexOf(parent); int target = parent.getLevel() + 1; @@ -206,13 +238,10 @@ public class PwDatabaseV3 extends PwDatabase { */ @Override public PwGroupIdV3 newGroupId() { - PwGroupIdV3 newId = new PwGroupIdV3(0); - + PwGroupIdV3 newId; Random random = new Random(); - while (true) { newId = new PwGroupIdV3(random.nextInt()); - if (!isGroupIdUsed(newId)) break; } @@ -244,7 +273,6 @@ public class PwDatabaseV3 extends PwDatabase { } - @Override public long getNumRounds() { return numKeyEncRounds; @@ -255,7 +283,6 @@ public class PwDatabaseV3 extends PwDatabase { if (rounds > Integer.MAX_VALUE || rounds < Integer.MIN_VALUE) { throw new NumberFormatException(); } - numKeyEncRounds = (int) rounds; } @@ -270,7 +297,6 @@ public class PwDatabaseV3 extends PwDatabase { // Add entry to root entries entries.add(newEntry); - } @Override @@ -334,24 +360,4 @@ public class PwDatabaseV3 extends PwDatabase { return !(omitBackup && isBackup(group)); } - - private void initAndAddGroup(String name, int iconId, PwGroup parent) { - PwGroup group = createGroup(); - group.initNewGroup(name, newGroupId()); - group.setIcon(iconFactory.getIcon(iconId)); - addGroupTo(group, parent); - } - - @Override - public void initNew(String dbPath) { - algorithm = PwEncryptionAlgorithm.Rjindal; - numKeyEncRounds = DEFAULT_ENCRYPTION_ROUNDS; - name = "KeePass Password Manager"; - // Build the root tree - constructTree(null); - - // Add a couple default groups - initAndAddGroup("Internet", 1, rootGroup); - initAndAddGroup("eMail", 19, rootGroup); - } } diff --git a/app/src/main/java/com/keepassdroid/database/PwDatabaseV3Debug.java b/app/src/main/java/com/keepassdroid/database/PwDatabaseV3Debug.java index 7563b3821..6cd1c5c43 100644 --- a/app/src/main/java/com/keepassdroid/database/PwDatabaseV3Debug.java +++ b/app/src/main/java/com/keepassdroid/database/PwDatabaseV3Debug.java @@ -20,16 +20,26 @@ package com.keepassdroid.database; public class PwDatabaseV3Debug extends PwDatabaseV3 { - public byte[] postHeader; - public PwDbHeaderV3 dbHeader; + + private byte[] postHeader; + private PwDbHeaderV3 dbHeader; @Override public void copyEncrypted(byte[] buf, int offset, int size) { postHeader = new byte[size]; System.arraycopy(buf, offset, postHeader, 0, size); } + @Override public void copyHeader(PwDbHeaderV3 header) { dbHeader = header; } + + public byte[] getPostHeader() { + return postHeader; + } + + public PwDbHeaderV3 getDbHeader() { + return dbHeader; + } } diff --git a/app/src/main/java/com/keepassdroid/database/PwDatabaseV4.java b/app/src/main/java/com/keepassdroid/database/PwDatabaseV4.java index 254d5fde6..0c261a3fa 100644 --- a/app/src/main/java/com/keepassdroid/database/PwDatabaseV4.java +++ b/app/src/main/java/com/keepassdroid/database/PwDatabaseV4.java @@ -63,66 +63,248 @@ public class PwDatabaseV4 extends PwDatabase { private static final long DEFAULT_HISTORY_MAX_SIZE = 6 * 1024 * 1024; // -1 unlimited private static final String RECYCLEBIN_NAME = "RecycleBin"; - public byte[] hmacKey; - public UUID dataCipher = AesEngine.CIPHER_UUID; - public CipherEngine dataEngine = new AesEngine(); - public PwCompressionAlgorithm compressionAlgorithm = PwCompressionAlgorithm.Gzip; - // TODO: Refactor me away to get directly from kdfParameters - public long numKeyEncRounds = 6000; - public Date nameChanged = new Date(); - public Date settingsChanged = new Date(); - public String description = ""; - public Date descriptionChanged = new Date(); - public String defaultUserName = ""; - public Date defaultUserNameChanged = new Date(); - - public Date keyLastChanged = new Date(); - public long keyChangeRecDays = -1; - public long keyChangeForceDays = 1; - public boolean keyChangeForceOnce = false; - - public long maintenanceHistoryDays = 365; - public String color = ""; - public boolean recycleBinEnabled = true; - public UUID recycleBinUUID = UUID_ZERO; - public Date recycleBinChanged = new Date(); - public UUID entryTemplatesGroup = UUID_ZERO; - public Date entryTemplatesGroupChanged = new Date(); - public int historyMaxItems = DEFAULT_HISTORY_MAX_ITEMS; - public long historyMaxSize = DEFAULT_HISTORY_MAX_SIZE; - public UUID lastSelectedGroup = UUID_ZERO; - public UUID lastTopVisibleGroup = UUID_ZERO; - public MemoryProtectionConfig memoryProtection = new MemoryProtectionConfig(); - public List deletedObjects = new ArrayList<>(); - public List customIcons = new ArrayList<>(); - public Map customData = new HashMap<>(); - public KdfParameters kdfParameters = KdfFactory.getDefaultParameters(); - public VariantDictionary publicCustomData = new VariantDictionary(); - public BinaryPool binPool = new BinaryPool(); + private byte[] hmacKey; + private UUID dataCipher = AesEngine.CIPHER_UUID; + private CipherEngine dataEngine = new AesEngine(); + private PwCompressionAlgorithm compressionAlgorithm = PwCompressionAlgorithm.Gzip; - public String localizedAppName = "KeePassDroid"; + // TODO: Refactor me away to get directly from kdfParameters + private long numKeyEncRounds = 6000; + private PwDate nameChanged = new PwDate(); + private PwDate settingsChanged = new PwDate(); + private String description = ""; + private PwDate descriptionChanged = new PwDate(); + private String defaultUserName = ""; + private PwDate defaultUserNameChanged = new PwDate(); - public class MemoryProtectionConfig { - public boolean protectTitle = false; - public boolean protectUserName = false; - public boolean protectPassword = false; - public boolean protectUrl = false; - public boolean protectNotes = false; - - public boolean autoEnableVisualHiding = false; - - public boolean GetProtection(String field) { - if ( field.equalsIgnoreCase(PwDefsV4.TITLE_FIELD)) return protectTitle; - if ( field.equalsIgnoreCase(PwDefsV4.USERNAME_FIELD)) return protectUserName; - if ( field.equalsIgnoreCase(PwDefsV4.PASSWORD_FIELD)) return protectPassword; - if ( field.equalsIgnoreCase(PwDefsV4.URL_FIELD)) return protectUrl; - if ( field.equalsIgnoreCase(PwDefsV4.NOTES_FIELD)) return protectNotes; - - return false; - } + private PwDate keyLastChanged = new PwDate(); + private long keyChangeRecDays = -1; + private long keyChangeForceDays = 1; + private boolean keyChangeForceOnce = false; + + private long maintenanceHistoryDays = 365; + private String color = ""; + private boolean recycleBinEnabled = true; + private UUID recycleBinUUID = UUID_ZERO; + private Date recycleBinChanged = new Date(); + private UUID entryTemplatesGroup = UUID_ZERO; + private PwDate entryTemplatesGroupChanged = new PwDate(); + private int historyMaxItems = DEFAULT_HISTORY_MAX_ITEMS; + private long historyMaxSize = DEFAULT_HISTORY_MAX_SIZE; + private UUID lastSelectedGroup = UUID_ZERO; + private UUID lastTopVisibleGroup = UUID_ZERO; + private MemoryProtectionConfig memoryProtection = new MemoryProtectionConfig(); + private List deletedObjects = new ArrayList<>(); + private List customIcons = new ArrayList<>(); + private Map customData = new HashMap<>(); + + private KdfParameters kdfParameters = KdfFactory.getDefaultParameters(); + private VariantDictionary publicCustomData = new VariantDictionary(); + private BinaryPool binPool = new BinaryPool(); + + public String localizedAppName = "KeePassDX"; // TODO resource + + public byte[] getHmacKey() { + return hmacKey; } - - @Override + + public UUID getDataCipher() { + return dataCipher; + } + + public void setDataCipher(UUID dataCipher) { + this.dataCipher = dataCipher; + } + + public void setDataEngine(CipherEngine dataEngine) { + this.dataEngine = dataEngine; + } + + public PwCompressionAlgorithm getCompressionAlgorithm() { + return compressionAlgorithm; + } + + public void setCompressionAlgorithm(PwCompressionAlgorithm compressionAlgorithm) { + this.compressionAlgorithm = compressionAlgorithm; + } + + public long getNumKeyEncRounds() { + return numKeyEncRounds; + } + + public void setNumKeyEncRounds(long numKeyEncRounds) { + this.numKeyEncRounds = numKeyEncRounds; + } + + public PwDate getNameChanged() { + return nameChanged; + } + + public void setNameChanged(PwDate nameChanged) { + this.nameChanged = nameChanged; + } + + public PwDate getSettingsChanged() { + return settingsChanged; // TODO change setting date + } + + public void setSettingsChanged(PwDate settingsChanged) { + this.settingsChanged = settingsChanged; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public PwDate getDescriptionChanged() { + return descriptionChanged; + } + + public void setDescriptionChanged(PwDate descriptionChanged) { + this.descriptionChanged = descriptionChanged; + } + + public String getDefaultUserName() { + return defaultUserName; + } + + public void setDefaultUserName(String defaultUserName) { + this.defaultUserName = defaultUserName; + } + + public PwDate getDefaultUserNameChanged() { + return defaultUserNameChanged; + } + + public void setDefaultUserNameChanged(PwDate defaultUserNameChanged) { + this.defaultUserNameChanged = defaultUserNameChanged; + } + + public PwDate getKeyLastChanged() { + return keyLastChanged; + } + + public void setKeyLastChanged(PwDate keyLastChanged) { + this.keyLastChanged = keyLastChanged; + } + + public long getKeyChangeRecDays() { + return keyChangeRecDays; + } + + public void setKeyChangeRecDays(long keyChangeRecDays) { + this.keyChangeRecDays = keyChangeRecDays; + } + + public long getKeyChangeForceDays() { + return keyChangeForceDays; + } + + public void setKeyChangeForceDays(long keyChangeForceDays) { + this.keyChangeForceDays = keyChangeForceDays; + } + + public boolean isKeyChangeForceOnce() { + return keyChangeForceOnce; + } + + public void setKeyChangeForceOnce(boolean keyChangeForceOnce) { + this.keyChangeForceOnce = keyChangeForceOnce; + } + + public long getMaintenanceHistoryDays() { + return maintenanceHistoryDays; + } + + public void setMaintenanceHistoryDays(long maintenanceHistoryDays) { + this.maintenanceHistoryDays = maintenanceHistoryDays; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public UUID getEntryTemplatesGroup() { + return entryTemplatesGroup; + } + + public void setEntryTemplatesGroup(UUID entryTemplatesGroup) { + this.entryTemplatesGroup = entryTemplatesGroup; + } + + public PwDate getEntryTemplatesGroupChanged() { + return entryTemplatesGroupChanged; + } + + public void setEntryTemplatesGroupChanged(PwDate entryTemplatesGroupChanged) { + this.entryTemplatesGroupChanged = entryTemplatesGroupChanged; + } + + public int getHistoryMaxItems() { + return historyMaxItems; + } + + public void setHistoryMaxItems(int historyMaxItems) { + this.historyMaxItems = historyMaxItems; + } + + public long getHistoryMaxSize() { + return historyMaxSize; + } + + public void setHistoryMaxSize(long historyMaxSize) { + this.historyMaxSize = historyMaxSize; + } + + public UUID getLastSelectedGroup() { + return lastSelectedGroup; + } + + public void setLastSelectedGroup(UUID lastSelectedGroup) { + this.lastSelectedGroup = lastSelectedGroup; + } + + public UUID getLastTopVisibleGroup() { + return lastTopVisibleGroup; + } + + public void setLastTopVisibleGroup(UUID lastTopVisibleGroup) { + this.lastTopVisibleGroup = lastTopVisibleGroup; + } + + public MemoryProtectionConfig getMemoryProtection() { + return memoryProtection; + } + + public void setMemoryProtection(MemoryProtectionConfig memoryProtection) { + this.memoryProtection = memoryProtection; + } + + public List getCustomIcons() { + return customIcons; + } + + public void addCustomIcon(PwIconCustom customIcon) { + this.customIcons.add(customIcon); + } + + public Map getCustomData() { + return customData; + } + + public void putCustomData(String label, String value) { + this.customData.put(label, value); + } + + @Override public byte[] getMasterKey(String key, InputStream keyInputStream) throws InvalidKeyFileException, IOException { assert(key != null); @@ -265,7 +447,7 @@ public class PwDatabaseV4 extends PwDatabase { @Override public List getGroups() { - List list = new ArrayList(); + List list = new ArrayList<>(); PwGroupV4 root = (PwGroupV4) rootGroup; buildChildGroupsRecursive(root, list); @@ -287,7 +469,7 @@ public class PwDatabaseV4 extends PwDatabase { @Override public List getEntries() { - List list = new ArrayList(); + List list = new ArrayList<>(); PwGroupV4 root = (PwGroupV4) rootGroup; buildChildEntriesRecursive(root, list); return list; @@ -320,7 +502,7 @@ public class PwDatabaseV4 extends PwDatabase { } @Override - public PwEncryptionAlgorithm getEncAlgorithm() { + public PwEncryptionAlgorithm getEncryptionAlgorithm() { return PwEncryptionAlgorithm.Rjindal; } @@ -376,17 +558,37 @@ public class PwDatabaseV4 extends PwDatabase { } } + public UUID getRecycleBinUUID() { + return recycleBinUUID; + } + + public void setRecycleBinUUID(UUID recycleBinUUID) { + this.recycleBinUUID = recycleBinUUID; + } + @Override public boolean isRecycleBinAvailable() { return true; } @Override - public boolean isRecycleBinEnable() { + public boolean isRecycleBinEnabled() { return recycleBinEnabled; } - - @Override + + public void setRecycleBinEnabled(boolean recycleBinEnabled) { + this.recycleBinEnabled = recycleBinEnabled; + } + + public Date getRecycleBinChanged() { + return recycleBinChanged; + } + + public void setRecycleBinChanged(Date recycleBinChanged) { + this.recycleBinChanged = recycleBinChanged; + } + + @Override public boolean canRecycle(PwGroup group) { if (!recycleBinEnabled) { return false; @@ -452,7 +654,15 @@ public class PwDatabaseV4 extends PwDatabase { addEntryTo(entry, origParent); } - @Override + public List getDeletedObjects() { + return deletedObjects; + } + + public void addDeletedObject(PwDeletedObject deletedObject) { + this.deletedObjects.add(deletedObject); + } + + @Override public void deleteEntry(PwEntry entry) { super.deleteEntry(entry); deletedObjects.add(new PwDeletedObject(entry.getUUID())); @@ -466,7 +676,7 @@ public class PwDatabaseV4 extends PwDatabase { } @Override - public PwGroupV4 getRecycleBin() { + public PwGroupV4 getRecycleBin() { // TODO delete recycle bin preference if (recycleBinUUID == null) { return null; } @@ -475,7 +685,35 @@ public class PwDatabaseV4 extends PwDatabase { return (PwGroupV4) groups.get(recycleId); } - @Override + public KdfParameters getKdfParameters() { + return kdfParameters; + } + + public void setKdfParameters(KdfParameters kdfParameters) { + this.kdfParameters = kdfParameters; + } + + public VariantDictionary getPublicCustomData() { + return publicCustomData; + } + + public boolean containsPublicCustomData() { + return publicCustomData.size() > 0; + } + + public void setPublicCustomData(VariantDictionary publicCustomData) { + this.publicCustomData = publicCustomData; + } + + public BinaryPool getBinPool() { + return binPool; + } + + public void setBinPool(BinaryPool binPool) { + this.binPool = binPool; + } + + @Override public boolean isGroupSearchable(PwGroup group, boolean omitBackup) { if (!super.isGroupSearchable(group, omitBackup)) { return false; diff --git a/app/src/main/java/com/keepassdroid/database/PwDatabaseV4Debug.java b/app/src/main/java/com/keepassdroid/database/PwDatabaseV4Debug.java deleted file mode 100644 index ecf4b27a7..000000000 --- a/app/src/main/java/com/keepassdroid/database/PwDatabaseV4Debug.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. - * - * This file is part of KeePass DX. - * - * KeePass DX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * KeePass DX is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with KeePass DX. If not, see . - * - */ -package com.keepassdroid.database; - -public class PwDatabaseV4Debug extends PwDatabaseV4 { - public byte[] postHeader; - public PwDbHeaderV3 dbHeader; - -} diff --git a/app/src/main/java/com/keepassdroid/database/PwDatabaseV4XML.java b/app/src/main/java/com/keepassdroid/database/PwDatabaseV4XML.java index 5aadfb98e..92da28205 100644 --- a/app/src/main/java/com/keepassdroid/database/PwDatabaseV4XML.java +++ b/app/src/main/java/com/keepassdroid/database/PwDatabaseV4XML.java @@ -19,7 +19,6 @@ */ package com.keepassdroid.database; -import android.annotation.SuppressLint; import java.text.SimpleDateFormat; import java.util.TimeZone; diff --git a/app/src/main/java/com/keepassdroid/database/PwDbHeaderV4.java b/app/src/main/java/com/keepassdroid/database/PwDbHeaderV4.java index 0af4ac539..acc7a7675 100644 --- a/app/src/main/java/com/keepassdroid/database/PwDbHeaderV4.java +++ b/app/src/main/java/com/keepassdroid/database/PwDbHeaderV4.java @@ -180,22 +180,22 @@ public class PwDbHeaderV4 extends PwDbHeader { case PwDbHeaderV4Fields.TransformSeed: assert(version < PwDbHeaderV4.FILE_VERSION_32_4); AesKdf kdfS = new AesKdf(); - if (!db.kdfParameters.kdfUUID.equals(kdfS.uuid)) { - db.kdfParameters = kdfS.getDefaultParameters(); + if (!db.getKdfParameters().kdfUUID.equals(kdfS.uuid)) { + db.setKdfParameters(kdfS.getDefaultParameters()); } - db.kdfParameters.setByteArray(AesKdf.ParamSeed, fieldData); + db.getKdfParameters().setByteArray(AesKdf.ParamSeed, fieldData); break; case PwDbHeaderV4Fields.TransformRounds: assert(version < PwDbHeaderV4.FILE_VERSION_32_4); AesKdf kdfR = new AesKdf(); - if (!db.kdfParameters.kdfUUID.equals(kdfR.uuid)) { - db.kdfParameters = kdfR.getDefaultParameters(); + if (!db.getKdfParameters().kdfUUID.equals(kdfR.uuid)) { + db.setKdfParameters(kdfR.getDefaultParameters()); } long rounds = LEDataInputStream.readLong(fieldData, 0); - db.kdfParameters.setUInt64(AesKdf.ParamRounds, rounds); - db.numKeyEncRounds = rounds; + db.getKdfParameters().setUInt64(AesKdf.ParamRounds, rounds); + db.setNumKeyEncRounds(rounds); break; case PwDbHeaderV4Fields.EncryptionIV: @@ -217,11 +217,11 @@ public class PwDbHeaderV4 extends PwDbHeader { break; case PwDbHeaderV4Fields.KdfParameters: - db.kdfParameters = KdfParameters.deserialize(fieldData); + db.setKdfParameters(KdfParameters.deserialize(fieldData)); break; case PwDbHeaderV4Fields.PublicCustomData: - db.publicCustomData = KdfParameters.deserialize(fieldData); + db.setPublicCustomData(KdfParameters.deserialize(fieldData)); // TODO verify default: throw new IOException("Invalid header type: " + fieldID); @@ -235,7 +235,7 @@ public class PwDbHeaderV4 extends PwDbHeader { throw new IOException("Invalid cipher ID."); } - db.dataCipher = Types.bytestoUUID(pbId); + db.setDataCipher(Types.bytestoUUID(pbId)); } private void setCompressionFlags(byte[] pbFlags) throws IOException { @@ -248,7 +248,7 @@ public class PwDbHeaderV4 extends PwDbHeader { throw new IOException("Unrecognized compression flag."); } - db.compressionAlgorithm = PwCompressionAlgorithm.fromId(flag); + db.setCompressionAlgorithm(PwCompressionAlgorithm.fromId(flag)); } @@ -264,7 +264,7 @@ public class PwDbHeaderV4 extends PwDbHeader { throw new IOException("Rounds higher than " + Integer.MAX_VALUE + " are not currently supported."); } - db.numKeyEncRounds = rnd; + db.setNumKeyEncRounds(rnd); } @@ -319,6 +319,6 @@ public class PwDbHeaderV4 extends PwDbHeader { public byte[] getTransformSeed() { assert(version < FILE_VERSION_32_4); - return db.kdfParameters.getByteArray(AesKdf.ParamSeed); + return db.getKdfParameters().getByteArray(AesKdf.ParamSeed); } } diff --git a/app/src/main/java/com/keepassdroid/database/PwEntryV3.java b/app/src/main/java/com/keepassdroid/database/PwEntryV3.java index fb439eb87..82410816c 100644 --- a/app/src/main/java/com/keepassdroid/database/PwEntryV3.java +++ b/app/src/main/java/com/keepassdroid/database/PwEntryV3.java @@ -219,7 +219,7 @@ public class PwEntryV3 extends PwEntry { public void populateBlankFields(PwDatabaseV3 db) { // TODO verify and remove if (icon == null) { - icon = db.iconFactory.getIcon(1); + icon = db.getIconFactory().getFirstIcon(); } if (username == null) { diff --git a/app/src/main/java/com/keepassdroid/database/PwEntryV4.java b/app/src/main/java/com/keepassdroid/database/PwEntryV4.java index 213db4856..4d8796317 100644 --- a/app/src/main/java/com/keepassdroid/database/PwEntryV4.java +++ b/app/src/main/java/com/keepassdroid/database/PwEntryV4.java @@ -169,7 +169,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { @Override public void setTitle(String title) { PwDatabaseV4 db = (PwDatabaseV4) mDatabase; - boolean protect = db.memoryProtection.protectTitle; + boolean protect = db.getMemoryProtection().protectTitle; setString(STR_TITLE, title, protect); } @@ -177,7 +177,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { @Override public void setUsername(String user) { PwDatabaseV4 db = (PwDatabaseV4) mDatabase; - boolean protect = db.memoryProtection.protectUserName; + boolean protect = db.getMemoryProtection().protectUserName; setString(STR_USERNAME, user, protect); } @@ -185,7 +185,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { @Override public void setPassword(String pass) { PwDatabaseV4 db = (PwDatabaseV4) mDatabase; - boolean protect = db.memoryProtection.protectPassword; + boolean protect = db.getMemoryProtection().protectPassword; setString(STR_PASSWORD, pass, protect); } @@ -193,7 +193,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { @Override public void setUrl(String url) { PwDatabaseV4 db = (PwDatabaseV4) mDatabase; - boolean protect = db.memoryProtection.protectUrl; + boolean protect = db.getMemoryProtection().protectUrl; setString(STR_URL, url, protect); } @@ -201,7 +201,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { @Override public void setNotes(String notes) { PwDatabaseV4 db = (PwDatabaseV4) mDatabase; - boolean protect = db.memoryProtection.protectNotes; + boolean protect = db.getMemoryProtection().protectNotes; setString(STR_NOTES, notes, protect); } @@ -459,7 +459,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { private boolean maintainBackups(PwDatabaseV4 db) { boolean deleted = false; - int maxItems = db.historyMaxItems; + int maxItems = db.getHistoryMaxItems(); if (maxItems >= 0) { while (history.size() > maxItems) { removeOldestBackup(); @@ -467,7 +467,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { } } - long maxSize = db.historyMaxSize; + long maxSize = db.getHistoryMaxSize(); if (maxSize >= 0) { while(true) { long histSize = 0; diff --git a/app/src/main/java/com/keepassdroid/database/PwGroupV3.java b/app/src/main/java/com/keepassdroid/database/PwGroupV3.java index 3b5054ed8..44132d881 100644 --- a/app/src/main/java/com/keepassdroid/database/PwGroupV3.java +++ b/app/src/main/java/com/keepassdroid/database/PwGroupV3.java @@ -95,7 +95,7 @@ public class PwGroupV3 extends PwGroup { public void populateBlankFields(PwDatabaseV3 db) { // TODO populate blanck field if (icon == null) { - icon = db.iconFactory.getIcon(1); + icon = db.getIconFactory().getFirstIcon(); } if (name == null) { diff --git a/app/src/main/java/com/keepassdroid/database/PwIconFactory.java b/app/src/main/java/com/keepassdroid/database/PwIconFactory.java index 30e2c41bb..49594ab3d 100644 --- a/app/src/main/java/com/keepassdroid/database/PwIconFactory.java +++ b/app/src/main/java/com/keepassdroid/database/PwIconFactory.java @@ -36,7 +36,11 @@ public class PwIconFactory { * Keys: UUID, Values: PwIconCustom */ private ReferenceMap customCache = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); - + + public PwIconStandard getFirstIcon() { + return getIcon(0); + } + public PwIconStandard getIcon(int iconId) { PwIconStandard icon = (PwIconStandard) cache.get(iconId); diff --git a/app/src/main/java/com/keepassdroid/database/edit/AddGroup.java b/app/src/main/java/com/keepassdroid/database/edit/AddGroup.java index 00f3662b5..ae1ecccb8 100644 --- a/app/src/main/java/com/keepassdroid/database/edit/AddGroup.java +++ b/app/src/main/java/com/keepassdroid/database/edit/AddGroup.java @@ -56,7 +56,7 @@ public class AddGroup extends RunnableOnFinish { // Generate new group mGroup = pm.createGroup(); mGroup.initNewGroup(mName, pm.newGroupId()); - mGroup.setIcon(mDb.pm.iconFactory.getIcon(mIconID)); + mGroup.setIcon(mDb.pm.getIconFactory().getIcon(mIconID)); pm.addGroupTo(mGroup, mParent); // Commit to disk diff --git a/app/src/main/java/com/keepassdroid/database/edit/SetPassword.java b/app/src/main/java/com/keepassdroid/database/edit/SetPassword.java index b810a24e1..1539ce77f 100644 --- a/app/src/main/java/com/keepassdroid/database/edit/SetPassword.java +++ b/app/src/main/java/com/keepassdroid/database/edit/SetPassword.java @@ -69,8 +69,8 @@ public class SetPassword extends RunnableOnFinish { public void run() { PwDatabase pm = mDb.pm; - byte[] backupKey = new byte[pm.masterKey.length]; - System.arraycopy(pm.masterKey, 0, backupKey, 0, backupKey.length); + byte[] backupKey = new byte[pm.getMasterKey().length]; + System.arraycopy(pm.getMasterKey(), 0, backupKey, 0, backupKey.length); // Set key try { @@ -105,8 +105,8 @@ public class SetPassword extends RunnableOnFinish { public void run() { if ( ! mSuccess ) { // Erase the current master key - erase(mDb.pm.masterKey); - mDb.pm.masterKey = mBackup; + erase(mDb.pm.getMasterKey()); + mDb.pm.setMasterKey(mBackup); } super.run(); diff --git a/app/src/main/java/com/keepassdroid/database/load/ImporterV3.java b/app/src/main/java/com/keepassdroid/database/load/ImporterV3.java index 2ae70bed3..1c6f6f489 100644 --- a/app/src/main/java/com/keepassdroid/database/load/ImporterV3.java +++ b/app/src/main/java/com/keepassdroid/database/load/ImporterV3.java @@ -160,9 +160,9 @@ public class ImporterV3 extends Importer { // Select algorithm if( (hdr.flags & PwDbHeaderV3.FLAG_RIJNDAEL) != 0 ) { - newManager.algorithm = PwEncryptionAlgorithm.Rjindal; + newManager.setEncryptionAlgorithm(PwEncryptionAlgorithm.Rjindal); } else if( (hdr.flags & PwDbHeaderV3.FLAG_TWOFISH) != 0 ) { - newManager.algorithm = PwEncryptionAlgorithm.Twofish; + newManager.setEncryptionAlgorithm(PwEncryptionAlgorithm.Twofish); } else { throw new InvalidAlgorithmException(); } @@ -170,20 +170,20 @@ public class ImporterV3 extends Importer { // Copy for testing newManager.copyHeader(hdr); - newManager.numKeyEncRounds = hdr.numKeyEncRounds; + newManager.setNumKeyEncRounds(hdr.numKeyEncRounds); - newManager.name = "KeePass Password Manager"; + newManager.setName("KeePass Password Manager"); // TODO as resource; // Generate transformedMasterKey from masterKey - newManager.makeFinalKey(hdr.masterSeed, hdr.transformSeed, newManager.numKeyEncRounds); + newManager.makeFinalKey(hdr.masterSeed, hdr.transformSeed, newManager.getNumKeyEncRounds()); status.updateMessage(R.string.decrypting_db); // Initialize Rijndael algorithm Cipher cipher; try { - if ( newManager.algorithm == PwEncryptionAlgorithm.Rjindal ) { + if ( newManager.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding"); - } else if ( newManager.algorithm == PwEncryptionAlgorithm.Twofish ) { + } else if ( newManager.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Twofish ) { cipher = CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING"); } else { throw new IOException( "Encryption algorithm is not supported" ); @@ -196,7 +196,7 @@ public class ImporterV3 extends Importer { } try { - cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( newManager.finalKey, "AES" ), new IvParameterSpec( hdr.encryptionIV ) ); + cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( newManager.getFinalKey(), "AES" ), new IvParameterSpec( hdr.encryptionIV ) ); } catch (InvalidKeyException e1) { throw new IOException("Invalid key"); } catch (InvalidAlgorithmParameterException e1) { @@ -250,7 +250,7 @@ public class ImporterV3 extends Importer { // End-Group record. Save group and count it. newGrp.populateBlankFields(newManager); - newManager.groups.add(newGrp); + newManager.addGroup(newGrp); newGrp = new PwGroupV3(); i++; } @@ -269,7 +269,7 @@ public class ImporterV3 extends Importer { if( fieldType == 0xFFFF ) { // End-Group record. Save group and count it. newEnt.populateBlankFields(newManager); - newManager.entries.add(newEnt); + newManager.addEntry(newEnt); newEnt = new PwEntryV3(); i++; } @@ -358,7 +358,7 @@ public class ImporterV3 extends Importer { grp.setExpiryTime(new PwDate(buf, offset)); break; case 0x0007 : - grp.setIcon(db.iconFactory.getIcon(LEDataInputStream.readInt(buf, offset))); + grp.setIcon(db.getIconFactory().getIcon(LEDataInputStream.readInt(buf, offset))); break; case 0x0008 : grp.setLevel(LEDataInputStream.readUShort(buf, offset)); @@ -397,7 +397,7 @@ public class ImporterV3 extends Importer { iconId = 0; } - ent.setIcon(db.iconFactory.getIcon(iconId)); + ent.setIcon(db.getIconFactory().getIcon(iconId)); break; case 0x0004 : ent.setTitle(Types.readCString(buf, offset)); diff --git a/app/src/main/java/com/keepassdroid/database/load/ImporterV3Debug.java b/app/src/main/java/com/keepassdroid/database/load/ImporterV3Debug.java index 8daaf3f79..baf7e391a 100644 --- a/app/src/main/java/com/keepassdroid/database/load/ImporterV3Debug.java +++ b/app/src/main/java/com/keepassdroid/database/load/ImporterV3Debug.java @@ -41,5 +41,4 @@ public class ImporterV3Debug extends ImporterV3 { roundsFix); } - } diff --git a/app/src/main/java/com/keepassdroid/database/load/ImporterV4.java b/app/src/main/java/com/keepassdroid/database/load/ImporterV4.java index 45a743e91..98813e811 100644 --- a/app/src/main/java/com/keepassdroid/database/load/ImporterV4.java +++ b/app/src/main/java/com/keepassdroid/database/load/ImporterV4.java @@ -109,7 +109,7 @@ public class ImporterV4 extends Importer { db = createDB(); PwDbHeaderV4 header = new PwDbHeaderV4(db); - db.binPool.clear(); + db.getBinPool().clear(); PwDbHeaderV4.HeaderAndHash hh = header.loadFromFile(inStream); version = header.version; @@ -118,14 +118,14 @@ public class ImporterV4 extends Importer { pbHeader = hh.header; db.setMasterKey(password, keyInputStream); - db.makeFinalKey(header.masterSeed, db.kdfParameters, roundsFix); + db.makeFinalKey(header.masterSeed, db.getKdfParameters(), roundsFix); CipherEngine engine; Cipher cipher; try { - engine = CipherFactory.getInstance(db.dataCipher); - db.dataEngine = engine; - cipher = engine.getCipher(Cipher.DECRYPT_MODE, db.finalKey, header.encryptionIV); + engine = CipherFactory.getInstance(db.getDataCipher()); + db.setDataEngine(engine); + cipher = engine.getCipher(Cipher.DECRYPT_MODE, db.getFinalKey(), header.encryptionIV); } catch (NoSuchAlgorithmException e) { throw new IOException("Invalid algorithm."); } catch (NoSuchPaddingException e) { @@ -164,7 +164,7 @@ public class ImporterV4 extends Importer { throw new InvalidDBException(); } - byte[] hmacKey = db.hmacKey; + byte[] hmacKey = db.getHmacKey(); byte[] headerHmac = PwDbHeaderV4.computeHeaderHmac(pbHeader, hmacKey); byte[] storedHmac = isData.readBytes(32); if (storedHmac == null || storedHmac.length != 32) { @@ -181,7 +181,7 @@ public class ImporterV4 extends Importer { } InputStream isXml; - if ( db.compressionAlgorithm == PwCompressionAlgorithm.Gzip ) { + if ( db.getCompressionAlgorithm() == PwCompressionAlgorithm.Gzip ) { isXml = new GZIPInputStream(isPlain); } else { isXml = isPlain; @@ -253,7 +253,7 @@ public class ImporterV4 extends Importer { byte[] bin = new byte[data.length - 1]; System.arraycopy(data, 1, bin, 0, data.length-1); ProtectedBinary pb = new ProtectedBinary(prot, bin); - db.binPool.poolAdd(pb); + db.getBinPool().poolAdd(pb); if (prot) { Arrays.fill(data, (byte)0); @@ -408,56 +408,54 @@ public class ImporterV4 extends Importer { } } } else if (name.equalsIgnoreCase(ElemSettingsChanged)) { - db.settingsChanged = ReadTime(xpp); + db.setSettingsChanged(ReadPwTime(xpp)); } else if ( name.equalsIgnoreCase(ElemDbName) ) { - db.name = ReadString(xpp); + db.setName(ReadString(xpp)); } else if ( name.equalsIgnoreCase(ElemDbNameChanged) ) { - db.nameChanged = ReadTime(xpp); + db.setNameChanged(ReadPwTime(xpp)); } else if ( name.equalsIgnoreCase(ElemDbDesc) ) { - db.description = ReadString(xpp); + db.setDescription(ReadString(xpp)); } else if ( name.equalsIgnoreCase(ElemDbDescChanged) ) { - db.descriptionChanged = ReadTime(xpp); + db.setDescriptionChanged(ReadPwTime(xpp)); } else if ( name.equalsIgnoreCase(ElemDbDefaultUser) ) { - db.defaultUserName = ReadString(xpp); + db.setDefaultUserName(ReadString(xpp)); } else if ( name.equalsIgnoreCase(ElemDbDefaultUserChanged) ) { - db.defaultUserNameChanged = ReadTime(xpp); + db.setDefaultUserNameChanged(ReadPwTime(xpp)); } else if ( name.equalsIgnoreCase(ElemDbColor)) { // TODO: Add support to interpret the color if we want to allow changing the database color - db.color = ReadString(xpp); + db.setColor(ReadString(xpp)); } else if ( name.equalsIgnoreCase(ElemDbMntncHistoryDays) ) { - db.maintenanceHistoryDays = ReadUInt(xpp, DEFAULT_HISTORY_DAYS); + db.setMaintenanceHistoryDays(ReadUInt(xpp, DEFAULT_HISTORY_DAYS)); } else if ( name.equalsIgnoreCase(ElemDbKeyChanged) ) { - db.keyLastChanged = ReadTime(xpp); + db.setKeyLastChanged(ReadPwTime(xpp)); } else if ( name.equalsIgnoreCase(ElemDbKeyChangeRec) ) { - db.keyChangeRecDays = ReadLong(xpp, -1); + db.setKeyChangeRecDays(ReadLong(xpp, -1)); } else if ( name.equalsIgnoreCase(ElemDbKeyChangeForce) ) { - db.keyChangeForceDays = ReadLong(xpp, -1); + db.setKeyChangeForceDays(ReadLong(xpp, -1)); } else if ( name.equalsIgnoreCase(ElemDbKeyChangeForceOnce) ) { - db.keyChangeForceOnce = ReadBool(xpp, false); + db.setKeyChangeForceOnce(ReadBool(xpp, false)); } else if ( name.equalsIgnoreCase(ElemMemoryProt) ) { return SwitchContext(ctx, KdbContext.MemoryProtection, xpp); } else if ( name.equalsIgnoreCase(ElemCustomIcons) ) { return SwitchContext(ctx, KdbContext.CustomIcons, xpp); } else if ( name.equalsIgnoreCase(ElemRecycleBinEnabled) ) { - db.recycleBinEnabled = ReadBool(xpp, true); + db.setRecycleBinEnabled(ReadBool(xpp, true)); } else if ( name.equalsIgnoreCase(ElemRecycleBinUuid) ) { - db.recycleBinUUID = ReadUuid(xpp); + db.setRecycleBinUUID(ReadUuid(xpp)); } else if ( name.equalsIgnoreCase(ElemRecycleBinChanged) ) { - db.recycleBinChanged = ReadTime(xpp); + db.setRecycleBinChanged(ReadTime(xpp)); } else if ( name.equalsIgnoreCase(ElemEntryTemplatesGroup) ) { - db.entryTemplatesGroup = ReadUuid(xpp); + db.setEntryTemplatesGroup(ReadUuid(xpp)); } else if ( name.equalsIgnoreCase(ElemEntryTemplatesGroupChanged) ) { - db.entryTemplatesGroupChanged = ReadTime(xpp); + db.setEntryTemplatesGroupChanged(ReadPwTime(xpp)); } else if ( name.equalsIgnoreCase(ElemHistoryMaxItems) ) { - db.historyMaxItems = ReadInt(xpp, -1); + db.setHistoryMaxItems(ReadInt(xpp, -1)); } else if ( name.equalsIgnoreCase(ElemHistoryMaxSize) ) { - db.historyMaxSize = ReadLong(xpp, -1); - } else if ( name.equalsIgnoreCase(ElemEntryTemplatesGroupChanged) ) { - db.entryTemplatesGroupChanged = ReadTime(xpp); + db.setHistoryMaxSize(ReadLong(xpp, -1)); } else if ( name.equalsIgnoreCase(ElemLastSelectedGroup) ) { - db.lastSelectedGroup = ReadUuid(xpp); + db.setLastSelectedGroup(ReadUuid(xpp)); } else if ( name.equalsIgnoreCase(ElemLastTopVisibleGroup) ) { - db.lastTopVisibleGroup = ReadUuid(xpp); + db.setLastTopVisibleGroup(ReadUuid(xpp)); } else if ( name.equalsIgnoreCase(ElemBinaries) ) { return SwitchContext(ctx, KdbContext.Binaries, xpp); } else if ( name.equalsIgnoreCase(ElemCustomData) ) { @@ -467,17 +465,17 @@ public class ImporterV4 extends Importer { case MemoryProtection: if ( name.equalsIgnoreCase(ElemProtTitle) ) { - db.memoryProtection.protectTitle = ReadBool(xpp, false); + db.getMemoryProtection().protectTitle = ReadBool(xpp, false); } else if ( name.equalsIgnoreCase(ElemProtUserName) ) { - db.memoryProtection.protectUserName = ReadBool(xpp, false); + db.getMemoryProtection().protectUserName = ReadBool(xpp, false); } else if ( name.equalsIgnoreCase(ElemProtPassword) ) { - db.memoryProtection.protectPassword = ReadBool(xpp, false); + db.getMemoryProtection().protectPassword = ReadBool(xpp, false); } else if ( name.equalsIgnoreCase(ElemProtURL) ) { - db.memoryProtection.protectUrl = ReadBool(xpp, false); + db.getMemoryProtection().protectUrl = ReadBool(xpp, false); } else if ( name.equalsIgnoreCase(ElemProtNotes) ) { - db.memoryProtection.protectNotes = ReadBool(xpp, false); + db.getMemoryProtection().protectNotes = ReadBool(xpp, false); } else if ( name.equalsIgnoreCase(ElemProtAutoHide) ) { - db.memoryProtection.autoEnableVisualHiding = ReadBool(xpp, false); + db.getMemoryProtection().autoEnableVisualHiding = ReadBool(xpp, false); } else { ReadUnknown(xpp); } @@ -512,7 +510,7 @@ public class ImporterV4 extends Importer { if ( key != null ) { ProtectedBinary pbData = ReadProtectedBinary(xpp); int id = Integer.parseInt(key); - db.binPool.put(id, pbData); + db.getBinPool().put(id, pbData); } else { ReadUnknown(xpp); } @@ -545,8 +543,8 @@ public class ImporterV4 extends Importer { assert(ctxGroups.size() == 0); if ( ctxGroups.size() != 0 ) throw new IOException("Group list should be empty."); - db.rootGroup = new PwGroupV4(); - ctxGroups.push((PwGroupV4)db.rootGroup); + db.setRootGroup(new PwGroupV4()); + ctxGroups.push((PwGroupV4)db.getRootGroup()); ctxGroup = ctxGroups.peek(); return SwitchContext(ctx, KdbContext.Group, xpp); @@ -565,9 +563,9 @@ public class ImporterV4 extends Importer { } else if ( name.equalsIgnoreCase(ElemNotes) ) { ctxGroup.setNotes(ReadString(xpp)); } else if ( name.equalsIgnoreCase(ElemIcon) ) { - ctxGroup.setIcon(db.iconFactory.getIcon((int)ReadUInt(xpp, 0))); + ctxGroup.setIcon(db.getIconFactory().getIcon((int)ReadUInt(xpp, 0))); } else if ( name.equalsIgnoreCase(ElemCustomIconID) ) { - ctxGroup.setCustomIcon(db.iconFactory.getIcon(ReadUuid(xpp))); + ctxGroup.setCustomIcon(db.getIconFactory().getIcon(ReadUuid(xpp))); } else if ( name.equalsIgnoreCase(ElemTimes) ) { return SwitchContext(ctx, KdbContext.GroupTimes, xpp); } else if ( name.equalsIgnoreCase(ElemIsExpanded) ) { @@ -620,9 +618,9 @@ public class ImporterV4 extends Importer { if ( name.equalsIgnoreCase(ElemUuid) ) { ctxEntry.setUUID(ReadUuid(xpp)); } else if ( name.equalsIgnoreCase(ElemIcon) ) { - ctxEntry.setIcon(db.iconFactory.getIcon((int)ReadUInt(xpp, 0))); + ctxEntry.setIcon(db.getIconFactory().getIcon((int)ReadUInt(xpp, 0))); } else if ( name.equalsIgnoreCase(ElemCustomIconID) ) { - ctxEntry.setCustomIcon(db.iconFactory.getIcon(ReadUuid(xpp))); + ctxEntry.setCustomIcon(db.getIconFactory().getIcon(ReadUuid(xpp))); } else if ( name.equalsIgnoreCase(ElemFgColor) ) { ctxEntry.setForegroundColor(ReadString(xpp)); } else if ( name.equalsIgnoreCase(ElemBgColor) ) { @@ -756,7 +754,7 @@ public class ImporterV4 extends Importer { case RootDeletedObjects: if ( name.equalsIgnoreCase(ElemDeletedObject) ) { ctxDeletedObject = new PwDeletedObject(); - db.deletedObjects.add(ctxDeletedObject); + db.addDeletedObject(ctxDeletedObject); return SwitchContext(ctx, KdbContext.DeletedObject, xpp); } else { @@ -799,8 +797,8 @@ public class ImporterV4 extends Importer { } else if ( ctx == KdbContext.CustomIcon && name.equalsIgnoreCase(ElemCustomIconItem) ) { if ( ! customIconID.equals(PwDatabase.UUID_ZERO) ) { PwIconCustom icon = new PwIconCustom(customIconID, customIconData); - db.customIcons.add(icon); - db.iconFactory.put(icon); + db.addCustomIcon(icon); + db.getIconFactory().put(icon); } else assert(false); customIconID = PwDatabase.UUID_ZERO; @@ -813,7 +811,7 @@ public class ImporterV4 extends Importer { return KdbContext.Meta; } else if ( ctx == KdbContext.CustomDataItem && name.equalsIgnoreCase(ElemStringDictExItem) ) { if ( customDataKey != null && customDataValue != null) { - db.customData.put(customDataKey, customDataValue); + db.putCustomData(customDataKey, customDataValue); } else assert(false); customDataKey = null; @@ -1066,7 +1064,7 @@ public class ImporterV4 extends Importer { xpp.next(); // Consume end tag int id = Integer.parseInt(ref); - return db.binPool.get(id); + return db.getBinPool().get(id); } boolean compressed = false; diff --git a/app/src/main/java/com/keepassdroid/database/load/ImporterV4Debug.java b/app/src/main/java/com/keepassdroid/database/load/ImporterV4Debug.java deleted file mode 100644 index 12d8d4c57..000000000 --- a/app/src/main/java/com/keepassdroid/database/load/ImporterV4Debug.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. - * - * This file is part of KeePass DX. - * - * KeePass DX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * KeePass DX is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with KeePass DX. If not, see . - * - */ -package com.keepassdroid.database.load; - -import com.keepassdroid.tasks.UpdateStatus; -import com.keepassdroid.database.PwDatabaseV4Debug; -import com.keepassdroid.database.exception.InvalidDBException; - -import java.io.IOException; -import java.io.InputStream; - -public class ImporterV4Debug extends ImporterV4 { - - @Override - protected PwDatabaseV4Debug createDB() { - return new PwDatabaseV4Debug(); - } - - @Override - public PwDatabaseV4Debug openDatabase(InputStream inStream, String password, - InputStream keyInputFile, UpdateStatus status, long roundsFix) throws IOException, - InvalidDBException { - return (PwDatabaseV4Debug) super.openDatabase(inStream, password, keyInputFile, status, - roundsFix); - } - -} diff --git a/app/src/main/java/com/keepassdroid/database/save/PwDbHeaderOutputV4.java b/app/src/main/java/com/keepassdroid/database/save/PwDbHeaderOutputV4.java index 1c5c82331..9239dbea9 100644 --- a/app/src/main/java/com/keepassdroid/database/save/PwDbHeaderOutputV4.java +++ b/app/src/main/java/com/keepassdroid/database/save/PwDbHeaderOutputV4.java @@ -64,7 +64,7 @@ public class PwDbHeaderOutputV4 extends PwDbHeaderOutput { } try { - d.makeFinalKey(header.masterSeed, d.kdfParameters); + d.makeFinalKey(header.masterSeed, d.getKdfParameters()); } catch (IOException e) { throw new PwDbOutputException(e); } @@ -72,7 +72,7 @@ public class PwDbHeaderOutputV4 extends PwDbHeaderOutput { Mac hmac; try { hmac = Mac.getInstance("HmacSHA256"); - SecretKeySpec signingKey = new SecretKeySpec(HmacBlockStream.GetHmacKey64(db.hmacKey, Types.ULONG_MAX_VALUE), "HmacSHA256"); + SecretKeySpec signingKey = new SecretKeySpec(HmacBlockStream.GetHmacKey64(db.getHmacKey(), Types.ULONG_MAX_VALUE), "HmacSHA256"); hmac.init(signingKey); } catch (NoSuchAlgorithmException e) { throw new PwDbOutputException(e); @@ -92,15 +92,15 @@ public class PwDbHeaderOutputV4 extends PwDbHeaderOutput { los.writeUInt(header.version); - writeHeaderField(PwDbHeaderV4Fields.CipherID, Types.UUIDtoBytes(db.dataCipher)); - writeHeaderField(PwDbHeaderV4Fields.CompressionFlags, LEDataOutputStream.writeIntBuf(db.compressionAlgorithm.id)); + writeHeaderField(PwDbHeaderV4Fields.CipherID, Types.UUIDtoBytes(db.getDataCipher())); + writeHeaderField(PwDbHeaderV4Fields.CompressionFlags, LEDataOutputStream.writeIntBuf(db.getCompressionAlgorithm().id)); writeHeaderField(PwDbHeaderV4Fields.MasterSeed, header.masterSeed); if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) { writeHeaderField(PwDbHeaderV4Fields.TransformSeed, header.getTransformSeed()); - writeHeaderField(PwDbHeaderV4Fields.TransformRounds, LEDataOutputStream.writeLongBuf(db.numKeyEncRounds)); + writeHeaderField(PwDbHeaderV4Fields.TransformRounds, LEDataOutputStream.writeLongBuf(db.getNumKeyEncRounds())); } else { - writeHeaderField(PwDbHeaderV4Fields.KdfParameters, KdfParameters.serialize(db.kdfParameters)); + writeHeaderField(PwDbHeaderV4Fields.KdfParameters, KdfParameters.serialize(db.getKdfParameters())); } if (header.encryptionIV.length > 0) { @@ -113,10 +113,10 @@ public class PwDbHeaderOutputV4 extends PwDbHeaderOutput { writeHeaderField(PwDbHeaderV4Fields.InnerRandomStreamID, LEDataOutputStream.writeIntBuf(header.innerRandomStream.id)); } - if (db.publicCustomData.size() > 0) { + if (db.containsPublicCustomData()) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); LEDataOutputStream los = new LEDataOutputStream(bos); - VariantDictionary.serialize(db.publicCustomData, los); + VariantDictionary.serialize(db.getPublicCustomData(), los); writeHeaderField(PwDbHeaderV4Fields.PublicCustomData, bos.toByteArray()); } diff --git a/app/src/main/java/com/keepassdroid/database/save/PwDbInnerHeaderOutputV4.java b/app/src/main/java/com/keepassdroid/database/save/PwDbInnerHeaderOutputV4.java index a5bc78d83..cfd14a8cf 100644 --- a/app/src/main/java/com/keepassdroid/database/save/PwDbInnerHeaderOutputV4.java +++ b/app/src/main/java/com/keepassdroid/database/save/PwDbInnerHeaderOutputV4.java @@ -52,7 +52,7 @@ public class PwDbInnerHeaderOutputV4 { los.writeInt(streamKeySize); los.write(header.innerRandomStreamKey); - for (ProtectedBinary bin : db.binPool.binaries()) { + for (ProtectedBinary bin : db.getBinPool().binaries()) { byte flag = KdbxBinaryFlags.None; if (bin.isProtected()) { flag |= KdbxBinaryFlags.Protected; diff --git a/app/src/main/java/com/keepassdroid/database/save/PwDbV3Output.java b/app/src/main/java/com/keepassdroid/database/save/PwDbV3Output.java index afae09f54..e864de95b 100644 --- a/app/src/main/java/com/keepassdroid/database/save/PwDbV3Output.java +++ b/app/src/main/java/com/keepassdroid/database/save/PwDbV3Output.java @@ -55,16 +55,14 @@ public class PwDbV3Output extends PwDbOutput { public PwDbV3Output(PwDatabaseV3 pm, OutputStream os) { super(os); - mPM = pm; - } public byte[] getFinalKey(PwDbHeader header) throws PwDbOutputException { try { PwDbHeaderV3 h3 = (PwDbHeaderV3) header; - mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.numKeyEncRounds); - return mPM.finalKey; + mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.getNumKeyEncRounds()); + return mPM.getFinalKey(); } catch (IOException e) { throw new PwDbOutputException("Key creation failed: " + e.getMessage()); } @@ -80,9 +78,9 @@ public class PwDbV3Output extends PwDbOutput { Cipher cipher; try { - if (mPM.algorithm == PwEncryptionAlgorithm.Rjindal) { + if (mPM.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Rjindal) { cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding"); - } else if (mPM.algorithm == PwEncryptionAlgorithm.Twofish){ + } else if (mPM.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Twofish){ cipher = CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING"); } else { throw new Exception(); @@ -130,18 +128,18 @@ public class PwDbV3Output extends PwDbOutput { header.signature2 = PwDbHeaderV3.DBSIG_2; header.flags = PwDbHeaderV3.FLAG_SHA2; - if ( mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { + if ( mPM.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL; - } else if ( mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish ) { + } else if ( mPM.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Twofish ) { header.flags |= PwDbHeaderV3.FLAG_TWOFISH; } else { throw new PwDbOutputException("Unsupported algorithm."); } header.version = PwDbHeaderV3.DBVER_DW; - header.numGroups = mPM.getGroups().size(); - header.numEntries = mPM.entries.size(); - header.numKeyEncRounds = mPM.getNumKeyEncRecords(); + header.numGroups = mPM.numberOfGroups(); + header.numEntries = mPM.numberOfEntries(); + header.numKeyEncRounds = mPM.getNumKeyEncRounds(); setIVs(header); @@ -232,8 +230,8 @@ public class PwDbV3Output extends PwDbOutput { } // Entries - for (int i = 0; i < mPM.entries.size(); i++ ) { - PwEntryV3 pe = (PwEntryV3) mPM.entries.get(i); + for (int i = 0; i < mPM.numberOfEntries(); i++ ) { + PwEntryV3 pe = (PwEntryV3) mPM.getEntryAt(i); PwEntryOutputV3 peo = new PwEntryOutputV3(pe, os); try { peo.output(); diff --git a/app/src/main/java/com/keepassdroid/database/save/PwDbV3OutputDebug.java b/app/src/main/java/com/keepassdroid/database/save/PwDbV3OutputDebug.java index edbbdd43a..cd93c40bf 100644 --- a/app/src/main/java/com/keepassdroid/database/save/PwDbV3OutputDebug.java +++ b/app/src/main/java/com/keepassdroid/database/save/PwDbV3OutputDebug.java @@ -48,7 +48,7 @@ public class PwDbV3OutputDebug extends PwDbV3Output { // Reuse random values to test equivalence in debug mode - PwDbHeaderV3 origHeader = debugDb.dbHeader; + PwDbHeaderV3 origHeader = debugDb.getDbHeader(); System.arraycopy(origHeader.encryptionIV, 0, header.encryptionIV, 0, origHeader.encryptionIV.length); System.arraycopy(origHeader.masterSeed, 0, header.masterSeed, 0, origHeader.masterSeed.length); System.arraycopy(origHeader.transformSeed, 0, header.transformSeed, 0, origHeader.transformSeed.length); diff --git a/app/src/main/java/com/keepassdroid/database/save/PwDbV4Output.java b/app/src/main/java/com/keepassdroid/database/save/PwDbV4Output.java index a8cb64233..6199a6843 100644 --- a/app/src/main/java/com/keepassdroid/database/save/PwDbV4Output.java +++ b/app/src/main/java/com/keepassdroid/database/save/PwDbV4Output.java @@ -32,7 +32,7 @@ import com.keepassdroid.database.GroupHandler; import com.keepassdroid.database.ITimeLogger; import com.keepassdroid.database.PwCompressionAlgorithm; import com.keepassdroid.database.PwDatabaseV4; -import com.keepassdroid.database.PwDatabaseV4.MemoryProtectionConfig; +import com.keepassdroid.database.MemoryProtectionConfig; import com.keepassdroid.database.PwDatabaseV4XML; import com.keepassdroid.database.PwDbHeader; import com.keepassdroid.database.PwDbHeaderV4; @@ -181,7 +181,7 @@ public class PwDbV4Output extends PwDbOutput { try { try { - engine = CipherFactory.getInstance(mPM.dataCipher); + engine = CipherFactory.getInstance(mPM.getDataCipher()); } catch (NoSuchAlgorithmException e) { throw new PwDbOutputException("No such cipher", e); } @@ -199,15 +199,13 @@ public class PwDbV4Output extends PwDbOutput { mOS.write(hashOfHeader); mOS.write(headerHmac); - HmacBlockOutputStream hbos = new HmacBlockOutputStream(mOS, mPM.hmacKey); + HmacBlockOutputStream hbos = new HmacBlockOutputStream(mOS, mPM.getHmacKey()); osPlain = attachStreamEncryptor(header, hbos); } OutputStream osXml; try { - - - if (mPM.compressionAlgorithm == PwCompressionAlgorithm.Gzip) { + if (mPM.getCompressionAlgorithm() == PwCompressionAlgorithm.Gzip) { osXml = new GZIPOutputStream(osPlain); } else { osXml = osPlain; @@ -218,7 +216,6 @@ public class PwDbV4Output extends PwDbOutput { ihOut.output(); } - outputDatabase(osXml); osXml.close(); } catch (IllegalArgumentException e) { @@ -292,7 +289,7 @@ public class PwDbV4Output extends PwDbOutput { writeMeta(); - PwGroupV4 root = (PwGroupV4) mPM.rootGroup; + PwGroupV4 root = (PwGroupV4) mPM.getRootGroup(); xml.startTag(null, ElemRoot); startGroup(root); Stack groupStack = new Stack(); @@ -307,7 +304,7 @@ public class PwDbV4Output extends PwDbOutput { endGroup(); - writeList(ElemDeletedObjects, mPM.deletedObjects); + writeList(ElemDeletedObjects, mPM.getDeletedObjects()); xml.endTag(null, ElemRoot); @@ -325,37 +322,36 @@ public class PwDbV4Output extends PwDbOutput { writeObject(ElemHeaderHash, String.valueOf(Base64Coder.encode(hashOfHeader))); } - writeObject(ElemDbName, mPM.name, true); - writeObject(ElemDbNameChanged, mPM.nameChanged); - writeObject(ElemDbDesc, mPM.description, true); - writeObject(ElemDbDescChanged, mPM.descriptionChanged); - writeObject(ElemDbDefaultUser, mPM.defaultUserName, true); - writeObject(ElemDbDefaultUserChanged, mPM.defaultUserNameChanged); - writeObject(ElemDbMntncHistoryDays, mPM.maintenanceHistoryDays); - writeObject(ElemDbColor, mPM.color); - writeObject(ElemDbKeyChanged, mPM.keyLastChanged); - writeObject(ElemDbKeyChangeRec, mPM.keyChangeRecDays); - writeObject(ElemDbKeyChangeForce, mPM.keyChangeForceDays); + writeObject(ElemDbName, mPM.getName(), true); + writeObject(ElemDbNameChanged, mPM.getNameChanged().getDate()); + writeObject(ElemDbDesc, mPM.getDescription(), true); + writeObject(ElemDbDescChanged, mPM.getDescriptionChanged().getDate()); + writeObject(ElemDbDefaultUser, mPM.getDefaultUserName(), true); + writeObject(ElemDbDefaultUserChanged, mPM.getDefaultUserNameChanged().getDate()); + writeObject(ElemDbMntncHistoryDays, mPM.getMaintenanceHistoryDays()); + writeObject(ElemDbColor, mPM.getColor()); + writeObject(ElemDbKeyChanged, mPM.getKeyLastChanged().getDate()); + writeObject(ElemDbKeyChangeRec, mPM.getKeyChangeRecDays()); + writeObject(ElemDbKeyChangeForce, mPM.getKeyChangeForceDays()); - - writeList(ElemMemoryProt, mPM.memoryProtection); + writeList(ElemMemoryProt, mPM.getMemoryProtection()); writeCustomIconList(); - writeObject(ElemRecycleBinEnabled, mPM.recycleBinEnabled); - writeObject(ElemRecycleBinUuid, mPM.recycleBinUUID); - writeObject(ElemRecycleBinChanged, mPM.recycleBinChanged); - writeObject(ElemEntryTemplatesGroup, mPM.entryTemplatesGroup); - writeObject(ElemEntryTemplatesGroupChanged, mPM.entryTemplatesGroupChanged); - writeObject(ElemHistoryMaxItems, mPM.historyMaxItems); - writeObject(ElemHistoryMaxSize, mPM.historyMaxSize); - writeObject(ElemLastSelectedGroup, mPM.lastSelectedGroup); - writeObject(ElemLastTopVisibleGroup, mPM.lastTopVisibleGroup); + writeObject(ElemRecycleBinEnabled, mPM.isRecycleBinEnabled()); + writeObject(ElemRecycleBinUuid, mPM.getRecycleBinUUID()); + writeObject(ElemRecycleBinChanged, mPM.getRecycleBinChanged()); + writeObject(ElemEntryTemplatesGroup, mPM.getEntryTemplatesGroup()); + writeObject(ElemEntryTemplatesGroupChanged, mPM.getEntryTemplatesGroupChanged().getDate()); + writeObject(ElemHistoryMaxItems, mPM.getHistoryMaxItems()); + writeObject(ElemHistoryMaxSize, mPM.getHistoryMaxSize()); + writeObject(ElemLastSelectedGroup, mPM.getLastSelectedGroup()); + writeObject(ElemLastTopVisibleGroup, mPM.getLastTopVisibleGroup()); if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) { writeBinPool(); } - writeList(ElemCustomData, mPM.customData); + writeList(ElemCustomData, mPM.getCustomData()); xml.endTag(null, ElemMeta); @@ -366,7 +362,7 @@ public class PwDbV4Output extends PwDbOutput { try { //mPM.makeFinalKey(header.masterSeed, mPM.kdfParameters); - cipher = engine.getCipher(Cipher.ENCRYPT_MODE, mPM.finalKey, header.encryptionIV); + cipher = engine.getCipher(Cipher.ENCRYPT_MODE, mPM.getFinalKey(), header.encryptionIV); } catch (Exception e) { throw new PwDbOutputException("Invalid algorithm.", e); } @@ -389,9 +385,9 @@ public class PwDbV4Output extends PwDbOutput { } random.nextBytes(h.encryptionIV); - UUID kdfUUID = mPM.kdfParameters.kdfUUID; + UUID kdfUUID = mPM.getKdfParameters().kdfUUID; KdfEngine kdf = KdfFactory.get(kdfUUID); - kdf.randomize(mPM.kdfParameters); + kdf.randomize(mPM.getKdfParameters()); if (h.version < PwDbHeaderV4.FILE_VERSION_32_4) { h.innerRandomStream = CrsAlgorithm.Salsa20; @@ -500,7 +496,7 @@ public class PwDbV4Output extends PwDbOutput { xml.startTag(null, ElemValue); String strRef = null; if (allowRef) { - int ref = mPM.binPool.poolFind(value); + int ref = mPM.getBinPool().poolFind(value); strRef = Integer.toString(ref); } @@ -528,7 +524,7 @@ public class PwDbV4Output extends PwDbOutput { } } else { - if (mPM.compressionAlgorithm == PwCompressionAlgorithm.Gzip) { + if (mPM.getCompressionAlgorithm() == PwCompressionAlgorithm.Gzip) { xml.attribute(null, AttrCompressed, ValTrue); byte[] raw = value.getData(); byte[] compressed = MemUtil.compress(raw); @@ -651,19 +647,19 @@ public class PwDbV4Output extends PwDbOutput { boolean protect = value.isProtected(); if (isEntryString) { if (key.equals(PwDefsV4.TITLE_FIELD)) { - protect = mPM.memoryProtection.protectTitle; + protect = mPM.getMemoryProtection().protectTitle; } else if (key.equals(PwDefsV4.USERNAME_FIELD)) { - protect = mPM.memoryProtection.protectUserName; + protect = mPM.getMemoryProtection().protectUserName; } else if (key.equals(PwDefsV4.PASSWORD_FIELD)) { - protect = mPM.memoryProtection.protectPassword; + protect = mPM.getMemoryProtection().protectPassword; } else if (key.equals(PwDefsV4.URL_FIELD)) { - protect = mPM.memoryProtection.protectUrl; + protect = mPM.getMemoryProtection().protectUrl; } else if (key.equals(PwDefsV4.NOTES_FIELD)) { - protect = mPM.memoryProtection.protectNotes; + protect = mPM.getMemoryProtection().protectNotes; } } @@ -780,7 +776,7 @@ public class PwDbV4Output extends PwDbOutput { } private void writeCustomIconList() throws IllegalArgumentException, IllegalStateException, IOException { - List customIcons = mPM.customIcons; + List customIcons = mPM.getCustomIcons(); if (customIcons.size() == 0) return; xml.startTag(null, ElemCustomIcons); @@ -800,7 +796,7 @@ public class PwDbV4Output extends PwDbOutput { private void writeBinPool() throws IllegalArgumentException, IllegalStateException, IOException { xml.startTag(null, ElemBinaries); - for (Entry pair : mPM.binPool.entrySet()) { + for (Entry pair : mPM.getBinPool().entrySet()) { xml.startTag(null, ElemBinary); xml.attribute(null, AttrId, Integer.toString(pair.getKey())); diff --git a/app/src/main/java/com/keepassdroid/search/SearchDbHelper.java b/app/src/main/java/com/keepassdroid/search/SearchDbHelper.java index 179b0614a..2eb6e0b83 100644 --- a/app/src/main/java/com/keepassdroid/search/SearchDbHelper.java +++ b/app/src/main/java/com/keepassdroid/search/SearchDbHelper.java @@ -74,9 +74,9 @@ public class SearchDbHelper { qStr = qStr.toLowerCase(loc); boolean isOmitBackup = omitBackup(); - Queue worklist = new LinkedList(); - if (pm.rootGroup != null) { - worklist.add(pm.rootGroup); + Queue worklist = new LinkedList<>(); + if (pm.getRootGroup() != null) { + worklist.add(pm.getRootGroup()); } while (worklist.size() != 0) { diff --git a/app/src/main/java/com/keepassdroid/settings/NestedSettingsFragment.java b/app/src/main/java/com/keepassdroid/settings/NestedSettingsFragment.java index affc89de5..eefae05d4 100644 --- a/app/src/main/java/com/keepassdroid/settings/NestedSettingsFragment.java +++ b/app/src/main/java/com/keepassdroid/settings/NestedSettingsFragment.java @@ -284,7 +284,7 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat SwitchPreference recycleBinPref = (SwitchPreference) findPreference(getString(R.string.recycle_bin_key)); // TODO Recycle //recycleBinPref.setEnabled(true); - recycleBinPref.setChecked(db.pm.isRecycleBinEnable()); + recycleBinPref.setChecked(db.pm.isRecycleBinEnabled()); } } else { @@ -318,7 +318,7 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat private void setAlgorithm(Database db, Preference algorithm) { int resId; - if ( db.pm.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { + if ( db.pm.getEncryptionAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { resId = R.string.rijndael; } else { resId = R.string.twofish; diff --git a/app/src/main/java/com/keepassdroid/utils/SprEngineV4.java b/app/src/main/java/com/keepassdroid/utils/SprEngineV4.java index 5f97d931f..dfe64c0be 100644 --- a/app/src/main/java/com/keepassdroid/utils/SprEngineV4.java +++ b/app/src/main/java/com/keepassdroid/utils/SprEngineV4.java @@ -154,8 +154,8 @@ public class SprEngineV4 { else if (scan == 'O') { sp.searchInOther = true; } else { return null; } - List list = new ArrayList(); - ctx.db.rootGroup.searchEntries(sp, list); + List list = new ArrayList<>(); + ctx.db.getRootGroup().searchEntries(sp, list); if (list.size() > 0) { return new TargetResult((PwEntryV4)list.get(0), wanted);