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 .
20 #include <sal/config.h>
22 #include <xmlsec/io.h>
25 * Implementation of the I/O interfaces based on stream and URI binding
27 #include <xmlsec/xmlstreamio.hxx>
28 #include <xmlsec/errorcallback.hxx>
29 #include <rtl/ustring.hxx>
30 #include <rtl/uri.hxx>
31 #include <comphelper/scopeguard.hxx>
32 #include <sal/log.hxx>
34 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
36 static bool g_bInputCallbacksEnabled
= false;
37 static bool g_bInputCallbacksRegistered
= false;
39 static css::uno::Reference
< css::xml::crypto::XUriBinding
> m_xUriBinding
;
43 static int xmlStreamMatch( const char* uri
)
45 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
47 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
49 if( uri
== nullptr || !m_xUriBinding
.is() )
51 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
52 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
53 //first. If this failed it would try this
54 //again with the original escaped string. However, it does not get this far, because there
55 //is another callback registered by libxml which claims to be able to handle this uri.
57 ::rtl::Uri::encode( OUString::createFromAscii( uri
),
58 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
59 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
60 if (!xInputStream
.is())
62 //Try the passed in uri directly.
63 //For old documents prior OOo 3.0. We did not use URIs then.
64 xInputStream
= m_xUriBinding
->getUriBinding(
65 OUString::createFromAscii(uri
));
68 SAL_INFO("xmlsecurity.xmlsec",
69 "xmlStreamMath: uri is '" << uri
<< "', returning " << xInputStream
.is());
70 if (xInputStream
.is())
76 static void* xmlStreamOpen( const char* uri
)
78 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
80 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
82 if( uri
== nullptr || !m_xUriBinding
.is() )
87 ::rtl::Uri::encode( OUString::createFromAscii( uri
),
88 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
89 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
90 if (!xInputStream
.is())
93 //try the passed in uri directly.
94 xInputStream
= m_xUriBinding
->getUriBinding(
95 OUString::createFromAscii(uri
));
98 if( xInputStream
.is() ) {
99 css::io::XInputStream
* pInputStream
;
100 pInputStream
= xInputStream
.get() ;
101 pInputStream
->acquire() ;
102 SAL_INFO("xmlsecurity.xmlsec",
103 "xmlStreamOpen: uri is '" << uri
<< "', returning context " << pInputStream
);
104 return static_cast<void*>(pInputStream
) ;
111 static 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 (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
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
) ;
132 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: context is " << context
<< ", buffer is now '"
133 << OString(buffer
, numbers
) << "'");
137 static int xmlStreamClose( void * context
)
139 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
141 if( context
!= nullptr ) {
142 css::io::XInputStream
* pInputStream
;
143 pInputStream
= static_cast<css::io::XInputStream
*>(context
);
144 pInputStream
->release() ;
145 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: closed context " << context
);
154 int xmlEnableStreamInputCallbacks()
156 if (!g_bInputCallbacksEnabled
)
158 //Register the callbacks into xmlSec
159 //In order to make the xmlsec io finding the callbacks firstly,
160 //I put the callbacks at the very beginning.
162 //Cleanup the older callbacks.
163 //Notes: all none default callbacks will lose.
164 xmlSecIOCleanupCallbacks() ;
166 // Make sure that errors are reported via SAL_WARN().
168 comphelper::ScopeGuard
g([] { clearErrorRecorder(); });
170 // Newer xmlsec wants the callback order in the opposite direction.
171 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible
))
173 //Register the default callbacks.
174 //Notes: the error will cause xmlsec working problems.
175 int cbs
= xmlSecIORegisterDefaultCallbacks() ;
180 //Register my classbacks.
181 cbs
= xmlSecIORegisterCallbacks(
192 //Register my classbacks.
193 int cbs
= xmlSecIORegisterCallbacks(
202 //Register the default callbacks.
203 //Notes: the error will cause xmlsec working problems.
204 cbs
= xmlSecIORegisterDefaultCallbacks() ;
210 g_bInputCallbacksEnabled
= true;
216 int xmlRegisterStreamInputCallbacks(
217 css::uno::Reference
< css::xml::crypto::XUriBinding
> const & aUriBinding
219 if (!g_bInputCallbacksEnabled
)
221 if( xmlEnableStreamInputCallbacks() < 0 )
225 if (!g_bInputCallbacksRegistered
)
226 g_bInputCallbacksRegistered
= true;
228 m_xUriBinding
= aUriBinding
;
233 int xmlUnregisterStreamInputCallbacks()
235 if (g_bInputCallbacksRegistered
)
237 //Clear the uri-stream binding
238 m_xUriBinding
.clear() ;
240 //disable the registered flag
241 g_bInputCallbacksRegistered
= false;
247 void xmlDisableStreamInputCallbacks() {
248 xmlUnregisterStreamInputCallbacks() ;
249 g_bInputCallbacksEnabled
= false;
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */