Debian Codegraph @

xmlsecurity/source/xmlsec/xmlstreamio.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <sal/config.h>
21
22 #include <xmlsec/io.h>
23
24 /*
25 * Implementation of the I/O interfaces based on stream and URI binding
26 */
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>
33
34 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
35
36 static bool g_bInputCallbacksEnabled = false;
37 static bool g_bInputCallbacksRegistered = false;
38
39 static css::uno::Reference< css::xml::crypto::XUriBinding > m_xUriBinding ;
40
41 extern "C" {
42
43 static int xmlStreamMatch( const char* uri )
44 {
45 css::uno::Reference< css::io::XInputStream > xInputStream ;
46
47 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
48 {
49 if( uri == nullptr || !m_xUriBinding.is() )
50 return 0 ;
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.
56 OUString sUri =
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())
61 {
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));
66 }
67 }
68 SAL_INFO("xmlsecurity.xmlsec",
69 "xmlStreamMath: uri is '" << uri << "', returning " << xInputStream.is());
70 if (xInputStream.is())
71 return 1;
72 else
73 return 0 ;
74 }
75
76 static void* xmlStreamOpen( const char* uri )
77 {
78 css::uno::Reference< css::io::XInputStream > xInputStream ;
79
80 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
81 {
82 if( uri == nullptr || !m_xUriBinding.is() )
83 return nullptr ;
84
85 //see xmlStreamMatch
86 OUString sUri =
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())
91 {
92 //For old documents.
93 //try the passed in uri directly.
94 xInputStream = m_xUriBinding->getUriBinding(
95 OUString::createFromAscii(uri));
96 }
97
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) ;
105 }
106 }
107
108 return nullptr ;
109 }
110
111 static int xmlStreamRead( void* context, char* buffer, int len )
112 {
113 int numbers ;
114 css::uno::Reference< css::io::XInputStream > xInputStream ;
115 css::uno::Sequence< sal_Int8 > outSeqs( len ) ;
116
117 numbers = 0 ;
118 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
119 {
120 if( context != nullptr ) {
121 xInputStream = static_cast<css::io::XInputStream*>(context);
122 if( !xInputStream.is() )
123 return 0 ;
124
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 ) ;
129 }
130 }
131
132 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: context is " << context << ", buffer is now '"
133 << OString(buffer, numbers) << "'");
134 return numbers ;
135 }
136
137 static int xmlStreamClose( void * context )
138 {
139 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
140 {
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);
146 }
147 }
148
149 return 0 ;
150 }
151
152 }
153
154 int xmlEnableStreamInputCallbacks()
155 {
156 if (!g_bInputCallbacksEnabled)
157 {
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.
161
162 //Cleanup the older callbacks.
163 //Notes: all none default callbacks will lose.
164 xmlSecIOCleanupCallbacks() ;
165
166 // Make sure that errors are reported via SAL_WARN().
167 setErrorRecorder();
168 comphelper::ScopeGuard g([] { clearErrorRecorder(); });
169
170 // Newer xmlsec wants the callback order in the opposite direction.
171 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible))
172 {
173 //Register the default callbacks.
174 //Notes: the error will cause xmlsec working problems.
175 int cbs = xmlSecIORegisterDefaultCallbacks() ;
176 if( cbs < 0 ) {
177 return -1 ;
178 }
179
180 //Register my classbacks.
181 cbs = xmlSecIORegisterCallbacks(
182 xmlStreamMatch,
183 xmlStreamOpen,
184 xmlStreamRead,
185 xmlStreamClose ) ;
186 if( cbs < 0 ) {
187 return -1 ;
188 }
189 }
190 else
191 {
192 //Register my classbacks.
193 int cbs = xmlSecIORegisterCallbacks(
194 xmlStreamMatch,
195 xmlStreamOpen,
196 xmlStreamRead,
197 xmlStreamClose ) ;
198 if( cbs < 0 ) {
199 return -1 ;
200 }
201
202 //Register the default callbacks.
203 //Notes: the error will cause xmlsec working problems.
204 cbs = xmlSecIORegisterDefaultCallbacks() ;
205 if( cbs < 0 ) {
206 return -1 ;
207 }
208 }
209
210 g_bInputCallbacksEnabled = true;
211 }
212
213 return 0 ;
214 }
215
216 int xmlRegisterStreamInputCallbacks(
217 css::uno::Reference< css::xml::crypto::XUriBinding > const & aUriBinding
218 ) {
219 if (!g_bInputCallbacksEnabled)
220 {
221 if( xmlEnableStreamInputCallbacks() < 0 )
222 return -1 ;
223 }
224
225 if (!g_bInputCallbacksRegistered)
226 g_bInputCallbacksRegistered = true;
227
228 m_xUriBinding = aUriBinding ;
229
230 return 0 ;
231 }
232
233 int xmlUnregisterStreamInputCallbacks()
234 {
235 if (g_bInputCallbacksRegistered)
236 {
237 //Clear the uri-stream binding
238 m_xUriBinding.clear() ;
239
240 //disable the registered flag
241 g_bInputCallbacksRegistered = false;
242 }
243
244 return 0 ;
245 }
246
247 void xmlDisableStreamInputCallbacks() {
248 xmlUnregisterStreamInputCallbacks() ;
249 g_bInputCallbacksEnabled = false;
250 }
251
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */