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>
32 #include "securityenvironment_nssimpl.hxx"
34 #ifndef _XMLSECURITYCONTEXT_NSSIMPL_HXX_
35 #include "xmlsecuritycontext_nssimpl.hxx"
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
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 )
64 if( xmlSecInit() < 0 ) {
65 throw RuntimeException() ;
68 //Init xmlsec crypto engine library
69 if( xmlSecCryptoInit() < 0 ) {
71 throw RuntimeException() ;
74 //Enable external stream handlers
75 if( xmlEnableStreamInputCallbacks() < 0 ) {
76 xmlSecCryptoShutdown() ;
78 throw RuntimeException() ;
82 XMLSecurityContext_NssImpl :: ~XMLSecurityContext_NssImpl() {
84 if( m_pKeysMngr
!= NULL
) {
85 xmlSecKeysMngrDestroy( m_pKeysMngr
) ;
89 xmlDisableStreamInputCallbacks() ;
90 xmlSecCryptoShutdown() ;
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
];
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
);
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
) {
158 CERTCertDBHandle
* handler
;
160 //xmlSecKeyDataPtr keyData ;
162 SECKEYPublicKey
* pubKey
;
163 SECKEYPrivateKey
* priKey
;
166 if( !aSecurityEnvironment
.is() )
167 throw RuntimeException() ;
169 m_xSecurityEnvironment
= aSecurityEnvironment
;
172 if( m_pKeysMngr
!= NULL
) {
173 xmlSecKeysMngrDestroy( m_pKeysMngr
) ;
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() ;
188 // slot = pSecEnv->getCryptoSlot() ;
189 handler
= pSecEnv
->getCertDb() ;
192 * The following lines is based on the private version of xmlSec-NSS
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
;
236 /* XInitialization */
237 void SAL_CALL
XMLSecurityContext_NssImpl :: initialize( const Sequence
< Any
>& /*aArguments*/ ) throw( Exception
, RuntimeException
) {
242 OUString SAL_CALL
XMLSecurityContext_NssImpl :: getImplementationName() throw( RuntimeException
) {
243 return impl_getImplementationName() ;
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
)
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 ) ;
283 return ::cppu::createSingleFactory( aServiceManager
, impl_getImplementationName() , impl_createInstance
, impl_getSupportedServiceNames() ) ;
286 #if 0 //not useful any longer
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 ;
297 /* XUnoTunnel extension */
298 const Sequence
< sal_Int8
>& XMLSecurityContext_NssImpl :: getUnoTunnelId() {
299 static Sequence
< sal_Int8
>* pSeq
= 0 ;
301 ::osl::Guard
< ::osl::Mutex
> aGuard( ::osl::Mutex::getGlobalMutex() ) ;
303 static Sequence
< sal_Int8
> aSeq( 16 ) ;
304 rtl_createUuid( ( sal_uInt8
* )aSeq
.getArray() , 0 , sal_True
) ;
311 /* XUnoTunnel extension */
312 XMLSecurityContext_NssImpl
* XMLSecurityContext_NssImpl :: getImplementation( const Reference
< XInterface
> xObj
) {
313 Reference
< XUnoTunnel
> xUT( xObj
, UNO_QUERY
) ;
315 return ( XMLSecurityContext_NssImpl
* )xUT
->getSomething( getUnoTunnelId() ) ;
321 xmlSecKeysMngrPtr
XMLSecurityContext_NssImpl :: keysManager() throw( Exception
, RuntimeException
) {