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