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: seinitializer_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"
34 #include "seinitializer_mscryptimpl.hxx"
36 #include "securityenvironment_mscryptimpl.hxx"
38 #include "xmlsec/strings.h"
39 #include "xmlsec/mscrypto/app.h"
41 namespace cssu
= com::sun::star::uno
;
42 namespace cssl
= com::sun::star::lang
;
43 namespace cssxc
= com::sun::star::xml::crypto
;
45 #define SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer"
46 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_MSCryptImpl"
47 #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment"
48 #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext"
50 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
51 const com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
> &rxMSF
)
56 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
61 cssu::Reference
< cssxc::XXMLSecurityContext
> SAL_CALL
62 SEInitializer_MSCryptImpl::createSecurityContext(
63 const rtl::OUString
& sCertDB
)
64 throw (cssu::RuntimeException
)
66 const char* n_pCertStore
;
67 HCERTSTORE n_hStoreHandle
;
69 //Initialize the crypto engine
70 if( sCertDB
.getLength() > 0 )
72 rtl::OString
sCertDir(sCertDB
, sCertDB
.getLength(), RTL_TEXTENCODING_ASCII_US
);
73 n_pCertStore
= sCertDir
.getStr();
74 n_hStoreHandle
= CertOpenSystemStore( NULL
, n_pCertStore
) ;
75 if( n_hStoreHandle
== NULL
)
83 n_hStoreHandle
= NULL
;
86 xmlSecMSCryptoAppInit( n_pCertStore
) ;
89 /* Build Security Environment */
90 const rtl::OUString
sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT
) );
91 cssu::Reference
< cssxc::XSecurityEnvironment
> xSecEnv( mxMSF
->createInstance ( sSecyrutyEnvironment
), cssu::UNO_QUERY
);
94 if( n_hStoreHandle
!= NULL
)
96 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
99 xmlSecMSCryptoAppShutdown() ;
103 /* Setup key slot and certDb */
104 cssu::Reference
< cssl::XUnoTunnel
> xEnvTunnel( xSecEnv
, cssu::UNO_QUERY
) ;
105 if( !xEnvTunnel
.is() )
107 if( n_hStoreHandle
!= NULL
)
109 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
112 xmlSecMSCryptoAppShutdown() ;
116 SecurityEnvironment_MSCryptImpl
* pSecEnv
= ( SecurityEnvironment_MSCryptImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
117 if( pSecEnv
== NULL
)
119 if( n_hStoreHandle
!= NULL
)
121 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
124 xmlSecMSCryptoAppShutdown() ;
128 if( n_hStoreHandle
!= NULL
)
130 pSecEnv
->setCryptoSlot( n_hStoreHandle
) ;
131 pSecEnv
->setCertDb( n_hStoreHandle
) ;
135 pSecEnv
->enableDefaultCrypt( sal_True
) ;
138 /* Build XML Security Context */
139 const rtl::OUString
sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT
) );
140 cssu::Reference
< cssxc::XXMLSecurityContext
> xSecCtx( mxMSF
->createInstance ( sSecyrutyContext
), cssu::UNO_QUERY
);
143 if( n_hStoreHandle
!= NULL
)
145 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
148 xmlSecMSCryptoAppShutdown() ;
152 xSecCtx
->setDefaultSecurityEnvironmentIndex(xSecCtx
->addSecurityEnvironment( xSecEnv
)) ;
155 catch( cssu::Exception
& )
157 if( n_hStoreHandle
!= NULL
)
159 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
162 xmlSecMSCryptoAppShutdown() ;
167 void SAL_CALL
SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference
< cssxc::XXMLSecurityContext
>&)
168 throw (cssu::RuntimeException
)
171 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv
172 = securityContext->getSecurityEnvironment();
176 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
177 if( xEnvTunnel.is() )
179 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
180 HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
182 if( n_hStoreHandle != NULL )
184 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
185 pSecEnv->setCryptoSlot( NULL ) ;
186 pSecEnv->setCertDb( NULL ) ;
189 xmlSecMSCryptoAppShutdown() ;
194 xmlSecMSCryptoAppShutdown() ;
197 rtl::OUString
SEInitializer_MSCryptImpl_getImplementationName ()
198 throw (cssu::RuntimeException
)
200 return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME
) );
203 sal_Bool SAL_CALL
SEInitializer_MSCryptImpl_supportsService( const rtl::OUString
& ServiceName
)
204 throw (cssu::RuntimeException
)
206 return ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME
));
209 cssu::Sequence
< rtl::OUString
> SAL_CALL
SEInitializer_MSCryptImpl_getSupportedServiceNames( )
210 throw (cssu::RuntimeException
)
212 cssu::Sequence
< rtl::OUString
> aRet(1);
213 rtl::OUString
* pArray
= aRet
.getArray();
214 pArray
[0] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME
) );
219 cssu::Reference
< cssu::XInterface
> SAL_CALL
SEInitializer_MSCryptImpl_createInstance( const cssu::Reference
< cssl::XMultiServiceFactory
> & rSMgr
)
220 throw( cssu::Exception
)
222 return (cppu::OWeakObject
*) new SEInitializer_MSCryptImpl(rSMgr
);
226 rtl::OUString SAL_CALL
SEInitializer_MSCryptImpl::getImplementationName( )
227 throw (cssu::RuntimeException
)
229 return SEInitializer_MSCryptImpl_getImplementationName();
231 sal_Bool SAL_CALL
SEInitializer_MSCryptImpl::supportsService( const rtl::OUString
& rServiceName
)
232 throw (cssu::RuntimeException
)
234 return SEInitializer_MSCryptImpl_supportsService( rServiceName
);
236 cssu::Sequence
< rtl::OUString
> SAL_CALL
SEInitializer_MSCryptImpl::getSupportedServiceNames( )
237 throw (cssu::RuntimeException
)
239 return SEInitializer_MSCryptImpl_getSupportedServiceNames();