aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/gpg
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-12-02 14:01:16 +0000
committersaturneric <[email protected]>2023-12-02 14:01:16 +0000
commit0ec3358e4cfce7ad242e851c450292a25619221e (patch)
tree28a50308678ccbe0e0820d367f363df7df4e0be2 /src/core/function/gpg
parentfix: add set owner struct action in keymanager menu (diff)
downloadGpgFrontend-0ec3358e4cfce7ad242e851c450292a25619221e.tar.gz
GpgFrontend-0ec3358e4cfce7ad242e851c450292a25619221e.zip
feat: add buddled qt pinentry and make it works
Diffstat (limited to 'src/core/function/gpg')
-rw-r--r--src/core/function/gpg/GpgContext.cpp80
-rw-r--r--src/core/function/gpg/GpgContext.h5
-rw-r--r--src/core/function/gpg/GpgKeyOpera.cpp31
-rw-r--r--src/core/function/gpg/GpgKeyOpera.h4
4 files changed, 63 insertions, 57 deletions
diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp
index e6975ea4..9ee81bad 100644
--- a/src/core/function/gpg/GpgContext.cpp
+++ b/src/core/function/gpg/GpgContext.cpp
@@ -30,10 +30,19 @@
#include <gpg-error.h>
#include <gpgme.h>
+#include <qapplication.h>
+#include <qcoreapplication.h>
+#include <qeasingcurve.h>
#include <qeventloop.h>
+#include <qlabel.h>
#include <qobject.h>
+#include <qtmetamacros.h>
+#include <qwindowdefs.h>
+#include <sys/types.h>
#include <unistd.h>
+#include <cstring>
+
#include "core/function/CoreSignalStation.h"
#include "core/function/basic/GpgFunctionObject.h"
#include "core/function/gpg/GpgCommandExecutor.h"
@@ -44,6 +53,7 @@
#include "core/utils/CacheUtils.h"
#include "core/utils/CommonUtils.h"
#include "core/utils/GpgUtils.h"
+#include "function/CacheManager.h"
#include "spdlog/spdlog.h"
#ifdef _WIN32
@@ -247,36 +257,28 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
const char *passphrase_info, int last_was_bad,
int fd) -> gpgme_error_t {
auto *p_ctx = static_cast<GpgContext *>(hook);
- SPDLOG_DEBUG("custom passphrase cb called, bad times: {}", last_was_bad);
-
- if (last_was_bad > 3) {
- SPDLOG_WARN("failure_counts is over three times");
- return gpgme_error_from_errno(GPG_ERR_CANCELED);
- }
+ std::string passphrase;
- std::string passphrase = GetTempCacheValue("__key_passphrase");
- // no pawword is an error situation
- if (passphrase.empty()) {
- // user input passphrase
- SPDLOG_DEBUG("might need user to input passparase");
+ SPDLOG_DEBUG(
+ "custom passphrase cb called, uid: {}, info: {}, last_was_bad: {}",
+ uid_hint == nullptr ? "<empty>" : std::string{uid_hint},
+ passphrase_info == nullptr ? "<empty>" : std::string{passphrase_info},
+ last_was_bad);
- p_ctx->ShowPasswordInputDialog();
- passphrase = GetTempCacheValue("__key_passphrase");
+ emit CoreSignalStation::GetInstance()->SignalNeedUserInputPassphrase();
- SPDLOG_DEBUG("use may has inputed the passphrase");
-
- if (passphrase.empty()) {
- SPDLOG_ERROR("cannot get passphrase from use or passphrase is empty");
-
- gpgme_io_write(fd, "\n", 1);
- return gpgme_error_from_errno(GPG_ERR_CANCELED);
- }
- }
+ QEventLoop looper;
+ QObject::connect(
+ CoreSignalStation::GetInstance(),
+ &CoreSignalStation::SignalUserInputPassphraseCallback, &looper,
+ [&](const QByteArray &buffer) { passphrase = buffer.toStdString(); });
+ QObject::connect(CoreSignalStation::GetInstance(),
+ &CoreSignalStation::SignalUserInputPassphraseCallback,
+ &looper, &QEventLoop::quit);
+ looper.exec();
- // the user must at least write a newline character before returning from
- // the callback.
- passphrase = passphrase.append("\n");
auto passpahrase_size = passphrase.size();
+ SPDLOG_DEBUG("get passphrase from pinentry size: {}", passpahrase_size);
size_t off = 0;
size_t res = 0;
@@ -285,8 +287,12 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
if (res > 0) off += res;
} while (res > 0 && off != passpahrase_size);
- return off == passpahrase_size ? 0
- : gpgme_error_from_errno(GPG_ERR_CANCELED);
+ res += gpgme_io_write(fd, "\n", 1);
+
+ SPDLOG_DEBUG("custom passphrase cd is about to return, res: {}", res);
+ return res == passpahrase_size + 1
+ ? 0
+ : gpgme_error_from_errno(GPG_ERR_CANCELED);
}
static auto TestStatusCb(void *hook, const char *keyword, const char *args)
@@ -295,18 +301,6 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
return GPG_ERR_NO_ERROR;
}
- void ShowPasswordInputDialog() {
- emit parent_->SignalNeedUserInputPassphrase();
-
- QEventLoop looper;
- QObject::connect(CoreSignalStation::GetInstance(),
- &CoreSignalStation::SignalUserInputPassphraseDone, &looper,
- &QEventLoop::quit);
- looper.exec();
-
- SPDLOG_DEBUG("show password input dialog done");
- }
-
private:
struct CtxRefDeleter {
void operator()(gpgme_ctx_t _ctx) {
@@ -364,10 +358,6 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
SPDLOG_DEBUG("ctx set custom key db path: {}", database_path);
assert(CheckGpgError(err) == GPG_ERR_NO_ERROR);
}
-
- QObject::connect(parent_, &GpgContext::SignalNeedUserInputPassphrase,
- CoreSignalStation::GetInstance(),
- &CoreSignalStation::SignalNeedUserInputPassphrase);
}
};
@@ -389,10 +379,6 @@ GpgContext::operator gpgme_ctx_t() const {
return static_cast<gpgme_ctx_t>(*p_);
}
-void GpgContext::ShowPasswordInputDialog() {
- return p_->ShowPasswordInputDialog();
-}
-
GpgContext::~GpgContext() = default;
} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/function/gpg/GpgContext.h b/src/core/function/gpg/GpgContext.h
index b9042128..527099fc 100644
--- a/src/core/function/gpg/GpgContext.h
+++ b/src/core/function/gpg/GpgContext.h
@@ -71,11 +71,6 @@ class GPGFRONTEND_CORE_EXPORT GpgContext
void SetPassphraseCb(gpgme_passphrase_cb_t passphrase_cb) const;
- void ShowPasswordInputDialog();
-
- signals:
- void SignalNeedUserInputPassphrase();
-
private:
class Impl;
std::unique_ptr<Impl> p_;
diff --git a/src/core/function/gpg/GpgKeyOpera.cpp b/src/core/function/gpg/GpgKeyOpera.cpp
index e48a84d8..64f33373 100644
--- a/src/core/function/gpg/GpgKeyOpera.cpp
+++ b/src/core/function/gpg/GpgKeyOpera.cpp
@@ -28,6 +28,9 @@
#include "GpgKeyOpera.h"
+#include <gpg-error.h>
+#include <qeventloop.h>
+
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
@@ -41,8 +44,12 @@
#include "core/function/result_analyse/GpgResultAnalyse.h"
#include "core/model/GpgGenKeyInfo.h"
#include "core/module/ModuleManager.h"
+#include "core/thread/Task.h"
+#include "core/thread/TaskRunnerGetter.h"
#include "core/utils/CommonUtils.h"
#include "core/utils/GpgUtils.h"
+#include "model/DataObject.h"
+#include "spdlog/spdlog.h"
namespace GpgFrontend {
@@ -271,17 +278,33 @@ auto GpgKeyOpera::GenerateSubkey(const GpgKey& key,
return CheckGpgError(err);
}
-auto GpgKeyOpera::ModifyPassword(const GpgKey& key) -> GpgError {
+void GpgKeyOpera::ModifyPassword(const GpgKey& key,
+ std::function<void(gpgme_error_t)> callback) {
const auto gnupg_version = Module::RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gnupg_version", std::string{"2.0.0"});
SPDLOG_DEBUG("got gnupg version from rt: {}", gnupg_version);
if (CompareSoftwareVersion(gnupg_version, "2.0.15") < 0) {
SPDLOG_ERROR("operator not support");
- return GPG_ERR_NOT_SUPPORTED;
+ callback(GPG_ERR_NOT_SUPPORTED);
+ return;
}
- auto err = gpgme_op_passwd(ctx_, static_cast<gpgme_key_t>(key), 0);
- return CheckGpgError(err);
+
+ auto *task = new Thread::Task(
+ [&](const DataObjectPtr& data_object) -> int {
+ auto err = gpgme_op_passwd(ctx_, static_cast<gpgme_key_t>(key), 0);
+ data_object->Swap({err});
+ return 0;
+ },
+ "gpgme_op_passwd", TransferParams(),
+ [=](int, const DataObjectPtr& data_object) {
+ SPDLOG_DEBUG("callback called");
+ callback(ExtractParams<gpgme_error_t>(data_object, 0));
+ });
+
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_GPG)
+ ->PostTask(task);
}
auto GpgKeyOpera::ModifyTOFUPolicy(const GpgKey& key,
diff --git a/src/core/function/gpg/GpgKeyOpera.h b/src/core/function/gpg/GpgKeyOpera.h
index 0186cdbb..dd2ee60d 100644
--- a/src/core/function/gpg/GpgKeyOpera.h
+++ b/src/core/function/gpg/GpgKeyOpera.h
@@ -28,6 +28,8 @@
#pragma once
+#include <functional>
+
#include "core/function/gpg/GpgContext.h"
#include "core/function/result_analyse/GpgResultAnalyse.h"
#include "core/typedef/GpgTypedef.h"
@@ -98,7 +100,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera
* @param key
* @return GpgFrontend::GpgError
*/
- auto ModifyPassword(const GpgKey& key) -> GpgFrontend::GpgError;
+ void ModifyPassword(const GpgKey& key, std::function<void(gpgme_error_t)>);
/**
* @brief