[kdbx] Custos v1.2 core: KeePass import (KDBX 3.1, 4) and export (KDBX 4), v0.8.0 #10

Manually merged
mnovak merged 4 commits from feature/kdbx into main 2026-11-03 23:11:54 +01:00
Owner

Custos v1.2 core: KeePass import and export (docs/KDBX.md). Version 0.8.0.

What

  • custos-kdbx (new crate), in two layers:
    • The file format: reads KDBX 3.1 and KDBX 4 (4.0, 4.1), writes KDBX 4.0.
      • Ciphers: AES-256, ChaCha20. KDFs: Argon2d/id, AES-KDF. Password, key file (all four KeePass kinds) or both.
      • Protected values through the inner stream (ChaCha20, or Salsa20 in 3.1).
      • Attachments: from the inner header (KDBX 4) or Meta/Binaries (3.1).
    • The mapping: KeePass entries ↔ Custos items, as tabled in docs/KDBX.md:
      • groups become folders; the entry UUID becomes the item id;
      • standard and custom strings, extra URLs (KP2A_URL*), both TOTP forms;
      • expiry, auto-type, tags, attachments, and the history of secret fields;
      • the recycle bin is skipped.
    • "What does not survive" lists the losses plainly (icons, colours, custom data, history of non-secret fields, …).
  • custos-vault: items get optional folder and attachments (SecretBytes: wiped on drop, never printed,
    base64 in the JSON). An item without them serializes exactly as before.
  • No custom crypto: aes, cbc, chacha20, salsa20, hmac, sha2, argon2 (RustCrypto); flate2;
    roxmltree with DTDs off.

Why its own reader instead of the keepass crate

  • The rules here allow only RustCrypto for crypto.
  • The hostile-input limits must be checked before any work.
  • The gate is "a KeePassXC file survives import → export". That needs control over exactly what is kept and
    what is said to be lost.

Tests (all pass, debug and release)

  • custos-kdbx, 23:
    • format.rs (14): every cipher × KDF round trip; wrong password and no key; four spellings of one key file
      open each other's files; one changed byte anywhere and truncation at every length both fail with the
      right error and never panic; out-of-limit KDF parameters refused in well under a second; billion laughs,
      XXE, deep nesting, broken XML, a gzip bomb (600 MiB → TooLarge), Salsa20 in KDBX 4, Twofish and unknown
      versions refused; no secret in any Debug.
    • keepassxc.rs (3): two files KeePassXC 2.7.12 wrote (base64 test vectors, password custos-test): a KDBX
      3.1 from keepassxc-cli import and a KDBX 4.0 from merge into a Custos file. Both map exactly to what
      tools/kdbx/sample.xml put in them.
    • mapping.rs (5): every item type round-trips field by field; a KeePass-style database maps as tabled.
    • Plus a unit test of the time conversions.
  • custos-vault: 2 new (folder and attachments survive; old bodies unchanged; attachment bytes never print).
  • Interop gate, tools/kdbx/interop.sh: run in WSL with keepassxc-cli 2.7.12 from nixpkgs.
    • KDBX 3.1 and 4.0 files go through Custos (read → items → KDBX 4), then KeePassXC opens what Custos wrote.
    • compare.py on both XML exports: 0 differences (3 and 4 entries).
    • Attachment bytes are identical (attachment-export).
    • KeePassXC generates the 8-digit, 60 s TOTP that came from KeePass 2's TOTP Seed/TOTP Settings.

Self-review

  • What can a hostile .kdbx do? Every length is bounds-checked (Cur::take).
    • Header fields ≤ 64 KiB, file ≤ 512 MiB, decompressed ≤ 512 MiB (the gzip bomb test).
    • Argon2 ≤ 2 GiB / 1000 passes / 64 lanes and AES-KDF ≤ 2³⁰ rounds are checked before any KDF runs.
    • DTD is off, so no entities at all; group depth ≤ 256; history entries do not nest.
    • The tamper and truncation tests cover every byte of a file.
  • KDBX 3.1 has no header MAC. A changed 3.1 header can make us run AES-KDF with its stated rounds (capped)
    before StreamStartBytes says "wrong key". That is the format's weakness, not ours. The cap keeps it bounded.
  • Secrets in memory: decrypted payloads, protected values and attachments are Zeroizing. roxmltree's
    unescaped text copies (for unprotected values) are ordinary strings; THREAT_MODEL already says copies remain
    where parsers move buffers.
  • Test vectors: tests/data/keepassxc_files.rs holds two small KeePassXC files (≈3 KB each) as base64 in a
    source file. They are test data, not build output; interop.sh makes new ones.
  • Not verified: KeePass 2 (the .NET app) itself, KDBX 4.1-only fields beyond ignoring them, and real
    KeePassXC GUI files with Argon2d (the CLI writes AES-KDF/3.1). The reader handles Argon2d (round-trip tested),
    but no Argon2d file from KeePassXC is in the vectors.
  • The web vault (custos-wasm) shows items through an explicit view, so folder and attachments never
    reach the page.
Custos v1.2 core: KeePass import and export (`docs/KDBX.md`). Version 0.8.0. ## What - **`custos-kdbx`** (new crate), in two layers: - **The file format:** reads KDBX 3.1 and KDBX 4 (4.0, 4.1), writes KDBX 4.0. - Ciphers: AES-256, ChaCha20. KDFs: Argon2d/id, AES-KDF. Password, key file (all four KeePass kinds) or both. - Protected values through the inner stream (ChaCha20, or Salsa20 in 3.1). - Attachments: from the inner header (KDBX 4) or `Meta/Binaries` (3.1). - **The mapping:** KeePass entries ↔ Custos items, as tabled in `docs/KDBX.md`: - groups become folders; the entry UUID becomes the item id; - standard and custom strings, extra URLs (`KP2A_URL*`), both TOTP forms; - expiry, auto-type, tags, attachments, and the history of secret fields; - the recycle bin is skipped. - "What does not survive" lists the losses plainly (icons, colours, custom data, history of non-secret fields, …). - **`custos-vault`:** items get optional `folder` and `attachments` (`SecretBytes`: wiped on drop, never printed, base64 in the JSON). An item without them serializes exactly as before. - **No custom crypto:** `aes`, `cbc`, `chacha20`, `salsa20`, `hmac`, `sha2`, `argon2` (RustCrypto); `flate2`; `roxmltree` with DTDs off. ## Why its own reader instead of the `keepass` crate - The rules here allow only RustCrypto for crypto. - The hostile-input limits must be checked before any work. - The gate is "a KeePassXC file survives import → export". That needs control over exactly what is kept and what is said to be lost. ## Tests (all pass, debug and release) - **custos-kdbx, 23:** - `format.rs` (14): every cipher × KDF round trip; wrong password and no key; four spellings of one key file open each other's files; one changed byte anywhere and truncation at every length both fail with the right error and never panic; out-of-limit KDF parameters refused in well under a second; billion laughs, XXE, deep nesting, broken XML, a gzip bomb (600 MiB → `TooLarge`), Salsa20 in KDBX 4, Twofish and unknown versions refused; no secret in any `Debug`. - `keepassxc.rs` (3): two files KeePassXC 2.7.12 wrote (base64 test vectors, password `custos-test`): a KDBX 3.1 from `keepassxc-cli import` and a KDBX 4.0 from `merge` into a Custos file. Both map exactly to what `tools/kdbx/sample.xml` put in them. - `mapping.rs` (5): every item type round-trips field by field; a KeePass-style database maps as tabled. - Plus a unit test of the time conversions. - **custos-vault:** 2 new (folder and attachments survive; old bodies unchanged; attachment bytes never print). - **Interop gate, `tools/kdbx/interop.sh`:** run in WSL with `keepassxc-cli` 2.7.12 from nixpkgs. - KDBX 3.1 and 4.0 files go through Custos (read → items → KDBX 4), then KeePassXC opens what Custos wrote. - `compare.py` on both XML exports: **0 differences** (3 and 4 entries). - Attachment bytes are identical (`attachment-export`). - KeePassXC generates the 8-digit, 60 s TOTP that came from KeePass 2's `TOTP Seed`/`TOTP Settings`. ## Self-review - **What can a hostile `.kdbx` do?** Every length is bounds-checked (`Cur::take`). - Header fields ≤ 64 KiB, file ≤ 512 MiB, decompressed ≤ 512 MiB (the gzip bomb test). - Argon2 ≤ 2 GiB / 1000 passes / 64 lanes and AES-KDF ≤ 2³⁰ rounds are checked before any KDF runs. - DTD is off, so no entities at all; group depth ≤ 256; history entries do not nest. - The tamper and truncation tests cover every byte of a file. - **KDBX 3.1 has no header MAC.** A changed 3.1 header can make us run AES-KDF with its stated rounds (capped) before `StreamStartBytes` says "wrong key". That is the format's weakness, not ours. The cap keeps it bounded. - **Secrets in memory:** decrypted payloads, protected values and attachments are `Zeroizing`. roxmltree's unescaped text copies (for unprotected values) are ordinary strings; THREAT_MODEL already says copies remain where parsers move buffers. - **Test vectors:** `tests/data/keepassxc_files.rs` holds two small KeePassXC files (≈3 KB each) as base64 in a source file. They are test data, not build output; `interop.sh` makes new ones. - **Not verified:** KeePass 2 (the .NET app) itself, KDBX 4.1-only fields beyond ignoring them, and real KeePassXC GUI files with Argon2d (the CLI writes AES-KDF/3.1). The reader handles Argon2d (round-trip tested), but no Argon2d file from KeePassXC is in the vectors. - **The web vault** (custos-wasm) shows items through an explicit view, so `folder` and `attachments` never reach the page.
mnovak added 4 commits 2026-11-03 21:00:17 +01:00
mnovak manually merged commit 38c458d21b into main 2026-11-03 23:11:54 +01:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mnovak/CustosPublic#10