1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlsignature_mscryptimpl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
33 #include <sal/config.h>
36 #include "com/sun/star/xml/crypto/SecurityOperationStatus.hdl"
37 #include "xmlsignature_mscryptimpl.hxx"
39 #ifndef _XMLDOCUMENTWRAPPER_XMLSECIMPL_HXX_
40 #include "xmldocumentwrapper_xmlsecimpl.hxx"
43 #ifndef _XMLELEMENTWRAPPER_XMLSECIMPL_HXX_
44 #include "xmlelementwrapper_xmlsecimpl.hxx"
47 #ifndef _SECURITYENVIRONMENT_MSCRYPTIMPL_HXX_
48 #include "securityenvironment_mscryptimpl.hxx"
50 #include "xmlstreamio.hxx"
51 #include "errorcallback.hxx"
53 #include "xmlsec/xmlsec.h"
54 #include "xmlsec/xmldsig.h"
55 #include "xmlsec/crypto.h"
57 using namespace ::com::sun::star::uno
;
58 using namespace ::com::sun::star::lang
;
59 using ::com::sun::star::lang::XMultiServiceFactory
;
60 using ::com::sun::star::lang::XSingleServiceFactory
;
61 using ::rtl::OUString
;
63 using ::com::sun::star::xml::wrapper::XXMLElementWrapper
;
64 using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper
;
65 using ::com::sun::star::xml::crypto::XSecurityEnvironment
;
66 using ::com::sun::star::xml::crypto::XXMLSignature
;
67 using ::com::sun::star::xml::crypto::XXMLSignatureTemplate
;
68 using ::com::sun::star::xml::crypto::XXMLSecurityContext
;
69 using ::com::sun::star::xml::crypto::XUriBinding
;
70 using ::com::sun::star::xml::crypto::XMLSignatureException
;
73 XMLSignature_MSCryptImpl :: XMLSignature_MSCryptImpl( const Reference
< XMultiServiceFactory
>& aFactory
) : m_xServiceManager( aFactory
) {
76 XMLSignature_MSCryptImpl :: ~XMLSignature_MSCryptImpl() {
80 Reference
< XXMLSignatureTemplate
>
81 SAL_CALL
XMLSignature_MSCryptImpl :: generate(
82 const Reference
< XXMLSignatureTemplate
>& aTemplate
,
83 const Reference
< XSecurityEnvironment
>& aEnvironment
84 ) throw( com::sun::star::xml::crypto::XMLSignatureException
,
85 com::sun::star::uno::SecurityException
)
87 xmlSecKeysMngrPtr pMngr
= NULL
;
88 xmlSecDSigCtxPtr pDsigCtx
= NULL
;
89 xmlNodePtr pNode
= NULL
;
92 throw RuntimeException() ;
94 if( !aEnvironment
.is() )
95 throw RuntimeException() ;
98 Reference
< XUnoTunnel
> xSecTunnel( aEnvironment
, UNO_QUERY
) ;
99 if( !xSecTunnel
.is() ) {
100 throw RuntimeException() ;
103 SecurityEnvironment_MSCryptImpl
* pSecEnv
= ( SecurityEnvironment_MSCryptImpl
* )xSecTunnel
->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
104 if( pSecEnv
== NULL
)
105 throw RuntimeException() ;
108 Reference
< XXMLElementWrapper
> xElement
= aTemplate
->getTemplate() ;
109 if( !xElement
.is() ) {
110 throw RuntimeException() ;
113 Reference
< XUnoTunnel
> xNodTunnel( xElement
, UNO_QUERY
) ;
114 if( !xNodTunnel
.is() ) {
115 throw RuntimeException() ;
118 XMLElementWrapper_XmlSecImpl
* pElement
= ( XMLElementWrapper_XmlSecImpl
* )xNodTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
119 if( pElement
== NULL
) {
120 throw RuntimeException() ;
123 pNode
= pElement
->getNativeElement() ;
125 //Get the stream/URI binding
126 Reference
< XUriBinding
> xUriBinding
= aTemplate
->getBinding() ;
127 if( xUriBinding
.is() ) {
128 //Register the stream input callbacks into libxml2
129 if( xmlRegisterStreamInputCallbacks( xUriBinding
) < 0 )
130 throw RuntimeException() ;
135 pMngr
= pSecEnv
->createKeysManager() ; //i39448
137 throw RuntimeException() ;
140 //Create Signature context
141 pDsigCtx
= xmlSecDSigCtxCreate( pMngr
) ;
142 if( pDsigCtx
== NULL
)
144 //throw XMLSignatureException() ;
145 pSecEnv
->destroyKeysManager( pMngr
) ; //i39448
146 clearErrorRecorder();
151 if( xmlSecDSigCtxSign( pDsigCtx
, pNode
) == 0 )
153 if (pDsigCtx
->status
== xmlSecDSigStatusSucceeded
)
154 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED
);
156 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN
);
160 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN
);
164 xmlSecDSigCtxDestroy( pDsigCtx
) ;
165 pSecEnv
->destroyKeysManager( pMngr
) ; //i39448
167 //Unregistered the stream/URI binding
168 if( xUriBinding
.is() )
169 xmlUnregisterStreamInputCallbacks() ;
171 clearErrorRecorder();
176 Reference
< XXMLSignatureTemplate
>
177 SAL_CALL
XMLSignature_MSCryptImpl :: validate(
178 const Reference
< XXMLSignatureTemplate
>& aTemplate
,
179 const Reference
< XXMLSecurityContext
>& aSecurityCtx
180 ) throw( com::sun::star::uno::RuntimeException
,
181 com::sun::star::uno::SecurityException
,
182 com::sun::star::xml::crypto::XMLSignatureException
) {
183 xmlSecKeysMngrPtr pMngr
= NULL
;
184 xmlSecDSigCtxPtr pDsigCtx
= NULL
;
185 xmlNodePtr pNode
= NULL
;
188 if( !aTemplate
.is() )
189 throw RuntimeException() ;
191 if( !aSecurityCtx
.is() )
192 throw RuntimeException() ;
195 Reference
< XSecurityEnvironment
> xSecEnv
196 = aSecurityCtx
->getSecurityEnvironmentByIndex(
197 aSecurityCtx
->getDefaultSecurityEnvironmentIndex());
198 Reference
< XUnoTunnel
> xSecTunnel( xSecEnv
, UNO_QUERY
) ;
199 if( !xSecTunnel
.is() ) {
200 throw RuntimeException() ;
203 SecurityEnvironment_MSCryptImpl
* pSecEnv
= ( SecurityEnvironment_MSCryptImpl
* )xSecTunnel
->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
204 if( pSecEnv
== NULL
)
205 throw RuntimeException() ;
208 Reference
< XXMLElementWrapper
> xElement
= aTemplate
->getTemplate() ;
210 throw RuntimeException() ;
212 Reference
< XUnoTunnel
> xNodTunnel( xElement
, UNO_QUERY
) ;
213 if( !xNodTunnel
.is() ) {
214 throw RuntimeException() ;
217 XMLElementWrapper_XmlSecImpl
* pElement
= ( XMLElementWrapper_XmlSecImpl
* )xNodTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
218 if( pElement
== NULL
)
219 throw RuntimeException() ;
221 pNode
= pElement
->getNativeElement() ;
223 //Get the stream/URI binding
224 Reference
< XUriBinding
> xUriBinding
= aTemplate
->getBinding() ;
225 if( xUriBinding
.is() ) {
226 //Register the stream input callbacks into libxml2
227 if( xmlRegisterStreamInputCallbacks( xUriBinding
) < 0 )
228 throw RuntimeException() ;
231 //added for test: save the result
234 FILE *dstFile = fopen( "c:\\1.txt", "w" ) ;
235 xmlDocDump( dstFile, pNode->doc) ;
242 pMngr
= pSecEnv
->createKeysManager() ; //i39448
244 throw RuntimeException() ;
247 //Create Signature context
248 pDsigCtx
= xmlSecDSigCtxCreate( pMngr
) ;
249 if( pDsigCtx
== NULL
)
251 pSecEnv
->destroyKeysManager( pMngr
) ; //i39448
252 //throw XMLSignatureException() ;
253 clearErrorRecorder();
258 //The documentation says that the signature is only valid if the return value is 0 (that is, not < 0)
259 //AND pDsigCtx->status == xmlSecDSigStatusSucceeded. That is, we must not make any assumptions, if
260 //the return value is < 0. Then we must regard the signature as INVALID. We cannot use the
261 //error recorder feature to get the ONE error that made the verification fail, because there is no
262 //documentation/specification as to how to interpret the number of recorded errors and what is the initial
264 if( xmlSecDSigCtxVerify( pDsigCtx
, pNode
) == 0 )
266 if (pDsigCtx
->status
== xmlSecDSigStatusSucceeded
)
267 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED
);
269 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN
);
273 aTemplate
->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN
);
276 xmlSecDSigCtxDestroy( pDsigCtx
) ;
277 pSecEnv
->destroyKeysManager( pMngr
) ; //i39448
279 //Unregistered the stream/URI binding
280 if( xUriBinding
.is() )
281 xmlUnregisterStreamInputCallbacks() ;
284 clearErrorRecorder();
288 /* XInitialization */
289 void SAL_CALL
XMLSignature_MSCryptImpl :: initialize( const Sequence
< Any
>& /*aArguments*/ ) throw( Exception
, RuntimeException
) {
294 OUString SAL_CALL
XMLSignature_MSCryptImpl :: getImplementationName() throw( RuntimeException
) {
295 return impl_getImplementationName() ;
299 sal_Bool SAL_CALL
XMLSignature_MSCryptImpl :: supportsService( const OUString
& serviceName
) throw( RuntimeException
) {
300 Sequence
< OUString
> seqServiceNames
= getSupportedServiceNames() ;
301 const OUString
* pArray
= seqServiceNames
.getConstArray() ;
302 for( sal_Int32 i
= 0 ; i
< seqServiceNames
.getLength() ; i
++ ) {
303 if( *( pArray
+ i
) == serviceName
)
310 Sequence
< OUString
> SAL_CALL
XMLSignature_MSCryptImpl :: getSupportedServiceNames() throw( RuntimeException
) {
311 return impl_getSupportedServiceNames() ;
314 //Helper for XServiceInfo
315 Sequence
< OUString
> XMLSignature_MSCryptImpl :: impl_getSupportedServiceNames() {
316 ::osl::Guard
< ::osl::Mutex
> aGuard( ::osl::Mutex::getGlobalMutex() ) ;
317 Sequence
< OUString
> seqServiceNames( 1 ) ;
318 seqServiceNames
.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignature" ) ;
319 return seqServiceNames
;
322 OUString
XMLSignature_MSCryptImpl :: impl_getImplementationName() throw( RuntimeException
) {
323 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSignature_MSCryptImpl" ) ;
326 //Helper for registry
327 Reference
< XInterface
> SAL_CALL
XMLSignature_MSCryptImpl :: impl_createInstance( const Reference
< XMultiServiceFactory
>& aServiceManager
) throw( RuntimeException
) {
328 return Reference
< XInterface
>( *new XMLSignature_MSCryptImpl( aServiceManager
) ) ;
331 Reference
< XSingleServiceFactory
> XMLSignature_MSCryptImpl :: impl_createFactory( const Reference
< XMultiServiceFactory
>& aServiceManager
) {
332 //Reference< XSingleServiceFactory > xFactory ;
333 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
335 return ::cppu::createSingleFactory( aServiceManager
, impl_getImplementationName() , impl_createInstance
, impl_getSupportedServiceNames() ) ;