3030#include < xmlsec/xmltree.h>
3131#include < xmlsec/xmldsig.h>
3232#include < xmlsec/crypto.h>
33+ #include < xmlsec/parser.h>
3334
3435#include < openssl/evp.h>
3536
@@ -45,7 +46,7 @@ static std::vector<unsigned char> from_base64(std::string_view data)
4546 std::vector<unsigned char > result (EVP_DECODE_LENGTH (data.size ()), 0 );
4647 size_t dataPos = 0 ;
4748 int size = 0 ;
48- auto ctx = make_unique_ptr (EVP_ENCODE_CTX_new (), EVP_ENCODE_CTX_free );
49+ auto ctx = make_unique_ptr<EVP_ENCODE_CTX_free> (EVP_ENCODE_CTX_new ());
4950 EVP_DecodeInit (ctx.get ());
5051
5152 for (auto pos = data.find_first_of (whitespace);
@@ -71,7 +72,7 @@ static std::vector<unsigned char> from_base64(std::string_view data)
7172static std::string to_base64 (const std::vector<unsigned char > &data)
7273{
7374 std::string result (EVP_ENCODE_LENGTH (data.size ()), 0 );
74- auto ctx = make_unique_ptr (EVP_ENCODE_CTX_new (), EVP_ENCODE_CTX_free );
75+ auto ctx = make_unique_ptr<EVP_ENCODE_CTX_free> (EVP_ENCODE_CTX_new ());
7576 EVP_EncodeInit (ctx.get ());
7677 int size{};
7778 if (EVP_EncodeUpdate (ctx.get (), (unsigned char *)result.data (), &size, data.data (), int (data.size ())) < 1 )
@@ -305,19 +306,27 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
305306
306307 static XMLDocument openStream (std::istream &is, const XMLName &name = {}, bool hugeFile = false )
307308 {
308- auto ctxt = make_unique_ptr (xmlCreateIOParserCtxt (nullptr , nullptr , [](void *context, char *buffer, int len) -> int {
309+ auto ctxt = make_unique_ptr<xmlFreeParserCtxt> (xmlCreateIOParserCtxt (nullptr , nullptr , [](void *context, char *buffer, int len) -> int {
309310 auto *is = static_cast <std::istream *>(context);
310311 is->read (buffer, len);
311312 return is->good () || is->eof () ? int (is->gcount ()) : -1 ;
312- }, nullptr , &is, XML_CHAR_ENCODING_NONE ), xmlFreeParserCtxt);
313- ctxt->linenumbers = 1 ;
313+ }, nullptr , &is, XML_CHAR_ENCODING_NONE ));
314+ #if VERSION_CHECK(XMLSEC_VERSION_MAJOR, XMLSEC_VERSION_MINOR, XMLSEC_VERSION_SUBMINOR) >= VERSION_CHECK(1, 3, 0)
315+ ctxt->options |= xmlSecParserGetDefaultOptions ();
316+ #else
314317 ctxt->options |= XML_PARSE_NOENT |XML_PARSE_DTDLOAD |XML_PARSE_DTDATTR |XML_PARSE_NONET ;
318+ #endif
315319 ctxt->loadsubset |= XML_DETECT_IDS |XML_COMPLETE_ATTRS ;
316320 if (hugeFile)
317321 ctxt->options |= XML_PARSE_HUGE ;
318322 auto result = xmlParseDocument (ctxt.get ());
319323 if (result != 0 || !ctxt->wellFormed )
320- THROW (" %s" , ctxt->lastError .message );
324+ {
325+ if (const xmlError *lastError = xmlCtxtGetLastError (ctxt.get ()))
326+ THROW (" %s" , lastError->message );
327+ else
328+ THROW (" Failed to parse XML document from stream" );
329+ }
321330 return {ctxt->myDoc , name};
322331 }
323332
@@ -358,11 +367,11 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
358367 }
359368 else if (!algo.empty ())
360369 THROW (" Unsupported canonicalization method '%.*s'" , int (algo.size ()), algo.data ());
361- auto buf = make_unique_ptr (xmlOutputBufferCreateIO ([](void *context, const char *buffer, int len) {
370+ auto buf = make_unique_ptr<xmlOutputBufferClose> (xmlOutputBufferCreateIO ([](void *context, const char *buffer, int len) {
362371 auto *digest = static_cast <Digest *>(context);
363372 digest->update (pcxmlChar (buffer), size_t (len));
364373 return len;
365- }, nullptr , const_cast <Digest*>(&digest), nullptr ), xmlOutputBufferClose );
374+ }, nullptr , const_cast <Digest*>(&digest), nullptr ));
366375 int size = xmlC14NExecute (get (), [](void *root, xmlNodePtr node, xmlNodePtr parent) constexpr noexcept {
367376 if (root == node)
368377 return 1 ;
@@ -396,14 +405,14 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
396405
397406 void validateSchema (const std::string &schemaPath) const
398407 {
399- auto parser = make_unique_ptr (xmlSchemaNewParserCtxt (schemaPath.c_str ()), xmlSchemaFreeParserCtxt );
408+ auto parser = make_unique_ptr<xmlSchemaFreeParserCtxt> (xmlSchemaNewParserCtxt (schemaPath.c_str ()));
400409 if (!parser)
401410 THROW (" Failed to create schema parser context %s" , schemaPath.c_str ());
402411 xmlSchemaSetParserErrors (parser.get (), schemaValidationError, schemaValidationWarning, nullptr );
403- auto schema = make_unique_ptr (xmlSchemaParse (parser.get ()), xmlSchemaFree );
412+ auto schema = make_unique_ptr<xmlSchemaFree> (xmlSchemaParse (parser.get ()));
404413 if (!schema)
405414 THROW (" Failed to parse schema %s" , schemaPath.c_str ());
406- auto validate = make_unique_ptr (xmlSchemaNewValidCtxt (schema.get ()), xmlSchemaFreeValidCtxt );
415+ auto validate = make_unique_ptr<xmlSchemaFreeValidCtxt> (xmlSchemaNewValidCtxt (schema.get ()));
407416 if (!validate)
408417 THROW (" Failed to create schema validation context %s" , schemaPath.c_str ());
409418 Exception e (EXCEPTION_PARAMS (" Failed to XML with schema" ));
@@ -414,12 +423,12 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
414423
415424 static bool verifySignature (XMLNode signature, [[maybe_unused]] Exception *e = {}) noexcept
416425 {
417- auto mngr = make_unique_ptr (xmlSecKeysMngrCreate (), xmlSecKeysMngrDestroy );
426+ auto mngr = make_unique_ptr<xmlSecKeysMngrDestroy> (xmlSecKeysMngrCreate ());
418427 if (!mngr)
419428 return false ;
420429 if (xmlSecCryptoAppDefaultKeysMngrInit (mngr.get ()) < 0 )
421430 return false ;
422- auto ctx = make_unique_ptr (xmlSecDSigCtxCreate (mngr.get ()), xmlSecDSigCtxDestroy );
431+ auto ctx = make_unique_ptr<xmlSecDSigCtxDestroy> (xmlSecDSigCtxCreate (mngr.get ()));
423432 if (!ctx)
424433 return false ;
425434 ctx->keyInfoReadCtx .flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS ;
0 commit comments