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 /* 28 * Turn off DEBUG Assertions 29 */ 30 #ifdef _DEBUG 31 #define _DEBUG_WAS_DEFINED _DEBUG 32 #undef _DEBUG 33 #else 34 #undef _DEBUG_WAS_DEFINED 35 #endif 36 37 /* 38 * and turn off the additional virtual methods which are part of some interfaces when compiled 39 * with debug 40 */ 41 #ifdef DEBUG 42 #define DEBUG_WAS_DEFINED DEBUG 43 #undef DEBUG 44 #else 45 #undef DEBUG_WAS_DEFINED 46 #endif 47 48 #include <sal/types.h> 49 #include <rtl/bootstrap.hxx> 50 #include <rtl/string.hxx> 51 #include <rtl/strbuf.hxx> 52 #include <osl/file.hxx> 53 #include <osl/thread.h> 54 #include <tools/debug.hxx> 55 #include <rtl/logfile.hxx> 56 57 #include "seinitializer_nssimpl.hxx" 58 #include "securityenvironment_nssimpl.hxx" 59 60 #include <nspr.h> 61 #include <cert.h> 62 #include <nss.h> 63 #include <pk11pub.h> 64 #include <secmod.h> 65 #include <nssckbi.h> 66 67 68 namespace css = ::com::sun::star; 69 namespace cssu = css::uno; 70 namespace cssl = css::lang; 71 namespace cssxc = css::xml::crypto; 72 73 using namespace com::sun::star; 74 using ::rtl::OUString; 75 using ::rtl::OString; 76 77 #define SE_SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer" 78 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_NssImpl" 79 #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment" 80 #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext" 81 SEInitializer_NssImpl(const css::uno::Reference<css::lang::XMultiServiceFactory> & rxMSF)82SEInitializer_NssImpl::SEInitializer_NssImpl( 83 const css::uno::Reference< css::lang::XMultiServiceFactory > &rxMSF ) 84 { 85 mxMSF = rxMSF; 86 } 87 ~SEInitializer_NssImpl()88SEInitializer_NssImpl::~SEInitializer_NssImpl() 89 { 90 } 91 92 /* XSEInitializer */ 93 cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL createSecurityContext(const::rtl::OUString &)94 SEInitializer_NssImpl::createSecurityContext( const ::rtl::OUString& ) 95 { 96 CERTCertDBHandle *pCertHandle = NULL ; 97 98 if( !initNSS( mxMSF ) ) 99 return NULL; 100 101 pCertHandle = CERT_GetDefaultCertDB() ; 102 103 try 104 { 105 /* Build XML Security Context */ 106 const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) ); 107 cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY ); 108 if( !xSecCtx.is() ) 109 return NULL; 110 111 const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) ); 112 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY ); 113 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ; 114 if( !xEnvTunnel.is() ) 115 return NULL; 116 SecurityEnvironment_NssImpl* pSecEnv = reinterpret_cast<SecurityEnvironment_NssImpl*>( 117 sal::static_int_cast<sal_uIntPtr>( 118 xEnvTunnel->getSomething(SecurityEnvironment_NssImpl::getUnoTunnelId() ))) ; 119 pSecEnv->setCertDb(pCertHandle); 120 121 sal_Int32 n = xSecCtx->addSecurityEnvironment(xSecEnv); 122 //originally the SecurityEnvironment with the internal slot was set as default 123 xSecCtx->setDefaultSecurityEnvironmentIndex( n ); 124 return xSecCtx; 125 } 126 catch( cssu::Exception& ) 127 { 128 //PK11_LogoutAll(); 129 //NSS_Shutdown(); 130 return NULL; 131 } 132 } 133 freeSecurityContext(const cssu::Reference<cssxc::XXMLSecurityContext> &)134void SAL_CALL SEInitializer_NssImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >& ) 135 { 136 /* 137 * because the security context will free all its content when it 138 * is destructed, so here no free process for the security context 139 * is needed. 140 */ 141 //PK11_LogoutAll(); 142 //NSS_Shutdown(); 143 } 144 SEInitializer_NssImpl_getImplementationName()145rtl::OUString SEInitializer_NssImpl_getImplementationName () 146 { 147 148 return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) ); 149 } 150 SEInitializer_NssImpl_supportsService(const rtl::OUString & ServiceName)151sal_Bool SAL_CALL SEInitializer_NssImpl_supportsService( const rtl::OUString& ServiceName ) 152 { 153 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SE_SERVICE_NAME )) || ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( NSS_SERVICE_NAME )); 154 } 155 SEInitializer_NssImpl_getSupportedServiceNames()156cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl_getSupportedServiceNames( ) 157 { 158 cssu::Sequence < rtl::OUString > aRet(2); 159 rtl::OUString* pArray = aRet.getArray(); 160 pArray[0] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SE_SERVICE_NAME ) ); 161 pArray[1] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( NSS_SERVICE_NAME ) ); 162 return aRet; 163 } 164 SEInitializer_NssImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)165cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_NssImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr) 166 { 167 return (cppu::OWeakObject*) new SEInitializer_NssImpl(rSMgr); 168 } 169 170 /* XServiceInfo */ getImplementationName()171rtl::OUString SAL_CALL SEInitializer_NssImpl::getImplementationName( ) 172 { 173 return SEInitializer_NssImpl_getImplementationName(); 174 } supportsService(const rtl::OUString & rServiceName)175sal_Bool SAL_CALL SEInitializer_NssImpl::supportsService( const rtl::OUString& rServiceName ) 176 { 177 return SEInitializer_NssImpl_supportsService( rServiceName ); 178 } getSupportedServiceNames()179cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl::getSupportedServiceNames( ) 180 { 181 return SEInitializer_NssImpl_getSupportedServiceNames(); 182 } 183