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 43 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl( 44 const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > &rxMSF) 45 :mxMSF( rxMSF ) 46 { 47 } 48 49 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl() 50 { 51 } 52 53 /* XSEInitializer */ 54 cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL 55 SEInitializer_MSCryptImpl::createSecurityContext( 56 const rtl::OUString& sCertDB ) 57 { 58 const char* n_pCertStore ; 59 HCERTSTORE n_hStoreHandle ; 60 61 //Initialize the crypto engine 62 if( sCertDB.getLength() > 0 ) 63 { 64 rtl::OString sCertDir( OUStringToOString( sCertDB, RTL_TEXTENCODING_ASCII_US)); 65 n_pCertStore = sCertDir.getStr(); 66 n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; 67 if( n_hStoreHandle == NULL ) 68 { 69 return NULL; 70 } 71 } 72 else 73 { 74 n_pCertStore = NULL ; 75 n_hStoreHandle = NULL ; 76 } 77 78 xmlSecMSCryptoAppInit( n_pCertStore ) ; 79 80 try { 81 /* Build Security Environment */ 82 const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) ); 83 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY ); 84 if( !xSecEnv.is() ) 85 { 86 if( n_hStoreHandle != NULL ) 87 { 88 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 89 } 90 91 xmlSecMSCryptoAppShutdown() ; 92 return NULL; 93 } 94 95 /* Setup key slot and certDb */ 96 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ; 97 if( !xEnvTunnel.is() ) 98 { 99 if( n_hStoreHandle != NULL ) 100 { 101 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 102 } 103 104 xmlSecMSCryptoAppShutdown() ; 105 return NULL; 106 } 107 108 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; 109 if( pSecEnv == NULL ) 110 { 111 if( n_hStoreHandle != NULL ) 112 { 113 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 114 } 115 116 xmlSecMSCryptoAppShutdown() ; 117 return NULL; 118 } 119 120 if( n_hStoreHandle != NULL ) 121 { 122 pSecEnv->setCryptoSlot( n_hStoreHandle ) ; 123 pSecEnv->setCertDb( n_hStoreHandle ) ; 124 } 125 else 126 { 127 pSecEnv->enableDefaultCrypt( sal_True ) ; 128 } 129 130 /* Build XML Security Context */ 131 const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) ); 132 cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY ); 133 if( !xSecCtx.is() ) 134 { 135 if( n_hStoreHandle != NULL ) 136 { 137 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 138 } 139 140 xmlSecMSCryptoAppShutdown() ; 141 return NULL; 142 } 143 144 xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ; 145 return xSecCtx; 146 } 147 catch( cssu::Exception& ) 148 { 149 if( n_hStoreHandle != NULL ) 150 { 151 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 152 } 153 154 xmlSecMSCryptoAppShutdown() ; 155 return NULL; 156 } 157 } 158 159 void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >&) 160 { 161 /* 162 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv 163 = securityContext->getSecurityEnvironment(); 164 165 if( xSecEnv.is() ) 166 { 167 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ; 168 if( xEnvTunnel.is() ) 169 { 170 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; 171 HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot(); 172 173 if( n_hStoreHandle != NULL ) 174 { 175 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 176 pSecEnv->setCryptoSlot( NULL ) ; 177 pSecEnv->setCertDb( NULL ) ; 178 } 179 180 xmlSecMSCryptoAppShutdown() ; 181 } 182 } 183 */ 184 185 xmlSecMSCryptoAppShutdown() ; 186 } 187 188 rtl::OUString SEInitializer_MSCryptImpl_getImplementationName () 189 { 190 return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) ); 191 } 192 193 sal_Bool SAL_CALL SEInitializer_MSCryptImpl_supportsService( const rtl::OUString& ServiceName ) 194 { 195 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME )); 196 } 197 198 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl_getSupportedServiceNames( ) 199 { 200 cssu::Sequence < rtl::OUString > aRet(1); 201 rtl::OUString* pArray = aRet.getArray(); 202 pArray[0] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) ); 203 return aRet; 204 } 205 #undef SERVICE_NAME 206 207 cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_MSCryptImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr) 208 { 209 return (cppu::OWeakObject*) new SEInitializer_MSCryptImpl(rSMgr); 210 } 211 212 /* XServiceInfo */ 213 rtl::OUString SAL_CALL SEInitializer_MSCryptImpl::getImplementationName( ) 214 { 215 return SEInitializer_MSCryptImpl_getImplementationName(); 216 } 217 sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const rtl::OUString& rServiceName ) 218 { 219 return SEInitializer_MSCryptImpl_supportsService( rServiceName ); 220 } 221 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames( ) 222 { 223 return SEInitializer_MSCryptImpl_getSupportedServiceNames(); 224 } 225