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 .
20 #include <sal/config.h>
22 #include "seinitializer_mscryptimpl.hxx"
24 #include "securityenvironment_mscryptimpl.hxx"
26 #include <xmlsec/mscng/app.h>
27 #include <com/sun/star/xml/crypto/SecurityEnvironment.hpp>
28 #include <com/sun/star/xml/crypto/XMLSecurityContext.hpp>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <o3tl/char16_t2wchar_t.hxx>
31 #include <svl/cryptosign.hxx>
33 using namespace com::sun::star
;
34 namespace cssl
= com::sun::star::lang
;
35 namespace cssxc
= com::sun::star::xml::crypto
;
37 SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl(
38 const uno::Reference
< uno::XComponentContext
> &rxContext
)
39 :mxContext( rxContext
)
43 SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl()
48 uno::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
= OUStringToOString(sCertDB
, RTL_TEXTENCODING_ASCII_US
);
60 n_pCertStore
= sCertDir
.getStr();
61 n_hStoreHandle
= CertOpenSystemStoreW( 0, o3tl::toW(sCertDB
.getStr())) ;
62 if( n_hStoreHandle
== nullptr )
69 n_pCertStore
= nullptr ;
70 n_hStoreHandle
= nullptr ;
73 xmlSecMSCngAppInit(n_pCertStore
);
76 /* Build Security Environment */
77 uno::Reference
< cssxc::XSecurityEnvironment
> xSecEnv
= cssxc::SecurityEnvironment::create( mxContext
);
79 /* Setup key slot and certDb */
80 SecurityEnvironment_MSCryptImpl
* pSecEnv
= dynamic_cast<SecurityEnvironment_MSCryptImpl
*>(xSecEnv
.get());
81 if( pSecEnv
== nullptr )
83 if( n_hStoreHandle
!= nullptr )
85 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
88 xmlSecMSCngAppShutdown();
92 if( n_hStoreHandle
!= nullptr )
94 pSecEnv
->setCryptoSlot( n_hStoreHandle
) ;
95 pSecEnv
->setCertDb( n_hStoreHandle
) ;
99 pSecEnv
->enableDefaultCrypt( true ) ;
102 /* Build XML Security Context */
103 uno::Reference
< cssxc::XXMLSecurityContext
> xSecCtx
= cssxc::XMLSecurityContext::create( mxContext
);
105 xSecCtx
->setDefaultSecurityEnvironmentIndex(xSecCtx
->addSecurityEnvironment( xSecEnv
)) ;
108 catch( uno::Exception
& )
110 if( n_hStoreHandle
!= nullptr )
112 CertCloseStore( n_hStoreHandle
, CERT_CLOSE_STORE_FORCE_FLAG
) ;
115 xmlSecMSCngAppShutdown();
120 void SAL_CALL
SEInitializer_MSCryptImpl::freeSecurityContext( const uno::Reference
< cssxc::XXMLSecurityContext
>&)
123 uno::Reference< cssxc::XSecurityEnvironment > xSecEnv
124 = securityContext->getSecurityEnvironment();
128 uno::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , uno::UNO_QUERY ) ;
129 if (auto pSecEnv = comphelper::getFromUnoTunnel<SecurityEnvironment_MSCryptImpl>(xEnvTunnel))
131 HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
133 if( n_hStoreHandle != NULL )
135 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
136 pSecEnv->setCryptoSlot( NULL ) ;
137 pSecEnv->setCertDb( NULL ) ;
140 xmlSecMSCryptoAppShutdown() ;
145 xmlSecMSCngAppShutdown();
149 OUString SAL_CALL
SEInitializer_MSCryptImpl::getImplementationName()
151 return "com.sun.star.xml.crypto.SEInitializer";
154 sal_Bool SAL_CALL
SEInitializer_MSCryptImpl::supportsService( const OUString
& rServiceName
)
156 return cppu::supportsService( this, rServiceName
);
159 uno::Sequence
< OUString
> SAL_CALL
SEInitializer_MSCryptImpl::getSupportedServiceNames()
161 return { "com.sun.star.xml.crypto.SEInitializer" };
164 extern "C" SAL_DLLPUBLIC_EXPORT
uno::XInterface
*
165 com_sun_star_xml_crypto_SEInitializer_get_implementation(
166 uno::XComponentContext
* pCtx
, uno::Sequence
<uno::Any
> const& /*rSeq*/)
168 return cppu::acquire(new SEInitializer_MSCryptImpl(pCtx
));
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */