1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlsecuritycontext_nssimpl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
33 #include <sal/config.h>
35 #include "securityenvironment_nssimpl.hxx"
37 #ifndef _XMLSECURITYCONTEXT_NSSIMPL_HXX_
38 #include "xmlsecuritycontext_nssimpl.hxx"
40 #include "xmlstreamio.hxx"
42 #include <sal/types.h>
43 //For reasons that escape me, this is what xmlsec does when size_t is not 4
44 #if SAL_TYPES_SIZEOFPOINTER != 4
45 # define XMLSEC_NO_SIZE_T
47 #include "xmlsec/xmlsec.h"
48 #include "xmlsec/keysmngr.h"
49 #include "xmlsec/crypto.h"
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::lang
;
53 using ::com::sun::star::lang::XMultiServiceFactory
;
54 using ::com::sun::star::lang::XSingleServiceFactory
;
55 using ::rtl::OUString
;
57 using ::com::sun::star::xml::crypto::XSecurityEnvironment
;
58 using ::com::sun::star::xml::crypto::XXMLSecurityContext
;
60 XMLSecurityContext_NssImpl :: XMLSecurityContext_NssImpl( const Reference
< XMultiServiceFactory
>& aFactory
)
61 ://i39448 : m_pKeysMngr( NULL ) ,
62 m_xServiceManager( aFactory
) ,
63 m_nDefaultEnvIndex(-1)
64 //m_xSecurityEnvironment( NULL )
67 if( xmlSecInit() < 0 ) {
68 throw RuntimeException() ;
71 //Init xmlsec crypto engine library
72 if( xmlSecCryptoInit() < 0 ) {
74 throw RuntimeException() ;
77 //Enable external stream handlers
78 if( xmlEnableStreamInputCallbacks() < 0 ) {
79 xmlSecCryptoShutdown() ;
81 throw RuntimeException() ;
85 XMLSecurityContext_NssImpl :: ~XMLSecurityContext_NssImpl() {
87 if( m_pKeysMngr
!= NULL
) {
88 xmlSecKeysMngrDestroy( m_pKeysMngr
) ;
92 xmlDisableStreamInputCallbacks() ;
93 xmlSecCryptoShutdown() ;
97 //i39448 : new methods
98 sal_Int32 SAL_CALL
XMLSecurityContext_NssImpl::addSecurityEnvironment(
99 const ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
>& aSecurityEnvironment
)
100 throw (::com::sun::star::security::SecurityInfrastructureException
, ::com::sun::star::uno::RuntimeException
)
102 if( !aSecurityEnvironment
.is() )
104 throw RuntimeException() ;
107 m_vSecurityEnvironments
.push_back( aSecurityEnvironment
);
109 return m_vSecurityEnvironments
.size() - 1 ;
113 sal_Int32 SAL_CALL
XMLSecurityContext_NssImpl::getSecurityEnvironmentNumber( )
114 throw (::com::sun::star::uno::RuntimeException
)
116 return m_vSecurityEnvironments
.size();
119 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> SAL_CALL
120 XMLSecurityContext_NssImpl::getSecurityEnvironmentByIndex( sal_Int32 index
)
121 throw (::com::sun::star::uno::RuntimeException
)
123 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
;
125 if (index
>= 0 && index
< ( sal_Int32
)m_vSecurityEnvironments
.size())
127 xSecurityEnvironment
= m_vSecurityEnvironments
[index
];
130 throw RuntimeException() ;
132 return xSecurityEnvironment
;
135 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> SAL_CALL
136 XMLSecurityContext_NssImpl::getSecurityEnvironment( )
137 throw (::com::sun::star::uno::RuntimeException
)
139 if (m_nDefaultEnvIndex
>= 0 && m_nDefaultEnvIndex
< ( sal_Int32
)m_vSecurityEnvironments
.size())
140 return getSecurityEnvironmentByIndex(m_nDefaultEnvIndex
);
142 throw RuntimeException() ;
145 sal_Int32 SAL_CALL
XMLSecurityContext_NssImpl::getDefaultSecurityEnvironmentIndex( )
146 throw (::com::sun::star::uno::RuntimeException
)
148 return m_nDefaultEnvIndex
;
151 void SAL_CALL
XMLSecurityContext_NssImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 nDefaultEnvIndex
)
152 throw (::com::sun::star::uno::RuntimeException
)
154 m_nDefaultEnvIndex
= nDefaultEnvIndex
;
157 #if 0 //i39448 : old methods should be deleted
158 /* XXMLSecurityContext */
159 void SAL_CALL
XMLSecurityContext_NssImpl :: setSecurityEnvironment( const Reference
< XSecurityEnvironment
>& aSecurityEnvironment
) throw( com::sun::star::security::SecurityInfrastructureException
) {
161 CERTCertDBHandle
* handler
;
163 //xmlSecKeyDataPtr keyData ;
165 SECKEYPublicKey
* pubKey
;
166 SECKEYPrivateKey
* priKey
;
169 if( !aSecurityEnvironment
.is() )
170 throw RuntimeException() ;
172 m_xSecurityEnvironment
= aSecurityEnvironment
;
175 if( m_pKeysMngr
!= NULL
) {
176 xmlSecKeysMngrDestroy( m_pKeysMngr
) ;
181 Reference
< XUnoTunnel
> xEnvTunnel( m_xSecurityEnvironment
, UNO_QUERY
) ;
182 if( !xEnvTunnel
.is() ) {
183 throw RuntimeException() ;^1
186 SecurityEnvironment_NssImpl
* pSecEnv
= ( SecurityEnvironment_NssImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
187 if( pSecEnv
== NULL
)
188 throw RuntimeException() ;
191 // slot = pSecEnv->getCryptoSlot() ;
192 handler
= pSecEnv
->getCertDb() ;
195 * The following lines is based on the private version of xmlSec-NSS
198 m_pKeysMngr
= xmlSecNssAppliedKeysMngrCreate( slot
, handler
) ;
199 if( m_pKeysMngr
== NULL
)
200 throw RuntimeException() ;
203 * Adopt symmetric key into keys manager
205 for( i
= 0 ; ( symKey
= pSecEnv
->getSymKey( i
) ) != NULL
; i
++ ) {
206 if( xmlSecNssAppliedKeysMngrSymKeyLoad( m_pKeysMngr
, symKey
) < 0 ) {
207 throw RuntimeException() ;
212 * Adopt asymmetric public key into keys manager
214 for( i
= 0 ; ( pubKey
= pSecEnv
->getPubKey( i
) ) != NULL
; i
++ ) {
215 if( xmlSecNssAppliedKeysMngrPubKeyLoad( m_pKeysMngr
, pubKey
) < 0 ) {
216 throw RuntimeException() ;
221 * Adopt asymmetric private key into keys manager
223 for( i
= 0 ; ( priKey
= pSecEnv
->getPriKey( i
) ) != NULL
; i
++ ) {
224 if( xmlSecNssAppliedKeysMngrPriKeyLoad( m_pKeysMngr
, priKey
) < 0 ) {
225 throw RuntimeException() ;
230 /* XXMLSecurityContext */
231 Reference
< XSecurityEnvironment
> SAL_CALL
XMLSecurityContext_NssImpl :: getSecurityEnvironment()
232 throw (RuntimeException
)
234 return m_xSecurityEnvironment
;
239 /* XInitialization */
240 void SAL_CALL
XMLSecurityContext_NssImpl :: initialize( const Sequence
< Any
>& /*aArguments*/ ) throw( Exception
, RuntimeException
) {
245 OUString SAL_CALL
XMLSecurityContext_NssImpl :: getImplementationName() throw( RuntimeException
) {
246 return impl_getImplementationName() ;
250 sal_Bool SAL_CALL
XMLSecurityContext_NssImpl :: supportsService( const OUString
& serviceName
) throw( RuntimeException
) {
251 Sequence
< OUString
> seqServiceNames
= getSupportedServiceNames() ;
252 const OUString
* pArray
= seqServiceNames
.getConstArray() ;
253 for( sal_Int32 i
= 0 ; i
< seqServiceNames
.getLength() ; i
++ ) {
254 if( *( pArray
+ i
) == serviceName
)
261 Sequence
< OUString
> SAL_CALL
XMLSecurityContext_NssImpl :: getSupportedServiceNames() throw( RuntimeException
) {
262 return impl_getSupportedServiceNames() ;
265 //Helper for XServiceInfo
266 Sequence
< OUString
> XMLSecurityContext_NssImpl :: impl_getSupportedServiceNames() {
267 ::osl::Guard
< ::osl::Mutex
> aGuard( ::osl::Mutex::getGlobalMutex() ) ;
268 Sequence
< OUString
> seqServiceNames( 1 ) ;
269 seqServiceNames
.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
270 return seqServiceNames
;
273 OUString
XMLSecurityContext_NssImpl :: impl_getImplementationName() throw( RuntimeException
) {
274 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl" ) ;
277 //Helper for registry
278 Reference
< XInterface
> SAL_CALL
XMLSecurityContext_NssImpl :: impl_createInstance( const Reference
< XMultiServiceFactory
>& aServiceManager
) throw( RuntimeException
) {
279 return Reference
< XInterface
>( *new XMLSecurityContext_NssImpl( aServiceManager
) ) ;
282 Reference
< XSingleServiceFactory
> XMLSecurityContext_NssImpl :: impl_createFactory( const Reference
< XMultiServiceFactory
>& aServiceManager
) {
283 //Reference< XSingleServiceFactory > xFactory ;
284 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
286 return ::cppu::createSingleFactory( aServiceManager
, impl_getImplementationName() , impl_createInstance
, impl_getSupportedServiceNames() ) ;
289 #if 0 //not useful any longer
291 sal_Int64 SAL_CALL
XMLSecurityContext_NssImpl :: getSomething( const Sequence
< sal_Int8
>& aIdentifier
)
292 throw (RuntimeException
)
294 if( aIdentifier
.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier
.getConstArray(), 16 ) ) {
295 return ( sal_Int64
)this ;
300 /* XUnoTunnel extension */
301 const Sequence
< sal_Int8
>& XMLSecurityContext_NssImpl :: getUnoTunnelId() {
302 static Sequence
< sal_Int8
>* pSeq
= 0 ;
304 ::osl::Guard
< ::osl::Mutex
> aGuard( ::osl::Mutex::getGlobalMutex() ) ;
306 static Sequence
< sal_Int8
> aSeq( 16 ) ;
307 rtl_createUuid( ( sal_uInt8
* )aSeq
.getArray() , 0 , sal_True
) ;
314 /* XUnoTunnel extension */
315 XMLSecurityContext_NssImpl
* XMLSecurityContext_NssImpl :: getImplementation( const Reference
< XInterface
> xObj
) {
316 Reference
< XUnoTunnel
> xUT( xObj
, UNO_QUERY
) ;
318 return ( XMLSecurityContext_NssImpl
* )xUT
->getSomething( getUnoTunnelId() ) ;
324 xmlSecKeysMngrPtr
XMLSecurityContext_NssImpl :: keysManager() throw( Exception
, RuntimeException
) {