1*06b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*06b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06b3ce53SAndrew Rist  * distributed with this work for additional information
6*06b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
9*06b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*06b3ce53SAndrew Rist  *
11*06b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*06b3ce53SAndrew Rist  *
13*06b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06b3ce53SAndrew Rist  * software distributed under the License is distributed on an
15*06b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06b3ce53SAndrew Rist  * specific language governing permissions and limitations
18*06b3ce53SAndrew Rist  * under the License.
19*06b3ce53SAndrew Rist  *
20*06b3ce53SAndrew Rist  *************************************************************/
21*06b3ce53SAndrew Rist 
22*06b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /*
28cdf0e10cSrcweir  * Turn off DEBUG Assertions
29cdf0e10cSrcweir  */
30cdf0e10cSrcweir #ifdef _DEBUG
31cdf0e10cSrcweir     #define _DEBUG_WAS_DEFINED _DEBUG
32cdf0e10cSrcweir     #undef _DEBUG
33cdf0e10cSrcweir #else
34cdf0e10cSrcweir     #undef _DEBUG_WAS_DEFINED
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /*
38cdf0e10cSrcweir  * and turn off the additional virtual methods which are part of some interfaces when compiled
39cdf0e10cSrcweir  * with debug
40cdf0e10cSrcweir  */
41cdf0e10cSrcweir #ifdef DEBUG
42cdf0e10cSrcweir     #define DEBUG_WAS_DEFINED DEBUG
43cdf0e10cSrcweir     #undef DEBUG
44cdf0e10cSrcweir #else
45cdf0e10cSrcweir     #undef DEBUG_WAS_DEFINED
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <sal/types.h>
49cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
50cdf0e10cSrcweir #include <rtl/string.hxx>
51cdf0e10cSrcweir #include <rtl/strbuf.hxx>
52cdf0e10cSrcweir #include <osl/file.hxx>
53cdf0e10cSrcweir #include <osl/thread.h>
54cdf0e10cSrcweir #include <tools/debug.hxx>
55cdf0e10cSrcweir #include <rtl/logfile.hxx>
56cdf0e10cSrcweir 
57cdf0e10cSrcweir #include "seinitializer_nssimpl.hxx"
58cdf0e10cSrcweir #include "securityenvironment_nssimpl.hxx"
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #include <nspr.h>
61cdf0e10cSrcweir #include <cert.h>
62cdf0e10cSrcweir #include <nss.h>
63cdf0e10cSrcweir #include <pk11pub.h>
64cdf0e10cSrcweir #include <secmod.h>
65cdf0e10cSrcweir #include <nssckbi.h>
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
68cdf0e10cSrcweir namespace css = ::com::sun::star;
69cdf0e10cSrcweir namespace cssu = css::uno;
70cdf0e10cSrcweir namespace cssl = css::lang;
71cdf0e10cSrcweir namespace cssxc = css::xml::crypto;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir using namespace com::sun::star;
74cdf0e10cSrcweir using ::rtl::OUString;
75cdf0e10cSrcweir using ::rtl::OString;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir #define SE_SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer"
78cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_NssImpl"
79cdf0e10cSrcweir #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment"
80cdf0e10cSrcweir #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext"
81cdf0e10cSrcweir 
SEInitializer_NssImpl(const css::uno::Reference<css::lang::XMultiServiceFactory> & rxMSF)82cdf0e10cSrcweir SEInitializer_NssImpl::SEInitializer_NssImpl(
83cdf0e10cSrcweir     const css::uno::Reference< css::lang::XMultiServiceFactory > &rxMSF )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     mxMSF = rxMSF;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
~SEInitializer_NssImpl()88cdf0e10cSrcweir SEInitializer_NssImpl::~SEInitializer_NssImpl()
89cdf0e10cSrcweir {
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir /* XSEInitializer */
93cdf0e10cSrcweir cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL
createSecurityContext(const::rtl::OUString &)94cdf0e10cSrcweir     SEInitializer_NssImpl::createSecurityContext( const ::rtl::OUString& )
95cdf0e10cSrcweir     throw (cssu::RuntimeException)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     CERTCertDBHandle    *pCertHandle = NULL ;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     if( !initNSS( mxMSF ) )
100cdf0e10cSrcweir         return NULL;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     pCertHandle = CERT_GetDefaultCertDB() ;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     try
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         /* Build XML Security Context */
107cdf0e10cSrcweir         const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) );
108cdf0e10cSrcweir         cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY );
109cdf0e10cSrcweir         if( !xSecCtx.is() )
110cdf0e10cSrcweir             return NULL;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) );
113cdf0e10cSrcweir         cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY );
114cdf0e10cSrcweir         cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
115cdf0e10cSrcweir         if( !xEnvTunnel.is() )
116cdf0e10cSrcweir             return NULL;
117cdf0e10cSrcweir         SecurityEnvironment_NssImpl* pSecEnv = reinterpret_cast<SecurityEnvironment_NssImpl*>(
118cdf0e10cSrcweir             sal::static_int_cast<sal_uIntPtr>(
119cdf0e10cSrcweir                 xEnvTunnel->getSomething(SecurityEnvironment_NssImpl::getUnoTunnelId() ))) ;
120cdf0e10cSrcweir         pSecEnv->setCertDb(pCertHandle);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         sal_Int32 n = xSecCtx->addSecurityEnvironment(xSecEnv);
123cdf0e10cSrcweir         //originally the SecurityEnvironment with the internal slot was set as default
124cdf0e10cSrcweir         xSecCtx->setDefaultSecurityEnvironmentIndex( n );
125cdf0e10cSrcweir         return xSecCtx;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir     catch( cssu::Exception& )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         //PK11_LogoutAll();
130cdf0e10cSrcweir         //NSS_Shutdown();
131cdf0e10cSrcweir         return NULL;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
freeSecurityContext(const cssu::Reference<cssxc::XXMLSecurityContext> &)135cdf0e10cSrcweir void SAL_CALL SEInitializer_NssImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >& )
136cdf0e10cSrcweir     throw (cssu::RuntimeException)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     /*
139cdf0e10cSrcweir      * because the security context will free all its content when it
140cdf0e10cSrcweir      * is destructed, so here no free process for the security context
141cdf0e10cSrcweir      * is needed.
142cdf0e10cSrcweir      */
143cdf0e10cSrcweir     //PK11_LogoutAll();
144cdf0e10cSrcweir     //NSS_Shutdown();
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
SEInitializer_NssImpl_getImplementationName()147cdf0e10cSrcweir rtl::OUString SEInitializer_NssImpl_getImplementationName ()
148cdf0e10cSrcweir     throw (cssu::RuntimeException)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
SEInitializer_NssImpl_supportsService(const rtl::OUString & ServiceName)154cdf0e10cSrcweir sal_Bool SAL_CALL SEInitializer_NssImpl_supportsService( const rtl::OUString& ServiceName )
155cdf0e10cSrcweir     throw (cssu::RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SE_SERVICE_NAME )) || ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( NSS_SERVICE_NAME ));
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
SEInitializer_NssImpl_getSupportedServiceNames()160cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl_getSupportedServiceNames(  )
161cdf0e10cSrcweir     throw (cssu::RuntimeException)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     cssu::Sequence < rtl::OUString > aRet(2);
164cdf0e10cSrcweir     rtl::OUString* pArray = aRet.getArray();
165cdf0e10cSrcweir     pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SE_SERVICE_NAME ) );
166cdf0e10cSrcweir     pArray[1] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( NSS_SERVICE_NAME ) );
167cdf0e10cSrcweir     return aRet;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
SEInitializer_NssImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)170cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_NssImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
171cdf0e10cSrcweir     throw( cssu::Exception )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     return (cppu::OWeakObject*) new SEInitializer_NssImpl(rSMgr);
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir /* XServiceInfo */
getImplementationName()177cdf0e10cSrcweir rtl::OUString SAL_CALL SEInitializer_NssImpl::getImplementationName(  )
178cdf0e10cSrcweir     throw (cssu::RuntimeException)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     return SEInitializer_NssImpl_getImplementationName();
181cdf0e10cSrcweir }
supportsService(const rtl::OUString & rServiceName)182cdf0e10cSrcweir sal_Bool SAL_CALL SEInitializer_NssImpl::supportsService( const rtl::OUString& rServiceName )
183cdf0e10cSrcweir     throw (cssu::RuntimeException)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir     return SEInitializer_NssImpl_supportsService( rServiceName );
186cdf0e10cSrcweir }
getSupportedServiceNames()187cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl::getSupportedServiceNames(  )
188cdf0e10cSrcweir     throw (cssu::RuntimeException)
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     return SEInitializer_NssImpl_getSupportedServiceNames();
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193