|
Packit |
d7e8d0 |
/*
|
|
Packit |
d7e8d0 |
qgpgmebackend.cpp
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
This file is part of qgpgme, the Qt API binding for gpgme
|
|
Packit |
d7e8d0 |
Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB
|
|
Packit |
d7e8d0 |
Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
|
|
Packit |
d7e8d0 |
Software engineering by Intevation GmbH
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME is free software; you can redistribute it and/or
|
|
Packit |
d7e8d0 |
modify it under the terms of the GNU General Public License as
|
|
Packit |
d7e8d0 |
published by the Free Software Foundation; either version 2 of the
|
|
Packit |
d7e8d0 |
License, or (at your option) any later version.
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME is distributed in the hope that it will be useful,
|
|
Packit |
d7e8d0 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
d7e8d0 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
d7e8d0 |
General Public License for more details.
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
You should have received a copy of the GNU General Public License
|
|
Packit |
d7e8d0 |
along with this program; if not, write to the Free Software
|
|
Packit |
d7e8d0 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
In addition, as a special exception, the copyright holders give
|
|
Packit |
d7e8d0 |
permission to link the code of this program with any edition of
|
|
Packit |
d7e8d0 |
the Qt library by Trolltech AS, Norway (or with modified versions
|
|
Packit |
d7e8d0 |
of Qt that use the same license as Qt), and distribute linked
|
|
Packit |
d7e8d0 |
combinations including the two. You must obey the GNU General
|
|
Packit |
d7e8d0 |
Public License in all respects for all of the code used other than
|
|
Packit |
d7e8d0 |
Qt. If you modify this file, you may extend this exception to
|
|
Packit |
d7e8d0 |
your version of the file, but you are not obligated to do so. If
|
|
Packit |
d7e8d0 |
you do not wish to do so, delete this exception statement from
|
|
Packit |
d7e8d0 |
your version.
|
|
Packit |
d7e8d0 |
*/
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
d7e8d0 |
#include "config.h"
|
|
Packit |
d7e8d0 |
#endif
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include "qgpgmebackend.h"
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include "error.h"
|
|
Packit |
d7e8d0 |
#include "engineinfo.h"
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include "protocol_p.h"
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include <QFile>
|
|
Packit |
d7e8d0 |
#include <QString>
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
const char QGpgME::QGpgMEBackend::OpenPGP[] = "OpenPGP";
|
|
Packit |
d7e8d0 |
const char QGpgME::QGpgMEBackend::SMIME[] = "SMIME";
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::QGpgMEBackend::QGpgMEBackend()
|
|
Packit Service |
30b792 |
: mCryptoConfig(nullptr),
|
|
Packit Service |
30b792 |
mOpenPGPProtocol(nullptr),
|
|
Packit Service |
30b792 |
mSMIMEProtocol(nullptr)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
GpgME::initializeLibrary();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::QGpgMEBackend::~QGpgMEBackend()
|
|
Packit |
d7e8d0 |
{
|
|
Packit Service |
30b792 |
delete mCryptoConfig; mCryptoConfig = nullptr;
|
|
Packit Service |
30b792 |
delete mOpenPGPProtocol; mOpenPGPProtocol = nullptr;
|
|
Packit Service |
30b792 |
delete mSMIMEProtocol; mSMIMEProtocol = nullptr;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QString QGpgME::QGpgMEBackend::name() const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
return QStringLiteral("gpgme");
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QString QGpgME::QGpgMEBackend::displayName() const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
return QStringLiteral("GpgME");
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::CryptoConfig *QGpgME::QGpgMEBackend::config() const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!mCryptoConfig) {
|
|
Packit |
d7e8d0 |
if (GpgME::hasFeature(GpgME::GpgConfEngineFeature, 0)) {
|
|
Packit |
d7e8d0 |
mCryptoConfig = new QGpgMENewCryptoConfig;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return mCryptoConfig;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
static bool check(GpgME::Protocol proto, QString *reason)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!GpgME::checkEngine(proto)) {
|
|
Packit |
d7e8d0 |
return true;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
if (!reason) {
|
|
Packit |
d7e8d0 |
return false;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
// error, check why:
|
|
Packit |
d7e8d0 |
#if 0
|
|
Packit |
d7e8d0 |
Port away from localised string or delete.
|
|
Packit |
d7e8d0 |
const GpgME::EngineInfo ei = GpgME::engineInfo(proto);
|
|
Packit |
d7e8d0 |
if (ei.isNull()) {
|
|
Packit |
d7e8d0 |
*reason = i18n("GPGME was compiled without support for %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
|
|
Packit |
d7e8d0 |
} else if (ei.fileName() && !ei.version()) {
|
|
Packit |
d7e8d0 |
*reason = i18n("Engine %1 is not installed properly.", QFile::decodeName(ei.fileName()));
|
|
Packit |
d7e8d0 |
} else if (ei.fileName() && ei.version() && ei.requiredVersion())
|
|
Packit |
d7e8d0 |
*reason = i18n("Engine %1 version %2 installed, "
|
|
Packit |
d7e8d0 |
"but at least version %3 is required.",
|
|
Packit |
d7e8d0 |
QFile::decodeName(ei.fileName()), QLatin1String(ei.version()), QLatin1String(ei.requiredVersion()));
|
|
Packit |
d7e8d0 |
else {
|
|
Packit |
d7e8d0 |
*reason = i18n("Unknown problem with engine for protocol %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
#endif
|
|
Packit |
d7e8d0 |
return false;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
bool QGpgME::QGpgMEBackend::checkForOpenPGP(QString *reason) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
return check(GpgME::OpenPGP, reason);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
bool QGpgME::QGpgMEBackend::checkForSMIME(QString *reason) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
return check(GpgME::CMS, reason);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
bool QGpgME::QGpgMEBackend::checkForProtocol(const char *name, QString *reason) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (qstricmp(name, OpenPGP) == 0) {
|
|
Packit |
d7e8d0 |
return check(GpgME::OpenPGP, reason);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
if (qstricmp(name, SMIME) == 0) {
|
|
Packit |
d7e8d0 |
return check(GpgME::CMS, reason);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
if (reason) {
|
|
Packit |
d7e8d0 |
*reason = QStringLiteral("Unsupported protocol \"%1\"").arg(QLatin1String(name));
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return false;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::Protocol *QGpgME::QGpgMEBackend::openpgp() const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!mOpenPGPProtocol)
|
|
Packit |
d7e8d0 |
if (checkForOpenPGP()) {
|
|
Packit |
d7e8d0 |
mOpenPGPProtocol = new ::Protocol(GpgME::OpenPGP);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return mOpenPGPProtocol;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::Protocol *QGpgME::QGpgMEBackend::smime() const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!mSMIMEProtocol)
|
|
Packit |
d7e8d0 |
if (checkForSMIME()) {
|
|
Packit |
d7e8d0 |
mSMIMEProtocol = new ::Protocol(GpgME::CMS);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return mSMIMEProtocol;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::Protocol *QGpgME::QGpgMEBackend::protocol(const char *name) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (qstricmp(name, OpenPGP) == 0) {
|
|
Packit |
d7e8d0 |
return openpgp();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
if (qstricmp(name, SMIME) == 0) {
|
|
Packit |
d7e8d0 |
return smime();
|
|
Packit |
d7e8d0 |
}
|
|
Packit Service |
30b792 |
return nullptr;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
bool QGpgME::QGpgMEBackend::supportsProtocol(const char *name) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
return qstricmp(name, OpenPGP) == 0 || qstricmp(name, SMIME) == 0;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
const char *QGpgME::QGpgMEBackend::enumerateProtocols(int i) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
switch (i) {
|
|
Packit |
d7e8d0 |
case 0: return OpenPGP;
|
|
Packit |
d7e8d0 |
case 1: return SMIME;
|
|
Packit Service |
30b792 |
default: return nullptr;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
static QGpgME::QGpgMEBackend *gpgmeBackend;
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::CryptoConfig *QGpgME::cryptoConfig()
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!gpgmeBackend) {
|
|
Packit |
d7e8d0 |
gpgmeBackend = new QGpgME::QGpgMEBackend();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return gpgmeBackend->config();
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::Protocol *QGpgME::openpgp()
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!gpgmeBackend) {
|
|
Packit |
d7e8d0 |
gpgmeBackend = new QGpgME::QGpgMEBackend();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return gpgmeBackend->openpgp();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME::Protocol *QGpgME::smime()
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!gpgmeBackend) {
|
|
Packit |
d7e8d0 |
gpgmeBackend = new QGpgME::QGpgMEBackend();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
return gpgmeBackend->smime();
|
|
Packit |
d7e8d0 |
}
|