Repository navigation
fix: decode XML key files by version (1.0 base64, 2.0 hex) - #11
Open
guilherme-n-l wants to merge 1 commit into
Open
guilherme-n-l wants to merge 1 commit into
guilherme-n-l wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
KeePass XML key files store the key as base64 in version 1.0 and as hex in version 2.0 (the
.keyxformat of KeePassXC and KeePass 2.47+).parseXMLKeyFilehad the two swapped, so every.keyxfell back to SHA-256 of the whole file and produced a key no other client uses; databases protected with one reportedwrongCredentials.Change
UnlockData.parseXMLKeyFilereads<Meta><Version>:1.x→ base642.x→ hex, whitespace ignored (KeePassXC writes grouped hex over two lines), then the existingHashchecknormalizeKeyFilenow 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-Hashtest uses hex data, and a new test covers a version-less file with 64 hex digits.Testing
swift test --filter KeyFileTests: 14 tests pass..keyxopens in KeePassXC, both with a password and key-file only. Before this change both failed with "Invalid credentials".StaticReaderAPITestscompiler-crash workaround as in the earlier PR; it isn't part of this change.Generated by Claude Code