keepass-ng 0.10.5

KeePass .kdbx database file parser with ehancements
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Error types that this crate can return

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("std::io::Error {0}")]
    Io(#[from] std::io::Error),

    #[error("DatabaseError::RecycleBinDisabled")]
    RecycleBinDisabled,

    #[error("DatabaseError::RecycleBinAlreadyExists")]
    RecycleBinAlreadyExists,

    #[error("DatabaseOpenError {0}")]
    DatabaseOpenError(#[from] DatabaseOpenError),

    #[error("DatabaseSaveError {0}")]
    DatabaseSaveError(#[from] DatabaseSaveError),

    #[cfg(feature = "totp")]
    #[error("DbOtpError {0}")]
    DbOtpError(#[from] crate::db::otp::TOTPError),

    #[error("OuterCipherConfigError {0}")]
    OuterCipherConfigError(#[from] OuterCipherConfigError),

    #[error("InnerCipherConfigError {0}")]
    InnerCipherConfigError(#[from] InnerCipherConfigError),

    #[error("CompressionConfigError {0}")]
    CompressionConfigError(#[from] CompressionConfigError),

    #[error("KdfConfigError {0}")]
    KdfConfigError(#[from] KdfConfigError),

    #[error("CryptographyError {0}")]
    CryptographyError(#[from] CryptographyError),

    #[error("BlockStreamError {0}")]
    BlockStreamError(#[from] BlockStreamError),

    #[error("VariantDictionaryError {0}")]
    VariantDictionaryError(#[from] VariantDictionaryError),

    #[error("XmlParseError {0}")]
    XmlParseError(#[from] XmlParseError),

    #[error("ParseColorError {0}")]
    ParseColorError(#[from] ParseColorError),

    #[error("ParseIconIdError {}", icon_id)]
    ParseIconIdError { icon_id: usize },

    #[cfg(feature = "merge")]
    #[error("MergeError {0}")]
    MergeError(#[from] crate::db::merge::MergeError),

    #[error("String error: {0}")]
    String(String),
}

impl From<&str> for Error {
    fn from(s: &str) -> Self {
        Error::String(s.to_string())
    }
}

impl From<String> for Error {
    fn from(s: String) -> Self {
        Error::String(s)
    }
}

impl From<&String> for Error {
    fn from(s: &String) -> Self {
        Error::String(s.clone())
    }
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;

/// Errors upon reading a Database
#[derive(Debug, thiserror::Error)]
pub enum DatabaseOpenError {
    /// An I/O error has occurred while reading the database
    #[error(transparent)]
    Io(#[from] std::io::Error),

    /// An error with the database's key has occurred
    #[error(transparent)]
    Key(#[from] DatabaseKeyError),

    /// The database is corrupted
    #[error(transparent)]
    DatabaseIntegrity(#[from] DatabaseIntegrityError),

    /// The database version cannot be read by this library
    #[error("Opening this database version is not supported")]
    UnsupportedVersion,
}

/// Errors stemming from corrupted databases
#[derive(Debug, thiserror::Error)]
pub enum DatabaseIntegrityError {
    /// The database does not have a valid KDBX identifier
    #[error("Invalid KDBX identifier")]
    InvalidKDBXIdentifier,

    /// The version of the KDBX file is invalid
    #[error("Invalid KDBX version: {}.{}.{}", version, file_major_version, file_minor_version)]
    InvalidKDBXVersion {
        version: u32,
        file_major_version: u32,
        file_minor_version: u32,
    },

    /// The fixed header has an invalid size
    #[error("Invalid header size: {}", size)]
    InvalidFixedHeader { size: usize },

    #[error("Invalid field length for type {}: {} (expected {})", field_type, field_size, expected_field_size)]
    InvalidKDBFieldLength {
        field_type: u16,
        field_size: u32,
        expected_field_size: u32,
    },

    #[error("Missing group level")]
    MissingKDBGroupLevel,

    #[error("Invalid KDBX header field ID: {}", field_id)]
    InvalidKDBXHeaderFieldID { field_id: u8 },

    #[error("Invalid group level {} (current level {})", group_level, current_level)]
    InvalidKDBGroupLevel { group_level: u16, current_level: u16 },

    #[error("Missing group ID")]
    MissingKDBGroupId,

    #[error("Invalid group ID {}", group_id)]
    InvalidKDBGroupId { group_id: u32 },

    #[error("Invalid group field type: {}", field_type)]
    InvalidKDBGroupFieldType { field_type: u16 },

    #[error("Invalid entry field type: {}", field_type)]
    InvalidKDBEntryFieldType { field_type: u16 },

    #[error("Incomplete group")]
    IncompleteKDBGroup,

    #[error("Incomplete entry")]
    IncompleteKDBEntry,

    #[error("Invalid fixed cipher ID: {}", cid)]
    InvalidFixedCipherID { cid: u32 },

    #[error("Header hash masmatch")]
    HeaderHashMismatch,

    #[error("Invalid outer header entry: {}", entry_type)]
    InvalidOuterHeaderEntry { entry_type: u8 },

    #[error("Incomplete outer header: Missing {}", missing_field)]
    IncompleteOuterHeader { missing_field: String },

    #[error("Invalid inner header entry: {}", entry_type)]
    InvalidInnerHeaderEntry { entry_type: u8 },

    #[error("Incomplete outer header: Missing {}", missing_field)]
    IncompleteInnerHeader { missing_field: String },

    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    #[error(transparent)]
    Xml(#[from] XmlParseError),

    #[error(transparent)]
    OuterCipher(#[from] OuterCipherConfigError),

    #[error(transparent)]
    InnerCipher(#[from] InnerCipherConfigError),

    #[error(transparent)]
    Compression(#[from] CompressionConfigError),

    #[error(transparent)]
    BlockStream(#[from] BlockStreamError),

    #[error(transparent)]
    VariantDictionary(#[from] VariantDictionaryError),

    #[error(transparent)]
    KdfSettings(#[from] KdfConfigError),

    #[error(transparent)]
    Io(#[from] std::io::Error),
}

/// Errors occurring when saving a Database
#[derive(Debug, thiserror::Error)]
pub enum DatabaseSaveError {
    /// The current database version cannot be saved by this library
    #[error("Saving this database version is not supported")]
    UnsupportedVersion,

    /// Error while writing out the inner XML database
    #[error("Error while generating XML")]
    Xml(#[from] xml::writer::Error),

    /// General I/O issues while writing the database
    #[error(transparent)]
    Io(#[from] std::io::Error),

    /// An error with the key occurred while writing the database
    #[error(transparent)]
    Key(#[from] DatabaseKeyError),

    /// A cryptography error occurred while writing the database
    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    /// An error getting randomness for keys occurred
    #[error(transparent)]
    Random(#[from] getrandom::Error),
}

/// Errors related to the database key
#[derive(Debug, thiserror::Error)]
pub enum DatabaseKeyError {
    /// The key specified was incorrect, e.g. because of a wrong password
    #[error("Incorrect key")]
    IncorrectKey,

    /// An error occurred in an underlying cryptographic operation while computing the key
    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    /// An I/O error occurred while loading the keyfile
    #[error(transparent)]
    Io(#[from] std::io::Error),

    /// An XML error occurred while loading the keyfile
    #[error(transparent)]
    Xml(#[from] xml::reader::Error),

    /// The keyfile is invalid and did not contain a key
    #[error("Could not obtain a key from the keyfile")]
    InvalidKeyFile,

    /// Could not get challenge response key.
    #[cfg(feature = "challenge_response")]
    #[error("Error with the challenge-response key: {0}")]
    ChallengeResponseKeyError(String),
}

/// Errors with the configuration of the outer encryption
#[derive(Debug, thiserror::Error)]
pub enum OuterCipherConfigError {
    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    #[error("Invalid outer cipher ID: {:?}", cid)]
    InvalidOuterCipherID { cid: Vec<u8> },
}

/// Errors with the configuration of the inner encryption
#[derive(Debug, thiserror::Error)]
pub enum InnerCipherConfigError {
    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    #[error("Invalid inner cipher ID: {}", cid)]
    InvalidInnerCipherID { cid: u32 },
}

/// Errors with the configuration of the compression algorithm
#[derive(Debug, thiserror::Error)]
pub enum CompressionConfigError {
    /// The identifier for the compression algorithm specified in the database is invalid
    #[error("Invalid compression algorithm: {}", cid)]
    InvalidCompressionSuite { cid: u32 },
}

/// Errors with the configuration of the Key Derivation Function
#[derive(Debug, thiserror::Error)]
pub enum KdfConfigError {
    #[error("Invalid KDF version: {}", version)]
    InvalidKDFVersion { version: u32 },

    #[error("Invalid KDF UUID: {:?}", uuid)]
    InvalidKDFUUID { uuid: Vec<u8> },

    #[error(transparent)]
    VariantDictionary(#[from] VariantDictionaryError),
}

/// Errors while performing cryptographic operations
#[derive(Debug, thiserror::Error)]
pub enum CryptographyError {
    #[error(transparent)]
    InvalidLength(#[from] cipher::InvalidLength),

    #[error(transparent)]
    Unpadding(#[from] cipher::block_padding::UnpadError),

    #[error(transparent)]
    Padding(#[from] cipher::inout::PadError),

    #[error(transparent)]
    Argon2(#[from] argon2::Error),
}

/// Errors reading from the HMAC block stream
#[derive(Debug, thiserror::Error)]
pub enum BlockStreamError {
    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    #[error("Block hash mismatch for block {}", block_index)]
    BlockHashMismatch { block_index: u64 },
}

/// Errors while parsing a `VariantDictionary`
#[derive(Debug, thiserror::Error)]
pub enum VariantDictionaryError {
    #[error("Invalid variant dictionary version: {}", version)]
    InvalidVersion { version: u16 },

    #[error("Invalid value type: {}", value_type)]
    InvalidValueType { value_type: u8 },

    #[error("Missing key: {}", key)]
    MissingKey { key: String },

    #[error("Mistyped value: {}", key)]
    Mistyped { key: String },

    #[error("VariantDictionary did not end with null byte, when it should")]
    NotTerminated,
}

/// Errors while parsing the XML document inside of a `KeePass` database
#[derive(Debug, thiserror::Error)]
pub enum XmlParseError {
    #[error(transparent)]
    Xml(#[from] xml::reader::Error),

    #[error(transparent)]
    Base64(#[from] base64::DecodeError),

    #[error(transparent)]
    TimestampFormat(#[from] chrono::ParseError),

    #[error(transparent)]
    IntFormat(#[from] std::num::ParseIntError),

    #[error(transparent)]
    BoolFormat(#[from] std::str::ParseBoolError),

    #[error(transparent)]
    Uuid(#[from] uuid::Error),

    #[error(transparent)]
    Color(#[from] ParseColorError),

    #[error(transparent)]
    Cryptography(#[from] CryptographyError),

    #[error("Decompression error: {}", _0)]
    Compression(#[source] std::io::Error),

    /// An unexpected XML event occurred, such as opening an unexpected tag, or an error in the
    /// underlying XML reader
    #[error("Bad XML event: expected {}, got {:?}", expected, event)]
    BadEvent {
        expected: &'static str,
        event: crate::xml_db::parse::SimpleXmlEvent,
    },

    /// The stream of XML events ended when more events were expected
    #[error("Unexpected end of XML document")]
    Eof,
}

/// Error parsing a color code
#[derive(Debug, thiserror::Error)]
#[error("Cannot parse color: '{}'", _0)]
pub struct ParseColorError(pub String);

// move error type conversions to a module and exclude them from coverage counting.
mod conversions {
    use super::{
        BlockStreamError, CompressionConfigError, CryptographyError, DatabaseIntegrityError, DatabaseOpenError, InnerCipherConfigError,
        KdfConfigError, OuterCipherConfigError, VariantDictionaryError, XmlParseError,
    };

    impl From<CryptographyError> for DatabaseOpenError {
        fn from(e: CryptographyError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<BlockStreamError> for DatabaseOpenError {
        fn from(e: BlockStreamError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<XmlParseError> for DatabaseOpenError {
        fn from(e: XmlParseError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<InnerCipherConfigError> for DatabaseOpenError {
        fn from(e: InnerCipherConfigError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<OuterCipherConfigError> for DatabaseOpenError {
        fn from(e: OuterCipherConfigError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<KdfConfigError> for DatabaseOpenError {
        fn from(e: KdfConfigError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<VariantDictionaryError> for DatabaseOpenError {
        fn from(e: VariantDictionaryError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }

    impl From<CompressionConfigError> for DatabaseOpenError {
        fn from(e: CompressionConfigError) -> Self {
            DatabaseIntegrityError::from(e).into()
        }
    }
}