1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include "seinitializer_mscryptimpl.hxx"
32 
33 #include "securityenvironment_mscryptimpl.hxx"
34 
35 #include "xmlsec/strings.h"
36 #include "xmlsec/mscrypto/app.h"
37 
38 namespace cssu = com::sun::star::uno;
39 namespace cssl = com::sun::star::lang;
40 namespace cssxc = com::sun::star::xml::crypto;
41 
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"
46 
47 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
48 	const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > &rxMSF)
49 	:mxMSF( rxMSF )
50 {
51 }
52 
53 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
54 {
55 }
56 
57 /* XSEInitializer */
58 cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL
59 	SEInitializer_MSCryptImpl::createSecurityContext(
60 	const rtl::OUString& sCertDB )
61 	throw (cssu::RuntimeException)
62 {
63 	const char* n_pCertStore ;
64 	HCERTSTORE  n_hStoreHandle ;
65 
66 	//Initialize the crypto engine
67 	if( sCertDB.getLength() > 0 )
68 	{
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 )
73 		{
74 			return NULL;
75 		}
76 	}
77 	else
78 	{
79 		n_pCertStore = NULL ;
80 		n_hStoreHandle = NULL ;
81 	}
82 
83 	xmlSecMSCryptoAppInit( n_pCertStore ) ;
84 
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() )
90 		{
91 			if( n_hStoreHandle != NULL )
92 			{
93 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
94 			}
95 
96 			xmlSecMSCryptoAppShutdown() ;
97 			return NULL;
98 		}
99 
100 		/* Setup key slot and certDb */
101 		cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
102 		if( !xEnvTunnel.is() )
103 		{
104 			if( n_hStoreHandle != NULL )
105 			{
106 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
107 			}
108 
109 			xmlSecMSCryptoAppShutdown() ;
110 			return NULL;
111 		}
112 
113 		SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
114 		if( pSecEnv == NULL )
115 		{
116 			if( n_hStoreHandle != NULL )
117 			{
118 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
119 			}
120 
121 			xmlSecMSCryptoAppShutdown() ;
122 			return NULL;
123 		}
124 
125 		if( n_hStoreHandle != NULL )
126 		{
127 			pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
128 			pSecEnv->setCertDb( n_hStoreHandle ) ;
129 		}
130 		else
131 		{
132 			pSecEnv->enableDefaultCrypt( sal_True ) ;
133 		}
134 
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() )
139 		{
140 			if( n_hStoreHandle != NULL )
141 			{
142 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
143 			}
144 
145 			xmlSecMSCryptoAppShutdown() ;
146 			return NULL;
147 		}
148 
149 		xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ;
150 		return xSecCtx;
151 	}
152 	catch( cssu::Exception& )
153 	{
154 		if( n_hStoreHandle != NULL )
155 		{
156 			CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
157 		}
158 
159 		xmlSecMSCryptoAppShutdown() ;
160 		return NULL;
161 	}
162 }
163 
164 void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >&)
165 	throw (cssu::RuntimeException)
166 {
167 	/*
168 	cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv
169 		= securityContext->getSecurityEnvironment();
170 
171 	if( xSecEnv.is() )
172 	{
173 		cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
174 		if( xEnvTunnel.is() )
175 		{
176 			SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
177 			HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
178 
179 			if( n_hStoreHandle != NULL )
180 			{
181 				CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
182 				pSecEnv->setCryptoSlot( NULL ) ;
183 				pSecEnv->setCertDb( NULL ) ;
184 			}
185 
186 			xmlSecMSCryptoAppShutdown() ;
187 		}
188 	}
189 	*/
190 
191 	xmlSecMSCryptoAppShutdown() ;
192 }
193 
194 rtl::OUString SEInitializer_MSCryptImpl_getImplementationName ()
195 	throw (cssu::RuntimeException)
196 {
197 	return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
198 }
199 
200 sal_Bool SAL_CALL SEInitializer_MSCryptImpl_supportsService( const rtl::OUString& ServiceName )
201 	throw (cssu::RuntimeException)
202 {
203 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
204 }
205 
206 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl_getSupportedServiceNames(  )
207 	throw (cssu::RuntimeException)
208 {
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;
213 }
214 #undef SERVICE_NAME
215 
216 cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_MSCryptImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
217 	throw( cssu::Exception )
218 {
219 	return (cppu::OWeakObject*) new SEInitializer_MSCryptImpl(rSMgr);
220 }
221 
222 /* XServiceInfo */
223 rtl::OUString SAL_CALL SEInitializer_MSCryptImpl::getImplementationName(  )
224 	throw (cssu::RuntimeException)
225 {
226 	return SEInitializer_MSCryptImpl_getImplementationName();
227 }
228 sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const rtl::OUString& rServiceName )
229 	throw (cssu::RuntimeException)
230 {
231 	return SEInitializer_MSCryptImpl_supportsService( rServiceName );
232 }
233 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames(  )
234 	throw (cssu::RuntimeException)
235 {
236 	return SEInitializer_MSCryptImpl_getSupportedServiceNames();
237 }
238 
239