Update ooo320-m1
[ooovba.git] / xmlsecurity / source / xmlsec / mscrypt / xmlsecuritycontext_mscryptimpl.cxx
blob74e2ae646d2416836a67b9c59d9f0f13935ef25f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlsecuritycontext_mscryptimpl.cxx,v $
10 * $Revision: 1.6 $
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>
34 #include <rtl/uuid.h>
35 #include "securityenvironment_mscryptimpl.hxx"
37 #ifndef _XMLSECURITYCONTEXT_MSCRYPTIMPL_HXX_
38 #include "xmlsecuritycontext_mscryptimpl.hxx"
39 #endif
40 #include "xmlstreamio.hxx"
42 #include "xmlsec/xmlsec.h"
43 #include "xmlsec/keysmngr.h"
44 #include "xmlsec/crypto.h"
45 #include "xmlsec/mscrypto/akmngr.h"
47 using namespace ::com::sun::star::uno ;
48 using namespace ::com::sun::star::lang ;
49 using ::com::sun::star::lang::XMultiServiceFactory ;
50 using ::com::sun::star::lang::XSingleServiceFactory ;
51 using ::rtl::OUString ;
53 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
54 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
56 XMLSecurityContext_MSCryptImpl :: XMLSecurityContext_MSCryptImpl( const Reference< XMultiServiceFactory >& aFactory )
57 ://m_pKeysMngr( NULL ) ,
58 m_xServiceManager( aFactory ),
59 m_xSecurityEnvironment( NULL )
61 //Init xmlsec library
62 if( xmlSecInit() < 0 ) {
63 throw RuntimeException() ;
66 //Init xmlsec crypto engine library
67 if( xmlSecCryptoInit() < 0 ) {
68 xmlSecShutdown() ;
69 throw RuntimeException() ;
72 //Enable external stream handlers
73 if( xmlEnableStreamInputCallbacks() < 0 ) {
74 xmlSecCryptoShutdown() ;
75 xmlSecShutdown() ;
76 throw RuntimeException() ;
80 XMLSecurityContext_MSCryptImpl :: ~XMLSecurityContext_MSCryptImpl() {
81 xmlDisableStreamInputCallbacks() ;
82 xmlSecCryptoShutdown() ;
83 xmlSecShutdown() ;
86 //i39448 : new methods
87 sal_Int32 SAL_CALL XMLSecurityContext_MSCryptImpl::addSecurityEnvironment(
88 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >& aSecurityEnvironment)
89 throw (::com::sun::star::security::SecurityInfrastructureException, ::com::sun::star::uno::RuntimeException)
91 if( !aSecurityEnvironment.is() )
93 throw RuntimeException() ;
96 m_xSecurityEnvironment = aSecurityEnvironment;
98 return 0;
102 sal_Int32 SAL_CALL XMLSecurityContext_MSCryptImpl::getSecurityEnvironmentNumber( )
103 throw (::com::sun::star::uno::RuntimeException)
105 return 1;
108 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
109 XMLSecurityContext_MSCryptImpl::getSecurityEnvironmentByIndex( sal_Int32 index )
110 throw (::com::sun::star::uno::RuntimeException)
112 if (index == 0)
114 return m_xSecurityEnvironment;
116 else
117 throw RuntimeException() ;
120 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
121 XMLSecurityContext_MSCryptImpl::getSecurityEnvironment( )
122 throw (::com::sun::star::uno::RuntimeException)
124 return m_xSecurityEnvironment;
127 sal_Int32 SAL_CALL XMLSecurityContext_MSCryptImpl::getDefaultSecurityEnvironmentIndex( )
128 throw (::com::sun::star::uno::RuntimeException)
130 return 0;
133 void SAL_CALL XMLSecurityContext_MSCryptImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 /*nDefaultEnvIndex*/ )
134 throw (::com::sun::star::uno::RuntimeException)
136 //dummy
139 #if 0
140 /* XXMLSecurityContext */
141 void SAL_CALL XMLSecurityContext_MSCryptImpl :: setSecurityEnvironment( const Reference< XSecurityEnvironment >& aSecurityEnvironment ) throw( com::sun::star::security::SecurityInfrastructureException ) {
142 HCERTSTORE hkeyStore ;
143 HCERTSTORE hCertStore ;
144 HCRYPTKEY symKey ;
145 HCRYPTKEY pubKey ;
146 HCRYPTKEY priKey ;
147 unsigned int i ;
149 if( !aSecurityEnvironment.is() )
150 throw RuntimeException() ;
152 m_xSecurityEnvironment = aSecurityEnvironment ;
154 //Clear key manager
155 if( m_pKeysMngr != NULL ) {
156 xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
157 m_pKeysMngr = NULL ;
160 //Create key manager
161 Reference< XUnoTunnel > xEnvTunnel( m_xSecurityEnvironment , UNO_QUERY ) ;
162 if( !xEnvTunnel.is() ) {
163 throw RuntimeException() ;
166 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
167 if( pSecEnv == NULL )
168 throw RuntimeException() ;
170 hkeyStore = pSecEnv->getCryptoSlot() ;
171 hCertStore = pSecEnv->getCertDb() ;
174 * The following lines is based on the of xmlsec-mscrypto crypto engine
176 m_pKeysMngr = xmlSecMSCryptoAppliedKeysMngrCreate( hkeyStore , hCertStore ) ;
177 if( m_pKeysMngr == NULL )
178 throw RuntimeException() ;
181 * Adopt symmetric key into keys manager
183 for( i = 0 ; ( symKey = pSecEnv->getSymKey( i ) ) != NULL ; i ++ ) {
184 if( xmlSecMSCryptoAppliedKeysMngrSymKeyLoad( m_pKeysMngr, symKey ) < 0 ) {
185 throw RuntimeException() ;
190 * Adopt asymmetric public key into keys manager
192 for( i = 0 ; ( pubKey = pSecEnv->getPubKey( i ) ) != NULL ; i ++ ) {
193 if( xmlSecMSCryptoAppliedKeysMngrPubKeyLoad( m_pKeysMngr, pubKey ) < 0 ) {
194 throw RuntimeException() ;
199 * Adopt asymmetric private key into keys manager
201 for( i = 0 ; ( priKey = pSecEnv->getPriKey( i ) ) != NULL ; i ++ ) {
202 if( xmlSecMSCryptoAppliedKeysMngrPriKeyLoad( m_pKeysMngr, priKey ) < 0 ) {
203 throw RuntimeException() ;
208 * Adopt system default certificate store.
210 if( pSecEnv->defaultEnabled() ) {
211 HCERTSTORE hSystemStore ;
213 //Add system key store into the keys manager.
214 hSystemStore = CertOpenSystemStore( 0, "MY" ) ;
215 if( hSystemStore != NULL ) {
216 if( xmlSecMSCryptoAppliedKeysMngrAdoptKeyStore( m_pKeysMngr, hSystemStore ) < 0 ) {
217 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
218 throw RuntimeException() ;
222 //Add system root store into the keys manager.
223 hSystemStore = CertOpenSystemStore( 0, "Root" ) ;
224 if( hSystemStore != NULL ) {
225 if( xmlSecMSCryptoAppliedKeysMngrAdoptTrustedStore( m_pKeysMngr, hSystemStore ) < 0 ) {
226 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
227 throw RuntimeException() ;
231 //Add system trusted store into the keys manager.
232 hSystemStore = CertOpenSystemStore( 0, "Trust" ) ;
233 if( hSystemStore != NULL ) {
234 if( xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore( m_pKeysMngr, hSystemStore ) < 0 ) {
235 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
236 throw RuntimeException() ;
240 //Add system CA store into the keys manager.
241 hSystemStore = CertOpenSystemStore( 0, "CA" ) ;
242 if( hSystemStore != NULL ) {
243 if( xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore( m_pKeysMngr, hSystemStore ) < 0 ) {
244 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
245 throw RuntimeException() ;
251 /* XXMLSecurityContext */
252 Reference< XSecurityEnvironment > SAL_CALL XMLSecurityContext_MSCryptImpl :: getSecurityEnvironment()
253 throw (RuntimeException)
255 return m_xSecurityEnvironment ;
257 #endif
259 /* XInitialization */
260 void SAL_CALL XMLSecurityContext_MSCryptImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
261 // TBD
264 /* XServiceInfo */
265 OUString SAL_CALL XMLSecurityContext_MSCryptImpl :: getImplementationName() throw( RuntimeException ) {
266 return impl_getImplementationName() ;
269 /* XServiceInfo */
270 sal_Bool SAL_CALL XMLSecurityContext_MSCryptImpl :: supportsService( const OUString& serviceName) throw( RuntimeException ) {
271 Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
272 const OUString* pArray = seqServiceNames.getConstArray() ;
273 for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
274 if( *( pArray + i ) == serviceName )
275 return sal_True ;
277 return sal_False ;
280 /* XServiceInfo */
281 Sequence< OUString > SAL_CALL XMLSecurityContext_MSCryptImpl :: getSupportedServiceNames() throw( RuntimeException ) {
282 return impl_getSupportedServiceNames() ;
285 //Helper for XServiceInfo
286 Sequence< OUString > XMLSecurityContext_MSCryptImpl :: impl_getSupportedServiceNames() {
287 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
288 Sequence< OUString > seqServiceNames( 1 ) ;
289 seqServiceNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
290 return seqServiceNames ;
293 OUString XMLSecurityContext_MSCryptImpl :: impl_getImplementationName() throw( RuntimeException ) {
294 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl" ) ;
297 //Helper for registry
298 Reference< XInterface > SAL_CALL XMLSecurityContext_MSCryptImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
299 return Reference< XInterface >( *new XMLSecurityContext_MSCryptImpl( aServiceManager ) ) ;
302 Reference< XSingleServiceFactory > XMLSecurityContext_MSCryptImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
303 //Reference< XSingleServiceFactory > xFactory ;
304 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
305 //return xFactory ;
306 return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
309 #if 0
310 /* XUnoTunnel */
311 sal_Int64 SAL_CALL XMLSecurityContext_MSCryptImpl :: getSomething( const Sequence< sal_Int8 >& aIdentifier )
312 throw (RuntimeException)
314 if( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
315 return ( sal_Int64 )this ;
317 return 0 ;
320 /* XUnoTunnel extension */
321 const Sequence< sal_Int8>& XMLSecurityContext_MSCryptImpl :: getUnoTunnelId() {
322 static Sequence< sal_Int8 >* pSeq = 0 ;
323 if( !pSeq ) {
324 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
325 if( !pSeq ) {
326 static Sequence< sal_Int8> aSeq( 16 ) ;
327 rtl_createUuid( ( sal_uInt8* )aSeq.getArray() , 0 , sal_True ) ;
328 pSeq = &aSeq ;
331 return *pSeq ;
334 /* XUnoTunnel extension */
335 XMLSecurityContext_MSCryptImpl* XMLSecurityContext_MSCryptImpl :: getImplementation( const Reference< XInterface > xObj ) {
336 Reference< XUnoTunnel > xUT( xObj , UNO_QUERY ) ;
337 if( xUT.is() ) {
338 return ( XMLSecurityContext_MSCryptImpl* )xUT->getSomething( getUnoTunnelId() ) ;
339 } else
340 return NULL ;
343 /* Native methods */
344 xmlSecKeysMngrPtr XMLSecurityContext_MSCryptImpl :: keysManager() throw( Exception, RuntimeException ) {
345 return m_pKeysMngr ;
347 #endif