Branch data Line data Source code
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 <rtl/uuid.h>
22 : : #include "xmlsignature_nssimpl.hxx"
23 : :
24 : : #include "xmldocumentwrapper_xmlsecimpl.hxx"
25 : :
26 : : #include "xmlelementwrapper_xmlsecimpl.hxx"
27 : :
28 : : #include "securityenvironment_nssimpl.hxx"
29 : :
30 : : #include "xmlsecuritycontext_nssimpl.hxx"
31 : : #include "xmlstreamio.hxx"
32 : : #include "errorcallback.hxx"
33 : :
34 : : #include <sal/types.h>
35 : : //For reasons that escape me, this is what xmlsec does when size_t is not 4
36 : : #if SAL_TYPES_SIZEOFPOINTER != 4
37 : : # define XMLSEC_NO_SIZE_T
38 : : #endif
39 : : #include "xmlsec/xmlsec.h"
40 : : #include "xmlsec/xmldsig.h"
41 : : #include "xmlsec/crypto.h"
42 : :
43 : : using namespace ::com::sun::star::uno ;
44 : : using namespace ::com::sun::star::lang ;
45 : : using ::com::sun::star::lang::XMultiServiceFactory ;
46 : : using ::com::sun::star::lang::XSingleServiceFactory ;
47 : : using ::rtl::OUString ;
48 : :
49 : : using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
50 : : using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper ;
51 : : using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
52 : : using ::com::sun::star::xml::crypto::XXMLSignature ;
53 : : using ::com::sun::star::xml::crypto::XXMLSignatureTemplate ;
54 : : using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
55 : : using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
56 : : using ::com::sun::star::xml::crypto::XUriBinding ;
57 : : using ::com::sun::star::xml::crypto::XMLSignatureException ;
58 : :
59 : 0 : XMLSignature_NssImpl :: XMLSignature_NssImpl( const Reference< XMultiServiceFactory >& aFactory ) : m_xServiceManager( aFactory ) {
60 : 0 : }
61 : :
62 : 0 : XMLSignature_NssImpl :: ~XMLSignature_NssImpl() {
63 [ # # ]: 0 : }
64 : :
65 : : /* XXMLSignature */
66 : : Reference< XXMLSignatureTemplate >
67 : 0 : SAL_CALL XMLSignature_NssImpl :: generate(
68 : : const Reference< XXMLSignatureTemplate >& aTemplate ,
69 : : const Reference< XSecurityEnvironment >& aEnvironment
70 : : ) throw( com::sun::star::xml::crypto::XMLSignatureException,
71 : : com::sun::star::uno::SecurityException )
72 : : {
73 : 0 : xmlSecKeysMngrPtr pMngr = NULL ;
74 : 0 : xmlSecDSigCtxPtr pDsigCtx = NULL ;
75 : 0 : xmlNodePtr pNode = NULL ;
76 : :
77 [ # # ]: 0 : if( !aTemplate.is() )
78 [ # # ]: 0 : throw RuntimeException() ;
79 : :
80 [ # # ]: 0 : if( !aEnvironment.is() )
81 [ # # ]: 0 : throw RuntimeException() ;
82 : :
83 : : //Get the xml node
84 [ # # ][ # # ]: 0 : Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
85 [ # # ]: 0 : if( !xElement.is() ) {
86 [ # # ]: 0 : throw RuntimeException() ;
87 : : }
88 : :
89 [ # # ]: 0 : Reference< XUnoTunnel > xNodTunnel( xElement , UNO_QUERY ) ;
90 [ # # ]: 0 : if( !xNodTunnel.is() ) {
91 [ # # ]: 0 : throw RuntimeException() ;
92 : : }
93 : :
94 : : XMLElementWrapper_XmlSecImpl* pElement =
95 : : reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
96 : : sal::static_int_cast<sal_uIntPtr>(
97 [ # # ][ # # ]: 0 : xNodTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
[ # # ][ # # ]
98 [ # # ]: 0 : if( pElement == NULL ) {
99 [ # # ]: 0 : throw RuntimeException() ;
100 : : }
101 : :
102 [ # # ]: 0 : pNode = pElement->getNativeElement() ;
103 : :
104 : : //Get the stream/URI binding
105 [ # # ][ # # ]: 0 : Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
106 [ # # ]: 0 : if( xUriBinding.is() ) {
107 : : //Register the stream input callbacks into libxml2
108 [ # # ][ # # ]: 0 : if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
109 [ # # ]: 0 : throw RuntimeException() ;
110 : : }
111 : :
112 : : //Get Keys Manager
113 [ # # ]: 0 : Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
114 [ # # ]: 0 : if( !xSecTunnel.is() ) {
115 [ # # ]: 0 : throw RuntimeException() ;
116 : : }
117 : :
118 : : //i39448 : the key manager should be retrieved from SecurityEnvironment, instead of SecurityContext
119 : :
120 : : SecurityEnvironment_NssImpl* pSecEnv =
121 : : reinterpret_cast<SecurityEnvironment_NssImpl*>(
122 : : sal::static_int_cast<sal_uIntPtr>(
123 [ # # ][ # # ]: 0 : xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
[ # # ]
124 [ # # ]: 0 : if( pSecEnv == NULL )
125 [ # # ]: 0 : throw RuntimeException() ;
126 : :
127 [ # # ]: 0 : setErrorRecorder();
128 : :
129 [ # # ]: 0 : pMngr = pSecEnv->createKeysManager() ; //i39448
130 [ # # ]: 0 : if( !pMngr ) {
131 [ # # ]: 0 : throw RuntimeException() ;
132 : : }
133 : :
134 : : //Create Signature context
135 [ # # ]: 0 : pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
136 [ # # ]: 0 : if( pDsigCtx == NULL )
137 : : {
138 [ # # ]: 0 : pSecEnv->destroyKeysManager( pMngr ) ; //i39448
139 : : //throw XMLSignatureException() ;
140 [ # # ]: 0 : clearErrorRecorder();
141 : 0 : return aTemplate;
142 : : }
143 : :
144 : : //Sign the template
145 [ # # ][ # # ]: 0 : if( xmlSecDSigCtxSign( pDsigCtx , pNode ) == 0 )
146 : : {
147 [ # # ]: 0 : if (pDsigCtx->status == xmlSecDSigStatusSucceeded)
148 [ # # ][ # # ]: 0 : aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
149 : : else
150 [ # # ][ # # ]: 0 : aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
151 : : }
152 : : else
153 : : {
154 [ # # ][ # # ]: 0 : aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
155 : : }
156 : :
157 : :
158 [ # # ]: 0 : xmlSecDSigCtxDestroy( pDsigCtx ) ;
159 [ # # ]: 0 : pSecEnv->destroyKeysManager( pMngr ) ; //i39448
160 : :
161 : : //Unregistered the stream/URI binding
162 [ # # ]: 0 : if( xUriBinding.is() )
163 [ # # ]: 0 : xmlUnregisterStreamInputCallbacks() ;
164 : :
165 [ # # ]: 0 : clearErrorRecorder();
166 : 0 : return aTemplate ;
167 : : }
168 : :
169 : : /* XXMLSignature */
170 : : Reference< XXMLSignatureTemplate >
171 : 0 : SAL_CALL XMLSignature_NssImpl :: validate(
172 : : const Reference< XXMLSignatureTemplate >& aTemplate ,
173 : : const Reference< XXMLSecurityContext >& aSecurityCtx
174 : : ) throw( com::sun::star::uno::RuntimeException,
175 : : com::sun::star::uno::SecurityException,
176 : : com::sun::star::xml::crypto::XMLSignatureException ) {
177 : 0 : xmlSecKeysMngrPtr pMngr = NULL ;
178 : 0 : xmlSecDSigCtxPtr pDsigCtx = NULL ;
179 : 0 : xmlNodePtr pNode = NULL ;
180 : : //sal_Bool valid ;
181 : :
182 [ # # ]: 0 : if( !aTemplate.is() )
183 [ # # ]: 0 : throw RuntimeException() ;
184 : :
185 [ # # ]: 0 : if( !aSecurityCtx.is() )
186 [ # # ]: 0 : throw RuntimeException() ;
187 : :
188 : : //Get the xml node
189 [ # # ][ # # ]: 0 : Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
190 [ # # ]: 0 : if( !xElement.is() )
191 [ # # ]: 0 : throw RuntimeException() ;
192 : :
193 [ # # ]: 0 : Reference< XUnoTunnel > xNodTunnel( xElement , UNO_QUERY ) ;
194 [ # # ]: 0 : if( !xNodTunnel.is() ) {
195 [ # # ]: 0 : throw RuntimeException() ;
196 : : }
197 : :
198 : : XMLElementWrapper_XmlSecImpl* pElement =
199 : : reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
200 : : sal::static_int_cast<sal_uIntPtr>(
201 [ # # ][ # # ]: 0 : xNodTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
[ # # ][ # # ]
202 [ # # ]: 0 : if( pElement == NULL )
203 [ # # ]: 0 : throw RuntimeException() ;
204 : :
205 [ # # ]: 0 : pNode = pElement->getNativeElement() ;
206 : :
207 : : //Get the stream/URI binding
208 [ # # ][ # # ]: 0 : Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
209 [ # # ]: 0 : if( xUriBinding.is() ) {
210 : : //Register the stream input callbacks into libxml2
211 [ # # ][ # # ]: 0 : if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
212 [ # # ]: 0 : throw RuntimeException() ;
213 : : }
214 : :
215 [ # # ]: 0 : setErrorRecorder();
216 : :
217 [ # # ][ # # ]: 0 : sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
218 : : sal_Int32 i;
219 : :
220 [ # # ]: 0 : for (i=0; i<nSecurityEnvironment; ++i)
221 : : {
222 [ # # ][ # # ]: 0 : Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
223 : :
224 : : //Get Keys Manager
225 [ # # ]: 0 : Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
226 [ # # ]: 0 : if( !xSecTunnel.is() ) {
227 [ # # ]: 0 : throw RuntimeException() ;
228 : : }
229 : :
230 : : SecurityEnvironment_NssImpl* pSecEnv =
231 : : reinterpret_cast<SecurityEnvironment_NssImpl*>(
232 : : sal::static_int_cast<sal_uIntPtr>(
233 [ # # ][ # # ]: 0 : xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
[ # # ]
234 [ # # ]: 0 : if( pSecEnv == NULL )
235 [ # # ]: 0 : throw RuntimeException() ;
236 : :
237 [ # # ]: 0 : pMngr = pSecEnv->createKeysManager() ; //i39448
238 [ # # ]: 0 : if( !pMngr ) {
239 [ # # ]: 0 : throw RuntimeException() ;
240 : : }
241 : :
242 : : //Create Signature context
243 [ # # ]: 0 : pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
244 [ # # ]: 0 : if( pDsigCtx == NULL )
245 : : {
246 [ # # ]: 0 : pSecEnv->destroyKeysManager( pMngr ) ; //i39448
247 : : //throw XMLSignatureException() ;
248 [ # # ]: 0 : clearErrorRecorder();
249 : 0 : return aTemplate;
250 : : }
251 : :
252 : : //Verify signature
253 [ # # ]: 0 : int rs = xmlSecDSigCtxVerify( pDsigCtx , pNode );
254 : :
255 : :
256 [ # # ][ # # ]: 0 : if (rs == 0 &&
257 : : pDsigCtx->status == xmlSecDSigStatusSucceeded)
258 : : {
259 [ # # ][ # # ]: 0 : aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
260 [ # # ]: 0 : xmlSecDSigCtxDestroy( pDsigCtx ) ;
261 [ # # ]: 0 : pSecEnv->destroyKeysManager( pMngr );
262 : : break;
263 : : }
264 : : else
265 : : {
266 [ # # ][ # # ]: 0 : aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
267 : : }
268 [ # # ]: 0 : xmlSecDSigCtxDestroy( pDsigCtx ) ;
269 [ # # ]: 0 : pSecEnv->destroyKeysManager( pMngr );
[ # # # ]
270 [ # # # ]: 0 : }
271 : :
272 : :
273 : :
274 : : //Unregistered the stream/URI binding
275 [ # # ]: 0 : if( xUriBinding.is() )
276 [ # # ]: 0 : xmlUnregisterStreamInputCallbacks() ;
277 : :
278 : : //return valid ;
279 [ # # ]: 0 : clearErrorRecorder();
280 : 0 : return aTemplate;
281 : : }
282 : :
283 : : /* XInitialization */
284 : 0 : void SAL_CALL XMLSignature_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
285 : : // TBD
286 : 0 : } ;
287 : :
288 : : /* XServiceInfo */
289 : 0 : OUString SAL_CALL XMLSignature_NssImpl :: getImplementationName() throw( RuntimeException ) {
290 : 0 : return impl_getImplementationName() ;
291 : : }
292 : :
293 : : /* XServiceInfo */
294 : 0 : sal_Bool SAL_CALL XMLSignature_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException ) {
295 [ # # ]: 0 : Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
296 : 0 : const OUString* pArray = seqServiceNames.getConstArray() ;
297 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
298 [ # # ]: 0 : if( *( pArray + i ) == serviceName )
299 : 0 : return sal_True ;
300 : : }
301 [ # # ]: 0 : return sal_False ;
302 : : }
303 : :
304 : : /* XServiceInfo */
305 : 0 : Sequence< OUString > SAL_CALL XMLSignature_NssImpl :: getSupportedServiceNames() throw( RuntimeException ) {
306 : 0 : return impl_getSupportedServiceNames() ;
307 : : }
308 : :
309 : : //Helper for XServiceInfo
310 : 0 : Sequence< OUString > XMLSignature_NssImpl :: impl_getSupportedServiceNames() {
311 [ # # ][ # # ]: 0 : ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
312 [ # # ]: 0 : Sequence< OUString > seqServiceNames( 1 ) ;
313 [ # # ]: 0 : seqServiceNames.getArray()[0] = OUString("com.sun.star.xml.crypto.XMLSignature") ;
314 [ # # ]: 0 : return seqServiceNames ;
315 : : }
316 : :
317 : 0 : OUString XMLSignature_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
318 : 0 : return OUString("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl") ;
319 : : }
320 : :
321 : : //Helper for registry
322 : 0 : Reference< XInterface > SAL_CALL XMLSignature_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
323 [ # # ]: 0 : return Reference< XInterface >( *new XMLSignature_NssImpl( aServiceManager ) ) ;
324 : : }
325 : :
326 : 0 : Reference< XSingleServiceFactory > XMLSignature_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
327 : : //Reference< XSingleServiceFactory > xFactory ;
328 : : //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
329 : : //return xFactory ;
330 [ # # ][ # # ]: 0 : return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
331 : : }
332 : :
333 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|