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