xref: /trunk/main/xmlsecurity/source/xmlsec/mscrypt/seinitializer_mscryptimpl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "seinitializer_mscryptimpl.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "xmlsec/strings.h"
36*cdf0e10cSrcweir #include "xmlsec/mscrypto/app.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
39*cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
40*cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer"
43*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_MSCryptImpl"
44*cdf0e10cSrcweir #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment"
45*cdf0e10cSrcweir #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext"
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
48*cdf0e10cSrcweir     const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > &rxMSF)
49*cdf0e10cSrcweir     :mxMSF( rxMSF )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir /* XSEInitializer */
58*cdf0e10cSrcweir cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL
59*cdf0e10cSrcweir     SEInitializer_MSCryptImpl::createSecurityContext(
60*cdf0e10cSrcweir     const rtl::OUString& sCertDB )
61*cdf0e10cSrcweir     throw (cssu::RuntimeException)
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     const char* n_pCertStore ;
64*cdf0e10cSrcweir     HCERTSTORE  n_hStoreHandle ;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     //Initialize the crypto engine
67*cdf0e10cSrcweir     if( sCertDB.getLength() > 0 )
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         rtl::OString sCertDir(sCertDB, sCertDB.getLength(), RTL_TEXTENCODING_ASCII_US);
70*cdf0e10cSrcweir         n_pCertStore = sCertDir.getStr();
71*cdf0e10cSrcweir         n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ;
72*cdf0e10cSrcweir         if( n_hStoreHandle == NULL )
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir             return NULL;
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir     else
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         n_pCertStore = NULL ;
80*cdf0e10cSrcweir         n_hStoreHandle = NULL ;
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     xmlSecMSCryptoAppInit( n_pCertStore ) ;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     try {
86*cdf0e10cSrcweir         /* Build Security Environment */
87*cdf0e10cSrcweir         const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) );
88*cdf0e10cSrcweir         cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY );
89*cdf0e10cSrcweir         if( !xSecEnv.is() )
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             if( n_hStoreHandle != NULL )
92*cdf0e10cSrcweir             {
93*cdf0e10cSrcweir                 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             xmlSecMSCryptoAppShutdown() ;
97*cdf0e10cSrcweir             return NULL;
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         /* Setup key slot and certDb */
101*cdf0e10cSrcweir         cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
102*cdf0e10cSrcweir         if( !xEnvTunnel.is() )
103*cdf0e10cSrcweir         {
104*cdf0e10cSrcweir             if( n_hStoreHandle != NULL )
105*cdf0e10cSrcweir             {
106*cdf0e10cSrcweir                 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
107*cdf0e10cSrcweir             }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir             xmlSecMSCryptoAppShutdown() ;
110*cdf0e10cSrcweir             return NULL;
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
114*cdf0e10cSrcweir         if( pSecEnv == NULL )
115*cdf0e10cSrcweir         {
116*cdf0e10cSrcweir             if( n_hStoreHandle != NULL )
117*cdf0e10cSrcweir             {
118*cdf0e10cSrcweir                 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir             xmlSecMSCryptoAppShutdown() ;
122*cdf0e10cSrcweir             return NULL;
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         if( n_hStoreHandle != NULL )
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
128*cdf0e10cSrcweir             pSecEnv->setCertDb( n_hStoreHandle ) ;
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir         else
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             pSecEnv->enableDefaultCrypt( sal_True ) ;
133*cdf0e10cSrcweir         }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         /* Build XML Security Context */
136*cdf0e10cSrcweir         const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) );
137*cdf0e10cSrcweir         cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY );
138*cdf0e10cSrcweir         if( !xSecCtx.is() )
139*cdf0e10cSrcweir         {
140*cdf0e10cSrcweir             if( n_hStoreHandle != NULL )
141*cdf0e10cSrcweir             {
142*cdf0e10cSrcweir                 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
143*cdf0e10cSrcweir             }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir             xmlSecMSCryptoAppShutdown() ;
146*cdf0e10cSrcweir             return NULL;
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ;
150*cdf0e10cSrcweir         return xSecCtx;
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir     catch( cssu::Exception& )
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         if( n_hStoreHandle != NULL )
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir             CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         xmlSecMSCryptoAppShutdown() ;
160*cdf0e10cSrcweir         return NULL;
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >&)
165*cdf0e10cSrcweir     throw (cssu::RuntimeException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     /*
168*cdf0e10cSrcweir     cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv
169*cdf0e10cSrcweir         = securityContext->getSecurityEnvironment();
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     if( xSecEnv.is() )
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
174*cdf0e10cSrcweir         if( xEnvTunnel.is() )
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
177*cdf0e10cSrcweir             HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir             if( n_hStoreHandle != NULL )
180*cdf0e10cSrcweir             {
181*cdf0e10cSrcweir                 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
182*cdf0e10cSrcweir                 pSecEnv->setCryptoSlot( NULL ) ;
183*cdf0e10cSrcweir                 pSecEnv->setCertDb( NULL ) ;
184*cdf0e10cSrcweir             }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir             xmlSecMSCryptoAppShutdown() ;
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir     */
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     xmlSecMSCryptoAppShutdown() ;
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir rtl::OUString SEInitializer_MSCryptImpl_getImplementationName ()
195*cdf0e10cSrcweir     throw (cssu::RuntimeException)
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir sal_Bool SAL_CALL SEInitializer_MSCryptImpl_supportsService( const rtl::OUString& ServiceName )
201*cdf0e10cSrcweir     throw (cssu::RuntimeException)
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl_getSupportedServiceNames(  )
207*cdf0e10cSrcweir     throw (cssu::RuntimeException)
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     cssu::Sequence < rtl::OUString > aRet(1);
210*cdf0e10cSrcweir     rtl::OUString* pArray = aRet.getArray();
211*cdf0e10cSrcweir     pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
212*cdf0e10cSrcweir     return aRet;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir #undef SERVICE_NAME
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_MSCryptImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
217*cdf0e10cSrcweir     throw( cssu::Exception )
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir     return (cppu::OWeakObject*) new SEInitializer_MSCryptImpl(rSMgr);
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir /* XServiceInfo */
223*cdf0e10cSrcweir rtl::OUString SAL_CALL SEInitializer_MSCryptImpl::getImplementationName(  )
224*cdf0e10cSrcweir     throw (cssu::RuntimeException)
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir     return SEInitializer_MSCryptImpl_getImplementationName();
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const rtl::OUString& rServiceName )
229*cdf0e10cSrcweir     throw (cssu::RuntimeException)
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     return SEInitializer_MSCryptImpl_supportsService( rServiceName );
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames(  )
234*cdf0e10cSrcweir     throw (cssu::RuntimeException)
235*cdf0e10cSrcweir {
236*cdf0e10cSrcweir     return SEInitializer_MSCryptImpl_getSupportedServiceNames();
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239