merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / xmlsec / mscrypt / seinitializer_mscryptimpl.cxx
blob43c0bc53cb22637c6dab2065bc0dbdb7f524834d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "seinitializer_mscryptimpl.hxx"
33 #include "securityenvironment_mscryptimpl.hxx"
35 #include "xmlsec/strings.h"
36 #include "xmlsec/mscrypto/app.h"
38 namespace cssu = com::sun::star::uno;
39 namespace cssl = com::sun::star::lang;
40 namespace cssxc = com::sun::star::xml::crypto;
42 #define SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer"
43 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_MSCryptImpl"
44 #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment"
45 #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext"
47 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
48 const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > &rxMSF)
49 :mxMSF( rxMSF )
53 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
57 /* XSEInitializer */
58 cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL
59 SEInitializer_MSCryptImpl::createSecurityContext(
60 const rtl::OUString& sCertDB )
61 throw (cssu::RuntimeException)
63 const char* n_pCertStore ;
64 HCERTSTORE n_hStoreHandle ;
66 //Initialize the crypto engine
67 if( sCertDB.getLength() > 0 )
69 rtl::OString sCertDir(sCertDB, sCertDB.getLength(), RTL_TEXTENCODING_ASCII_US);
70 n_pCertStore = sCertDir.getStr();
71 n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ;
72 if( n_hStoreHandle == NULL )
74 return NULL;
77 else
79 n_pCertStore = NULL ;
80 n_hStoreHandle = NULL ;
83 xmlSecMSCryptoAppInit( n_pCertStore ) ;
85 try {
86 /* Build Security Environment */
87 const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) );
88 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY );
89 if( !xSecEnv.is() )
91 if( n_hStoreHandle != NULL )
93 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
96 xmlSecMSCryptoAppShutdown() ;
97 return NULL;
100 /* Setup key slot and certDb */
101 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
102 if( !xEnvTunnel.is() )
104 if( n_hStoreHandle != NULL )
106 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
109 xmlSecMSCryptoAppShutdown() ;
110 return NULL;
113 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
114 if( pSecEnv == NULL )
116 if( n_hStoreHandle != NULL )
118 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
121 xmlSecMSCryptoAppShutdown() ;
122 return NULL;
125 if( n_hStoreHandle != NULL )
127 pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
128 pSecEnv->setCertDb( n_hStoreHandle ) ;
130 else
132 pSecEnv->enableDefaultCrypt( sal_True ) ;
135 /* Build XML Security Context */
136 const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) );
137 cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY );
138 if( !xSecCtx.is() )
140 if( n_hStoreHandle != NULL )
142 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
145 xmlSecMSCryptoAppShutdown() ;
146 return NULL;
149 xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ;
150 return xSecCtx;
152 catch( cssu::Exception& )
154 if( n_hStoreHandle != NULL )
156 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
159 xmlSecMSCryptoAppShutdown() ;
160 return NULL;
164 void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >&)
165 throw (cssu::RuntimeException)
168 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv
169 = securityContext->getSecurityEnvironment();
171 if( xSecEnv.is() )
173 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
174 if( xEnvTunnel.is() )
176 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
177 HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
179 if( n_hStoreHandle != NULL )
181 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
182 pSecEnv->setCryptoSlot( NULL ) ;
183 pSecEnv->setCertDb( NULL ) ;
186 xmlSecMSCryptoAppShutdown() ;
191 xmlSecMSCryptoAppShutdown() ;
194 rtl::OUString SEInitializer_MSCryptImpl_getImplementationName ()
195 throw (cssu::RuntimeException)
197 return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
200 sal_Bool SAL_CALL SEInitializer_MSCryptImpl_supportsService( const rtl::OUString& ServiceName )
201 throw (cssu::RuntimeException)
203 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
206 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl_getSupportedServiceNames( )
207 throw (cssu::RuntimeException)
209 cssu::Sequence < rtl::OUString > aRet(1);
210 rtl::OUString* pArray = aRet.getArray();
211 pArray[0] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
212 return aRet;
214 #undef SERVICE_NAME
216 cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_MSCryptImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
217 throw( cssu::Exception )
219 return (cppu::OWeakObject*) new SEInitializer_MSCryptImpl(rSMgr);
222 /* XServiceInfo */
223 rtl::OUString SAL_CALL SEInitializer_MSCryptImpl::getImplementationName( )
224 throw (cssu::RuntimeException)
226 return SEInitializer_MSCryptImpl_getImplementationName();
228 sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const rtl::OUString& rServiceName )
229 throw (cssu::RuntimeException)
231 return SEInitializer_MSCryptImpl_supportsService( rServiceName );
233 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames( )
234 throw (cssu::RuntimeException)
236 return SEInitializer_MSCryptImpl_getSupportedServiceNames();