forked from open-eid/libdigidocpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLDocument.h
More file actions
482 lines (420 loc) · 15.4 KB
/
XMLDocument.h
File metadata and controls
482 lines (420 loc) · 15.4 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* libdigidocpp
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include "crypto/Digest.h"
#include "util/log.h"
#include <libxml/parser.h>
#include <libxml/xmlschemas.h>
#include <libxml/c14n.h> // needs to be last to workaround old libxml2 errors
#include <xmlsec/xmltree.h>
#include <xmlsec/xmldsig.h>
#include <xmlsec/crypto.h>
#include <openssl/evp.h>
#include <memory>
#include <istream>
#include <ostream>
namespace digidoc {
#define VERSION_CHECK(major, minor, patch) (((major)<<16)|((minor)<<8)|(patch))
template<typename> struct unique_xml;
template<class T>
struct unique_xml<void(T *)>
{
using type = std::unique_ptr<T,void(*)(T *)>;
};
template<typename T>
using unique_xml_t = typename unique_xml<T>::type;
template<class T, typename D>
[[nodiscard]]
constexpr std::unique_ptr<T, D> make_unique_ptr(T *p, D d) noexcept
{
return {p, std::forward<D>(d)};
}
static std::vector<unsigned char> from_base64(std::string_view data)
{
static constexpr std::string_view whitespace {" \n\r\f\t\v"};
std::vector<unsigned char> result(EVP_DECODE_LENGTH(data.size()), 0);
size_t dataPos = 0;
int size = 0;
auto ctx = make_unique_ptr(EVP_ENCODE_CTX_new(), EVP_ENCODE_CTX_free);
EVP_DecodeInit(ctx.get());
for(auto pos = data.find_first_of(whitespace);
!data.empty();
pos = data.find_first_of(whitespace), dataPos += size_t(size))
{
auto sub = data.substr(0, pos);
if(pos == std::string_view::npos)
data = {};
else
data.remove_prefix(pos + 1);
if(EVP_DecodeUpdate(ctx.get(), &result[dataPos], &size, (const unsigned char*)sub.data(), int(sub.size())) == -1)
THROW("Invalid Base64 Binary");
}
if(EVP_DecodeFinal(ctx.get(), &result[dataPos], &size) == 1)
result.resize(dataPos + size_t(size));
else
result.clear();
return result;
}
static std::string to_base64(const std::vector<unsigned char> &data)
{
std::string result(EVP_ENCODE_LENGTH(data.size()), 0);
auto ctx = make_unique_ptr(EVP_ENCODE_CTX_new(), EVP_ENCODE_CTX_free);
EVP_EncodeInit(ctx.get());
int size{};
if(EVP_EncodeUpdate(ctx.get(), (unsigned char*)result.data(), &size, data.data(), int(data.size())) < 1)
{
result.clear();
return result;
}
auto pos = size_t(size);
EVP_EncodeFinal(ctx.get(), (unsigned char*)&result[pos], &size);
result.resize(pos + size_t(size));
return result;
}
template<class T>
struct XMLElem
{
using value_type = T;
using pointer = value_type*;
using sv = std::string_view;
using pcxmlChar = const xmlChar *;
static constexpr sv whitespace {" \n\r\f\t\v"};
template<class C, typename P>
constexpr static auto safe(C c, P p) noexcept
{
return c ? c->*p : nullptr;
}
template<class C>
constexpr static C find(C n, xmlElementType type) noexcept
{
for(; n && n->type != type; n = n->next);
return n;
}
template<class C>
constexpr static C find(C n, sv name, sv ns) noexcept
{
for(; n && (n.name() != name || n.ns() != ns); ++n);
return n;
}
template<class C, typename P>
constexpr static sv to_string_view(C d, P p) noexcept
{
auto str = safe(d, p);
return str ? sv(sv::const_pointer(str)) : sv();
}
template<typename P>
constexpr auto children(P p, xmlElementType type = XML_ELEMENT_NODE) const noexcept
{
return find(safe(d, p), type);
}
constexpr sv name() const noexcept
{
return to_string_view(d, &T::name);
}
constexpr sv ns() const noexcept
{
return to_string_view(safe(d, &T::ns), &xmlNs::href);
}
constexpr operator bool() const noexcept
{
return bool(d);
}
constexpr auto& operator++() noexcept
{
d = d ? find(d->next, d->type) : nullptr;
return *this;
}
constexpr auto operator++(int) noexcept
{
auto c = *this;
d = find(operator++(), c.name(), c.ns()).d;
return c;
}
constexpr operator sv() const noexcept
{
auto *text = children(&value_type::children, XML_TEXT_NODE);
auto result = to_string_view(text, &std::decay_t<decltype(*text)>::content);
result.remove_prefix(std::min<size_t>(result.find_first_not_of(whitespace), result.size()));
return result;
}
pointer d{};
};
struct XMLName
{
std::string_view name;
std::string_view ns;
};
struct XMLNode: public XMLElem<xmlNode>
{
struct iterator: XMLElem<xmlNode>
{
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
constexpr XMLNode operator*() const noexcept { return {d}; }
};
constexpr iterator begin() const noexcept
{
return {children(&value_type::children)};
}
constexpr iterator end() const noexcept
{
return {};
}
xmlNsPtr addNS(sv href, sv prefix = {}) const noexcept
{
return xmlNewNs(d, pcxmlChar(href.data()), prefix.empty() ? nullptr : pcxmlChar(prefix.data()));
}
xmlNsPtr searchNS(sv ns) const noexcept
{
return xmlSearchNsByHref(nullptr, d, ns.empty() ? nullptr : pcxmlChar(ns.data()));
}
constexpr sv property(sv name, sv ns = {}) const noexcept
{
return find(XMLElem<xmlAttr>{children(&value_type::properties, XML_ATTRIBUTE_NODE)}, name, ns);
}
void setProperty(sv name, sv value, sv ns) const noexcept
{
setProperty(name, value, searchNS(ns));
}
void setProperty(sv name, sv value, xmlNsPtr ns = {}) const noexcept
{
xmlSetNsProp(d, ns, pcxmlChar(name.data()), pcxmlChar(value.data()));
}
static iterator erase(iterator pos) noexcept
{
iterator next = pos;
++next;
xmlUnlinkNode(pos.d);
xmlFreeNode(pos.d);
return next;
}
operator std::vector<unsigned char>()
{
return from_base64(operator sv());
}
XMLNode& operator=(sv text) noexcept
{
if(!d)
return *this;
xmlNodeSetContent(d, pcxmlChar(text.data()));
return *this;
}
constexpr XMLNode operator/(sv name) const noexcept
{
return find(*begin(), name, ns());
}
constexpr XMLNode operator/(const XMLName &name) const noexcept
{
return find(*begin(), name.name, name.ns);
}
XMLNode operator+(const XMLName &name) const noexcept
{
return {xmlNewChild(d, searchNS(name.ns), pcxmlChar(name.name.data()), nullptr)};
}
XMLNode operator+(const char *name) const noexcept
{
return operator +({name, ns()});
}
constexpr auto operator+(int n) noexcept
{
XMLNode c{*this};
for(int i = 0; c && i < n; ++i, c++);
return c;
}
XMLNode& operator=(const std::vector<unsigned char> &data)
{
operator=(to_base64(data));
return *this;
}
};
struct XMLDocument: public unique_xml_t<decltype(xmlFreeDoc)>, public XMLNode
{
static constexpr std::string_view C14D_ID_1_0 {"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"};
static constexpr std::string_view C14D_ID_1_0_COM {"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"};
static constexpr std::string_view C14D_ID_1_1 {"http://www.w3.org/2006/12/xml-c14n11"};
static constexpr std::string_view C14D_ID_1_1_COM {"http://www.w3.org/2006/12/xml-c14n11#WithComments"};
static constexpr std::string_view C14D_ID_1_0_EXC {"http://www.w3.org/2001/10/xml-exc-c14n#"};
static constexpr std::string_view C14D_ID_1_0_EXC_COM {"http://www.w3.org/2001/10/xml-exc-c14n#WithComments"};
using XMLNode::operator bool;
XMLDocument(element_type *ptr = {}, const XMLName &n = {}) noexcept
: std::unique_ptr<element_type, deleter_type>(ptr, xmlFreeDoc)
, XMLNode{xmlDocGetRootElement(get())}
{
if(d && !n.name.empty() && n.name != name() && !n.ns.empty() && n.ns != ns())
d = {};
}
XMLDocument(std::string_view path, const XMLName &n = {}) noexcept
: XMLDocument(path.empty() ? nullptr : xmlParseFile(path.data()), n)
{}
static XMLDocument openStream(std::istream &is, const XMLName &name = {}, bool hugeFile = false)
{
auto ctxt = make_unique_ptr(xmlCreateIOParserCtxt(nullptr, nullptr, [](void *context, char *buffer, int len) -> int {
auto *is = static_cast<std::istream *>(context);
is->read(buffer, len);
return is->good() || is->eof() ? int(is->gcount()) : -1;
}, nullptr, &is, XML_CHAR_ENCODING_NONE), xmlFreeParserCtxt);
ctxt->linenumbers = 1;
ctxt->options |= XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_DTDATTR|XML_PARSE_NONET;
ctxt->loadsubset |= XML_DETECT_IDS|XML_COMPLETE_ATTRS;
if(hugeFile)
ctxt->options |= XML_PARSE_HUGE;
auto result = xmlParseDocument(ctxt.get());
if(result != 0 || !ctxt->wellFormed)
THROW("%s", ctxt->lastError.message);
return {ctxt->myDoc, name};
}
static XMLDocument create(std::string_view name = {}, std::string_view href = {}, std::string_view prefix = {}) noexcept
{
XMLDocument doc(xmlNewDoc(nullptr));
if(!name.empty())
{
doc.d = xmlNewNode(nullptr, pcxmlChar(name.data()));
if(!href.empty())
xmlSetNs(doc.d, doc.addNS(href, prefix));
xmlDocSetRootElement(doc.get(), doc.d);
}
return doc;
}
void c14n(Digest *digest, std::string_view algo, XMLNode node)
{
xmlC14NMode mode = XML_C14N_1_0;
int with_comments = 0;
if(algo == C14D_ID_1_0)
mode = XML_C14N_1_0;
else if(algo == C14D_ID_1_0_COM)
with_comments = 1;
else if(algo == C14D_ID_1_1)
mode = XML_C14N_1_1;
else if(algo == C14D_ID_1_1_COM)
{
mode = XML_C14N_1_1;
with_comments = 1;
}
else if(algo == C14D_ID_1_0_EXC)
mode = XML_C14N_EXCLUSIVE_1_0;
else if(algo == C14D_ID_1_0_EXC_COM)
{
mode = XML_C14N_EXCLUSIVE_1_0;
with_comments = 1;
}
else if(!algo.empty())
THROW("Unsupported canonicalization method '%.*s'", int(algo.size()), algo.data());
auto buf = make_unique_ptr(xmlOutputBufferCreateIO([](void *context, const char *buffer, int len) {
auto *digest = static_cast<Digest *>(context);
digest->update(pcxmlChar(buffer), size_t(len));
return len;
}, nullptr, digest, nullptr), xmlOutputBufferClose);
int size = xmlC14NExecute(get(), [](void *root, xmlNodePtr node, xmlNodePtr parent) constexpr noexcept {
if(root == node)
return 1;
for(; parent; parent = parent->parent)
{
if(root == parent)
return 1;
}
return 0;
}, node.d, mode, nullptr, with_comments, buf.get());
if(size < 0)
THROW("Failed to canonicalizate input");
}
bool save(std::string_view path) const noexcept
{
return xmlSaveFormatFileEnc(path.data(), get(), "UTF-8", 0) > 0;
}
bool save(std::ostream &os) const noexcept
{
auto *buf = xmlOutputBufferCreateIO([](void *context, const char *buffer, int len) {
auto *os = static_cast<std::ostream *>(context);
return os->write(buffer, len) ? len : -1;
}, nullptr, &os, nullptr);
return xmlSaveFormatFileTo(buf, get(), "UTF-8", 0) > 0;
}
void validateSchema(const std::string &schemaPath) const
{
auto parser = make_unique_ptr(xmlSchemaNewParserCtxt(schemaPath.c_str()), xmlSchemaFreeParserCtxt);
if(!parser)
THROW("Failed to create schema parser context %s", schemaPath.c_str());
xmlSchemaSetParserErrors(parser.get(), schemaValidationError, schemaValidationWarning, nullptr);
auto schema = make_unique_ptr(xmlSchemaParse(parser.get()), xmlSchemaFree);
if(!schema)
THROW("Failed to parse schema %s", schemaPath.c_str());
auto validate = make_unique_ptr(xmlSchemaNewValidCtxt(schema.get()), xmlSchemaFreeValidCtxt);
if(!validate)
THROW("Failed to create schema validation context %s", schemaPath.c_str());
Exception e(EXCEPTION_PARAMS("Failed to XML with schema"));
xmlSchemaSetValidErrors(validate.get(), schemaValidationError, schemaValidationWarning, &e);
if(xmlSchemaValidateDoc(validate.get(), get()) != 0)
throw e;
}
static bool verifySignature(XMLNode signature, [[maybe_unused]] Exception *e = {}) noexcept
{
auto mngr = make_unique_ptr(xmlSecKeysMngrCreate(), xmlSecKeysMngrDestroy);
if(!mngr)
return false;
if(xmlSecCryptoAppDefaultKeysMngrInit(mngr.get()) < 0)
return false;
auto ctx = make_unique_ptr(xmlSecDSigCtxCreate(mngr.get()), xmlSecDSigCtxDestroy);
if(!ctx)
return false;
ctx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
int result = xmlSecDSigCtxVerify(ctx.get(), signature.d);
#if VERSION_CHECK(XMLSEC_VERSION_MAJOR, XMLSEC_VERSION_MINOR, XMLSEC_VERSION_SUBMINOR) >= VERSION_CHECK(1, 3, 0)
if(ctx->failureReason == xmlSecDSigFailureReasonReference)
{
for(xmlSecSize i = 0; i < xmlSecPtrListGetSize(&(ctx->signedInfoReferences)); ++i)
{
auto *ref = xmlSecDSigReferenceCtxPtr(xmlSecPtrListGetItem(&(ctx->signedInfoReferences), i));
if(ref->status != xmlSecDSigStatusSucceeded)
{
if(e)
e->addCause(Exception(EXCEPTION_PARAMS("Failed to validate Reference '%s'", ref->uri)));
else
WARN("Failed to validate Reference '%s'", ref->uri);
}
}
}
#endif
if(result < 0)
return false;
return ctx->status == xmlSecDSigStatusSucceeded;
}
static void schemaValidationError(void *ctx, const char *msg, ...) noexcept
{
va_list args{};
va_start(args, msg);
std::string m = Log::formatArgList(msg, args);
va_end(args);
if(ctx)
{
auto *e = static_cast<Exception*>(ctx);
e->addCause(digidoc::Exception(EXCEPTION_PARAMS("Schema validation error: %s", m.c_str())));
}
else
ERR("Schema validation error: %s", m.c_str());
}
static void schemaValidationWarning(void */*ctx*/, const char *msg, ...) noexcept
{
va_list args{};
va_start(args, msg);
std::string m = Log::formatArgList(msg, args);
va_end(args);
WARN("Schema validation warning: %s", m.c_str());
}
};
}