Skip to content

fix: decode XML key files by version (1.0 base64, 2.0 hex) - #11

Open
guilherme-n-l wants to merge 1 commit into
shadone:developfrom
guilherme-n-l:fix/xml-keyfile-versions
Open

guilherme-n-l wants to merge 1 commit into
shadone:developfrom
guilherme-n-l:fix/xml-keyfile-versions

Conversation

@guilherme-n-l

Copy link
Copy Markdown

Summary

KeePass XML key files store the key as base64 in version 1.0 and as hex in version 2.0 (the .keyx format of KeePassXC and KeePass 2.47+). parseXMLKeyFile had the two swapped, so every .keyx fell back to SHA-256 of the whole file and produced a key no other client uses; databases protected with one reported wrongCredentials.

Change

  • UnlockData.parseXMLKeyFile reads <Meta><Version>:
    • 1.x → base64
    • 2.x → hex, whitespace ignored (KeePassXC writes grouped hex over two lines), then the existing Hash check
    • no version → 64 hex digits if present, otherwise base64
  • The doc comment on normalizeKeyFile now describes the formats correctly.
  • KeyFileTests: the v1/v2 tests use the spec's encodings (base64 for 1.0, KeePassXC's grouped multi-line hex for 2.0), the mismatched-Hash test uses hex data, and a new test covers a version-less file with 64 hex digits.

Testing

  • swift test --filter KeyFileTests: 14 tests pass.
  • Against keepassxc-cli 2.7.6: a KDBX 4 file written by KDBXKit with a KeePassXC-generated .keyx opens in KeePassXC, both with a password and key-file only. Before this change both failed with "Invalid credentials".
  • Note: with Swift 6.3.3 the test target needs the same StaticReaderAPITests compiler-crash workaround as in the earlier PR; it isn't part of this change.

Generated by Claude Code

KeePass XML key files come in two versions
(https://keepass.info/help/base/keys.html#keyfiles):

  version 1.0  <Data>AAECAwQF...=</Data>                 base64
  version 2.0  <Data Hash="630DCD29">                     hex, grouped,
                 00010203 04050607 08090A0B 0C0D0E0F      plus the
                 10111213 14151617 18191A1B 1C1D1E1F      Hash check
               </Data>

parseXMLKeyFile had them swapped: it treated a file with a Hash
attribute (2.0) as base64 and one without (1.0) as hex. For a .keyx
written by KeePassXC or KeePass 2.47+ it base64-decoded the hex text,
got 48 bytes instead of 32, returned nil, and normalizeKeyFile fell
back to SHA-256 of the whole file. The derived key then differs from
every other client and the database reports wrongCredentials. The
KeyFileTests asserted the swapped encodings, so the suite passed.

The encoding now follows <Meta><Version>: 1.x is base64, 2.x is hex
(whitespace ignored) with the Hash check; files without a version
accept 64 hex digits, otherwise base64. KeyFileTests use the spec's
formats, including KeePassXC's grouped multi-line hex layout and a
version-less file.

Verified against keepassxc-cli 2.7.6: a KDBX 4 file written with a
KeePassXC-generated .keyx (with and without a password) now opens in
KeePassXC; before this change both failed with "Invalid credentials".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HdzqH7rejgXYHCaGaUAzNj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant