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: xmlsecuritycontext_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>
35 #include "securityenvironment_mscryptimpl.hxx"
37 #ifndef _XMLSECURITYCONTEXT_MSCRYPTIMPL_HXX_
38 #include "xmlsecuritycontext_mscryptimpl.hxx"
40 #include "xmlstreamio.hxx"
42 #include "xmlsec/xmlsec.h"
43 #include "xmlsec/keysmngr.h"
44 #include "xmlsec/crypto.h"
45 #include "xmlsec/mscrypto/akmngr.h"
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::lang
;
49 using ::com::sun::star::lang::XMultiServiceFactory
;
50 using ::com::sun::star::lang::XSingleServiceFactory
;
51 using ::rtl::OUString
;
53 using ::com::sun::star::xml::crypto::XSecurityEnvironment
;
54 using ::com::sun::star::xml::crypto::XXMLSecurityContext
;
56 XMLSecurityContext_MSCryptImpl :: XMLSecurityContext_MSCryptImpl( const Reference
< XMultiServiceFactory
>& aFactory
)
57 ://m_pKeysMngr( NULL ) ,
58 m_xServiceManager( aFactory
),
59 m_xSecurityEnvironment( NULL
)
62 if( xmlSecInit() < 0 ) {
63 throw RuntimeException() ;
66 //Init xmlsec crypto engine library
67 if( xmlSecCryptoInit() < 0 ) {
69 throw RuntimeException() ;
72 //Enable external stream handlers
73 if( xmlEnableStreamInputCallbacks() < 0 ) {
74 xmlSecCryptoShutdown() ;
76 throw RuntimeException() ;
80 XMLSecurityContext_MSCryptImpl :: ~XMLSecurityContext_MSCryptImpl() {
81 xmlDisableStreamInputCallbacks() ;
82 xmlSecCryptoShutdown() ;
86 //i39448 : new methods
87 sal_Int32 SAL_CALL
XMLSecurityContext_MSCryptImpl::addSecurityEnvironment(
88 const ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
>& aSecurityEnvironment
)
89 throw (::com::sun::star::security::SecurityInfrastructureException
, ::com::sun::star::uno::RuntimeException
)
91 if( !aSecurityEnvironment
.is() )
93 throw RuntimeException() ;
96 m_xSecurityEnvironment
= aSecurityEnvironment
;
102 sal_Int32 SAL_CALL
XMLSecurityContext_MSCryptImpl::getSecurityEnvironmentNumber( )
103 throw (::com::sun::star::uno::RuntimeException
)
108 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> SAL_CALL
109 XMLSecurityContext_MSCryptImpl::getSecurityEnvironmentByIndex( sal_Int32 index
)
110 throw (::com::sun::star::uno::RuntimeException
)
114 return m_xSecurityEnvironment
;
117 throw RuntimeException() ;
120 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> SAL_CALL
121 XMLSecurityContext_MSCryptImpl::getSecurityEnvironment( )
122 throw (::com::sun::star::uno::RuntimeException
)
124 return m_xSecurityEnvironment
;
127 sal_Int32 SAL_CALL
XMLSecurityContext_MSCryptImpl::getDefaultSecurityEnvironmentIndex( )
128 throw (::com::sun::star::uno::RuntimeException
)
133 void SAL_CALL
XMLSecurityContext_MSCryptImpl::setDefaultSecurityEnvironmentIndex( sal_Int32
/*nDefaultEnvIndex*/ )
134 throw (::com::sun::star::uno::RuntimeException
)
140 /* XXMLSecurityContext */
141 void SAL_CALL
XMLSecurityContext_MSCryptImpl :: setSecurityEnvironment( const Reference
< XSecurityEnvironment
>& aSecurityEnvironment
) throw( com::sun::star::security::SecurityInfrastructureException
) {
142 HCERTSTORE hkeyStore
;
143 HCERTSTORE hCertStore
;
149 if( !aSecurityEnvironment
.is() )
150 throw RuntimeException() ;
152 m_xSecurityEnvironment
= aSecurityEnvironment
;
155 if( m_pKeysMngr
!= NULL
) {
156 xmlSecKeysMngrDestroy( m_pKeysMngr
) ;
161 Reference
< XUnoTunnel
> xEnvTunnel( m_xSecurityEnvironment
, UNO_QUERY
) ;
162 if( !xEnvTunnel
.is() ) {
163 throw RuntimeException() ;
166 SecurityEnvironment_MSCryptImpl
* pSecEnv
= ( SecurityEnvironment_MSCryptImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
167 if( pSecEnv
== NULL
)
168 throw RuntimeException() ;
170 hkeyStore
= pSecEnv
->getCryptoSlot() ;
171 hCertStore
= pSecEnv
->getCertDb() ;
174 * The following lines is based on the of xmlsec-mscrypto crypto engine
176 m_pKeysMngr
= xmlSecMSCryptoAppliedKeysMngrCreate( hkeyStore
, hCertStore
) ;
177 if( m_pKeysMngr
== NULL
)
178 throw RuntimeException() ;
181 * Adopt symmetric key into keys manager
183 for( i
= 0 ; ( symKey
= pSecEnv
->getSymKey( i
) ) != NULL
; i
++ ) {
184 if( xmlSecMSCryptoAppliedKeysMngrSymKeyLoad( m_pKeysMngr
, symKey
) < 0 ) {
185 throw RuntimeException() ;
190 * Adopt asymmetric public key into keys manager
192 for( i
= 0 ; ( pubKey
= pSecEnv
->getPubKey( i
) ) != NULL
; i
++ ) {
193 if( xmlSecMSCryptoAppliedKeysMngrPubKeyLoad( m_pKeysMngr
, pubKey
) < 0 ) {
194 throw RuntimeException() ;
199 * Adopt asymmetric private key into keys manager
201 for( i
= 0 ; ( priKey
= pSecEnv
->getPriKey( i
) ) != NULL
; i
++ ) {
202 if( xmlSecMSCryptoAppliedKeysMngrPriKeyLoad( m_pKeysMngr
, priKey
) < 0 ) {
203 throw RuntimeException() ;
208 * Adopt system default certificate store.
210 if( pSecEnv
->defaultEnabled() ) {
211 HCERTSTORE hSystemStore
;
213 //Add system key store into the keys manager.
214 hSystemStore
= CertOpenSystemStore( 0, "MY" ) ;
215 if( hSystemStore
!= NULL
) {
216 if( xmlSecMSCryptoAppliedKeysMngrAdoptKeyStore( m_pKeysMngr
, hSystemStore
) < 0 ) {
217 CertCloseStore( hSystemStore
, CERT_CLOSE_STORE_CHECK_FLAG
) ;
218 throw RuntimeException() ;
222 //Add system root store into the keys manager.
223 hSystemStore
= CertOpenSystemStore( 0, "Root" ) ;
224 if( hSystemStore
!= NULL
) {
225 if( xmlSecMSCryptoAppliedKeysMngrAdoptTrustedStore( m_pKeysMngr
, hSystemStore
) < 0 ) {
226 CertCloseStore( hSystemStore
, CERT_CLOSE_STORE_CHECK_FLAG
) ;
227 throw RuntimeException() ;
231 //Add system trusted store into the keys manager.
232 hSystemStore
= CertOpenSystemStore( 0, "Trust" ) ;
233 if( hSystemStore
!= NULL
) {
234 if( xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore( m_pKeysMngr
, hSystemStore
) < 0 ) {
235 CertCloseStore( hSystemStore
, CERT_CLOSE_STORE_CHECK_FLAG
) ;
236 throw RuntimeException() ;
240 //Add system CA store into the keys manager.
241 hSystemStore
= CertOpenSystemStore( 0, "CA" ) ;
242 if( hSystemStore
!= NULL
) {
243 if( xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore( m_pKeysMngr
, hSystemStore
) < 0 ) {
244 CertCloseStore( hSystemStore
, CERT_CLOSE_STORE_CHECK_FLAG
) ;
245 throw RuntimeException() ;
251 /* XXMLSecurityContext */
252 Reference
< XSecurityEnvironment
> SAL_CALL
XMLSecurityContext_MSCryptImpl :: getSecurityEnvironment()
253 throw (RuntimeException
)
255 return m_xSecurityEnvironment
;
259 /* XInitialization */
260 void SAL_CALL
XMLSecurityContext_MSCryptImpl :: initialize( const Sequence
< Any
>& /*aArguments*/ ) throw( Exception
, RuntimeException
) {
265 OUString SAL_CALL
XMLSecurityContext_MSCryptImpl :: getImplementationName() throw( RuntimeException
) {
266 return impl_getImplementationName() ;
270 sal_Bool SAL_CALL
XMLSecurityContext_MSCryptImpl :: supportsService( const OUString
& serviceName
) throw( RuntimeException
) {
271 Sequence
< OUString
> seqServiceNames
= getSupportedServiceNames() ;
272 const OUString
* pArray
= seqServiceNames
.getConstArray() ;
273 for( sal_Int32 i
= 0 ; i
< seqServiceNames
.getLength() ; i
++ ) {
274 if( *( pArray
+ i
) == serviceName
)
281 Sequence
< OUString
> SAL_CALL
XMLSecurityContext_MSCryptImpl :: getSupportedServiceNames() throw( RuntimeException
) {
282 return impl_getSupportedServiceNames() ;
285 //Helper for XServiceInfo
286 Sequence
< OUString
> XMLSecurityContext_MSCryptImpl :: impl_getSupportedServiceNames() {
287 ::osl::Guard
< ::osl::Mutex
> aGuard( ::osl::Mutex::getGlobalMutex() ) ;
288 Sequence
< OUString
> seqServiceNames( 1 ) ;
289 seqServiceNames
.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
290 return seqServiceNames
;
293 OUString
XMLSecurityContext_MSCryptImpl :: impl_getImplementationName() throw( RuntimeException
) {
294 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl" ) ;
297 //Helper for registry
298 Reference
< XInterface
> SAL_CALL
XMLSecurityContext_MSCryptImpl :: impl_createInstance( const Reference
< XMultiServiceFactory
>& aServiceManager
) throw( RuntimeException
) {
299 return Reference
< XInterface
>( *new XMLSecurityContext_MSCryptImpl( aServiceManager
) ) ;
302 Reference
< XSingleServiceFactory
> XMLSecurityContext_MSCryptImpl :: impl_createFactory( const Reference
< XMultiServiceFactory
>& aServiceManager
) {
303 //Reference< XSingleServiceFactory > xFactory ;
304 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
306 return ::cppu::createSingleFactory( aServiceManager
, impl_getImplementationName() , impl_createInstance
, impl_getSupportedServiceNames() ) ;
311 sal_Int64 SAL_CALL
XMLSecurityContext_MSCryptImpl :: getSomething( const Sequence
< sal_Int8
>& aIdentifier
)
312 throw (RuntimeException
)
314 if( aIdentifier
.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier
.getConstArray(), 16 ) ) {
315 return ( sal_Int64
)this ;
320 /* XUnoTunnel extension */
321 const Sequence
< sal_Int8
>& XMLSecurityContext_MSCryptImpl :: getUnoTunnelId() {
322 static Sequence
< sal_Int8
>* pSeq
= 0 ;
324 ::osl::Guard
< ::osl::Mutex
> aGuard( ::osl::Mutex::getGlobalMutex() ) ;
326 static Sequence
< sal_Int8
> aSeq( 16 ) ;
327 rtl_createUuid( ( sal_uInt8
* )aSeq
.getArray() , 0 , sal_True
) ;
334 /* XUnoTunnel extension */
335 XMLSecurityContext_MSCryptImpl
* XMLSecurityContext_MSCryptImpl :: getImplementation( const Reference
< XInterface
> xObj
) {
336 Reference
< XUnoTunnel
> xUT( xObj
, UNO_QUERY
) ;
338 return ( XMLSecurityContext_MSCryptImpl
* )xUT
->getSomething( getUnoTunnelId() ) ;
344 xmlSecKeysMngrPtr
XMLSecurityContext_MSCryptImpl :: keysManager() throw( Exception
, RuntimeException
) {