From 0855a1296a1908016f011eb5e6552854ac53e63a Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Mon, 22 Feb 2016 18:28:08 +0100 Subject: Initial checkin of gpgmepp sources Based on git.kde.org/pim/gpgmepp rev. 0e3ebc02 * lang/cpp/src/assuanresult.cpp, lang/cpp/src/assuanresult.h, lang/cpp/src/callbacks.cpp, lang/cpp/src/callbacks.h, lang/cpp/src/configuration.cpp, lang/cpp/src/configuration.h, lang/cpp/src/context.cpp, lang/cpp/src/context.h, lang/cpp/src/context_glib.cpp, lang/cpp/src/context_p.h, lang/cpp/src/context_qt.cpp, lang/cpp/src/context_vanilla.cpp, lang/cpp/src/data.cpp, lang/cpp/src/data.h, lang/cpp/src/data_p.h, lang/cpp/src/decryptionresult.cpp, lang/cpp/src/decryptionresult.h, lang/cpp/src/defaultassuantransaction.cpp, lang/cpp/src/defaultassuantransaction.h, lang/cpp/src/editinteractor.cpp, lang/cpp/src/editinteractor.h, lang/cpp/src/encryptionresult.cpp, lang/cpp/src/encryptionresult.h, lang/cpp/src/engineinfo.cpp, lang/cpp/src/engineinfo.h, lang/cpp/src/error.h, lang/cpp/src/eventloopinteractor.cpp, lang/cpp/src/eventloopinteractor.h, lang/cpp/src/exception.cpp, lang/cpp/src/exception.h, lang/cpp/src/global.h, lang/cpp/src/gpgadduserideditinteractor.cpp, lang/cpp/src/gpgadduserideditinteractor.h, lang/cpp/src/gpgagentgetinfoassuantransaction.cpp, lang/cpp/src/gpgagentgetinfoassuantransaction.h, lang/cpp/src/gpgmefw.h, lang/cpp/src/gpgmepp_export.h, lang/cpp/src/gpgsetexpirytimeeditinteractor.cpp, lang/cpp/src/gpgsetexpirytimeeditinteractor.h, lang/cpp/src/gpgsetownertrusteditinteractor.cpp, lang/cpp/src/gpgsetownertrusteditinteractor.h, lang/cpp/src/gpgsignkeyeditinteractor.cpp, lang/cpp/src/gpgsignkeyeditinteractor.h, lang/cpp/src/importresult.cpp, lang/cpp/src/importresult.h, lang/cpp/src/key.cpp, lang/cpp/src/key.h, lang/cpp/src/keygenerationresult.cpp, lang/cpp/src/keygenerationresult.h, lang/cpp/src/keylistresult.cpp, lang/cpp/src/keylistresult.h, lang/cpp/src/notation.h, lang/cpp/src/result.h, lang/cpp/src/result_p.h, lang/cpp/src/scdgetinfoassuantransaction.cpp, lang/cpp/src/scdgetinfoassuantransaction.h, lang/cpp/src/signingresult.cpp, lang/cpp/src/signingresult.h, lang/cpp/src/trustitem.cpp, lang/cpp/src/trustitem.h, lang/cpp/src/util.h, lang/cpp/src/verificationresult.cpp, lang/cpp/src/verificationresult.h, lang/cpp/src/vfsmountresult.cpp, lang/cpp/src/vfsmountresult.h, lang/cpp/src/interfaces/assuantransaction.h, lang/cpp/src/interfaces/dataprovider.h, lang/cpp/src/interfaces/passphraseprovider.h, lang/cpp/src/interfaces/progressprovider.h: New. --- lang/cpp/src/decryptionresult.cpp | 276 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 lang/cpp/src/decryptionresult.cpp (limited to 'lang/cpp/src/decryptionresult.cpp') diff --git a/lang/cpp/src/decryptionresult.cpp b/lang/cpp/src/decryptionresult.cpp new file mode 100644 index 00000000..88e85670 --- /dev/null +++ b/lang/cpp/src/decryptionresult.cpp @@ -0,0 +1,276 @@ +/* + decryptionresult.cpp - wraps a gpgme keygen result + Copyright (C) 2004 Klarälvdalens Datakonsult AB + + This file is part of GPGME++. + + GPGME++ is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + GPGME++ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with GPGME++; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include + +#include +#include "result_p.h" +#include "util.h" + +#include + +#include +#include +#include +#include +#include + +#include + +class GpgME::DecryptionResult::Private +{ +public: + explicit Private(const _gpgme_op_decrypt_result &r) : res(r) + { + if (res.unsupported_algorithm) { + res.unsupported_algorithm = strdup(res.unsupported_algorithm); + } +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_FILE_NAME + if (res.file_name) { + res.file_name = strdup(res.file_name); + } +#endif +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + //FIXME: copying gpgme_recipient_t objects invalidates the keyid member, + //thus we use _keyid for now (internal API) + for (gpgme_recipient_t r = res.recipients ; r ; r = r->next) { + recipients.push_back(*r); + } + res.recipients = 0; +#endif + } + ~Private() + { + if (res.unsupported_algorithm) { + std::free(res.unsupported_algorithm); + } + res.unsupported_algorithm = 0; +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_FILE_NAME + if (res.file_name) { + std::free(res.file_name); + } + res.file_name = 0; +#endif + } + + _gpgme_op_decrypt_result res; +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + std::vector<_gpgme_recipient> recipients; +#endif +}; + +GpgME::DecryptionResult::DecryptionResult(gpgme_ctx_t ctx, int error) + : GpgME::Result(error), d() +{ + init(ctx); +} + +GpgME::DecryptionResult::DecryptionResult(gpgme_ctx_t ctx, const Error &error) + : GpgME::Result(error), d() +{ + init(ctx); +} + +void GpgME::DecryptionResult::init(gpgme_ctx_t ctx) +{ + if (!ctx) { + return; + } + gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx); + if (!res) { + return; + } + d.reset(new Private(*res)); +} + +make_standard_stuff(DecryptionResult) + +const char *GpgME::DecryptionResult::unsupportedAlgorithm() const +{ + return d ? d->res.unsupported_algorithm : 0 ; +} + +bool GpgME::DecryptionResult::isWrongKeyUsage() const +{ + return d && d->res.wrong_key_usage; +} + +const char *GpgME::DecryptionResult::fileName() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_FILE_NAME + return d ? d->res.file_name : 0 ; +#else + return 0; +#endif +} + +unsigned int GpgME::DecryptionResult::numRecipients() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + return d ? d->recipients.size() : 0 ; +#else + return 0; +#endif +} + +GpgME::DecryptionResult::Recipient GpgME::DecryptionResult::recipient(unsigned int idx) const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + if (d && idx < d->recipients.size()) { + return Recipient(&d->recipients[idx]); + } +#endif + return Recipient(); +} + +namespace +{ +struct make_recipient { + GpgME::DecryptionResult::Recipient operator()(_gpgme_recipient &t) + { + return GpgME::DecryptionResult::Recipient(&t); + } +}; +} + +std::vector GpgME::DecryptionResult::recipients() const +{ + std::vector result; +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + if (d) { + result.reserve(d->recipients.size()); + std::transform(d->recipients.begin(), d->recipients.end(), + std::back_inserter(result), + make_recipient()); + } +#endif + return result; +} + +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS +class GpgME::DecryptionResult::Recipient::Private : public _gpgme_recipient +{ +public: + Private(gpgme_recipient_t reci) : _gpgme_recipient(*reci) {} +}; +#endif + +GpgME::DecryptionResult::Recipient::Recipient() + : d() +{ + +} + +GpgME::DecryptionResult::Recipient::Recipient(gpgme_recipient_t r) + : d() +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + if (r) { + d.reset(new Private(r)); + } +#endif +} + +bool GpgME::DecryptionResult::Recipient::isNull() const +{ + return !d; +} + +const char *GpgME::DecryptionResult::Recipient::keyID() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + //_keyid is internal API, but the public keyid is invalid after copying (see above) + if (d) { + return d->_keyid; + } +#endif + return 0; +} + +const char *GpgME::DecryptionResult::Recipient::shortKeyID() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + //_keyid is internal API, but the public keyid is invalid after copying (see above) + if (d) { + return d->_keyid + 8; + } +#endif + return 0; +} + +unsigned int GpgME::DecryptionResult::Recipient::publicKeyAlgorithm() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + if (d) { + return d->pubkey_algo; + } +#endif + return 0; +} + +const char *GpgME::DecryptionResult::Recipient::publicKeyAlgorithmAsString() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + if (d) { + return gpgme_pubkey_algo_name(d->pubkey_algo); + } +#endif + return 0; +} + +GpgME::Error GpgME::DecryptionResult::Recipient::status() const +{ +#ifdef HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS + if (d) { + return Error(d->status); + } +#endif + return Error(); +} + +std::ostream &GpgME::operator<<(std::ostream &os, const DecryptionResult &result) +{ + os << "GpgME::DecryptionResult("; + if (!result.isNull()) { + os << "\n error: " << result.error() + << "\n fileName: " << protect(result.fileName()) + << "\n unsupportedAlgorithm: " << protect(result.unsupportedAlgorithm()) + << "\n isWrongKeyUsage: " << result.isWrongKeyUsage() + << "\n recipients:\n"; + const std::vector recipients = result.recipients(); + std::copy(recipients.begin(), recipients.end(), + std::ostream_iterator(os, "\n")); + } + return os << ')'; +} + +std::ostream &GpgME::operator<<(std::ostream &os, const DecryptionResult::Recipient &reci) +{ + os << "GpgME::DecryptionResult::Recipient("; + if (!reci.isNull()) { + os << "\n keyID: " << protect(reci.keyID()) + << "\n shortKeyID: " << protect(reci.shortKeyID()) + << "\n publicKeyAlgorithm: " << protect(reci.publicKeyAlgorithmAsString()) + << "\n status: " << reci.status(); + } + return os << ')'; +} -- cgit From 433bb8e84b2d1e50b5c5b9f7f2006b60cd7d7785 Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Mon, 22 Feb 2016 19:07:41 +0100 Subject: Remove feature check ifdefs * lang/cpp/src/assuanresult.cpp, lang/cpp/src/callbacks.cpp, lang/cpp/src/configuration.cpp, lang/cpp/src/context.cpp, lang/cpp/src/context_glib.cpp, lang/cpp/src/context_qt.cpp, lang/cpp/src/context_vanilla.cpp, lang/cpp/src/data.cpp, lang/cpp/src/decryptionresult.cpp, lang/cpp/src/defaultassuantransaction.cpp, lang/cpp/src/editinteractor.cpp, lang/cpp/src/encryptionresult.cpp, lang/cpp/src/engineinfo.cpp, lang/cpp/src/eventloopinteractor.cpp, lang/cpp/src/global.h, lang/cpp/src/gpgagentgetinfoassuantransaction.cpp, lang/cpp/src/importresult.cpp, lang/cpp/src/interfaces/assuantransaction.h, lang/cpp/src/key.cpp, lang/cpp/src/keygenerationresult.cpp, lang/cpp/src/keylistresult.cpp, lang/cpp/src/scdgetinfoassuantransaction.cpp, lang/cpp/src/signingresult.cpp, lang/cpp/src/trustitem.cpp, lang/cpp/src/util.h, lang/cpp/src/verificationresult.cpp, lang/cpp/src/vfsmountresult.cpp: Remove feature checks. --- lang/cpp/src/assuanresult.cpp | 8 - lang/cpp/src/callbacks.cpp | 38 ---- lang/cpp/src/configuration.cpp | 214 +--------------------- lang/cpp/src/context.cpp | 214 ---------------------- lang/cpp/src/context_glib.cpp | 9 - lang/cpp/src/context_qt.cpp | 9 - lang/cpp/src/context_vanilla.cpp | 2 - lang/cpp/src/data.cpp | 11 -- lang/cpp/src/decryptionresult.cpp | 36 ---- lang/cpp/src/defaultassuantransaction.cpp | 2 - lang/cpp/src/editinteractor.cpp | 29 +-- lang/cpp/src/encryptionresult.cpp | 2 - lang/cpp/src/engineinfo.cpp | 6 - lang/cpp/src/eventloopinteractor.cpp | 2 - lang/cpp/src/global.h | 1 - lang/cpp/src/gpgagentgetinfoassuantransaction.cpp | 2 - lang/cpp/src/importresult.cpp | 1 - lang/cpp/src/interfaces/assuantransaction.h | 2 + lang/cpp/src/key.cpp | 30 --- lang/cpp/src/keygenerationresult.cpp | 2 - lang/cpp/src/keylistresult.cpp | 2 - lang/cpp/src/scdgetinfoassuantransaction.cpp | 2 - lang/cpp/src/signingresult.cpp | 2 - lang/cpp/src/trustitem.cpp | 2 - lang/cpp/src/util.h | 35 +--- lang/cpp/src/verificationresult.cpp | 33 ---- lang/cpp/src/vfsmountresult.cpp | 8 - 27 files changed, 12 insertions(+), 692 deletions(-) (limited to 'lang/cpp/src/decryptionresult.cpp') diff --git a/lang/cpp/src/assuanresult.cpp b/lang/cpp/src/assuanresult.cpp index 056aefb3..3d6d0a3a 100644 --- a/lang/cpp/src/assuanresult.cpp +++ b/lang/cpp/src/assuanresult.cpp @@ -20,8 +20,6 @@ Boston, MA 02110-1301, USA. */ -#include - #include #include "result_p.h" @@ -31,7 +29,6 @@ using namespace GpgME; -#ifdef HAVE_GPGME_ASSUAN_ENGINE class AssuanResult::Private { public: @@ -45,7 +42,6 @@ public: gpgme_error_t error; }; -#endif AssuanResult::AssuanResult(gpgme_ctx_t ctx, int error) : Result(error), d() @@ -62,7 +58,6 @@ AssuanResult::AssuanResult(gpgme_ctx_t ctx, const Error &error) void AssuanResult::init(gpgme_ctx_t ctx) { (void)ctx; -#ifdef HAVE_GPGME_ASSUAN_ENGINE if (!ctx) { return; } @@ -71,18 +66,15 @@ void AssuanResult::init(gpgme_ctx_t ctx) return; } d.reset(new Private(res)); -#endif } make_standard_stuff(AssuanResult) Error AssuanResult::assuanError() const { -#ifdef HAVE_GPGME_ASSUAN_ENGINE if (d) { return Error(d->error); } -#endif return Error(); } diff --git a/lang/cpp/src/callbacks.cpp b/lang/cpp/src/callbacks.cpp index 091975d7..4b4dd806 100644 --- a/lang/cpp/src/callbacks.cpp +++ b/lang/cpp/src/callbacks.cpp @@ -20,8 +20,6 @@ Boston, MA 02110-1301, USA. */ -#include - #include "callbacks.h" #include "util.h" @@ -39,21 +37,9 @@ #include #include -#ifndef HAVE_GPGME_SSIZE_T -# define gpgme_ssize_t ssize_t -#endif - -#ifndef HAVE_GPGME_OFF_T -# define gpgme_off_t off_t -#endif - static inline gpgme_error_t make_err_from_syserror() { -#ifdef HAVE_GPGME_GPG_ERROR_WRAPPERS return gpgme_error_from_syserror(); -#else - return gpg_error_from_syserror(); -#endif } using GpgME::ProgressProvider; @@ -92,11 +78,7 @@ gpgme_error_t passphrase_callback(void *opaque, const char *uid_hint, const char size_t passphrase_length = std::strlen(passphrase); size_t written = 0; do { -#ifdef HAVE_GPGME_IO_READWRITE ssize_t now_written = gpgme_io_write(fd, passphrase + written, passphrase_length - written); -#else - ssize_t now_written = write(fd, passphrase + written, passphrase_length - written); -#endif if (now_written < 0) { err = make_err_from_syserror(); break; @@ -110,11 +92,7 @@ gpgme_error_t passphrase_callback(void *opaque, const char *uid_hint, const char wipememory(passphrase, std::strlen(passphrase)); } free(passphrase); -#ifdef HAVE_GPGME_IO_READWRITE gpgme_io_write(fd, "\n", 1); -#else - write(fd, "\n", 1); -#endif return err; } @@ -123,11 +101,7 @@ data_read_callback(void *opaque, void *buf, size_t buflen) { DataProvider *provider = static_cast(opaque); if (!provider) { -#ifdef HAVE_GPGME_GPG_ERROR_WRAPPERS gpgme_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#else - gpg_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#endif return -1; } return (gpgme_ssize_t)provider->read(buf, buflen); @@ -138,11 +112,7 @@ data_write_callback(void *opaque, const void *buf, size_t buflen) { DataProvider *provider = static_cast(opaque); if (!provider) { -#ifdef HAVE_GPGME_GPG_ERROR_WRAPPERS gpgme_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#else - gpg_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#endif return -1; } return (gpgme_ssize_t)provider->write(buf, buflen); @@ -153,19 +123,11 @@ data_seek_callback(void *opaque, gpgme_off_t offset, int whence) { DataProvider *provider = static_cast(opaque); if (!provider) { -#ifdef HAVE_GPGME_GPG_ERROR_WRAPPERS gpgme_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#else - gpg_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#endif return -1; } if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) { -#ifdef HAVE_GPGME_GPG_ERROR_WRAPPERS gpgme_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#else - gpg_err_set_errno(gpgme_err_code_to_errno(GPG_ERR_EINVAL)); -#endif return -1; } return provider->seek((off_t)offset, whence); diff --git a/lang/cpp/src/configuration.cpp b/lang/cpp/src/configuration.cpp index fc19020e..8a23efcf 100644 --- a/lang/cpp/src/configuration.cpp +++ b/lang/cpp/src/configuration.cpp @@ -20,8 +20,6 @@ Boston, MA 02110-1301, USA. */ -#include - #include "configuration.h" #include "error.h" #include "util.h" @@ -58,7 +56,6 @@ struct nodelete { std::vector Component::load(Error &returnedError) { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF // // 1. get a context: // @@ -101,16 +98,11 @@ std::vector Component::load(Error &returnedError) } return result; -#else - returnedError = Error(make_error(GPG_ERR_NOT_SUPPORTED)); - return std::vector(); -#endif } Error Component::save() const { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF if (isNull()) { return Error(make_error(GPG_ERR_INV_ARG)); } @@ -128,41 +120,25 @@ Error Component::save() const // 2. save the config: // return Error(gpgme_op_conf_save(ctx.get(), comp.get())); -#else - return Error(make_error(GPG_ERR_NOT_SUPPORTED)); -#endif } const char *Component::name() const { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF return comp ? comp->name : 0 ; -#else - return 0; -#endif } const char *Component::description() const { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF return comp ? comp->description : 0 ; -#else - return 0; -#endif } const char *Component::programName() const { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF return comp ? comp->program_name : 0 ; -#else - return 0; -#endif } Option Component::option(unsigned int idx) const { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF gpgme_conf_opt_t opt = 0; if (comp) { opt = comp->options; @@ -173,17 +149,12 @@ Option Component::option(unsigned int idx) const } if (opt) { return Option(comp, opt); - } else { -#endif - return Option(); -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF } -#endif + return Option(); } Option Component::option(const char *name) const { -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF gpgme_conf_opt_t opt = 0; if (comp) { opt = comp->options; @@ -194,37 +165,28 @@ Option Component::option(const char *name) const } if (opt) { return Option(comp, opt); - } else { -#endif - return Option(); -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF } -#endif + return Option(); } unsigned int Component::numOptions() const { unsigned int result = 0; -#ifdef HAVE_GPGME_PROTOCOL_GPGCONF for (gpgme_conf_opt_t opt = comp ? comp->options : 0 ; opt ; opt = opt->next) { ++result; } -#endif return result; } std::vector