jorabin / KeePassJava2

Java API for KeePass Password Databases - Read/Write 2.x (File versions 3 and 4), Read 1.x
Apache License 2.0
247 stars 69 forks source link

Error with composite key #9

Closed ivanovpv closed 7 years ago

ivanovpv commented 7 years ago

Tried to load test database with DomDatabaseWrapper.

Database was created with KeePass v. 2.34 for Windows and secured with composite key: password+key file.

Code looks like:

  Credentials credentials = new KdbxCreds(password.getBytes(), keyInputStream);
  DomDatabaseWrapper.load(credentials, databaseInputStream);

Meantime, if I remove from database composite key (i.e. only password based encryption) - I'm able to read database correctly using:

  Credentials credentials = new KdbxCreds(password.getBytes());
  DomDatabaseWrapper.load(credentials, databaseInputStream);
jorabin commented 7 years ago

That's odd. There's a specific test for this use case in DomDatabaseWrapperTest, does the test work for you?

@Test
public void inspectKeyfileDatabase() throws IOException {
    InputStream keyFileInputStream = getClass().getClassLoader().getResourceAsStream("KeyFileDatabase.key");
    Credentials credentials = new KdbxCreds("123".getBytes(), keyFileInputStream);
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream("KeyFileDatabase.kdbx");
    DomDatabaseWrapper database = new DomDatabaseWrapper(new KdbxStreamFormat(), credentials, inputStream);

    database.save(new StreamFormat.None(), new Credentials.None(), System.out);
}

What's the specific error that is being thrown?

ivanovpv commented 7 years ago

I agree - that's odd. Unit test mentioned by you works well but only with your test database.

With my database it fails with trace:

java.lang.IllegalStateException: Inconsistent stream start bytes. This usually means the credentials were wromng.
                                                                                     at org.linguafranca.pwdb.kdbx.stream_3_1.KdbxSerializer.checkStartBytes(KdbxSerializer.java:129)
                                                                                     at org.linguafranca.pwdb.kdbx.stream_3_1.KdbxSerializer.createUnencryptedInputStream(KdbxSerializer.java:88)
                                                                                     at org.linguafranca.pwdb.kdbx.stream_3_1.KdbxStreamFormat.load(KdbxStreamFormat.java:37)
                                                                                     at org.linguafranca.pwdb.kdbx.dom.DomDatabaseWrapper.<init>(DomDatabaseWrapper.java:53)
                                                                                     at org.linguafranca.pwdb.kdbx.dom.DomDatabaseWrapper.load(DomDatabaseWrapper.java:58)

Here's my test database and test database key file

Database password is 123456

jorabin commented 7 years ago

That database opens with password 123456, and does not require the key file. So when I try to open it with the key file as well as the password it fails, as expected. Am I misunderstanding something?

ivanovpv commented 7 years ago

Hmmm... in my case with native KeePass v. 2.34 (Win x64) I can open database only if I apply both password and key file. But suddenly I realized that I could open it with KeePassJava2 w/o key file (only using password).

So from this point of view looks like correct, but a bit strange... Can't understand whose bug/feature it is? native KeePass?

jorabin commented 7 years ago

When I run KeePass 2.34 in a windows emulation it will not open with both password and creds so that is odd. Anyway, glad all sorted for now. Plus the good news is I got to see that there is a typo in "This usually means the credentials were wromng." :-)