diff options
Diffstat (limited to 'src/core/function/gpg/GpgContext.cpp')
-rw-r--r-- | src/core/function/gpg/GpgContext.cpp | 152 |
1 files changed, 77 insertions, 75 deletions
diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index 7dec318e..75e97e0c 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -32,6 +32,7 @@ #include <gpgme.h> #include <cassert> +#include <cstddef> #include <mutex> #include "core/function/CoreSignalStation.h" @@ -77,8 +78,8 @@ class GpgContext::Impl { [[nodiscard]] auto Good() const -> bool { return good_; } - auto SetPassphraseCb(const gpgme_ctx_t &ctx, gpgme_passphrase_cb_t cb) - -> bool { + auto SetPassphraseCb(const gpgme_ctx_t &ctx, + gpgme_passphrase_cb_t cb) -> bool { if (gpgme_get_pinentry_mode(ctx) != GPGME_PINENTRY_MODE_LOOPBACK) { if (CheckGpgError(gpgme_set_pinentry_mode( ctx, GPGME_PINENTRY_MODE_LOOPBACK)) != GPG_ERR_NO_ERROR) { @@ -92,31 +93,24 @@ class GpgContext::Impl { static auto TestPassphraseCb(void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd) -> gpgme_error_t { - size_t res; -#ifdef QT5_BUILD - QString pass_qstr = "abcdefg\n"; - QByteArray pass = pass_qstr.toUtf8(); -#else - QString pass = "abcdefg\n"; -#endif - - auto passpahrase_size = pass.size(); - size_t off = 0; - - do { -#ifdef QT5_BUILD - const char *p_pass = pass.data(); - res = gpgme_io_write(fd, &p_pass[off], passpahrase_size - off); -#else - res = gpgme_io_write(fd, &pass[off], passpahrase_size - off); -#endif - if (res > 0) off += res; - } while (res > 0 && off != passpahrase_size); + QString passphrase = "abcdefg"; + auto pass_bytes = passphrase.toLatin1(); + auto pass_size = pass_bytes.size(); + const auto *p_pass_bytes = pass_bytes.constData(); + + ssize_t res = 0; + if (pass_size > 0) { + ssize_t off = 0; + ssize_t ret = 0; + do { + ret = gpgme_io_write(fd, &p_pass_bytes[off], pass_size - off); + if (ret > 0) off += ret; + } while (ret > 0 && off != pass_size); + res = off; + } res += gpgme_io_write(fd, "\n", 1); - return res == passpahrase_size + 1 - ? 0 - : gpgme_error_from_errno(GPG_ERR_CANCELED); + return res == pass_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } static auto CustomPassphraseCb(void *hook, const char *uid_hint, @@ -130,47 +124,54 @@ class GpgContext::Impl { passphrase_info != nullptr ? passphrase_info : "", prev_was_bad != 0, ask_for_new)); - GF_CORE_LOG_DEBUG( - "custom passphrase cb called, uid: {}, info: {}, last_was_bad: {}", - uid_hint == nullptr ? "<empty>" : QString{uid_hint}, - passphrase_info == nullptr ? "<empty>" : QString{passphrase_info}, - prev_was_bad); + qCDebug(core) << "custom passphrase cb called, uid: " + << (uid_hint == nullptr ? "<empty>" : QString{uid_hint}) + << ", info: " + << (passphrase_info == nullptr ? "<empty>" + : QString{passphrase_info}) + << ", last_was_bad: " << prev_was_bad; QEventLoop looper; - QObject::connect(CoreSignalStation::GetInstance(), - &CoreSignalStation::SignalUserInputPassphraseCallback, - &looper, &QEventLoop::quit); + QString passphrase = ""; + + Module::TriggerEvent( + "REQUEST_PIN_ENTRY", + {{"uid_hint", uid_hint != nullptr ? uid_hint : ""}, + {"passphrase_info", passphrase_info != nullptr ? passphrase_info : ""}, + {"prev_was_bad", (prev_was_bad != 0) ? "1" : "0"}, + {"ask_for_new", ask_for_new ? "1" : "0"}}, + [&passphrase, &looper](Module::EventIdentifier i, + Module::Event::ListenerIdentifier ei, + Module::Event::Params p) { + passphrase = p["passphrase"]; + looper.quit(); + }); - emit CoreSignalStation::GetInstance()->SignalNeedUserInputPassphrase( - context); looper.exec(); - ResetCacheValue("PinentryContext"); - auto passphrase = context->GetPassphrase().toStdString(); - auto passpahrase_size = passphrase.size(); - GF_CORE_LOG_DEBUG("get passphrase from pinentry size: {}", - passpahrase_size); - - size_t res = 0; - if (passpahrase_size > 0) { - size_t off = 0; + + auto pass_bytes = passphrase.toLatin1(); + auto pass_size = pass_bytes.size(); + const auto *p_pass_bytes = pass_bytes.constData(); + + ssize_t res = 0; + if (pass_size > 0) { + ssize_t off = 0; + ssize_t ret = 0; do { - res = gpgme_io_write(fd, &passphrase[off], passpahrase_size - off); - if (res > 0) off += res; - } while (res > 0 && off != passpahrase_size); + ret = gpgme_io_write(fd, &p_pass_bytes[off], pass_size - off); + if (ret > 0) off += ret; + } while (ret > 0 && off != pass_size); + res = off; } res += gpgme_io_write(fd, "\n", 1); - - GF_CORE_LOG_DEBUG("custom passphrase cd is about to return, res: {}", res); - return res == passpahrase_size + 1 - ? 0 - : gpgme_error_from_errno(GPG_ERR_CANCELED); + return res == pass_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } - static auto TestStatusCb(void *hook, const char *keyword, const char *args) - -> gpgme_error_t { - GF_CORE_LOG_DEBUG("keyword {}", keyword); + static auto TestStatusCb(void *hook, const char *keyword, + const char *args) -> gpgme_error_t { + qCDebug(core, "keyword %s", keyword); return GPG_ERR_NO_ERROR; } @@ -188,10 +189,11 @@ class GpgContext::Impl { const auto gpgme_version = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.version", QString{"0.0.0"}); - GF_CORE_LOG_DEBUG("got gpgme version version from rt: {}", gpgme_version); + qCDebug(core) << "got gpgme version version from rt: " << gpgme_version; if (gpgme_get_keylist_mode(ctx) == 0) { - GF_CORE_LOG_ERROR( + qCWarning( + core, "ctx is not a valid pointer, reported by gpgme_get_keylist_mode"); return false; } @@ -210,8 +212,8 @@ class GpgContext::Impl { const auto database_path = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.database_path", QString{}); - GF_CORE_LOG_DEBUG("ctx set engine info, db path: {}, app path: {}", - database_path, app_path); + qCDebug(core) << "ctx set engine info, db path: " << database_path + << ", app path: " << app_path; auto app_path_buffer = app_path.toUtf8(); auto database_path_buffer = database_path.toUtf8(); @@ -232,23 +234,22 @@ class GpgContext::Impl { assert(ctx != nullptr); if (args.custom_gpgconf && !args.custom_gpgconf_path.isEmpty()) { - GF_CORE_LOG_DEBUG("set custom gpgconf path: {}", - args.custom_gpgconf_path); + qCDebug(core) << "set custom gpgconf path: " << args.custom_gpgconf_path; auto err = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_GPGCONF, args.custom_gpgconf_path.toUtf8(), nullptr); if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - GF_CORE_LOG_ERROR("set gpg context engine info error: {}", - DescribeGpgErrCode(err).second); + qCWarning(core) << "set gpg context engine info error: " + << DescribeGpgErrCode(err).second; return false; } } // set context offline mode - GF_CORE_LOG_DEBUG("gpg context: offline mode: {}", args_.offline_mode); - GF_CORE_LOG_DEBUG("gpg context: auto import missing key: {}", - args_.auto_import_missing_key); + qCDebug(core, "gpg context: offline mode: %d", args_.offline_mode); + qCDebug(core, "gpg context: auto import missing key: %d", + args_.auto_import_missing_key); gpgme_set_offline(ctx, args_.offline_mode ? 1 : 0); // set option auto import missing key @@ -260,19 +261,20 @@ class GpgContext::Impl { } if (!set_ctx_key_list_mode(ctx)) { - GF_CORE_LOG_DEBUG("set ctx key list mode failed"); + qCDebug(core, "set ctx key list mode failed"); return false; } // for unit test if (args_.test_mode) { if (!SetPassphraseCb(ctx, TestPassphraseCb)) { - GF_CORE_LOG_ERROR("set passphrase cb failed, test"); + qCWarning(core, "set passphrase cb failed, test"); return false; }; - } else if (!args_.use_pinentry) { + } else if (!args_.use_pinentry && + Module::IsModuleActivate(kPinentryModuleID)) { if (!SetPassphraseCb(ctx, CustomPassphraseCb)) { - GF_CORE_LOG_DEBUG("set passphrase cb failed, custom"); + qCDebug(core, "set passphrase cb failed, custom"); return false; } } @@ -283,7 +285,7 @@ class GpgContext::Impl { } if (!set_ctx_openpgp_engine_info(ctx)) { - GF_CORE_LOG_ERROR("set gpgme context openpgp engine info failed"); + qCWarning(core, "set gpgme context openpgp engine info failed"); return false; } @@ -293,15 +295,15 @@ class GpgContext::Impl { auto binary_ctx_initialize(const GpgContextInitArgs &args) -> bool { gpgme_ctx_t p_ctx; if (auto err = CheckGpgError(gpgme_new(&p_ctx)); err != GPG_ERR_NO_ERROR) { - GF_CORE_LOG_ERROR("get new gpg context error: {}", - DescribeGpgErrCode(err).second); + qCWarning(core) << "get new gpg context error: " + << DescribeGpgErrCode(err).second; return false; } assert(p_ctx != nullptr); binary_ctx_ref_ = p_ctx; if (!common_ctx_initialize(binary_ctx_ref_, args)) { - GF_CORE_LOG_ERROR("get new ctx failed, binary"); + qCWarning(core, "get new ctx failed, binary"); return false; } @@ -312,7 +314,7 @@ class GpgContext::Impl { auto default_ctx_initialize(const GpgContextInitArgs &args) -> bool { gpgme_ctx_t p_ctx; if (CheckGpgError(gpgme_new(&p_ctx)) != GPG_ERR_NO_ERROR) { - GF_CORE_LOG_ERROR("get new ctx failed, default"); + qCWarning(core, "get new ctx failed, default"); return false; } assert(p_ctx != nullptr); |