diff options
author | Saturneric <[email protected]> | 2021-09-10 23:58:09 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-09-10 16:06:21 +0000 |
commit | 4e076fa1982812ea7d1222c7e981879799ca8853 (patch) | |
tree | 1b96ae4a6296ca0f7b9df8ce3a3c7339643bf1f0 /src | |
parent | Adjust the code structure. (diff) | |
download | GpgFrontend-4e076fa1982812ea7d1222c7e981879799ca8853.tar.gz GpgFrontend-4e076fa1982812ea7d1222c7e981879799ca8853.zip |
Continue to optimize and improve the code.
Diffstat (limited to '')
-rw-r--r-- | src/GpgFrontendBuildInfo.h | 10 | ||||
-rw-r--r-- | src/gpg/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/gpg/GpgConstants.cpp | 13 | ||||
-rw-r--r-- | src/gpg/GpgConstants.h | 1 | ||||
-rw-r--r-- | src/gpg/GpgGenKeyInfo.cpp | 20 | ||||
-rw-r--r-- | src/gpg/function/BasicOperator.cpp | 3 | ||||
-rw-r--r-- | src/gpg/function/GpgFileOpera.cpp | 227 | ||||
-rw-r--r-- | src/gpg/function/GpgFileOpera.h | 3 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyGetter.cpp | 4 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyImportExportor.cpp | 10 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyManager.cpp | 47 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyManager.h | 6 | ||||
-rw-r--r-- | src/gpg/function/UidOperator.cpp | 17 | ||||
-rw-r--r-- | src/gpg/model/GpgKey.cpp | 4 |
14 files changed, 162 insertions, 209 deletions
diff --git a/src/GpgFrontendBuildInfo.h b/src/GpgFrontendBuildInfo.h index 54270bb4..1155a6c9 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 "f8668adc7fe319896a3444409c076bd61c690c84" +#define GIT_COMMIT_HASH "22b454912a8993a6897b21db2baf345961711cb5" /** * 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_f8668adc7fe319896a3444409c076bd61c690c84" +#define BUILD_VERSION "1.3.1_Linux-5.4.0-81-generic_x86_64_Release" +#define GIT_VERSION "develop-core_22b454912a8993a6897b21db2baf345961711cb5" /** * Build Information */ -#define BUILD_FLAG 1 -#define BUILD_TIMESTAMP "2021-09-05 21:22:47" +#define BUILD_FLAG 0 +#define BUILD_TIMESTAMP "2021-09-10 23:57:04" #endif // GPGFRONTEND_BUILD_INFO_H_IN diff --git a/src/gpg/CMakeLists.txt b/src/gpg/CMakeLists.txt index 01956fc0..9c32761a 100644 --- a/src/gpg/CMakeLists.txt +++ b/src/gpg/CMakeLists.txt @@ -19,13 +19,17 @@ message(STATUS "Third Party Libraries " ${THIRD_PARTY_LIBS}) if (MINGW) message(STATUS "Link GPG Static Library For MINGW") target_link_libraries(gpg_core ${THIRD_PARTY_LIBS} + Boost::date_time gpgme gpg-error assuan wsock32) elseif(APPLE) message(STATUS "Link GPG Static Library For macOS") target_link_libraries(gpg_core ${THIRD_PARTY_LIBS} + Boost::date_time libgpgme.a libgpg-error.a libassuan.a) else() message(STATUS "Link GPG Static Library For Unix") target_link_libraries(gpg_core ${THIRD_PARTY_LIBS} - libgpgme.a libgpg-error.a libassuan.a pthread) + libgpgme.a libgpg-error.a libassuan.a + Boost::date_time + pthread) endif() diff --git a/src/gpg/GpgConstants.cpp b/src/gpg/GpgConstants.cpp index 937033eb..2ba04ed2 100644 --- a/src/gpg/GpgConstants.cpp +++ b/src/gpg/GpgConstants.cpp @@ -23,6 +23,7 @@ */ #include "gpg/GpgConstants.h" +#include <gpg-error.h> const char *GpgFrontend::GpgConstants::PGP_CRYPT_BEGIN = "-----BEGIN PGP MESSAGE-----"; @@ -40,7 +41,6 @@ const char *GpgFrontend::GpgConstants::GPG_FRONTEND_SHORT_CRYPTO_HEAD = "GpgF_Scpt://"; 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) { LOG(ERROR) << "[Error " << gpg_err_code(err) << "] Source: " << gpgme_strsource(err) @@ -49,10 +49,19 @@ gpgme_error_t GpgFrontend::check_gpg_error(gpgme_error_t err) { return err; } +gpg_err_code_t GpgFrontend::check_gpg_error_2_err_code(gpgme_error_t err) { + auto err_code = gpg_err_code(err); + if (err_code != GPG_ERR_NO_ERROR) { + LOG(ERROR) << "[Error " << gpg_err_code(err) + << "] Source: " << gpgme_strsource(err) + << " Description: " << gpgme_strerror(err); + } + return err_code; +} + // error-handling 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) { LOG(ERROR) << "[Error " << gpg_err_code(err) << "] Source: " << gpgme_strsource(err) diff --git a/src/gpg/GpgConstants.h b/src/gpg/GpgConstants.h index 6237a974..e82810b0 100644 --- a/src/gpg/GpgConstants.h +++ b/src/gpg/GpgConstants.h @@ -68,6 +68,7 @@ using GpgVerifyResult = // Error Info Printer GpgError check_gpg_error(GpgError err); GpgError check_gpg_error(GpgError gpgmeError, const std::string &comment); +gpg_err_code_t check_gpg_error_2_err_code(gpgme_error_t err); // Fingerprint std::string beautify_fingerprint(BypeArrayRef fingerprint); diff --git a/src/gpg/GpgGenKeyInfo.cpp b/src/gpg/GpgGenKeyInfo.cpp index f4dc777a..e45f67c8 100644 --- a/src/gpg/GpgGenKeyInfo.cpp +++ b/src/gpg/GpgGenKeyInfo.cpp @@ -23,6 +23,10 @@ */ #include "gpg/GpgGenKeyInfo.h" +#include <boost/date_time/gregorian/greg_date.hpp> +#include <boost/date_time/gregorian/greg_duration.hpp> +#include <boost/date_time/gregorian/gregorian_types.hpp> +#include <boost/date_time/posix_time/ptime.hpp> #include <string> #include <vector> @@ -34,8 +38,6 @@ const std::vector<std::string> GpgFrontend::GenKeyInfo::SupportedSubkeyAlgo = { void GpgFrontend::GenKeyInfo::setAlgo(const std::string &m_algo) { - qDebug() << "set algo " << m_algo.c_str(); - reset_options(); if (!this->subKey) { @@ -141,17 +143,19 @@ void GpgFrontend::GenKeyInfo::setKeySize(int m_key_size) { GenKeyInfo::keySize = m_key_size; } -void GpgFrontend::GenKeyInfo::setExpired(const QDateTime &m_expired) { - auto current = QDateTime::currentDateTime(); - if (isNonExpired() && m_expired < current.addYears(2)) { +void GpgFrontend::GenKeyInfo::setExpired( + const boost::gregorian::date &m_expired) { + using namespace boost::gregorian; + auto current = day_clock::local_day(); + if (isNonExpired() && m_expired < current + years(2)) { GenKeyInfo::expired = m_expired; } } void GpgFrontend::GenKeyInfo::setNonExpired(bool m_non_expired) { - if (!m_non_expired) { - this->expired = QDateTime(QDateTime::fromTime_t(0)); - } + using namespace boost::posix_time; + if (!m_non_expired) + this->expired = from_time_t(0).date(); GenKeyInfo::nonExpired = m_non_expired; } diff --git a/src/gpg/function/BasicOperator.cpp b/src/gpg/function/BasicOperator.cpp index 295b6433..268b7c6d 100644 --- a/src/gpg/function/BasicOperator.cpp +++ b/src/gpg/function/BasicOperator.cpp @@ -77,7 +77,6 @@ GpgFrontend::BasicOperator::Verify(BypeArrayRef &in_buffer, BypeArrayPtr &sig_buffer, GpgVerifyResult &result) const { gpgme_error_t err; - gpgme_verify_result_t m_result; GpgData data_in(in_buffer.data(), in_buffer.size()); @@ -204,7 +203,7 @@ GpgFrontend::BasicOperator::GetSigners() { auto signers = std::make_unique<std::vector<GpgKey>>(); for (auto i = 0; i < count; i++) { auto key = GpgKey(gpgme_signers_enum(ctx, i)); - signers->push_back(std::move(GpgKey(std::move(key)))); + signers->push_back(GpgKey(std::move(key))); } return signers; } diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 726c2695..71e60f76 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -22,83 +22,101 @@ * */ #include "gpg/function/GpgFileOpera.h" +#include "GpgConstants.h" #include "gpg/function/BasicOperator.h" +#include <boost/process/detail/config.hpp> #include <filesystem> +#include <iterator> #include <memory> +#include <string> -GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { - +std::string read_all_data_in_file(const std::string &path) { using namespace std::filesystem; class path file_info(path.c_str()); if (!exists(file_info) || !is_regular_file(path)) throw std::runtime_error("no permission"); - QFile in_file(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) + std::ifstream in_file; + in_file.open(path, std::ios::in); + if (!in_file.good()) throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - auto out_buffer = std::make_unique<QByteArray>(); + std::istreambuf_iterator<char> begin(in_file); + std::istreambuf_iterator<char> end; + std::string in_buffer(begin, end); in_file.close(); + return in_buffer; +} + +void write_buufer_to_file(const std::string &path, + const std::string &out_buffer) { + std::ofstream out_file(path); + out_file.open(path.c_str(), std::ios::out); + if (!out_file.good()) + throw std::runtime_error("cannot open file"); + out_file.write(out_buffer.c_str(), out_buffer.size()); + out_file.close(); +} + +GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( + KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { + + std::string in_buffer = read_all_data_in_file(path); + std::unique_ptr<std::string> out_buffer; auto err = BasicOperator::GetInstance().Encrypt(keys, in_buffer, out_buffer, result); - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) - return err; + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - QFile out_file((path + ".asc").c_str()); + write_buufer_to_file(path + ".asc", *out_buffer); + return err; +} - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); +std::string get_file_extension(const std::string &path) { + // Create a Path object from given string + std::filesystem::path path_obj(path); + // Check if file name in the path object has extension + if (path_obj.has_extension()) { + // Fetch the extension from path object and return + return path_obj.extension().string(); + } + // In case of no extension return empty string + return std::string(); +} - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); - return err; +std::string get_file_name_with_path(const std::string &path) { + // Create a Path object from given string + std::filesystem::path path_obj(path); + // Check if file name in the path object has extension + if (path_obj.has_filename()) { + // Fetch the extension from path object and return + return path_obj.parent_path() / path_obj.filename(); + } + // In case of no extension return empty string + throw std::runtime_error("invalid file path"); } GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile(const std::string &path, GpgDecrResult &result) { - QFileInfo file_info(path); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file(path); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - auto out_buffer = std::make_unique<QByteArray>(); - in_file.close(); + std::string in_buffer = read_all_data_in_file(path); + std::unique_ptr<std::string> out_buffer; auto err = BasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); - assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - QString out_file_name, file_extension = file_info.suffix(); + std::string out_file_name = get_file_name_with_path(path), + file_extension = get_file_extension(path); - if (file_extension == "asc" || file_extension == "gpg") { - int pos = path.lastIndexOf('.'); - out_file_name = path.left(pos); - } else - out_file_name = path + ".out"; - - QFile out_file(out_file_name); + if (!(file_extension == ".asc" || file_extension == ".gpg")) + out_file_name += ".out"; - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); - - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(out_file_name, *out_buffer); return err; } @@ -107,31 +125,15 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, const std::string &path, GpgSignResult &result) { - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - auto out_buffer = std::make_unique<QByteArray>(); - in_file.close(); + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr<std::string> out_buffer; auto err = BasicOperator::GetInstance().Sign(keys, in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); - assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); - QFile out_file((path + ".sig").c_str()); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); - - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(path + ".sig", *out_buffer); return err; } @@ -139,33 +141,17 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string &path, GpgVerifyResult &result) { - qDebug() << "Verify File Path" << path.c_str(); - - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr<std::string> sign_buffer = nullptr; - QByteArray in_buffer = in_file.readAll(); - std::unique_ptr<QByteArray> sign_buffer = nullptr; - - if (file_info.suffix() == "gpg") { + if (get_file_extension(path) == ".gpg") { auto err = BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); return err; } else { - QFile sign_file; - sign_file.setFileName((path + ".sig").c_str()); - if (!sign_file.open(QIODevice::ReadOnly)) { - throw std::runtime_error("cannot open file"); - } - - auto sign_buffer = std::make_unique<QByteArray>(sign_file.readAll()); - in_file.close(); + auto sign_buffer = + std::make_unique<std::string>(read_all_data_in_file(path + ".sig")); auto err = BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); @@ -179,21 +165,8 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( KeyArgsList &keys, const std::string &path, GpgEncrResult &encr_res, GpgSignResult &sign_res) { - qDebug() << "Encrypt Sign File Path" << path.c_str(); - - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file; - in_file.setFileName(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - in_file.close(); - std::unique_ptr<QByteArray> out_buffer = nullptr; + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr<std::string> out_buffer = nullptr; // TODO Fill the vector std::vector<GpgKey> signerKeys; @@ -202,17 +175,9 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( auto err = BasicOperator::GetInstance().EncryptSign( keys, signerKeys, in_buffer, out_buffer, encr_res, sign_res); - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) - return err; - - QFile out_file((path + ".gpg").c_str()); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); - - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(path + ".gpg", *out_buffer); return err; } @@ -222,44 +187,20 @@ GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string &path, GpgDecrResult &decr_res, GpgVerifyResult &verify_res) { - qDebug() << "Decrypt Verify File Path" << path.c_str(); - - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file; - in_file.setFileName(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - in_file.close(); - std::unique_ptr<QByteArray> out_buffer = nullptr; + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr<std::string> out_buffer = nullptr; auto err = BasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, decr_res, verify_res); - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) - return err; - - std::string out_file_name, - file_extension = file_info.suffix().toUtf8().constData(); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - if (file_extension == "asc" || file_extension == "gpg") { - int pos = path.find_last_of('.'); - out_file_name = path.substr(0, pos); - } else - out_file_name = (path + ".out").c_str(); + std::string out_file_name = get_file_name_with_path(path), + file_extension = get_file_extension(path); - QFile out_file(out_file_name.c_str()); - - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); + if (!(file_extension == ".asc" || file_extension == ".gpg")) + out_file_name = path + ".out"; - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(out_file_name, *out_buffer); return err; } diff --git a/src/gpg/function/GpgFileOpera.h b/src/gpg/function/GpgFileOpera.h index 5cdd69bf..582dbbd2 100644 --- a/src/gpg/function/GpgFileOpera.h +++ b/src/gpg/function/GpgFileOpera.h @@ -48,9 +48,6 @@ public: GpgError DecryptVerifyFile(const std::string &path, GpgDecrResult &decr_res, GpgVerifyResult &verify_res); - -private: - GpgContext &ctx = GpgContext::GetInstance(); }; } // namespace GpgFrontend diff --git a/src/gpg/function/GpgKeyGetter.cpp b/src/gpg/function/GpgKeyGetter.cpp index f8e361bd..88f80fbf 100644 --- a/src/gpg/function/GpgKeyGetter.cpp +++ b/src/gpg/function/GpgKeyGetter.cpp @@ -30,14 +30,14 @@ GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetKey(const std::string &fpr) { 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))); + return GpgKey(std::move(_p_key)); } GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetPubkey(const std::string &fpr) { gpgme_key_t _p_key; gpgme_get_key(ctx, fpr.c_str(), &_p_key, 0); - return std::move(GpgKey(std::move(_p_key))); + return GpgKey(std::move(_p_key)); } GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::FetchKey() { diff --git a/src/gpg/function/GpgKeyImportExportor.cpp b/src/gpg/function/GpgKeyImportExportor.cpp index 37b12650..79d0c36a 100644 --- a/src/gpg/function/GpgKeyImportExportor.cpp +++ b/src/gpg/function/GpgKeyImportExportor.cpp @@ -1,5 +1,6 @@ #include "gpg/function/GpgKeyImportExportor.h" #include "GpgConstants.h" +#include <gpg-error.h> /** * Import key pair @@ -51,7 +52,7 @@ bool GpgFrontend::GpgKeyImportExportor::ExportKeys( LOG(INFO) << "exportKeys read_bytes" << gpgme_data_seek(data_out, 0, SEEK_END); - auto temp_out_buffer = std::move(data_out.Read2Buffer()); + auto temp_out_buffer = data_out.Read2Buffer(); std::swap(out_buffer, temp_out_buffer); } @@ -88,10 +89,11 @@ bool GpgFrontend::GpgKeyImportExportor::ExportSecretKey( GpgData data_out; // export private key to outBuffer - gpgme_error_t error = + gpgme_error_t err = gpgme_op_export_keys(ctx, target_key, GPGME_EXPORT_MODE_SECRET, data_out); - auto temp_out_buffer = std::move(data_out.Read2Buffer()); + auto temp_out_buffer = data_out.Read2Buffer(); std::swap(out_buffer, temp_out_buffer); - return true; + + return check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR; } diff --git a/src/gpg/function/GpgKeyManager.cpp b/src/gpg/function/GpgKeyManager.cpp index 9e0f610e..a6697bc4 100644 --- a/src/gpg/function/GpgKeyManager.cpp +++ b/src/gpg/function/GpgKeyManager.cpp @@ -25,29 +25,28 @@ #include "gpg/function/GpgKeyManager.h" #include "gpg/function/BasicOperator.h" #include "gpg/function/GpgKeyGetter.h" +#include <boost/date_time/posix_time/conversion.hpp> +#include <string> + +bool GpgFrontend::GpgKeyManager::signKey( + const GpgFrontend::GpgKey &target, GpgFrontend::KeyArgsList &keys, + const std::string &uid, std::unique_ptr<boost::gregorian::date> &expires) { + using namespace boost::posix_time; -bool GpgFrontend::GpgKeyManager::signKey(const GpgFrontend::GpgKey &target, - GpgFrontend::KeyArgsList &keys, - const QString &uid, - std::unique_ptr<QDateTime> &expires) { BasicOperator::GetInstance().SetSigners(keys); unsigned int flags = 0; - unsigned int expires_time_t = 0; + if (expires == nullptr) flags |= GPGME_KEYSIGN_NOEXPIRE; else - expires_time_t = QDateTime::currentDateTime().secsTo(*expires); + expires_time_t = to_time_t(ptime(*expires)); - auto err = check_gpg_error(gpgme_op_keysign(ctx, gpgme_key_t(target), - uid.toUtf8().constData(), - expires_time_t, flags)); + auto err = check_gpg_error(gpgme_op_keysign( + ctx, gpgme_key_t(target), uid.c_str(), expires_time_t, flags)); - if (gpg_err_code(err) == GPG_ERR_NO_ERROR) - return true; - else - return false; + return check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR; } bool GpgFrontend::GpgKeyManager::revSign( @@ -60,20 +59,18 @@ bool GpgFrontend::GpgKeyManager::revSign( auto err = check_gpg_error(gpgme_op_revsig(ctx, gpgme_key_t(key), gpgme_key_t(signing_key), signature.uid().data(), 0)); - if (gpg_err_code(err) == GPG_ERR_NO_ERROR) - return true; - else - return false; + return check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR; } bool GpgFrontend::GpgKeyManager::setExpire( const GpgFrontend::GpgKey &key, std::unique_ptr<GpgSubKey> &subkey, - std::unique_ptr<QDateTime> &expires) { + std::unique_ptr<boost::gregorian::date> &expires) { + using namespace boost::posix_time; + unsigned long expires_time = 0; - if (expires != nullptr) { - qDebug() << "Expire Datetime" << expires->toString(); - expires_time = QDateTime::currentDateTime().secsTo(*expires); - } + + if (expires != nullptr) + expires_time = to_time_t(ptime(*expires)); const char *sub_fprs = nullptr; @@ -82,8 +79,6 @@ bool GpgFrontend::GpgKeyManager::setExpire( auto err = check_gpg_error( gpgme_op_setexpire(ctx, gpgme_key_t(key), expires_time, sub_fprs, 0)); - if (gpg_err_code(err) == GPG_ERR_NO_ERROR) - return true; - else - return false; + + return check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR; } diff --git a/src/gpg/function/GpgKeyManager.h b/src/gpg/function/GpgKeyManager.h index 87557baf..6d57c7d7 100644 --- a/src/gpg/function/GpgKeyManager.h +++ b/src/gpg/function/GpgKeyManager.h @@ -40,13 +40,13 @@ public: * @param expires expire date and time of the signature * @return if successful */ - bool signKey(const GpgKey &target, KeyArgsList &keys, const QString &uid, - std::unique_ptr<QDateTime> &expires); + bool signKey(const GpgKey &target, KeyArgsList &keys, const std::string &uid, + std::unique_ptr<boost::gregorian::date> &expires); bool revSign(const GpgKey &key, const GpgKeySignature &signature); bool setExpire(const GpgKey &key, std::unique_ptr<GpgSubKey> &subkey, - std::unique_ptr<QDateTime> &expires); + std::unique_ptr<boost::gregorian::date> &expires); private: GpgContext &ctx = GpgContext::GetInstance(); diff --git a/src/gpg/function/UidOperator.cpp b/src/gpg/function/UidOperator.cpp index 937396d4..d0058540 100644 --- a/src/gpg/function/UidOperator.cpp +++ b/src/gpg/function/UidOperator.cpp @@ -24,14 +24,15 @@ #include "gpg/function/UidOperator.h" +#include "boost/format.hpp" + bool GpgFrontend::UidOperator::addUID(const GpgFrontend::GpgKey &key, const GpgFrontend::GpgUID &uid) { - QString userid = - QString("%1 (%3) <%2>") - .arg(uid.name().c_str(), uid.email().c_str(), uid.comment().c_str()); - auto err = - gpgme_op_adduid(ctx, gpgme_key_t(key), userid.toUtf8().constData(), 0); - if (gpgme_err_code(err) == GPG_ERR_NO_ERROR) + auto userid = (boost::format("%1% (%2%) <%3%>") % uid.name() % uid.comment() % + uid.email()) + .str(); + auto err = gpgme_op_adduid(ctx, gpgme_key_t(key), userid.c_str(), 0); + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) return true; else return false; @@ -41,7 +42,7 @@ bool GpgFrontend::UidOperator::revUID(const GpgFrontend::GpgKey &key, const GpgFrontend::GpgUID &uid) { auto err = check_gpg_error( gpgme_op_revuid(ctx, gpgme_key_t(key), uid.uid().c_str(), 0)); - if (gpgme_err_code(err) == GPG_ERR_NO_ERROR) + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) return true; else return false; @@ -51,7 +52,7 @@ bool GpgFrontend::UidOperator::setPrimaryUID(const GpgFrontend::GpgKey &key, const GpgFrontend::GpgUID &uid) { auto err = check_gpg_error(gpgme_op_set_uid_flag( ctx, gpgme_key_t(key), uid.uid().c_str(), "primary", nullptr)); - if (gpgme_err_code(err) == GPG_ERR_NO_ERROR) + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) return true; else return false; diff --git a/src/gpg/model/GpgKey.cpp b/src/gpg/model/GpgKey.cpp index fa52c6f2..a0511051 100644 --- a/src/gpg/model/GpgKey.cpp +++ b/src/gpg/model/GpgKey.cpp @@ -38,7 +38,7 @@ GpgFrontend::GpgKey::subKeys() const { auto p_keys = std::make_unique<std::vector<GpgSubKey>>(); auto next = _key_ref->subkeys; while (next != nullptr) { - p_keys->push_back(std::move(GpgSubKey(next))); + p_keys->push_back(GpgSubKey(next)); next = next->next; } return p_keys; @@ -49,7 +49,7 @@ GpgFrontend::GpgKey::uids() const { auto p_uids = std::make_unique<std::vector<GpgUID>>(); auto uid_next = _key_ref->uids; while (uid_next != nullptr) { - p_uids->push_back(std::move(GpgUID(uid_next))); + p_uids->push_back(GpgUID(uid_next)); uid_next = uid_next->next; } return p_uids; |