libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
kdbx_xml.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
25#pragma once
26#include <array>
27#include <cstdint>
28#include <istream>
29#include <memory>
30#include <ostream>
31#include <string>
32#include <unordered_map>
33
36#include "libkeepass/export.hh"
37#include "libkeepass/secure.hh"
38
39namespace pugi {
40class xml_document;
41class xml_node;
42} // namespace pugi
43
44namespace keepass {
45
46class Binary;
47class Entry;
48class Group;
49class Icon;
50class Metadata;
51class RandomObfuscator;
52
67public:
68 using BinaryPool = std::unordered_map<std::string, std::shared_ptr<Binary>>;
69
70 using IconPool = std::unordered_map<std::string, std::weak_ptr<Icon>>;
71
72 using GroupPool = std::unordered_map<std::string, std::shared_ptr<Group>>;
73
75 void set_kdbx4(bool kdbx4) { kdbx4_ = kdbx4; }
76 bool kdbx4() const { return kdbx4_; }
77
79 void set_kdbx41(bool kdbx41) { kdbx41_ = kdbx41; }
80 bool kdbx41() const { return kdbx41_; }
81
83 void set_resource_limits(const detail::ResourceLimits& limits) { resource_limits_ = limits; }
84
86 const detail::ResourceLimits& resource_limits() const { return resource_limits_; }
87
89 void Reset();
90
93 BinaryPool& binary_pool() { return binary_pool_; }
94
97 std::array<uint8_t, 32>& header_hash() { return header_hash_; }
98 const std::array<uint8_t, 32>& header_hash() const { return header_hash_; }
99
101
106 void Parse(std::istream& src, RandomObfuscator& obfuscator, Database& db);
107
109
114 void Write(std::ostream& dst, RandomObfuscator& obfuscator, const Database& db);
115
116#ifdef DEBUG
118 void PrintXml(pugi::xml_document& doc);
119#endif
120
121private:
122 BinaryPool binary_pool_;
123 IconPool icon_pool_;
124 GroupPool group_pool_;
125 std::array<uint8_t, 32> header_hash_ = {{0}};
126
127 detail::ResourceLimits resource_limits_ = {};
128
130 uint64_t groups_seen_ = 0;
131
133 uint64_t entries_seen_ = 0;
134
136 uint64_t history_items_seen_ = 0;
137
138 bool kdbx4_ = false;
139 bool kdbx41_ = false;
140
142 std::shared_ptr<Group> GetGroup(const std::string& uuid_str);
143
145 std::time_t ParseDateTime(const char* text) const;
146
148 std::string WriteDateTime(std::time_t time) const;
149
151 static int64_t NeverSeconds();
152
154 protect<secure_string> ParseProtectedString(const pugi::xml_node& node, const char* name,
155 RandomObfuscator& obfuscator) const;
156
158 static void WriteProtectedString(pugi::xml_node& node, const protect<secure_string>& str,
159 RandomObfuscator& obfuscator);
160
162 std::shared_ptr<Metadata> ParseMeta(const pugi::xml_node& meta_node,
163 RandomObfuscator& obfuscator);
164
166 void WriteMeta(pugi::xml_node& meta_node, RandomObfuscator& obfuscator,
167 const std::shared_ptr<Metadata>& meta);
168
170
176 std::shared_ptr<Entry> ParseEntry(const pugi::xml_node& entry_node,
177 std::array<uint8_t, 16>& entry_uuid,
178 RandomObfuscator& obfuscator);
179
181 void WriteEntry(pugi::xml_node& entry_node, RandomObfuscator& obfuscator,
182 const std::shared_ptr<Entry>& entry);
183
185
191 std::shared_ptr<Group> ParseGroup(const pugi::xml_node& group_node, RandomObfuscator& obfuscator,
192 uint32_t depth = 0);
193
195 void WriteGroup(pugi::xml_node& group_node, RandomObfuscator& obfuscator,
196 const std::shared_ptr<Group>& group);
197};
198
199} // namespace keepass
Represents a KeePass (KDB/KDBX) database.
Definition database.hh:74
Parses and serializes the KDBX XML body.
Definition kdbx_xml.hh:66
void set_resource_limits(const detail::ResourceLimits &limits)
Sets the resource budgets enforced while parsing the XML body.
Definition kdbx_xml.hh:83
void Parse(std::istream &src, RandomObfuscator &obfuscator, Database &db)
Parses the full KDBX XML body from a stream.
void set_kdbx41(bool kdbx41)
True while writing KDBX 4.1 (0x00040001).
Definition kdbx_xml.hh:79
BinaryPool & binary_pool()
Definition kdbx_xml.hh:93
const detail::ResourceLimits & resource_limits() const
The resource budgets currently enforced while parsing.
Definition kdbx_xml.hh:86
std::array< uint8_t, 32 > & header_hash()
Definition kdbx_xml.hh:97
void Write(std::ostream &dst, RandomObfuscator &obfuscator, const Database &db)
Writes the full KDBX XML body to a stream.
void set_kdbx4(bool kdbx4)
True while reading or writing a KDBX 4 database.
Definition kdbx_xml.hh:75
void Reset()
Resets all internal pools and state for a new import/export operation.
Definition random.hh:40
In-memory representation of a KeePass database.
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
Resource budgets enforced while importing (and parsing) databases.
Secure memory handling: zeroization, locked allocations and sensitive value containers.
Configurable resource budgets for database import and XML parsing.
Definition limits.hh:45