diff options
author | saturneric <[email protected]> | 2025-04-17 15:28:04 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2025-04-17 15:28:04 +0000 |
commit | e276f9e86b161362d6f379ab790476936a0e6208 (patch) | |
tree | 65c27e3efcbf113a052bc053e1c2739f4a351217 /src/core | |
parent | fix: compiler issues on CI (diff) | |
download | GpgFrontend-e276f9e86b161362d6f379ab790476936a0e6208.tar.gz GpgFrontend-e276f9e86b161362d6f379ab790476936a0e6208.zip |
feat: generate cark keys
Diffstat (limited to 'src/core')
31 files changed, 473 insertions, 316 deletions
diff --git a/src/core/function/gpg/GpgAbstractKeyGetter.cpp b/src/core/function/gpg/GpgAbstractKeyGetter.cpp index 193fec0e..581d027e 100644 --- a/src/core/function/gpg/GpgAbstractKeyGetter.cpp +++ b/src/core/function/gpg/GpgAbstractKeyGetter.cpp @@ -54,7 +54,7 @@ auto GpgAbstractKeyGetter::Fetch() -> GpgAbstractKeyPtrList { auto GpgAbstractKeyGetter::GetGpgKeyTableModel() -> QSharedPointer<GpgKeyTableModel> { - return SecureCreateQSharedObject<GpgKeyTableModel>( + return SecureCreateSharedObject<GpgKeyTableModel>( SingletonFunctionObject::GetChannel(), Fetch(), nullptr); } diff --git a/src/core/function/gpg/GpgAutomatonHandler.cpp b/src/core/function/gpg/GpgAutomatonHandler.cpp index 33c94436..6cf06ab6 100644 --- a/src/core/function/gpg/GpgAutomatonHandler.cpp +++ b/src/core/function/gpg/GpgAutomatonHandler.cpp @@ -28,6 +28,8 @@ #include "GpgAutomatonHandler.h" +#include <utility> + #include "core/model/GpgData.h" #include "core/model/GpgKey.h" #include "core/utils/GpgUtils.h" @@ -37,9 +39,8 @@ namespace GpgFrontend { GpgAutomatonHandler::GpgAutomatonHandler(int channel) : SingletonFunctionObject<GpgAutomatonHandler>(channel) {} -auto GpgAutomatonHandler::interator_cb_func(void* handle, const char* status, - const char* args, - int fd) -> gpgme_error_t { +auto InteratorCbFunc(void* handle, const char* status, const char* args, + int fd) -> gpgme_error_t { auto* handle_struct = static_cast<AutomatonHandelStruct*>(handle); QString status_s = status; QString args_s = args; @@ -47,8 +48,26 @@ auto GpgAutomatonHandler::interator_cb_func(void* handle, const char* status, if (status_s == "KEY_CONSIDERED") { auto tokens = QString(args).split(' '); + if (handle_struct->KeyFpr().isEmpty()) return 0; + if (tokens.empty() || tokens[0] != handle_struct->KeyFpr()) { - LOG_W() << "handle struct key fpr " << handle_struct->KeyFpr() + LOG_W() << "handle struct key fpr: " << handle_struct->KeyFpr() + << "mismatch token: " << tokens[0] << ", exit..."; + + return -1; + } + + return 0; + } + + if (status_s == "CARDCTRL") { + auto tokens = QString(args).split(' '); + + if (handle_struct->SerialNumber().isEmpty()) return 0; + + if (tokens.empty() || tokens[0] != handle_struct->SerialNumber()) { + LOG_W() << "handle struct serial number: " + << handle_struct->SerialNumber() << "mismatch token: " << tokens[0] << ", exit..."; return -1; @@ -65,20 +84,22 @@ auto GpgAutomatonHandler::interator_cb_func(void* handle, const char* status, LOG_D() << "current state" << handle_struct->CurrentStatus() << "gpg status: " << status_s << ", args: " << args_s; + handle_struct->SetPromptStatus(status_s, args_s); + AutomatonState next_state = handle_struct->NextState(status_s, args_s); - if (next_state == AS_ERROR) { + if (next_state == GpgAutomatonHandler::kAS_ERROR) { FLOG_D("handle struct next state caught error, abort..."); return -1; } LOG_D() << "next state" << next_state; - if (next_state == AS_SAVE) { + if (next_state == GpgAutomatonHandler::kAS_SAVE) { handle_struct->SetSuccess(true); } // set state and preform action handle_struct->SetStatus(next_state); - Command cmd = handle_struct->Action(); + GpgAutomatonHandler::Command cmd = handle_struct->Action(); LOG_D() << "next action, cmd:" << cmd; @@ -86,7 +107,7 @@ auto GpgAutomatonHandler::interator_cb_func(void* handle, const char* status, auto btye_array = cmd.toUtf8(); gpgme_io_write(fd, btye_array, btye_array.size()); gpgme_io_write(fd, "\n", 1); - } else if (status_s == "GET_LINE") { + } else if (status_s.startsWith("GET_")) { // avoid trapping in this state return GPG_ERR_FALSE; } @@ -94,30 +115,40 @@ auto GpgAutomatonHandler::interator_cb_func(void* handle, const char* status, return 0; } -auto GpgAutomatonHandler::DoInteract( - const GpgKeyPtr& key, AutomatonNextStateHandler next_state_handler, - AutomatonActionHandler action_handler, int flags) -> bool { - gpgme_key_t p_key = - flags == GPGME_INTERACT_CARD ? nullptr : static_cast<gpgme_key_t>(*key); +auto DoInteractImpl(GpgContext& ctx_, const GpgKeyPtr& key, bool card_edit, + const QString& id, + AutomatonNextStateHandler next_state_handler, + AutomatonActionHandler action_handler, int flags) -> bool { + gpgme_key_t p_key = key == nullptr ? nullptr : static_cast<gpgme_key_t>(*key); - AutomatonHandelStruct handel_struct( - flags == GPGME_INTERACT_CARD ? "" : key->Fingerprint()); + AutomatonHandelStruct handel_struct(card_edit, id); handel_struct.SetHandler(std::move(next_state_handler), std::move(action_handler)); GpgData data_out; - auto err = gpgme_op_interact(ctx_.DefaultContext(), p_key, flags, - GpgAutomatonHandler::interator_cb_func, - static_cast<void*>(&handel_struct), data_out); + auto err = + gpgme_op_interact(ctx_.DefaultContext(), p_key, flags, InteratorCbFunc, + static_cast<void*>(&handel_struct), data_out); return CheckGpgError(err) == GPG_ERR_NO_ERROR && handel_struct.Success(); } +auto GpgAutomatonHandler::DoInteract( + const GpgKeyPtr& key, AutomatonNextStateHandler next_state_handler, + AutomatonActionHandler action_handler, int flags) -> bool { + assert(key != nullptr); + if (key == nullptr) return false; + return DoInteractImpl(ctx_, key, false, key->ID(), + std::move(next_state_handler), + std::move(action_handler), flags); +} + auto GpgAutomatonHandler::DoCardInteract( - AutomatonNextStateHandler next_state_handler, + const QString& serial_number, AutomatonNextStateHandler next_state_handler, AutomatonActionHandler action_handler) -> bool { - return DoInteract({}, std::move(next_state_handler), - std::move(action_handler), GPGME_INTERACT_CARD); + return DoInteractImpl(ctx_, nullptr, true, serial_number, + std::move(next_state_handler), + std::move(action_handler), GPGME_INTERACT_CARD); } auto GpgAutomatonHandler::AutomatonHandelStruct::NextState( @@ -133,7 +164,7 @@ void GpgAutomatonHandler::AutomatonHandelStruct::SetHandler( action_handler_ = std::move(action_handler); } -auto GpgAutomatonHandler::AutomatonHandelStruct::CurrentStatus() +auto GpgAutomatonHandler::AutomatonHandelStruct::CurrentStatus() const -> AutomatonState { return current_state_; } @@ -154,11 +185,27 @@ void GpgAutomatonHandler::AutomatonHandelStruct::SetSuccess(bool success) { auto GpgAutomatonHandler::AutomatonHandelStruct::Success() const -> bool { return success_; } -auto GpgAutomatonHandler::AutomatonHandelStruct::KeyFpr() -> QString { - return key_fpr_; +auto GpgAutomatonHandler::AutomatonHandelStruct::KeyFpr() const -> QString { + return card_edit_ ? "" : id_; } GpgAutomatonHandler::AutomatonHandelStruct::AutomatonHandelStruct( - QString key_fpr) - : key_fpr_(std::move(key_fpr)) {} + bool card_edit, QString id) + : card_edit_(card_edit), id_(std::move(id)) {} + +auto GpgAutomatonHandler::AutomatonHandelStruct::PromptStatus() const + -> std::tuple<QString, QString> { + return {prompt_status_, prompt_args_}; +} + +void GpgAutomatonHandler::AutomatonHandelStruct::SetPromptStatus(QString status, + QString args) { + prompt_status_ = std::move(status); + prompt_args_ = std::move(args); +} + +auto GpgAutomatonHandler::AutomatonHandelStruct::SerialNumber() const + -> QString { + return card_edit_ ? id_ : ""; +} } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgAutomatonHandler.h b/src/core/function/gpg/GpgAutomatonHandler.h index cdf96060..d142bbcc 100644 --- a/src/core/function/gpg/GpgAutomatonHandler.h +++ b/src/core/function/gpg/GpgAutomatonHandler.h @@ -28,6 +28,8 @@ #pragma once +#include <utility> + #include "core/GpgFrontendCore.h" #include "core/function/basic/GpgFunctionObject.h" #include "core/function/gpg/GpgContext.h" @@ -40,16 +42,18 @@ class GpgAutomatonHandler public: using Command = QString; using AutomatonState = enum { - AS_START, - AS_SELECT, - AS_COMMAND, - AS_VALUE, - AS_REASON_CODE, - AS_REASON_TEXT, - AS_REALLY_ULTIMATE, - AS_SAVE, - AS_ERROR, - AS_QUIT, + kAS_START, + kAS_SELECT, + kAS_COMMAND, + kAS_VALUE, + kAS_ADMIN, + kAS_REASON_CODE, + kAS_REASON_TEXT, + kAS_REALLY_ULTIMATE, + kAS_SAVE, + kAS_INFO, + kAS_ERROR, + kAS_QUIT, }; struct AutomatonHandelStruct; @@ -60,23 +64,32 @@ class GpgAutomatonHandler std::function<AutomatonState(AutomatonState, QString, QString)>; struct AutomatonHandelStruct { - explicit AutomatonHandelStruct(QString key_fpr); - void SetStatus(AutomatonState next_state); - auto CurrentStatus() -> AutomatonState; - void SetHandler(AutomatonNextStateHandler next_state_handler, - AutomatonActionHandler action_handler); + explicit AutomatonHandelStruct(bool card_edit, QString id); + auto NextState(QString gpg_status, QString args) -> AutomatonState; auto Action() -> Command; + + void SetStatus(AutomatonState next_state); void SetSuccess(bool success); + void SetPromptStatus(QString status, QString args); + void SetHandler(AutomatonNextStateHandler next_state_handler, + AutomatonActionHandler action_handler); + + [[nodiscard]] auto CurrentStatus() const -> AutomatonState; [[nodiscard]] auto Success() const -> bool; - auto KeyFpr() -> QString; + [[nodiscard]] auto KeyFpr() const -> QString; + [[nodiscard]] auto SerialNumber() const -> QString; + [[nodiscard]] auto PromptStatus() const -> std::tuple<QString, QString>; private: - AutomatonState current_state_ = AS_START; + AutomatonState current_state_ = kAS_START; AutomatonNextStateHandler next_state_handler_; AutomatonActionHandler action_handler_; bool success_ = false; - QString key_fpr_; + bool card_edit_; + QString id_; + QString prompt_status_; + QString prompt_args_; }; /** @@ -109,24 +122,13 @@ class GpgAutomatonHandler * @return true * @return false */ - auto DoCardInteract(AutomatonNextStateHandler next_state_handler, + auto DoCardInteract(const QString& serial_number, + AutomatonNextStateHandler next_state_handler, AutomatonActionHandler action_handler) -> bool; private: GpgContext& ctx_ = GpgContext::GetInstance(SingletonFunctionObject::GetChannel()); ///< - - /** - * @brief - * - * @param handle - * @param status - * @param args - * @param fd - * @return gpgme_error_t - */ - static auto interator_cb_func(void* handle, const char* status, - const char* args, int fd) -> gpgme_error_t; }; using AutomatonNextStateHandler = diff --git a/src/core/function/gpg/GpgKeyImportExporter.cpp b/src/core/function/gpg/GpgKeyImportExporter.cpp index f42c84ce..eda36273 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.cpp +++ b/src/core/function/gpg/GpgKeyImportExporter.cpp @@ -45,7 +45,7 @@ GpgKeyImportExporter::GpgKeyImportExporter(int channel) * @return Import information */ auto GpgKeyImportExporter::ImportKey(const GFBuffer& in_buffer) - -> std::shared_ptr<GpgImportInformation> { + -> QSharedPointer<GpgImportInformation> { if (in_buffer.Empty()) return {}; GpgData data_in(in_buffer); @@ -55,7 +55,8 @@ auto GpgKeyImportExporter::ImportKey(const GFBuffer& in_buffer) gpgme_import_result_t result; result = gpgme_op_import_result(ctx_.BinaryContext()); gpgme_import_status_t status = result->imports; - auto import_info = SecureCreateSharedObject<GpgImportInformation>(result); + + auto import_info = QSharedPointer<GpgImportInformation>::create(result); while (status != nullptr) { GpgImportInformation::GpgImportedKey key; key.import_status = static_cast<int>(status->status); diff --git a/src/core/function/gpg/GpgKeyImportExporter.h b/src/core/function/gpg/GpgKeyImportExporter.h index 4f938cb7..dcadec36 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.h +++ b/src/core/function/gpg/GpgKeyImportExporter.h @@ -58,7 +58,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyImportExporter * @param inBuffer * @return GpgImportInformation */ - auto ImportKey(const GFBuffer&) -> std::shared_ptr<GpgImportInformation>; + auto ImportKey(const GFBuffer&) -> QSharedPointer<GpgImportInformation>; /** * @brief diff --git a/src/core/function/gpg/GpgKeyManager.cpp b/src/core/function/gpg/GpgKeyManager.cpp index 9f43d03d..1fa050bb 100644 --- a/src/core/function/gpg/GpgKeyManager.cpp +++ b/src/core/function/gpg/GpgKeyManager.cpp @@ -106,62 +106,62 @@ auto GpgKeyManager::SetOwnerTrustLevel(const GpgKeyPtr& key, auto tokens = args.split(' '); switch (state) { - case GpgAutomatonHandler::AS_START: + case GpgAutomatonHandler::kAS_START: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_COMMAND; + return GpgAutomatonHandler::kAS_COMMAND; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_COMMAND: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: if (status == "GET_LINE" && args == "edit_ownertrust.value") { - return GpgAutomatonHandler::AS_VALUE; + return GpgAutomatonHandler::kAS_VALUE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_VALUE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_VALUE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_BOOL" && args == "edit_ownertrust.set_ultimate.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_QUIT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: if (status == "GET_BOOL" && args == "keyedit.save.okay") { - return GpgAutomatonHandler::AS_SAVE; + return GpgAutomatonHandler::kAS_SAVE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_ERROR: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_ERROR: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; default: - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; }; }; AutomatonActionHandler action_handler = [trust_level](AutomatonHandelStruct& handler, AutomatonState state) { switch (state) { - case GpgAutomatonHandler::AS_COMMAND: + case GpgAutomatonHandler::kAS_COMMAND: return QString("trust"); - case GpgAutomatonHandler::AS_VALUE: + case GpgAutomatonHandler::kAS_VALUE: handler.SetSuccess(true); return QString::number(trust_level); - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_QUIT: + case GpgAutomatonHandler::kAS_QUIT: return QString("quit"); - case GpgAutomatonHandler::AS_SAVE: + case GpgAutomatonHandler::kAS_SAVE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_START: - case GpgAutomatonHandler::AS_ERROR: + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: return QString(""); default: return QString(""); @@ -186,74 +186,67 @@ auto GpgKeyManager::DeleteSubkey(const GpgKeyPtr& key, auto tokens = args.split(' '); switch (state) { - case GpgAutomatonHandler::AS_START: + case GpgAutomatonHandler::kAS_START: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_SELECT; + return GpgAutomatonHandler::kAS_SELECT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_SELECT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_SELECT: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_COMMAND; + return GpgAutomatonHandler::kAS_COMMAND; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_COMMAND: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_BOOL" && args == "keyedit.remove.subkey.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_QUIT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: if (status == "GET_BOOL" && args == "keyedit.save.okay") { - return GpgAutomatonHandler::AS_SAVE; + return GpgAutomatonHandler::kAS_SAVE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_ERROR: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_ERROR: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; default: - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; }; }; AutomatonActionHandler action_handler = [subkey_index](AutomatonHandelStruct& handler, AutomatonState state) { switch (state) { - case GpgAutomatonHandler::AS_SELECT: + case GpgAutomatonHandler::kAS_SELECT: return QString("key %1").arg(subkey_index); - case GpgAutomatonHandler::AS_COMMAND: + case GpgAutomatonHandler::kAS_COMMAND: return QString("delkey"); - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_QUIT: + case GpgAutomatonHandler::kAS_QUIT: return QString("quit"); - case GpgAutomatonHandler::AS_SAVE: + case GpgAutomatonHandler::kAS_SAVE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_START: - case GpgAutomatonHandler::AS_ERROR: - return QString(""); + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: default: return QString(""); } return QString(""); }; - auto key_fpr = key->Fingerprint(); - AutomatonHandelStruct handel_struct(key_fpr); - handel_struct.SetHandler(next_state_handler, action_handler); - - GpgData data_out; - return GpgAutomatonHandler::GetInstance(GetChannel()) .DoInteract(key, next_state_handler, action_handler); } @@ -281,63 +274,63 @@ auto GpgKeyManager::RevokeSubkey(const GpgKeyPtr& key, int subkey_index, auto tokens = args.split(' '); switch (state) { - case GpgAutomatonHandler::AS_START: + case GpgAutomatonHandler::kAS_START: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_SELECT; + return GpgAutomatonHandler::kAS_SELECT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_SELECT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_SELECT: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_COMMAND; + return GpgAutomatonHandler::kAS_COMMAND; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_COMMAND: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_BOOL" && args == "keyedit.revoke.subkey.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REASON_CODE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REASON_CODE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_LINE" && args == "ask_revocation_reason.text") { - return GpgAutomatonHandler::AS_REASON_TEXT; + return GpgAutomatonHandler::kAS_REASON_TEXT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REASON_TEXT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REASON_TEXT: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_LINE" && args == "ask_revocation_reason.text") { - return GpgAutomatonHandler::AS_REASON_TEXT; + return GpgAutomatonHandler::kAS_REASON_TEXT; } else if (status == "GET_BOOL" && args == "ask_revocation_reason.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_LINE" && args == "ask_revocation_reason.code") { - return GpgAutomatonHandler::AS_REASON_CODE; + return GpgAutomatonHandler::kAS_REASON_CODE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_QUIT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: if (status == "GET_BOOL" && args == "keyedit.save.okay") { - return GpgAutomatonHandler::AS_SAVE; + return GpgAutomatonHandler::kAS_SAVE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_ERROR: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_ERROR: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; default: - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; }; }; @@ -345,38 +338,31 @@ auto GpgKeyManager::RevokeSubkey(const GpgKeyPtr& key, int subkey_index, [subkey_index, reason_code, reason_text_lines]( AutomatonHandelStruct& handler, AutomatonState state) { switch (state) { - case GpgAutomatonHandler::AS_SELECT: + case GpgAutomatonHandler::kAS_SELECT: return QString("key %1").arg(subkey_index); - case GpgAutomatonHandler::AS_COMMAND: + case GpgAutomatonHandler::kAS_COMMAND: return QString("revkey"); - case GpgAutomatonHandler::AS_REASON_CODE: + case GpgAutomatonHandler::kAS_REASON_CODE: return QString::number(reason_code); - case GpgAutomatonHandler::AS_REASON_TEXT: + case GpgAutomatonHandler::kAS_REASON_TEXT: return reason_text_lines->isEmpty() ? QString("") : QString(reason_text_lines->takeFirst().toUtf8()); - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: return QString("Y"); - case GpgAutomatonHandler::AS_QUIT: + case GpgAutomatonHandler::kAS_QUIT: return QString("quit"); - case GpgAutomatonHandler::AS_SAVE: + case GpgAutomatonHandler::kAS_SAVE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_START: - case GpgAutomatonHandler::AS_ERROR: - return QString(""); + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: default: return QString(""); } return QString(""); }; - auto key_fpr = key->Fingerprint(); - AutomatonHandelStruct handel_struct(key_fpr); - handel_struct.SetHandler(next_state_handler, action_handler); - - GpgData data_out; - return GpgAutomatonHandler::GetInstance(GetChannel()) .DoInteract(key, next_state_handler, action_handler); } diff --git a/src/core/function/gpg/GpgSmartCardManager.cpp b/src/core/function/gpg/GpgSmartCardManager.cpp index 4c55b63d..9937ef7a 100644 --- a/src/core/function/gpg/GpgSmartCardManager.cpp +++ b/src/core/function/gpg/GpgSmartCardManager.cpp @@ -41,39 +41,36 @@ auto GpgSmartCardManager::Fetch(const QString& serial_number) -> bool { auto tokens = args.split(' '); switch (state) { - case GpgAutomatonHandler::AS_START: - if (status == "CARDCTRL" && args.contains(serial_number)) { - return GpgAutomatonHandler::AS_START; - } else if (status == "GET_LINE" && args == "cardedit.prompt") { - return GpgAutomatonHandler::AS_COMMAND; + case GpgAutomatonHandler::kAS_START: + if (status == "GET_LINE" && args == "cardedit.prompt") { + return GpgAutomatonHandler::kAS_COMMAND; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_COMMAND: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: if (status == "GET_LINE" && args == "cardedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_QUIT: - case GpgAutomatonHandler::AS_ERROR: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: + case GpgAutomatonHandler::kAS_ERROR: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; default: - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; }; }; AutomatonActionHandler action_handler = [](AutomatonHandelStruct& handler, AutomatonState state) { switch (state) { - case GpgAutomatonHandler::AS_COMMAND: + case GpgAutomatonHandler::kAS_COMMAND: return QString("fetch"); - case GpgAutomatonHandler::AS_QUIT: + case GpgAutomatonHandler::kAS_QUIT: return QString("quit"); - case GpgAutomatonHandler::AS_START: - case GpgAutomatonHandler::AS_ERROR: - return QString(""); + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: default: return QString(""); } @@ -81,7 +78,7 @@ auto GpgSmartCardManager::Fetch(const QString& serial_number) -> bool { }; return GpgAutomatonHandler::GetInstance(GetChannel()) - .DoCardInteract(next_state_handler, action_handler); + .DoCardInteract(serial_number, next_state_handler, action_handler); } auto GpgSmartCardManager::GetSerialNumbers() -> QStringList { @@ -256,4 +253,126 @@ auto GpgSmartCardManager::ModifyPin(const QString& pin_ref) return {true, {}}; } +auto GpgSmartCardManager::GenerateKey( + const QString& serial_number, const QString& name, const QString& email, + const QString& comment, const QDateTime& expire, + bool non_expire) -> std::tuple<bool, QString> { + if (name.isEmpty() || email.isEmpty()) { + return {false, "name or email is empty"}; + } + + qint64 days_before_expire = 0; + if (!non_expire) { + days_before_expire = QDateTime::currentDateTime().daysTo(expire); + } + + GpgAutomatonHandler::AutomatonNextStateHandler next_state_handler = + [=](AutomatonState state, const QString& status, const QString& args) { + auto tokens = args.split(' '); + + switch (state) { + case GpgAutomatonHandler::kAS_START: + if (status == "GET_LINE" && args == "cardedit.prompt") { + return GpgAutomatonHandler::kAS_ADMIN; + } + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_ADMIN: + if (status == "GET_LINE" && args == "cardedit.prompt") { + return GpgAutomatonHandler::kAS_COMMAND; + } + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: + if (status == "GET_LINE" && args == "cardedit.genkeys.backup_enc") { + return GpgAutomatonHandler::kAS_COMMAND; + } + if (status == "GET_BOOL" && + args == "cardedit.genkeys.replace_keys") { + return GpgAutomatonHandler::kAS_COMMAND; + } + if (status == "GET_LINE" && args == "keygen.valid") { + return GpgAutomatonHandler::kAS_COMMAND; + } + if (status == "GET_LINE" && args == "keygen.name") { + return GpgAutomatonHandler::kAS_COMMAND; + } + if (status == "GET_LINE" && args == "keygen.email") { + return GpgAutomatonHandler::kAS_COMMAND; + } + if (status == "GET_LINE" && args == "keygen.comment") { + return GpgAutomatonHandler::kAS_COMMAND; + } + if (status == "PINENTRY_LAUNCHED" || + status == "BACKUP_KEY_CREATED" || status == "KEY_CONSIDERED" || + status == "KEY_CREATED") { + return GpgAutomatonHandler::kAS_INFO; + } + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_INFO: + if (status == "PINENTRY_LAUNCHED" || + status == "BACKUP_KEY_CREATED" || status == "KEY_CONSIDERED" || + status == "KEY_CREATED") { + return GpgAutomatonHandler::kAS_INFO; + } + if (status == "GET_LINE" && args == "cardedit.prompt") { + return GpgAutomatonHandler::kAS_QUIT; + } + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: + case GpgAutomatonHandler::kAS_ERROR: + if (status == "GET_LINE" && args == "keyedit.prompt") { + return GpgAutomatonHandler::kAS_QUIT; + } + return GpgAutomatonHandler::kAS_ERROR; + default: + return GpgAutomatonHandler::kAS_ERROR; + }; + }; + + AutomatonActionHandler action_handler = [=](AutomatonHandelStruct& handler, + AutomatonState state) { + switch (state) { + case GpgAutomatonHandler::kAS_ADMIN: + return QString("admin"); + case GpgAutomatonHandler::kAS_COMMAND: { + auto [status, args] = handler.PromptStatus(); + if (args == "cardedit.prompt") { + return QString("generate"); + } + if (args == "cardedit.genkeys.backup_enc") { + return QString("y"); + } + if (args == "cardedit.genkeys.replace_keys") { + return QString("y"); + } + if (args == "keygen.valid") { + return QString::number(days_before_expire); + } + if (args == "keygen.name") { + return name; + } + if (args == "keygen.email") { + return email; + } + if (args == "keygen.comment") { + return comment; + } + return QString{}; + } + case GpgAutomatonHandler::kAS_QUIT: + return QString("quit"); + case GpgAutomatonHandler::kAS_INFO: + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: + default: + return QString{}; + } + + return QString{}; + }; + + GpgAutomatonHandler::GetInstance(GetChannel()) + .DoCardInteract(serial_number, next_state_handler, action_handler); + + return {true, {}}; +} } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgSmartCardManager.h b/src/core/function/gpg/GpgSmartCardManager.h index 2d3038b4..78c21bc0 100644 --- a/src/core/function/gpg/GpgSmartCardManager.h +++ b/src/core/function/gpg/GpgSmartCardManager.h @@ -99,6 +99,16 @@ class GPGFRONTEND_CORE_EXPORT GpgSmartCardManager */ auto ModifyPin(const QString& pin_ref) -> std::tuple<bool, QString>; + /** + * @brief + * + * @return auto + */ + auto GenerateKey(const QString& serial_number, const QString& name, + const QString& email, const QString& comment, + const QDateTime& expire, + bool non_expire) -> std::tuple<bool, QString>; + private: GpgContext& ctx_ = GpgContext::GetInstance(SingletonFunctionObject::GetChannel()); ///< diff --git a/src/core/function/gpg/GpgUIDOperator.cpp b/src/core/function/gpg/GpgUIDOperator.cpp index e34d224a..e07b4192 100644 --- a/src/core/function/gpg/GpgUIDOperator.cpp +++ b/src/core/function/gpg/GpgUIDOperator.cpp @@ -70,60 +70,60 @@ auto GpgUIDOperator::DeleteUID(const GpgKeyPtr& key, int uid_index) -> bool { auto tokens = args.split(' '); switch (state) { - case GpgAutomatonHandler::AS_START: + case GpgAutomatonHandler::kAS_START: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_SELECT; + return GpgAutomatonHandler::kAS_SELECT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_SELECT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_SELECT: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_COMMAND; + return GpgAutomatonHandler::kAS_COMMAND; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_COMMAND: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_BOOL" && args == "keyedit.remove.uid.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_QUIT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: if (status == "GET_BOOL" && args == "keyedit.save.okay") { - return GpgAutomatonHandler::AS_SAVE; + return GpgAutomatonHandler::kAS_SAVE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_ERROR: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_ERROR: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; default: - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; }; }; AutomatonActionHandler action_handler = [uid_index](AutomatonHandelStruct& handler, AutomatonState state) { switch (state) { - case GpgAutomatonHandler::AS_SELECT: + case GpgAutomatonHandler::kAS_SELECT: return QString("uid %1").arg(uid_index); - case GpgAutomatonHandler::AS_COMMAND: + case GpgAutomatonHandler::kAS_COMMAND: return QString("deluid"); - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_QUIT: + case GpgAutomatonHandler::kAS_QUIT: return QString("quit"); - case GpgAutomatonHandler::AS_SAVE: + case GpgAutomatonHandler::kAS_SAVE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_START: - case GpgAutomatonHandler::AS_ERROR: + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: return QString(""); default: return QString(""); @@ -131,12 +131,6 @@ auto GpgUIDOperator::DeleteUID(const GpgKeyPtr& key, int uid_index) -> bool { return QString(""); }; - auto key_fpr = key->Fingerprint(); - AutomatonHandelStruct handel_struct(key_fpr); - handel_struct.SetHandler(next_state_handler, action_handler); - - GpgData data_out; - return GpgAutomatonHandler::GetInstance(GetChannel()) .DoInteract(key, next_state_handler, action_handler); } @@ -164,62 +158,62 @@ auto GpgUIDOperator::RevokeUID(const GpgKeyPtr& key, int uid_index, auto tokens = args.split(' '); switch (state) { - case GpgAutomatonHandler::AS_START: + case GpgAutomatonHandler::kAS_START: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_SELECT; + return GpgAutomatonHandler::kAS_SELECT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_SELECT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_SELECT: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_COMMAND; + return GpgAutomatonHandler::kAS_COMMAND; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_COMMAND: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_COMMAND: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_BOOL" && args == "keyedit.revoke.uid.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REASON_CODE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REASON_CODE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_LINE" && args == "ask_revocation_reason.text") { - return GpgAutomatonHandler::AS_REASON_TEXT; + return GpgAutomatonHandler::kAS_REASON_TEXT; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REASON_TEXT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REASON_TEXT: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_LINE" && args == "ask_revocation_reason.text") { - return GpgAutomatonHandler::AS_REASON_TEXT; + return GpgAutomatonHandler::kAS_REASON_TEXT; } else if (status == "GET_BOOL" && args == "ask_revocation_reason.okay") { - return GpgAutomatonHandler::AS_REALLY_ULTIMATE; + return GpgAutomatonHandler::kAS_REALLY_ULTIMATE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } else if (status == "GET_LINE" && args == "ask_revocation_reason.code") { - return GpgAutomatonHandler::AS_REASON_CODE; + return GpgAutomatonHandler::kAS_REASON_CODE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_QUIT: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_QUIT: if (status == "GET_BOOL" && args == "keyedit.save.okay") { - return GpgAutomatonHandler::AS_SAVE; + return GpgAutomatonHandler::kAS_SAVE; } - return GpgAutomatonHandler::AS_ERROR; - case GpgAutomatonHandler::AS_ERROR: + return GpgAutomatonHandler::kAS_ERROR; + case GpgAutomatonHandler::kAS_ERROR: if (status == "GET_LINE" && args == "keyedit.prompt") { - return GpgAutomatonHandler::AS_QUIT; + return GpgAutomatonHandler::kAS_QUIT; } - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; default: - return GpgAutomatonHandler::AS_ERROR; + return GpgAutomatonHandler::kAS_ERROR; }; }; @@ -227,25 +221,25 @@ auto GpgUIDOperator::RevokeUID(const GpgKeyPtr& key, int uid_index, [uid_index, reason_code, reason_text_lines]( AutomatonHandelStruct& handler, AutomatonState state) { switch (state) { - case GpgAutomatonHandler::AS_SELECT: + case GpgAutomatonHandler::kAS_SELECT: return QString("uid %1").arg(uid_index); - case GpgAutomatonHandler::AS_COMMAND: + case GpgAutomatonHandler::kAS_COMMAND: return QString("revuid"); - case GpgAutomatonHandler::AS_REASON_CODE: + case GpgAutomatonHandler::kAS_REASON_CODE: return QString::number(reason_code); - case GpgAutomatonHandler::AS_REASON_TEXT: + case GpgAutomatonHandler::kAS_REASON_TEXT: return reason_text_lines->isEmpty() ? QString("") : QString(reason_text_lines->takeFirst().toUtf8()); - case GpgAutomatonHandler::AS_REALLY_ULTIMATE: + case GpgAutomatonHandler::kAS_REALLY_ULTIMATE: return QString("Y"); - case GpgAutomatonHandler::AS_QUIT: + case GpgAutomatonHandler::kAS_QUIT: return QString("quit"); - case GpgAutomatonHandler::AS_SAVE: + case GpgAutomatonHandler::kAS_SAVE: handler.SetSuccess(true); return QString("Y"); - case GpgAutomatonHandler::AS_START: - case GpgAutomatonHandler::AS_ERROR: + case GpgAutomatonHandler::kAS_START: + case GpgAutomatonHandler::kAS_ERROR: return QString(""); default: return QString(""); @@ -253,12 +247,6 @@ auto GpgUIDOperator::RevokeUID(const GpgKeyPtr& key, int uid_index, return QString(""); }; - auto key_fpr = key->Fingerprint(); - AutomatonHandelStruct handel_struct(key_fpr); - handel_struct.SetHandler(next_state_handler, action_handler); - - GpgData data_out; - return GpgAutomatonHandler::GetInstance(GetChannel()) .DoInteract(key, next_state_handler, action_handler); } diff --git a/src/core/model/DataObject.h b/src/core/model/DataObject.h index efd75c16..ea8fc4c4 100644 --- a/src/core/model/DataObject.h +++ b/src/core/model/DataObject.h @@ -80,13 +80,13 @@ class GPGFRONTEND_CORE_EXPORT DataObject { }; template <typename... Args> -auto TransferParams(Args&&... args) -> std::shared_ptr<DataObject> { +auto TransferParams(Args&&... args) -> QSharedPointer<DataObject> { return GpgFrontend::SecureCreateSharedObject<DataObject>( DataObject{std::forward<Args>(args)...}); } template <typename T> -auto ExtractParams(const std::shared_ptr<DataObject>& d_o, int index) -> T { +auto ExtractParams(const QSharedPointer<DataObject>& d_o, int index) -> T { if (!d_o) { throw std::invalid_argument("nullptr provided for DataObjectPtr"); } @@ -95,7 +95,7 @@ auto ExtractParams(const std::shared_ptr<DataObject>& d_o, int index) -> T { void swap(DataObject& a, DataObject& b) noexcept; -using DataObjectPtr = std::shared_ptr<DataObject>; ///< +using DataObjectPtr = QSharedPointer<DataObject>; ///< using OperaRunnable = std::function<GFError(DataObjectPtr)>; using OperationCallback = std::function<void(GFError, DataObjectPtr)>; diff --git a/src/core/model/GpgDecryptResult.cpp b/src/core/model/GpgDecryptResult.cpp index a2e2c9a7..bcfbe908 100644 --- a/src/core/model/GpgDecryptResult.cpp +++ b/src/core/model/GpgDecryptResult.cpp @@ -31,7 +31,7 @@ namespace GpgFrontend { GpgDecryptResult::GpgDecryptResult(gpgme_decrypt_result_t r) - : result_ref_(std::shared_ptr<struct _gpgme_op_decrypt_result>( + : result_ref_(QSharedPointer<struct _gpgme_op_decrypt_result>( (gpgme_result_ref(r), r), [](gpgme_decrypt_result_t p) { if (p != nullptr) { gpgme_result_unref(p); diff --git a/src/core/model/GpgDecryptResult.h b/src/core/model/GpgDecryptResult.h index 1b755868..772b24cb 100644 --- a/src/core/model/GpgDecryptResult.h +++ b/src/core/model/GpgDecryptResult.h @@ -48,7 +48,7 @@ class GPGFRONTEND_CORE_EXPORT GpgDecryptResult { virtual ~GpgDecryptResult(); private: - std::shared_ptr<struct _gpgme_op_decrypt_result> result_ref_ = nullptr; ///< + QSharedPointer<struct _gpgme_op_decrypt_result> result_ref_ = nullptr; ///< }; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgEncryptResult.cpp b/src/core/model/GpgEncryptResult.cpp index 69a8535a..a3a29fcc 100644 --- a/src/core/model/GpgEncryptResult.cpp +++ b/src/core/model/GpgEncryptResult.cpp @@ -30,7 +30,7 @@ namespace GpgFrontend { GpgEncryptResult::GpgEncryptResult(gpgme_encrypt_result_t r) - : result_ref_(std::shared_ptr<struct _gpgme_op_encrypt_result>( + : result_ref_(QSharedPointer<struct _gpgme_op_encrypt_result>( (gpgme_result_ref(r), r), [](gpgme_encrypt_result_t p) { if (p != nullptr) { gpgme_result_unref(p); diff --git a/src/core/model/GpgEncryptResult.h b/src/core/model/GpgEncryptResult.h index 97a52a03..9167f28d 100644 --- a/src/core/model/GpgEncryptResult.h +++ b/src/core/model/GpgEncryptResult.h @@ -47,7 +47,7 @@ class GPGFRONTEND_CORE_EXPORT GpgEncryptResult { virtual ~GpgEncryptResult(); private: - std::shared_ptr<struct _gpgme_op_encrypt_result> result_ref_ = nullptr; ///< + QSharedPointer<struct _gpgme_op_encrypt_result> result_ref_ = nullptr; ///< }; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgGenerateKeyResult.cpp b/src/core/model/GpgGenerateKeyResult.cpp index 2b455a22..928321c7 100644 --- a/src/core/model/GpgGenerateKeyResult.cpp +++ b/src/core/model/GpgGenerateKeyResult.cpp @@ -33,7 +33,7 @@ namespace GpgFrontend { GpgGenerateKeyResult::GpgGenerateKeyResult(gpgme_genkey_result_t r) - : result_ref_(std::shared_ptr<struct _gpgme_op_genkey_result>( + : result_ref_(QSharedPointer<struct _gpgme_op_genkey_result>( (gpgme_result_ref(r), r), [](gpgme_genkey_result_t p) { if (p != nullptr) { gpgme_result_unref(p); diff --git a/src/core/model/GpgGenerateKeyResult.h b/src/core/model/GpgGenerateKeyResult.h index ac5d6b05..80e49a38 100644 --- a/src/core/model/GpgGenerateKeyResult.h +++ b/src/core/model/GpgGenerateKeyResult.h @@ -52,8 +52,7 @@ class GPGFRONTEND_CORE_EXPORT GpgGenerateKeyResult { virtual ~GpgGenerateKeyResult(); private: - using ResultRefHandler = - std::shared_ptr<struct _gpgme_op_genkey_result>; ///< + using ResultRefHandler = QSharedPointer<struct _gpgme_op_genkey_result>; ///< ResultRefHandler result_ref_ = nullptr; ///< }; diff --git a/src/core/model/GpgKeyTreeModel.cpp b/src/core/model/GpgKeyTreeModel.cpp index 92a0f77c..f521e8c9 100644 --- a/src/core/model/GpgKeyTreeModel.cpp +++ b/src/core/model/GpgKeyTreeModel.cpp @@ -218,6 +218,7 @@ auto GpgKeyTreeModel::create_gpg_key_tree_items(const GpgAbstractKeyPtr &key) columns << g_key->Algo(); columns << QLocale().toString(g_key->CreationTime(), "yyyy-MM-dd"); + assert(key != nullptr); auto i_key = QSharedPointer<GpgKeyTreeItem>::create(key, columns); i_key->SetEnable(true); i_key->SetCheckable(checkable_detector_(i_key->Key())); @@ -317,9 +318,15 @@ void GpgKeyTreeItem::SetChecked(bool checked) { checked_ = checked; } void GpgKeyTreeItem::SetCheckable(bool checkable) { checkable_ = checkable; } -auto GpgKeyTreeItem::Key() const -> GpgAbstractKey * { return key_.get(); } +auto GpgKeyTreeItem::Key() const -> GpgAbstractKey * { + assert(key_ != nullptr); + return key_.get(); +} -auto GpgKeyTreeItem::SharedKey() const -> GpgAbstractKeyPtr { return key_; } +auto GpgKeyTreeItem::SharedKey() const -> GpgAbstractKeyPtr { + assert(key_ != nullptr); + return key_; +} auto GpgKeyTreeItem::Enable() const -> bool { return enable_; } diff --git a/src/core/model/GpgSignResult.cpp b/src/core/model/GpgSignResult.cpp index 8227d46e..6428a461 100644 --- a/src/core/model/GpgSignResult.cpp +++ b/src/core/model/GpgSignResult.cpp @@ -29,7 +29,7 @@ #include "GpgSignResult.h" namespace GpgFrontend { GpgSignResult::GpgSignResult(gpgme_sign_result_t r) - : result_ref_(std::shared_ptr<struct _gpgme_op_sign_result>( + : result_ref_(QSharedPointer<struct _gpgme_op_sign_result>( (gpgme_result_ref(r), r), [](gpgme_sign_result_t p) { if (p != nullptr) { gpgme_result_unref(p); diff --git a/src/core/model/GpgSignResult.h b/src/core/model/GpgSignResult.h index 75f106f6..434a590e 100644 --- a/src/core/model/GpgSignResult.h +++ b/src/core/model/GpgSignResult.h @@ -50,6 +50,6 @@ class GPGFRONTEND_CORE_EXPORT GpgSignResult { virtual ~GpgSignResult(); private: - std::shared_ptr<struct _gpgme_op_sign_result> result_ref_ = nullptr; ///< + QSharedPointer<struct _gpgme_op_sign_result> result_ref_ = nullptr; ///< }; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgVerifyResult.cpp b/src/core/model/GpgVerifyResult.cpp index ca5784d8..aab23028 100644 --- a/src/core/model/GpgVerifyResult.cpp +++ b/src/core/model/GpgVerifyResult.cpp @@ -32,7 +32,7 @@ namespace GpgFrontend { GpgVerifyResult::GpgVerifyResult(gpgme_verify_result_t r) - : result_ref_(std::shared_ptr<struct _gpgme_op_verify_result>( + : result_ref_(QSharedPointer<struct _gpgme_op_verify_result>( (gpgme_result_ref(r), r), [](gpgme_verify_result_t p) { if (p != nullptr) { gpgme_result_unref(p); diff --git a/src/core/model/GpgVerifyResult.h b/src/core/model/GpgVerifyResult.h index 6e61497e..b928d2ca 100644 --- a/src/core/model/GpgVerifyResult.h +++ b/src/core/model/GpgVerifyResult.h @@ -48,6 +48,6 @@ class GPGFRONTEND_CORE_EXPORT GpgVerifyResult { virtual ~GpgVerifyResult(); private: - std::shared_ptr<struct _gpgme_op_verify_result> result_ref_ = nullptr; ///< + QSharedPointer<struct _gpgme_op_verify_result> result_ref_ = nullptr; ///< }; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/module/Event.h b/src/core/module/Event.h index d37992b7..a6660266 100644 --- a/src/core/module/Event.h +++ b/src/core/module/Event.h @@ -40,7 +40,7 @@ namespace GpgFrontend::Module { class Event; -using EventReference = std::shared_ptr<Event>; +using EventReference = QSharedPointer<Event>; using EventIdentifier = QString; using EventTriggerIdentifier = QString; using Evnets = QContainer<Event>; diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index fd530432..6e971089 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -353,7 +353,7 @@ class GlobalModuleContext::Impl { QStringList listening_event_ids; }; - using ModuleRegisterInfoPtr = std::shared_ptr<ModuleRegisterInfo>; + using ModuleRegisterInfoPtr = QSharedPointer<ModuleRegisterInfo>; std::unordered_map<ModuleIdentifier, ModuleRegisterInfoPtr> module_register_table_; diff --git a/src/core/module/GlobalModuleContext.h b/src/core/module/GlobalModuleContext.h index e0f8e644..611926b1 100644 --- a/src/core/module/GlobalModuleContext.h +++ b/src/core/module/GlobalModuleContext.h @@ -43,13 +43,13 @@ class GlobalRegisterTable; class Module; class ModuleManager; using ModuleIdentifier = QString; -using ModulePtr = std::shared_ptr<Module>; +using ModulePtr = QSharedPointer<Module>; using ModuleRawPtr = Module*; -using GMCPtr = std::shared_ptr<GlobalModuleContext>; -using GRTPtr = std::shared_ptr<GlobalRegisterTable>; +using GMCPtr = QSharedPointer<GlobalModuleContext>; +using GRTPtr = QSharedPointer<GlobalRegisterTable>; -using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; +using TaskRunnerPtr = QSharedPointer<Thread::TaskRunner>; class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject { Q_OBJECT diff --git a/src/core/module/GlobalRegisterTable.cpp b/src/core/module/GlobalRegisterTable.cpp index 6d83c2fb..e873f527 100644 --- a/src/core/module/GlobalRegisterTable.cpp +++ b/src/core/module/GlobalRegisterTable.cpp @@ -58,7 +58,7 @@ class GlobalRegisterTable::Impl { explicit Impl(GlobalRegisterTable* parent) : parent_(parent), - root_node_(SecureCreateQSharedObject<RTNode>("", nullptr)) {} + root_node_(SecureCreateSharedObject<RTNode>("", nullptr)) {} auto PublishKV(const Namespace& n, const Key& k, std::any v) -> bool { QStringList const segments = (n + "." + k).split('.'); @@ -72,7 +72,7 @@ class GlobalRegisterTable::Impl { auto it = current->children.find(segment); if (it == current->children.end()) { it = current->children.insert( - segment, SecureCreateQSharedObject<RTNode>(segment, current)); + segment, SecureCreateSharedObject<RTNode>(segment, current)); } current = it.value(); } diff --git a/src/core/module/Module.h b/src/core/module/Module.h index d6182688..19c86be4 100644 --- a/src/core/module/Module.h +++ b/src/core/module/Module.h @@ -40,9 +40,9 @@ class ModuleManager; using ModuleIdentifier = QString; using ModuleVersion = QString; using ModuleMetaData = QMap<QString, QString>; -using ModulePtr = std::shared_ptr<Module>; +using ModulePtr = QSharedPointer<Module>; -using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; +using TaskRunnerPtr = QSharedPointer<Thread::TaskRunner>; class GPGFRONTEND_CORE_EXPORT Module : public QObject { Q_OBJECT diff --git a/src/core/module/ModuleManager.h b/src/core/module/ModuleManager.h index 0077fe44..2c7d3bee 100644 --- a/src/core/module/ModuleManager.h +++ b/src/core/module/ModuleManager.h @@ -41,7 +41,7 @@ class TaskRunner; namespace GpgFrontend::Module { -using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; +using TaskRunnerPtr = QSharedPointer<Thread::TaskRunner>; class Event; class Module; @@ -49,11 +49,11 @@ class GlobalModuleContext; class ModuleManager; class GlobalRegisterTable; -using EventReference = std::shared_ptr<Event>; +using EventReference = QSharedPointer<Event>; using ModuleIdentifier = QString; -using ModulePtr = std::shared_ptr<Module>; -using ModuleMangerPtr = std::shared_ptr<ModuleManager>; -using GMCPtr = std::shared_ptr<GlobalModuleContext>; +using ModulePtr = QSharedPointer<Module>; +using ModuleMangerPtr = QSharedPointer<ModuleManager>; +using GMCPtr = QSharedPointer<GlobalModuleContext>; using Namespace = QString; using Key = QString; using LPCallback = std::function<void(Namespace, Key, int, std::any)>; diff --git a/src/core/thread/TaskRunnerGetter.h b/src/core/thread/TaskRunnerGetter.h index cea775f9..dd69c888 100644 --- a/src/core/thread/TaskRunnerGetter.h +++ b/src/core/thread/TaskRunnerGetter.h @@ -36,7 +36,7 @@ namespace GpgFrontend::Thread { -using TaskRunnerPtr = std::shared_ptr<TaskRunner>; +using TaskRunnerPtr = QSharedPointer<TaskRunner>; class GPGFRONTEND_CORE_EXPORT TaskRunnerGetter : public GpgFrontend::SingletonFunctionObject<TaskRunnerGetter> { diff --git a/src/core/utils/CommonUtils.cpp b/src/core/utils/CommonUtils.cpp index 0adc4d7f..ce36c71e 100644 --- a/src/core/utils/CommonUtils.cpp +++ b/src/core/utils/CommonUtils.cpp @@ -107,4 +107,11 @@ auto GPGFRONTEND_CORE_EXPORT ParseHexEncodedVersionTuple(const QString& s) const auto version = s.toUtf8().toUInt(&ok, 16); return ok ? static_cast<int>(version) : -1; } + +const auto kReEmail = QRegularExpression{ + R"((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))"}; + +auto GPGFRONTEND_CORE_EXPORT IsEmailAddress(const QString& str) -> bool { + return kReEmail.match(str).hasMatch(); +} } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/CommonUtils.h b/src/core/utils/CommonUtils.h index 468d8a59..d62f4867 100644 --- a/src/core/utils/CommonUtils.h +++ b/src/core/utils/CommonUtils.h @@ -82,4 +82,12 @@ auto GPGFRONTEND_CORE_EXPORT IsFlatpakENV() -> bool; auto GPGFRONTEND_CORE_EXPORT ParseHexEncodedVersionTuple(const QString &s) -> int; +/** + * @brief + * + * @return true + * @return false + */ +auto GPGFRONTEND_CORE_EXPORT IsEmailAddress(const QString &) -> bool; + } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/MemoryUtils.h b/src/core/utils/MemoryUtils.h index 223c6f82..6c2bb69f 100644 --- a/src/core/utils/MemoryUtils.h +++ b/src/core/utils/MemoryUtils.h @@ -143,24 +143,7 @@ static auto SecureCreateUniqueObject(Args &&...args) } template <typename T, typename... Args> -auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> { - void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); - if (!mem) throw std::bad_alloc(); - - try { - T *obj = new (mem) T(std::forward<Args>(args)...); - return std::shared_ptr<T>(obj, [](T *ptr) { - ptr->~T(); - SecureMemoryAllocator::Deallocate(ptr); - }); - } catch (...) { - SecureMemoryAllocator::Deallocate(mem); - throw; - } -} - -template <typename T, typename... Args> -auto SecureCreateQSharedObject(Args &&...args) -> QSharedPointer<T> { +auto SecureCreateSharedObject(Args &&...args) -> QSharedPointer<T> { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); |