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