From ebe8424d11b91fc517e12f7963967b9563b7cdae Mon Sep 17 00:00:00 2001 From: Brian Pellin Date: Thu, 29 Apr 2010 13:45:44 -0500 Subject: [PATCH] Kdb4 header parsing. --- .../keepassdroid/database/CrsAlgorithm.java | 28 ++++++++++ src/com/keepassdroid/database/PwDbHeader.java | 3 + .../keepassdroid/database/PwDbHeaderV3.java | 6 +- .../keepassdroid/database/PwDbHeaderV4.java | 56 ++++++++++++++++++- .../database/edit/SetPassword.java | 4 +- .../database/load/ImporterV3.java | 5 +- .../database/load/ImporterV4.java | 15 ++++- .../database/save/PwDbHeaderOutput.java | 2 +- .../database/save/PwDbV3Output.java | 8 +-- .../com/keepassdroid/tests/database/Kdb4.java | 2 +- .../tests/database/Kdb4Header.java | 45 +++++++++++++++ .../tests/output/PwManagerOutputTest.java | 2 +- 12 files changed, 155 insertions(+), 21 deletions(-) create mode 100644 src/com/keepassdroid/database/CrsAlgorithm.java create mode 100644 tests/src/com/keepassdroid/tests/database/Kdb4Header.java diff --git a/src/com/keepassdroid/database/CrsAlgorithm.java b/src/com/keepassdroid/database/CrsAlgorithm.java new file mode 100644 index 000000000..caa4ef4a0 --- /dev/null +++ b/src/com/keepassdroid/database/CrsAlgorithm.java @@ -0,0 +1,28 @@ +/* + * Copyright 2010 Brian Pellin. + * + * This file is part of KeePassDroid. + * + * KeePassDroid 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. + * + * KeePassDroid 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 KeePassDroid. If not, see . + * + */ +package com.keepassdroid.database; + +public class CrsAlgorithm { + public static final int Null = 0; + public static final int ArcFourVariant = 1; + public static final int Salsa20 = 2; + + public static final int Count = 3; +} diff --git a/src/com/keepassdroid/database/PwDbHeader.java b/src/com/keepassdroid/database/PwDbHeader.java index b5186f7ac..24727190e 100644 --- a/src/com/keepassdroid/database/PwDbHeader.java +++ b/src/com/keepassdroid/database/PwDbHeader.java @@ -28,5 +28,8 @@ public abstract class PwDbHeader { /** Used for the dwKeyEncRounds AES transformations */ public byte mTransformSeed[]; + + /** IV used for content encryption */ + public byte mEncryptionIV[]; } diff --git a/src/com/keepassdroid/database/PwDbHeaderV3.java b/src/com/keepassdroid/database/PwDbHeaderV3.java index 7c445eb9b..0faf4c4d2 100644 --- a/src/com/keepassdroid/database/PwDbHeaderV3.java +++ b/src/com/keepassdroid/database/PwDbHeaderV3.java @@ -53,9 +53,6 @@ public class PwDbHeaderV3 extends PwDbHeader { public int flags; public int version; - /** IV used for content encryption */ - public byte encryptionIV[] = new byte[16]; - /** Number of groups in the database */ public int numGroups; /** Number of entries in the database */ @@ -78,7 +75,7 @@ public class PwDbHeaderV3 extends PwDbHeader { version = Types.readInt( buf, offset + 12 ); System.arraycopy( buf, offset + 16, mMasterSeed, 0, 16 ); - System.arraycopy( buf, offset + 32, encryptionIV, 0, 16 ); + System.arraycopy( buf, offset + 32, mEncryptionIV, 0, 16 ); numGroups = Types.readInt( buf, offset + 48 ); numEntries = Types.readInt( buf, offset + 52 ); @@ -96,6 +93,7 @@ public class PwDbHeaderV3 extends PwDbHeader { public PwDbHeaderV3() { mMasterSeed = new byte[16]; mTransformSeed = new byte[32]; + mEncryptionIV = new byte[16]; } public static boolean matchesHeader(int sig1, int sig2) { diff --git a/src/com/keepassdroid/database/PwDbHeaderV4.java b/src/com/keepassdroid/database/PwDbHeaderV4.java index cfe7fd1c3..c20f31f02 100644 --- a/src/com/keepassdroid/database/PwDbHeaderV4.java +++ b/src/com/keepassdroid/database/PwDbHeaderV4.java @@ -42,12 +42,15 @@ public class PwDbHeaderV4 extends PwDbHeader { public static final byte TransformRounds = 6; public static final byte EncryptionIV = 7; public static final byte ProtectedStreamKey = 8; - public static final byte StreamStartBytes = 8; - public static final byte InnerRandomStreamID = 9; + public static final byte StreamStartBytes = 9; + public static final byte InnerRandomStreamID = 10; } private PwDatabaseV4 mDb; + private byte[] mProtectedStreamKey; + private byte[] mStreamStartBytes; + private int mInnerRandomStream; public PwDbHeaderV4(PwDatabaseV4 db) { mDb = db; @@ -59,6 +62,13 @@ public class PwDbHeaderV4 extends PwDbHeader { * @throws InvalidDBVersionException */ public void loadFromFile(InputStream is) throws IOException, InvalidDBVersionException { + + int sig1 = Types.readInt(is); + int sig2 = Types.readInt(is); + + if ( ! matchesHeader(sig1, sig2) ) { + throw new InvalidDBVersionException(); + } long version = Types.readUInt(is); if ( ! validVersion(version) ) { @@ -92,18 +102,43 @@ public class PwDbHeaderV4 extends PwDbHeader { case PwDbHeaderV4Fields.CipherID: setCipher(fieldData); + break; case PwDbHeaderV4Fields.CompressionFlags: setCompressionFlags(fieldData); + break; case PwDbHeaderV4Fields.MasterSeed: mMasterSeed = fieldData; + break; case PwDbHeaderV4Fields.TransformSeed: mTransformSeed = fieldData; + break; case PwDbHeaderV4Fields.TransformRounds: setTransformRounds(fieldData); + break; + + case PwDbHeaderV4Fields.EncryptionIV: + mEncryptionIV = fieldData; + break; + + case PwDbHeaderV4Fields.ProtectedStreamKey: + mProtectedStreamKey = fieldData; + break; + + case PwDbHeaderV4Fields.StreamStartBytes: + mStreamStartBytes = fieldData; + break; + + case PwDbHeaderV4Fields.InnerRandomStreamID: + setRandomStreamID(fieldData); + break; + + default: + throw new IOException("Invalid header type."); + } return false; @@ -123,7 +158,7 @@ public class PwDbHeaderV4 extends PwDbHeader { } int flag = Types.readInt(pbFlags, 0); - if ( flag >= PwCompressionAlgorithm.Count ) { + if ( flag < 0 || flag >= PwCompressionAlgorithm.Count ) { throw new IOException("Unrecognized compression flag."); } @@ -143,6 +178,21 @@ public class PwDbHeaderV4 extends PwDbHeader { throw new IOException("Rounds higher than " + Long.MAX_VALUE + " are not currently supported."); } + mDb.mNumKeyEncRounds = rnd; + + } + + private void setRandomStreamID(byte[] streamID) throws IOException { + if ( streamID == null || streamID.length != 4 ) { + throw new IOException("Invalid stream id."); + } + + int id = Types.readInt(streamID, 0); + if ( id < 0 || id >= CrsAlgorithm.Count ) { + throw new IOException("Invalid stream id."); + } + + mInnerRandomStream = id; } /** Determines if this is a supported version. diff --git a/src/com/keepassdroid/database/edit/SetPassword.java b/src/com/keepassdroid/database/edit/SetPassword.java index 0ee2c19c1..da9036708 100644 --- a/src/com/keepassdroid/database/edit/SetPassword.java +++ b/src/com/keepassdroid/database/edit/SetPassword.java @@ -23,7 +23,7 @@ import java.io.IOException; import com.keepassdroid.Database; -import com.keepassdroid.database.PwDatabaseV3; +import com.keepassdroid.database.PwDatabase; import com.keepassdroid.database.exception.InvalidKeyFileException; public class SetPassword extends RunnableOnFinish { @@ -53,7 +53,7 @@ public class SetPassword extends RunnableOnFinish { @Override public void run() { - PwDatabaseV3 pm = mDb.mPM; + PwDatabase pm = mDb.mPM; byte[] backupKey = new byte[pm.masterKey.length]; System.arraycopy(pm.masterKey, 0, backupKey, 0, backupKey.length); diff --git a/src/com/keepassdroid/database/load/ImporterV3.java b/src/com/keepassdroid/database/load/ImporterV3.java index b3c73e73a..4a16ddff0 100644 --- a/src/com/keepassdroid/database/load/ImporterV3.java +++ b/src/com/keepassdroid/database/load/ImporterV3.java @@ -49,6 +49,7 @@ import com.keepassdroid.UpdateStatus; import com.keepassdroid.crypto.AESProvider; import com.keepassdroid.crypto.finalkey.FinalKey; import com.keepassdroid.crypto.finalkey.FinalKeyFactory; +import com.keepassdroid.database.PwDatabase; import com.keepassdroid.database.PwDatabaseV3; import com.keepassdroid.database.PwDate; import com.keepassdroid.database.PwDbHeader; @@ -100,7 +101,7 @@ public class ImporterV3 extends Importer { * @throws InvalidAlgorithmParameterException if error decrypting main file body. * @throws ShortBufferException if error decrypting main file body. */ - public PwDatabaseV3 openDatabase( InputStream inStream, String password, String keyfile ) + public PwDatabase openDatabase( InputStream inStream, String password, String keyfile ) throws IOException, InvalidKeyFileException, InvalidPasswordException, InvalidDBSignatureException, InvalidDBVersionException { return openDatabase(inStream, password, keyfile, new UpdateStatus()); @@ -175,7 +176,7 @@ public class ImporterV3 extends Importer { //BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESEngine())); try { - cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( finalKey, "AES" ), new IvParameterSpec( hdr.encryptionIV ) ); + cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( finalKey, "AES" ), new IvParameterSpec( hdr.mEncryptionIV ) ); } catch (InvalidKeyException e1) { throw new IOException("Invalid key"); } catch (InvalidAlgorithmParameterException e1) { diff --git a/src/com/keepassdroid/database/load/ImporterV4.java b/src/com/keepassdroid/database/load/ImporterV4.java index 5d5fe6ea8..fd069b0ff 100644 --- a/src/com/keepassdroid/database/load/ImporterV4.java +++ b/src/com/keepassdroid/database/load/ImporterV4.java @@ -24,7 +24,9 @@ import java.io.InputStream; import com.keepassdroid.UpdateStatus; import com.keepassdroid.database.PwDatabaseV4; +import com.keepassdroid.database.PwDbHeaderV4; import com.keepassdroid.database.exception.InvalidDBSignatureException; +import com.keepassdroid.database.exception.InvalidDBVersionException; import com.keepassdroid.database.exception.InvalidKeyFileException; import com.keepassdroid.database.exception.InvalidPasswordException; @@ -33,7 +35,7 @@ public class ImporterV4 extends Importer { @Override public PwDatabaseV4 openDatabase(InputStream inStream, String password, String keyfile) throws IOException, InvalidKeyFileException, - InvalidPasswordException, InvalidDBSignatureException { + InvalidPasswordException, InvalidDBSignatureException, InvalidDBVersionException { return openDatabase(inStream, password, keyfile, new UpdateStatus()); } @@ -42,9 +44,16 @@ public class ImporterV4 extends Importer { public PwDatabaseV4 openDatabase(InputStream inStream, String password, String keyfile, UpdateStatus status) throws IOException, InvalidKeyFileException, InvalidPasswordException, - InvalidDBSignatureException { + InvalidDBSignatureException, InvalidDBVersionException { - return null; + + PwDatabaseV4 db = new PwDatabaseV4(); + + PwDbHeaderV4 header = new PwDbHeaderV4(db); + + header.loadFromFile(inStream); + + return db; } diff --git a/src/com/keepassdroid/database/save/PwDbHeaderOutput.java b/src/com/keepassdroid/database/save/PwDbHeaderOutput.java index b4d9020ea..11c2bffcf 100644 --- a/src/com/keepassdroid/database/save/PwDbHeaderOutput.java +++ b/src/com/keepassdroid/database/save/PwDbHeaderOutput.java @@ -41,7 +41,7 @@ public class PwDbHeaderOutput { mOS.write(Types.writeInt(mHeader.flags)); mOS.write(Types.writeInt(mHeader.version)); mOS.write(mHeader.mMasterSeed); - mOS.write(mHeader.encryptionIV); + mOS.write(mHeader.mEncryptionIV); mOS.write(Types.writeInt(mHeader.numGroups)); mOS.write(Types.writeInt(mHeader.numEntries)); mOS.write(mHeader.contentsHash); diff --git a/src/com/keepassdroid/database/save/PwDbV3Output.java b/src/com/keepassdroid/database/save/PwDbV3Output.java index e2da9c612..5131a7be9 100644 --- a/src/com/keepassdroid/database/save/PwDbV3Output.java +++ b/src/com/keepassdroid/database/save/PwDbV3Output.java @@ -84,7 +84,7 @@ public class PwDbV3Output { // Before we output the header, we should sort our list of groups and remove any orphaned nodes that are no longer part of the group hierarchy sortGroupsForOutput(); - PwDbHeaderV3 header = outputHeader(mOS); + PwDbHeader header = outputHeader(mOS); byte[] finalKey = getFinalKey(header); @@ -96,7 +96,7 @@ public class PwDbV3Output { } try { - cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES" ), new IvParameterSpec(header.encryptionIV) ); + cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES" ), new IvParameterSpec(header.mEncryptionIV) ); CipherOutputStream cos = new CipherOutputStream(mOS, cipher); BufferedOutputStream bos = new BufferedOutputStream(cos); outputPlanGroupAndEntries(bos); @@ -135,7 +135,7 @@ public class PwDbV3Output { // Reuse random values to test equivalence in debug mode if ( mDebug ) { - System.arraycopy(mPM.dbHeader.encryptionIV, 0, header.encryptionIV, 0, mPM.dbHeader.encryptionIV.length); + System.arraycopy(mPM.dbHeader.mEncryptionIV, 0, header.mEncryptionIV, 0, mPM.dbHeader.mEncryptionIV.length); System.arraycopy(mPM.dbHeader.mMasterSeed, 0, header.mMasterSeed, 0, mPM.dbHeader.mMasterSeed.length); System.arraycopy(mPM.dbHeader.mTransformSeed, 0, header.mTransformSeed, 0, mPM.dbHeader.mTransformSeed.length); } else { @@ -145,7 +145,7 @@ public class PwDbV3Output { } catch (NoSuchAlgorithmException e) { throw new PwDbOutputException("Does not support secure random number generation."); } - random.nextBytes(header.encryptionIV); + random.nextBytes(header.mEncryptionIV); random.nextBytes(header.mMasterSeed); random.nextBytes(header.mTransformSeed); } diff --git a/tests/src/com/keepassdroid/tests/database/Kdb4.java b/tests/src/com/keepassdroid/tests/database/Kdb4.java index 94e3b213b..0070bf65c 100644 --- a/tests/src/com/keepassdroid/tests/database/Kdb4.java +++ b/tests/src/com/keepassdroid/tests/database/Kdb4.java @@ -1,5 +1,5 @@ /* - * Copyright 2009 Brian Pellin. + * Copyright 2010 Brian Pellin. * * This file is part of KeePassDroid. * diff --git a/tests/src/com/keepassdroid/tests/database/Kdb4Header.java b/tests/src/com/keepassdroid/tests/database/Kdb4Header.java new file mode 100644 index 000000000..47b65ffd4 --- /dev/null +++ b/tests/src/com/keepassdroid/tests/database/Kdb4Header.java @@ -0,0 +1,45 @@ +/* + * Copyright 2010 Brian Pellin. + * + * This file is part of KeePassDroid. + * + * KeePassDroid 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 3 of the License, or + * (at your option) any later version. + * + * KeePassDroid 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 KeePassDroid. If not, see . + * + */ +package com.keepassdroid.tests.database; + +import java.io.InputStream; + +import android.content.Context; +import android.content.res.AssetManager; +import android.test.AndroidTestCase; + +import com.keepassdroid.database.PwDatabaseV4; +import com.keepassdroid.database.load.ImporterV4; + +public class Kdb4Header extends AndroidTestCase { + public void testReadHeader() throws Exception { + Context ctx = getContext(); + + AssetManager am = ctx.getAssets(); + InputStream is = am.open("test.kdbx", AssetManager.ACCESS_STREAMING); + + ImporterV4 importer = new ImporterV4(); + + PwDatabaseV4 db = importer.openDatabase(is, null, null); + + assertEquals(6000, db.mNumKeyEncRounds); + + } +} diff --git a/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java b/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java index 7e53c2648..25b36fbf2 100644 --- a/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java +++ b/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java @@ -88,7 +88,7 @@ public class PwManagerOutputTest extends AndroidTestCase { assertEquals("Signature2 unequal", expected.signature2, actual.signature2); assertEquals("Version unequal", expected.version, actual.version); assertArrayEquals("Hash unequal", expected.contentsHash, actual.contentsHash); - assertArrayEquals("IV unequal", expected.encryptionIV, actual.encryptionIV); + assertArrayEquals("IV unequal", expected.mEncryptionIV, actual.mEncryptionIV); assertArrayEquals("Seed unequal", expected.mMasterSeed, actual.mMasterSeed); assertArrayEquals("Seed2 unequal", expected.mTransformSeed, actual.mTransformSeed); }