LibreOffice Module xmlsecurity (master) 1
xmlstreamio.cxx
Go to the documentation of this file.
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#include <xmlsec-wrapper.h>
22
23/*
24 * Implementation of the I/O interfaces based on stream and URI binding
25 */
28#include <rtl/ustring.hxx>
29#include <rtl/uri.hxx>
31#include <sal/log.hxx>
32
33#include <com/sun/star/xml/crypto/XUriBinding.hpp>
34
35static bool g_bInputCallbacksEnabled = false;
36static bool g_bInputCallbacksRegistered = false;
37
38static css::uno::Reference< css::xml::crypto::XUriBinding > m_xUriBinding ;
39
40extern "C" {
41
42static int xmlStreamMatch( const char* uri )
43{
44 css::uno::Reference< css::io::XInputStream > xInputStream ;
45
47 {
48 if( uri == nullptr || !m_xUriBinding.is() )
49 return 0 ;
50 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
51 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
52 //first. If this failed it would try this
53 //again with the original escaped string. However, it does not get this far, because there
54 //is another callback registered by libxml which claims to be able to handle this uri.
55 OUString sUri =
56 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
57 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
58 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
59 if (!xInputStream.is())
60 {
61 //Try the passed in uri directly.
62 //For old documents prior OOo 3.0. We did not use URIs then.
63 xInputStream = m_xUriBinding->getUriBinding(
64 OUString::createFromAscii(uri));
65 }
66 }
67 SAL_INFO("xmlsecurity.xmlsec",
68 "xmlStreamMath: uri is '" << uri << "', returning " << xInputStream.is());
69 if (xInputStream.is())
70 return 1;
71 else
72 return 0 ;
73}
74
75static void* xmlStreamOpen( const char* uri )
76{
77 css::uno::Reference< css::io::XInputStream > xInputStream ;
78
80 {
81 if( uri == nullptr || !m_xUriBinding.is() )
82 return nullptr ;
83
84 //see xmlStreamMatch
85 OUString sUri =
86 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
87 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
88 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
89 if (!xInputStream.is())
90 {
91 //For old documents.
92 //try the passed in uri directly.
93 xInputStream = m_xUriBinding->getUriBinding(
94 OUString::createFromAscii(uri));
95 }
96
97 if( xInputStream.is() ) {
98 css::io::XInputStream* pInputStream ;
99 pInputStream = xInputStream.get() ;
100 pInputStream->acquire() ;
101 SAL_INFO("xmlsecurity.xmlsec",
102 "xmlStreamOpen: uri is '" << uri << "', returning context " << pInputStream);
103 return static_cast<void*>(pInputStream) ;
104 }
105 }
106
107 return nullptr ;
108}
109
110static int xmlStreamRead( void* context, char* buffer, int len )
111{
112 int numbers ;
113 css::uno::Reference< css::io::XInputStream > xInputStream ;
114 css::uno::Sequence< sal_Int8 > outSeqs( len ) ;
115
116 numbers = 0 ;
118 {
119 if( context != nullptr ) {
120 xInputStream = static_cast<css::io::XInputStream*>(context);
121 if( !xInputStream.is() )
122 return 0 ;
123
124 numbers = xInputStream->readBytes( outSeqs, len ) ;
125 const sal_Int8* readBytes = outSeqs.getArray() ;
126 for( int i = 0 ; i < numbers ; i ++ )
127 *( buffer + i ) = *( readBytes + i ) ;
128 }
129 }
130
131 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: context is " << context << ", buffer is now '"
132 << OString(buffer, numbers) << "'");
133 return numbers ;
134}
135
136static int xmlStreamClose( void * context )
137{
139 {
140 if( context != nullptr ) {
141 css::io::XInputStream* pInputStream ;
142 pInputStream = static_cast<css::io::XInputStream*>(context);
143 pInputStream->release() ;
144 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: closed context " << context);
145 }
146 }
147
148 return 0 ;
149}
150
151}
152
154{
156 {
157 //Register the callbacks into xmlSec
158 //In order to make the xmlsec io finding the callbacks firstly,
159 //I put the callbacks at the very beginning.
160
161 //Cleanup the older callbacks.
162 //Notes: all none default callbacks will lose.
163 xmlSecIOCleanupCallbacks() ;
164
165 // Make sure that errors are reported via SAL_WARN().
168
169 // Newer xmlsec wants the callback order in the opposite direction.
170 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible))
171 {
172 //Register the default callbacks.
173 //Notes: the error will cause xmlsec working problems.
174 int cbs = xmlSecIORegisterDefaultCallbacks() ;
175 if( cbs < 0 ) {
176 return -1 ;
177 }
178
179 //Register my classbacks.
180 cbs = xmlSecIORegisterCallbacks(
185 if( cbs < 0 ) {
186 return -1 ;
187 }
188 }
189 else
190 {
191 //Register my classbacks.
192 int cbs = xmlSecIORegisterCallbacks(
197 if( cbs < 0 ) {
198 return -1 ;
199 }
200
201 //Register the default callbacks.
202 //Notes: the error will cause xmlsec working problems.
203 cbs = xmlSecIORegisterDefaultCallbacks() ;
204 if( cbs < 0 ) {
205 return -1 ;
206 }
207 }
208
210 }
211
212 return 0 ;
213}
214
216 css::uno::Reference< css::xml::crypto::XUriBinding > const & aUriBinding
217) {
219 {
221 return -1 ;
222 }
223
226
227 m_xUriBinding = aUriBinding ;
228
229 return 0 ;
230}
231
233{
235 {
236 //Clear the uri-stream binding
237 m_xUriBinding.clear() ;
238
239 //disable the registered flag
241 }
242
243 return 0 ;
244}
245
249}
250
251/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void setErrorRecorder()
void clearErrorRecorder()
#define SAL_INFO(area, stream)
int i
signed char sal_Int8
int xmlRegisterStreamInputCallbacks(css::uno::Reference< css::xml::crypto::XUriBinding > const &aUriBinding)
int xmlUnregisterStreamInputCallbacks()
int xmlEnableStreamInputCallbacks()
static css::uno::Reference< css::xml::crypto::XUriBinding > m_xUriBinding
Definition: xmlstreamio.cxx:38
void xmlDisableStreamInputCallbacks()
static int xmlStreamRead(void *context, char *buffer, int len)
static bool g_bInputCallbacksEnabled
Definition: xmlstreamio.cxx:35
static int xmlStreamClose(void *context)
static void * xmlStreamOpen(const char *uri)
Definition: xmlstreamio.cxx:75
static bool g_bInputCallbacksRegistered
Definition: xmlstreamio.cxx:36
static int xmlStreamMatch(const char *uri)
Definition: xmlstreamio.cxx:42