LibreOffice Module xmlsecurity (master) 1
seinitializer_mscryptimpl.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21#include <xmlsec-wrapper.h>
22
24
26
27#include <xmlsec/mscng/app.h>
28#include <com/sun/star/xml/crypto/SecurityEnvironment.hpp>
29#include <com/sun/star/xml/crypto/XMLSecurityContext.hpp>
32#include <svl/cryptosign.hxx>
33
34using namespace com::sun::star;
35namespace cssl = com::sun::star::lang;
37
39 const uno::Reference< uno::XComponentContext > &rxContext)
40 :mxContext( rxContext )
41{
42}
43
45{
46}
47
48/* XSEInitializer */
49uno::Reference< cssxc::XXMLSecurityContext > SAL_CALL
51 const OUString& sCertDB )
52{
53 const char* n_pCertStore ;
54 HCERTSTORE n_hStoreHandle ;
55 OString sCertDir;
56
57 //Initialize the crypto engine
58 if( sCertDB.getLength() > 0 )
59 {
60 sCertDir = OUStringToOString(sCertDB, RTL_TEXTENCODING_ASCII_US);
61 n_pCertStore = sCertDir.getStr();
62 n_hStoreHandle = CertOpenSystemStoreW( 0, o3tl::toW(sCertDB.getStr())) ;
63 if( n_hStoreHandle == nullptr )
64 {
65 return nullptr;
66 }
67 }
68 else
69 {
70 n_pCertStore = nullptr ;
71 n_hStoreHandle = nullptr ;
72 }
73
74 xmlSecMSCngAppInit(n_pCertStore);
75
76 try {
77 /* Build Security Environment */
78 uno::Reference< cssxc::XSecurityEnvironment > xSecEnv = cssxc::SecurityEnvironment::create( mxContext );
79
80 /* Setup key slot and certDb */
81 SecurityEnvironment_MSCryptImpl* pSecEnv = dynamic_cast<SecurityEnvironment_MSCryptImpl*>(xSecEnv.get());
82 if( pSecEnv == nullptr )
83 {
84 if( n_hStoreHandle != nullptr )
85 {
86 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
87 }
88
89 xmlSecMSCngAppShutdown();
90 return nullptr;
91 }
92
93 if( n_hStoreHandle != nullptr )
94 {
95 pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
96 pSecEnv->setCertDb( n_hStoreHandle ) ;
97 }
98 else
99 {
100 pSecEnv->enableDefaultCrypt( true ) ;
101 }
102
103 /* Build XML Security Context */
104 uno::Reference< cssxc::XXMLSecurityContext > xSecCtx = cssxc::XMLSecurityContext::create( mxContext );
105
106 xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ;
107 return xSecCtx;
108 }
109 catch( uno::Exception& )
110 {
111 if( n_hStoreHandle != nullptr )
112 {
113 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
114 }
115
116 xmlSecMSCngAppShutdown();
117 return nullptr;
118 }
119}
120
121void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const uno::Reference< cssxc::XXMLSecurityContext >&)
122{
123 /*
124 uno::Reference< cssxc::XSecurityEnvironment > xSecEnv
125 = securityContext->getSecurityEnvironment();
126
127 if( xSecEnv.is() )
128 {
129 uno::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , uno::UNO_QUERY ) ;
130 if (auto pSecEnv = comphelper::getFromUnoTunnel<SecurityEnvironment_MSCryptImpl>(xEnvTunnel))
131 {
132 HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot();
133
134 if( n_hStoreHandle != NULL )
135 {
136 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
137 pSecEnv->setCryptoSlot( NULL ) ;
138 pSecEnv->setCertDb( NULL ) ;
139 }
140
141 xmlSecMSCryptoAppShutdown() ;
142 }
143 }
144 */
145
146 xmlSecMSCngAppShutdown();
147}
148
149/* XServiceInfo */
151{
152 return "com.sun.star.xml.crypto.SEInitializer";
153}
154
155sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const OUString& rServiceName )
156{
157 return cppu::supportsService( this, rServiceName );
158}
159
160uno::Sequence< OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames()
161{
162 return { "com.sun.star.xml.crypto.SEInitializer" };
163}
164
165extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
167 uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
168{
169 return cppu::acquire(new SEInitializer_MSCryptImpl(pCtx));
170}
171
172/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual OUString SAL_CALL getImplementationName() override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL freeSecurityContext(const css::uno::Reference< css::xml::crypto::XXMLSecurityContext > &securityContext) override
virtual css::uno::Reference< css::xml::crypto::XXMLSecurityContext > SAL_CALL createSecurityContext(const OUString &certDB) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
SEInitializer_MSCryptImpl(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual ~SEInitializer_MSCryptImpl() override
uno::Reference< uno::XComponentContext > mxContext
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_xml_crypto_SEInitializer_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
unsigned char sal_Bool