9.5 KiB
KeePass import and export (custos-kdbx)
Custos reads KeePass's KDBX 3.1 and KDBX 4 files (4.0 and 4.1) and writes KDBX 4.0, so a vault can move in from KeePassXC or KeePass 2 and back out to them. KeePassXC 2.7 still writes 3.1 for a database with AES-KDF (its command line does by default); its app writes 4.0 with Argon2d. The crate has two layers:
- The file format (
read,write): bytes and a key in, a KDBX database model out, and back. It knows groups, entries, strings, attachments, times, auto-type and history. It does not know Custos items. - The mapping (
to_items,from_items): KDBX entries to Custos items and back (the table below).
No custom crypto: AES-256 (aes, cbc), ChaCha20 (chacha20), SHA-256/512 (sha2), HMAC-SHA-256 (hmac),
Argon2d/id (argon2), all RustCrypto. Compression is gzip (flate2), XML is quick-xml events.
Reading
| Step | What |
|---|---|
| Signature | 03 D9 A2 9A, 67 FB 4B B5, then minor/major version (u16 LE each). Major 4 here; major 3 is read as below ("KDBX 3.1"); anything else is UnsupportedVersion |
| Outer header | TLV fields: id (u8), length (u32 LE), data, until id 0. Cipher, compression, master seed (32 bytes), IV, KDF parameters (a VariantDictionary) |
| Header checks | SHA-256 of the header (mismatch: Corrupted), then HMAC-SHA-256 of the header with the block key for index 2⁶⁴−1 (mismatch: WrongKey) |
| Key | composite = SHA-256(SHA-256(password) ‖ key-file key); with only one of the two, only that one goes in |
| KDF | Argon2d or Argon2id (S, P, M bytes, I, V = 0x13; a non-empty secret K or data A is refused), or AES-KDF (R rounds of AES-256-ECB keyed with S over both halves, then SHA-256) |
| Keys | cipher key = SHA-256(master seed ‖ transformed); HMAC base = SHA-512(master seed ‖ transformed ‖ 0x01); block i key = SHA-512(u64 LE i ‖ HMAC base) |
| Blocks | HMAC (32) ‖ length (u32 LE) ‖ data, each checked: HMAC-SHA-256(block key, u64 LE index ‖ u32 LE length ‖ data). A zero-length block ends the stream. A failed block: Corrupted |
| Payload | AES-256-CBC (PKCS#7) or ChaCha20 (96-bit IV) decryption, then gunzip if the compression flag is 1 |
| Inner header | TLV: inner stream id (3 = ChaCha20; Salsa20 = 2 is refused), inner stream key (64 bytes), binaries (flag byte ‖ data, in pool order), end |
| XML | protected values are XORed with the inner stream, key = SHA-512(stream key)[0..32], nonce = [32..44], in document order |
KDBX 3.1
The same key, cipher and XML, with these differences: header field lengths are u16; the KDF is always AES-KDF
from the header (TransformSeed, TransformRounds); there is no header HMAC: after decryption the first 32
bytes must equal the header's StreamStartBytes (otherwise WrongKey); the payload is SHA-256 hashed blocks
(index u32, hash, length u32, data; a changed block is Corrupted); the inner stream is Salsa20 (key =
SHA-256 of ProtectedStreamKey, KeePass's fixed nonce E8 30 09 4B 97 20 5D 2A), and its id and key are in the
outer header; attachments are in Meta/Binaries (base64, gzipped when Compressed="True"), referenced by ID.
Key files (Key::key_file): KeePass XML 2.0 (<Data Hash="…"> hex, the hash is checked), XML 1.0 (base64),
exactly 32 bytes (used as is), exactly 64 hex digits (decoded), anything else: SHA-256 of the file.
Limits (checked before any expensive work)
| What | Limit | Error |
|---|---|---|
| File size | 512 MiB | TooLarge |
| A header field | 64 KiB (a binary in the inner header: 256 MiB) | Malformed |
| Argon2 | M ≤ 2 GiB, I 1 ..= 1000, P 1 ..= 64, V = 0x13 |
UnsupportedKdf |
| AES-KDF | R 1 ..= 2³⁰ |
UnsupportedKdf |
| Decompressed payload | 512 MiB | TooLarge |
| XML | no <!DOCTYPE> (entities are never expanded), element depth ≤ 256 |
Malformed |
| Unknown cipher (Twofish) | UnsupportedCipher |
|
| Inner stream other than ChaCha20 (and Salsa20 in 3.1) | UnsupportedStream |
No error carries any part of the password, the key or a value. A panic on any input is a bug.
Writing
KDBX 4.0: ChaCha20, gzip, Argon2id (M = 64 MiB, I = 20, P = 1 by default: the vault's own defaults),
ChaCha20 inner stream with a fresh 64-byte key, a fresh master seed, IV and salt from the OS CSPRNG. Every
Password and every string Custos keeps secret is written protected. Blocks are 1 MiB. AES-256 and AES-KDF
can be chosen (WriteOptions) so the reader is tested against both.
The export is protected by its own password only (and optionally a key file). It is meant for moving to KeePassXC or for an offline backup; the app says so before it writes one.
Mapping: KeePass entry ↔ Custos item
Entry UUID ↔ item id (16 bytes ↔ 32 hex digits), so importing the same file again finds the items it
already imported.
| KDBX entry | Custos item |
|---|---|
| group path below the root group | folder (list of group names; the root itself is not included) |
Title |
title |
UserName |
username (web-login, local-app, and any type without its own), ssid (wifi), cardholder (card) |
Password (protected) |
password (web-login, local-app, wifi, and others), secret (api-credential), number (card) |
URL, then KP2A_URL, KP2A_URL_1, … (exactly those names) |
url, repeated in that order |
Notes |
notes |
otp (KeePassXC) |
totp |
TOTP Seed + TOTP Settings (KeePass 2 / older KeePassXC) |
totp, as an otpauth:// URI when the settings are not 30 s / 6 digits |
custos-type |
the item type (written by Custos; other files are guessed: no user name, password or URL → secure-note, otherwise web-login) |
| any other string | a field with the same name; secret when protected. A Custos field whose name KeePass reserves (Title, custos-type, KP2A_URL, …) or that is already taken is written as name (2), name (3), … |
Tags (; or , separated) |
tags |
Times/CreationTime, LastModificationTime |
created, modified (seconds; milliseconds are dropped on export) |
Times/Expires + ExpiryTime |
field expires: YYYY-MM-DD at midnight UTC, else YYYY-MM-DDTHH:MM:SSZ |
Binary (attachments) |
attachments (name and bytes) |
AutoType/DefaultSequence |
autotype-sequence |
each AutoType/Association/Window |
autotype-match, repeated |
History |
history: for each snapshot, each secret field (the password, every protected string) whose value differs in the next version: {field, value, changed: the snapshot's modification time}, newest 20 kept. On export, one snapshot per distinct change time, with the values just before that change |
On export, an empty field from the item's template is left out; every other field is written, even when
empty. custos-type is written only when the type differs from what the guess above would give, so a plain
KeePass entry comes back without it. The root group is named after the export (default "Custos"); folders
become groups under it. Imported items come back folder by folder: extra fields after the template's ones.
What does not survive
Said plainly, so nobody expects it:
- Icons, colours, custom icons,
OverrideURL, an entry's or group'sCustomData, group notes and settings. - History of non-secret fields: KeePass snapshots hold every field; Custos keeps old values of secret fields only, at most 20 per item (KeePassXC keeps 10 snapshots by default).
- Auto-type: a per-window keystroke sequence and the "auto-type disabled" switch.
- The recycle bin: entries in it are not imported. Deleted-object records are not kept.
- Database settings (
Meta): name, description, history limits, KDF and cipher. The export uses its own. - Sub-second times.
Custos items that have nothing in KeePass go out as custom strings (custos-type, the template fields such as
key-id or cvv) and come back the same. Version 2 vault factors and the Domi identity are never exported.
Tests (crates/custos-kdbx/tests/)
format.rs: write → read is identical for every cipher and KDF; wrong password, no key, each key-file kind (four spellings of one key open each other's files); a changed byte anywhere (header and hash:Corrupted, header HMAC:WrongKey, blocks:Corrupted); truncation at every length; out-of-limit KDF parameters refused in well under a second; DOCTYPE (billion laughs, XXE), deep nesting, broken XML, a gzip bomb, a Salsa20 inner stream in KDBX 4, Twofish and unknown versions refused; no secret in anyDebugoutput.keepassxc.rs: two files KeePassXC 2.7.12 wrote fromtools/kdbx/sample.xml(base64 test vectors intests/data/, passwordcustos-test): KDBX 3.1 fromimport, KDBX 4.0 frommergeinto a Custos file. Both open with the expected folders, fields, protected strings, extra URLs, both TOTP forms, expiry, auto-type, history and attachment; the 3.1 file's recycle bin is skipped; a wrong password isWrongKey.mapping.rs: every item type throughfrom_items→ write → read →to_itemsis unchanged, field by field; what the export looks like to KeePassXC; a KeePass-style database maps as the table says; reserved names are renamed, ids survive.tools/kdbx/interop.sh(WSL + Nix withkeepassxc-cli, not part ofcargo test): KeePassXC makes a 3.1 and a 4.0 file fromsample.xml; Custos reads each, maps it to items and back, and writes KDBX 4; KeePassXC opens that,compare.pychecks both XML exports entry by entry (leaving out only what "What does not survive" lists),attachment-exportcompares the attachment bytes, andshow --totpgives the 8-digit code.