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/.
10 #include "SecurityEnvironment.hxx"
11 #include "CertificateImpl.hxx"
13 #include <com/sun/star/security/CertificateCharacters.hpp>
14 #include <com/sun/star/security/CertificateValidity.hpp>
16 #include <comphelper/servicehelper.hxx>
18 #include <rtl/ref.hxx>
21 #include <config_folders.h>
22 #include <osl/file.hxx>
23 #include <osl/process.h>
24 #include <rtl/bootstrap.hxx>
25 #include <tools/urlobj.hxx>
29 #include <keylistresult.h>
30 #include <xmlsec-wrapper.h>
32 #if defined _MSC_VER && defined __clang__
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wundef"
37 #if defined _MSC_VER && defined __clang__
38 #pragma clang diagnostic pop
43 using namespace css::security
;
44 using namespace css::uno
;
45 using namespace css::lang
;
47 SecurityEnvironmentGpg::SecurityEnvironmentGpg()
50 // On Windows, gpgme expects gpgme-w32spawn.exe to be in the same directory as the current
51 // process executable. This assumption might be wrong, e.g., for bundled python, which is
52 // in instdir/program/python-core-x.y.z/bin, while gpgme-w32spawn.exe is in instdir/program.
53 // If we can't find gpgme-w32spawn.exe in the current executable location, then try to find
54 // the spawn executable, and inform gpgme about actual location using gpgme_set_global_flag.
55 [[maybe_unused
]] static bool bSpawnPathInitialized
= [] {
56 auto accessUrl
= [](const INetURLObject
& url
) {
57 osl::File
file(url
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
58 return file
.open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
;
61 osl_getExecutableFile(&sPath
.pData
);
62 INetURLObject
aPathUrl(sPath
);
63 aPathUrl
.setName(u
"gpgme-w32spawn.exe");
64 if (!accessUrl(aPathUrl
))
66 sPath
= "$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER
"/gpgme-w32spawn.exe";
67 rtl::Bootstrap::expandMacros(sPath
);
68 aPathUrl
.SetURL(sPath
);
69 if (accessUrl(aPathUrl
))
71 aPathUrl
.removeSegment();
72 GpgME::setGlobalFlag("w32-inst-dir",
73 aPathUrl
.getFSysPath(FSysStyle::Dos
).toUtf8().getStr());
79 GpgME::Error err
= GpgME::checkEngine(GpgME::OpenPGP
);
81 throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
83 m_ctx
.reset( GpgME::Context::createForProtocol(GpgME::OpenPGP
) );
85 throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
86 m_ctx
->setArmor(false);
89 SecurityEnvironmentGpg::~SecurityEnvironmentGpg()
93 OUString
SecurityEnvironmentGpg::getSecurityEnvironmentInformation()
98 Sequence
< Reference
< XCertificate
> > SecurityEnvironmentGpg::getCertificatesImpl( bool bPrivateOnly
)
100 std::vector
< GpgME::Key
> keyList
;
101 std::vector
< rtl::Reference
<CertificateImpl
> > certsList
;
103 m_ctx
->setKeyListMode(GPGME_KEYLIST_MODE_LOCAL
);
104 GpgME::Error err
= m_ctx
->startKeyListing("", bPrivateOnly
);
106 GpgME::Key k
= m_ctx
->nextKey(err
);
109 if (!k
.isRevoked() && !k
.isExpired() && !k
.isDisabled() && !k
.isInvalid()) {
110 // We can't create CertificateImpl here as CertificateImpl::setCertificate uses GpgME API
111 // which interrupts our key listing here. So first get the keys from GpgME, then create the CertificateImpls
112 keyList
.push_back(k
);
115 m_ctx
->endKeyListing();
117 for (auto const& key
: keyList
) {
118 rtl::Reference
<CertificateImpl
> xCert
= new CertificateImpl();
119 xCert
->setCertificate(m_ctx
.get(),key
);
120 certsList
.push_back(xCert
);
123 Sequence
< Reference
< XCertificate
> > xCertificateSequence(certsList
.size());
124 auto xCertificateSequenceRange
= asNonConstRange(xCertificateSequence
);
126 for (const auto& cert
: certsList
) {
127 xCertificateSequenceRange
[i
++] = cert
;
130 return xCertificateSequence
;
133 Sequence
< Reference
< XCertificate
> > SecurityEnvironmentGpg::getPersonalCertificates()
135 return getCertificatesImpl( true );
138 Sequence
< Reference
< XCertificate
> > SecurityEnvironmentGpg::getAllCertificates()
140 return getCertificatesImpl( false );
143 Reference
< XCertificate
> SecurityEnvironmentGpg::getCertificate( const OUString
& keyId
, const Sequence
< sal_Int8
>& /*serialNumber*/ )
145 //xmlChar* pSignatureValue=xmlNodeGetContent(cur);
146 OString ostr
= OUStringToOString( keyId
, RTL_TEXTENCODING_UTF8
);
147 const xmlChar
* strKeyId
= reinterpret_cast<const xmlChar
*>(ostr
.getStr());
149 int nRet
= xmlSecBase64Decode_ex(strKeyId
, const_cast<xmlSecByte
*>(strKeyId
), xmlStrlen(strKeyId
), &nWritten
);
151 throw RuntimeException("Base64 decode failed");
153 m_ctx
->setKeyListMode(GPGME_KEYLIST_MODE_LOCAL
);
154 GpgME::Error err
= m_ctx
->startKeyListing("", false);
156 GpgME::Key k
= m_ctx
->nextKey(err
);
159 if (!k
.isInvalid() && strcmp(k
.primaryFingerprint(), reinterpret_cast<const char*>(strKeyId
)) == 0) {
160 rtl::Reference
<CertificateImpl
> xCert
= new CertificateImpl();
161 xCert
->setCertificate(m_ctx
.get(), k
);
162 m_ctx
->endKeyListing();
166 m_ctx
->endKeyListing();
171 Sequence
< Reference
< XCertificate
> > SecurityEnvironmentGpg::buildCertificatePath( const Reference
< XCertificate
>& /*begin*/ )
173 return Sequence
< Reference
< XCertificate
> >();
176 Reference
< XCertificate
> SecurityEnvironmentGpg::createCertificateFromRaw( const Sequence
< sal_Int8
>& /*rawCertificate*/ )
181 Reference
< XCertificate
> SecurityEnvironmentGpg::createCertificateFromAscii( const OUString
& /*asciiCertificate*/ )
186 sal_Int32
SecurityEnvironmentGpg::verifyCertificate( const Reference
< XCertificate
>& aCert
,
187 const Sequence
< Reference
< XCertificate
> >& /*intermediateCerts*/ )
189 const CertificateImpl
* xCert
= dynamic_cast<CertificateImpl
*>(aCert
.get());
190 if (xCert
== nullptr) {
191 // Can't find the key locally -> unknown owner
192 return security::CertificateValidity::ISSUER_UNKNOWN
;
195 const GpgME::Key
* key
= xCert
->getCertificate();
196 if (key
->ownerTrust() == GpgME::Key::OwnerTrust::Marginal
||
197 key
->ownerTrust() == GpgME::Key::OwnerTrust::Full
||
198 key
->ownerTrust() == GpgME::Key::OwnerTrust::Ultimate
)
200 return security::CertificateValidity::VALID
;
203 return security::CertificateValidity::ISSUER_UNTRUSTED
;
206 sal_Int32
SecurityEnvironmentGpg::getCertificateCharacters(
207 const Reference
< XCertificate
>& aCert
)
209 if (dynamic_cast<CertificateImpl
*>(aCert
.get()) == nullptr)
210 throw RuntimeException();
212 // we only listed private keys anyway, up in
213 // SecurityEnvironmentGpg::getPersonalCertificates
214 return CertificateCharacters::HAS_PRIVATE_KEY
;
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */