Kdb4 header parsing.

This commit is contained in:
Brian Pellin committed 2010-04-29 13:45:44 -05:00
1 parent b4818ec18a
commit ebe8424d11
12 files changed
+155 -21

No files matched your search

@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
@@ -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[];
}
@@ -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) {
@@ -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.
@@ -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);
@@ -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) {
@@ -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;
}
@@ -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);
@@ -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);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2009 Brian Pellin.
* Copyright 2010 Brian Pellin.
*
* This file is part of KeePassDroid.
*
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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);
}
}
@@ -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);
}