diff options
Diffstat (limited to '')
35 files changed, 455 insertions, 377 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 44eb370c..6477039d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,3 @@ -if(QT_MOC_CONFIG) - file(GLOB_RECURSE GPGFRONTEND_HEADER_FILES RELACTIVE ${CMAKE_SOURCE_DIR}/include/*.h) - qt5_wrap_cpp(QT5_MOCS ${GPGFRONTEND_HEADER_FILES} TARGET ${AppName}) -endif() - if(GPG_CORE) message(STATUS "Build Gpg Core") add_subdirectory(gpg) diff --git a/src/GpgFrontendBuildInfo.h b/src/GpgFrontendBuildInfo.h index afc1b2c9..54270bb4 100644 --- a/src/GpgFrontendBuildInfo.h +++ b/src/GpgFrontendBuildInfo.h @@ -36,19 +36,19 @@ * Code Version (According to Git) */ #define GIT_BRANCH_NAME "develop-core" -#define GIT_COMMIT_HASH "7b7dd67b99a758924e531a63efd25894a32a3298" +#define GIT_COMMIT_HASH "f8668adc7fe319896a3444409c076bd61c690c84" /** * Generated Information (According to CMake) */ #define PROJECT_NAME "GpgFrontend" #define BUILD_VERSION "1.3.1_Linux-5.4.0-81-generic_x86_64_Debug" -#define GIT_VERSION "develop-core_7b7dd67b99a758924e531a63efd25894a32a3298" +#define GIT_VERSION "develop-core_f8668adc7fe319896a3444409c076bd61c690c84" /** * Build Information */ #define BUILD_FLAG 1 -#define BUILD_TIMESTAMP "2021-09-05 14:39:04" +#define BUILD_TIMESTAMP "2021-09-05 21:22:47" #endif // GPGFRONTEND_BUILD_INFO_H_IN diff --git a/src/gpg/CMakeLists.txt b/src/gpg/CMakeLists.txt index 6b767814..01956fc0 100644 --- a/src/gpg/CMakeLists.txt +++ b/src/gpg/CMakeLists.txt @@ -1,5 +1,4 @@ aux_source_directory(./result_analyse GPG_SOURCE) -aux_source_directory(./gpg_context GPG_SOURCE) aux_source_directory(./function GPG_SOURCE) aux_source_directory(./model GPG_SOURCE) aux_source_directory(. GPG_SOURCE) @@ -10,16 +9,23 @@ set(UTILS_DIR ${CMAKE_SOURCE_DIR}/utils) set(GPGME_LIB_DIR ${UTILS_DIR}/gpgme/lib) +if(ESAY_LOGGING_CPP) + message(STATUS "Link ESAY_LOGGING_CPP") + set(THIRD_PARTY_LIBS easy_logging_cpp) +endif() + +message(STATUS "Third Party Libraries " ${THIRD_PARTY_LIBS}) + if (MINGW) message(STATUS "Link GPG Static Library For MINGW") - target_link_libraries(gpg_core - gpgme gpg-error assuan Qt5::Core wsock32) + target_link_libraries(gpg_core ${THIRD_PARTY_LIBS} + gpgme gpg-error assuan wsock32) elseif(APPLE) message(STATUS "Link GPG Static Library For macOS") - target_link_libraries(gpg_core - libgpgme.a libgpg-error.a libassuan.a Qt5::Core) + target_link_libraries(gpg_core ${THIRD_PARTY_LIBS} + libgpgme.a libgpg-error.a libassuan.a) else() message(STATUS "Link GPG Static Library For Unix") - target_link_libraries(gpg_core - libgpgme.a libgpg-error.a libassuan.a Qt5::Core pthread) + target_link_libraries(gpg_core ${THIRD_PARTY_LIBS} + libgpgme.a libgpg-error.a libassuan.a pthread) endif() diff --git a/src/gpg/GpgConstants.cpp b/src/gpg/GpgConstants.cpp index bf189342..937033eb 100644 --- a/src/gpg/GpgConstants.cpp +++ b/src/gpg/GpgConstants.cpp @@ -42,9 +42,9 @@ const char *GpgFrontend::GpgConstants::GPG_FRONTEND_SHORT_CRYPTO_HEAD = gpgme_error_t GpgFrontend::check_gpg_error(gpgme_error_t err) { // if (gpgmeError != GPG_ERR_NO_ERROR && gpgmeError != GPG_ERR_CANCELED) { if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { - qDebug() << "[Error " << gpg_err_code(err) - << "] Source: " << gpgme_strsource(err) - << " Description: " << gpgme_strerror(err); + LOG(ERROR) << "[Error " << gpg_err_code(err) + << "] Source: " << gpgme_strsource(err) + << " Description: " << gpgme_strerror(err); } return err; } @@ -54,15 +54,16 @@ gpgme_error_t GpgFrontend::check_gpg_error(gpgme_error_t err, const std::string &comment) { // if (gpgmeError != GPG_ERR_NO_ERROR && gpgmeError != GPG_ERR_CANCELED) { if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { - qDebug() << "[Error " << gpg_err_code(err) - << "] Source: " << gpgme_strsource(err) - << " Description: " << gpgme_strerror(err) << " " - << comment.c_str(); + LOG(ERROR) << "[Error " << gpg_err_code(err) + << "] Source: " << gpgme_strsource(err) + << " Description: " << gpgme_strerror(err) << " " + << comment.c_str(); } return err; } -std::string GpgFrontend::beautify_fingerprint(std::string fingerprint) { +std::string +GpgFrontend::beautify_fingerprint(GpgFrontend::BypeArrayRef fingerprint) { uint len = fingerprint.size(); if ((len > 0) && (len % 4 == 0)) for (uint n = 0; 4 * (n + 1) < len; ++n) @@ -70,18 +71,40 @@ std::string GpgFrontend::beautify_fingerprint(std::string fingerprint) { return fingerprint; } +// trim from start (in place) +static inline void ltrim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); +} + +// trim from end (in place) +static inline void rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), + [](unsigned char ch) { return !std::isspace(ch); }) + .base(), + s.end()); +} + +// trim from both ends (in place) +static inline std::string trim(std::string &s) { + ltrim(s); + rtrim(s); + return s; +} + /* * isSigned returns: * - 0, if text isn't signed at all * - 1, if text is partially signed * - 2, if text is completly signed */ -int GpgFrontend::text_is_signed(const QByteArray &text) { - if (text.trimmed().startsWith(GpgConstants::PGP_SIGNED_BEGIN) && - text.trimmed().endsWith(GpgConstants::PGP_SIGNED_END)) +int GpgFrontend::text_is_signed(GpgFrontend::BypeArrayRef text) { + if (trim(text).starts_with(GpgConstants::PGP_SIGNED_BEGIN) && + trim(text).ends_with(GpgConstants::PGP_SIGNED_END)) return 2; - else if (text.contains(GpgConstants::PGP_SIGNED_BEGIN) && - text.contains(GpgConstants::PGP_SIGNED_END)) + else if (text.find(GpgConstants::PGP_SIGNED_BEGIN) != std::string::npos && + text.find(GpgConstants::PGP_SIGNED_END) != std::string::npos) return 1; else diff --git a/src/gpg/GpgConstants.h b/src/gpg/GpgConstants.h index c3ef9041..6237a974 100644 --- a/src/gpg/GpgConstants.h +++ b/src/gpg/GpgConstants.h @@ -27,45 +27,53 @@ #include "GpgFrontend.h" -#include <QtCore> #include <functional> +#include <assert.h> #include <gpgme.h> #include <memory> #include <string> +#include <easyloggingpp/easylogging++.h> + const int RESTART_CODE = 1000; namespace GpgFrontend { -using BypeArrayPtr = std::unique_ptr<QByteArray>; +using BypeArrayPtr = std::unique_ptr<std::string>; using StdBypeArrayPtr = std::unique_ptr<std::string>; -using BypeArrayRef = QByteArray &; +using BypeArrayRef = std::string &; +using StringArgsPtr = std::unique_ptr<std::vector<std::string>>; +using StringArgsRef = std::vector<std::string> &; using GpgError = gpgme_error_t; +// Result +struct __result_ref_deletor { + void operator()(void *_result) { + if (_result != nullptr) + gpgme_result_unref(_result); + } +}; + using GpgEncrResult = - std::unique_ptr<struct _gpgme_op_encrypt_result, - std::function<void(gpgme_encrypt_result_t)>>; + std::unique_ptr<struct _gpgme_op_encrypt_result, __result_ref_deletor>; using GpgDecrResult = - std::unique_ptr<struct _gpgme_op_decrypt_result, - std::function<void(gpgme_decrypt_result_t)>>; -using GpgSignResult = std::unique_ptr<struct _gpgme_op_sign_result, - std::function<void(gpgme_sign_result_t)>>; + std::unique_ptr<struct _gpgme_op_decrypt_result, __result_ref_deletor>; +using GpgSignResult = + std::unique_ptr<struct _gpgme_op_sign_result, __result_ref_deletor>; using GpgVerifyResult = - std::unique_ptr<struct _gpgme_op_verify_result, - std::function<void(gpgme_verify_result_t)>>; + std::unique_ptr<struct _gpgme_op_verify_result, __result_ref_deletor>; // Error Info Printer GpgError check_gpg_error(GpgError err); - GpgError check_gpg_error(GpgError gpgmeError, const std::string &comment); // Fingerprint -std::string beautify_fingerprint(std::string fingerprint); +std::string beautify_fingerprint(BypeArrayRef fingerprint); // Check -int text_is_signed(const QByteArray &text); +int text_is_signed(BypeArrayRef text); class GpgConstants { public: diff --git a/src/gpg/gpg_context/GpgContext.cpp b/src/gpg/GpgContext.cpp index a9b83d92..c956e126 100644 --- a/src/gpg/gpg_context/GpgContext.cpp +++ b/src/gpg/GpgContext.cpp @@ -23,14 +23,12 @@ */ #include "gpg/GpgContext.h" +#include "GpgConstants.h" #include <functional> -#include <unistd.h> /* contains read/write */ #ifdef _WIN32 - #include <windows.h> - #endif #define INT2VOIDP(i) (void *)(uintptr_t)(i) @@ -43,22 +41,36 @@ namespace GpgFrontend { */ GpgContext::GpgContext() { + static bool __first = true; + + if (__first) { + /* Initialize the locale environment. */ + setlocale(LC_ALL, ""); + gpgme_check_version(nullptr); + gpgme_set_locale(nullptr, LC_CTYPE, setlocale(LC_CTYPE, nullptr)); +#ifdef LC_MESSAGES + gpgme_set_locale(nullptr, LC_MESSAGES, setlocale(LC_MESSAGES, nullptr)); +#endif + __first = false; + } + gpgme_ctx_t _p_ctx; - auto err = gpgme_new(&_p_ctx); - check_gpg_error(err); - _ctx_ref = - CtxRefHandler(_p_ctx, [&](gpgme_ctx_t ctx) { gpgme_release(ctx); }); + check_gpg_error(gpgme_new(&_p_ctx)); + _ctx_ref = CtxRefHandler(_p_ctx); + + LOG(INFO) << "GpgContext _ctx_ref Created"; + + auto engineInfo = gpgme_ctx_get_engine_info(*this); - gpgme_engine_info_t engineInfo; - engineInfo = gpgme_ctx_get_engine_info(*this); + LOG(INFO) << "GpgContext gpgme_ctx_get_engine_info Called"; // Check ENV before running bool check_pass = false, find_openpgp = false, find_gpgconf = false, find_assuan = false, find_cms = false; while (engineInfo != nullptr) { - qDebug() << gpgme_get_protocol_name(engineInfo->protocol) - << engineInfo->file_name << engineInfo->protocol - << engineInfo->home_dir << engineInfo->version; + LOG(INFO) << gpgme_get_protocol_name(engineInfo->protocol) << " " + << engineInfo->file_name << " " << engineInfo->version; + if (engineInfo->protocol == GPGME_PROTOCOL_GPGCONF && strcmp(engineInfo->version, "1.0.0") != 0) find_gpgconf = true; @@ -76,32 +88,29 @@ GpgContext::GpgContext() { if (find_gpgconf && find_openpgp && find_cms && find_assuan) check_pass = true; + LOG(INFO) << "GpgContext check_pass " << check_pass; if (!check_pass) { good_ = false; return; - } else + } else { + /** Setting the output type must be done at the beginning */ + /** think this means ascii-armor --> ? */ + gpgme_set_armor(*this, 1); + // Speed up loading process + gpgme_set_offline(*this, 1); + + check_gpg_error(gpgme_set_keylist_mode( + *this, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_WITH_SECRET | + GPGME_KEYLIST_MODE_SIGS | GPGME_KEYLIST_MODE_SIG_NOTATIONS | + GPGME_KEYLIST_MODE_WITH_TOFU)); good_ = true; - - /** Setting the output type must be done at the beginning */ - /** think this means ascii-armor --> ? */ - gpgme_set_armor(*this, 1); - // Speed up loading process - gpgme_set_offline(*this, 1); - - gpgme_set_keylist_mode( - *this, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_WITH_SECRET | - GPGME_KEYLIST_MODE_SIGS | GPGME_KEYLIST_MODE_SIG_NOTATIONS | - GPGME_KEYLIST_MODE_WITH_TOFU); + } } bool GpgContext::good() const { return good_; } -/** also from kgpgme.cpp, seems to clear password from mem */ -void GpgContext::clearPasswordCache() { - if (mPasswordCache.size() > 0) { - mPasswordCache.fill('\0'); - mPasswordCache.truncate(0); - } +void GpgContext::SetPassphraseCb(decltype(test_passphrase_cb) cb) { + gpgme_set_passphrase_cb(*this, cb, nullptr); } /** return type should be gpgme_error_t*/ @@ -111,15 +120,13 @@ void GpgContext::clearPasswordCache() { * GPGME doesn't recognise the Message as encrypted. This function adds '\n' * before the PGP-Begin-Block, if missing. */ -void GpgContext::preventNoDataErr(QByteArray *in) { - int block_start = in->indexOf(GpgConstants::PGP_CRYPT_BEGIN); - if (block_start > 0 && in->at(block_start - 1) != '\n') { - in->insert(block_start, '\n'); - } - block_start = in->indexOf(GpgConstants::PGP_SIGNED_BEGIN); - if (block_start > 0 && in->at(block_start - 1) != '\n') { - in->insert(block_start, '\n'); - } +void GpgContext::preventNoDataErr(BypeArrayPtr in) { + int block_start = in->find(GpgConstants::PGP_CRYPT_BEGIN); + if (block_start != std::string::npos && in->at(block_start - 1) != '\n') + in->insert(block_start, "\n"); + block_start = in->find(GpgConstants::PGP_SIGNED_BEGIN); + if (block_start != std::string::npos && in->at(block_start - 1) != '\n') + in->insert(block_start, "\n"); } std::string GpgContext::getGpgmeVersion() { diff --git a/src/gpg/GpgContext.h b/src/gpg/GpgContext.h index fc0b80e0..e786c2b3 100644 --- a/src/gpg/GpgContext.h +++ b/src/gpg/GpgContext.h @@ -39,12 +39,10 @@ class GpgContext : public SingletonFunctionObject<GpgContext> { public: GpgContext(); - ~GpgContext() override = default; + ~GpgContext() = default; [[nodiscard]] bool good() const; - bool exportKeys(const QVector<GpgKey> &keys, QByteArray &outBuffer); - const GpgInfo &GetInfo() const { return info; } void clearPasswordCache(); @@ -55,7 +53,7 @@ public: * * @param in Pointer to the QBytearray to check. */ - static void preventNoDataErr(QByteArray *in); + static void preventNoDataErr(BypeArrayPtr in); static std::string getGpgmeVersion(); @@ -64,20 +62,48 @@ public: private: GpgInfo info; + struct __ctx_ref_deletor { + void operator()(gpgme_ctx_t _ctx) { + if (_ctx != nullptr) + gpgme_release(_ctx); + } + }; + using CtxRefHandler = - std::unique_ptr<struct gpgme_context, std::function<void(gpgme_ctx_t)>>; + std::unique_ptr<struct gpgme_context, __ctx_ref_deletor>; CtxRefHandler _ctx_ref = nullptr; bool good_ = true; - QByteArray mPasswordCache; - - static gpgme_error_t passphraseCb(void *hook, const char *uid_hint, - const char *passphrase_info, - int last_was_bad, int fd); - gpgme_error_t passphrase(const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd); + +public: + static gpgme_error_t test_passphrase_cb(void *opaque, const char *uid_hint, + const char *passphrase_info, + int last_was_bad, int fd) { + + LOG(INFO) << "test_passphrase_cb Called"; + int res; + char pass[] = "abcdefg\n"; + int passlen = strlen(pass); + int off = 0; + + (void)opaque; + (void)uid_hint; + (void)passphrase_info; + (void)last_was_bad; + + do { + res = gpgme_io_write(fd, &pass[off], passlen - off); + if (res > 0) + off += res; + } while (res > 0 && off != passlen); + + return off == passlen ? 0 : gpgme_error_from_errno(errno); + } + + void SetPassphraseCb(decltype(test_passphrase_cb) func); }; } // namespace GpgFrontend diff --git a/src/gpg/GpgFunctionObject.h b/src/gpg/GpgFunctionObject.h index 0f772c92..654f81d3 100644 --- a/src/gpg/GpgFunctionObject.h +++ b/src/gpg/GpgFunctionObject.h @@ -28,10 +28,14 @@ #include <memory> #include <mutex> +#include <easyloggingpp/easylogging++.h> + namespace GpgFrontend { template <typename T> class SingletonFunctionObject { public: static T &GetInstance() { + LOG(INFO) << "SingletonFunctionObject GetInstance Calling " + << typeid(T).name(); std::lock_guard<std::mutex> guard(_instance_mutex); if (_instance == nullptr) _instance = std::make_unique<T>(); diff --git a/src/gpg/GpgGenKeyInfo.h b/src/gpg/GpgGenKeyInfo.h index 7b830dd3..d2ec8141 100644 --- a/src/gpg/GpgGenKeyInfo.h +++ b/src/gpg/GpgGenKeyInfo.h @@ -25,10 +25,11 @@ #ifndef GPG4USB_GPGGENKEYINFO_H #define GPG4USB_GPGGENKEYINFO_H +#include <boost/date_time/gregorian/greg_duration_types.hpp> #include <string> #include <vector> -#include <QtCore> +#include <boost/date_time.hpp> namespace GpgFrontend { @@ -38,7 +39,8 @@ class GenKeyInfo { std::string userid; std::string algo; int keySize = 2048; - QDateTime expired = QDateTime::currentDateTime().addYears(2); + boost::gregorian::date expired = + boost::gregorian::day_clock::local_day() + boost::gregorian::years(2); bool nonExpired = false; bool noPassPhrase = false; @@ -73,9 +75,11 @@ public: void setKeySize(int m_key_size); - [[nodiscard]] const QDateTime &getExpired() const { return expired; } + [[nodiscard]] const boost::gregorian::date &getExpired() const { + return expired; + } - void setExpired(const QDateTime &m_expired); + void setExpired(const boost::gregorian::date &m_expired); [[nodiscard]] bool isNonExpired() const { return nonExpired; } diff --git a/src/gpg/GpgModel.h b/src/gpg/GpgModel.h index 88241c23..f9851f1e 100644 --- a/src/gpg/GpgModel.h +++ b/src/gpg/GpgModel.h @@ -28,6 +28,8 @@ #include "gpg/model/GpgData.h" #include "gpg/model/GpgKey.h" +#include <list> + namespace GpgFrontend { using KeyIdArgsListPtr = std::unique_ptr<std::vector<std::string>>; diff --git a/src/gpg/function/BasicOperator.cpp b/src/gpg/function/BasicOperator.cpp index c76f581a..295b6433 100644 --- a/src/gpg/function/BasicOperator.cpp +++ b/src/gpg/function/BasicOperator.cpp @@ -47,9 +47,7 @@ GpgFrontend::GpgError GpgFrontend::BasicOperator::Encrypt( auto temp_data_out = data_out.Read2Buffer(); std::swap(temp_data_out, out_buffer); - auto temp_result = GpgEncrResult( - gpgme_op_encrypt_result(ctx), - [&](gpgme_encrypt_result_t res) { gpgme_result_unref(res); }); + auto temp_result = GpgEncrResult(gpgme_op_encrypt_result(ctx)); std::swap(result, temp_result); return err; @@ -68,16 +66,14 @@ GpgFrontend::BasicOperator::Decrypt(BypeArrayRef in_buffer, auto temp_data_out = data_out.Read2Buffer(); std::swap(temp_data_out, out_buffer); - auto temp_result = GpgDecrResult( - gpgme_op_decrypt_result(ctx), - [&](gpgme_decrypt_result_t res) { gpgme_result_unref(res); }); + auto temp_result = GpgDecrResult(gpgme_op_decrypt_result(ctx)); std::swap(result, temp_result); return err; } GpgFrontend::GpgError -GpgFrontend::BasicOperator::Verify(QByteArray &in_buffer, +GpgFrontend::BasicOperator::Verify(BypeArrayRef &in_buffer, BypeArrayPtr &sig_buffer, GpgVerifyResult &result) const { gpgme_error_t err; @@ -91,9 +87,7 @@ GpgFrontend::BasicOperator::Verify(QByteArray &in_buffer, } else err = check_gpg_error(gpgme_op_verify(ctx, data_in, nullptr, data_in)); - auto temp_result = GpgVerifyResult( - gpgme_op_verify_result(ctx), - [&](gpgme_verify_result_t res) { gpgme_result_unref(res); }); + auto temp_result = GpgVerifyResult(gpgme_op_verify_result(ctx)); std::swap(result, temp_result); return err; @@ -129,9 +123,7 @@ GpgFrontend::GpgError GpgFrontend::BasicOperator::Sign(KeyArgsList &keys, auto temp_data_out = data_out.Read2Buffer(); std::swap(temp_data_out, out_buffer); - auto temp_result = - GpgSignResult(gpgme_op_sign_result(ctx), - [&](gpgme_sign_result_t res) { gpgme_result_unref(res); }); + auto temp_result = GpgSignResult(gpgme_op_sign_result(ctx)); std::swap(result, temp_result); @@ -150,14 +142,10 @@ gpgme_error_t GpgFrontend::BasicOperator::DecryptVerify( auto temp_data_out = data_out.Read2Buffer(); std::swap(temp_data_out, out_buffer); - auto temp_decr_result = GpgDecrResult( - gpgme_op_decrypt_result(ctx), - [&](gpgme_decrypt_result_t res) { gpgme_result_unref(res); }); + auto temp_decr_result = GpgDecrResult(gpgme_op_decrypt_result(ctx)); std::swap(decrypt_result, temp_decr_result); - auto temp_verify_result = GpgVerifyResult( - gpgme_op_verify_result(ctx), - [&](gpgme_verify_result_t res) { gpgme_result_unref(res); }); + auto temp_verify_result = GpgVerifyResult(gpgme_op_verify_result(ctx)); std::swap(verify_result, temp_verify_result); return err; @@ -190,13 +178,9 @@ gpgme_error_t GpgFrontend::BasicOperator::EncryptSign( auto temp_data_out = data_out.Read2Buffer(); std::swap(temp_data_out, out_buffer); - auto temp_encr_result = GpgEncrResult( - gpgme_op_encrypt_result(ctx), - [&](gpgme_encrypt_result_t res) { gpgme_result_unref(res); }); + auto temp_encr_result = GpgEncrResult(gpgme_op_encrypt_result(ctx)); swap(encr_result, temp_encr_result); - auto temp_sign_result = - GpgSignResult(gpgme_op_sign_result(ctx), - [&](gpgme_sign_result_t res) { gpgme_result_unref(res); }); + auto temp_sign_result = GpgSignResult(gpgme_op_sign_result(ctx)); swap(sign_result, temp_sign_result); return err; @@ -211,7 +195,7 @@ void GpgFrontend::BasicOperator::SetSigners(KeyArgsList &keys) { } } if (keys.size() != gpgme_signers_count(ctx)) - qDebug() << "No All Signers Added"; + LOG(INFO) << "No All Signers Added"; } std::unique_ptr<std::vector<GpgFrontend::GpgKey>> diff --git a/src/gpg/function/GpgCommandExecutor.cpp b/src/gpg/function/GpgCommandExecutor.cpp index c36cb2b6..dc4750ef 100644 --- a/src/gpg/function/GpgCommandExecutor.cpp +++ b/src/gpg/function/GpgCommandExecutor.cpp @@ -23,27 +23,33 @@ */ #include "gpg/function/GpgCommandExecutor.h" +#include <boost/asio.hpp> + +using boost::process::async_pipe; + void GpgFrontend::GpgCommandExecutor::Execute( - const QStringList &arguments, - const std::function<void(QProcess *)> &interact_func) { - QEventLoop looper; - auto *gpg_process = new QProcess(&looper); - gpg_process->setProcessChannelMode(QProcess::MergedChannels); - connect(gpg_process, - qOverload<int, QProcess::ExitStatus>(&QProcess::finished), &looper, - &QEventLoop::quit); - connect(gpg_process, &QProcess::errorOccurred, - []() -> void { qDebug("Error in Process"); }); - connect(gpg_process, &QProcess::errorOccurred, &looper, &QEventLoop::quit); - connect(gpg_process, &QProcess::started, - []() -> void { qDebug() << "Gpg Process Started Success"; }); - connect(gpg_process, &QProcess::readyReadStandardOutput, - [interact_func, gpg_process]() { - qDebug() << "Function Called"; - interact_func(gpg_process); - }); - gpg_process->setProgram(ctx.GetInfo().appPath.c_str()); - gpg_process->setArguments(arguments); - gpg_process->start(); - looper.exec(); + StringArgsRef arguments, + const std::function<void(async_pipe &in, async_pipe &out)> &interact_func) { + + using namespace boost::process; + + boost::asio::io_service ios; + + std::vector<char> buf; + + async_pipe in_pipe_stream(ios); + async_pipe out_pipe_stream(ios); + + child child_process(ctx.GetInfo().appPath.c_str(), arguments, + std_out > in_pipe_stream, std_in < out_pipe_stream); + + boost::asio::async_read( + in_pipe_stream, boost::asio::buffer(buf), + [&](const boost::system::error_code &ec, std::size_t size) { + interact_func(in_pipe_stream, out_pipe_stream); + }); + + ios.run(); + child_process.wait(); + int result = child_process.exit_code(); } diff --git a/src/gpg/function/GpgCommandExecutor.h b/src/gpg/function/GpgCommandExecutor.h index d347a601..01904fd3 100644 --- a/src/gpg/function/GpgCommandExecutor.h +++ b/src/gpg/function/GpgCommandExecutor.h @@ -28,14 +28,15 @@ #include "gpg/GpgContext.h" #include "gpg/GpgFunctionObject.h" -namespace GpgFrontend { +#include <boost/process.hpp> -class GpgCommandExecutor : public QObject, - public SingletonFunctionObject<GpgCommandExecutor> { - Q_OBJECT +namespace GpgFrontend { +class GpgCommandExecutor : public SingletonFunctionObject<GpgCommandExecutor> { public: - void Execute(const QStringList &arguments, - const std::function<void(QProcess *)> &interact_func); + void Execute(StringArgsRef arguments, + const std::function<void(boost::process::async_pipe &in, + boost::process::async_pipe &out)> + &interact_func); private: GpgContext &ctx = GpgContext::GetInstance(); diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 50e4e932..726c2695 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -23,14 +23,17 @@ */ #include "gpg/function/GpgFileOpera.h" #include "gpg/function/BasicOperator.h" + +#include <filesystem> #include <memory> GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { - QFileInfo file_info(path.c_str()); + using namespace std::filesystem; + class path file_info(path.c_str()); - if (!file_info.isFile() || !file_info.isReadable()) + if (!exists(file_info) || !is_regular_file(path)) throw std::runtime_error("no permission"); QFile in_file(path.c_str()); @@ -59,7 +62,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( } GpgFrontend::GpgError -GpgFrontend::GpgFileOpera::DecryptFile(const QString &path, +GpgFrontend::GpgFileOpera::DecryptFile(const std::string &path, GpgDecrResult &result) { QFileInfo file_info(path); diff --git a/src/gpg/function/GpgFileOpera.h b/src/gpg/function/GpgFileOpera.h index 1642a8eb..5cdd69bf 100644 --- a/src/gpg/function/GpgFileOpera.h +++ b/src/gpg/function/GpgFileOpera.h @@ -36,7 +36,7 @@ public: GpgError EncryptFile(KeyArgsList &keys, const std::string &path, GpgEncrResult &result); - GpgError DecryptFile(const QString &path, GpgDecrResult &result); + GpgError DecryptFile(const std::string &path, GpgDecrResult &result); GpgError SignFile(KeyArgsList &keys, const std::string &path, GpgSignResult &result); diff --git a/src/gpg/function/GpgKeyGetter.cpp b/src/gpg/function/GpgKeyGetter.cpp index 4bea83f3..f8e361bd 100644 --- a/src/gpg/function/GpgKeyGetter.cpp +++ b/src/gpg/function/GpgKeyGetter.cpp @@ -24,14 +24,16 @@ #include "gpg/function/GpgKeyGetter.h" -GpgFrontend::GpgKey && -GpgFrontend::GpgKeyGetter::GetKey(const std::string &fpr) { +GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetKey(const std::string &fpr) { + LOG(INFO) << "GpgKeyGetter GetKey Fpr " << fpr; gpgme_key_t _p_key; gpgme_get_key(ctx, fpr.c_str(), &_p_key, 1); + if (_p_key == nullptr) + LOG(WARNING) << "GpgKeyGetter GetKey _p_key Null"; return std::move(GpgKey(std::move(_p_key))); } -GpgFrontend::GpgKey && +GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetPubkey(const std::string &fpr) { gpgme_key_t _p_key; gpgme_get_key(ctx, fpr.c_str(), &_p_key, 0); @@ -42,28 +44,28 @@ GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::FetchKey() { gpgme_error_t err; - qDebug() << "Clear List and Map"; + LOG(INFO) << "Clear List and Map"; KeyListPtr keys_list = std::make_unique<std::vector<GpgKey>>(); - qDebug() << "Operate KeyList Start"; + LOG(INFO) << "Operate KeyList Start"; err = gpgme_op_keylist_start(ctx, nullptr, 0); assert(gpg_err_code(err) != GPG_ERR_NO_ERROR); - qDebug() << "Start Loop"; + LOG(INFO) << "Start Loop"; gpgme_key_t key; while ((err = gpgme_op_keylist_next(ctx, &key)) == GPG_ERR_NO_ERROR) { keys_list->push_back(GpgKey(std::move(key))); - qDebug() << "Append Key" << keys_list->back().id().c_str(); + LOG(INFO) << "Append Key" << keys_list->back().id().c_str(); } assert(gpg_err_code(err) != GPG_ERR_NO_ERROR); err = gpgme_op_keylist_end(ctx); - qDebug() << "Operate KeyList End"; + LOG(INFO) << "Operate KeyList End"; return keys_list; } diff --git a/src/gpg/function/GpgKeyGetter.h b/src/gpg/function/GpgKeyGetter.h index a848b2f8..3a291d2d 100644 --- a/src/gpg/function/GpgKeyGetter.h +++ b/src/gpg/function/GpgKeyGetter.h @@ -34,9 +34,9 @@ namespace GpgFrontend { class GpgKeyGetter : public SingletonFunctionObject<GpgKeyGetter> { public: - GpgKey &&GetKey(const std::string &fpr); + GpgKey GetKey(const std::string &fpr); - GpgKey &&GetPubkey(const std::string &fpr); + GpgKey GetPubkey(const std::string &fpr); KeyListPtr FetchKey(); diff --git a/src/gpg/function/GpgKeyImportExportor.cpp b/src/gpg/function/GpgKeyImportExportor.cpp index 8c293617..37b12650 100644 --- a/src/gpg/function/GpgKeyImportExportor.cpp +++ b/src/gpg/function/GpgKeyImportExportor.cpp @@ -1,4 +1,5 @@ #include "gpg/function/GpgKeyImportExportor.h" +#include "GpgConstants.h" /** * Import key pair @@ -7,54 +8,26 @@ */ GpgFrontend::GpgImportInformation GpgFrontend::GpgKeyImportExportor::ImportKey(StdBypeArrayPtr in_buffer) { - auto import_information = std::make_unique<GpgImportInformation>(); + LOG(INFO) << "ImportKey Called in_buffer Size " << in_buffer->size(); GpgData data_in(in_buffer->data(), in_buffer->size()); - auto err = gpgme_op_import(ctx, data_in); + auto err = check_gpg_error(gpgme_op_import(ctx, data_in)); assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); gpgme_import_result_t result; result = gpgme_op_import_result(ctx); - - if (result->unchanged) - import_information->unchanged = result->unchanged; - if (result->considered) - import_information->considered = result->considered; - if (result->no_user_id) - import_information->no_user_id = result->no_user_id; - if (result->imported) - import_information->imported = result->imported; - if (result->imported_rsa) - import_information->imported_rsa = result->imported_rsa; - if (result->unchanged) - import_information->unchanged = result->unchanged; - if (result->new_user_ids) - import_information->new_user_ids = result->new_user_ids; - if (result->new_sub_keys) - import_information->new_sub_keys = result->new_sub_keys; - if (result->new_signatures) - import_information->new_signatures = result->new_signatures; - if (result->new_revocations) - import_information->new_revocations = result->new_revocations; - if (result->secret_read) - import_information->secret_read = result->secret_read; - if (result->secret_imported) - import_information->secret_imported = result->secret_imported; - if (result->secret_unchanged) - import_information->secret_unchanged = result->secret_unchanged; - if (result->not_imported) - import_information->not_imported = result->not_imported; - - gpgme_result_unref(result); - gpgme_import_status_t status = result->imports; + auto import_info = std::make_unique<GpgImportInformation>(result); + LOG(INFO) << "ImportKey import_information " << result->not_imported << " " + << result->imported << " " << result->considered; while (status != nullptr) { GpgImportedKey key; - key.importStatus = static_cast<int>(status->status); + key.import_status = static_cast<int>(status->status); key.fpr = status->fpr; - import_information->importedKeys.emplace_back(key); + import_info->importedKeys.emplace_back(key); status = status->next; + LOG(INFO) << "ImportKey Fpr " << key.fpr << " Status " << key.import_status; } - return *import_information; + return *import_info; } /** @@ -75,8 +48,8 @@ bool GpgFrontend::GpgKeyImportExportor::ExportKeys( auto err = gpgme_op_export(ctx, (*uid_list)[i].c_str(), 0, data_out); assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); - qDebug() << "exportKeys read_bytes" - << gpgme_data_seek(data_out, 0, SEEK_END); + LOG(INFO) << "exportKeys read_bytes" + << gpgme_data_seek(data_out, 0, SEEK_END); auto temp_out_buffer = std::move(data_out.Read2Buffer()); std::swap(out_buffer, temp_out_buffer); @@ -108,7 +81,7 @@ bool GpgFrontend::GpgKeyImportExportor::ExportKeys( bool GpgFrontend::GpgKeyImportExportor::ExportSecretKey( const GpgKey &key, BypeArrayPtr out_buffer) const { - qDebug() << "Export Secret Key" << key.id().c_str(); + LOG(INFO) << "Export Secret Key" << key.id().c_str(); gpgme_key_t target_key[2] = {gpgme_key_t(key), nullptr}; diff --git a/src/gpg/function/GpgKeyImportExportor.h b/src/gpg/function/GpgKeyImportExportor.h index 4eaba19b..455e59f8 100644 --- a/src/gpg/function/GpgKeyImportExportor.h +++ b/src/gpg/function/GpgKeyImportExportor.h @@ -35,7 +35,7 @@ namespace GpgFrontend { class GpgImportedKey { public: std::string fpr; - int importStatus; + int import_status; }; typedef std::list<GpgImportedKey> GpgImportedKeyList; @@ -44,6 +44,37 @@ class GpgImportInformation { public: GpgImportInformation() = default; + explicit GpgImportInformation(gpgme_import_result_t result) { + if (result->unchanged) + unchanged = result->unchanged; + if (result->considered) + considered = result->considered; + if (result->no_user_id) + no_user_id = result->no_user_id; + if (result->imported) + imported = result->imported; + if (result->imported_rsa) + imported_rsa = result->imported_rsa; + if (result->unchanged) + unchanged = result->unchanged; + if (result->new_user_ids) + new_user_ids = result->new_user_ids; + if (result->new_sub_keys) + new_sub_keys = result->new_sub_keys; + if (result->new_signatures) + new_signatures = result->new_signatures; + if (result->new_revocations) + new_revocations = result->new_revocations; + if (result->secret_read) + secret_read = result->secret_read; + if (result->secret_imported) + secret_imported = result->secret_imported; + if (result->secret_unchanged) + secret_unchanged = result->secret_unchanged; + if (result->not_imported) + not_imported = result->not_imported; + } + int considered = 0; int no_user_id = 0; int imported = 0; diff --git a/src/gpg/function/GpgKeyOpera.cpp b/src/gpg/function/GpgKeyOpera.cpp index 6eed26f1..f79fb2c3 100644 --- a/src/gpg/function/GpgKeyOpera.cpp +++ b/src/gpg/function/GpgKeyOpera.cpp @@ -26,6 +26,15 @@ #include "gpg/GpgConstants.h" #include "gpg/GpgGenKeyInfo.h" #include "gpg/function/GpgCommandExecutor.h" +#include "gpg/function/GpgKeyGetter.h" + +#include <boost/asio/read_until.hpp> +#include <boost/date_time/posix_time/conversion.hpp> + +#include <boost/process/async_pipe.hpp> +#include <memory> +#include <string> +#include <vector> /** * Delete keys @@ -33,23 +42,15 @@ */ void GpgFrontend::GpgKeyOpera::DeleteKeys( GpgFrontend::KeyIdArgsListPtr uid_list) { - GpgError err; - gpgme_key_t key; - for (const auto &tmp : *uid_list) { - - err = gpgme_op_keylist_start(ctx, tmp.c_str(), 0); - assert(gpg_err_code(err) != GPG_ERR_NO_ERROR); - - err = gpgme_op_keylist_next(ctx, &key); - assert(gpg_err_code(err) != GPG_ERR_NO_ERROR); - - err = gpgme_op_keylist_end(ctx); - assert(gpg_err_code(err) != GPG_ERR_NO_ERROR); - - err = gpgme_op_delete(ctx, key, 1); - assert(gpg_err_code(err) != GPG_ERR_NO_ERROR); + auto key = GpgKeyGetter::GetInstance().GetKey(tmp); + if (key.good()) { + LOG(INFO) << "GpgKeyOpera DeleteKeys Get Key Good"; + err = check_gpg_error(gpgme_op_delete(ctx, gpgme_key_t(key), 1)); + assert(gpg_err_code(err) == GPG_ERR_NO_ERROR); + } else + LOG(WARNING) << "GpgKeyOpera DeleteKeys Get Key Bad"; } } @@ -60,13 +61,13 @@ void GpgFrontend::GpgKeyOpera::DeleteKeys( * @param expires date and time * @return if successful */ -void GpgFrontend::GpgKeyOpera::SetExpire(const GpgKey &key, - std::unique_ptr<GpgSubKey> &subkey, - std::unique_ptr<QDateTime> &expires) { +void GpgFrontend::GpgKeyOpera::SetExpire( + const GpgKey &key, std::unique_ptr<GpgSubKey> &subkey, + std::unique_ptr<boost::gregorian::date> &expires) { unsigned long expires_time = 0; if (expires != nullptr) { - qDebug() << "Expire Datetime" << expires->toString(); - expires_time = QDateTime::currentDateTime().secsTo(*expires); + using namespace boost::posix_time; + expires_time = to_time_t(ptime(*expires)); } const char *sub_fprs = nullptr; @@ -88,34 +89,20 @@ void GpgFrontend::GpgKeyOpera::SetExpire(const GpgKey &key, */ void GpgFrontend::GpgKeyOpera::GenerateRevokeCert( const GpgKey &key, const std::string &output_file_name) { + auto args = std::vector<std::string>{"--command-fd", "0", + "--status-fd", "1", + "-o", output_file_name.c_str(), + "--gen-revoke", key.fpr().c_str()}; + + using boost::process::async_pipe; GpgCommandExecutor::GetInstance().Execute( - {"--command-fd", "0", "--status-fd", "1", "-o", output_file_name.c_str(), - "--gen-revoke", key.fpr().c_str()}, - [](QProcess *proc) -> void { - qDebug() << "Function Called" << proc; - // Code From Gpg4Win - while (proc->canReadLine()) { - const QString line = QString::fromUtf8(proc->readLine()).trimmed(); - if (line == QLatin1String("[GNUPG:] GET_BOOL gen_revoke.okay")) { - proc->write("y\n"); - } else if (line == - QLatin1String( - "[GNUPG:] GET_LINE ask_revocation_reason.code")) { - proc->write("0\n"); - } else if (line == - QLatin1String( - "[GNUPG:] GET_LINE ask_revocation_reason.text")) { - proc->write("\n"); - } else if (line == QLatin1String( - "[GNUPG:] GET_BOOL openfile.overwrite.okay")) { - // We asked before - proc->write("y\n"); - } else if (line == - QLatin1String( - "[GNUPG:] GET_BOOL ask_revocation_reason.okay")) { - proc->write("y\n"); - } - } + args, [](async_pipe &in, async_pipe &out) -> void { + boost::asio::streambuf buff; + boost::asio::read_until(in, buff, '\n'); + + std::string line; + std::istream is(&buff); + is >> line; // Code From Gpg4Win }); } @@ -132,8 +119,12 @@ GpgFrontend::GpgKeyOpera::GenerateKey(std::unique_ptr<GenKeyInfo> params) { const char *userid = userid_utf8.c_str(); auto algo_utf8 = (params->getAlgo() + params->getKeySizeStr()); const char *algo = algo_utf8.c_str(); - unsigned long expires = - QDateTime::currentDateTime().secsTo(params->getExpired()); + unsigned long expires = 0; + { + using namespace boost::posix_time; + expires = to_time_t(ptime(params->getExpired())); + } + unsigned int flags = 0; if (!params->isSubKey()) @@ -168,8 +159,11 @@ GpgFrontend::GpgKeyOpera::GenerateSubkey(const GpgKey &key, auto algo_utf8 = (params->getAlgo() + params->getKeySizeStr()); const char *algo = algo_utf8.c_str(); - unsigned long expires = - QDateTime::currentDateTime().secsTo(params->getExpired()); + unsigned long expires = 0; + { + using namespace boost::posix_time; + expires = to_time_t(ptime(params->getExpired())); + } unsigned int flags = 0; if (!params->isSubKey()) diff --git a/src/gpg/function/GpgKeyOpera.h b/src/gpg/function/GpgKeyOpera.h index 06ca26bd..a635084d 100644 --- a/src/gpg/function/GpgKeyOpera.h +++ b/src/gpg/function/GpgKeyOpera.h @@ -36,7 +36,7 @@ public: void DeleteKeys(KeyIdArgsListPtr uid_list); void SetExpire(const GpgKey &key, std::unique_ptr<GpgSubKey> &subkey, - std::unique_ptr<QDateTime> &expires); + std::unique_ptr<boost::gregorian::date> &expires); void GenerateRevokeCert(const GpgKey &key, const std::string &output_file_name); diff --git a/src/gpg/model/GpgData.cpp b/src/gpg/model/GpgData.cpp index 52e9cf28..30b03069 100644 --- a/src/gpg/model/GpgData.cpp +++ b/src/gpg/model/GpgData.cpp @@ -30,8 +30,8 @@ GpgFrontend::GpgData::GpgData() { gpgme_error_t err = gpgme_data_new(&data); assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); - data_ = std::unique_ptr<struct gpgme_data, std::function<void(gpgme_data_t)>>( - std::move(data), [](gpgme_data_t t) { gpgme_data_release(t); }); + data_ = + std::unique_ptr<struct gpgme_data, __data_ref_deletor>(std::move(data)); } GpgFrontend::GpgData::GpgData(void *buffer, size_t size, bool copy) { @@ -41,8 +41,8 @@ GpgFrontend::GpgData::GpgData(void *buffer, size_t size, bool copy) { gpgme_data_new_from_mem(&data, (const char *)buffer, size, copy); assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); - data_ = std::unique_ptr<struct gpgme_data, std::function<void(gpgme_data_t)>>( - std::move(data), [](gpgme_data_t t) { gpgme_data_release(t); }); + data_ = + std::unique_ptr<struct gpgme_data, __data_ref_deletor>(std::move(data)); } /** @@ -55,7 +55,7 @@ GpgFrontend::BypeArrayPtr GpgFrontend::GpgData::Read2Buffer() { gpgme_off_t ret = gpgme_data_seek(*this, 0, SEEK_SET); gpgme_error_t err = gpg_error(GPG_ERR_NO_ERROR); - BypeArrayPtr out_buffer = std::make_unique<QByteArray>(); + BypeArrayPtr out_buffer = std::make_unique<std::string>(); if (ret) { err = gpgme_err_code_from_errno(errno); diff --git a/src/gpg/model/GpgData.h b/src/gpg/model/GpgData.h index cb0c4ffd..0c4615ad 100644 --- a/src/gpg/model/GpgData.h +++ b/src/gpg/model/GpgData.h @@ -40,7 +40,14 @@ public: BypeArrayPtr Read2Buffer(); private: - std::unique_ptr<struct gpgme_data, std::function<void(gpgme_data_t)>> data_; + struct __data_ref_deletor { + void operator()(gpgme_data_t _data) { + if (_data != nullptr) + gpgme_data_release(_data); + } + }; + + std::unique_ptr<struct gpgme_data, __data_ref_deletor> data_ = nullptr; }; } // namespace GpgFrontend diff --git a/src/gpg/model/GpgKey.cpp b/src/gpg/model/GpgKey.cpp index 800c1c23..fa52c6f2 100644 --- a/src/gpg/model/GpgKey.cpp +++ b/src/gpg/model/GpgKey.cpp @@ -24,9 +24,7 @@ #include "gpg/model/GpgKey.h" -GpgFrontend::GpgKey::GpgKey(gpgme_key_t &&key) - : _key_ref(std::move(key), - [&](gpgme_key_t key) { gpgme_key_release(key); }) {} +GpgFrontend::GpgKey::GpgKey(gpgme_key_t &&key) : _key_ref(std::move(key)) {} GpgFrontend::GpgKey::GpgKey(GpgKey &&k) noexcept { swap(_key_ref, k._key_ref); } diff --git a/src/gpg/model/GpgKey.h b/src/gpg/model/GpgKey.h index f34fece1..c0671b35 100644 --- a/src/gpg/model/GpgKey.h +++ b/src/gpg/model/GpgKey.h @@ -28,11 +28,14 @@ #include "GpgSubKey.h" #include "GpgUID.h" +#include <boost/date_time.hpp> +#include <boost/date_time/posix_time/conversion.hpp> + namespace GpgFrontend { class GpgKey { public: - [[nodiscard]] bool good() const { return _key_ref == nullptr; } + [[nodiscard]] bool good() const { return _key_ref != nullptr; } [[nodiscard]] std::string id() const { return _key_ref->subkeys->keyid; } @@ -69,16 +72,16 @@ public: return gpgme_pubkey_algo_name(_key_ref->subkeys->pubkey_algo); } - [[nodiscard]] QDateTime last_update() const { - return QDateTime::fromTime_t(_key_ref->last_update); + [[nodiscard]] boost::gregorian::date last_update() const { + return boost::posix_time::from_time_t(_key_ref->last_update).date(); } - [[nodiscard]] QDateTime expires() const { - return QDateTime::fromTime_t(_key_ref->subkeys->expires); + [[nodiscard]] boost::gregorian::date expires() const { + return boost::posix_time::from_time_t(_key_ref->subkeys->expires).date(); }; - [[nodiscard]] QDateTime create_time() const { - return QDateTime::fromTime_t(_key_ref->subkeys->timestamp); + [[nodiscard]] boost::gregorian::date create_time() const { + return boost::posix_time::from_time_t(_key_ref->subkeys->timestamp).date(); }; [[nodiscard]] unsigned int length() const { @@ -119,10 +122,12 @@ public: [[nodiscard]] std::unique_ptr<std::vector<GpgUID>> uids() const; - explicit GpgKey() = default; + GpgKey() = default; explicit GpgKey(gpgme_key_t &&key); + ~GpgKey() = default; + GpgKey(const gpgme_key_t &key) = delete; GpgKey(GpgKey &&k) noexcept; @@ -134,10 +139,16 @@ public: explicit operator gpgme_key_t() const { return _key_ref.get(); } private: - using KeyRefHandler = - std::unique_ptr<struct _gpgme_key, std::function<void(gpgme_key_t)>>; + struct __key_ref_deletor { + void operator()(gpgme_key_t _key) { + if (_key != nullptr) + gpgme_key_unref(_key); + } + }; + + using KeyRefHandler = std::unique_ptr<struct _gpgme_key, __key_ref_deletor>; - KeyRefHandler _key_ref; + KeyRefHandler _key_ref = nullptr; }; } // namespace GpgFrontend diff --git a/src/gpg/model/GpgKeySignature.h b/src/gpg/model/GpgKeySignature.h index c91ab794..9de6b074 100644 --- a/src/gpg/model/GpgKeySignature.h +++ b/src/gpg/model/GpgKeySignature.h @@ -27,6 +27,7 @@ #include "gpg/GpgConstants.h" +#include <boost/date_time.hpp> #include <string> namespace GpgFrontend { @@ -45,11 +46,11 @@ public: return gpgme_pubkey_algo_name(_signature_ref->pubkey_algo); } - [[nodiscard]] QDateTime create_time() const { - return QDateTime::fromTime_t(_signature_ref->timestamp); + [[nodiscard]] boost::gregorian::date create_time() const { + return boost::posix_time::from_time_t(_signature_ref->timestamp).date(); } - [[nodiscard]] QDateTime expire_time() const { - return QDateTime::fromTime_t(_signature_ref->expires); + [[nodiscard]] boost::gregorian::date expire_time() const { + return boost::posix_time::from_time_t(_signature_ref->expires).date(); } [[nodiscard]] std::string uid() const { return _signature_ref->uid; } diff --git a/src/gpg/model/GpgSubKey.h b/src/gpg/model/GpgSubKey.h index e474f5a9..c251141b 100644 --- a/src/gpg/model/GpgSubKey.h +++ b/src/gpg/model/GpgSubKey.h @@ -26,6 +26,7 @@ #include "gpg/GpgConstants.h" +#include <boost/date_time.hpp> #include <string> namespace GpgFrontend { @@ -64,12 +65,12 @@ public: [[nodiscard]] bool is_cardkey() const { return _subkey_ref->is_cardkey; } - [[nodiscard]] QDateTime timestamp() const { - return QDateTime::fromTime_t(_subkey_ref->timestamp); + [[nodiscard]] boost::gregorian::date timestamp() const { + return boost::posix_time::from_time_t(_subkey_ref->timestamp).date(); } - [[nodiscard]] QDateTime expires() const { - return QDateTime::fromTime_t(_subkey_ref->expires); + [[nodiscard]] boost::gregorian::date expires() const { + return boost::posix_time::from_time_t(_subkey_ref->expires).date(); } GpgSubKey() = default; diff --git a/src/gpg/result_analyse/DecryptResultAnalyse.cpp b/src/gpg/result_analyse/DecryptResultAnalyse.cpp index c18a0111..8ecb92b9 100644 --- a/src/gpg/result_analyse/DecryptResultAnalyse.cpp +++ b/src/gpg/result_analyse/DecryptResultAnalyse.cpp @@ -30,30 +30,30 @@ GpgFrontend::DecryptResultAnalyse::DecryptResultAnalyse(GpgError error, : error(error), result(std::move(result)) {} void GpgFrontend::DecryptResultAnalyse::do_analyse() { - stream << tr("[#] Decrypt Operation ").constData(); + stream << "[#] Decrypt Operation "; if (gpgme_err_code(error) == GPG_ERR_NO_ERROR) { - stream << tr("[Success]").constData() << std::endl; + stream << "[Success]" << std::endl; } else { - stream << tr("[Failed] ").constData() << gpgme_strerror(error) << std::endl; + stream << "[Failed] " << gpgme_strerror(error) << std::endl; setStatus(-1); if (result != nullptr && result->unsupported_algorithm != nullptr) { stream << "------------>" << std::endl; - stream << tr("Unsupported Algo: ").constData() - << result->unsupported_algorithm << std::endl; + stream << "Unsupported Algo: " << result->unsupported_algorithm + << std::endl; } } if (result != nullptr && result->recipients != nullptr) { stream << "------------>" << std::endl; if (result->file_name != nullptr) { - stream << tr("File Name: ").constData() << result->file_name << std::endl; + stream << "File Name: " << result->file_name << std::endl; stream << std::endl; } auto reci = result->recipients; if (reci != nullptr) - stream << tr("Recipient(s): ").constData() << std::endl; + stream << "Recipient(s): " << std::endl; while (reci != nullptr) { print_reci(stream, reci); reci = reci->next; @@ -67,7 +67,7 @@ void GpgFrontend::DecryptResultAnalyse::do_analyse() { bool GpgFrontend::DecryptResultAnalyse::print_reci(std::stringstream &stream, gpgme_recipient_t reci) { bool keyFound = true; - stream << tr(" {>} Recipient: ").constData(); + stream << " {>} Recipient: "; auto key = GpgFrontend::GpgKeyGetter::GetInstance().GetKey(reci->keyid); if (key.good()) { @@ -83,9 +83,9 @@ bool GpgFrontend::DecryptResultAnalyse::print_reci(std::stringstream &stream, stream << std::endl; - stream << tr(" Keu ID: ").constData() << key.id().c_str() << std::endl; - stream << tr(" Public Algo: ").constData() - << gpgme_pubkey_algo_name(reci->pubkey_algo) << std::endl; + stream << " Keu ID: " << key.id().c_str() << std::endl; + stream << " Public Algo: " << gpgme_pubkey_algo_name(reci->pubkey_algo) + << std::endl; return keyFound; } diff --git a/src/gpg/result_analyse/DecryptResultAnalyse.h b/src/gpg/result_analyse/DecryptResultAnalyse.h index c9696159..44640f7a 100644 --- a/src/gpg/result_analyse/DecryptResultAnalyse.h +++ b/src/gpg/result_analyse/DecryptResultAnalyse.h @@ -31,7 +31,6 @@ namespace GpgFrontend { class DecryptResultAnalyse : public ResultAnalyse { - Q_OBJECT public: explicit DecryptResultAnalyse(GpgError error, GpgDecrResult result); diff --git a/src/gpg/result_analyse/EncryptResultAnalyse.cpp b/src/gpg/result_analyse/EncryptResultAnalyse.cpp index 898550fd..6659f1df 100644 --- a/src/gpg/result_analyse/EncryptResultAnalyse.cpp +++ b/src/gpg/result_analyse/EncryptResultAnalyse.cpp @@ -29,7 +29,7 @@ GpgFrontend::EncryptResultAnalyse::EncryptResultAnalyse(GpgError error, : error(error), result(std::move(result)) {} void GpgFrontend::EncryptResultAnalyse::do_analyse() { - qDebug() << "Start Encrypt Result Analyse"; + LOG(INFO) << "Start Encrypt Result Analyse"; stream << "[#] Encrypt Operation "; @@ -43,12 +43,11 @@ void GpgFrontend::EncryptResultAnalyse::do_analyse() { if (!~status) { stream << "------------>" << std::endl; if (result != nullptr) { - stream << tr("Invalid Recipients: ").constData() << std::endl; + stream << "Invalid Recipients: " << std::endl; auto inv_reci = result->invalid_recipients; while (inv_reci != nullptr) { - stream << tr("Fingerprint: ").constData() << inv_reci->fpr << std::endl; - stream << tr("Reason: ").constData() << gpgme_strerror(inv_reci->reason) - << std::endl; + stream << "Fingerprint: " << inv_reci->fpr << std::endl; + stream << "Reason: " << gpgme_strerror(inv_reci->reason) << std::endl; stream << std::endl; inv_reci = inv_reci->next; diff --git a/src/gpg/result_analyse/EncryptResultAnalyse.h b/src/gpg/result_analyse/EncryptResultAnalyse.h index 31b83753..9b6bc788 100644 --- a/src/gpg/result_analyse/EncryptResultAnalyse.h +++ b/src/gpg/result_analyse/EncryptResultAnalyse.h @@ -30,7 +30,6 @@ namespace GpgFrontend { class EncryptResultAnalyse : public ResultAnalyse { - Q_OBJECT public: explicit EncryptResultAnalyse(GpgError error, GpgEncrResult result); diff --git a/src/gpg/result_analyse/ResultAnalyse.h b/src/gpg/result_analyse/ResultAnalyse.h index f10c1129..d2cf0037 100644 --- a/src/gpg/result_analyse/ResultAnalyse.h +++ b/src/gpg/result_analyse/ResultAnalyse.h @@ -30,8 +30,7 @@ #include <string> namespace GpgFrontend { -class ResultAnalyse : public QObject { - Q_OBJECT +class ResultAnalyse { public: ResultAnalyse() = default; diff --git a/src/gpg/result_analyse/SignResultAnalyse.cpp b/src/gpg/result_analyse/SignResultAnalyse.cpp index 21e18bfa..fcad9060 100644 --- a/src/gpg/result_analyse/SignResultAnalyse.cpp +++ b/src/gpg/result_analyse/SignResultAnalyse.cpp @@ -30,76 +30,75 @@ GpgFrontend::SignResultAnalyse::SignResultAnalyse(GpgError error, : error(error), result(std::move(result)) {} void GpgFrontend::SignResultAnalyse::do_analyse() { - qDebug() << "Start Sign Result Analyse"; + LOG(INFO) << "Start Sign Result Analyse"; - stream << tr("[#] Sign Operation ").constData(); + stream << "[#] Sign Operation "; if (gpgme_err_code(error) == GPG_ERR_NO_ERROR) - stream << tr("[Success]").constData() << std::endl; + stream << "[Success]" << std::endl; else { - stream << tr("[Failed] ").constData() << gpgme_strerror(error) << std::endl; + stream << "[Failed] " << gpgme_strerror(error) << std::endl; setStatus(-1); } if (result != nullptr && (result->signatures != nullptr || result->invalid_signers != nullptr)) { - qDebug() << "Sign Result Analyse Getting Result"; + LOG(INFO) << "Sign Result Analyse Getting Result"; stream << "------------>" << std::endl; auto new_sign = result->signatures; while (new_sign != nullptr) { - stream << tr("[>] New Signature: ").constData() << std::endl; + stream << "[>] New Signature: " << std::endl; - qDebug() << "Signers Fingerprint: " << new_sign->fpr; + LOG(INFO) << "Signers Fingerprint: " << new_sign->fpr; - stream << tr(" Sign Mode: ").constData(); + stream << " Sign Mode: "; if (new_sign->type == GPGME_SIG_MODE_NORMAL) - stream << tr("Normal").constData(); + stream << "Normal"; else if (new_sign->type == GPGME_SIG_MODE_CLEAR) - stream << tr("Clear").constData(); + stream << "Clear"; else if (new_sign->type == GPGME_SIG_MODE_DETACH) - stream << tr("Detach").constData(); + stream << "Detach"; stream << std::endl; auto singerKey = GpgFrontend::GpgKeyGetter::GetInstance().GetKey(new_sign->fpr); if (singerKey.good()) { - stream << tr(" Signer: ").constData() - << singerKey.uids()->front().uid() << std::endl; - } else { - stream << tr(" Signer: ").constData() << tr("<unknown>").constData() + stream << " Signer: " << singerKey.uids()->front().uid() << std::endl; + } else { + stream << " Signer: " + << "<unknown>" << std::endl; } - stream << tr(" Public Key Algo: ").constData() + stream << " Public Key Algo: " << gpgme_pubkey_algo_name(new_sign->pubkey_algo) << std::endl; - stream << tr(" Hash Algo: ").constData() - << gpgme_hash_algo_name(new_sign->hash_algo) << std::endl; - stream - << tr(" Date & Time: ").constData() - << QDateTime::fromTime_t(new_sign->timestamp).toString().constData() - << std::endl; + stream << " Hash Algo: " << gpgme_hash_algo_name(new_sign->hash_algo) + << std::endl; + stream << " Date & Time: " + << boost::posix_time::to_iso_string( + boost::posix_time::from_time_t(new_sign->timestamp)) + << std::endl; stream << std::endl; new_sign = new_sign->next; } - qDebug() << "Sign Result Analyse Getting Invalid Signer"; + LOG(INFO) << "Sign Result Analyse Getting Invalid Signer"; auto invalid_signer = result->invalid_signers; if (invalid_signer != nullptr) - stream << tr("Invalid Signers: ").constData() << std::endl; + stream << "Invalid Signers: " << std::endl; while (invalid_signer != nullptr) { setStatus(0); - stream << tr("[>] Signer: ").constData() << std::endl; - stream << tr(" Fingerprint: ").constData() << invalid_signer->fpr + stream << "[>] Signer: " << std::endl; + stream << " Fingerprint: " << invalid_signer->fpr << std::endl; + stream << " Reason: " << gpgme_strerror(invalid_signer->reason) << std::endl; - stream << tr(" Reason: ").constData() - << gpgme_strerror(invalid_signer->reason) << std::endl; stream << std::endl; invalid_signer = invalid_signer->next; diff --git a/src/gpg/result_analyse/SignResultAnalyse.h b/src/gpg/result_analyse/SignResultAnalyse.h index 0d9eb3b6..4a1234fe 100644 --- a/src/gpg/result_analyse/SignResultAnalyse.h +++ b/src/gpg/result_analyse/SignResultAnalyse.h @@ -30,7 +30,6 @@ namespace GpgFrontend { class SignResultAnalyse : public ResultAnalyse { - Q_OBJECT public: explicit SignResultAnalyse(GpgError error, GpgSignResult result); diff --git a/src/gpg/result_analyse/VerifyResultAnalyse.cpp b/src/gpg/result_analyse/VerifyResultAnalyse.cpp index b807de53..598329dd 100644 --- a/src/gpg/result_analyse/VerifyResultAnalyse.cpp +++ b/src/gpg/result_analyse/VerifyResultAnalyse.cpp @@ -32,14 +32,14 @@ GpgFrontend::VerifyResultAnalyse::VerifyResultAnalyse(GpgError error, : error(error), result(std::move(result)) {} void GpgFrontend::VerifyResultAnalyse::do_analyse() { - qDebug() << "Verify Result Analyse Started"; + LOG(INFO) << "Verify Result Analyse Started"; - stream << tr("[#] Verify Operation ").constData(); + stream << "[#] Verify Operation "; if (gpgme_err_code(error) == GPG_ERR_NO_ERROR) - stream << tr("[Success]").constData() << std::endl; + stream << "[Success]" << std::endl; else { - stream << tr("[Failed] ").constData() << gpgme_strerror(error) << std::endl; + stream << "[Failed] " << gpgme_strerror(error) << std::endl; setStatus(-1); } @@ -54,7 +54,8 @@ void GpgFrontend::VerifyResultAnalyse::do_analyse() { } stream << "[>] Signed On " - << QDateTime::fromTime_t(sign->timestamp).toString().constData() + << boost::posix_time::to_iso_string( + boost::posix_time::from_time_t(sign->timestamp)) << std::endl; stream << std::endl << "[>] Signatures:" << std::endl; @@ -62,93 +63,89 @@ void GpgFrontend::VerifyResultAnalyse::do_analyse() { bool canContinue = true; while (sign && canContinue) { - switch (gpg_err_code(sign->status)) { case GPG_ERR_BAD_SIGNATURE: - stream << tr("One or More Bad Signatures.").constData() << std::endl; + stream << "One or More Bad Signatures." << std::endl; canContinue = false; setStatus(-1); break; case GPG_ERR_NO_ERROR: - stream << tr("A ").constData(); + stream << "A "; if (sign->summary & GPGME_SIGSUM_GREEN) { - stream << tr("Good ").constData(); + stream << "Good "; } if (sign->summary & GPGME_SIGSUM_RED) { - stream << tr("Bad ").constData(); + stream << "Bad "; } if (sign->summary & GPGME_SIGSUM_SIG_EXPIRED) { - stream << tr("Expired ").constData(); + stream << "Expired "; } if (sign->summary & GPGME_SIGSUM_KEY_MISSING) { - stream << tr("Missing Key's ").constData(); + stream << "Missing Key's "; } if (sign->summary & GPGME_SIGSUM_KEY_REVOKED) { - stream << tr("Revoked Key's ").constData(); + stream << "Revoked Key's "; } if (sign->summary & GPGME_SIGSUM_KEY_EXPIRED) { - stream << tr("Expired Key's ").constData(); + stream << "Expired Key's "; } if (sign->summary & GPGME_SIGSUM_CRL_MISSING) { - stream << tr("Missing CRL's ").constData(); + stream << "Missing CRL's "; } if (sign->summary & GPGME_SIGSUM_VALID) { - stream << tr("Signature Fully Valid.").constData() << std::endl; + stream << "Signature Fully Valid." << std::endl; } else { - stream << tr("Signature Not Fully Valid.").constData() << std::endl; + stream << "Signature Not Fully Valid." << std::endl; } if (!(sign->status & GPGME_SIGSUM_KEY_MISSING)) { if (!print_signer(stream, sign)) setStatus(0); } else { - stream << tr("Key is NOT present with ID 0x").constData() << sign->fpr - << std::endl; + stream << "Key is NOT present with ID 0x" << sign->fpr << std::endl; } setStatus(1); break; case GPG_ERR_NO_PUBKEY: - stream << tr("A signature could NOT be verified due to a Missing Key\n") - .constData(); + stream << "A signature could NOT be verified due to a Missing " + "Key\n"; setStatus(-1); break; case GPG_ERR_CERT_REVOKED: - stream << tr("A signature is valid but the key used to " - "verify the signature has been revoked\n") - .constData(); + stream << "A signature is valid but the key used to " + "verify the signature has been revoked\n"; if (!print_signer(stream, sign)) { setStatus(0); } setStatus(-1); break; case GPG_ERR_SIG_EXPIRED: - stream << tr("A signature is valid but expired\n").constData(); + stream << "A signature is valid but expired\n"; if (!print_signer(stream, sign)) { setStatus(0); } setStatus(-1); break; case GPG_ERR_KEY_EXPIRED: - stream << tr("A signature is valid but the key used to " - "verify the signature has expired.\n") - .constData(); + stream << "A signature is valid but the key used to " + "verify the signature has expired.\n"; if (!print_signer(stream, sign)) { setStatus(0); } break; case GPG_ERR_GENERAL: - stream << tr("There was some other error which prevented " - "the signature verification.\n") - .constData(); + stream << "There was some other error which prevented " + "the signature verification.\n"; status = -1; canContinue = false; break; default: - stream << tr("Error for key with fingerprint ").constData() - << GpgFrontend::beautify_fingerprint(sign->fpr); + auto fpr = std::string(sign->fpr); + stream << "Error for key with fingerprint " + << GpgFrontend::beautify_fingerprint(fpr); setStatus(-1); } stream << std::endl; @@ -164,20 +161,20 @@ bool GpgFrontend::VerifyResultAnalyse::print_signer(std::stringstream &stream, auto key = GpgFrontend::GpgKeyGetter::GetInstance().GetKey(sign->fpr); if (!key.good()) { - stream << tr(" Signed By: ").constData() << tr("<unknown>").constData() - << std::endl; + stream << " Signed By: " + << "<unknown>" << std::endl; setStatus(0); keyFound = false; } else { - stream << tr(" Signed By: ").constData() << key.uids()->front().uid() - << std::endl; + stream << " Signed By: " << key.uids()->front().uid() << std::endl; } - stream << tr(" Public Key Algo: ").constData() - << gpgme_pubkey_algo_name(sign->pubkey_algo) << std::endl; - stream << tr(" Hash Algo: ").constData() - << gpgme_hash_algo_name(sign->hash_algo) << std::endl; - stream << tr(" Date & Time: ").constData() - << QDateTime::fromTime_t(sign->timestamp).toString().constData() + stream << " Public Key Algo: " << gpgme_pubkey_algo_name(sign->pubkey_algo) + << std::endl; + stream << " Hash Algo: " << gpgme_hash_algo_name(sign->hash_algo) + << std::endl; + stream << " Date & Time: " + << boost::posix_time::to_iso_string( + boost::posix_time::from_time_t(sign->timestamp)) << std::endl; stream << std::endl; return keyFound; |