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 <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>
31 #include <o3tl/char16_t2wchar_t.hxx>
33 namespace cssu
= com::sun::star::uno
;
34 namespace cssl
= com::sun::star::lang
;
35 namespace cssxc
= com::sun::star::xml::crypto
;
37 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
38 const cssu::Reference
< cssu::XComponentContext
> &rxContext
)
39 :mxContext( rxContext
)
43 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
48 cssu::Reference
< cssxc::XXMLSecurityContext
> SAL_CALL
49 SEInitializer_MSCryptImpl::createSecurityContext(
50 const OUString
& sCertDB
)
52 const char* n_pCertStore
;
53 HCERTSTORE n_hStoreHandle
;
56 //Initialize the crypto engine
57 if( sCertDB
.getLength() > 0 )
59 sCertDir
= OString(sCertDB
.getStr(), sCertDB
.getLength(), RTL_TEXTENCODING_ASCII_US
);
60 n_pCertStore
= sCertDir
.getStr();
61 n_hStoreHandle
= CertOpenSystemStoreW( NULL
, o3tl::toW(sCertDB
.getStr())) ;
62 if( n_hStoreHandle
== nullptr )
69 n_pCertStore
= nullptr ;
70 n_hStoreHandle
= nullptr ;
73 xmlSecMSCryptoAppInit( n_pCertStore
) ;
76 /* Build Security Environment */
77 cssu::Reference
< cssxc::XSecurityEnvironment
> xSecEnv
= cssxc::SecurityEnvironment::create( mxContext
);
79 /* Setup key slot and certDb */
80 cssu::Reference
< cssl::XUnoTunnel
> xSecEnvTunnel( xSecEnv
, cssu::UNO_QUERY_THROW
);
81 SecurityEnvironment_MSCryptImpl
* pSecEnv
= reinterpret_cast<SecurityEnvironment_MSCryptImpl
*>(xSecEnvTunnel
->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ));
82 if( pSecEnv
== nullptr )
84 if( n_hStoreHandle
!= nullptr )
86 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
89 xmlSecMSCryptoAppShutdown() ;
93 if( n_hStoreHandle
!= nullptr )
95 pSecEnv
->setCryptoSlot( n_hStoreHandle
) ;
96 pSecEnv
->setCertDb( n_hStoreHandle
) ;
100 pSecEnv
->enableDefaultCrypt( true ) ;
103 /* Build XML Security Context */
104 cssu::Reference
< cssxc::XXMLSecurityContext
> xSecCtx
= cssxc::XMLSecurityContext::create( mxContext
);
106 xSecCtx
->setDefaultSecurityEnvironmentIndex(xSecCtx
->addSecurityEnvironment( xSecEnv
)) ;
109 catch( cssu::Exception
& )
111 if( n_hStoreHandle
!= nullptr )
113 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
116 xmlSecMSCryptoAppShutdown() ;
121 void SAL_CALL
SEInitializer_MSCryptImpl::freeSecurityContext( const cssu::Reference
< cssxc::XXMLSecurityContext
>&)
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()
152 return OUString( "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_MSCryptImpl" );
155 cssu::Sequence
< OUString
> SAL_CALL
SEInitializer_MSCryptImpl_getSupportedServiceNames()
157 cssu::Sequence
<OUString
> aRet
{ "com.sun.star.xml.crypto.SEInitializer" };
161 cssu::Reference
< cssu::XInterface
> SAL_CALL
SEInitializer_MSCryptImpl_createInstance( const cssu::Reference
< cssl::XMultiServiceFactory
> & rSMgr
)
163 return static_cast<cppu::OWeakObject
*>(new SEInitializer_MSCryptImpl( comphelper::getComponentContext(rSMgr
) ));
167 OUString SAL_CALL
SEInitializer_MSCryptImpl::getImplementationName()
169 return SEInitializer_MSCryptImpl_getImplementationName();
172 sal_Bool SAL_CALL
SEInitializer_MSCryptImpl::supportsService( const OUString
& rServiceName
)
174 return cppu::supportsService( this, rServiceName
);
177 cssu::Sequence
< OUString
> SAL_CALL
SEInitializer_MSCryptImpl::getSupportedServiceNames()
179 return SEInitializer_MSCryptImpl_getSupportedServiceNames();
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */