1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 /*
32  * Turn off DEBUG Assertions
33  */
34 #ifdef _DEBUG
35     #define _DEBUG_WAS_DEFINED _DEBUG
36     #undef _DEBUG
37 #else
38     #undef _DEBUG_WAS_DEFINED
39 #endif
40 
41 /*
42  * and turn off the additional virtual methods which are part of some interfaces when compiled
43  * with debug
44  */
45 #ifdef DEBUG
46     #define DEBUG_WAS_DEFINED DEBUG
47     #undef DEBUG
48 #else
49     #undef DEBUG_WAS_DEFINED
50 #endif
51 
52 #include <sal/types.h>
53 #include <rtl/bootstrap.hxx>
54 #include <rtl/string.hxx>
55 #include <rtl/strbuf.hxx>
56 #include <osl/file.hxx>
57 #include <osl/thread.h>
58 #include <tools/debug.hxx>
59 #include <rtl/logfile.hxx>
60 
61 #include "seinitializer_nssimpl.hxx"
62 #include "securityenvironment_nssimpl.hxx"
63 
64 #include <nspr.h>
65 #include <cert.h>
66 #include <nss.h>
67 #include <pk11pub.h>
68 #include <secmod.h>
69 #include <nssckbi.h>
70 
71 
72 namespace css = ::com::sun::star;
73 namespace cssu = css::uno;
74 namespace cssl = css::lang;
75 namespace cssxc = css::xml::crypto;
76 
77 using namespace com::sun::star;
78 using ::rtl::OUString;
79 using ::rtl::OString;
80 
81 #define SE_SERVICE_NAME "com.sun.star.xml.crypto.SEInitializer"
82 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_NssImpl"
83 #define SECURITY_ENVIRONMENT "com.sun.star.xml.crypto.SecurityEnvironment"
84 #define SECURITY_CONTEXT "com.sun.star.xml.crypto.XMLSecurityContext"
85 
86 SEInitializer_NssImpl::SEInitializer_NssImpl(
87     const css::uno::Reference< css::lang::XMultiServiceFactory > &rxMSF )
88 {
89     mxMSF = rxMSF;
90 }
91 
92 SEInitializer_NssImpl::~SEInitializer_NssImpl()
93 {
94 }
95 
96 /* XSEInitializer */
97 cssu::Reference< cssxc::XXMLSecurityContext > SAL_CALL
98     SEInitializer_NssImpl::createSecurityContext( const ::rtl::OUString& )
99     throw (cssu::RuntimeException)
100 {
101     CERTCertDBHandle    *pCertHandle = NULL ;
102 
103     if( !initNSS( mxMSF ) )
104         return NULL;
105 
106     pCertHandle = CERT_GetDefaultCertDB() ;
107 
108     try
109     {
110         /* Build XML Security Context */
111         const rtl::OUString sSecyrutyContext ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_CONTEXT ) );
112         cssu::Reference< cssxc::XXMLSecurityContext > xSecCtx( mxMSF->createInstance ( sSecyrutyContext ), cssu::UNO_QUERY );
113         if( !xSecCtx.is() )
114             return NULL;
115 
116         const rtl::OUString sSecyrutyEnvironment ( RTL_CONSTASCII_USTRINGPARAM( SECURITY_ENVIRONMENT ) );
117         cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv( mxMSF->createInstance ( sSecyrutyEnvironment ), cssu::UNO_QUERY );
118         cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
119         if( !xEnvTunnel.is() )
120             return NULL;
121         SecurityEnvironment_NssImpl* pSecEnv = reinterpret_cast<SecurityEnvironment_NssImpl*>(
122             sal::static_int_cast<sal_uIntPtr>(
123                 xEnvTunnel->getSomething(SecurityEnvironment_NssImpl::getUnoTunnelId() ))) ;
124         pSecEnv->setCertDb(pCertHandle);
125 
126         sal_Int32 n = xSecCtx->addSecurityEnvironment(xSecEnv);
127         //originally the SecurityEnvironment with the internal slot was set as default
128         xSecCtx->setDefaultSecurityEnvironmentIndex( n );
129         return xSecCtx;
130     }
131     catch( cssu::Exception& )
132     {
133         //PK11_LogoutAll();
134         //NSS_Shutdown();
135         return NULL;
136     }
137 }
138 
139 void SAL_CALL SEInitializer_NssImpl::freeSecurityContext( const cssu::Reference< cssxc::XXMLSecurityContext >& )
140     throw (cssu::RuntimeException)
141 {
142     /*
143      * because the security context will free all its content when it
144      * is destructed, so here no free process for the security context
145      * is needed.
146      */
147     //PK11_LogoutAll();
148     //NSS_Shutdown();
149 }
150 
151 rtl::OUString SEInitializer_NssImpl_getImplementationName ()
152     throw (cssu::RuntimeException)
153 {
154 
155     return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
156 }
157 
158 sal_Bool SAL_CALL SEInitializer_NssImpl_supportsService( const rtl::OUString& ServiceName )
159     throw (cssu::RuntimeException)
160 {
161     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SE_SERVICE_NAME )) || ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( NSS_SERVICE_NAME ));
162 }
163 
164 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl_getSupportedServiceNames(  )
165     throw (cssu::RuntimeException)
166 {
167     cssu::Sequence < rtl::OUString > aRet(2);
168     rtl::OUString* pArray = aRet.getArray();
169     pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SE_SERVICE_NAME ) );
170     pArray[1] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( NSS_SERVICE_NAME ) );
171     return aRet;
172 }
173 
174 cssu::Reference< cssu::XInterface > SAL_CALL SEInitializer_NssImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
175     throw( cssu::Exception )
176 {
177     return (cppu::OWeakObject*) new SEInitializer_NssImpl(rSMgr);
178 }
179 
180 /* XServiceInfo */
181 rtl::OUString SAL_CALL SEInitializer_NssImpl::getImplementationName(  )
182     throw (cssu::RuntimeException)
183 {
184     return SEInitializer_NssImpl_getImplementationName();
185 }
186 sal_Bool SAL_CALL SEInitializer_NssImpl::supportsService( const rtl::OUString& rServiceName )
187     throw (cssu::RuntimeException)
188 {
189     return SEInitializer_NssImpl_supportsService( rServiceName );
190 }
191 cssu::Sequence< rtl::OUString > SAL_CALL SEInitializer_NssImpl::getSupportedServiceNames(  )
192     throw (cssu::RuntimeException)
193 {
194     return SEInitializer_NssImpl_getSupportedServiceNames();
195 }
196 
197