1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 * Implementation of the I/O interfaces based on stream and URI binding
24 #include <xmlsec/xmlstreamio.hxx>
25 #include <xmlsec/errorcallback.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/uri.hxx>
28 #include <comphelper/scopeguard.hxx>
30 #include <libxml/uri.h>
31 #include <xmlsec-wrapper.h>
33 #define XMLSTREAMIO_INITIALIZED 0x01
34 #define XMLSTREAMIO_REGISTERED 0x02
36 /* Global variables */
38 * Enable stream I/O or not.
40 static char enableXmlStreamIO
= 0x00 ;
42 static css::uno::Reference
< css::xml::crypto::XUriBinding
> m_xUriBinding
;
45 int xmlStreamMatch( const char* uri
)
47 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
49 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
50 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
51 if( uri
== nullptr || !m_xUriBinding
.is() )
53 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
54 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
55 //first. If this failed it would try this
56 //again with the original escaped string. However, it does not get this far, because there
57 //is another callback registered by libxml which claims to be able to handle this uri.
59 ::rtl::Uri::encode( OUString::createFromAscii( uri
),
60 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
61 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
62 if (!xInputStream
.is())
64 //Try the passed in uri directly.
65 //For old documents prior OOo 3.0. We did not use URIs then.
66 xInputStream
= m_xUriBinding
->getUriBinding(
67 OUString::createFromAscii(uri
));
70 if (xInputStream
.is())
77 void* xmlStreamOpen( const char* uri
)
79 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
81 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
82 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
83 if( uri
== nullptr || !m_xUriBinding
.is() )
88 ::rtl::Uri::encode( OUString::createFromAscii( uri
),
89 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
90 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
91 if (!xInputStream
.is())
94 //try the passed in uri directly.
95 xInputStream
= m_xUriBinding
->getUriBinding(
96 OUString::createFromAscii(uri
));
99 if( xInputStream
.is() ) {
100 css::io::XInputStream
* pInputStream
;
101 pInputStream
= xInputStream
.get() ;
102 pInputStream
->acquire() ;
103 return static_cast<void*>(pInputStream
) ;
111 int xmlStreamRead( void* context
, char* buffer
, int len
)
114 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
115 css::uno::Sequence
< sal_Int8
> outSeqs( len
) ;
118 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
119 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
120 if( context
!= nullptr ) {
121 xInputStream
= static_cast<css::io::XInputStream
*>(context
);
122 if( !xInputStream
.is() )
125 numbers
= xInputStream
->readBytes( outSeqs
, len
) ;
126 const sal_Int8
* readBytes
= outSeqs
.getArray() ;
127 for( int i
= 0 ; i
< numbers
; i
++ )
128 *( buffer
+ i
) = *( readBytes
+ i
) ;
136 int xmlStreamClose( void * context
)
138 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
139 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
140 if( context
!= nullptr ) {
141 css::io::XInputStream
* pInputStream
;
142 pInputStream
= static_cast<css::io::XInputStream
*>(context
);
143 pInputStream
->release() ;
150 XSECXMLSEC_DLLPUBLIC
int xmlEnableStreamInputCallbacks()
153 if( !( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) ) {
154 //Register the callbacks into xmlSec
155 //In order to make the xmlsec io finding the callbacks firstly,
156 //I put the callbacks at the very beginning.
158 //Cleanup the older callbacks.
159 //Notes: all none default callbacks will lose.
160 xmlSecIOCleanupCallbacks() ;
162 // Make sure that errors are reported via SAL_WARN().
164 comphelper::ScopeGuard
g([] { clearErrorRecorder(); });
166 // Newer xmlsec wants the callback order in the opposite direction.
167 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible
))
169 //Register the default callbacks.
170 //Notes: the error will cause xmlsec working problems.
171 int cbs
= xmlSecIORegisterDefaultCallbacks() ;
176 //Register my classbacks.
177 cbs
= xmlSecIORegisterCallbacks(
188 //Register my classbacks.
189 int cbs
= xmlSecIORegisterCallbacks(
198 //Register the default callbacks.
199 //Notes: the error will cause xmlsec working problems.
200 cbs
= xmlSecIORegisterDefaultCallbacks() ;
206 enableXmlStreamIO
|= XMLSTREAMIO_INITIALIZED
;
212 XSECXMLSEC_DLLPUBLIC
int xmlRegisterStreamInputCallbacks(
213 css::uno::Reference
< css::xml::crypto::XUriBinding
> const & aUriBinding
215 if( !( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) ) {
216 if( xmlEnableStreamInputCallbacks() < 0 )
220 if( !( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
221 enableXmlStreamIO
|= XMLSTREAMIO_REGISTERED
;
224 m_xUriBinding
= aUriBinding
;
229 XSECXMLSEC_DLLPUBLIC
int xmlUnregisterStreamInputCallbacks()
231 if( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) {
232 //Clear the uri-stream binding
233 m_xUriBinding
.clear() ;
235 //disable the registered flag
236 enableXmlStreamIO
&= ~XMLSTREAMIO_REGISTERED
;
242 XSECXMLSEC_DLLPUBLIC
void xmlDisableStreamInputCallbacks() {
243 xmlUnregisterStreamInputCallbacks() ;
244 enableXmlStreamIO
&= ~XMLSTREAMIO_INITIALIZED
;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */