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
449
450
451
452
use base64::{Engine as _, engine::general_purpose as base64_engine};
use chrono::NaiveDateTime;
use uuid::Uuid;

use crate::{
    compression::{Compression, GZipCompression},
    db::{
        Color,
        meta::{BinaryAttachment, BinaryAttachments, CustomIcons, Icon, MemoryProtection, Meta},
    },
    xml_db::parse::{CustomData, FromXml, IgnoreSubfield, SimpleTag, SimpleXmlEvent, XmlParseError, bad_event},
};

impl FromXml for Meta {
    type Parses = Self;

    #[allow(clippy::too_many_lines)]
    fn from_xml<I: Iterator<Item = crate::xml_db::parse::SimpleXmlEvent>>(
        iterator: &mut std::iter::Peekable<I>,
        inner_cipher: &mut dyn crate::crypt::ciphers::Cipher,
    ) -> Result<Self::Parses, crate::xml_db::parse::XmlParseError> {
        let open_tag = iterator.next().ok_or(XmlParseError::Eof)?;
        if !matches!(open_tag, SimpleXmlEvent::Start(ref tag, _) if tag == "Meta") {
            return Err(bad_event("Open Meta tag", open_tag));
        }

        let mut out = Meta::new();

        while let Some(event) = iterator.peek() {
            match event {
                SimpleXmlEvent::Start(name, _) => match &name[..] {
                    "Generator" => {
                        out.generator = SimpleTag::<Option<String>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "DatabaseName" => {
                        out.database_name = SimpleTag::<Option<String>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "DatabaseNameChanged" => {
                        out.database_name_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "DatabaseDescription" => {
                        out.database_description = SimpleTag::<Option<String>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "DatabaseDescriptionChanged" => {
                        out.database_description_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "DefaultUserName" => {
                        out.default_username = SimpleTag::<Option<String>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "DefaultUserNameChanged" => {
                        out.default_username_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "MaintenanceHistoryDays" => {
                        out.maintenance_history_days = SimpleTag::<Option<usize>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "Color" => {
                        out.color = SimpleTag::<Option<Color>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "MasterKeyChanged" => {
                        out.master_key_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "MasterKeyChangeRec" => {
                        out.master_key_change_rec = SimpleTag::<Option<isize>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "MasterKeyChangeForce" => {
                        out.master_key_change_force = SimpleTag::<Option<isize>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "MemoryProtection" => {
                        out.memory_protection = Some(MemoryProtection::from_xml(iterator, inner_cipher)?);
                    }
                    "CustomIcons" => {
                        out.custom_icons = CustomIcons::from_xml(iterator, inner_cipher)?;
                    }
                    "RecycleBinEnabled" => {
                        out.recyclebin_enabled = SimpleTag::<Option<bool>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "RecycleBinUUID" => {
                        out.recyclebin_uuid = SimpleTag::<Option<Uuid>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "RecycleBinChanged" => {
                        out.recyclebin_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "EntryTemplatesGroup" => {
                        out.entry_templates_group = SimpleTag::<Option<Uuid>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "EntryTemplatesGroupChanged" => {
                        out.entry_templates_group_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "LastSelectedGroup" => {
                        out.last_selected_group = SimpleTag::<Option<Uuid>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "LastTopVisibleGroup" => {
                        out.last_top_visible_group = SimpleTag::<Option<Uuid>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "HistoryMaxItems" => {
                        out.history_max_items = SimpleTag::<Option<usize>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "HistoryMaxSize" => {
                        out.history_max_size = SimpleTag::<Option<usize>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "SettingsChanged" => {
                        out.settings_changed = SimpleTag::<Option<NaiveDateTime>>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "Binaries" => {
                        out.binaries = BinaryAttachments::from_xml(iterator, inner_cipher)?;
                        // TODO figure out where this is needed. Is it only in KDBX3? How to
                        // migrate to KDBX4?
                    }
                    "CustomData" => {
                        out.custom_data = CustomData::from_xml(iterator, inner_cipher)?;
                    }
                    _ => IgnoreSubfield::from_xml(iterator, inner_cipher)?,
                },
                SimpleXmlEvent::End(name) if name == "Meta" => break,
                _ => return Err(bad_event("start tag or close Meta", event.clone())),
            }
        }

        // no need to check for the correct closing tag - checked by XmlReader
        let _close_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        Ok(out)
    }
}

impl FromXml for MemoryProtection {
    type Parses = Self;

    fn from_xml<I: Iterator<Item = SimpleXmlEvent>>(
        iterator: &mut std::iter::Peekable<I>,
        inner_cipher: &mut dyn crate::crypt::ciphers::Cipher,
    ) -> Result<Self::Parses, XmlParseError> {
        let open_tag = iterator.next().ok_or(XmlParseError::Eof)?;
        if !matches!(open_tag, SimpleXmlEvent::Start(ref tag, _) if tag == "MemoryProtection") {
            return Err(bad_event("Open MemoryProtection tag", open_tag));
        }

        let mut out = MemoryProtection::default();

        while let Some(event) = iterator.peek() {
            match event {
                SimpleXmlEvent::Start(name, _) => match &name[..] {
                    "ProtectTitle" => {
                        out.protect_title = SimpleTag::<bool>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "ProtectUserName" => {
                        out.protect_username = SimpleTag::<bool>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "ProtectPassword" => {
                        out.protect_password = SimpleTag::<bool>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "ProtectURL" => {
                        out.protect_url = SimpleTag::<bool>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "ProtectNotes" => {
                        out.protect_notes = SimpleTag::<bool>::from_xml(iterator, inner_cipher)?.value;
                    }
                    _ => IgnoreSubfield::from_xml(iterator, inner_cipher)?,
                },
                SimpleXmlEvent::End(name) if name == "MemoryProtection" => break,
                _ => return Err(bad_event("start tag or close MemoryProtection", event.clone())),
            }
        }

        // no need to check for the correct closing tag - checked by XmlReader
        let _close_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        Ok(out)
    }
}

impl FromXml for BinaryAttachments {
    type Parses = Self;

    fn from_xml<I: Iterator<Item = SimpleXmlEvent>>(
        iterator: &mut std::iter::Peekable<I>,
        inner_cipher: &mut dyn crate::crypt::ciphers::Cipher,
    ) -> Result<Self::Parses, XmlParseError> {
        let open_tag = iterator.next().ok_or(XmlParseError::Eof)?;
        if !matches!(open_tag, SimpleXmlEvent::Start(ref tag, _) if tag == "Binaries") {
            return Err(bad_event("Open Binaries tag", open_tag));
        }

        let mut out = BinaryAttachments::default();

        while let Some(event) = iterator.peek() {
            match event {
                SimpleXmlEvent::Start(name, _) => match &name[..] {
                    "Binary" => {
                        let binary = BinaryAttachment::from_xml(iterator, inner_cipher)?;
                        out.binaries.push(binary);
                    }
                    _ => IgnoreSubfield::from_xml(iterator, inner_cipher)?,
                },
                SimpleXmlEvent::End(name) if name == "Binaries" => break,
                _ => return Err(bad_event("start tag or close Binaries", event.clone())),
            }
        }

        // no need to check for the correct closing tag - checked by XmlReader
        let _close_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        Ok(out)
    }
}

impl FromXml for BinaryAttachment {
    type Parses = Self;

    fn from_xml<I: Iterator<Item = SimpleXmlEvent>>(
        iterator: &mut std::iter::Peekable<I>,
        inner_cipher: &mut dyn crate::crypt::ciphers::Cipher,
    ) -> Result<Self::Parses, XmlParseError> {
        let open_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        let mut out = BinaryAttachment::default();
        let (identifier, compressed) = if let SimpleXmlEvent::Start(ref name, ref attributes) = open_tag {
            if name != "Binary" {
                return Err(bad_event("Open Binary tag", open_tag));
            }

            let identifier = attributes.get("ID").map(std::string::ToString::to_string);

            let compressed = attributes.get("Compressed").map_or(Ok(false), |v| v.to_lowercase().parse())?;

            (identifier, compressed)
        } else {
            return Err(bad_event("Open Binary tag", open_tag));
        };

        let data = String::from_xml(iterator, inner_cipher)?;
        let buf = base64_engine::STANDARD.decode(data)?;

        out.identifier = identifier;
        out.compressed = compressed;
        out.content = if compressed {
            Compression::decompress(&GZipCompression, &buf).map_err(XmlParseError::Compression)?
        } else {
            buf
        };

        // no need to check for the correct closing tag - checked by XmlReader
        let _close_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        Ok(out)
    }
}

impl FromXml for CustomIcons {
    type Parses = Self;

    fn from_xml<I: Iterator<Item = SimpleXmlEvent>>(
        iterator: &mut std::iter::Peekable<I>,
        inner_cipher: &mut dyn crate::crypt::ciphers::Cipher,
    ) -> Result<Self::Parses, XmlParseError> {
        let open_tag = iterator.next().ok_or(XmlParseError::Eof)?;
        if !matches!(open_tag, SimpleXmlEvent::Start(ref tag, _) if tag == "CustomIcons") {
            return Err(bad_event("Open CustomIcons tag", open_tag));
        }

        let mut out = CustomIcons::default();

        while let Some(event) = iterator.peek() {
            match event {
                SimpleXmlEvent::Start(name, _) => match &name[..] {
                    "Icon" => {
                        let icon = Icon::from_xml(iterator, inner_cipher)?;
                        out.icons.push(icon);
                    }
                    _ => IgnoreSubfield::from_xml(iterator, inner_cipher)?,
                },
                SimpleXmlEvent::End(name) if name == "CustomIcons" => break,
                _ => return Err(bad_event("start tag or close CustomIcons", event.clone())),
            }
        }

        // no need to check for the correct closing tag - checked by XmlReader
        let _close_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        Ok(out)
    }
}

impl FromXml for Icon {
    type Parses = Self;

    fn from_xml<I: Iterator<Item = SimpleXmlEvent>>(
        iterator: &mut std::iter::Peekable<I>,
        inner_cipher: &mut dyn crate::crypt::ciphers::Cipher,
    ) -> Result<Self::Parses, XmlParseError> {
        let open_tag = iterator.next().ok_or(XmlParseError::Eof)?;
        if !matches!(open_tag, SimpleXmlEvent::Start(ref tag, _) if tag == "Icon") {
            return Err(bad_event("Open Icon tag", open_tag));
        }

        let mut out = Icon::default();

        while let Some(event) = iterator.peek() {
            match event {
                SimpleXmlEvent::Start(name, _) => match &name[..] {
                    "UUID" => {
                        out.uuid = SimpleTag::<Uuid>::from_xml(iterator, inner_cipher)?.value;
                    }
                    "Data" => {
                        let data = SimpleTag::<String>::from_xml(iterator, inner_cipher)?.value;
                        let buf = base64_engine::STANDARD.decode(&data)?;
                        out.data = buf;
                    }
                    _ => IgnoreSubfield::from_xml(iterator, inner_cipher)?,
                },
                SimpleXmlEvent::End(name) if name == "Icon" => break,
                _ => return Err(bad_event("start tag or close Icon", event.clone())),
            }
        }

        // no need to check for the correct closing tag - checked by XmlReader
        let _close_tag = iterator.next().ok_or(XmlParseError::Eof)?;

        Ok(out)
    }
}

#[cfg(test)]
mod parse_meta_test {

    use crate::{
        db::meta::{BinaryAttachment, BinaryAttachments, CustomIcons, Icon, MemoryProtection, Meta},
        xml_db::parse::{XmlParseError, parse_test::parse_test_xml},
    };
    use uuid::{Uuid, uuid};

    #[test]
    fn test_meta() -> Result<(), XmlParseError> {
        let _value = parse_test_xml::<Meta>("<Meta></Meta>")?;

        let value = parse_test_xml::<Meta>("<TestTag>SomeData</TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<Meta>("<Meta></TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<Meta>("<Meta>No-Characters-Allowed</Meta>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let _value = parse_test_xml::<Meta>("<Meta><UnkownChildTag/></Meta>")?;

        Ok(())
    }

    #[test]
    fn test_memory_protection() -> Result<(), XmlParseError> {
        let _value = parse_test_xml::<MemoryProtection>("<MemoryProtection></MemoryProtection>")?;

        let value = parse_test_xml::<MemoryProtection>("<TestTag>SomeData</TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<MemoryProtection>("<MemoryProtection></TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<MemoryProtection>("<MemoryProtection>No-Characters-Allowed</MemoryProtection>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let _value = parse_test_xml::<MemoryProtection>("<MemoryProtection><UnkownChildTag/></MemoryProtection>")?;

        Ok(())
    }

    #[test]
    fn test_binary_attachments() -> Result<(), XmlParseError> {
        let _value = parse_test_xml::<BinaryAttachments>("<Binaries></Binaries>")?;

        let value = parse_test_xml::<BinaryAttachments>("<TestTag>SomeData</TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<BinaryAttachments>("<Binaries></TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<BinaryAttachments>("<Binaries>No-Characters-Allowed</Binaries>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let _value = parse_test_xml::<BinaryAttachments>("<Binaries><UnkownChildTag/></Binaries>")?;

        Ok(())
    }

    #[test]
    fn test_binary_attachment() -> Result<(), XmlParseError> {
        let value = parse_test_xml::<BinaryAttachment>("<Binary ID=\"1\">QmluYXJ5IERhdGE=</Binary>")?;
        assert_eq!(value.identifier, Some("1".to_string()));
        assert_eq!(value.content, r"Binary Data".as_bytes());

        let value = parse_test_xml::<BinaryAttachment>("<TestTag>SomeData</TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<BinaryAttachment>("");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<BinaryAttachment>("<Binary></TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<BinaryAttachment>("<Binary></Binary>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<BinaryAttachment>("<Binary><UnkownChildTag/></Binary>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        Ok(())
    }

    #[test]
    fn test_custom_icons() -> Result<(), XmlParseError> {
        let _value = parse_test_xml::<CustomIcons>("<CustomIcons/>")?;
        let _value = parse_test_xml::<CustomIcons>("<CustomIcons></CustomIcons>")?;

        let value = parse_test_xml::<CustomIcons>("<TestTag>SomeData</TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<CustomIcons>("<CustomIcons></TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<CustomIcons>("<CustomIcons>No-Characters-Allowed</CustomIcons>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let _value = parse_test_xml::<CustomIcons>("<CustomIcons><UnkownChildTag/></CustomIcons>")?;

        Ok(())
    }

    #[test]
    fn test_custom_icon() -> Result<(), XmlParseError> {
        let value = parse_test_xml::<Icon>("<Icon></Icon>")?;
        assert_eq!(value.uuid, Uuid::default());
        assert_eq!(value.data.len(), 0);

        let value = parse_test_xml::<Icon>("<Icon><UUID>oaKjpLGywcLR0tPU1dbX2A==</UUID><Data>QmluYXJ5IERhdGE=</Data></Icon>")?;
        assert_eq!(value.uuid, uuid!("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"));
        assert_eq!(value.data, r"Binary Data".as_bytes());

        let value = parse_test_xml::<Icon>("<TestTag>SomeData</TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<Icon>("<Icon></TestTag>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let value = parse_test_xml::<Icon>("<Icon>No-Characters-Allowed</Icon>");
        assert!(matches!(value, Err(XmlParseError::BadEvent { .. })));

        let _value = parse_test_xml::<Icon>("<Icon><UnkownChildTag/></Icon>")?;

        Ok(())
    }
}