bump product version to 4.1.6.2
[LibreOffice.git] / xmlsecurity / source / xmlsec / nss / xmlsignature_nssimpl.cxx
blob9d7029c7b21205a786d96e894ddda30d90645071
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <rtl/uuid.h>
22 #include "xmlsignature_nssimpl.hxx"
24 #include "xmldocumentwrapper_xmlsecimpl.hxx"
26 #include "xmlelementwrapper_xmlsecimpl.hxx"
28 #include "securityenvironment_nssimpl.hxx"
30 #include "xmlsecuritycontext_nssimpl.hxx"
31 #include "xmlstreamio.hxx"
32 #include "errorcallback.hxx"
34 #include "xmlsecurity/xmlsec-wrapper.h"
36 using namespace ::com::sun::star::uno ;
37 using namespace ::com::sun::star::lang ;
38 using ::com::sun::star::lang::XMultiServiceFactory ;
39 using ::com::sun::star::lang::XSingleServiceFactory ;
41 using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
42 using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper ;
43 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
44 using ::com::sun::star::xml::crypto::XXMLSignature ;
45 using ::com::sun::star::xml::crypto::XXMLSignatureTemplate ;
46 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
47 using ::com::sun::star::xml::crypto::XUriBinding ;
48 using ::com::sun::star::xml::crypto::XMLSignatureException ;
50 XMLSignature_NssImpl :: XMLSignature_NssImpl( const Reference< XMultiServiceFactory >& aFactory ) : m_xServiceManager( aFactory ) {
53 XMLSignature_NssImpl :: ~XMLSignature_NssImpl() {
56 /* XXMLSignature */
57 Reference< XXMLSignatureTemplate >
58 SAL_CALL XMLSignature_NssImpl :: generate(
59 const Reference< XXMLSignatureTemplate >& aTemplate ,
60 const Reference< XSecurityEnvironment >& aEnvironment
61 ) throw( com::sun::star::xml::crypto::XMLSignatureException,
62 com::sun::star::uno::SecurityException )
64 xmlSecKeysMngrPtr pMngr = NULL ;
65 xmlSecDSigCtxPtr pDsigCtx = NULL ;
66 xmlNodePtr pNode = NULL ;
68 if( !aTemplate.is() )
69 throw RuntimeException() ;
71 if( !aEnvironment.is() )
72 throw RuntimeException() ;
74 //Get the xml node
75 Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
76 if( !xElement.is() ) {
77 throw RuntimeException() ;
80 Reference< XUnoTunnel > xNodTunnel( xElement , UNO_QUERY ) ;
81 if( !xNodTunnel.is() ) {
82 throw RuntimeException() ;
85 XMLElementWrapper_XmlSecImpl* pElement =
86 reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
87 sal::static_int_cast<sal_uIntPtr>(
88 xNodTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
89 if( pElement == NULL ) {
90 throw RuntimeException() ;
93 pNode = pElement->getNativeElement() ;
95 //Get the stream/URI binding
96 Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
97 if( xUriBinding.is() ) {
98 //Register the stream input callbacks into libxml2
99 if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
100 throw RuntimeException() ;
103 //Get Keys Manager
104 Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
105 if( !xSecTunnel.is() ) {
106 throw RuntimeException() ;
109 //i39448 : the key manager should be retrieved from SecurityEnvironment, instead of SecurityContext
111 SecurityEnvironment_NssImpl* pSecEnv =
112 reinterpret_cast<SecurityEnvironment_NssImpl*>(
113 sal::static_int_cast<sal_uIntPtr>(
114 xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
115 if( pSecEnv == NULL )
116 throw RuntimeException() ;
118 setErrorRecorder();
120 pMngr = pSecEnv->createKeysManager() ; //i39448
121 if( !pMngr ) {
122 throw RuntimeException() ;
125 //Create Signature context
126 pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
127 if( pDsigCtx == NULL )
129 pSecEnv->destroyKeysManager( pMngr ) ; //i39448
130 //throw XMLSignatureException() ;
131 clearErrorRecorder();
132 return aTemplate;
135 //Sign the template
136 if( xmlSecDSigCtxSign( pDsigCtx , pNode ) == 0 )
138 if (pDsigCtx->status == xmlSecDSigStatusSucceeded)
139 aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
140 else
141 aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
143 else
145 aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
149 xmlSecDSigCtxDestroy( pDsigCtx ) ;
150 pSecEnv->destroyKeysManager( pMngr ) ; //i39448
152 //Unregistered the stream/URI binding
153 if( xUriBinding.is() )
154 xmlUnregisterStreamInputCallbacks() ;
156 clearErrorRecorder();
157 return aTemplate ;
160 /* XXMLSignature */
161 Reference< XXMLSignatureTemplate >
162 SAL_CALL XMLSignature_NssImpl :: validate(
163 const Reference< XXMLSignatureTemplate >& aTemplate ,
164 const Reference< XXMLSecurityContext >& aSecurityCtx
165 ) throw( com::sun::star::uno::RuntimeException,
166 com::sun::star::uno::SecurityException,
167 com::sun::star::xml::crypto::XMLSignatureException ) {
168 xmlSecKeysMngrPtr pMngr = NULL ;
169 xmlSecDSigCtxPtr pDsigCtx = NULL ;
170 xmlNodePtr pNode = NULL ;
171 //sal_Bool valid ;
173 if( !aTemplate.is() )
174 throw RuntimeException() ;
176 if( !aSecurityCtx.is() )
177 throw RuntimeException() ;
179 //Get the xml node
180 Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
181 if( !xElement.is() )
182 throw RuntimeException() ;
184 Reference< XUnoTunnel > xNodTunnel( xElement , UNO_QUERY ) ;
185 if( !xNodTunnel.is() ) {
186 throw RuntimeException() ;
189 XMLElementWrapper_XmlSecImpl* pElement =
190 reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
191 sal::static_int_cast<sal_uIntPtr>(
192 xNodTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
193 if( pElement == NULL )
194 throw RuntimeException() ;
196 pNode = pElement->getNativeElement() ;
198 //Get the stream/URI binding
199 Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
200 if( xUriBinding.is() ) {
201 //Register the stream input callbacks into libxml2
202 if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
203 throw RuntimeException() ;
206 setErrorRecorder();
208 sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
209 sal_Int32 i;
211 for (i=0; i<nSecurityEnvironment; ++i)
213 Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
215 //Get Keys Manager
216 Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
217 if( !xSecTunnel.is() ) {
218 throw RuntimeException() ;
221 SecurityEnvironment_NssImpl* pSecEnv =
222 reinterpret_cast<SecurityEnvironment_NssImpl*>(
223 sal::static_int_cast<sal_uIntPtr>(
224 xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
225 if( pSecEnv == NULL )
226 throw RuntimeException() ;
228 pMngr = pSecEnv->createKeysManager() ; //i39448
229 if( !pMngr ) {
230 throw RuntimeException() ;
233 //Create Signature context
234 pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
235 if( pDsigCtx == NULL )
237 pSecEnv->destroyKeysManager( pMngr ) ; //i39448
238 //throw XMLSignatureException() ;
239 clearErrorRecorder();
240 return aTemplate;
243 //Verify signature
244 int rs = xmlSecDSigCtxVerify( pDsigCtx , pNode );
247 if (rs == 0 &&
248 pDsigCtx->status == xmlSecDSigStatusSucceeded)
250 aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
251 xmlSecDSigCtxDestroy( pDsigCtx ) ;
252 pSecEnv->destroyKeysManager( pMngr );
253 break;
255 else
257 aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
259 xmlSecDSigCtxDestroy( pDsigCtx ) ;
260 pSecEnv->destroyKeysManager( pMngr );
265 //Unregistered the stream/URI binding
266 if( xUriBinding.is() )
267 xmlUnregisterStreamInputCallbacks() ;
269 //return valid ;
270 clearErrorRecorder();
271 return aTemplate;
274 /* XInitialization */
275 void SAL_CALL XMLSignature_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
276 // TBD
279 /* XServiceInfo */
280 OUString SAL_CALL XMLSignature_NssImpl :: getImplementationName() throw( RuntimeException ) {
281 return impl_getImplementationName() ;
284 /* XServiceInfo */
285 sal_Bool SAL_CALL XMLSignature_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException ) {
286 Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
287 const OUString* pArray = seqServiceNames.getConstArray() ;
288 for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
289 if( *( pArray + i ) == serviceName )
290 return sal_True ;
292 return sal_False ;
295 /* XServiceInfo */
296 Sequence< OUString > SAL_CALL XMLSignature_NssImpl :: getSupportedServiceNames() throw( RuntimeException ) {
297 return impl_getSupportedServiceNames() ;
300 //Helper for XServiceInfo
301 Sequence< OUString > XMLSignature_NssImpl :: impl_getSupportedServiceNames() {
302 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
303 Sequence< OUString > seqServiceNames( 1 ) ;
304 seqServiceNames.getArray()[0] = OUString("com.sun.star.xml.crypto.XMLSignature") ;
305 return seqServiceNames ;
308 OUString XMLSignature_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
309 return OUString("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl") ;
312 //Helper for registry
313 Reference< XInterface > SAL_CALL XMLSignature_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
314 return Reference< XInterface >( *new XMLSignature_NssImpl( aServiceManager ) ) ;
317 Reference< XSingleServiceFactory > XMLSignature_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
318 //Reference< XSingleServiceFactory > xFactory ;
319 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
320 //return xFactory ;
321 return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */