merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / xmlsec / nss / xmlsecuritycontext_nssimpl.cxx
blobca881d0ee92b18c663c8254eb76a92d5766c5c57
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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"
34 #ifndef _XMLSECURITYCONTEXT_NSSIMPL_HXX_
35 #include "xmlsecuritycontext_nssimpl.hxx"
36 #endif
37 #include "xmlstreamio.hxx"
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"
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 ;
54 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
55 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
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 )
63 //Init xmlsec library
64 if( xmlSecInit() < 0 ) {
65 throw RuntimeException() ;
68 //Init xmlsec crypto engine library
69 if( xmlSecCryptoInit() < 0 ) {
70 xmlSecShutdown() ;
71 throw RuntimeException() ;
74 //Enable external stream handlers
75 if( xmlEnableStreamInputCallbacks() < 0 ) {
76 xmlSecCryptoShutdown() ;
77 xmlSecShutdown() ;
78 throw RuntimeException() ;
82 XMLSecurityContext_NssImpl :: ~XMLSecurityContext_NssImpl() {
83 #if 0 //i39448
84 if( m_pKeysMngr != NULL ) {
85 xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
87 #endif
89 xmlDisableStreamInputCallbacks() ;
90 xmlSecCryptoShutdown() ;
91 xmlSecShutdown() ;
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)
99 if( !aSecurityEnvironment.is() )
101 throw RuntimeException() ;
104 m_vSecurityEnvironments.push_back( aSecurityEnvironment );
106 return m_vSecurityEnvironments.size() - 1 ;
110 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getSecurityEnvironmentNumber( )
111 throw (::com::sun::star::uno::RuntimeException)
113 return m_vSecurityEnvironments.size();
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)
120 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment;
122 if (index >= 0 && index < ( sal_Int32 )m_vSecurityEnvironments.size())
124 xSecurityEnvironment = m_vSecurityEnvironments[index];
126 else
127 throw RuntimeException() ;
129 return xSecurityEnvironment;
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)
136 if (m_nDefaultEnvIndex >= 0 && m_nDefaultEnvIndex < ( sal_Int32 )m_vSecurityEnvironments.size())
137 return getSecurityEnvironmentByIndex(m_nDefaultEnvIndex);
138 else
139 throw RuntimeException() ;
142 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getDefaultSecurityEnvironmentIndex( )
143 throw (::com::sun::star::uno::RuntimeException)
145 return m_nDefaultEnvIndex ;
148 void SAL_CALL XMLSecurityContext_NssImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 nDefaultEnvIndex )
149 throw (::com::sun::star::uno::RuntimeException)
151 m_nDefaultEnvIndex = nDefaultEnvIndex;
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 ;
166 if( !aSecurityEnvironment.is() )
167 throw RuntimeException() ;
169 m_xSecurityEnvironment = aSecurityEnvironment ;
171 //Clear key manager
172 if( m_pKeysMngr != NULL ) {
173 xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
174 m_pKeysMngr = NULL ;
177 //Create key manager
178 Reference< XUnoTunnel > xEnvTunnel( m_xSecurityEnvironment , UNO_QUERY ) ;
179 if( !xEnvTunnel.is() ) {
180 throw RuntimeException() ;^1
183 SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
184 if( pSecEnv == NULL )
185 throw RuntimeException() ;
187 //todo
188 // slot = pSecEnv->getCryptoSlot() ;
189 handler = pSecEnv->getCertDb() ;
192 * The following lines is based on the private version of xmlSec-NSS
193 * crypto engine
195 m_pKeysMngr = xmlSecNssAppliedKeysMngrCreate( slot , handler ) ;
196 if( m_pKeysMngr == NULL )
197 throw RuntimeException() ;
200 * Adopt symmetric key into keys manager
202 for( i = 0 ; ( symKey = pSecEnv->getSymKey( i ) ) != NULL ; i ++ ) {
203 if( xmlSecNssAppliedKeysMngrSymKeyLoad( m_pKeysMngr, symKey ) < 0 ) {
204 throw RuntimeException() ;
209 * Adopt asymmetric public key into keys manager
211 for( i = 0 ; ( pubKey = pSecEnv->getPubKey( i ) ) != NULL ; i ++ ) {
212 if( xmlSecNssAppliedKeysMngrPubKeyLoad( m_pKeysMngr, pubKey ) < 0 ) {
213 throw RuntimeException() ;
218 * Adopt asymmetric private key into keys manager
220 for( i = 0 ; ( priKey = pSecEnv->getPriKey( i ) ) != NULL ; i ++ ) {
221 if( xmlSecNssAppliedKeysMngrPriKeyLoad( m_pKeysMngr, priKey ) < 0 ) {
222 throw RuntimeException() ;
227 /* XXMLSecurityContext */
228 Reference< XSecurityEnvironment > SAL_CALL XMLSecurityContext_NssImpl :: getSecurityEnvironment()
229 throw (RuntimeException)
231 return m_xSecurityEnvironment ;
233 #endif
236 /* XInitialization */
237 void SAL_CALL XMLSecurityContext_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
238 // TBD
241 /* XServiceInfo */
242 OUString SAL_CALL XMLSecurityContext_NssImpl :: getImplementationName() throw( RuntimeException ) {
243 return impl_getImplementationName() ;
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 ;
254 return sal_False ;
257 /* XServiceInfo */
258 Sequence< OUString > SAL_CALL XMLSecurityContext_NssImpl :: getSupportedServiceNames() throw( RuntimeException ) {
259 return impl_getSupportedServiceNames() ;
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 ;
270 OUString XMLSecurityContext_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
271 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl" ) ;
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 ) ) ;
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() ) ;
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)
291 if( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
292 return ( sal_Int64 )this ;
294 return 0 ;
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 ;
308 return *pSeq ;
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 ;
320 /* Native methods */
321 xmlSecKeysMngrPtr XMLSecurityContext_NssImpl :: keysManager() throw( Exception, RuntimeException ) {
322 return m_pKeysMngr ;
325 #endif