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 "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() {
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
;
69 throw RuntimeException() ;
71 if( !aEnvironment
.is() )
72 throw RuntimeException() ;
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() ;
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() ;
120 pMngr
= pSecEnv
->createKeysManager() ; //i39448
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();
136 if( xmlSecDSigCtxSign( pDsigCtx
, pNode
) == 0 )
138 if (pDsigCtx
->status
== xmlSecDSigStatusSucceeded
)
139 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED
);
141 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN
);
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();
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
;
173 if( !aTemplate
.is() )
174 throw RuntimeException() ;
176 if( !aSecurityCtx
.is() )
177 throw RuntimeException() ;
180 Reference
< XXMLElementWrapper
> xElement
= aTemplate
->getTemplate() ;
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() ;
208 sal_Int32 nSecurityEnvironment
= aSecurityCtx
->getSecurityEnvironmentNumber();
211 for (i
=0; i
<nSecurityEnvironment
; ++i
)
213 Reference
< XSecurityEnvironment
> aEnvironment
= aSecurityCtx
->getSecurityEnvironmentByIndex(i
);
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
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();
244 int rs
= xmlSecDSigCtxVerify( pDsigCtx
, pNode
);
248 pDsigCtx
->status
== xmlSecDSigStatusSucceeded
)
250 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED
);
251 xmlSecDSigCtxDestroy( pDsigCtx
) ;
252 pSecEnv
->destroyKeysManager( pMngr
);
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() ;
270 clearErrorRecorder();
274 /* XInitialization */
275 void SAL_CALL
XMLSignature_NssImpl :: initialize( const Sequence
< Any
>& /*aArguments*/ ) throw( Exception
, RuntimeException
) {
280 OUString SAL_CALL
XMLSignature_NssImpl :: getImplementationName() throw( RuntimeException
) {
281 return impl_getImplementationName() ;
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
)
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 ) ;
321 return ::cppu::createSingleFactory( aServiceManager
, impl_getImplementationName() , impl_createInstance
, impl_getSupportedServiceNames() ) ;
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */