xref: /trunk/main/xmlsecurity/source/xmlsec/nss/xmlsecuritycontext_nssimpl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 #include <sal/config.h>
31 #include <rtl/uuid.h>
32 #include "securityenvironment_nssimpl.hxx"
33 
34 #ifndef _XMLSECURITYCONTEXT_NSSIMPL_HXX_
35 #include "xmlsecuritycontext_nssimpl.hxx"
36 #endif
37 #include "xmlstreamio.hxx"
38 
39 #include <sal/types.h>
40 //For reasons that escape me, this is what xmlsec does when size_t is not 4
41 #if SAL_TYPES_SIZEOFPOINTER != 4
42 #    define XMLSEC_NO_SIZE_T
43 #endif
44 #include "xmlsec/xmlsec.h"
45 #include "xmlsec/keysmngr.h"
46 #include "xmlsec/crypto.h"
47 
48 using namespace ::com::sun::star::uno ;
49 using namespace ::com::sun::star::lang ;
50 using ::com::sun::star::lang::XMultiServiceFactory ;
51 using ::com::sun::star::lang::XSingleServiceFactory ;
52 using ::rtl::OUString ;
53 
54 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
55 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
56 
57 XMLSecurityContext_NssImpl :: XMLSecurityContext_NssImpl( const Reference< XMultiServiceFactory >& aFactory )
58     ://i39448 : m_pKeysMngr( NULL ) ,
59     m_xServiceManager( aFactory ) ,
60     m_nDefaultEnvIndex(-1)
61     //m_xSecurityEnvironment( NULL )
62 {
63     //Init xmlsec library
64     if( xmlSecInit() < 0 ) {
65         throw RuntimeException() ;
66     }
67 
68     //Init xmlsec crypto engine library
69     if( xmlSecCryptoInit() < 0 ) {
70         xmlSecShutdown() ;
71         throw RuntimeException() ;
72     }
73 
74     //Enable external stream handlers
75     if( xmlEnableStreamInputCallbacks() < 0 ) {
76         xmlSecCryptoShutdown() ;
77         xmlSecShutdown() ;
78         throw RuntimeException() ;
79     }
80 }
81 
82 XMLSecurityContext_NssImpl :: ~XMLSecurityContext_NssImpl() {
83 #if 0 //i39448
84     if( m_pKeysMngr != NULL ) {
85         xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
86     }
87 #endif
88 
89     xmlDisableStreamInputCallbacks() ;
90     xmlSecCryptoShutdown() ;
91     xmlSecShutdown() ;
92 }
93 
94 //i39448 : new methods
95 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::addSecurityEnvironment(
96     const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >& aSecurityEnvironment)
97     throw (::com::sun::star::security::SecurityInfrastructureException, ::com::sun::star::uno::RuntimeException)
98 {
99     if( !aSecurityEnvironment.is() )
100     {
101         throw RuntimeException() ;
102     }
103 
104     m_vSecurityEnvironments.push_back( aSecurityEnvironment );
105 
106     return m_vSecurityEnvironments.size() - 1 ;
107 }
108 
109 
110 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getSecurityEnvironmentNumber(  )
111     throw (::com::sun::star::uno::RuntimeException)
112 {
113     return m_vSecurityEnvironments.size();
114 }
115 
116 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
117     XMLSecurityContext_NssImpl::getSecurityEnvironmentByIndex( sal_Int32 index )
118     throw (::com::sun::star::uno::RuntimeException)
119 {
120     ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment;
121 
122     if (index >= 0 && index < ( sal_Int32 )m_vSecurityEnvironments.size())
123     {
124         xSecurityEnvironment = m_vSecurityEnvironments[index];
125     }
126     else
127         throw RuntimeException() ;
128 
129     return xSecurityEnvironment;
130 }
131 
132 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
133     XMLSecurityContext_NssImpl::getSecurityEnvironment(  )
134     throw (::com::sun::star::uno::RuntimeException)
135 {
136     if (m_nDefaultEnvIndex >= 0 && m_nDefaultEnvIndex < ( sal_Int32 )m_vSecurityEnvironments.size())
137         return getSecurityEnvironmentByIndex(m_nDefaultEnvIndex);
138     else
139         throw RuntimeException() ;
140 }
141 
142 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getDefaultSecurityEnvironmentIndex(  )
143     throw (::com::sun::star::uno::RuntimeException)
144 {
145     return m_nDefaultEnvIndex ;
146 }
147 
148 void SAL_CALL XMLSecurityContext_NssImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 nDefaultEnvIndex )
149     throw (::com::sun::star::uno::RuntimeException)
150 {
151     m_nDefaultEnvIndex = nDefaultEnvIndex;
152 }
153 
154 #if 0 //i39448 : old methods should be deleted
155 /* XXMLSecurityContext */
156 void SAL_CALL XMLSecurityContext_NssImpl :: setSecurityEnvironment( const Reference< XSecurityEnvironment >& aSecurityEnvironment ) throw( com::sun::star::security::SecurityInfrastructureException ) {
157     PK11SlotInfo* slot ;
158     CERTCertDBHandle* handler ;
159     //xmlSecKeyPtr key ;
160     //xmlSecKeyDataPtr keyData ;
161     PK11SymKey* symKey ;
162     SECKEYPublicKey* pubKey ;
163     SECKEYPrivateKey* priKey ;
164     unsigned int i ;
165 
166     if( !aSecurityEnvironment.is() )
167         throw RuntimeException() ;
168 
169     m_xSecurityEnvironment = aSecurityEnvironment ;
170 
171     //Clear key manager
172     if( m_pKeysMngr != NULL ) {
173         xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
174         m_pKeysMngr = NULL ;
175     }
176 
177     //Create key manager
178     Reference< XUnoTunnel > xEnvTunnel( m_xSecurityEnvironment , UNO_QUERY ) ;
179     if( !xEnvTunnel.is() ) {
180         throw RuntimeException() ;^1
181     }
182 
183     SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
184     if( pSecEnv == NULL )
185         throw RuntimeException() ;
186 
187     //todo
188 //  slot = pSecEnv->getCryptoSlot() ;
189     handler = pSecEnv->getCertDb() ;
190 
191     /*-
192      * The following lines is based on the private version of xmlSec-NSS
193      * crypto engine
194      */
195     m_pKeysMngr = xmlSecNssAppliedKeysMngrCreate( slot , handler ) ;
196     if( m_pKeysMngr == NULL )
197         throw RuntimeException() ;
198 
199     /*-
200      * Adopt symmetric key into keys manager
201      */
202     for( i = 0 ; ( symKey = pSecEnv->getSymKey( i ) ) != NULL ; i ++ ) {
203         if( xmlSecNssAppliedKeysMngrSymKeyLoad( m_pKeysMngr, symKey ) < 0 ) {
204             throw RuntimeException() ;
205         }
206     }
207 
208     /*-
209      * Adopt asymmetric public key into keys manager
210      */
211     for( i = 0 ; ( pubKey = pSecEnv->getPubKey( i ) ) != NULL ; i ++ ) {
212         if( xmlSecNssAppliedKeysMngrPubKeyLoad( m_pKeysMngr, pubKey ) < 0 ) {
213             throw RuntimeException() ;
214         }
215     }
216 
217     /*-
218      * Adopt asymmetric private key into keys manager
219      */
220     for( i = 0 ; ( priKey = pSecEnv->getPriKey( i ) ) != NULL ; i ++ ) {
221         if( xmlSecNssAppliedKeysMngrPriKeyLoad( m_pKeysMngr, priKey ) < 0 ) {
222             throw RuntimeException() ;
223         }
224     }
225 }
226 
227 /* XXMLSecurityContext */
228 Reference< XSecurityEnvironment > SAL_CALL XMLSecurityContext_NssImpl :: getSecurityEnvironment()
229     throw (RuntimeException)
230 {
231     return  m_xSecurityEnvironment ;
232 }
233 #endif
234 
235 
236 /* XInitialization */
237 void SAL_CALL XMLSecurityContext_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
238     // TBD
239 } ;
240 
241 /* XServiceInfo */
242 OUString SAL_CALL XMLSecurityContext_NssImpl :: getImplementationName() throw( RuntimeException ) {
243     return impl_getImplementationName() ;
244 }
245 
246 /* XServiceInfo */
247 sal_Bool SAL_CALL XMLSecurityContext_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException ) {
248     Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
249     const OUString* pArray = seqServiceNames.getConstArray() ;
250     for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
251         if( *( pArray + i ) == serviceName )
252             return sal_True ;
253     }
254     return sal_False ;
255 }
256 
257 /* XServiceInfo */
258 Sequence< OUString > SAL_CALL XMLSecurityContext_NssImpl :: getSupportedServiceNames() throw( RuntimeException ) {
259     return impl_getSupportedServiceNames() ;
260 }
261 
262 //Helper for XServiceInfo
263 Sequence< OUString > XMLSecurityContext_NssImpl :: impl_getSupportedServiceNames() {
264     ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
265     Sequence< OUString > seqServiceNames( 1 ) ;
266     seqServiceNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
267     return seqServiceNames ;
268 }
269 
270 OUString XMLSecurityContext_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
271     return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl" ) ;
272 }
273 
274 //Helper for registry
275 Reference< XInterface > SAL_CALL XMLSecurityContext_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
276     return Reference< XInterface >( *new XMLSecurityContext_NssImpl( aServiceManager ) ) ;
277 }
278 
279 Reference< XSingleServiceFactory > XMLSecurityContext_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
280     //Reference< XSingleServiceFactory > xFactory ;
281     //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
282     //return xFactory ;
283     return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
284 }
285 
286 #if 0 //not useful any longer
287 /* XUnoTunnel */
288 sal_Int64 SAL_CALL XMLSecurityContext_NssImpl :: getSomething( const Sequence< sal_Int8 >& aIdentifier )
289 throw (RuntimeException)
290 {
291     if( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
292         return ( sal_Int64 )this ;
293     }
294     return 0 ;
295 }
296 
297 /* XUnoTunnel extension */
298 const Sequence< sal_Int8>& XMLSecurityContext_NssImpl :: getUnoTunnelId() {
299     static Sequence< sal_Int8 >* pSeq = 0 ;
300     if( !pSeq ) {
301         ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
302         if( !pSeq ) {
303             static Sequence< sal_Int8> aSeq( 16 ) ;
304             rtl_createUuid( ( sal_uInt8* )aSeq.getArray() , 0 , sal_True ) ;
305             pSeq = &aSeq ;
306         }
307     }
308     return *pSeq ;
309 }
310 
311 /* XUnoTunnel extension */
312 XMLSecurityContext_NssImpl* XMLSecurityContext_NssImpl :: getImplementation( const Reference< XInterface > xObj ) {
313     Reference< XUnoTunnel > xUT( xObj , UNO_QUERY ) ;
314     if( xUT.is() ) {
315         return ( XMLSecurityContext_NssImpl* )xUT->getSomething( getUnoTunnelId() ) ;
316     } else
317         return NULL ;
318 }
319 
320 /* Native methods */
321 xmlSecKeysMngrPtr XMLSecurityContext_NssImpl :: keysManager() throw( Exception, RuntimeException ) {
322     return m_pKeysMngr ;
323 }
324 
325 #endif
326