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)82 SEInitializer_NssImpl::SEInitializer_NssImpl(
83     const css::uno::Reference< css::lang::XMultiServiceFactory > &rxMSF )
84 {
85     mxMSF = rxMSF;
86 }
87 
~SEInitializer_NssImpl()88 SEInitializer_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     throw (cssu::RuntimeException)
96 {
97     CERTCertDBHandle    *pCertHandle = NULL ;
98 
99     if( !initNSS( mxMSF ) )
100         return NULL;
101 
102     pCertHandle = CERT_GetDefaultCertDB() ;
103 
104     try
105     {
106         /* Build XML Security Context */
107         const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) );
108         cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY );
109         if( !xSecCtx.is() )
110             return NULL;
111 
112         const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) );
113         cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY );
114         cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
115         if( !xEnvTunnel.is() )
116             return NULL;
117         SecurityEnvironment_NssImpl* pSecEnv = reinterpret_cast<SecurityEnvironment_NssImpl*>(
118             sal::static_int_cast<sal_uIntPtr>(
119                 xEnvTunnel->getSomething(SecurityEnvironment_NssImpl::getUnoTunnelId() ))) ;
120         pSecEnv->setCertDb(pCertHandle);
121 
122         sal_Int32 n = xSecCtx->addSecurityEnvironment(xSecEnv);
123         //originally the SecurityEnvironment with the internal slot was set as default
124         xSecCtx->setDefaultSecurityEnvironmentIndex( n );
125         return xSecCtx;
126     }
127     catch( cssu::Exception& )
128     {
129         //PK11_LogoutAll();
130         //NSS_Shutdown();
131         return NULL;
132     }
133 }
134 
freeSecurityContext(const cssu::Reference<cssxc::XXMLSecurityContext> &)135 void SAL_CALL SEInitializer_NssImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >& )
136     throw (cssu::RuntimeException)
137 {
138     /*
139      * because the security context will free all its content when it
140      * is destructed, so here no free process for the security context
141      * is needed.
142      */
143     //PK11_LogoutAll();
144     //NSS_Shutdown();
145 }
146 
SEInitializer_NssImpl_getImplementationName()147 rtl::OUString SEInitializer_NssImpl_getImplementationName ()
148     throw (cssu::RuntimeException)
149 {
150 
151     return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
152 }
153 
SEInitializer_NssImpl_supportsService(const rtl::OUString & ServiceName)154 sal_Bool SAL_CALL SEInitializer_NssImpl_supportsService( const rtl::OUString& ServiceName )
155     throw (cssu::RuntimeException)
156 {
157     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SE_SERVICE_NAME )) || ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( NSS_SERVICE_NAME ));
158 }
159 
SEInitializer_NssImpl_getSupportedServiceNames()160 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl_getSupportedServiceNames(  )
161     throw (cssu::RuntimeException)
162 {
163     cssu::Sequence < rtl::OUString > aRet(2);
164     rtl::OUString* pArray = aRet.getArray();
165     pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SE_SERVICE_NAME ) );
166     pArray[1] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( NSS_SERVICE_NAME ) );
167     return aRet;
168 }
169 
SEInitializer_NssImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)170 cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_NssImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
171     throw( cssu::Exception )
172 {
173     return (cppu::OWeakObject*) new SEInitializer_NssImpl(rSMgr);
174 }
175 
176 /* XServiceInfo */
getImplementationName()177 rtl::OUString SAL_CALL SEInitializer_NssImpl::getImplementationName(  )
178     throw (cssu::RuntimeException)
179 {
180     return SEInitializer_NssImpl_getImplementationName();
181 }
supportsService(const rtl::OUString & rServiceName)182 sal_Bool SAL_CALL SEInitializer_NssImpl::supportsService( const rtl::OUString& rServiceName )
183     throw (cssu::RuntimeException)
184 {
185     return SEInitializer_NssImpl_supportsService( rServiceName );
186 }
getSupportedServiceNames()187 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl::getSupportedServiceNames(  )
188     throw (cssu::RuntimeException)
189 {
190     return SEInitializer_NssImpl_getSupportedServiceNames();
191 }
192 
193