1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "seinitializer_mscryptimpl.hxx"
23 #include "securityenvironment_mscryptimpl.hxx"
25 #include "xmlsecurity/xmlsec-wrapper.h"
26 #include "xmlsec/mscrypto/app.h"
27 #include <com/sun/star/xml/crypto/SecurityEnvironment.hpp>
28 #include <com/sun/star/xml/crypto/XMLSecurityContext.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 namespace cssu
= com::sun::star::uno
;
33 namespace cssl
= com::sun::star::lang
;
34 namespace cssxc
= com::sun::star::xml::crypto
;
36 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
37 const cssu::Reference
< cssu::XComponentContext
> &rxContext
)
38 :mxContext( rxContext
)
42 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
47 cssu::Reference
< cssxc::XXMLSecurityContext
> SAL_CALL
48 SEInitializer_MSCryptImpl::createSecurityContext(
49 const OUString
& sCertDB
)
50 throw (cssu::RuntimeException
)
52 const char* n_pCertStore
;
53 HCERTSTORE n_hStoreHandle
;
55 //Initialize the crypto engine
56 if( sCertDB
.getLength() > 0 )
58 OString
sCertDir(sCertDB
.getStr(), sCertDB
.getLength(), RTL_TEXTENCODING_ASCII_US
);
59 n_pCertStore
= sCertDir
.getStr();
60 n_hStoreHandle
= CertOpenSystemStore( NULL
, n_pCertStore
) ;
61 if( n_hStoreHandle
== NULL
)
69 n_hStoreHandle
= NULL
;
72 xmlSecMSCryptoAppInit( n_pCertStore
) ;
75 /* Build Security Environment */
76 cssu::Reference
< cssxc::XSecurityEnvironment
> xSecEnv
= cssxc::SecurityEnvironment::create( mxContext
);
78 /* Setup key slot and certDb */
79 cssu::Reference
< cssl::XUnoTunnel
> xSecEnvTunnel( xSecEnv
, cssu::UNO_QUERY_THROW
);
80 SecurityEnvironment_MSCryptImpl
* pSecEnv
= ( SecurityEnvironment_MSCryptImpl
* )xSecEnvTunnel
->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
83 if( n_hStoreHandle
!= NULL
)
85 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
88 xmlSecMSCryptoAppShutdown() ;
92 if( n_hStoreHandle
!= NULL
)
94 pSecEnv
->setCryptoSlot( n_hStoreHandle
) ;
95 pSecEnv
->setCertDb( n_hStoreHandle
) ;
99 pSecEnv
->enableDefaultCrypt( sal_True
) ;
102 /* Build XML Security Context */
103 cssu::Reference
< cssxc::XXMLSecurityContext
> xSecCtx
= cssxc::XMLSecurityContext::create( mxContext
);
105 xSecCtx
->setDefaultSecurityEnvironmentIndex(xSecCtx
->addSecurityEnvironment( xSecEnv
)) ;
108 catch( cssu::Exception
& )
110 if( n_hStoreHandle
!= NULL
)
112 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
115 xmlSecMSCryptoAppShutdown() ;
120 void SAL_CALL
SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference
< cssxc::XXMLSecurityContext
>&)
121 throw (cssu::RuntimeException
)
124 cssu::Reference< cssxc::XSecurityEnvironment > xSecEnv
125 = securityContext->getSecurityEnvironment();
129 cssu::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , cssu::UNO_QUERY ) ;
130 if( xEnvTunnel.is() )
132 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
133 HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
135 if( n_hStoreHandle != NULL )
137 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
138 pSecEnv->setCryptoSlot( NULL ) ;
139 pSecEnv->setCertDb( NULL ) ;
142 xmlSecMSCryptoAppShutdown() ;
147 xmlSecMSCryptoAppShutdown() ;
150 OUString
SEInitializer_MSCryptImpl_getImplementationName()
151 throw (cssu::RuntimeException
)
153 return OUString( "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_MSCryptImpl" );
156 cssu::Sequence
< OUString
> SAL_CALL
SEInitializer_MSCryptImpl_getSupportedServiceNames()
157 throw (cssu::RuntimeException
)
159 cssu::Sequence
< OUString
> aRet(1);
160 OUString
* pArray
= aRet
.getArray();
161 pArray
[0] = "com.sun.star.xml.crypto.SEInitializer";
165 cssu::Reference
< cssu::XInterface
> SAL_CALL
SEInitializer_MSCryptImpl_createInstance( const cssu::Reference
< cssl::XMultiServiceFactory
> & rSMgr
)
166 throw( cssu::Exception
)
168 return (cppu::OWeakObject
*) new SEInitializer_MSCryptImpl( comphelper::getComponentContext(rSMgr
) );
172 OUString SAL_CALL
SEInitializer_MSCryptImpl::getImplementationName()
173 throw (cssu::RuntimeException
)
175 return SEInitializer_MSCryptImpl_getImplementationName();
178 sal_Bool SAL_CALL
SEInitializer_MSCryptImpl::supportsService( const OUString
& rServiceName
)
179 throw (cssu::RuntimeException
)
181 return cppu::supportsService( this, rServiceName
);
184 cssu::Sequence
< OUString
> SAL_CALL
SEInitializer_MSCryptImpl::getSupportedServiceNames()
185 throw (cssu::RuntimeException
)
187 return SEInitializer_MSCryptImpl_getSupportedServiceNames();
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */