106b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
306b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
406b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
506b3ce53SAndrew Rist  * distributed with this work for additional information
606b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
706b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
806b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
906b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
1006b3ce53SAndrew Rist  *
1106b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1206b3ce53SAndrew Rist  *
1306b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1406b3ce53SAndrew Rist  * software distributed under the License is distributed on an
1506b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1606b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
1706b3ce53SAndrew Rist  * specific language governing permissions and limitations
1806b3ce53SAndrew Rist  * under the License.
1906b3ce53SAndrew Rist  *
2006b3ce53SAndrew Rist  *************************************************************/
2106b3ce53SAndrew Rist 
2206b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "seinitializer_mscryptimpl.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "xmlsec/strings.h"
32cdf0e10cSrcweir #include "xmlsec/mscrypto/app.h"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
35cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
36cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer"
39cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_MSCryptImpl"
40cdf0e10cSrcweir #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment"
41cdf0e10cSrcweir #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext"
42cdf0e10cSrcweir 
SEInitializer_MSCryptImpl(const com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> & rxMSF)43cdf0e10cSrcweir SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
44cdf0e10cSrcweir 	const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > &rxMSF)
45cdf0e10cSrcweir 	:mxMSF( rxMSF )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
~SEInitializer_MSCryptImpl()49cdf0e10cSrcweir SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /* XSEInitializer */
54cdf0e10cSrcweir cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL
createSecurityContext(const rtl::OUString & sCertDB)55cdf0e10cSrcweir 	SEInitializer_MSCryptImpl::createSecurityContext(
56cdf0e10cSrcweir 	const rtl::OUString& sCertDB )
57cdf0e10cSrcweir 	throw (cssu::RuntimeException)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	const char* n_pCertStore ;
60cdf0e10cSrcweir 	HCERTSTORE  n_hStoreHandle ;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	//Initialize the crypto engine
63cdf0e10cSrcweir 	if( sCertDB.getLength() > 0 )
64cdf0e10cSrcweir 	{
65*24c56ab9SHerbert Dürr 		rtl::OString sCertDir( OUStringToOString( sCertDB, RTL_TEXTENCODING_ASCII_US));
66cdf0e10cSrcweir 		n_pCertStore = sCertDir.getStr();
67cdf0e10cSrcweir 		n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ;
68cdf0e10cSrcweir 		if( n_hStoreHandle == NULL )
69cdf0e10cSrcweir 		{
70cdf0e10cSrcweir 			return NULL;
71cdf0e10cSrcweir 		}
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 	else
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 		n_pCertStore = NULL ;
76cdf0e10cSrcweir 		n_hStoreHandle = NULL ;
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	xmlSecMSCryptoAppInit( n_pCertStore ) ;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	try {
82cdf0e10cSrcweir 		/* Build Security Environment */
83cdf0e10cSrcweir 		const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) );
84cdf0e10cSrcweir 		cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY );
85cdf0e10cSrcweir 		if( !xSecEnv.is() )
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			if( n_hStoreHandle != NULL )
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
90cdf0e10cSrcweir 			}
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			xmlSecMSCryptoAppShutdown() ;
93cdf0e10cSrcweir 			return NULL;
94cdf0e10cSrcweir 		}
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 		/* Setup key slot and certDb */
97cdf0e10cSrcweir 		cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
98cdf0e10cSrcweir 		if( !xEnvTunnel.is() )
99cdf0e10cSrcweir 		{
100cdf0e10cSrcweir 			if( n_hStoreHandle != NULL )
101cdf0e10cSrcweir 			{
102cdf0e10cSrcweir 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
103cdf0e10cSrcweir 			}
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 			xmlSecMSCryptoAppShutdown() ;
106cdf0e10cSrcweir 			return NULL;
107cdf0e10cSrcweir 		}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
110cdf0e10cSrcweir 		if( pSecEnv == NULL )
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			if( n_hStoreHandle != NULL )
113cdf0e10cSrcweir 			{
114cdf0e10cSrcweir 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 			xmlSecMSCryptoAppShutdown() ;
118cdf0e10cSrcweir 			return NULL;
119cdf0e10cSrcweir 		}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		if( n_hStoreHandle != NULL )
122cdf0e10cSrcweir 		{
123cdf0e10cSrcweir 			pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
124cdf0e10cSrcweir 			pSecEnv->setCertDb( n_hStoreHandle ) ;
125cdf0e10cSrcweir 		}
126cdf0e10cSrcweir 		else
127cdf0e10cSrcweir 		{
128cdf0e10cSrcweir 			pSecEnv->enableDefaultCrypt( sal_True ) ;
129cdf0e10cSrcweir 		}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 		/* Build XML Security Context */
132cdf0e10cSrcweir 		const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) );
133cdf0e10cSrcweir 		cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY );
134cdf0e10cSrcweir 		if( !xSecCtx.is() )
135cdf0e10cSrcweir 		{
136cdf0e10cSrcweir 			if( n_hStoreHandle != NULL )
137cdf0e10cSrcweir 			{
138cdf0e10cSrcweir 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
139cdf0e10cSrcweir 			}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 			xmlSecMSCryptoAppShutdown() ;
142cdf0e10cSrcweir 			return NULL;
143cdf0e10cSrcweir 		}
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 		xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ;
146cdf0e10cSrcweir 		return xSecCtx;
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 	catch( cssu::Exception& )
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		if( n_hStoreHandle != NULL )
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
153cdf0e10cSrcweir 		}
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 		xmlSecMSCryptoAppShutdown() ;
156cdf0e10cSrcweir 		return NULL;
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
freeSecurityContext(const cssu::Reference<cssxc::XXMLSecurityContext> &)160cdf0e10cSrcweir void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >&)
161cdf0e10cSrcweir 	throw (cssu::RuntimeException)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	/*
164cdf0e10cSrcweir 	cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv
165cdf0e10cSrcweir 		= securityContext->getSecurityEnvironment();
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	if( xSecEnv.is() )
168cdf0e10cSrcweir 	{
169cdf0e10cSrcweir 		cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
170cdf0e10cSrcweir 		if( xEnvTunnel.is() )
171cdf0e10cSrcweir 		{
172cdf0e10cSrcweir 			SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
173cdf0e10cSrcweir 			HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 			if( n_hStoreHandle != NULL )
176cdf0e10cSrcweir 			{
177cdf0e10cSrcweir 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
178cdf0e10cSrcweir 				pSecEnv->setCryptoSlot( NULL ) ;
179cdf0e10cSrcweir 				pSecEnv->setCertDb( NULL ) ;
180cdf0e10cSrcweir 			}
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 			xmlSecMSCryptoAppShutdown() ;
183cdf0e10cSrcweir 		}
184cdf0e10cSrcweir 	}
185cdf0e10cSrcweir 	*/
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	xmlSecMSCryptoAppShutdown() ;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
SEInitializer_MSCryptImpl_getImplementationName()190cdf0e10cSrcweir rtl::OUString SEInitializer_MSCryptImpl_getImplementationName ()
191cdf0e10cSrcweir 	throw (cssu::RuntimeException)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
SEInitializer_MSCryptImpl_supportsService(const rtl::OUString & ServiceName)196cdf0e10cSrcweir sal_Bool SAL_CALL SEInitializer_MSCryptImpl_supportsService( const rtl::OUString& ServiceName )
197cdf0e10cSrcweir 	throw (cssu::RuntimeException)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
SEInitializer_MSCryptImpl_getSupportedServiceNames()202cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl_getSupportedServiceNames(  )
203cdf0e10cSrcweir 	throw (cssu::RuntimeException)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	cssu::Sequence < rtl::OUString > aRet(1);
206cdf0e10cSrcweir 	rtl::OUString* pArray = aRet.getArray();
207cdf0e10cSrcweir 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
208cdf0e10cSrcweir 	return aRet;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir #undef SERVICE_NAME
211cdf0e10cSrcweir 
SEInitializer_MSCryptImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)212cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_MSCryptImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
213cdf0e10cSrcweir 	throw( cssu::Exception )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	return (cppu::OWeakObject*) new SEInitializer_MSCryptImpl(rSMgr);
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir /* XServiceInfo */
getImplementationName()219cdf0e10cSrcweir rtl::OUString SAL_CALL SEInitializer_MSCryptImpl::getImplementationName(  )
220cdf0e10cSrcweir 	throw (cssu::RuntimeException)
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	return SEInitializer_MSCryptImpl_getImplementationName();
223cdf0e10cSrcweir }
supportsService(const rtl::OUString & rServiceName)224cdf0e10cSrcweir sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const rtl::OUString& rServiceName )
225cdf0e10cSrcweir 	throw (cssu::RuntimeException)
226cdf0e10cSrcweir {
227cdf0e10cSrcweir 	return SEInitializer_MSCryptImpl_supportsService( rServiceName );
228cdf0e10cSrcweir }
getSupportedServiceNames()229cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames(  )
230cdf0e10cSrcweir 	throw (cssu::RuntimeException)
231cdf0e10cSrcweir {
232cdf0e10cSrcweir 	return SEInitializer_MSCryptImpl_getSupportedServiceNames();
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235