122 lines
3.6 KiB
Rust
122 lines
3.6 KiB
Rust
//! KeePass KDBX import (3.1 and 4) and export (4) (docs/KDBX.md).
|
|
//!
|
|
//! Two layers: [`read`] / [`write`] turn bytes and a [`Key`] into a [`Database`] and back;
|
|
//! [`to_items`] / [`from_items`] map KeePass entries to Custos items and back.
|
|
|
|
mod crypto;
|
|
mod format;
|
|
mod map;
|
|
mod time;
|
|
mod xml;
|
|
|
|
pub use custos_vault::{SecretBytes, SecretString};
|
|
#[doc(hidden)]
|
|
pub use format::testing;
|
|
pub use format::{Cipher, Kdf, WriteOptions, read, write};
|
|
pub use map::{from_items, to_items};
|
|
pub use time::{format_iso, parse_iso};
|
|
|
|
/// A whole KDBX database, as far as Custos keeps it (docs/KDBX.md, "What does not survive").
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct Database {
|
|
/// `Meta/DatabaseName`.
|
|
pub name: String,
|
|
pub root: Group,
|
|
/// The recycle bin group, when the file has one switched on. Its entries are not imported.
|
|
pub recycle_bin: Option<[u8; 16]>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct Group {
|
|
pub uuid: [u8; 16],
|
|
pub name: String,
|
|
pub groups: Vec<Group>,
|
|
pub entries: Vec<Entry>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct Entry {
|
|
pub uuid: [u8; 16],
|
|
/// In file order. Keys are unique within an entry.
|
|
pub strings: Vec<StringField>,
|
|
pub attachments: Vec<custos_vault::Attachment>,
|
|
pub tags: Vec<String>,
|
|
pub times: Times,
|
|
pub autotype: AutoType,
|
|
/// Older versions of this entry, oldest first (KeePass order). Their own history is empty.
|
|
pub history: Vec<Entry>,
|
|
}
|
|
|
|
impl Entry {
|
|
/// The value of the string called `key`.
|
|
pub fn get(&self, key: &str) -> Option<&SecretString> {
|
|
self.strings.iter().find(|s| s.key == key).map(|s| &s.value)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct StringField {
|
|
pub key: String,
|
|
pub value: SecretString,
|
|
/// Written XORed with the inner stream (KeePass's "protect in memory").
|
|
pub protected: bool,
|
|
}
|
|
|
|
/// Unix seconds (UTC).
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
pub struct Times {
|
|
pub created: i64,
|
|
pub modified: i64,
|
|
/// `ExpiryTime` when `Expires` is true.
|
|
pub expires: Option<i64>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct AutoType {
|
|
pub default_sequence: String,
|
|
/// The `Window` of each association, in order.
|
|
pub windows: Vec<String>,
|
|
}
|
|
|
|
/// What opens a KDBX file: a password, a key file, or both.
|
|
#[derive(Clone, Copy, Default)]
|
|
pub struct Key<'a> {
|
|
/// An empty password counts as no password (as in KeePassXC).
|
|
pub password: Option<&'a SecretString>,
|
|
/// The key file's whole content.
|
|
pub key_file: Option<&'a [u8]>,
|
|
}
|
|
|
|
impl std::fmt::Debug for Key<'_> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_struct("Key")
|
|
.field("password", &self.password.is_some())
|
|
.field("key_file", &self.key_file.is_some())
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("this is not a KeePass database")]
|
|
NotKdbx,
|
|
#[error("unsupported KDBX version {0}.{1}")]
|
|
UnsupportedVersion(u16, u16),
|
|
#[error("a password or a key file is needed")]
|
|
NoKey,
|
|
#[error("wrong password or key file")]
|
|
WrongKey,
|
|
#[error("the file is damaged")]
|
|
Corrupted,
|
|
#[error("unsupported cipher: only AES-256 and ChaCha20 are supported")]
|
|
UnsupportedCipher,
|
|
#[error("unsupported key derivation settings")]
|
|
UnsupportedKdf,
|
|
#[error("unsupported inner stream: only ChaCha20 and Salsa20 are supported")]
|
|
UnsupportedStream,
|
|
#[error("the file is too large")]
|
|
TooLarge,
|
|
#[error("malformed KDBX file: {0}")]
|
|
Malformed(&'static str),
|
|
}
|