| 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 |
|
| 22 |
#include "seinitializer_mscryptimpl.hxx" |
| 23 |
|
| 24 |
#include "securityenvironment_mscryptimpl.hxx" |
| 25 |
|
| 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> |
| 32 |
|
| 33 |
using namespace com::sun::star; |
| 34 |
namespace cssl = com::sun::star::lang; |
| 35 |
namespace cssxc = com::sun::star::xml::crypto; |
| 36 |
|
| 37 |
SEInitializer_MSCryptImpl::SEInitializer_MSCryptImpl( |
| 38 |
const uno::Reference< uno::XComponentContext > &rxContext) |
| 39 |
:mxContext( rxContext ) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
SEInitializer_MSCryptImpl::~SEInitializer_MSCryptImpl() |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
/* XSEInitializer */ |
| 48 |
uno::Reference< cssxc::XXMLSecurityContext > SAL_CALL |
| 49 |
SEInitializer_MSCryptImpl::createSecurityContext( |
| 50 |
const OUString& sCertDB ) |
| 51 |
{ |
| 52 |
const char* n_pCertStore ; |
| 53 |
HCERTSTORE n_hStoreHandle ; |
| 54 |
OString sCertDir; |
| 55 |
|
| 56 |
//Initialize the crypto engine |
| 57 |
if( sCertDB.getLength() > 0 ) |
| 58 |
{ |
| 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 ) |
| 63 |
{ |
| 64 |
return nullptr; |
| 65 |
} |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
n_pCertStore = nullptr ; |
| 70 |
n_hStoreHandle = nullptr ; |
| 71 |
} |
| 72 |
|
| 73 |
xmlSecMSCngAppInit(n_pCertStore); |
| 74 |
|
| 75 |
try { |
| 76 |
/* Build Security Environment */ |
| 77 |
uno::Reference< cssxc::XSecurityEnvironment > xSecEnv = cssxc::SecurityEnvironment::create( mxContext ); |
| 78 |
|
| 79 |
/* Setup key slot and certDb */ |
| 80 |
SecurityEnvironment_MSCryptImpl* pSecEnv = dynamic_cast<SecurityEnvironment_MSCryptImpl*>(xSecEnv.get()); |
| 81 |
if( pSecEnv == nullptr ) |
| 82 |
{ |
| 83 |
if( n_hStoreHandle != nullptr ) |
| 84 |
{ |
| 85 |
CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; |
| 86 |
} |
| 87 |
|
| 88 |
xmlSecMSCngAppShutdown(); |
| 89 |
return nullptr; |
| 90 |
} |
| 91 |
|
| 92 |
if( n_hStoreHandle != nullptr ) |
| 93 |
{ |
| 94 |
pSecEnv->setCryptoSlot( n_hStoreHandle ) ; |
| 95 |
pSecEnv->setCertDb( n_hStoreHandle ) ; |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
pSecEnv->enableDefaultCrypt( true ) ; |
| 100 |
} |
| 101 |
|
| 102 |
/* Build XML Security Context */ |
| 103 |
uno::Reference< cssxc::XXMLSecurityContext > xSecCtx = cssxc::XMLSecurityContext::create( mxContext ); |
| 104 |
|
| 105 |
xSecCtx->setDefaultSecurityEnvironmentIndex(xSecCtx->addSecurityEnvironment( xSecEnv )) ; |
| 106 |
return xSecCtx; |
| 107 |
} |
| 108 |
catch( uno::Exception& ) |
| 109 |
{ |
| 110 |
if( n_hStoreHandle != nullptr ) |
| 111 |
{ |
| 112 |
CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; |
| 113 |
} |
| 114 |
|
| 115 |
xmlSecMSCngAppShutdown(); |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void SAL_CALL SEInitializer_MSCryptImpl::freeSecurityContext( const uno::Reference< cssxc::XXMLSecurityContext >&) |
| 121 |
{ |
| 122 |
/* |
| 123 |
uno::Reference< cssxc::XSecurityEnvironment > xSecEnv |
| 124 |
= securityContext->getSecurityEnvironment(); |
| 125 |
|
| 126 |
if( xSecEnv.is() ) |
| 127 |
{ |
| 128 |
uno::Reference< cssl::XUnoTunnel > xEnvTunnel( xSecEnv , uno::UNO_QUERY ) ; |
| 129 |
if (auto pSecEnv = comphelper::getFromUnoTunnel<SecurityEnvironment_MSCryptImpl>(xEnvTunnel)) |
| 130 |
{ |
| 131 |
HCERTSTORE n_hStoreHandle = pSecEnv->getCryptoSlot(); |
| 132 |
|
| 133 |
if( n_hStoreHandle != NULL ) |
| 134 |
{ |
| 135 |
CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; |
| 136 |
pSecEnv->setCryptoSlot( NULL ) ; |
| 137 |
pSecEnv->setCertDb( NULL ) ; |
| 138 |
} |
| 139 |
|
| 140 |
xmlSecMSCryptoAppShutdown() ; |
| 141 |
} |
| 142 |
} |
| 143 |
*/ |
| 144 |
|
| 145 |
xmlSecMSCngAppShutdown(); |
| 146 |
} |
| 147 |
|
| 148 |
/* XServiceInfo */ |
| 149 |
OUString SAL_CALL SEInitializer_MSCryptImpl::getImplementationName() |
| 150 |
{ |
| 151 |
return "com.sun.star.xml.crypto.SEInitializer"; |
| 152 |
} |
| 153 |
|
| 154 |
sal_Bool SAL_CALL SEInitializer_MSCryptImpl::supportsService( const OUString& rServiceName ) |
| 155 |
{ |
| 156 |
return cppu::supportsService( this, rServiceName ); |
| 157 |
} |
| 158 |
|
| 159 |
uno::Sequence< OUString > SAL_CALL SEInitializer_MSCryptImpl::getSupportedServiceNames() |
| 160 |
{ |
| 161 |
return { "com.sun.star.xml.crypto.SEInitializer" }; |
| 162 |
} |
| 163 |
|
| 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*/) |
| 167 |
{ |
| 168 |
return cppu::acquire(new SEInitializer_MSCryptImpl(pCtx)); |
| 169 |
} |
| 170 |
|
| 171 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |